1 | // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later |
2 | // Copyright 2010, SIL International, All rights reserved. |
3 | |
4 | #include "graphite2/Segment.h" |
5 | #include "inc/UtfCodec.h" |
6 | #include "inc/Segment.h" |
7 | |
8 | using namespace graphite2; |
9 | |
10 | namespace |
11 | { |
12 | |
13 | gr_segment* makeAndInitialize(const Font *font, const Face *face, uint32 script, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void* pStart, size_t nChars, int dir) |
14 | { |
15 | if (script == 0x20202020) script = 0; |
16 | else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000; |
17 | else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000; |
18 | else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00; |
19 | // if (!font) return NULL; |
20 | Segment* pRes=new Segment(nChars, face, script, dir); |
21 | |
22 | |
23 | if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite()) |
24 | { |
25 | delete pRes; |
26 | return NULL; |
27 | } |
28 | pRes->finalise(font, true); |
29 | |
30 | return static_cast<gr_segment*>(pRes); |
31 | } |
32 | |
33 | template <typename utf_iter> |
34 | inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error) |
35 | { |
36 | size_t n_chars = 0; |
37 | uint32 usv = 0; |
38 | |
39 | if (last) |
40 | { |
41 | if (!first.validate(last)) |
42 | { |
43 | if (error) *error = last - 1; |
44 | return 0; |
45 | } |
46 | for (;first != last; ++first, ++n_chars) |
47 | if ((usv = *first) == 0 || first.error()) break; |
48 | } |
49 | else |
50 | { |
51 | while ((usv = *first) != 0 && !first.error()) |
52 | { |
53 | ++first; |
54 | ++n_chars; |
55 | } |
56 | } |
57 | |
58 | if (error) *error = first.error() ? first : 0; |
59 | return n_chars; |
60 | } |
61 | } |
62 | |
63 | |
64 | extern "C" { |
65 | |
66 | size_t gr_count_unicode_characters(gr_encform enc, const void* buffer_begin, const void* buffer_end/*don't go on or past end, If NULL then ignored*/, const void** pError) //Also stops on nul. Any nul is not in the count |
67 | { |
68 | assert(buffer_begin); |
69 | |
70 | switch (enc) |
71 | { |
72 | case gr_utf8: return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break; |
73 | case gr_utf16: return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break; |
74 | case gr_utf32: return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break; |
75 | default: return 0; |
76 | } |
77 | } |
78 | |
79 | |
80 | gr_segment* gr_make_seg(const gr_font *font, const gr_face *face, gr_uint32 script, const gr_feature_val* pFeats, gr_encform enc, const void* pStart, size_t nChars, int dir) |
81 | { |
82 | if (!face) return nullptr; |
83 | |
84 | const gr_feature_val * tmp_feats = 0; |
85 | if (pFeats == 0) |
86 | pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0)); |
87 | gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir); |
88 | delete static_cast<const FeatureVal*>(tmp_feats); |
89 | |
90 | return seg; |
91 | } |
92 | |
93 | |
94 | void gr_seg_destroy(gr_segment* p) |
95 | { |
96 | delete static_cast<Segment*>(p); |
97 | } |
98 | |
99 | |
100 | float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/) |
101 | { |
102 | assert(pSeg); |
103 | return pSeg->advance().x; |
104 | } |
105 | |
106 | |
107 | float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/) |
108 | { |
109 | assert(pSeg); |
110 | return pSeg->advance().y; |
111 | } |
112 | |
113 | |
114 | unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/) |
115 | { |
116 | assert(pSeg); |
117 | return static_cast<unsigned int>(pSeg->charInfoCount()); |
118 | } |
119 | |
120 | |
121 | const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/) |
122 | { |
123 | assert(pSeg); |
124 | return static_cast<const gr_char_info*>(pSeg->charinfo(index)); |
125 | } |
126 | |
127 | unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/) |
128 | { |
129 | assert(pSeg); |
130 | return static_cast<unsigned int>(pSeg->slotCount()); |
131 | } |
132 | |
133 | const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/) |
134 | { |
135 | assert(pSeg); |
136 | return static_cast<const gr_slot*>(pSeg->first()); |
137 | } |
138 | |
139 | const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/) |
140 | { |
141 | assert(pSeg); |
142 | return static_cast<const gr_slot*>(pSeg->last()); |
143 | } |
144 | |
145 | float gr_seg_justify(gr_segment* pSeg/*not NULL*/, const gr_slot* pSlot/*not NULL*/, const gr_font *pFont, double width, enum gr_justFlags flags, const gr_slot *pFirst, const gr_slot *pLast) |
146 | { |
147 | assert(pSeg); |
148 | assert(pSlot); |
149 | return pSeg->justify(const_cast<gr_slot *>(pSlot), pFont, float(width), justFlags(flags), const_cast<gr_slot *>(pFirst), const_cast<gr_slot *>(pLast)); |
150 | } |
151 | |
152 | } // extern "C" |
153 | |