label: Add text wrapping

This commit is contained in:
Simon Bruder 2024-07-19 18:02:03 +02:00
parent 0f453072ea
commit cf5157c3cf
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 {
font_size: f32,
position: (Mm, Mm),
wrap: Option<usize>,
}
impl TextConfig {
@ -133,15 +134,38 @@ impl TextConfig {
font: &IndirectFontRef,
text: Option<S>,
) -> Result<()> {
layer.use_text(
text.ok_or(Error::DataIncomplete("ID text".to_string()))?,
self.font_size,
self.position.0,
self.position.1,
font,
);
layer.begin_text_section();
layer.set_font(font, self.font_size);
layer.set_line_height(self.font_size);
layer.set_text_cursor(self.position.0, self.position.1);
if let Some(text) = self.wrap(text) {
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(())
}
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)]

View file

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