/work/workdir/UnpackedTarball/graphite/src/Justifier.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* GRAPHITE2 LICENSING |
2 | | |
3 | | Copyright 2012, 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 | | |
28 | | #include "inc/Segment.h" |
29 | | #include "graphite2/Font.h" |
30 | | #include "inc/debug.h" |
31 | | #include "inc/CharInfo.h" |
32 | | #include "inc/Slot.h" |
33 | | #include "inc/Main.h" |
34 | | #include <cmath> |
35 | | |
36 | | using namespace graphite2; |
37 | | |
38 | | class JustifyTotal { |
39 | | public: |
40 | 0 | JustifyTotal() : m_numGlyphs(0), m_tStretch(0), m_tShrink(0), m_tStep(0), m_tWeight(0) {} |
41 | | void accumulate(Slot *s, Segment *seg, int level); |
42 | 0 | int weight() const { return m_tWeight; } |
43 | | |
44 | | CLASS_NEW_DELETE |
45 | | |
46 | | private: |
47 | | int m_numGlyphs; |
48 | | int m_tStretch; |
49 | | int m_tShrink; |
50 | | int m_tStep; |
51 | | int m_tWeight; |
52 | | }; |
53 | | |
54 | | void JustifyTotal::accumulate(Slot *s, Segment *seg, int level) |
55 | 0 | { |
56 | 0 | ++m_numGlyphs; |
57 | 0 | m_tStretch += s->getJustify(seg, level, 0); |
58 | 0 | m_tShrink += s->getJustify(seg, level, 1); |
59 | 0 | m_tStep += s->getJustify(seg, level, 2); |
60 | 0 | m_tWeight += s->getJustify(seg, level, 3); |
61 | 0 | } |
62 | | |
63 | | float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUSED justFlags jflags, Slot *pFirst, Slot *pLast) |
64 | 0 | { |
65 | 0 | Slot *end = last(); |
66 | 0 | float currWidth = 0.0; |
67 | 0 | const float scale = font ? font->scale() : 1.0f; |
68 | 0 | Position res; |
69 | |
|
70 | 0 | if (width < 0 && !(silf()->flags())) |
71 | 0 | return width; |
72 | | |
73 | 0 | if ((m_dir & 1) != m_silf->dir() && m_silf->bidiPass() != m_silf->numPasses()) |
74 | 0 | { |
75 | 0 | reverseSlots(); |
76 | 0 | std::swap(pFirst, pLast); |
77 | 0 | } |
78 | 0 | if (!pFirst) pFirst = pSlot; |
79 | 0 | while (!pFirst->isBase()) pFirst = pFirst->attachedTo(); |
80 | 0 | if (!pLast) pLast = last(); |
81 | 0 | while (!pLast->isBase()) pLast = pLast->attachedTo(); |
82 | 0 | const float base = pFirst->origin().x / scale; |
83 | 0 | width = width / scale; |
84 | 0 | if ((jflags & gr_justEndInline) == 0) |
85 | 0 | { |
86 | 0 | while (pLast != pFirst && pLast) |
87 | 0 | { |
88 | 0 | Rect bbox = theGlyphBBoxTemporary(pLast->glyph()); |
89 | 0 | if (bbox.bl.x != 0.f || bbox.bl.y != 0.f || bbox.tr.x != 0.f || bbox.tr.y == 0.f) |
90 | 0 | break; |
91 | 0 | pLast = pLast->prev(); |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | 0 | if (pLast) |
96 | 0 | end = pLast->nextSibling(); |
97 | 0 | if (pFirst) |
98 | 0 | pFirst = pFirst->nextSibling(); |
99 | |
|
100 | 0 | int icount = 0; |
101 | 0 | int numLevels = silf()->numJustLevels(); |
102 | 0 | if (!numLevels) |
103 | 0 | { |
104 | 0 | for (Slot *s = pSlot; s && s != end; s = s->nextSibling()) |
105 | 0 | { |
106 | 0 | CharInfo *c = charinfo(s->before()); |
107 | 0 | if (isWhitespace(c->unicodeChar())) |
108 | 0 | { |
109 | 0 | s->setJustify(this, 0, 3, 1); |
110 | 0 | s->setJustify(this, 0, 2, 1); |
111 | 0 | s->setJustify(this, 0, 0, -1); |
112 | 0 | ++icount; |
113 | 0 | } |
114 | 0 | } |
115 | 0 | if (!icount) |
116 | 0 | { |
117 | 0 | for (Slot *s = pSlot; s && s != end; s = s->nextSibling()) |
118 | 0 | { |
119 | 0 | s->setJustify(this, 0, 3, 1); |
120 | 0 | s->setJustify(this, 0, 2, 1); |
121 | 0 | s->setJustify(this, 0, 0, -1); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | ++numLevels; |
125 | 0 | } |
126 | |
|
127 | 0 | Vector<JustifyTotal> stats(numLevels); |
128 | 0 | for (Slot *s = pFirst; s && s != end; s = s->nextSibling()) |
129 | 0 | { |
130 | 0 | float w = s->origin().x / scale + s->advance() - base; |
131 | 0 | if (w > currWidth) currWidth = w; |
132 | 0 | for (int j = 0; j < numLevels; ++j) |
133 | 0 | stats[j].accumulate(s, this, j); |
134 | 0 | s->just(0); |
135 | 0 | } |
136 | |
|
137 | 0 | for (int i = (width < 0.0f) ? -1 : numLevels - 1; i >= 0; --i) |
138 | 0 | { |
139 | 0 | float diff; |
140 | 0 | float error = 0.; |
141 | 0 | float diffpw; |
142 | 0 | int tWeight = stats[i].weight(); |
143 | 0 | if (tWeight == 0) continue; |
144 | | |
145 | 0 | do { |
146 | 0 | error = 0.; |
147 | 0 | diff = width - currWidth; |
148 | 0 | diffpw = diff / tWeight; |
149 | 0 | tWeight = 0; |
150 | 0 | for (Slot *s = pFirst; s && s != end; s = s->nextSibling()) // don't include final glyph |
151 | 0 | { |
152 | 0 | int w = s->getJustify(this, i, 3); |
153 | 0 | float pref = diffpw * w + error; |
154 | 0 | int step = s->getJustify(this, i, 2); |
155 | 0 | if (!step) step = 1; // handle lazy font developers |
156 | 0 | if (pref > 0) |
157 | 0 | { |
158 | 0 | float max = uint16(s->getJustify(this, i, 0)); |
159 | 0 | if (i == 0) max -= s->just(); |
160 | 0 | if (pref > max) pref = max; |
161 | 0 | else tWeight += w; |
162 | 0 | } |
163 | 0 | else |
164 | 0 | { |
165 | 0 | float max = uint16(s->getJustify(this, i, 1)); |
166 | 0 | if (i == 0) max += s->just(); |
167 | 0 | if (-pref > max) pref = -max; |
168 | 0 | else tWeight += w; |
169 | 0 | } |
170 | 0 | int actual = int(pref / step) * step; |
171 | |
|
172 | 0 | if (actual) |
173 | 0 | { |
174 | 0 | error += diffpw * w - actual; |
175 | 0 | if (i == 0) |
176 | 0 | s->just(s->just() + actual); |
177 | 0 | else |
178 | 0 | s->setJustify(this, i, 4, actual); |
179 | 0 | } |
180 | 0 | } |
181 | 0 | currWidth += diff - error; |
182 | 0 | } while (i == 0 && int(std::abs(error)) > 0 && tWeight); |
183 | 0 | } |
184 | |
|
185 | 0 | Slot *oldFirst = m_first; |
186 | 0 | Slot *oldLast = m_last; |
187 | 0 | if (silf()->flags() & 1) |
188 | 0 | { |
189 | 0 | m_first = pSlot = addLineEnd(pSlot); |
190 | 0 | m_last = pLast = addLineEnd(end); |
191 | 0 | if (!m_first || !m_last) return -1.0; |
192 | 0 | } |
193 | 0 | else |
194 | 0 | { |
195 | 0 | m_first = pSlot; |
196 | 0 | m_last = pLast; |
197 | 0 | } |
198 | | |
199 | | // run justification passes here |
200 | | #if !defined GRAPHITE2_NTRACING |
201 | | json * const dbgout = m_face->logger(); |
202 | | if (dbgout) |
203 | | *dbgout << json::object |
204 | | << "justifies" << objectid(this) |
205 | | << "passes" << json::array; |
206 | | #endif |
207 | | |
208 | 0 | if (m_silf->justificationPass() != m_silf->positionPass() && (width >= 0.f || (silf()->flags() & 1))) |
209 | 0 | m_silf->runGraphite(this, m_silf->justificationPass(), m_silf->positionPass()); |
210 | |
|
211 | | #if !defined GRAPHITE2_NTRACING |
212 | | if (dbgout) |
213 | | { |
214 | | *dbgout << json::item << json::close; // Close up the passes array |
215 | | positionSlots(NULL, pSlot, pLast, m_dir); |
216 | | Slot *lEnd = pLast->nextSibling(); |
217 | | *dbgout << "output" << json::array; |
218 | | for(Slot * t = pSlot; t != lEnd; t = t->next()) |
219 | | *dbgout << dslot(this, t); |
220 | | *dbgout << json::close << json::close; |
221 | | } |
222 | | #endif |
223 | |
|
224 | 0 | res = positionSlots(font, pSlot, pLast, m_dir); |
225 | |
|
226 | 0 | if (silf()->flags() & 1) |
227 | 0 | { |
228 | 0 | if (m_first) |
229 | 0 | delLineEnd(m_first); |
230 | 0 | if (m_last) |
231 | 0 | delLineEnd(m_last); |
232 | 0 | } |
233 | 0 | m_first = oldFirst; |
234 | 0 | m_last = oldLast; |
235 | |
|
236 | 0 | if ((m_dir & 1) != m_silf->dir() && m_silf->bidiPass() != m_silf->numPasses()) |
237 | 0 | reverseSlots(); |
238 | 0 | return res.x; |
239 | 0 | } |
240 | | |
241 | | Slot *Segment::addLineEnd(Slot *nSlot) |
242 | 0 | { |
243 | 0 | Slot *eSlot = newSlot(); |
244 | 0 | if (!eSlot) return NULL; |
245 | 0 | const uint16 gid = silf()->endLineGlyphid(); |
246 | 0 | const GlyphFace * theGlyph = m_face->glyphs().glyphSafe(gid); |
247 | 0 | eSlot->setGlyph(this, gid, theGlyph); |
248 | 0 | if (nSlot) |
249 | 0 | { |
250 | 0 | eSlot->next(nSlot); |
251 | 0 | eSlot->prev(nSlot->prev()); |
252 | 0 | nSlot->prev(eSlot); |
253 | 0 | eSlot->before(nSlot->before()); |
254 | 0 | if (eSlot->prev()) |
255 | 0 | eSlot->after(eSlot->prev()->after()); |
256 | 0 | else |
257 | 0 | eSlot->after(nSlot->before()); |
258 | 0 | } |
259 | 0 | else |
260 | 0 | { |
261 | 0 | nSlot = m_last; |
262 | 0 | eSlot->prev(nSlot); |
263 | 0 | nSlot->next(eSlot); |
264 | 0 | eSlot->after(eSlot->prev()->after()); |
265 | 0 | eSlot->before(nSlot->after()); |
266 | 0 | } |
267 | 0 | return eSlot; |
268 | 0 | } |
269 | | |
270 | | void Segment::delLineEnd(Slot *s) |
271 | 0 | { |
272 | 0 | Slot *nSlot = s->next(); |
273 | 0 | if (nSlot) |
274 | 0 | { |
275 | 0 | nSlot->prev(s->prev()); |
276 | 0 | if (s->prev()) |
277 | 0 | s->prev()->next(nSlot); |
278 | 0 | } |
279 | 0 | else |
280 | 0 | s->prev()->next(NULL); |
281 | 0 | freeSlot(s); |
282 | 0 | } |