Skip to content

Commit f4be9ee

Browse files
authored
Merge pull request #7 from SimplePixelFont/improve/abi
Improve ABI Error Management
2 parents 75f9d0d + 51606de commit f4be9ee

17 files changed

Lines changed: 172 additions & 91 deletions

File tree

.vscode/settings.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
{
22
"cSpell.words": [
33
"bezpath",
4+
"bspf",
45
"cmap",
56
"codepoints",
67
"colr",
8+
"DOGRAY",
79
"glyf",
10+
"GRIDFIT",
811
"gsub",
912
"hhea",
1013
"hmtx",
14+
"kurbo",
1115
"loca",
1216
"maxp",
17+
"nonmarkingreturn",
18+
"notdef",
1319
"panose",
20+
"PICO",
1421
"pixmap",
1522
"pixmaps",
1623
"ppem",
17-
"spfc"
24+
"Renderable",
25+
"spfc",
26+
"Subtable"
1827
]
1928
}

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backends/spfc-target-p8/src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{Error, anyhow};
12
use spf::core::layout_from_data;
23
use spfc_abi::{BackendInfo, CURRENT_ABI_VERSION, CompileOptions, CompileResult, PluginOption};
34

@@ -21,26 +22,33 @@ fn get_plugin_options() -> Vec<PluginOption> {
2122
}
2223

2324
#[spfc_abi::export]
24-
fn compile(options: CompileOptions) -> CompileResult {
25-
let data = std::fs::read(&options.input).unwrap();
26-
let layout = layout_from_data(&data).unwrap();
27-
let font_table = layout.font_tables.first().unwrap();
28-
let font = font_table.fonts.first().unwrap();
25+
fn compile(options: CompileOptions) -> Result<CompileResult, Error> {
26+
let data = std::fs::read(&options.input)?;
27+
let layout = layout_from_data(&data)
28+
.map_err(|e| anyhow!("Failed to parse input data into layout: {e:?}"))?;
29+
let font_table = layout
30+
.font_tables
31+
.first()
32+
.ok_or_else(|| anyhow!("No font tables found"))?;
33+
let font = font_table
34+
.fonts
35+
.first()
36+
.ok_or_else(|| anyhow!("No fonts found in font table"))?;
2937

3038
let mut process = Process::default();
3139
process.family_name = font.name.clone();
3240
process.family_version = font.version as f64;
3341
process.manufacturer = font.author.clone();
3442
process.pixmap_pairs = create_pixmap_pairs(&layout);
3543

36-
let font_data = create_program_string(&process).unwrap();
37-
std::fs::write(&options.output, &font_data).unwrap();
44+
let font_data = create_program_string(&process)?;
45+
std::fs::write(&options.output, &font_data)?;
3846

3947
println!(
4048
"Finished writing {} bytes to {}",
4149
font_data.len(),
4250
options.output
4351
);
4452

45-
CompileResult::Success
53+
Ok(CompileResult::Success)
4654
}

backends/spfc-target-p8/src/utilities/pixmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl TextureBuilder<PixmapGlyph> for PixmapGlyphTextureBuilder {
2828
let bits_per_pixel = pixmap_table
2929
.constant_bits_per_pixel
3030
.or(pixmap.custom_bits_per_pixel)
31-
.unwrap();
31+
.unwrap_or(1);
3232

3333
let advance_x = character.advance_x.unwrap_or(width);
3434
if advance_x != width {

backends/spfc-target-ttf/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "spfc-target-ttf"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition.workspace = true
55

66
[lib]

backends/spfc-target-ttf/src/builders/glyf_loca.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utilities::PixelGrid;
22

33
use super::Process;
4-
use anyhow::Result;
4+
use anyhow::{Error, Result};
55
use kurbo::BezPath;
66
use write_fonts::tables::glyf::{GlyfLocaBuilder, SimpleGlyph};
77

@@ -21,14 +21,20 @@ pub fn push_glyf_loca_tables(process: &mut Process) -> Result<()> {
2121
process.max_pixel_height as usize - process.descender_pixels as usize - 1,
2222
);
2323
let notdef =
24-
SimpleGlyph::from_bezpath(&notdef.to_bezpath(process.descender_pixels as usize)).unwrap();
24+
SimpleGlyph::from_bezpath(&notdef.to_bezpath(process.descender_pixels as usize)?).map_err(
25+
|x| Error::msg(format!("{x:?}"))
26+
)?;
2527
glyf_builder.add_glyph(&notdef)?;
2628

27-
let null_glyph = SimpleGlyph::from_bezpath(&BezPath::new()).unwrap();
29+
let null_glyph = SimpleGlyph::from_bezpath(&BezPath::new()).map_err(
30+
|x| Error::msg(format!("{x:?}"))
31+
)?;
2832
glyf_builder.add_glyph(&null_glyph)?;
2933

3034
// Glyph 2: nonmarkingreturn (empty glyph for tab/return)
31-
let nonmarkingreturn = SimpleGlyph::from_bezpath(&BezPath::new()).unwrap();
35+
let nonmarkingreturn = SimpleGlyph::from_bezpath(&BezPath::new()).map_err(
36+
|x| Error::msg(format!("{x:?}"))
37+
)?;
3238
glyf_builder.add_glyph(&nonmarkingreturn)?;
3339

3440
for (_, pixmap) in &process.pixmap_pairs {
@@ -37,15 +43,15 @@ pub fn push_glyf_loca_tables(process: &mut Process) -> Result<()> {
3743
let glyph = pixmap.clone().into_simple_glyph(
3844
process.target_pixel_size as u16,
3945
process.descender_pixels as usize,
40-
);
46+
)?;
4147
glyf_builder.add_glyph(&glyph)?;
4248
}
4349

4450
for pixmap in &process.color_layer_glyphs {
4551
let glyph = pixmap.clone().into_simple_glyph(
4652
process.target_pixel_size as u16,
4753
process.descender_pixels as usize,
48-
);
54+
)?;
4955
glyf_builder.add_glyph(&glyph)?;
5056
}
5157

backends/spfc-target-ttf/src/builders/head.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ fn unix_to_mac_epoch(unix_time: u64) -> i64 {
1313

1414
pub fn push_head_table(process: &mut Process) -> Result<()> {
1515
let now = SystemTime::now()
16-
.duration_since(UNIX_EPOCH)
17-
.unwrap()
16+
.duration_since(UNIX_EPOCH)?
1817
.as_secs();
1918
let mac_timestamp = unix_to_mac_epoch(now);
2019

backends/spfc-target-ttf/src/builders/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeMap;
2+
use anyhow::Result;
23
use render_spf::PixelRef;
34
use spf::core::Layout;
45

@@ -255,7 +256,7 @@ impl Process<'_> {
255256
let dynamic_glyphs = u16::try_from(dynamic_glyphs).unwrap_or(u16::MAX);
256257
AUTOINSERTED_CHARS_COUNT.saturating_add(dynamic_glyphs)
257258
}
258-
pub fn update_max_points_and_contours(&mut self) {
259+
pub fn update_max_points_and_contours(&mut self) -> Result<()> {
259260
self.max_points = 0;
260261
self.max_contours = 0;
261262

@@ -267,7 +268,7 @@ impl Process<'_> {
267268
let glyph = pixmap.clone().into_simple_glyph(
268269
self.target_pixel_size as u16,
269270
self.descender_pixels as usize,
270-
);
271+
)?;
271272

272273
let mut points = 0;
273274
for contour in &glyph.contours {
@@ -277,5 +278,6 @@ impl Process<'_> {
277278
self.max_points = self.max_points.max(points);
278279
self.max_contours = self.max_contours.max(contours);
279280
}
281+
Ok(())
280282
}
281283
}

0 commit comments

Comments
 (0)