1 /*  GRAPHITE2 LICENSING
2 
3     Copyright 2010, SIL International
4     All rights reserved.
5 
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as published
8     by the Free Software Foundation; either version 2.1 of License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should also have received a copy of the GNU Lesser General Public
17     License along with this library in the file named "LICENSE".
18     If not, write to the Free Software Foundation, 51 Franklin Street,
19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20     internet at http://www.fsf.org/licenses/lgpl.html.
21 
22 Alternatively, the contents of this file may be used under the terms of the
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 License, as published by the Free Software Foundation, either version 2
25 of the License or (at your option) any later version.
26 */
27 #include "graphite2/Segment.h"
28 #include "inc/UtfCodec.h"
29 #include "inc/Segment.h"
30 
31 using namespace graphite2;
32 
33 namespace
34 {
35 
makeAndInitialize(const Font * font,const Face * face,uint32 script,const Features * pFeats,gr_encform enc,const void * pStart,size_t nChars,int dir)36   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)
37   {
38       if (script == 0x20202020) script = 0;
39       else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000;
40       else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000;
41       else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00;
42       // if (!font) return NULL;
43       Segment* pRes=new Segment(nChars, face, script, dir);
44 
45 
46       if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite())
47       {
48         delete pRes;
49         return NULL;
50       }
51       pRes->finalise(font, true);
52 
53       return static_cast<gr_segment*>(pRes);
54   }
55 
56   template <typename utf_iter>
count_unicode_chars(utf_iter first,const utf_iter last,const void ** error)57   inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error)
58   {
59       size_t n_chars = 0;
60       uint32 usv = 0;
61 
62       if (last)
63       {
64           if (!first.validate(last))
65           {
66               if (error)  *error = last - 1;
67               return 0;
68           }
69           for (;first != last; ++first, ++n_chars)
70               if ((usv = *first) == 0 || first.error()) break;
71       }
72       else
73       {
74           while ((usv = *first) != 0 && !first.error())
75           {
76               ++first;
77               ++n_chars;
78           }
79       }
80 
81       if (error)  *error = first.error() ? first : 0;
82       return n_chars;
83   }
84 }
85 
86 
87 extern "C" {
88 
gr_count_unicode_characters(gr_encform enc,const void * buffer_begin,const void * buffer_end,const void ** pError)89 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
90 {
91     assert(buffer_begin);
92 
93     switch (enc)
94     {
95     case gr_utf8:   return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break;
96     case gr_utf16:  return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break;
97     case gr_utf32:  return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break;
98     default:        return 0;
99     }
100 }
101 
102 
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)103 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)
104 {
105     if (!face) return nullptr;
106 
107     const gr_feature_val * tmp_feats = 0;
108     if (pFeats == 0)
109         pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0));
110     gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir);
111     delete static_cast<const FeatureVal*>(tmp_feats);
112 
113     return seg;
114 }
115 
116 
gr_seg_destroy(gr_segment * p)117 void gr_seg_destroy(gr_segment* p)
118 {
119     delete static_cast<Segment*>(p);
120 }
121 
122 
gr_seg_advance_X(const gr_segment * pSeg)123 float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/)
124 {
125     assert(pSeg);
126     return pSeg->advance().x;
127 }
128 
129 
gr_seg_advance_Y(const gr_segment * pSeg)130 float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/)
131 {
132     assert(pSeg);
133     return pSeg->advance().y;
134 }
135 
136 
gr_seg_n_cinfo(const gr_segment * pSeg)137 unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/)
138 {
139     assert(pSeg);
140     return static_cast<unsigned int>(pSeg->charInfoCount());
141 }
142 
143 
gr_seg_cinfo(const gr_segment * pSeg,unsigned int index)144 const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/)
145 {
146     assert(pSeg);
147     return static_cast<const gr_char_info*>(pSeg->charinfo(index));
148 }
149 
gr_seg_n_slots(const gr_segment * pSeg)150 unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/)
151 {
152     assert(pSeg);
153     return static_cast<unsigned int>(pSeg->slotCount());
154 }
155 
gr_seg_first_slot(gr_segment * pSeg)156 const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/)
157 {
158     assert(pSeg);
159     return static_cast<const gr_slot*>(pSeg->first());
160 }
161 
gr_seg_last_slot(gr_segment * pSeg)162 const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/)
163 {
164     assert(pSeg);
165     return static_cast<const gr_slot*>(pSeg->last());
166 }
167 
gr_seg_justify(gr_segment * pSeg,const gr_slot * pSlot,const gr_font * pFont,double width,enum gr_justFlags flags,const gr_slot * pFirst,const gr_slot * pLast)168 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)
169 {
170     assert(pSeg);
171     assert(pSlot);
172     return pSeg->justify(const_cast<gr_slot *>(pSlot), pFont, float(width), justFlags(flags), const_cast<gr_slot *>(pFirst), const_cast<gr_slot *>(pLast));
173 }
174 
175 } // extern "C"
176