277 lines
8.6 KiB
Plaintext
277 lines
8.6 KiB
Plaintext
#! Contains SVG's styling: the properties each element has, which its
|
|
#! presentation attributes, the rules of `<style>` sheets, and its `style`
|
|
#! attribute set, in that order, over what it inherits.
|
|
|
|
import std::math;
|
|
module image::svg;
|
|
|
|
|
|
enum line_cap: u8 {
|
|
butt,
|
|
round,
|
|
square,
|
|
}
|
|
|
|
enum line_join: u8 {
|
|
miter,
|
|
round,
|
|
bevel,
|
|
}
|
|
|
|
# The most dashes a stroke's pattern holds.
|
|
const most_dashes: u64 = 16;
|
|
|
|
# The properties of an element, after its own and what it inherits.
|
|
struct style {
|
|
# Those inherited.
|
|
var fill: paint = paint { kind = paint_kind::color };
|
|
var fill_opacity: f64 = 1;
|
|
var even_odd: bool;
|
|
var stroke: paint;
|
|
var stroke_opacity: f64 = 1;
|
|
var stroke_width: f64 = 1;
|
|
var cap: line_cap;
|
|
var join: line_join;
|
|
var miter_limit: f64 = 4;
|
|
var dashes: f64[16];
|
|
var dash_count: u64;
|
|
var dash_offset: f64;
|
|
var color: color;
|
|
var visible: bool = true;
|
|
var clip_even_odd: bool;
|
|
# Those not.
|
|
var opacity: f64 = 1;
|
|
var displayed: bool = true;
|
|
var clip_path: strview;
|
|
var mask: strview;
|
|
var stop_color: color;
|
|
var stop_opacity: f64 = 1;
|
|
}
|
|
|
|
# The properties an element's presentation attributes may set.
|
|
const presentation: strview[20] = [
|
|
"fill", "fill-opacity", "fill-rule", "stroke", "stroke-width", "stroke-opacity", "stroke-linecap",
|
|
"stroke-linejoin", "stroke-miterlimit", "stroke-dasharray", "stroke-dashoffset", "opacity", "color",
|
|
"display", "visibility", "clip-path", "clip-rule", "mask", "stop-color", "stop-opacity",
|
|
];
|
|
|
|
# The style a child begins with: what its parent's is that's inherited, and the
|
|
# rest as it begins.
|
|
def style::inherited(&this): style {
|
|
var made := copy(this);
|
|
made.opacity = 1;
|
|
made.displayed = true;
|
|
made.clip_path = "";
|
|
made.mask = "";
|
|
made.stop_color = color {};
|
|
made.stop_opacity = 1;
|
|
return made;
|
|
}
|
|
|
|
# Sets a property of a style by its name and its value as written, leaving it as
|
|
# it was where either isn't one known, or the value is wrong, as CSS does.
|
|
def style::set(&mut this, name: strview, given: strview, parent: &style, view: &viewport): void {
|
|
val value := given.trim();
|
|
if value == "inherit" {
|
|
this.inherit(name, parent);
|
|
return;
|
|
}
|
|
|
|
if name == "fill" {
|
|
this.fill = parse_paint(value) else { return; };
|
|
} else if name == "stroke" {
|
|
this.stroke = parse_paint(value) else { return; };
|
|
} else if name == "fill-opacity" || name == "stroke-opacity" || name == "opacity" || name == "stop-opacity" {
|
|
val read := parse_fraction(value) else { return; };
|
|
val clamped := math::clamp(read, 0.0, 1.0);
|
|
if name == "fill-opacity" {
|
|
this.fill_opacity = clamped;
|
|
} else if name == "stroke-opacity" {
|
|
this.stroke_opacity = clamped;
|
|
} else if name == "opacity" {
|
|
this.opacity = clamped;
|
|
} else {
|
|
this.stop_opacity = clamped;
|
|
}
|
|
} else if name == "fill-rule" {
|
|
if value == "evenodd" || value == "nonzero" {
|
|
this.even_odd = value == "evenodd";
|
|
}
|
|
} else if name == "clip-rule" {
|
|
if value == "evenodd" || value == "nonzero" {
|
|
this.clip_even_odd = value == "evenodd";
|
|
}
|
|
} else if name == "stroke-width" {
|
|
val width := parse_length(value, view, axis::diagonal) else { return; };
|
|
if width >= 0 {
|
|
this.stroke_width = width;
|
|
}
|
|
} else if name == "stroke-linecap" {
|
|
if value == "butt" {
|
|
this.cap = line_cap::butt;
|
|
} else if value == "round" {
|
|
this.cap = line_cap::round;
|
|
} else if value == "square" {
|
|
this.cap = line_cap::square;
|
|
}
|
|
} else if name == "stroke-linejoin" {
|
|
if value == "miter" || value == "miter-clip" || value == "arcs" {
|
|
this.join = line_join::miter;
|
|
} else if value == "round" {
|
|
this.join = line_join::round;
|
|
} else if value == "bevel" {
|
|
this.join = line_join::bevel;
|
|
}
|
|
} else if name == "stroke-miterlimit" {
|
|
val limit := parse_fraction(value) else { return; };
|
|
if limit >= 1 {
|
|
this.miter_limit = limit;
|
|
}
|
|
} else if name == "stroke-dasharray" {
|
|
this.set_dashes(value, view);
|
|
} else if name == "stroke-dashoffset" {
|
|
this.dash_offset = parse_length(value, view, axis::diagonal) else { return; };
|
|
} else if name == "color" {
|
|
this.color = parse_color(value) else { return; };
|
|
} else if name == "stop-color" {
|
|
if same_letters(value, "currentColor") {
|
|
this.stop_color = this.color;
|
|
return;
|
|
}
|
|
this.stop_color = parse_color(value) else { return; };
|
|
} else if name == "display" {
|
|
this.displayed = value != "none";
|
|
} else if name == "visibility" {
|
|
this.visible = value == "visible";
|
|
} else if name == "clip-path" {
|
|
this.clip_path = url_of(value);
|
|
} else if name == "mask" {
|
|
this.mask = url_of(value);
|
|
}
|
|
}
|
|
|
|
# Reads a dash pattern, a list repeated where it's odd, which with any length
|
|
# below zero, or none above it, dashes nothing.
|
|
def style::set_dashes(&mut this, value: strview, view: &viewport): void {
|
|
this.dash_count = 0;
|
|
if value == "none" {
|
|
return;
|
|
}
|
|
var read: f64[16];
|
|
var count: u64 = 0;
|
|
var total: f64 = 0;
|
|
var at: u64 = 0;
|
|
while at < value.length {
|
|
while at < value.length && (value[at].is_space || value[at] == ',') {
|
|
at++;
|
|
}
|
|
if at >= value.length {
|
|
break;
|
|
}
|
|
var end := at;
|
|
while end < value.length && !value[end].is_space && value[end] != ',' {
|
|
end++;
|
|
}
|
|
val length := parse_length(value.substr(at, end - at), view, axis::diagonal) else { return; };
|
|
if length < 0 || count >= most_dashes / 2 {
|
|
return;
|
|
}
|
|
read[count] = length;
|
|
total += length;
|
|
count++;
|
|
at = end;
|
|
}
|
|
if total <= 0 {
|
|
return;
|
|
}
|
|
for index in 0..count {
|
|
this.dashes[index] = read[index];
|
|
this.dashes[index + count] = read[index];
|
|
}
|
|
this.dash_count = count % 2 == 1 ? count * 2 : count;
|
|
}
|
|
|
|
# Takes a property from the parent's style, as `inherit` says.
|
|
def style::inherit(&mut this, name: strview, parent: &style): void {
|
|
if name == "fill" {
|
|
this.fill = parent.fill;
|
|
} else if name == "stroke" {
|
|
this.stroke = parent.stroke;
|
|
} else if name == "fill-opacity" {
|
|
this.fill_opacity = parent.fill_opacity;
|
|
} else if name == "stroke-opacity" {
|
|
this.stroke_opacity = parent.stroke_opacity;
|
|
} else if name == "opacity" {
|
|
this.opacity = parent.opacity;
|
|
} else if name == "fill-rule" {
|
|
this.even_odd = parent.even_odd;
|
|
} else if name == "clip-rule" {
|
|
this.clip_even_odd = parent.clip_even_odd;
|
|
} else if name == "stroke-width" {
|
|
this.stroke_width = parent.stroke_width;
|
|
} else if name == "stroke-linecap" {
|
|
this.cap = parent.cap;
|
|
} else if name == "stroke-linejoin" {
|
|
this.join = parent.join;
|
|
} else if name == "stroke-miterlimit" {
|
|
this.miter_limit = parent.miter_limit;
|
|
} else if name == "stroke-dasharray" {
|
|
this.dashes = parent.dashes;
|
|
this.dash_count = parent.dash_count;
|
|
} else if name == "stroke-dashoffset" {
|
|
this.dash_offset = parent.dash_offset;
|
|
} else if name == "color" {
|
|
this.color = parent.color;
|
|
} else if name == "stop-color" {
|
|
this.stop_color = parent.stop_color;
|
|
} else if name == "stop-opacity" {
|
|
this.stop_opacity = parent.stop_opacity;
|
|
} else if name == "display" {
|
|
this.displayed = parent.displayed;
|
|
} else if name == "visibility" {
|
|
this.visible = parent.visible;
|
|
} else if name == "clip-path" {
|
|
this.clip_path = parent.clip_path;
|
|
} else if name == "mask" {
|
|
this.mask = parent.mask;
|
|
}
|
|
}
|
|
|
|
# Sets each property a list of declarations sets, as `name: value;` writes each.
|
|
def style::declare(&mut this, declarations: strview, parent: &style, view: &viewport): void {
|
|
var at: u64 = 0;
|
|
while at < declarations.length {
|
|
# A declaration ends at a `;` not within parentheses or quotes.
|
|
var end := at;
|
|
var depth := 0;
|
|
var quote: u8 = 0;
|
|
while end < declarations.length {
|
|
val byte := declarations[end];
|
|
if quote != 0 {
|
|
if byte == quote {
|
|
quote = 0;
|
|
}
|
|
} else if byte == '"' || byte == '\'' {
|
|
quote = byte;
|
|
} else if byte == '(' {
|
|
depth++;
|
|
} else if byte == ')' {
|
|
depth--;
|
|
} else if byte == ';' && depth <= 0 {
|
|
break;
|
|
}
|
|
end++;
|
|
}
|
|
val declaration := declarations.substr(at, end - at);
|
|
at = end + 1;
|
|
val colon := declaration.find(":") else { continue; };
|
|
val name := declaration.substr(0, colon).trim();
|
|
var value := declaration.substr(colon + 1, declaration.length - colon - 1).trim();
|
|
if value.find("!important") is some(important) {
|
|
value = value.substr(0, important).trim();
|
|
}
|
|
val lowered := name.lower();
|
|
this.set(lowered.view(), value, parent, view);
|
|
}
|
|
}
|