57 lines
2.1 KiB
Plaintext
57 lines
2.1 KiB
Plaintext
#! Contains clips and masks: the coverage a `clipPath`'s shapes give, each solid,
|
|
#! and the luminance a `mask`'s content gives, each drawn as a layer of its own.
|
|
|
|
import std::xml;
|
|
module malunal::image::svg;
|
|
|
|
|
|
# A clip path's coverage, drawn as a layer of its shapes, each solid.
|
|
def renderer::clip_mask(&mut this, clip: &xml::element, placed: &matrix, bounds: maybe<point[2]>): vector<u8> {
|
|
var space := copy(placed);
|
|
if clip.attribute("clipPathUnits") is some(units) && units == "objectBoundingBox" {
|
|
val box := bounds else {
|
|
var empty := vector::<u8>::new();
|
|
empty.resize(this.canvas.width * this.canvas.height * 4);
|
|
return empty;
|
|
};
|
|
space = matrix { a = box[1].x - box[0].x, d = box[1].y - box[0].y, e = box[0].x, f = box[0].y }.then(placed);
|
|
}
|
|
if clip.attribute("transform") is some(written) {
|
|
if parse_transform(written) is some(own_transform) {
|
|
space = own_transform.then(&space);
|
|
}
|
|
}
|
|
val clip_style := this.style_of(clip, &style {});
|
|
this.canvas.push();
|
|
val saved := this.clipping;
|
|
this.clipping = true;
|
|
for child in clip.elements() {
|
|
if is_shape(child.local) || child.local == "use" {
|
|
this.draw(&child, &clip_style, &space);
|
|
}
|
|
}
|
|
this.clipping = saved;
|
|
return this.canvas.pop();
|
|
}
|
|
|
|
# A mask's coverage: its content drawn as a layer, its luminance taken as alpha.
|
|
def renderer::luminance_mask(&mut this, mask: &xml::element, placed: &matrix, bounds: maybe<point[2]>): vector<u8> {
|
|
var space := copy(placed);
|
|
if mask.attribute("maskContentUnits") is some(units) && units == "objectBoundingBox" {
|
|
if bounds is some(box) {
|
|
space = matrix { a = box[1].x - box[0].x, d = box[1].y - box[0].y, e = box[0].x, f = box[0].y }.then(placed);
|
|
}
|
|
}
|
|
val mask_style := this.style_of(mask, &style {});
|
|
this.canvas.push();
|
|
this.children(mask, &mask_style, &space);
|
|
var made := this.canvas.pop();
|
|
var at: u64 = 0;
|
|
while at < made.length {
|
|
val luminance := made[at] as f32 * 0.2125 + made[at + 1] as f32 * 0.7154 + made[at + 2] as f32 * 0.0721;
|
|
made[at + 3] = (luminance + 0.5) as u8;
|
|
at += 4;
|
|
}
|
|
return made;
|
|
}
|