Debian Codegraph @

devel-docs/viewport.rst

raw
1 Viewport abstraction
2 ====================
3
4 **Status:** implemented as of 2025/Apr/09.
5
6 :issue:`298` is about unifying several concepts into a Viewport abstraction.
7 This chapter attempts to explain that.
8
9 SVG has the concept of `establishing a new viewport
10 <https://svgwg.org/svg2-draft/coords.html#EstablishingANewSVGViewport>`_,
11 in particular for the elements ``<svg>`` and ``<symbol>``. The
12 viewport is set up like this:
13
14 - Apply the element's ``transform``.
15
16 - Compute a transform / coordinate system from the element's
17 size+position (``x``, ``y``, ``width``, ``height``), the
18 ``preserveAspectRatio`` and ``viewBox`` attributes.
19
20 - Set up a clipping rectangle if the element's ``overflow`` property
21 says so.
22
23 However, that mechanism is general enough that it can also be made to
24 work when rendering the elements ``<image>``, ``<marker>``, and
25 ``<pattern>``. They have their own way of specifying a size (e.g. the
26 marker-specific ``markerWidth`` and ``markerHeight`` attributes), but
27 they also need to compute a new transform, set up clipping, etc.
28
29 The original code for librsvg reimplemented the mechanism above
30 independently for each of ``<marker>``, ``<symbol>``, etc., by doing
31 direct calls to Cairo to set up a transform and a clipping rectangle.
32 Unfortunately, during the initial port to Rust we did not identify
33 this pattern to gather the various implementations and abstract them
34 in a single place. Gradual refactoring led to all calls to Cairo
35 happening in ``drawing_ctx.rs``, instead of all over the code. Still,
36 the various versions still exist, with slightly different mechanisms
37 for each.
38
39 What I'd like to do
40 -------------------
41
42 The idea in #298 is to consolidate all the parameters needed for a
43 viewport, as mentioned above, into a single place. Now that the
44 structs for a :doc:`render_tree` are starting to take hold, I think we
45 can do these:
46
47 - Move the ``Viewport`` struct from ``drawing_ctx.rs`` into ``layout.rs``.
48
49 - Add the necessary fields from the previous section (element's
50 transform, perhaps moved from the ``StackingCtx``), the viewport
51 size, ``preserveAspectRatio``, and ``overflow``.
52
53 - (Look at the Firefox source code a bit before doing that; they have
54 nice code for it.)
55
56 - One by one, migrate each part of librsvg that requires it to using
57 the new ``Viewport`` abstraction with everything in it. We can
58 probably start with ``<svg>`` and ``<use> / <symbol>``; markers and
59 patterns may need a little extra untangling.
60