Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-aat-layout-lcar-table.hh
1 /*
2  * Copyright © 2018  Ebrahim Byagowi
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  */
24 #ifndef HB_AAT_LAYOUT_LCAR_TABLE_HH
25 #define HB_AAT_LAYOUT_LCAR_TABLE_HH
26
27 #include "hb-open-type.hh"
28 #include "hb-aat-layout-common.hh"
29
30 /*
31  * lcar -- Ligature caret
32  * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6lcar.html
33  */
34 #define HB_AAT_TAG_lcar HB_TAG('l','c','a','r')
35
36
37 namespace AAT {
38
39 typedef ArrayOf<HBINT16> LigCaretClassEntry;
40
41 struct lcarFormat0
42 {
43   unsigned int get_lig_carets (hb_font_t      *font,
44                                hb_direction_t  direction,
45                                hb_codepoint_t  glyph,
46                                unsigned int    start_offset,
47                                unsigned int   *caret_count /* IN/OUT */,
48                                hb_position_t  *caret_array /* OUT */,
49                                const void     *base) const
50   {
51     const OffsetTo<LigCaretClassEntry>* entry_offset = lookupTable.get_value (glyph,
52                                                                               font->face->get_num_glyphs ());
53     const LigCaretClassEntry& array = entry_offset ? base+*entry_offset : Null (LigCaretClassEntry);
54     if (caret_count)
55     {
56       hb_array_t<const HBINT16> arr = array.sub_array (start_offset, caret_count);
57       for (unsigned int i = 0; i < arr.length; ++i)
58         caret_array[i] = font->em_scale_dir (arr[i], direction);
59     }
60     return array.len;
61   }
62
63   bool sanitize (hb_sanitize_context_t *c, const void *base) const
64   {
65     TRACE_SANITIZE (this);
66     return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
67   }
68
69   protected:
70   Lookup<OffsetTo<LigCaretClassEntry>>
71                 lookupTable;    /* data Lookup table associating glyphs */
72   public:
73   DEFINE_SIZE_MIN (2);
74 };
75
76 struct lcarFormat1
77 {
78   unsigned int get_lig_carets (hb_font_t      *font,
79                                hb_direction_t  direction,
80                                hb_codepoint_t  glyph,
81                                unsigned int    start_offset,
82                                unsigned int   *caret_count /* IN/OUT */,
83                                hb_position_t  *caret_array /* OUT */,
84                                const void     *base) const
85   {
86     const OffsetTo<LigCaretClassEntry>* entry_offset = lookupTable.get_value (glyph,
87                                                                               font->face->get_num_glyphs ());
88     const LigCaretClassEntry& array = entry_offset ? base+*entry_offset : Null (LigCaretClassEntry);
89     if (caret_count)
90     {
91       hb_array_t<const HBINT16> arr = array.sub_array (start_offset, caret_count);
92       for (unsigned int i = 0; i < arr.length; ++i)
93       {
94         hb_position_t x = 0, y = 0;
95         font->get_glyph_contour_point_for_origin (glyph, arr[i], direction, &x, &y);
96         caret_array[i] = HB_DIRECTION_IS_HORIZONTAL (direction) ? x : y;
97       }
98     }
99     return array.len;
100   }
101
102   bool sanitize (hb_sanitize_context_t *c, const void *base) const
103   {
104     TRACE_SANITIZE (this);
105     return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
106   }
107
108   protected:
109   Lookup<OffsetTo<LigCaretClassEntry>>
110                 lookupTable;    /* data Lookup table associating glyphs */
111   public:
112   DEFINE_SIZE_MIN (2);
113 };
114
115 struct lcar
116 {
117   static constexpr hb_tag_t tableTag = HB_AAT_TAG_lcar;
118
119   bool has_data () const { return version.major; }
120
121   unsigned int get_lig_carets (hb_font_t      *font,
122                                hb_direction_t  direction,
123                                hb_codepoint_t  glyph,
124                                unsigned int    start_offset,
125                                unsigned int   *caret_count /* IN/OUT */,
126                                hb_position_t  *caret_array /* OUT */) const
127   {
128     if (!has_data ())
129     {
130       if (caret_count)
131         *caret_count = 0;
132       return 0;
133     }
134
135     switch (format)
136     {
137     case 0: return u.format0.get_lig_carets (font, direction, glyph, start_offset,
138                                              caret_count, caret_array, this);
139     case 1: return u.format1.get_lig_carets (font, direction, glyph, start_offset,
140                                              caret_count, caret_array, this);
141     default:if (caret_count) *caret_count = 0; return 0;
142     }
143   }
144
145   bool sanitize (hb_sanitize_context_t *c) const
146   {
147     TRACE_SANITIZE (this);
148     if (unlikely (!c->check_struct (this) || version.major != 1))
149       return_trace (false);
150
151     switch (format) {
152     case 0: return_trace (u.format0.sanitize (c, this));
153     case 1: return_trace (u.format1.sanitize (c, this));
154     default:return_trace (true);
155     }
156   }
157
158   protected:
159   FixedVersion<>version;        /* Version number of the ligature caret table */
160   HBUINT16      format;         /* Format of the ligature caret table. */
161   union {
162   lcarFormat0   format0;
163   lcarFormat0   format1;
164   } u;
165   public:
166   DEFINE_SIZE_MIN (8);
167 };
168
169 } /* namespace AAT */
170
171 #endif /* HB_AAT_LAYOUT_LCAR_TABLE_HH */