Skip to content

Commit bb064fa

Browse files
committed
view: Cycle through multiple fg colours if supplied
1 parent 411ef8f commit bb064fa

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/cli.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ pub struct ViewOpts {
367367

368368
#[options(
369369
help = "set the fill colour of the glyphs",
370-
meta = "rrggbbaa",
370+
meta = "rrggbbaa...",
371371
no_short
372372
)]
373-
pub fg_colour: Option<Colour>,
373+
pub fg_colour: Vec<Colour>,
374374

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

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

385385
#[options(help = "alias for --bg-colour", meta = "rrggbbaa", no_short)]
386386
pub bg_color: Option<Colour>,

src/view.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,15 @@ fn parse_features(features: &str) -> Features {
178178

179179
impl From<&ViewOpts> for SVGMode {
180180
fn from(opts: &ViewOpts) -> Self {
181+
let fg = if opts.fg_colour.is_empty() {
182+
&opts.fg_color
183+
} else {
184+
&opts.fg_colour
185+
};
181186
SVGMode::View {
182187
mark_origin: opts.mark_origin,
183188
margin: opts.margin.unwrap_or_default(),
184-
fg: opts.fg_colour.or(opts.fg_color),
189+
fg: fg.clone(),
185190
bg: opts.bg_colour.or(opts.bg_color),
186191
}
187192
}

src/writer.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::fmt::{Display, Formatter};
4+
use std::iter;
45
use std::str::FromStr;
56

67
use allsorts::cff::CFF;
@@ -187,7 +188,7 @@ pub enum SVGMode {
187188
View {
188189
mark_origin: bool,
189190
margin: Margin,
190-
fg: Option<Colour>,
191+
fg: Vec<Colour>,
191192
bg: Option<Colour>,
192193
},
193194
}
@@ -196,6 +197,7 @@ pub struct SVGWriter {
196197
mode: SVGMode,
197198
transform: Matrix2x2F,
198199
usage: Vec<(usize, Vector2F)>,
200+
fg_colour: Box<dyn Iterator<Item = Colour>>,
199201
}
200202

201203
struct Symbols<'info> {
@@ -208,10 +210,18 @@ struct Symbols<'info> {
208210

209211
impl SVGWriter {
210212
pub fn new(mode: SVGMode, transform: Matrix2x2F) -> Self {
213+
let fg_colour = match &mode {
214+
SVGMode::TextRenderingTests(_) => {
215+
Box::new(iter::empty()) as Box<dyn Iterator<Item = Colour>>
216+
}
217+
SVGMode::View { fg, .. } => Box::new(fg.clone().into_iter().cycle()),
218+
};
219+
211220
SVGWriter {
212221
mode,
213222
transform,
214223
usage: Vec::new(),
224+
fg_colour,
215225
}
216226
}
217227

@@ -300,7 +310,7 @@ impl SVGWriter {
300310
.push((symbol_index, self.transform * vec2f(x, y)));
301311
}
302312

303-
fn end(self, x_max: f32, ascender: i16, descender: i16, symbols: Symbols) -> String {
313+
fn end(mut self, x_max: f32, ascender: i16, descender: i16, symbols: Symbols) -> String {
304314
let mut w = XmlWriter::new(xmlwriter::Options::default());
305315
w.write_declaration();
306316
w.start_element("svg");
@@ -414,11 +424,8 @@ impl SVGWriter {
414424
}
415425
}
416426

417-
fn fg_colour(&self) -> Option<Colour> {
418-
match self.mode {
419-
SVGMode::TextRenderingTests(_) => None,
420-
SVGMode::View { fg, .. } => fg,
421-
}
427+
fn fg_colour(&mut self) -> Option<Colour> {
428+
self.fg_colour.next()
422429
}
423430

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

0 commit comments

Comments
 (0)