/work/workdir/UnpackedTarball/graphite/src/Segment.cpp
Line | Count | Source (jump to first uncovered line) |
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 "inc/UtfCodec.h" |
28 | | #include <cstring> |
29 | | #include <cstdlib> |
30 | | |
31 | | #include "inc/bits.h" |
32 | | #include "inc/Segment.h" |
33 | | #include "graphite2/Font.h" |
34 | | #include "inc/CharInfo.h" |
35 | | #include "inc/debug.h" |
36 | | #include "inc/Slot.h" |
37 | | #include "inc/Main.h" |
38 | | #include "inc/CmapCache.h" |
39 | | #include "inc/Collider.h" |
40 | | #include "graphite2/Segment.h" |
41 | | |
42 | | |
43 | | using namespace graphite2; |
44 | | |
45 | | Segment::Segment(size_t numchars, const Face* face, uint32 script, int textDir) |
46 | | : m_freeSlots(NULL), |
47 | | m_freeJustifies(NULL), |
48 | 0 | m_charinfo(new CharInfo[numchars]), |
49 | | m_collisions(NULL), |
50 | 0 | m_face(face), |
51 | 0 | m_silf(face->chooseSilf(script)), |
52 | | m_first(NULL), |
53 | | m_last(NULL), |
54 | 0 | m_bufSize(numchars + 10), |
55 | 0 | m_numGlyphs(numchars), |
56 | 0 | m_numCharinfo(numchars), |
57 | 0 | m_defaultOriginal(0), |
58 | 0 | m_dir(textDir), |
59 | 0 | m_flags(((m_silf->flags() & 0x20) != 0) << 1), |
60 | 0 | m_passBits(m_silf->aPassBits() ? -1 : 0) |
61 | 0 | { |
62 | 0 | freeSlot(newSlot()); |
63 | 0 | m_bufSize = log_binary(numchars)+1; |
64 | 0 | } |
65 | | |
66 | | Segment::~Segment() |
67 | 0 | { |
68 | 0 | for (SlotRope::iterator i = m_slots.begin(); i != m_slots.end(); ++i) |
69 | 0 | free(*i); |
70 | 0 | for (AttributeRope::iterator i = m_userAttrs.begin(); i != m_userAttrs.end(); ++i) |
71 | 0 | free(*i); |
72 | 0 | for (JustifyRope::iterator i = m_justifies.begin(); i != m_justifies.end(); ++i) |
73 | 0 | free(*i); |
74 | 0 | delete[] m_charinfo; |
75 | 0 | free(m_collisions); |
76 | 0 | } |
77 | | |
78 | | void Segment::appendSlot(int id, int cid, int gid, int iFeats, size_t coffset) |
79 | 0 | { |
80 | 0 | Slot *aSlot = newSlot(); |
81 | |
|
82 | 0 | if (!aSlot) return; |
83 | 0 | m_charinfo[id].init(cid); |
84 | 0 | m_charinfo[id].feats(iFeats); |
85 | 0 | m_charinfo[id].base(coffset); |
86 | 0 | const GlyphFace * theGlyph = m_face->glyphs().glyphSafe(gid); |
87 | 0 | m_charinfo[id].breakWeight(theGlyph ? theGlyph->attrs()[m_silf->aBreak()] : 0); |
88 | |
|
89 | 0 | aSlot->child(NULL); |
90 | 0 | aSlot->setGlyph(this, gid, theGlyph); |
91 | 0 | aSlot->originate(id); |
92 | 0 | aSlot->before(id); |
93 | 0 | aSlot->after(id); |
94 | 0 | if (m_last) m_last->next(aSlot); |
95 | 0 | aSlot->prev(m_last); |
96 | 0 | m_last = aSlot; |
97 | 0 | if (!m_first) m_first = aSlot; |
98 | 0 | if (theGlyph && m_silf->aPassBits()) |
99 | 0 | m_passBits &= theGlyph->attrs()[m_silf->aPassBits()] |
100 | 0 | | (m_silf->numPasses() > 16 ? (theGlyph->attrs()[m_silf->aPassBits() + 1] << 16) : 0); |
101 | 0 | } |
102 | | |
103 | | Slot *Segment::newSlot() |
104 | 0 | { |
105 | 0 | if (!m_freeSlots) |
106 | 0 | { |
107 | | // check that the segment doesn't grow indefinintely |
108 | 0 | if (m_numGlyphs > m_numCharinfo * MAX_SEG_GROWTH_FACTOR) |
109 | 0 | return NULL; |
110 | 0 | int numUser = m_silf->numUser(); |
111 | | #if !defined GRAPHITE2_NTRACING |
112 | | if (m_face->logger()) ++numUser; |
113 | | #endif |
114 | 0 | Slot *newSlots = grzeroalloc<Slot>(m_bufSize); |
115 | 0 | int16 *newAttrs = grzeroalloc<int16>(m_bufSize * numUser); |
116 | 0 | if (!newSlots || !newAttrs) |
117 | 0 | { |
118 | 0 | free(newSlots); |
119 | 0 | free(newAttrs); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 0 | for (size_t i = 0; i < m_bufSize; i++) |
123 | 0 | { |
124 | 0 | ::new (newSlots + i) Slot(newAttrs + i * numUser); |
125 | 0 | newSlots[i].next(newSlots + i + 1); |
126 | 0 | } |
127 | 0 | newSlots[m_bufSize - 1].next(NULL); |
128 | 0 | newSlots[0].next(NULL); |
129 | 0 | m_slots.push_back(newSlots); |
130 | 0 | m_userAttrs.push_back(newAttrs); |
131 | 0 | m_freeSlots = (m_bufSize > 1)? newSlots + 1 : NULL; |
132 | 0 | return newSlots; |
133 | 0 | } |
134 | 0 | Slot *res = m_freeSlots; |
135 | 0 | m_freeSlots = m_freeSlots->next(); |
136 | 0 | res->next(NULL); |
137 | 0 | return res; |
138 | 0 | } |
139 | | |
140 | | void Segment::freeSlot(Slot *aSlot) |
141 | 0 | { |
142 | 0 | if (aSlot == nullptr) return; |
143 | 0 | if (m_last == aSlot) m_last = aSlot->prev(); |
144 | 0 | if (m_first == aSlot) m_first = aSlot->next(); |
145 | 0 | if (aSlot->attachedTo()) |
146 | 0 | aSlot->attachedTo()->removeChild(aSlot); |
147 | 0 | while (aSlot->firstChild()) |
148 | 0 | { |
149 | 0 | if (aSlot->firstChild()->attachedTo() == aSlot) |
150 | 0 | { |
151 | 0 | aSlot->firstChild()->attachTo(nullptr); |
152 | 0 | aSlot->removeChild(aSlot->firstChild()); |
153 | 0 | } |
154 | 0 | else |
155 | 0 | aSlot->firstChild(nullptr); |
156 | 0 | } |
157 | | // reset the slot incase it is reused |
158 | 0 | ::new (aSlot) Slot(aSlot->userAttrs()); |
159 | 0 | memset(aSlot->userAttrs(), 0, m_silf->numUser() * sizeof(int16)); |
160 | | // Update generation counter for debug |
161 | | #if !defined GRAPHITE2_NTRACING |
162 | | if (m_face->logger()) |
163 | | ++aSlot->userAttrs()[m_silf->numUser()]; |
164 | | #endif |
165 | | // update next pointer |
166 | 0 | if (!m_freeSlots) |
167 | 0 | aSlot->next(nullptr); |
168 | 0 | else |
169 | 0 | aSlot->next(m_freeSlots); |
170 | 0 | m_freeSlots = aSlot; |
171 | 0 | } |
172 | | |
173 | | SlotJustify *Segment::newJustify() |
174 | 0 | { |
175 | 0 | if (!m_freeJustifies) |
176 | 0 | { |
177 | 0 | const size_t justSize = SlotJustify::size_of(m_silf->numJustLevels()); |
178 | 0 | byte *justs = grzeroalloc<byte>(justSize * m_bufSize); |
179 | 0 | if (!justs) return NULL; |
180 | 0 | for (ptrdiff_t i = m_bufSize - 2; i >= 0; --i) |
181 | 0 | { |
182 | 0 | SlotJustify *p = reinterpret_cast<SlotJustify *>(justs + justSize * i); |
183 | 0 | SlotJustify *next = reinterpret_cast<SlotJustify *>(justs + justSize * (i + 1)); |
184 | 0 | p->next = next; |
185 | 0 | } |
186 | 0 | m_freeJustifies = (SlotJustify *)justs; |
187 | 0 | m_justifies.push_back(m_freeJustifies); |
188 | 0 | } |
189 | 0 | SlotJustify *res = m_freeJustifies; |
190 | 0 | m_freeJustifies = m_freeJustifies->next; |
191 | 0 | res->next = NULL; |
192 | 0 | return res; |
193 | 0 | } |
194 | | |
195 | | void Segment::freeJustify(SlotJustify *aJustify) |
196 | 0 | { |
197 | 0 | int numJust = m_silf->numJustLevels(); |
198 | 0 | if (m_silf->numJustLevels() <= 0) numJust = 1; |
199 | 0 | aJustify->next = m_freeJustifies; |
200 | 0 | memset(aJustify->values, 0, numJust*SlotJustify::NUMJUSTPARAMS*sizeof(int16)); |
201 | 0 | m_freeJustifies = aJustify; |
202 | 0 | } |
203 | | |
204 | | // reverse the slots but keep diacritics in their same position after their bases |
205 | | void Segment::reverseSlots() |
206 | 0 | { |
207 | 0 | m_dir = m_dir ^ 64; // invert the reverse flag |
208 | 0 | if (m_first == m_last) return; // skip 0 or 1 glyph runs |
209 | | |
210 | 0 | Slot *t = 0; |
211 | 0 | Slot *curr = m_first; |
212 | 0 | Slot *tlast; |
213 | 0 | Slot *tfirst; |
214 | 0 | Slot *out = 0; |
215 | |
|
216 | 0 | while (curr && getSlotBidiClass(curr) == 16) |
217 | 0 | curr = curr->next(); |
218 | 0 | if (!curr) return; |
219 | 0 | tfirst = curr->prev(); |
220 | 0 | tlast = curr; |
221 | |
|
222 | 0 | while (curr) |
223 | 0 | { |
224 | 0 | if (getSlotBidiClass(curr) == 16) |
225 | 0 | { |
226 | 0 | Slot *d = curr->next(); |
227 | 0 | while (d && getSlotBidiClass(d) == 16) |
228 | 0 | d = d->next(); |
229 | |
|
230 | 0 | d = d ? d->prev() : m_last; |
231 | 0 | Slot *p = out->next(); // one after the diacritics. out can't be null |
232 | 0 | if (p) |
233 | 0 | p->prev(d); |
234 | 0 | else |
235 | 0 | tlast = d; |
236 | 0 | t = d->next(); |
237 | 0 | d->next(p); |
238 | 0 | curr->prev(out); |
239 | 0 | out->next(curr); |
240 | 0 | } |
241 | 0 | else // will always fire first time round the loop |
242 | 0 | { |
243 | 0 | if (out) |
244 | 0 | out->prev(curr); |
245 | 0 | t = curr->next(); |
246 | 0 | curr->next(out); |
247 | 0 | out = curr; |
248 | 0 | } |
249 | 0 | curr = t; |
250 | 0 | } |
251 | 0 | out->prev(tfirst); |
252 | 0 | if (tfirst) |
253 | 0 | tfirst->next(out); |
254 | 0 | else |
255 | 0 | m_first = out; |
256 | 0 | m_last = tlast; |
257 | 0 | } |
258 | | |
259 | | void Segment::linkClusters(Slot *s, Slot * end) |
260 | 0 | { |
261 | 0 | end = end->next(); |
262 | |
|
263 | 0 | for (; s != end && !s->isBase(); s = s->next()); |
264 | 0 | Slot * ls = s; |
265 | |
|
266 | 0 | if (m_dir & 1) |
267 | 0 | { |
268 | 0 | for (; s != end; s = s->next()) |
269 | 0 | { |
270 | 0 | if (!s->isBase()) continue; |
271 | | |
272 | 0 | s->sibling(ls); |
273 | 0 | ls = s; |
274 | 0 | } |
275 | 0 | } |
276 | 0 | else |
277 | 0 | { |
278 | 0 | for (; s != end; s = s->next()) |
279 | 0 | { |
280 | 0 | if (!s->isBase()) continue; |
281 | | |
282 | 0 | ls->sibling(s); |
283 | 0 | ls = s; |
284 | 0 | } |
285 | 0 | } |
286 | 0 | } |
287 | | |
288 | | Position Segment::positionSlots(const Font *font, Slot * iStart, Slot * iEnd, bool isRtl, bool isFinal) |
289 | 0 | { |
290 | 0 | Position currpos(0., 0.); |
291 | 0 | float clusterMin = 0.; |
292 | 0 | Rect bbox; |
293 | 0 | bool reorder = (currdir() != isRtl); |
294 | |
|
295 | 0 | if (reorder) |
296 | 0 | { |
297 | 0 | Slot *temp; |
298 | 0 | reverseSlots(); |
299 | 0 | temp = iStart; |
300 | 0 | iStart = iEnd; |
301 | 0 | iEnd = temp; |
302 | 0 | } |
303 | 0 | if (!iStart) iStart = m_first; |
304 | 0 | if (!iEnd) iEnd = m_last; |
305 | |
|
306 | 0 | if (!iStart || !iEnd) // only true for empty segments |
307 | 0 | return currpos; |
308 | | |
309 | 0 | if (isRtl) |
310 | 0 | { |
311 | 0 | for (Slot * s = iEnd, * const end = iStart->prev(); s && s != end; s = s->prev()) |
312 | 0 | { |
313 | 0 | if (s->isBase()) |
314 | 0 | currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x, isRtl, isFinal); |
315 | 0 | } |
316 | 0 | } |
317 | 0 | else |
318 | 0 | { |
319 | 0 | for (Slot * s = iStart, * const end = iEnd->next(); s && s != end; s = s->next()) |
320 | 0 | { |
321 | 0 | if (s->isBase()) |
322 | 0 | currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x, isRtl, isFinal); |
323 | 0 | } |
324 | 0 | } |
325 | 0 | if (reorder) |
326 | 0 | reverseSlots(); |
327 | 0 | return currpos; |
328 | 0 | } |
329 | | |
330 | | |
331 | | void Segment::associateChars(int offset, size_t numChars) |
332 | 0 | { |
333 | 0 | int i = 0, j = 0; |
334 | 0 | CharInfo *c, *cend; |
335 | 0 | for (c = m_charinfo + offset, cend = m_charinfo + offset + numChars; c != cend; ++c) |
336 | 0 | { |
337 | 0 | c->before(-1); |
338 | 0 | c->after(-1); |
339 | 0 | } |
340 | 0 | for (Slot * s = m_first; s; s->index(i++), s = s->next()) |
341 | 0 | { |
342 | 0 | j = s->before(); |
343 | 0 | if (j < 0) continue; |
344 | | |
345 | 0 | for (const int after = s->after(); j <= after; ++j) |
346 | 0 | { |
347 | 0 | c = charinfo(j); |
348 | 0 | if (c->before() == -1 || i < c->before()) c->before(i); |
349 | 0 | if (c->after() < i) c->after(i); |
350 | 0 | } |
351 | 0 | } |
352 | 0 | for (Slot *s = m_first; s; s = s->next()) |
353 | 0 | { |
354 | 0 | int a; |
355 | 0 | for (a = s->after() + 1; a < offset + int(numChars) && charinfo(a)->after() < 0; ++a) |
356 | 0 | { charinfo(a)->after(s->index()); } |
357 | 0 | --a; |
358 | 0 | s->after(a); |
359 | |
|
360 | 0 | for (a = s->before() - 1; a >= offset && charinfo(a)->before() < 0; --a) |
361 | 0 | { charinfo(a)->before(s->index()); } |
362 | 0 | ++a; |
363 | 0 | s->before(a); |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | | |
368 | | template <typename utf_iter> |
369 | | inline void process_utf_data(Segment & seg, const Face & face, const int fid, utf_iter c, size_t n_chars) |
370 | 0 | { |
371 | 0 | const Cmap & cmap = face.cmap(); |
372 | 0 | int slotid = 0; |
373 | |
|
374 | 0 | const typename utf_iter::codeunit_type * const base = c; |
375 | 0 | for (; n_chars; --n_chars, ++c, ++slotid) |
376 | 0 | { |
377 | 0 | const uint32 usv = *c; |
378 | 0 | uint16 gid = cmap[usv]; |
379 | 0 | if (!gid) gid = face.findPseudo(usv); |
380 | 0 | seg.appendSlot(slotid, usv, gid, fid, c - base); |
381 | 0 | } |
382 | 0 | } Unexecuted instantiation: void process_utf_data<graphite2::_utf_iterator<unsigned char const> >(graphite2::Segment&, graphite2::Face const&, int, graphite2::_utf_iterator<unsigned char const>, unsigned long) Unexecuted instantiation: void process_utf_data<graphite2::_utf_iterator<unsigned short const> >(graphite2::Segment&, graphite2::Face const&, int, graphite2::_utf_iterator<unsigned short const>, unsigned long) Unexecuted instantiation: void process_utf_data<graphite2::_utf_iterator<unsigned int const> >(graphite2::Segment&, graphite2::Face const&, int, graphite2::_utf_iterator<unsigned int const>, unsigned long) |
383 | | |
384 | | |
385 | | bool Segment::read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void* pStart, size_t nChars) |
386 | 0 | { |
387 | 0 | assert(face); |
388 | 0 | assert(pFeats); |
389 | 0 | if (!m_charinfo) return false; |
390 | | |
391 | | // utf iterator is self recovering so we don't care about the error state of the iterator. |
392 | 0 | switch (enc) |
393 | 0 | { |
394 | 0 | case gr_utf8: process_utf_data(*this, *face, addFeatures(*pFeats), utf8::const_iterator(pStart), nChars); break; |
395 | 0 | case gr_utf16: process_utf_data(*this, *face, addFeatures(*pFeats), utf16::const_iterator(pStart), nChars); break; |
396 | 0 | case gr_utf32: process_utf_data(*this, *face, addFeatures(*pFeats), utf32::const_iterator(pStart), nChars); break; |
397 | 0 | } |
398 | 0 | return true; |
399 | 0 | } |
400 | | |
401 | | void Segment::doMirror(uint16 aMirror) |
402 | 0 | { |
403 | 0 | Slot * s; |
404 | 0 | for (s = m_first; s; s = s->next()) |
405 | 0 | { |
406 | 0 | unsigned short g = glyphAttr(s->gid(), aMirror); |
407 | 0 | if (g && (!(dir() & 4) || !glyphAttr(s->gid(), aMirror + 1))) |
408 | 0 | s->setGlyph(this, g); |
409 | 0 | } |
410 | 0 | } |
411 | | |
412 | | bool Segment::initCollisions() |
413 | 0 | { |
414 | 0 | m_collisions = grzeroalloc<SlotCollision>(slotCount()); |
415 | 0 | if (!m_collisions) return false; |
416 | | |
417 | 0 | for (Slot *p = m_first; p; p = p->next()) |
418 | 0 | if (p->index() < slotCount()) |
419 | 0 | ::new (collisionInfo(p)) SlotCollision(this, p); |
420 | 0 | else |
421 | 0 | return false; |
422 | 0 | return true; |
423 | 0 | } |