Skip to content

Commit

Permalink
view: Cycle through multiple fg colours if supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
wezm committed Oct 25, 2024
1 parent 146aa99 commit 6e9bcb5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ pub struct ViewOpts {

#[options(
help = "set the fill colour of the glyphs",
meta = "rrggbbaa",
meta = "rrggbbaa...",
no_short
)]
pub fg_colour: Option<Colour>,
pub fg_colour: Vec<Colour>,

#[options(
help = "set the background colour of the generated SVG",
Expand All @@ -379,8 +379,8 @@ pub struct ViewOpts {
)]
pub bg_colour: Option<Colour>,

#[options(help = "alias for --fg-colour", meta = "rrggbbaa", no_short)]
pub fg_color: Option<Colour>,
#[options(help = "alias for --fg-colour", meta = "rrggbbaa...", no_short)]
pub fg_color: Vec<Colour>,

#[options(help = "alias for --bg-colour", meta = "rrggbbaa", no_short)]
pub bg_color: Option<Colour>,
Expand Down
7 changes: 6 additions & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ fn parse_features(features: &str) -> Features {

impl From<&ViewOpts> for SVGMode {
fn from(opts: &ViewOpts) -> Self {
let fg = if opts.fg_colour.is_empty() {
&opts.fg_color
} else {
&opts.fg_colour
};
SVGMode::View {
mark_origin: opts.mark_origin,
margin: opts.margin.unwrap_or_default(),
fg: opts.fg_colour.or(opts.fg_color),
fg: fg.clone(),
bg: opts.bg_colour.or(opts.bg_color),
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::iter;
use std::str::FromStr;

use allsorts::cff::CFF;
Expand Down Expand Up @@ -187,7 +188,7 @@ pub enum SVGMode {
View {
mark_origin: bool,
margin: Margin,
fg: Option<Colour>,
fg: Vec<Colour>,
bg: Option<Colour>,
},
}
Expand All @@ -196,6 +197,7 @@ pub struct SVGWriter {
mode: SVGMode,
transform: Matrix2x2F,
usage: Vec<(usize, Vector2F)>,
fg_colour: Box<dyn Iterator<Item = Colour>>,
}

struct Symbols<'info> {
Expand All @@ -208,10 +210,18 @@ struct Symbols<'info> {

impl SVGWriter {
pub fn new(mode: SVGMode, transform: Matrix2x2F) -> Self {
let fg_colour = match &mode {
SVGMode::TextRenderingTests(_) => {
Box::new(iter::empty()) as Box<dyn Iterator<Item = Colour>>
}
SVGMode::View { fg, .. } => Box::new(fg.clone().into_iter().cycle()),
};

SVGWriter {
mode,
transform,
usage: Vec::new(),
fg_colour,
}
}

Expand Down Expand Up @@ -300,7 +310,7 @@ impl SVGWriter {
.push((symbol_index, self.transform * vec2f(x, y)));
}

fn end(self, x_max: f32, ascender: i16, descender: i16, symbols: Symbols) -> String {
fn end(mut self, x_max: f32, ascender: i16, descender: i16, symbols: Symbols) -> String {
let mut w = XmlWriter::new(xmlwriter::Options::default());
w.write_declaration();
w.start_element("svg");
Expand Down Expand Up @@ -414,11 +424,8 @@ impl SVGWriter {
}
}

fn fg_colour(&self) -> Option<Colour> {
match self.mode {
SVGMode::TextRenderingTests(_) => None,
SVGMode::View { fg, .. } => fg,
}
fn fg_colour(&mut self) -> Option<Colour> {
self.fg_colour.next()
}

fn bg_colour(&self) -> Option<Colour> {
Expand Down

0 comments on commit 6e9bcb5

Please sign in to comment.