Debian Codegraph @

devel-docs/text_layout.rst

raw
1 Text layout
2 ===========
3
4 This document describes the state of text layout in librsvg as of
5 version 2.55.90, and how I want to overhaul it completely for SVG2.
6
7 Status as of librsvg 2.55.90
8 ----------------------------
9
10 Basic supported features:
11
12 - Librsvg supports the elements ``text``, ``tspan``, ``a`` inside text,
13 and ``tref`` (deprecated in SVG2, but kept around for SVG1.1
14 compatibility). See below for the ``x/y/dx/dy`` attributes; librsvg
15 supports single-number values in these.
16
17 - ``text-anchor``.
18
19 - SVG1.1 values for ``direction``, ``writing-mode``. Non-LTR or
20 vertical text layout is very much untested.
21
22 - SVG1.1 values for ``letter-spacing``, ``baseline-shift``,
23 ``text-decoration``.
24
25 - ``font`` (shorthand), ``font-family``, ``font-size``,
26 ``font-stretch``, ``font-style``, ``font-variant``, ``font-weight``.
27
28 - ``text-rendering``.
29
30 - ``unicode-bidi`` and ``direction``. This is done for each text
31 span, but not for the whole ``<text>`` element yet. See below for
32 details.
33
34 Major missing features:
35
36 - ``text-orientation`` and ``glyph-orientation-vertical`` fallbacks,
37 SVG2 values for ``writing-mode``.
38
39 - SVG2 ``white-space`` handling. This deprecates ``xml:space`` from
40 SVG1.1.
41
42 - Support for multiple values in each of the attributes ``x/y/dx/dy``
43 from the ``text`` and ``tspan`` elements. Librsvg supports a single
44 value for each attribute, whereas SVG allows for multiple values —
45 these then get used to individually position “typographic characters”
46 (Pango clusters). In effect, librsvg’s single values for each of
47 those attributes mean that each text span can be positioned
48 independently, but not each character.
49
50 - Relatedly, the ``rotate`` attribute is not supported. In SVG it also
51 allows multiple values, one for each character.
52
53 - ``glyph-orientation-vertical`` (note that
54 ``glyph-orientation-horizontal`` is deprecated in SVG2).
55
56 - ``textPath`` is not supported at all. This will be made much easier
57 by implementing ``x/y/dx/dy/rotation`` first, since each character
58 needs to be positioned and oriented individually.
59
60 - ``@font-face`` and WOFF fonts.
61
62 - Emoji got inadvertently broken; see the "Emoji" section below.
63
64 Other missing features:
65
66 - ``display`` and ``visibility`` are not very well tested for the
67 sub-elements of ``<text>``.
68
69 - SVG2 text with a content area / multi-line / wrapped text:
70 ``inline-size``, ``shape-inside``, ``shape-subtract``,
71 ``shape-image-threshold``, ``shape-margin``, ``shape-padding``. This
72 is lower priority than the features above. Also the related
73 properties ``text-overflow``,
74
75 - ``text-align`` (shorthand), ``text-align-all``, ``text-align-last``,
76 ``text-indent``, ``word-spacing``.
77
78 - Baselines: ``vertical-align`` (shorthand), ``dominant-baseline``,
79 ``alignment-baseline``, ``baseline-source``, and SVG2 values for
80 ``baseline-shift``. Note that Pango doesn’t provide baseline
81 information yet.
82
83 - ``line-height`` (parsed, but not processed).
84
85 - SVG2 ``text-decoration``, which translates to
86 ``text-decoration-line``, ``text-decoration-style``,
87 ``text-decoration-color``.
88
89 - ``font-feature-settings``, ``font-kerning``, ``font-size-adjust``.
90
91 - CSS Text 3/4 features not mentioned here.
92
93 Features that will not be implemented:
94
95 - SVG1.1 features like ``<font>`` and the
96 ``glyph-orientation-horizontal`` property, that were deprecated for
97 SVG2.
98
99 Roadmap summary
100 ---------------
101
102 Since librsvg 2.52.1 I’ve started to systematically improve text
103 support. Many thanks to Behdad Esfahbod, Khaled Ghetas, Matthias Clasen
104 for their advice and inspiration.
105
106 First, I want to get **bidi** to a state where it is reliable, at least
107 as much as LTR languages with Latin text are reliable right now:
108
109 - Add tests for the different combinations of ``text-anchor`` and
110 ``direction``; right now there are only a few tested combinations.
111
112 - Test and implement multiply-nested changes of direction. I think only
113 a single level works right now.
114
115 - Even if white-space handling remains semi-broken, I think it’s more
116 important to have “mostly working” bidi than completely accurate
117 white-space handling and layout.
118
119 Second, actually overhaul librsvg’s text engine by implementing the SVG2
120 text layout algorithm:
121
122 - Implement the ``text-orientation`` property, and implement fallbacks
123 from the deprecated ``glyph-orientation-vertical`` to it. If this
124 turns out to be hard with the current state of the code, I will defer
125 it until the SVG2 text layout algorithm below.
126
127 - Implement the SVG2 text layout algorithm and ``white-space`` handling
128 at the same time. See the detailed roadmap below.
129
130 Third, implement all the properties that are not critical for the text
131 layout algorithm, and things like ``@font-face``. Those can be done
132 gradually, but I feel the text layout algorithm has to be done all in a
133 single step.
134
135 Architecture notes
136 ------------------
137
138 A common theme over the next subsections is, "we need a single
139 ``pango::Layout`` per ``<text>`` element". Keep that in mind as the
140 main goal of initial refactoring.
141
142 Chunks and spans
143 ~~~~~~~~~~~~~~~~
144
145 Librsvg implements a limited subset the `text layout as per SVG1.1
146 <https://www.w3.org/TR/SVG11/text.html>`_, which was feasible to
147 implement in terms of *chunks* and *spans*.
148
149 A *span* is an ``<tspan>`` element, or some character content inside ``<text>``.
150
151 When a ``tspan`` explicitly lists ``x`` or ``y`` attributes, it
152 creates a new *chunk*. A text chunk defines an absolutely-positioned
153 sequence of spans.
154
155 This is why you'll see that the code does this; start at
156 :internals:struct-method:`rsvg::text::Text::draw`:
157
158 - Start with an empty list of chunks
159 (:internals:struct-method:`rsvg::text::Text::make_chunks`).
160 Push an empty initial chunk defined by the ``x`` and ``y``
161 coordinates of the ``<text>`` element.
162
163 - Recursively call :internals:fn:`rsvg::text::children_to_chunks`
164 on the children of the ``<text>`` element, to create chunks and
165 spans for them.
166
167 - :internals:struct-method:`rsvg::text::TSpan::to_chunks`
168 sees if the span has ``x`` or ``y`` attributes; if so, it pushes a
169 new empty chunk with those coordinates. Then it recursively calls
170 ``children_to_chunks`` to grab its character content and children.
171
172 - Later, ``Text::draw`` takes the list of chunks and their spans, and
173 converts them into a list of :internals:struct:`rsvg::text::MeasuredChunk`.
174 This process turns each span into a
175 :internals:struct:`rsvg::text::MeasuredSpan`. The key element here is to
176 create a ``pango::Layout`` for each span, and ask it for its size.
177
178 - Then, ``Text::draw`` takes the list of ``MeasuredChunk`` and turns them
179 into a list of :internals:struct:`rsvg::text::PositionedChunk`. Each of
180 those builds a list of :internals:struct:`rsvg::text::PositionedSpan` based
181 on the span's own text advance, plus the span's ``dx``/``dy`` attributes.
182
183 **Note about SVG2:** The `text layout algorithm for SVG2
184 <https://www.w3.org/TR/SVG2/text.html#TextLayoutAlgorithm>`_ is very
185 different from the above. It mostly dispenses with explicit
186 computation of chunks with spans, and instead, for each glyph it
187 stores a flag that says whether the glyph is at the beginning of a
188 chunk.
189
190
191 Layouts and spans
192 ~~~~~~~~~~~~~~~~~
193
194 Librsvg creates a ``pango::Layout`` for each
195 text span in a ``<text>`` element, whether it comes from a ``<tspan>``
196 or not. For example, ``<text>A <tspan>B</tspan> C</text>`` has three
197 spans, and three Pango layouts created for it. Each span's
198 ``pango::Layout`` gets configured via ``pango::AttrList`` with the
199 styles it needs (bold/italic, font size, etc.).
200
201 When a ``pango::AttrList`` gets created, each individual attribute has
202 a start/end index based on the byte offsets for the corresponding
203 characters. Currently, **all the attributes for a span occupy the whole text span**. So, for something like
204
205 .. code-block:: xml
206
207 <text>
208 Hello
209 <tspan font-weight="bold">
210 BOLD
211 </tspan>
212 World
213 </text>
214
215 three ``pango::Layout`` objects get created, with ``Hello``, ``BOLD``,
216 and ``World``, and the second one has a ``pango::AttrList`` that spans
217 its entire 4 bytes. (There's probably some whitespace in the span,
218 and the attribute list would include it — I'm saying "4" since it is
219 easy to visualize for example purposes.) So, currently there are
220 three ``PangoLayout`` and each with a ``PangoAttrList``:
221
222 .. image:: assets/multiple-layouts.jpg
223
224 However, this is sub-optimal. Ideally there should be a *single*
225 ``pango::Layout`` for a single string, ``Hello BOLD World``, and the
226 attribute list should have a boldface attribute just for the word in
227 the middle.
228
229 .. image:: assets/single-layout.jpg
230
231 Why? Two reasons: shaping needs to happen across spans (it doesn't
232 right now), and the handling for ``unicode-bidi`` and ``direction``
233 need to be able to work across nested spans (they work with a single
234 level of nesting right now). Read the "Bidi handling" section below
235 for more info.
236
237 The ``add_pango_attributes`` function is already able to handle
238 substrings of a ``pango::Layout``; it's just that it is always called
239 with the whole layout right now.
240
241 **The initial refactoring:** Change the text handling code to first
242 gather all the character content inside a ``<text>`` into a single
243 string, while keeping track of the offsets of each span. Make the
244 ``pango::AttrList`` taking those offsets into account. Then, feed
245 that single string to a ``pango::Layout``, with the attributes.
246
247 **Further work:** Don't just paint the layout, but iterate it / break
248 it up into individual ``pango::GlyphString``, so librsvg can lay out
249 each individual glyph itself using the SVG2 layout algorithm.
250
251 Be careful with PDF output when handling individual glyphs: grep for
252 ``can_use_text_as_path`` in ``drawing_ctx.rs``.
253
254
255 Bidi handling
256 ~~~~~~~~~~~~~
257
258 The ``unicode-bidi`` and ``direction`` properties get handled
259 together. The :internals:struct:`rsvg::text::BidiControl`
260 struct computes which Unicode control characters need to be inserted
261 at the start and end of a ``<tspan>``'s text; SVG authors use these
262 properties to override text direction when inserting LTR or RTL text
263 within each other.
264
265 Unfortunately, these control characters can only really work for
266 nested levels of embedding **if the whole text is in a single
267 ``pango::Layout``**. Per the previous section, librsvg doesn't do
268 this yet.
269
270 :pr:`621` implemented the SVG2 values for the ``unicode-bidi`` property.
271 You may want to read the detailed commit messages there, and the
272 discussion in the merge request, to see details of future development.
273
274
275
276 Detailed roadmap
277 ----------------
278
279
280
281 Add tests for combinations of ``text-anchor`` and ``direction``
282 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
283
284 These are easy to add now that librsvg’s tests make use of the Ahem
285 font, in which each glyph is a 1x1 em square.
286
287 Implement the ``text-orientation`` property
288 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289
290 This may just be the property parser and hooking it up to the machinery
291 for properties. Actual processing may be easier to do in the SVG2 text
292 layout algorithm, detailed below.
293
294 Implement the SVG2 text layout algorithm and ``white-space`` handling.
295 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296
297 **Shaping:** One thing librsvg does wrong is that for each ``<tspan>``,
298 or for each synthesized text span from a ``<text>`` element, it creates
299 a separate ``pango::Layout``. This means that text shaping is not done
300 across element boundaries (SVG2 requirement). Implementing this can be
301 done by creating a string by recursively concatenating the character
302 content of each ``<text>`` element and its children, and adding
303 ``pango::Attribute``\ s with the proper indexes based on each child’s
304 character length. This creates an un-shaped string in logical order with
305 all the characters inside the ``<text>``, to be used in the next steps.
306
307 Pango details: create a single ``pango::Layout``, per ``<text>``
308 element, with ``pango::Attribute`` for each text span. Set the layout to
309 ``set_single_paragraph_mode()`` so it does not break newlines. Pango
310 will then translate them to characters in the ``Layout``, and the
311 white-space handling and SVG2 text layout algorithm below can detect
312 them.
313
314 **White-space handling:** SVG2 has a new ``white-space`` property that
315 obsoletes ``xml:space`` from SVG1.1. Implementing this depends on the
316 concatenated string from the steps above, so that white-space can be
317 collapsed on the result. Maybe this needs to be done before inserting
318 bidi control characters, or maybe not, if the state machine is adjusted
319 to ignore the control characters.
320
321 **SVG2 text layout algorithm:** This is the big one. The spec has
322 pseudocode. It depends on the shaping results from Pango, and involves
323 correlating “typographic characters” (Pango clusters) with the
324 un-shaped string in logical order from the “Shaping”, and the
325 information about `discarded white-space characters
326 <https://www.w3.org/TR/css-text-3/#white-space-processing>`_. The
327 complete text layout algorithm would take care of supporting
328 multi-valued ``x/y/dx/dy/rotate``, ``textPath`` (see below), plus bidi
329 and vertical text.
330
331 Do look at the issues in the `svgwg repository at GitHub
332 <https://github.com/w3c/svgwg/tree/master>`_ - there are a couple that
333 mention bugs in the spec's pseudocode for the text layout algorithm.
334
335 Bidi embedding
336 ~~~~~~~~~~~~~~
337
338 When there are nested left-to-right (LTR) and right-to-left (RTL) languages in a text element, this must happen:
339
340 1. Extract the text content from the spans, but...
341
342 2. Insert Unicode control characters at each embedding level (at each span boundary)...
343
344 3. So that Pango/Harfbuzz/etc. will know when text direction must change.
345
346 For example, consider this SVG from
347 :source:`rsvg/tests/fixtures/text/unicode-bidi-override.svg`:
348
349 .. code:: xml
350
351 <?xml version="1.0" encoding="utf-8"?>
352 <svg xmlns="http://www.w3.org/2000/svg" width="600" height="600">
353 <rect x="0" y="0" width="100%" height="100%" fill="white"/>
354
355 <text x="100" y="100" font-family="Ahem" font-size="20">ÉAp<tspan direction="rtl" unicode-bidi="bidi-override">ÉAp</tspan>ÉAp</text>
356 </svg>
357
358 It gets rendered like this (see the description of the Ahem font below to make sense of this):
359
360 .. image:: assets/unicode-bidi.jpg
361
362 Let's break it up part by part:
363
364 * The ``<text>`` element starts by default in left-to-right (LTR) direction.
365
366 * The first ``ÉAp`` gets laid out trivially, from left to right.
367
368 * The second ``ÉAp`` is in a ``tspan`` with ``direction="rtl"`` and ``unicode-bidi="bidi-override"``. It gets laid out right-to-left, and looks like ``pAÉ``, but for the platform libraries to know this, it needs to be surrounded with Unicode control characters "Right to Left Override (RLO)" and "Pop Directional Formatting (PDF)". This is done in `from_unicode_bidi_and_direction() <https://gitlab.gnome.org/GNOME/librsvg/-/blob/5d43d27adec414515669a817e015832c85ec7232/rsvg/src/text.rs?page=2#L1177-1198>`_ and `wrap_with_direction_control_chars() <https://gitlab.gnome.org/GNOME/librsvg/-/blob/5d43d27adec414515669a817e015832c85ec7232/rsvg/src/text.rs?page=2#L1200-1216>`_.
369
370 * The third ``ÉAp`` gets laid out again from left-to-right.
371
372
373
374 Text rendering
375 ~~~~~~~~~~~~~~
376
377 Librsvg is moving towards a “render tree” or “display list” model,
378 instead of just rendering everything directly while traversing the DOM
379 tree.
380
381 Currently, the text layout process generates a ``layout::Text`` object,
382 which is basically an array of ``pango::Layout`` with extra information.
383
384 It should be possible to explode these into ``pango::GlyphItem`` or
385 ``pango::GlyphString`` and annotate these with ``x/y/rotate``
386 information, which will be the actual results of the SVG2 text layout
387 algorithm.
388
389 Although currently Pango deals with underlining, it may be necessary to
390 do that in librsvg instead - I am not sure yet how ``textPath`` or
391 individually-positioned ``x/y/dx/dy/rotate`` interact with underlining. See also
392
393 Pango internals
394 ~~~~~~~~~~~~~~~
395
396 ::
397
398 /**
399 * pango_renderer_draw_glyph_item:
400 * @renderer: a `PangoRenderer`
401 * @text: (nullable): the UTF-8 text that @glyph_item refers to
402 * @glyph_item: a `PangoGlyphItem`
403 * @x: X position of left edge of baseline, in user space coordinates
404 * in Pango units
405 * @y: Y position of left edge of baseline, in user space coordinates
406 * in Pango units
407 *
408 * Draws the glyphs in @glyph_item with the specified `PangoRenderer`,
409 * embedding the text associated with the glyphs in the output if the
410 * output format supports it.
411 *
412 * This is useful for rendering text in PDF.
413 * ...
414 */
415
416 Note that embedding text in PDF to make it selectable involves passing a
417 non-null ``text`` to pango_renderer_draw_glyph_item(). We’ll have to
418 implement this by hand, probably.
419
420 Wrapped text in a content area
421 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422
423 This roadmap does not consider the implementation fo wrapped text yet.
424
425 User-provided fonts, ``@font-face`` and WOFF
426 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
427
428 This involves changes to the CSS machinery, to parse the ``@font-face``
429 at-rule. Librsvg would also have to obtain the font and feed it to
430 FontConfig. I am not sure if FontConfig can deal with WOFF just like
431 with normal ``.ttf`` files.
432
433 See the issue on the :issue:`Future of the pango dependency <876>`
434 for lots of goodies which may come in handy.
435
436 Emoji is broken
437 ~~~~~~~~~~~~~~~
438
439 :issue:`599` is a terrible bug in Pango, which causes it to report
440 incorrect metrics when text is scaled non-proportionally (e.g. different
441 scale factors for the X/Y dimensions). Librsvg works around this by
442 converting all text to Bézier paths, then scaling the paths, and then
443 stroking/filling them.
444
445 However, this breaks emoji - :issue:`911`, since converting its glyphs
446 to paths loses the color information.
447
448 Two strategies to fix this; there may be more:
449
450 - Detect if the text is scaled proportionally (this is the common
451 case), and use the old code for that, without converting text to
452 paths. This may be easy to do? Grep for ``can_use_text_as_path``
453 in ``drawing_ctx.rs`` which already has some of the logic but for
454 handling PDF output.
455
456 - Do the whole "split a ``pango::Layout`` into glyphs" from above;
457 keep handling individual glyphs as paths, and special-case emoji to
458 render them via Cairo.
459
460
461 Issues
462 ------
463
464 :issue:`795` - Implement SVG2 white-space behavior.
465
466
467 Issues that have not been filed yet
468 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
469
470 From the spec: “It is possible to apply a gradient, pattern, clipping
471 path, mask or filter to text.” We need better tests for the
472 ``objectBoundingBox`` of the whole ``<text>``; I think :issue:`they are
473 wrong for vertical text <55>`, and this shows up when filling its spans
474 with gradients or patterns.
475
476 Clip/mask/filter do not work on individual spans yet. I am not sure
477 if their ``objectBoundingBox`` refers to the whole ``<text>`` or just
478 the span.
479
480 Multiply-nested changes of text direction / bidi overrides; see the
481 "Bidi handling" section above.
482
483 Glossary so I don’t have to check the Pango docs every time
484 -----------------------------------------------------------
485
486 PangoItem - A range within the user’s string that has the same
487 language/script/direction/level/etc. (Logical order).
488
489 PangoLayoutRun - same as PangoGlyphItem - a pair of PangoItem and the
490 PangoGlyphString it generated during shaping. (Visual order).
491
492 PangoGlyphString - The glyphs generated for a single PangoItem.
493
494 PangoGravityHint - Defines how horizontal scripts should behave in a
495 vertical context.
496
497
498 Development plan
499 ----------------
500
501 There's a few of ways of implementing the SVG2 text algorithm, while
502 keeping the existing SVG1.1 code working:
503
504 * Try to refactor ``rsvg/src/text.rs`` gradually to SVG2. This is probably
505 overkill; the SVG1.1 algorithm there is very much oriented towards
506 just thinking of chunks ans spans, and the SVG2 algorithm subsumes
507 those.
508
509 * Disable all the tests that use text, and write a new implementation.
510 Enable the tests gradually as features appear.
511
512 * Implement a ``<text2>`` element with all the new code for SVG2.
513 Don't disable existing tests; rather, write a test suite based on
514 the Ahem font just for ``<text2>`` that lets us build things from
515 the ground up.
516
517 There is code in ``rsvg/src/text.rs`` that will still be useful: the
518 conversions between SVG types and Pango types, the tables with Unicode
519 directional formatting characters, and some of the utility functions
520 for Pango.
521
522 I think the third scheme is the best one to follow for internships:
523
524 * We can preserve the current code as it is; it works fine for many purposes.
525
526 * We can write a new test suite for text that we *know* is
527 comprehensive, instead of relying on the sketchy tests from the
528 SVG1.1 test suite. The Ahem font should make it easy to have
529 reftest-style tests and let us implement things in a "constructive"
530 fashion from the ground up.
531
532 * We can compare the output of ``<text2>`` and ``<text>`` without
533 hackery or conditional compilation, to ensure that new new
534 implementation produces as reasonable results as the old one.
535
536 * Eventually we can remove the old implementation, rename ``text2`` to
537 ``text`` everywhere, and just leave the new implementation in place.
538
539
540 Reproducible text rendering for the test suite
541 ----------------------------------------------
542
543 Librsvg uses the pango/harfbuzz/freetype/fontconfig stack, so it
544 assumes that fonts are installed in some system-wide location, and
545 perhaps the user's own ``~/.fonts``. However, for the test suite we
546 would like to have a 100% predictable set of available fonts, and a
547 reproducible configuration for things which even *have* configuration
548 like fontconfig.
549
550 Briefly:
551
552 * Pango - does high-level text layout for GTK and librsvg; uses all
553 the following libraries. You give it a Unicode string, and it will
554 do bidi/shaping/layout and render it for you.
555
556 * Harfbuzz - Does text shaping.
557
558 * Freetype - Renders glyphs.
559
560 * Fontconfig - Does "font enumeration", or finding the fonts that are
561 installed on the system, and substitues missing fonts; if you ask
562 for "Times New Roman" but don't have it installed, fontconfig can
563 give you another serif font instead.
564
565 Librsvg mostly only uses Pango directly, with one exception. The code
566 to set up the test suite calls Fontconfig to set up a minimal, custom
567 font map. We want to make the fonts available from
568 ``rsvg/tests/resources/`` *and nothing else* so that we know exactly
569 what will be used while rendering test files. There's also a custom
570 ``fonts.conf`` there for Fontconfig, to set up some minimal
571 substitutions.
572
573
574 Ahem font for tests
575 -------------------
576
577 A big problem when writing tests for a text layout engine is that font
578 rendering can change in extremely subtle ways depending on the
579 underlying font-rendering libraries, the available fonts, the
580 rendering options, etc. If a few pixels change in the antialiasing
581 around glyphs, or if glyphs shift around by sub-pixel distances, is
582 that a failed test or not?
583
584 To make it easy to write reproducible tests, there is the `Ahem font
585 <https://web-platform-tests.org/writing-tests/ahem.html>`_. It is
586 useless for human-readable text, but **most glyphs are 100% simple
587 squares** or simple rectangles that you can compare easily to
588 reference images.
589
590 This is the string ``A`` rendered in the Ahem font, with red lines
591 that cross at its anchor point and baseline:
592
593 .. image:: assets/ahem-a.png
594
595 .. literalinclude:: assets/ahem-a.svg
596 :language: xml
597
598 Note the following:
599
600 * This is a single glyph ``A`` that renders as a square.
601
602 * The square is exactly 50 pixels tall and wide, since we specified
603 ``50px`` in the ``font:`` property.
604
605 * The ascent is 40 pixels, above the horizontal red line, and the
606 descent is 10 pixels below it.
607
608 Now let's render two glyphs ``AB``, centered:
609
610 .. image:: assets/ahem-ab.png
611
612 .. literalinclude:: assets/ahem-ab.svg
613 :language: xml
614
615 Now the same as before, but with each glyph in a separate ``tspan`` of a different color:
616
617 .. image:: assets/ahem-ab-color.png
618
619 .. literalinclude:: assets/ahem-ab-color.svg
620 :language: xml
621
622 What if we need to test some things but actually be able to
623 differentiate glyphs? Here, the glyphs for ``A``, ``p`` and ``É`` are
624 rendered different. `See the available glyphs
625 <https://hydrock.github.io/AhemFont/>`_. In any case, all the glyphs
626 fit in the em-square and are just rectangles that cover different
627 parts of that area.
628
629 .. image:: assets/ahem-different-glyphs.png
630
631 .. literalinclude:: assets/ahem-different-glyphs.svg
632 :language: xml
633
634
635 Details on the Ahem font
636 ~~~~~~~~~~~~~~~~~~~~~~~~
637
638 * `Ahem font, main page <https://web-platform-tests.org/writing-tests/ahem.html>`_
639 * `Rendered glyphs for easy reference <https://hydrock.github.io/AhemFont/>`_
640 * `Source for the previous link, with interesting examples <https://github.com/Hydrock/AhemFont?tab=readme-ov-file>`_
641 * `Ahem font README with list of glyphs <https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README>`_
642 * `Fonts for CSS testing <https://www.w3.org/Style/CSS/Test/Fonts/>`_
643
644 How librsvg's test suite uses Ahem
645 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
646
647 If you grep for ``Ahem`` in the test files (``rsvg/tests/``), you will
648 find how it is used to test the layout of various aspects of the
649 ``<text>`` element.
650
651 When implementing the SVG2 text layout algorithm, we should have
652 fine-grained tests for each little feature, to ensure that it produces
653 the expected layout. Regretfully, the original SVG1.1 test suite only
654 has very high level tests for text layout, which don't make it easy to
655 automatically test if the building blocks of the code are correct.
656
657 In the Ahem font, for each glyph 4/5 of the heigth are above the
658 baseline, and 1/5 of the height is below the baseline. This means
659 that to get whole-pixel-exact rendering, your glyphs should have a
660 size that is a multiple of 5. In the examples above, we used font
661 heights of 40px and 50px, which are of course multiples of 5.
662
663 Some ideas for the ``<text2>`` tests with Ahem
664 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665
666 * Testing general layout and ``text-anchor``.
667
668 * Testing bounding boxes.
669
670 * Use different glyphs and colors to test bidi embedding. For
671 example, here ``RGB`` renders as ``BGR`` due to ``direction="rtl"``:
672
673 .. image:: assets/ahem-rtl.png
674
675 .. literalinclude:: assets/ahem-rtl.svg
676 :language: xml