Paint textcolor runs in named colours

This commit is contained in:
gloamxun committed 2026-10-26 01:41:00 +08:00
1 parent f98964f6b6
commit 9ad404acee
7 files changed
+264 -38

No files matched your search

+20 -1
View File
@@ -76,6 +76,8 @@ pub enum DisplayItem {
height: i64,
url: String,
},
/// Switch the fill colour for following text (RGB).
Fill([u8; 3]),
Special(String),
}
@@ -162,6 +164,7 @@ impl DisplayList {
*x += dx;
*y += dy;
}
DisplayItem::Fill(_) => {}
DisplayItem::Special(_) => {}
}
}
@@ -174,8 +177,10 @@ impl DisplayList {
let mut out = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width:.3}pt\" height=\"{height:.3}pt\" viewBox=\"0 0 {width:.3} {height:.3}\">\n"
);
let mut fill = [0u8, 0, 0];
for item in &self.items {
match item {
DisplayItem::Fill(color) => fill = *color,
DisplayItem::Text {
x,
y,
@@ -221,8 +226,13 @@ impl DisplayList {
} else {
""
};
let fill_attr = if fill == [0u8, 0, 0] {
String::new()
} else {
format!(" fill=\"#{:02x}{:02x}{:02x}\"", fill[0], fill[1], fill[2])
};
out.push_str(&format!(
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"{family}\"{weight}{style}>{}</text>\n",
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"{family}\"{weight}{style}{fill_attr}>{}</text>\n",
escape(text)
));
}
@@ -546,6 +556,7 @@ impl Page {
let lines = paragraph.lines.len().max(1) as i64;
let line_height = (paragraph.height / lines).max(1);
let size = paragraph.font_size.max(1);
let mut current_color: Option<[u8; 3]> = None;
for (line_index, line) in paragraph.lines.iter().enumerate() {
let indent = paragraph.line_indents.get(line_index).copied().unwrap_or(0);
let baseline = y + paragraph
@@ -595,6 +606,11 @@ impl Page {
} else {
flush_link(&mut link_run, &mut list.items);
}
if box_.color != current_color {
let rgb = box_.color.unwrap_or([0, 0, 0]);
list.items.push(DisplayItem::Fill(rgb));
current_color = box_.color;
}
let glyph_size = box_.size.unwrap_or(size);
let glyph_y = baseline - box_.raise;
if let Some(math) = &box_.math {
@@ -631,6 +647,9 @@ impl Page {
}
flush_link(&mut link_run, &mut list.items);
}
if current_color.is_some() {
list.items.push(DisplayItem::Fill([0, 0, 0]));
}
y += paragraph.height;
}
VerticalItem::Halign(halign) => {