Browse Source

Parametrize letter size in svg generation

master
Manos Pitsidianakis 2 years ago
parent
commit
7c839df7d0
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 48
      src/state.rs

48
src/state.rs

@ -753,6 +753,8 @@ impl State {
use crate::melib::text_processing::TextProcessing;
use std::collections::BTreeMap;
const LETTER_WIDTH: usize = 8;
const LETTER_HEIGHT: usize = 17;
use svg_crate::node::element::{Definitions, Group, Rectangle, Style, Text, Use};
use svg_crate::node::Text as TextNode;
use svg_crate::Document;
@ -801,11 +803,11 @@ impl State {
{
if cur_bg != Color::Default {
let mut rect = Rectangle::new()
.set("x", prev_x_bg * 8)
.set("y", 17 * row_idx)
.set("width", (x - prev_x_bg) * 8 + 1)
.set("x", prev_x_bg * LETTER_WIDTH)
.set("y", LETTER_HEIGHT * row_idx)
.set("width", (x - prev_x_bg) * LETTER_WIDTH + 1)
//.set("bgname", format!("{:?}", cur_bg))
.set("height", 18);
.set("height", LETTER_HEIGHT);
match cur_bg {
Color::Rgb(r, g, b) => {
let class = if classes.contains_key(&(r, g, b)) {
@ -854,8 +856,8 @@ impl State {
}
let mut text_el = Text::new()
.add(TextNode::new(&escaped_text))
.set("x", prev_x_fg * 8)
.set("textLength", text_length * 8);
.set("x", prev_x_fg * LETTER_WIDTH)
.set("textLength", text_length * LETTER_WIDTH);
/*.set("fgname", format!("{:?}", cur_fg));*/
if cur_attrs.intersects(Attr::BOLD) {
text_el = text_el.set("font-weight", "bold");
@ -927,11 +929,11 @@ impl State {
/* Append last elements of the row if any */
if cur_bg != Color::Default {
let mut rect = Rectangle::new()
.set("x", prev_x_bg * 8)
.set("y", 17 * row_idx)
.set("width", (width - prev_x_bg) * 8 + 1)
.set("x", prev_x_bg * LETTER_WIDTH)
.set("y", LETTER_HEIGHT * row_idx)
.set("width", (width - prev_x_bg) * LETTER_WIDTH + 1)
//.set("bgname", format!("{:?}", cur_bg))
.set("height", 18);
.set("height", LETTER_HEIGHT);
match cur_bg {
Color::Rgb(r, g, b) => {
let class = if classes.contains_key(&(r, g, b)) {
@ -978,8 +980,8 @@ impl State {
}
let mut text_el = Text::new()
.add(TextNode::new(&escaped_text))
.set("x", prev_x_fg * 8)
.set("textLength", text_length * 8);
.set("x", prev_x_fg * LETTER_WIDTH)
.set("textLength", text_length * LETTER_WIDTH);
/*.set("fgname", format!("{:?}", cur_fg));*/
if cur_attrs.intersects(Attr::BOLD) {
text_el = text_el.set("font-weight", "bold");
@ -1037,7 +1039,7 @@ impl State {
rows_group = rows_group.add(
Use::new()
.set("xlink:href", format!("#{:x}", row_idx + 1))
.set("y", 17 * row_idx),
.set("y", LETTER_HEIGHT * row_idx),
);
}
let mut style_string = CSS_STYLE.to_string();
@ -1046,17 +1048,23 @@ impl State {
.extend(format!(".f{:x}{{fill:#{:02x}{:02x}{:02x};}}", name, r, g, b).chars());
}
let document = Document::new()
.set("viewBox", (0, 0, width * 8, height * 17 + 2))
.set("width", width * 8)
.set("height", height * 17 + 2)
.set(
"viewBox",
(0, 0, width * LETTER_WIDTH, height * LETTER_HEIGHT + 1),
)
.set("width", width * LETTER_WIDTH)
.set("height", height * LETTER_HEIGHT + 1)
.add(Definitions::new().add(Style::new(&style_string).set("type", "text/css")))
.add(
Document::new()
.set("id", "t")
.set("preserveAspectRatio", "xMidYMin slice")
.set("viewBox", (0, 0, width * 8, height * 17))
.set("width", width * 8)
.set("height", height * 17)
.set(
"viewBox",
(0, 0, width * LETTER_WIDTH, height * LETTER_HEIGHT),
)
.set("width", width * LETTER_WIDTH)
.set("height", height * LETTER_HEIGHT)
.add(
Rectangle::new()
.set("class", "b")
@ -1525,7 +1533,7 @@ impl State {
//self.context.input_thread.check();
}
}
const CSS_STYLE: &'static str = r#"#t{font-family:var(--terminal-font);font-style:normal;font-size:14px;} text {dominant-baseline: text-before-edge; white-space: pre;} .f{fill:#e5e5e5;} .b{fill:#000;} .c0 {fill:#000;} .c1 {fill:#cd0000;} .c2 {fill:#00cd00;} .c3 {fill:#cdcd00;} .c4 {fill:#00e;} .c5 {fill:#cd00cd;} .c6 {fill:#00cdcd;} .c7 {fill:#e5e5e5;} .c8 {fill:#7f7f7f;} .c9 {fill:#f00;} .c10 {fill:#0f0;} .c11 {fill:#ff0;} .c12 {fill:#5c5cff;} .c13 {fill:#f0f;} .c14 {fill:#0ff;} .c15 {fill:#fff;}"#;
const CSS_STYLE: &'static str = r#"#t{font-family:var(--terminal-font);font-style:normal;font-size:14px;}text{dominant-baseline:text-before-edge;white-space:pre;transform:translateY(-2px);}.f{fill:#e5e5e5;}.b{fill:#000;}.c0{fill:#000;}.c1{fill:#cd0000;}.c2{fill:#00cd00;}.c3{fill:#cdcd00;}.c4{fill:#00e;}.c5{fill:#cd00cd;}.c6{fill:#00cdcd;}.c7{fill:#e5e5e5;}.c8{fill:#7f7f7f;}.c9{fill:#f00;}.c10{fill:#0f0;}.c11{fill:#ff0;}.c12{fill:#5c5cff;}.c13{fill:#f0f;}.c14{fill:#0ff;}.c15{fill:#fff;}"#;
const XTERM_COLORS: &'static [(u8, u8, u8)] = &[
/*0*/ (0, 0, 0),

Loading…
Cancel
Save