label: Add text wrapping

This commit is contained in:
Simon Bruder 2024-07-19 18:02:03 +02:00
parent e4fb62f1c9
commit 40497e83ab
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 40 additions and 7 deletions

View file

@ -124,6 +124,7 @@ impl Code128Config {
pub struct TextConfig { pub struct TextConfig {
font_size: f32, font_size: f32,
position: (Mm, Mm), position: (Mm, Mm),
wrap: Option<usize>,
} }
impl TextConfig { impl TextConfig {
@ -133,15 +134,38 @@ impl TextConfig {
font: &IndirectFontRef, font: &IndirectFontRef,
text: Option<S>, text: Option<S>,
) -> Result<()> { ) -> Result<()> {
layer.use_text( layer.begin_text_section();
text.ok_or(Error::DataIncomplete("ID text".to_string()))?, layer.set_font(font, self.font_size);
self.font_size, layer.set_line_height(self.font_size);
self.position.0, layer.set_text_cursor(self.position.0, self.position.1);
self.position.1, if let Some(text) = self.wrap(text) {
font, for line in text.split('\n') {
); layer.write_text(line, font);
layer.add_line_break();
}
} else {
return Err(Error::DataIncomplete("ID text".to_string()));
}
layer.end_text_section();
Ok(()) Ok(())
} }
fn wrap<S: Into<String>>(&self, text: Option<S>) -> Option<String> {
if let Some(wrap) = self.wrap {
let mut inserted = 0;
text.map(|s| {
let s = s.into();
let mut chars: Vec<char> = s.chars().collect();
for i in (wrap..s.len()).step_by(wrap) {
chars.insert(i + inserted, '\n');
inserted += 1;
}
chars.iter().collect()
})
} else {
text.map(|s| s.into())
}
}
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -53,14 +53,17 @@ impl Into<LabelConfig> for LabelPreset {
id_text: Some(TextConfig { id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(0.0), Mm(22.0)), position: (Mm(0.0), Mm(22.0)),
wrap: None,
}), }),
short_id_text: Some(TextConfig { short_id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(21.0), Mm(0.6)), position: (Mm(21.0), Mm(0.6)),
wrap: None,
}), }),
branding: Some(TextConfig { branding: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(39.5), Mm(0.6)), position: (Mm(39.5), Mm(0.6)),
wrap: None,
}), }),
}, },
Self::SeikoSlpMrlCode128 => LabelConfig { Self::SeikoSlpMrlCode128 => LabelConfig {
@ -74,14 +77,17 @@ impl Into<LabelConfig> for LabelPreset {
id_text: Some(TextConfig { id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(0.0), Mm(22.0)), position: (Mm(0.0), Mm(22.0)),
wrap: None,
}), }),
short_id_text: Some(TextConfig { short_id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(0.0), Mm(0.0)), position: (Mm(0.0), Mm(0.0)),
wrap: None,
}), }),
branding: Some(TextConfig { branding: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(39.5), Mm(0.6)), position: (Mm(39.5), Mm(0.6)),
wrap: None,
}), }),
}, },
Self::SeikoSlpMrl => LabelConfig { Self::SeikoSlpMrl => LabelConfig {
@ -98,14 +104,17 @@ impl Into<LabelConfig> for LabelPreset {
id_text: Some(TextConfig { id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(0.0), Mm(22.0)), position: (Mm(0.0), Mm(22.0)),
wrap: None,
}), }),
short_id_text: Some(TextConfig { short_id_text: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(17.0), Mm(0.0)), position: (Mm(17.0), Mm(0.0)),
wrap: None,
}), }),
branding: Some(TextConfig { branding: Some(TextConfig {
font_size: 7.0, font_size: 7.0,
position: (Mm(39.5), Mm(0.6)), position: (Mm(39.5), Mm(0.6)),
wrap: None,
}), }),
}, },
} }