| 1 |
How to add a new CSS property |
| 2 |
============================= |
| 3 |
|
| 4 |
This document is a little tour on how to add support for a CSS property |
| 5 |
to librsvg. We will implement the |mask-type|_ property from the |
| 6 |
**CSS Masking Module Level 1** specification. |
| 7 |
|
| 8 |
What is ``mask-type``? |
| 9 |
---------------------- |
| 10 |
|
| 11 |
The spec says about |mask-type|_: |
| 12 |
|
| 13 |
The mask-type property defines whether the content of the mask |
| 14 |
element is treated as as luminance mask or alpha mask, as described |
| 15 |
in Calculating mask values. |
| 16 |
|
| 17 |
A **luminance mask** takes the RGB values of each pixel, converts them |
| 18 |
to a single luminance value, and uses that as a mask. |
| 19 |
|
| 20 |
An **alpha mask** just takes the alpha value of each pixel and uses it |
| 21 |
as a mask. |
| 22 |
|
| 23 |
The only mask type that SVG1.1 supported was luminance masks; there |
| 24 |
wasn’t even a ``mask-type`` property back then. The SVG2 spec removed |
| 25 |
descriptions of masking, and offloaded them to the `CSS Masking Module |
| 26 |
Level 1 <https://www.w3.org/TR/css-masking-1/>`__ specification, which |
| 27 |
it adds the ``mask-type`` property and others as well. |
| 28 |
|
| 29 |
Let’s start by figuring out how to read the spec. |
| 30 |
|
| 31 |
What the specification says |
| 32 |
--------------------------- |
| 33 |
|
| 34 |
The specification for ``mask-type`` is in |
| 35 |
https://www.w3.org/TR/css-masking-1/#the-mask-type |
| 36 |
|
| 37 |
In the specs, most of the descriptions for properties start with a table |
| 38 |
that summarizes the property. For example, if you visit that link, you |
| 39 |
will find a table that starts with these items: |
| 40 |
|
| 41 |
- **Name:** ``mask-type`` |
| 42 |
- **Value:** ``luminance | alpha`` |
| 43 |
- **Initial:** ``luminance`` |
| 44 |
- **Applies to:** mask elements |
| 45 |
- **Inherited:** no |
| 46 |
- **Computed value:** as specified |
| 47 |
|
| 48 |
Let’s go through each of these: |
| 49 |
|
| 50 |
**Name:** We have the name of the property (``mask-type``). Properties |
| 51 |
are case-insensitive, and librsvg already has machinery to handle that. |
| 52 |
|
| 53 |
**Value:** The possible values for the property can be ``luminance`` or |
| 54 |
``alpha``. In the spec’s web page, even the little ``|`` between those |
| 55 |
two values is a hyperlink; clicking it will take you to the |
| 56 |
specification for CSS Values and Units, where it describes the grammar |
| 57 |
that the CSS specs use to describe their values. Here you just need to |
| 58 |
know that ``|`` means that exactly one of the two alternatives must |
| 59 |
occur. |
| 60 |
|
| 61 |
As you may imagine, librsvg already parses a lot of similar properties |
| 62 |
that are just symbolic values. For example, the ``stroke-linecap`` |
| 63 |
property can have values ``butt | round | square``. We’ll see how to |
| 64 |
write a parser for this kind of property with a minimal amount of code. |
| 65 |
|
| 66 |
**Initial:** Then there is the initial or default value, which is |
| 67 |
``luminance``. This means that if the ``mask-type`` property is not |
| 68 |
specified on an element, it takes ``luminance`` as its default. This is |
| 69 |
a sensible choice, since an SVG1.1 file that is processed by SVG2 |
| 70 |
software should retain the same semantics. It also means that if there |
| 71 |
is a parse error, for example if you typed ``ahlpha``, the property will |
| 72 |
silently revert back to the default ``luminance`` value. |
| 73 |
|
| 74 |
**Applies to:** Librsvg doesn’t pay much attention to “applies to” — it |
| 75 |
just carries property values for all elements, and the elements that |
| 76 |
don’t handle a property just ignore it. |
| 77 |
|
| 78 |
**Inherited:** This property is not inherited, which means that by |
| 79 |
default, its value does not cascade. So if you have this: |
| 80 |
|
| 81 |
.. code:: xml |
| 82 |
|
| 83 |
<mask style="mask-type: alpha;"> |
| 84 |
<other> |
| 85 |
<elements> |
| 86 |
<here/> |
| 87 |
</elements> |
| 88 |
</other> |
| 89 |
</mask> |
| 90 |
|
| 91 |
Then the ``other``, ``elements``, ``here`` will not inherit the |
| 92 |
``mask-type`` value from their ancestor. |
| 93 |
|
| 94 |
**Computed value:** Finally, the computed value is “as specified”, which |
| 95 |
means that librsvg does not need to modify it in any way when resolving |
| 96 |
the CSS cascade. Other properties, like ``width: 1em;`` may need to be |
| 97 |
resolved against the ``font-size`` to obtain the computed value. |
| 98 |
|
| 99 |
The W3C specifications can get pretty verbose and it takes some practice |
| 100 |
to read them, but fortunately this property is short and sweet. |
| 101 |
|
| 102 |
Let’s go on. |
| 103 |
|
| 104 |
How librsvg represents properties |
| 105 |
--------------------------------- |
| 106 |
|
| 107 |
Each property has a Rust type that can hold its values. Remember the |
| 108 |
part of the masking spec from above, that says the ``mask-type`` |
| 109 |
property can have values ``luminance`` or ``alpha``, and the |
| 110 |
initial/default is ``luminance``? This translates easily to Rust types: |
| 111 |
|
| 112 |
.. code:: rust |
| 113 |
|
| 114 |
#[derive(Debug, Copy, Clone, PartialEq)] |
| 115 |
pub enum MaskType { |
| 116 |
Luminance, |
| 117 |
Alpha, |
| 118 |
} |
| 119 |
|
| 120 |
impl Default for MaskType { |
| 121 |
fn default() -> MaskType { |
| 122 |
MaskType::Luminance |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
Additionally, we need to be able to say that the property does not |
| 127 |
inherit by default, and that its computed value is the same as the |
| 128 |
specified value (e.g. we can just copy the original value without |
| 129 |
changing it). Librsvg defines a ``Property`` trait for those actions: |
| 130 |
|
| 131 |
.. code:: rust |
| 132 |
|
| 133 |
pub trait Property { |
| 134 |
fn inherits_automatically() -> bool; |
| 135 |
|
| 136 |
fn compute(&self, _: &ComputedValues) -> Self; |
| 137 |
} |
| 138 |
|
| 139 |
For the ``mask-type`` property, we want ``inherits_automatically`` to |
| 140 |
return ``false``, and ``compute`` to return the value unchanged. So, |
| 141 |
like this: |
| 142 |
|
| 143 |
.. code:: rust |
| 144 |
|
| 145 |
impl Property for MaskType { |
| 146 |
fn inherits_automatically() -> bool { |
| 147 |
false |
| 148 |
} |
| 149 |
|
| 150 |
fn compute(&self, _: &ComputedValues) -> Self { |
| 151 |
self.clone() |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Ignore the ``ComputedValues`` argument for now — it is how librsvg |
| 156 |
represents an element’s complete set of property values. |
| 157 |
|
| 158 |
As you can imagine, there are a lot of properties like ``mask-type``, |
| 159 |
whose values are just symbolic names that map well to a data-less enum. |
| 160 |
For all of them, it would be a lot of repetitive code to define their |
| 161 |
default value, return whether they inherit or not, and clone them for |
| 162 |
the computed value. Additionally, we have not even written the parser |
| 163 |
for this property’s values yet. |
| 164 |
|
| 165 |
Fortunately, librsvg has a ``make_property!`` macro that lets you do |
| 166 |
this instead: |
| 167 |
|
| 168 |
.. code:: rust |
| 169 |
|
| 170 |
make_property!( |
| 171 |
/// `mask-type` property. // (1) |
| 172 |
/// |
| 173 |
/// https://www.w3.org/TR/css-masking-1/#the-mask-type |
| 174 |
MaskType, // (2) |
| 175 |
default: Luminance, // (3) |
| 176 |
inherits_automatically: false, // (4) |
| 177 |
|
| 178 |
identifiers: // (5) |
| 179 |
"luminance" => Luminance, |
| 180 |
"alpha" => Alpha, |
| 181 |
); |
| 182 |
|
| 183 |
- |
| 184 |
|
| 185 |
(1) is a documentation comment for the ``MaskType`` enum being |
| 186 |
defined. |
| 187 |
|
| 188 |
- |
| 189 |
|
| 190 |
(2) is ``MaskType``, the name we will use for the ``mask-type`` |
| 191 |
property. |
| 192 |
|
| 193 |
- |
| 194 |
|
| 195 |
(3) indicates the “initial value”, or default, for the property. |
| 196 |
|
| 197 |
- |
| 198 |
|
| 199 |
(4) … whether the spec says the property should inherit or not. |
| 200 |
|
| 201 |
- |
| 202 |
|
| 203 |
(5) Finally, ``identifiers:`` is what makes the ``make_property!`` |
| 204 |
macro know that it should generate a parser for the symbolic |
| 205 |
names ``luminance`` and ``alpha``, and that they should |
| 206 |
correspond to the values ``MaskType::Luminance`` and |
| 207 |
``MaskType::Alpha``, respectively. |
| 208 |
|
| 209 |
This saves a lot of typing! Also, it makes it easier to gradually change |
| 210 |
the way properties are represented, as librsvg evolves. |
| 211 |
|
| 212 |
Properties that use the same data type |
| 213 |
-------------------------------------- |
| 214 |
|
| 215 |
Consider the ``stroke`` and ``fill`` properties; both store a |<paint>|_ |
| 216 |
value, which librsvg represents with a type called ``PaintServer``. The |
| 217 |
``make_property!`` macro has a case for properties like that, so in the |
| 218 |
librsvg source code you will find both of thsese: |
| 219 |
|
| 220 |
.. code:: rust |
| 221 |
|
| 222 |
make_property!( |
| 223 |
/// `fill` property. |
| 224 |
/// |
| 225 |
/// https://www.w3.org/TR/SVG/painting.html#FillProperty |
| 226 |
/// |
| 227 |
/// https://www.w3.org/TR/SVG2/painting.html#FillProperty |
| 228 |
Fill, |
| 229 |
default: PaintServer::parse_str("#000").unwrap(), |
| 230 |
inherits_automatically: true, |
| 231 |
newtype_parse: PaintServer, |
| 232 |
); |
| 233 |
|
| 234 |
make_property!( |
| 235 |
/// `stroke` property. |
| 236 |
/// |
| 237 |
/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingStrokePaint |
| 238 |
Stroke, |
| 239 |
default: PaintServer::None, |
| 240 |
inherits_automatically: true, |
| 241 |
newtype_parse: PaintServer, |
| 242 |
); |
| 243 |
|
| 244 |
The ``newtype_parse:`` is what tells the macro that it should generate a |
| 245 |
newtype like ``struct Stroke(PaintServer)``, and that it should just use |
| 246 |
the parser that ``PaintServer`` already has. |
| 247 |
|
| 248 |
Which parser is that? Read on. |
| 249 |
|
| 250 |
Custom parsers |
| 251 |
-------------- |
| 252 |
|
| 253 |
Librsvg has a ``Parse`` trait for property values which looks rather |
| 254 |
scary: |
| 255 |
|
| 256 |
.. code:: rust |
| 257 |
|
| 258 |
pub trait Parse: Sized { |
| 259 |
fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>>; |
| 260 |
} |
| 261 |
|
| 262 |
Don’t let the lifetimes scare you. They are required because of |
| 263 |
``cssparser::Parser``, from the ``cssparser`` crate, tries really hard |
| 264 |
to let you implement zero-copy parsers, which give you string tokens as |
| 265 |
slices from the original string being parsed, instead of allocating lots |
| 266 |
of little ``String`` values. What this ``Parse`` trait means is, you get |
| 267 |
tokens out of the ``Parser``, and return what is basically a |
| 268 |
``Result<Self, Error>``. |
| 269 |
|
| 270 |
In this tutorial we will just show you the parser for simple numeric |
| 271 |
types, for example, for properties that can just be represented with an |
| 272 |
``f64``. There is the ``stroke-miterlimit`` property defined like this: |
| 273 |
|
| 274 |
.. code:: rust |
| 275 |
|
| 276 |
make_property!( |
| 277 |
/// `stroke-miterlimit` property. |
| 278 |
/// |
| 279 |
/// https://www.w3.org/TR/SVG2/painting.html#StrokeMiterlimitProperty |
| 280 |
StrokeMiterlimit, |
| 281 |
default: 4f64, |
| 282 |
inherits_automatically: true, |
| 283 |
newtype_parse: f64, |
| 284 |
); |
| 285 |
|
| 286 |
And the ``impl Parse for f64`` looks like this: |
| 287 |
|
| 288 |
.. code:: rust |
| 289 |
|
| 290 |
impl Parse for f64 { |
| 291 |
fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> { |
| 292 |
let loc = parser.current_source_location(); // (1) |
| 293 |
let n = parser.expect_number()?; // (2) |
| 294 |
if n.is_finite() { // (3) |
| 295 |
Ok(f64::from(n)) // (4) |
| 296 |
} else { |
| 297 |
Err(loc.new_custom_error(ValueErrorKind::value_error("expected finite number"))) // (5) |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
- |
| 303 |
|
| 304 |
(1) Store the current location in the parser. |
| 305 |
|
| 306 |
- |
| 307 |
|
| 308 |
(2) Ask the parser for a number. If a non-numeric token comes out |
| 309 |
(e.g. if the user put ``stroke-miterlimit: foo`` instead of |
| 310 |
``stroke-miterlimit: 5``), ``expect_number`` will return an |
| 311 |
``Err``, which we propagate upwards with the ``?``. |
| 312 |
|
| 313 |
- |
| 314 |
|
| 315 |
(3) Check the number for being non-infinite or NaN…. |
| 316 |
|
| 317 |
- |
| 318 |
|
| 319 |
(4) … and return the number converted to f64 (``cssparser`` returns |
| 320 |
f32, but we promote them so that subsequent calculations can use |
| 321 |
the extra precision)… |
| 322 |
|
| 323 |
- |
| 324 |
|
| 325 |
(5) … or return an error based on the location from (1). |
| 326 |
|
| 327 |
My advice: implement new parsers by doing cut&paste from existing ones, |
| 328 |
and you’ll be okay. |
| 329 |
|
| 330 |
Registering the property |
| 331 |
------------------------ |
| 332 |
|
| 333 |
Okay! We defined ``MaskType`` and its symbolic identifiers with the |
| 334 |
``make_property!`` macro, and the macro took care of writing a parser |
| 335 |
for it and implementing the traits that the property needs. |
| 336 |
|
| 337 |
Now we need to modify the code in a few places to process the property. |
| 338 |
|
| 339 |
Register the property |
| 340 |
--------------------- |
| 341 |
|
| 342 |
- First, look for ``longhands:`` in ``properties.rs``. You will find |
| 343 |
that it is part of a long macro invocation: |
| 344 |
|
| 345 |
.. code:: rust |
| 346 |
|
| 347 |
make_properties! { |
| 348 |
// ... stuff omitted here |
| 349 |
|
| 350 |
longhands: { |
| 351 |
// ... stuff omitted here |
| 352 |
|
| 353 |
"marker-end" => (PresentationAttr::Yes, marker_end : MarkerEnd), |
| 354 |
"marker-mid" => (PresentationAttr::Yes, marker_mid : MarkerMid), |
| 355 |
"marker-start" => (PresentationAttr::Yes, marker_start : MarkerStart), |
| 356 |
"mask" => (PresentationAttr::Yes, mask : Mask), |
| 357 |
// "mask-type" => (PresentationAttr::Yes, unimplemented), |
| 358 |
"opacity" => (PresentationAttr::Yes, opacity : Opacity), |
| 359 |
"overflow" => (PresentationAttr::Yes, overflow : Overflow), |
| 360 |
|
| 361 |
// ... stuff omitted here |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
In there, there is an entry for ``mask-type`` commented out. Let’s |
| 366 |
uncomment it and turn it into this: |
| 367 |
|
| 368 |
.. code:: rust |
| 369 |
|
| 370 |
"mask-type" => (PresentationAttr::Yes, mask_type : MaskType), |
| 371 |
|
| 372 |
``PresentationAttr::Yes`` indicates whether the property has a |
| 373 |
corresponding presentation attribute. This means that you can do |
| 374 |
``<mask style="mask-type: alpha;">`` which is property, as well as |
| 375 |
``<mask mask-type="alpha">``, which is a presentation attribute. |
| 376 |
|
| 377 |
How did we find out that ``mask-type`` also exists as a presentation |
| 378 |
attribute? Well, `the spec |
| 379 |
<https://www.w3.org/TR/css-masking-1/#the-mask-type>`__ says: |
| 380 |
|
| 381 |
The mask-type property is a presentation attribute for SVG elements. |
| 382 |
|
| 383 |
But wait! If we compile, we get this: |
| 384 |
|
| 385 |
:: |
| 386 |
|
| 387 |
error: no rules expected the token `"mask-type"` |
| 388 |
--> src/properties.rs:450:9 |
| 389 |
| |
| 390 |
450 | "mask-type" => (PresentationAttr::Yes, mask_type : MaskType), |
| 391 |
| ^^^^^^^^^^^ no rules expected this token in macro call |
| 392 |
|
| 393 |
When you see that error in exactly that macro invocation, it means this: |
| 394 |
librsvg uses a crate called ``markup5ever`` to have a compact |
| 395 |
representation of the names of properties/attributes/elements. It uses |
| 396 |
string interning so that, for example, there is a single definition of |
| 397 |
``rect`` in the program’s heap instead of there being a thousands of |
| 398 |
duplicated ``rect`` strings when you load a big document. The thing is, |
| 399 |
``markup5ever`` only has ready-made definitions of the most common |
| 400 |
HTML/SVG/CSS names, but unfortunately ``mask-type`` is not one of them. |
| 401 |
|
| 402 |
So, we scroll down in ``properties.rs`` and move the ``mask-type`` |
| 403 |
registration there: |
| 404 |
|
| 405 |
.. code:: rust |
| 406 |
|
| 407 |
longhands_not_supported_by_markup5ever: { |
| 408 |
"line-height" => (PresentationAttr::No, line_height : LineHeight), |
| 409 |
"mask-type" => (PresentationAttr::Yes, mask_type : MaskType), // <- right here |
| 410 |
"mix-blend-mode" => (PresentationAttr::No, mix_blend_mode : MixBlendMode), |
| 411 |
"paint-order" => (PresentationAttr::Yes, paint_order : PaintOrder), |
| 412 |
} |
| 413 |
|
| 414 |
That block named ``longhands_not_supported_by_markup5ever`` is, well, |
| 415 |
exactly what it says — a separate section with property names that are |
| 416 |
not built into ``markup5ever``, so they must be dealt with specially. |
| 417 |
Just put the property there and that’s it. |
| 418 |
|
| 419 |
Next, we have to calculate the computed value for the property. |
| 420 |
|
| 421 |
Calculate the computed value |
| 422 |
---------------------------- |
| 423 |
|
| 424 |
In ``properties.rs``, look for ``compute!``. You will find many |
| 425 |
invocations of this macro: |
| 426 |
|
| 427 |
.. code:: rust |
| 428 |
|
| 429 |
compute!(MarkerEnd, marker_end); |
| 430 |
compute!(MarkerMid, marker_mid); |
| 431 |
compute!(MarkerStart, marker_start); |
| 432 |
compute!(Mask, mask); |
| 433 |
compute!(MixBlendMode, mix_blend_mode); |
| 434 |
compute!(Opacity, opacity); |
| 435 |
compute!(Overflow, overflow); |
| 436 |
|
| 437 |
Add a call for ``MaskType``: |
| 438 |
|
| 439 |
.. code:: rust |
| 440 |
|
| 441 |
compute!(MarkerEnd, marker_end); |
| 442 |
compute!(MarkerMid, marker_mid); |
| 443 |
compute!(MarkerStart, marker_start); |
| 444 |
compute!(Mask, mask); |
| 445 |
compute!(MaskType, mask_type); // this is new |
| 446 |
compute!(MixBlendMode, mix_blend_mode); |
| 447 |
compute!(Opacity, opacity); |
| 448 |
compute!(Overflow, overflow); |
| 449 |
|
| 450 |
You will see that all those calls to ``compute!`` are inside a method |
| 451 |
called ``SpecifiedValues::to_computed_values()``. This method is run as |
| 452 |
part of the CSS cascade: it takes the ``SpecifiedValues`` from an |
| 453 |
element and composes them onto the ``ComputedValues`` from its parent |
| 454 |
element. For example, if you have a document with this bit: |
| 455 |
|
| 456 |
.. code:: xml |
| 457 |
|
| 458 |
<g stroke="red" fill="blue"> // ComputedValues with stroke:red, fill:blue |
| 459 |
<rect fill="green"/> // SpecifiedValues with fill:green |
| 460 |
</g> |
| 461 |
|
| 462 |
The ``ComputedValues`` that results from the ``<g>`` will have |
| 463 |
properties ``stroke:red`` and ``fill:blue`` in it. The |
| 464 |
``SpecifiedValues`` from the ``<rect>`` just has ``fill:green``. |
| 465 |
Composing them together for the ``<rect>`` gives us ``ComputedValues`` |
| 466 |
with ``stroke:red`` and ``fill:green``. |
| 467 |
|
| 468 |
Now that the property is registered, we can actually handle it in the |
| 469 |
drawing code! |
| 470 |
|
| 471 |
Handling the property |
| 472 |
--------------------- |
| 473 |
|
| 474 |
First, a digression: let’s change the name of a few methods to better |
| 475 |
reflect what the new structure of the code will be like. |
| 476 |
|
| 477 |
There are a few methods called ``to_mask`` in the code, that take an |
| 478 |
RGBA surface and turn it into an Alpha-only surface with the luminance |
| 479 |
of the original surface; and also the corresponding method to do this |
| 480 |
for a single pixel. Let’s do this kind of renaming: |
| 481 |
|
| 482 |
:: |
| 483 |
|
| 484 |
- pub fn to_mask(&self, opacity: UnitInterval) -> Result<SharedImageSurface, cairo::Error> { |
| 485 |
+ pub fn to_luminance_mask(&self, opacity: UnitInterval) -> Result<SharedImageSurface, cairo::Error> { |
| 486 |
|
| 487 |
Librsvg only effectively supported ``mask-type: luminance`` since that |
| 488 |
is what was in SVG1.1, but now for SVG2 we want to add behavior for |
| 489 |
``mask-type: alpha`` as well. So, it makes sense to rename ``to_mask`` |
| 490 |
as ``to_luminance_mask``. |
| 491 |
|
| 492 |
``SharedImageSurface`` is the type that librsvg uses to represent images |
| 493 |
in memory. They can be RGBA or Alpha-only. There is already a method |
| 494 |
called ``extract_alpha`` that we can use to create an Alpha-only mask: |
| 495 |
|
| 496 |
.. code:: rust |
| 497 |
|
| 498 |
// there's a type alias SharedImageSurface for this |
| 499 |
impl ImageSurface<Shared> { |
| 500 |
pub fn extract_alpha(&self, bounds: IRect) -> Result<SharedImageSurface, cairo::Error> { ... } |
| 501 |
} |
| 502 |
|
| 503 |
Now let’s look at where ``drawing_ctx.rs`` has this: |
| 504 |
|
| 505 |
.. code:: rust |
| 506 |
|
| 507 |
let mask = SharedImageSurface::wrap(mask_content_surface, SurfaceType::SRgb)? // (1) |
| 508 |
.to_luminance_mask()? // (2) |
| 509 |
.into_image_surface()?; // (3) |
| 510 |
|
| 511 |
- |
| 512 |
|
| 513 |
(1) Wraps a ``SharedImageSurface`` around the Cairo surface that was |
| 514 |
just rendered with the mask contents. |
| 515 |
|
| 516 |
- |
| 517 |
|
| 518 |
(2) Converts it to a luminance mask. We will need to change this! |
| 519 |
|
| 520 |
- |
| 521 |
|
| 522 |
(3) Extracts the Cairo image surface from the ``SharedImageSurface``, |
| 523 |
for further processing. |
| 524 |
|
| 525 |
Remember the ``ComputedValues`` where we had the ``mask_type``? We can |
| 526 |
extract it with ``values.mask_type()``. Now let’s change the lines above |
| 527 |
to this: |
| 528 |
|
| 529 |
.. code:: rust |
| 530 |
|
| 531 |
let tmp = SharedImageSurface::wrap(mask_content_surface, SurfaceType::SRgb)?; |
| 532 |
|
| 533 |
let mask_result = match values.mask_type() { |
| 534 |
MaskType::Luminance => tmp.to_luminance_mask()?, |
| 535 |
MaskType::Alpha => tmp.extract_alpha(IRect::from_size(tmp.width(), tmp.height()))?, |
| 536 |
}; |
| 537 |
|
| 538 |
let mask = mask_result.into_image_surface()?; |
| 539 |
|
| 540 |
But wait! We don’t have a test for this yet! Aaaaaargh, we are doing |
| 541 |
test-driven development backwards! |
| 542 |
|
| 543 |
No biggie. Let’s write the tests. |
| 544 |
|
| 545 |
Adding tests |
| 546 |
------------ |
| 547 |
|
| 548 |
Testing graphical output is really annoying if you compare PNG files, |
| 549 |
because any time Cairo changes something and antialiasing changes |
| 550 |
juuuuuust a bit, the tests break. So, librsvg tries to do “reftests”, or |
| 551 |
reference tests, by comparing the rendered results of two things: |
| 552 |
|
| 553 |
- The SVG you actually want to test. |
| 554 |
- An equivalent SVG that works only with known-good features. |
| 555 |
|
| 556 |
For ``mask-type``, we need an SVG document that actually uses that |
| 557 |
property with both of its values, and another document that produces the |
| 558 |
same results but with simpler primitives. |
| 559 |
|
| 560 |
Librsvg already has tests for luminance masks, as they were the only |
| 561 |
available kind in SVG1.1. So we can be confident that they already work |
| 562 |
- we just need to test that the presence of ``mask-type="luminance"`` |
| 563 |
actually does the same thing. |
| 564 |
|
| 565 |
First, let’s dissect the SVG that we want to test: |
| 566 |
|
| 567 |
.. code:: xml |
| 568 |
|
| 569 |
<?xml version="1.0" encoding="UTF-8"?> |
| 570 |
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100"> |
| 571 |
<mask id="luminance" mask-type="luminance" maskContentUnits="objectBoundingBox"> |
| 572 |
<rect x="0.1" y="0.1" width="0.8" height="0.8" fill="white"/> |
| 573 |
</mask> |
| 574 |
<mask id="alpha" mask-type="alpha" maskContentUnits="objectBoundingBox"> |
| 575 |
<rect x="0.1" y="0.1" width="0.8" height="0.8" fill="black"/> |
| 576 |
</mask> |
| 577 |
|
| 578 |
<rect x="0" y="0" width="100" height="100" fill="green" mask="url(#luminance)"/> |
| 579 |
|
| 580 |
<rect x="100" y="0" width="100" height="100" fill="green" mask="url(#alpha)"/> |
| 581 |
</svg> |
| 582 |
|
| 583 |
The image has two 100x100 ``green`` squares side by side. The one on the |
| 584 |
left gets masked with the ``luminance`` mask, which reduces it to an |
| 585 |
80x80 rectangle. That mask is a **white** square, so its has full |
| 586 |
luminance at every pixel. |
| 587 |
|
| 588 |
The square on the right gets masked with the ``alpha`` mask. That mask |
| 589 |
is a **black** square, but with alpha=1.0, so it should produce the same |
| 590 |
result as the first one. |
| 591 |
|
| 592 |
Note that to make things easy, we use **white** for the luminance mask. |
| 593 |
White pixels have full luminance (1.0), which gets used as the mask. |
| 594 |
Conversely, we use **black** for the alpha mask. Those black pixels are |
| 595 |
fully opaque, and since ``mask-type="alpha"`` only considers the alpha |
| 596 |
channel, it will be using the full opacity of each pixel (1.0), which |
| 597 |
also gets used as the mask. So, the masks should be equivalent. |
| 598 |
|
| 599 |
Okay! Now let’s write the reference SVG, the one built out of simpler |
| 600 |
elements but that should produce the same rendering: |
| 601 |
|
| 602 |
.. code:: xml |
| 603 |
|
| 604 |
<?xml version="1.0" encoding="UTF-8"?> |
| 605 |
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100"> |
| 606 |
<rect x="10" y="10" width="80" height="80" fill="green"/> |
| 607 |
|
| 608 |
<rect x="110" y="10" width="80" height="80" fill="green"/> |
| 609 |
</svg> |
| 610 |
|
| 611 |
This is just the two original squares, but already clipped or masked to |
| 612 |
the final result. |
| 613 |
|
| 614 |
Now, where do we put those SVG documents for the tests? |
| 615 |
|
| 616 |
Near the end of ``tests/src/filters.rs`` we can include this: |
| 617 |
|
| 618 |
.. code:: rust |
| 619 |
|
| 620 |
test_compare_render_output!( |
| 621 |
mask_type, |
| 622 |
200, |
| 623 |
100, |
| 624 |
br##"<?xml version="1.0" encoding="UTF-8"?> |
| 625 |
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100"> |
| 626 |
<mask id="luminance" mask-type="luminance" maskContentUnits="objectBoundingBox"> |
| 627 |
<rect x="0.1" y="0.1" width="0.8" height="0.8" fill="white"/> |
| 628 |
</mask> |
| 629 |
<mask id="alpha" mask-type="alpha" maskContentUnits="objectBoundingBox"> |
| 630 |
<rect x="0.1" y="0.1" width="0.8" height="0.8" fill="black"/> |
| 631 |
</mask> |
| 632 |
|
| 633 |
<rect x="0" y="0" width="100" height="100" fill="green" mask="url(#luminance)"/> |
| 634 |
|
| 635 |
<rect x="100" y="0" width="100" height="100" fill="green" mask="url(#alpha)"/> |
| 636 |
</svg> |
| 637 |
"##, |
| 638 |
br##"<?xml version="1.0" encoding="UTF-8"?> |
| 639 |
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100"> |
| 640 |
<rect x="10" y="10" width="80" height="80" fill="green"/> |
| 641 |
|
| 642 |
<rect x="110" y="10" width="80" height="80" fill="green"/> |
| 643 |
</svg> |
| 644 |
"##, |
| 645 |
); |
| 646 |
|
| 647 |
Here, ``test_compare_render_output!`` is a macro that takes two SVG |
| 648 |
documents, the test and the reference, and compares their rendered |
| 649 |
results. It also takes a test name (``mask_type`` in this case), and the |
| 650 |
pixel size of the image to generate for testing (200x100). |
| 651 |
|
| 652 |
Final steps: documentation |
| 653 |
-------------------------- |
| 654 |
|
| 655 |
To help people who are wondering what SVG features are supported in |
| 656 |
librsvg, there is a ``FEATURES.md`` file. It has a section called “CSS |
| 657 |
properties” with a big list of property names and notes about them. |
| 658 |
|
| 659 |
We’ll patch it like this: |
| 660 |
|
| 661 |
:: |
| 662 |
|
| 663 |
| marker-mid | | |
| 664 |
| marker-start | | |
| 665 |
| mask | | |
| 666 |
+| mask-type | | |
| 667 |
| mix-blend-mode | Not available as a presentation attribute. | |
| 668 |
| opacity | | |
| 669 |
| overflow | | |
| 670 |
|
| 671 |
There is nothing remarkable about ``mask-type``, it is a plain old |
| 672 |
property that also has a presentation attribute (remember the |
| 673 |
``PresentationAttr::Yes`` from above?), so we don’t need to list any |
| 674 |
extra information. |
| 675 |
|
| 676 |
And with that, we are done implementing ``mask-type``. Have fun! |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
.. See https://docutils.sourceforge.net/FAQ.html#is-nested-inline-markup-possible |
| 681 |
|
| 682 |
.. |mask-type| replace:: ``mask-type`` |
| 683 |
.. _mask-type: https://www.w3.org/TR/css-masking-1/#the-mask-type |
| 684 |
|
| 685 |
.. |<paint>| replace:: ``<paint>`` |
| 686 |
.. _<paint>: https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint |