Skip to content

Commit 627d48f

Browse files
committed
[TTF] Store global metrics in the top-level Font representation
1 parent c336113 commit 627d48f

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

rust_programs/ttf_renderer/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ impl Display for Codepoint {
5050
}
5151
}
5252

53+
#[derive(Clone)]
5354
pub struct GlyphIndex(usize);
5455

56+
#[derive(Clone)]
5557
pub struct Font {
5658
pub name: String,
5759
pub bounding_box: Rect,
5860
pub units_per_em: usize,
61+
pub global_layout_metrics: FontGlobalLayoutMetrics,
5962
/// Sorted by glyph index
6063
pub glyphs: Vec<GlyphRenderDescription>,
6164
pub codepoints_to_glyph_indexes: BTreeMap<Codepoint, GlyphIndex>,
@@ -69,6 +72,7 @@ impl Font {
6972
name: &str,
7073
bounding_box: &Rect,
7174
units_per_em: usize,
75+
global_layout_metrics: FontGlobalLayoutMetrics,
7276
glyphs: Vec<GlyphRenderDescription>,
7377
codepoints_to_glyph_indexes: BTreeMap<Codepoint, GlyphIndex>,
7478
functions_table: BTreeMap<usize, FunctionDefinition>,
@@ -79,6 +83,7 @@ impl Font {
7983
name: name.to_string(),
8084
bounding_box: bounding_box.clone(),
8185
units_per_em,
86+
global_layout_metrics,
8287
glyphs,
8388
codepoints_to_glyph_indexes,
8489
functions_table,

rust_programs/ttf_renderer/src/parser.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ use crate::metrics::{
2626
use crate::parse_utils::{
2727
fixed_word_to_i32, BigEndianValue, FromFontBufInPlace, TransmuteFontBufInPlace,
2828
};
29-
#[cfg(target_os = "axle")]
29+
#[cfg(any(target_os = "axle", feature = "no_std"))]
3030
use axle_rt::println;
3131
use core::ptr::slice_from_raw_parts;
32-
#[cfg(not(target_os = "axle"))]
32+
#[cfg(not(any(target_os = "axle", feature = "no_std")))]
3333
use std::println;
3434

3535
#[repr(C, packed)]
@@ -334,23 +334,26 @@ impl<'a> FontParser<'a> {
334334
pub fn parse(&mut self) -> Font {
335335
let mut cursor = 0;
336336
let offset_subtable = OffsetSubtable::from_in_place_buf(self.read_with_cursor(&mut cursor));
337-
println!("Got offset subtable {offset_subtable:?}",);
337+
//println!("Got offset subtable {offset_subtable:?}",);
338338

339339
for i in 0..offset_subtable.num_tables {
340340
let table = TableHeader::new(self.read_with_cursor(&mut cursor));
341-
println!("Table #{i}: {table}");
341+
//println!("Table #{i}: {table}");
342342
self.table_headers.insert(table.tag, table);
343343
}
344344
let head = self.parse_table("head");
345345
self.head = Some(head);
346-
println!("Found head: {:?}", head);
346+
//println!("Found head: {:?}", head);
347347
let glyph_bounding_box = head.glyph_bounding_box;
348348

349349
let max_profile: MaxProfile = self.parse_table("maxp");
350-
println!("Got max profile {max_profile:?}");
350+
//println!("Got max profile {max_profile:?}");
351351

352352
let glyph_indexes_to_codepoints = parse_character_map(self);
353353

354+
let horizontal_glyph_metrics = parse_horizontal_metrics(self);
355+
let vertical_glyph_metrics = parse_vertical_metrics(self, max_profile.num_glyphs);
356+
354357
let mut all_glyphs = vec![];
355358
let mut codepoints_to_glyph_indexes = BTreeMap::new();
356359
for i in 0..max_profile.num_glyphs {
@@ -365,8 +368,12 @@ impl<'a> FontParser<'a> {
365368
}
366369
}
367370

368-
let horizontal_glyph_metrics = parse_horizontal_metrics(self);
369-
let vertical_glyph_metrics = parse_vertical_metrics(self, max_profile.num_glyphs);
371+
/*
372+
println!(
373+
"num glyphs {} vert {:?}",
374+
max_profile.num_glyphs, vertical_glyph_metrics
375+
);
376+
*/
370377
for (i, glyph) in all_glyphs.iter().enumerate() {
371378
// Compound glyphs may inherit their metrics from one of their children
372379
if let GlyphRenderInstructions::CompoundGlyph(compound_glyph_instructions) =
@@ -381,7 +388,7 @@ impl<'a> FontParser<'a> {
381388
}
382389
}
383390

384-
if let Some(horizontal_metrics) = horizontal_glyph_metrics.get(i) {
391+
if let Some(horizontal_metrics) = horizontal_glyph_metrics.long_hor_metrics.get(i) {
385392
glyph
386393
.render_metrics
387394
.set_horizontal_metrics(horizontal_metrics.clone());
@@ -465,6 +472,7 @@ impl<'a> FontParser<'a> {
465472
"abc",
466473
&self.head.unwrap().glyph_bounding_box,
467474
self.head.unwrap().units_per_em as _,
475+
horizontal_glyph_metrics,
468476
all_glyphs,
469477
codepoints_to_glyph_indexes,
470478
function_boundaries_lookup_map,

0 commit comments

Comments
 (0)