|
1 /* |
|
2 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org> |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Library General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Library General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Library General Public License |
|
15 * along with this library; see the file COPYING.LIB. If not, write to |
|
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
17 * Boston, MA 02110-1301, USA. |
|
18 * |
|
19 */ |
|
20 |
|
21 #include "config.h" |
|
22 #include "SVGCharacterLayoutInfo.h" |
|
23 |
|
24 #if ENABLE(SVG) |
|
25 #include "InlineFlowBox.h" |
|
26 #include "InlineTextBox.h" |
|
27 #include "RenderSVGTextPath.h" |
|
28 #include "SVGCharacterData.h" |
|
29 #include "SVGLengthList.h" |
|
30 #include "SVGNumberList.h" |
|
31 #include "SVGTextPositioningElement.h" |
|
32 |
|
33 #include <float.h> |
|
34 |
|
35 namespace WebCore { |
|
36 |
|
37 // Helper function |
|
38 static float calculateBaselineShift(RenderObject* item) |
|
39 { |
|
40 const Font& font = item->style()->font(); |
|
41 const SVGRenderStyle* svgStyle = item->style()->svgStyle(); |
|
42 |
|
43 float baselineShift = 0.0f; |
|
44 if (svgStyle->baselineShift() == BS_LENGTH) { |
|
45 CSSPrimitiveValue* primitive = static_cast<CSSPrimitiveValue*>(svgStyle->baselineShiftValue()); |
|
46 baselineShift = primitive->getFloatValue(); |
|
47 |
|
48 if (primitive->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE) |
|
49 baselineShift = baselineShift / 100.0f * font.pixelSize(); |
|
50 } else { |
|
51 float baselineAscent = font.ascent() + font.descent(); |
|
52 |
|
53 switch (svgStyle->baselineShift()) { |
|
54 case BS_BASELINE: |
|
55 break; |
|
56 case BS_SUB: |
|
57 baselineShift = -baselineAscent / 2.0f; |
|
58 break; |
|
59 case BS_SUPER: |
|
60 baselineShift = baselineAscent / 2.0f; |
|
61 break; |
|
62 default: |
|
63 ASSERT_NOT_REACHED(); |
|
64 } |
|
65 } |
|
66 |
|
67 return baselineShift; |
|
68 } |
|
69 |
|
70 SVGCharacterLayoutInfo::SVGCharacterLayoutInfo() |
|
71 : curx(0.0f) |
|
72 , cury(0.0f) |
|
73 , angle(0.0f) |
|
74 , dx(0.0f) |
|
75 , dy(0.0f) |
|
76 , shiftx(0.0f) |
|
77 , shifty(0.0f) |
|
78 , pathExtraAdvance(0.0f) |
|
79 , pathTextLength(0.0f) |
|
80 , pathChunkLength(0.0f) |
|
81 , nextDrawnSeperated(false) |
|
82 , xStackChanged(false) |
|
83 , yStackChanged(false) |
|
84 , dxStackChanged(false) |
|
85 , dyStackChanged(false) |
|
86 , angleStackChanged(false) |
|
87 , baselineShiftStackChanged(false) |
|
88 , pathLayout(false) |
|
89 , currentOffset(0.0f) |
|
90 , startOffset(0.0f) |
|
91 , layoutPathLength(0.0f) |
|
92 { |
|
93 } |
|
94 |
|
95 bool SVGCharacterLayoutInfo::xValueAvailable() const |
|
96 { |
|
97 return xStack.isEmpty() ? false : xStack.last().position() < xStack.last().size(); |
|
98 } |
|
99 |
|
100 bool SVGCharacterLayoutInfo::yValueAvailable() const |
|
101 { |
|
102 return yStack.isEmpty() ? false : yStack.last().position() < yStack.last().size(); |
|
103 } |
|
104 |
|
105 bool SVGCharacterLayoutInfo::dxValueAvailable() const |
|
106 { |
|
107 return dxStack.isEmpty() ? false : dxStack.last().position() < dxStack.last().size(); |
|
108 } |
|
109 |
|
110 bool SVGCharacterLayoutInfo::dyValueAvailable() const |
|
111 { |
|
112 return dyStack.isEmpty() ? false : dyStack.last().position() < dyStack.last().size(); |
|
113 } |
|
114 |
|
115 bool SVGCharacterLayoutInfo::angleValueAvailable() const |
|
116 { |
|
117 return angleStack.isEmpty() ? false : angleStack.last().position() < angleStack.last().size(); |
|
118 } |
|
119 |
|
120 bool SVGCharacterLayoutInfo::baselineShiftValueAvailable() const |
|
121 { |
|
122 return !baselineShiftStack.isEmpty(); |
|
123 } |
|
124 |
|
125 float SVGCharacterLayoutInfo::xValueNext() const |
|
126 { |
|
127 ASSERT(!xStack.isEmpty()); |
|
128 return xStack.last().valueAtCurrentPosition(); |
|
129 } |
|
130 |
|
131 float SVGCharacterLayoutInfo::yValueNext() const |
|
132 { |
|
133 ASSERT(!yStack.isEmpty()); |
|
134 return yStack.last().valueAtCurrentPosition(); |
|
135 } |
|
136 |
|
137 float SVGCharacterLayoutInfo::dxValueNext() const |
|
138 { |
|
139 ASSERT(!dxStack.isEmpty()); |
|
140 return dxStack.last().valueAtCurrentPosition(); |
|
141 } |
|
142 |
|
143 float SVGCharacterLayoutInfo::dyValueNext() const |
|
144 { |
|
145 ASSERT(!dyStack.isEmpty()); |
|
146 return dyStack.last().valueAtCurrentPosition(); |
|
147 } |
|
148 |
|
149 float SVGCharacterLayoutInfo::angleValueNext() const |
|
150 { |
|
151 ASSERT(!angleStack.isEmpty()); |
|
152 return angleStack.last().valueAtCurrentPosition(); |
|
153 } |
|
154 |
|
155 float SVGCharacterLayoutInfo::baselineShiftValueNext() const |
|
156 { |
|
157 ASSERT(!baselineShiftStack.isEmpty()); |
|
158 return baselineShiftStack.last(); |
|
159 } |
|
160 |
|
161 |
|
162 bool SVGCharacterLayoutInfo::isInitialLayout() const |
|
163 { |
|
164 return xStack.isEmpty() |
|
165 && yStack.isEmpty() |
|
166 && dxStack.isEmpty() |
|
167 && dyStack.isEmpty() |
|
168 && angleStack.isEmpty() |
|
169 && baselineShiftStack.isEmpty() |
|
170 && curx == 0.0f |
|
171 && cury == 0.0f; |
|
172 } |
|
173 |
|
174 void SVGCharacterLayoutInfo::processedSingleCharacter() |
|
175 { |
|
176 xStackWalk(); |
|
177 yStackWalk(); |
|
178 dxStackWalk(); |
|
179 dyStackWalk(); |
|
180 angleStackWalk(); |
|
181 baselineShiftStackWalk(); |
|
182 } |
|
183 |
|
184 void SVGCharacterLayoutInfo::processedChunk(float savedShiftX, float savedShiftY) |
|
185 { |
|
186 // baseline-shift doesn't span across ancestors, unlike dx/dy. |
|
187 curx += savedShiftX - shiftx; |
|
188 cury += savedShiftY - shifty; |
|
189 |
|
190 if (inPathLayout()) { |
|
191 shiftx = savedShiftX; |
|
192 shifty = savedShiftY; |
|
193 } |
|
194 |
|
195 // rotation also doesn't span |
|
196 angle = 0.0f; |
|
197 |
|
198 if (xStackChanged) { |
|
199 ASSERT(!xStack.isEmpty()); |
|
200 xStack.removeLast(); |
|
201 xStackChanged = false; |
|
202 } |
|
203 |
|
204 if (yStackChanged) { |
|
205 ASSERT(!yStack.isEmpty()); |
|
206 yStack.removeLast(); |
|
207 yStackChanged = false; |
|
208 } |
|
209 |
|
210 if (dxStackChanged) { |
|
211 ASSERT(!dxStack.isEmpty()); |
|
212 dxStack.removeLast(); |
|
213 dxStackChanged = false; |
|
214 } |
|
215 |
|
216 if (dyStackChanged) { |
|
217 ASSERT(!dyStack.isEmpty()); |
|
218 dyStack.removeLast(); |
|
219 dyStackChanged = false; |
|
220 } |
|
221 |
|
222 if (angleStackChanged) { |
|
223 ASSERT(!angleStack.isEmpty()); |
|
224 angleStack.removeLast(); |
|
225 angleStackChanged = false; |
|
226 } |
|
227 |
|
228 if (baselineShiftStackChanged) { |
|
229 ASSERT(!baselineShiftStack.isEmpty()); |
|
230 baselineShiftStack.removeLast(); |
|
231 baselineShiftStackChanged = false; |
|
232 } |
|
233 } |
|
234 |
|
235 bool SVGCharacterLayoutInfo::nextPathLayoutPointAndAngle(float glyphAdvance, float extraAdvance, float newOffset) |
|
236 { |
|
237 if (layoutPathLength <= 0.0f) |
|
238 return false; |
|
239 |
|
240 if (newOffset != FLT_MIN) |
|
241 currentOffset = startOffset + newOffset; |
|
242 |
|
243 // Respect translation along path (extraAdvance is orthogonal to the path) |
|
244 currentOffset += extraAdvance; |
|
245 |
|
246 float offset = currentOffset + glyphAdvance / 2.0f; |
|
247 currentOffset += glyphAdvance + pathExtraAdvance; |
|
248 |
|
249 if (offset < 0.0f || offset > layoutPathLength) |
|
250 return false; |
|
251 |
|
252 bool ok = false; |
|
253 FloatPoint point = layoutPath.pointAtLength(offset, ok); |
|
254 ASSERT(ok); |
|
255 |
|
256 curx = point.x(); |
|
257 cury = point.y(); |
|
258 |
|
259 angle = layoutPath.normalAngleAtLength(offset, ok); |
|
260 ASSERT(ok); |
|
261 |
|
262 return true; |
|
263 } |
|
264 |
|
265 bool SVGCharacterLayoutInfo::inPathLayout() const |
|
266 { |
|
267 return pathLayout; |
|
268 } |
|
269 |
|
270 void SVGCharacterLayoutInfo::setInPathLayout(bool value) |
|
271 { |
|
272 pathLayout = value; |
|
273 |
|
274 pathExtraAdvance = 0.0f; |
|
275 pathTextLength = 0.0f; |
|
276 pathChunkLength = 0.0f; |
|
277 } |
|
278 |
|
279 void SVGCharacterLayoutInfo::addLayoutInformation(InlineFlowBox* flowBox, float textAnchorStartOffset) |
|
280 { |
|
281 bool wasInitialLayout = isInitialLayout(); |
|
282 |
|
283 RenderSVGTextPath* textPath = toRenderSVGTextPath(flowBox->renderer()); |
|
284 Path path = textPath->layoutPath(); |
|
285 |
|
286 float baselineShift = calculateBaselineShift(textPath); |
|
287 |
|
288 layoutPath = path; |
|
289 layoutPathLength = path.length(); |
|
290 |
|
291 if (layoutPathLength <= 0.0f) |
|
292 return; |
|
293 |
|
294 startOffset = textPath->startOffset(); |
|
295 |
|
296 if (textPath->startOffset() >= 0.0f && textPath->startOffset() <= 1.0f) |
|
297 startOffset *= layoutPathLength; |
|
298 |
|
299 startOffset += textAnchorStartOffset; |
|
300 currentOffset = startOffset; |
|
301 |
|
302 // Only baseline-shift is handled through the normal layout system |
|
303 addStackContent(BaselineShiftStack, baselineShift); |
|
304 |
|
305 if (wasInitialLayout) { |
|
306 xStackChanged = false; |
|
307 yStackChanged = false; |
|
308 dxStackChanged = false; |
|
309 dyStackChanged = false; |
|
310 angleStackChanged = false; |
|
311 baselineShiftStackChanged = false; |
|
312 } |
|
313 } |
|
314 |
|
315 void SVGCharacterLayoutInfo::addLayoutInformation(SVGTextPositioningElement* element) |
|
316 { |
|
317 bool wasInitialLayout = isInitialLayout(); |
|
318 float baselineShift = calculateBaselineShift(element->renderer()); |
|
319 |
|
320 addStackContent(XStack, element->x(), element); |
|
321 addStackContent(YStack, element->y(), element); |
|
322 addStackContent(DxStack, element->dx(), element); |
|
323 addStackContent(DyStack, element->dy(), element); |
|
324 addStackContent(AngleStack, element->rotate()); |
|
325 addStackContent(BaselineShiftStack, baselineShift); |
|
326 |
|
327 if (wasInitialLayout) { |
|
328 xStackChanged = false; |
|
329 yStackChanged = false; |
|
330 dxStackChanged = false; |
|
331 dyStackChanged = false; |
|
332 angleStackChanged = false; |
|
333 baselineShiftStackChanged = false; |
|
334 } |
|
335 } |
|
336 |
|
337 void SVGCharacterLayoutInfo::addStackContent(StackType type, SVGNumberList* list) |
|
338 { |
|
339 unsigned length = list->numberOfItems(); |
|
340 if (!length) |
|
341 return; |
|
342 |
|
343 PositionedFloatVector newLayoutInfo; |
|
344 |
|
345 // TODO: Convert more efficiently! |
|
346 ExceptionCode ec = 0; |
|
347 for (unsigned i = 0; i < length; ++i) { |
|
348 float value = list->getItem(i, ec); |
|
349 ASSERT(!ec); |
|
350 |
|
351 newLayoutInfo.append(value); |
|
352 } |
|
353 |
|
354 addStackContent(type, newLayoutInfo); |
|
355 } |
|
356 |
|
357 void SVGCharacterLayoutInfo::addStackContent(StackType type, SVGLengthList* list, const SVGElement* context) |
|
358 { |
|
359 unsigned length = list->numberOfItems(); |
|
360 if (!length) |
|
361 return; |
|
362 |
|
363 PositionedFloatVector newLayoutInfo; |
|
364 |
|
365 ExceptionCode ec = 0; |
|
366 for (unsigned i = 0; i < length; ++i) { |
|
367 float value = list->getItem(i, ec).value(context); |
|
368 ASSERT(!ec); |
|
369 |
|
370 newLayoutInfo.append(value); |
|
371 } |
|
372 |
|
373 addStackContent(type, newLayoutInfo); |
|
374 } |
|
375 |
|
376 void SVGCharacterLayoutInfo::addStackContent(StackType type, const PositionedFloatVector& list) |
|
377 { |
|
378 switch (type) { |
|
379 case XStack: |
|
380 xStackChanged = true; |
|
381 xStack.append(list); |
|
382 break; |
|
383 case YStack: |
|
384 yStackChanged = true; |
|
385 yStack.append(list); |
|
386 break; |
|
387 case DxStack: |
|
388 dxStackChanged = true; |
|
389 dxStack.append(list); |
|
390 break; |
|
391 case DyStack: |
|
392 dyStackChanged = true; |
|
393 dyStack.append(list); |
|
394 break; |
|
395 case AngleStack: |
|
396 angleStackChanged = true; |
|
397 angleStack.append(list); |
|
398 break; |
|
399 default: |
|
400 ASSERT_NOT_REACHED(); |
|
401 } |
|
402 } |
|
403 |
|
404 void SVGCharacterLayoutInfo::addStackContent(StackType type, float value) |
|
405 { |
|
406 if (value == 0.0f) |
|
407 return; |
|
408 |
|
409 switch (type) { |
|
410 case BaselineShiftStack: |
|
411 baselineShiftStackChanged = true; |
|
412 baselineShiftStack.append(value); |
|
413 break; |
|
414 default: |
|
415 ASSERT_NOT_REACHED(); |
|
416 } |
|
417 } |
|
418 |
|
419 void SVGCharacterLayoutInfo::xStackWalk() |
|
420 { |
|
421 unsigned i = 1; |
|
422 |
|
423 while (!xStack.isEmpty()) { |
|
424 PositionedFloatVector& cur = xStack.last(); |
|
425 if (i + cur.position() < cur.size()) { |
|
426 cur.advance(i); |
|
427 break; |
|
428 } |
|
429 |
|
430 i += cur.position(); |
|
431 xStack.removeLast(); |
|
432 xStackChanged = false; |
|
433 } |
|
434 } |
|
435 |
|
436 void SVGCharacterLayoutInfo::yStackWalk() |
|
437 { |
|
438 unsigned i = 1; |
|
439 |
|
440 while (!yStack.isEmpty()) { |
|
441 PositionedFloatVector& cur = yStack.last(); |
|
442 if (i + cur.position() < cur.size()) { |
|
443 cur.advance(i); |
|
444 break; |
|
445 } |
|
446 |
|
447 i += cur.position(); |
|
448 yStack.removeLast(); |
|
449 yStackChanged = false; |
|
450 } |
|
451 } |
|
452 |
|
453 void SVGCharacterLayoutInfo::dxStackWalk() |
|
454 { |
|
455 unsigned i = 1; |
|
456 |
|
457 while (!dxStack.isEmpty()) { |
|
458 PositionedFloatVector& cur = dxStack.last(); |
|
459 if (i + cur.position() < cur.size()) { |
|
460 cur.advance(i); |
|
461 break; |
|
462 } |
|
463 |
|
464 i += cur.position(); |
|
465 dxStack.removeLast(); |
|
466 dxStackChanged = false; |
|
467 } |
|
468 } |
|
469 |
|
470 void SVGCharacterLayoutInfo::dyStackWalk() |
|
471 { |
|
472 unsigned i = 1; |
|
473 |
|
474 while (!dyStack.isEmpty()) { |
|
475 PositionedFloatVector& cur = dyStack.last(); |
|
476 if (i + cur.position() < cur.size()) { |
|
477 cur.advance(i); |
|
478 break; |
|
479 } |
|
480 |
|
481 i += cur.position(); |
|
482 dyStack.removeLast(); |
|
483 dyStackChanged = false; |
|
484 } |
|
485 } |
|
486 |
|
487 void SVGCharacterLayoutInfo::angleStackWalk() |
|
488 { |
|
489 unsigned i = 1; |
|
490 |
|
491 while (!angleStack.isEmpty()) { |
|
492 PositionedFloatVector& cur = angleStack.last(); |
|
493 if (i + cur.position() < cur.size()) { |
|
494 cur.advance(i); |
|
495 break; |
|
496 } |
|
497 |
|
498 i += cur.position(); |
|
499 angleStack.removeLast(); |
|
500 angleStackChanged = false; |
|
501 } |
|
502 } |
|
503 |
|
504 void SVGCharacterLayoutInfo::baselineShiftStackWalk() |
|
505 { |
|
506 if (!baselineShiftStack.isEmpty()) { |
|
507 baselineShiftStack.removeLast(); |
|
508 baselineShiftStackChanged = false; |
|
509 } |
|
510 } |
|
511 |
|
512 } // namespace WebCore |
|
513 |
|
514 #endif // ENABLE(SVG) |