From c3361139d74ba0f774abc6f9baddb16b2c257548 Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Wed, 7 Feb 2024 15:14:42 +0000 Subject: [PATCH] [TTF] Flip glyph coordinate system by ascent, not by em-square-units --- rust_programs/ttf_renderer/src/glyphs.rs | 8 ++++---- rust_programs/ttf_renderer/src/lib.rs | 13 ++++++++----- rust_programs/ttf_renderer/src/parser.rs | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rust_programs/ttf_renderer/src/glyphs.rs b/rust_programs/ttf_renderer/src/glyphs.rs index 3f2831cd5..915449276 100644 --- a/rust_programs/ttf_renderer/src/glyphs.rs +++ b/rust_programs/ttf_renderer/src/glyphs.rs @@ -1,4 +1,4 @@ -use crate::metrics::{GlyphRenderMetrics, LongHorMetric, VerticalMetrics}; +use crate::metrics::{FontGlobalLayoutMetrics, GlyphRenderMetrics, LongHorMetric, VerticalMetrics}; use crate::parse_utils::{BigEndianValue, FromFontBufInPlace, TransmuteFontBufInPlace}; use crate::parser::FontParser; use crate::println; @@ -401,7 +401,7 @@ pub(crate) fn parse_glyph( parser: &FontParser, glyph_index: usize, all_glyphs_bounding_box: &Rect, - units_per_em: usize, + font_layout_metrics: &FontGlobalLayoutMetrics, ) -> GlyphRenderDescription { let glyph_header = parser.table_headers.get("glyf").unwrap(); let (glyph_local_offset, glyph_data_length) = get_glyph_offset_and_length(parser, glyph_index); @@ -469,7 +469,7 @@ pub(crate) fn parse_glyph( flag_count_to_parse -= 1; } - // Parse X coordinates + // Parse coordinates let x_values = interpret_values_via_flags(parser, &mut cursor, &all_flags, CoordinateComponentType::X); let y_values = @@ -478,7 +478,7 @@ pub(crate) fn parse_glyph( .iter() .zip(y_values.iter()) // Flip the Y axis of every point to match our coordinate system - .map(|(&x, &y)| PointF64::new(x as _, (units_per_em as isize - y) as _)) + .map(|(&x, &y)| PointF64::new(x as _, (font_layout_metrics.ascent - y) as _)) .collect(); // Split the total collection of points into polygons, using the last-point-indexes that diff --git a/rust_programs/ttf_renderer/src/lib.rs b/rust_programs/ttf_renderer/src/lib.rs index fa5d3e67a..47f803cb9 100644 --- a/rust_programs/ttf_renderer/src/lib.rs +++ b/rust_programs/ttf_renderer/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(target_os = "axle", no_std)] +#![cfg_attr(any(target_os = "axle", feature = "no_std"), no_std)] #![feature(core_intrinsics)] #![feature(slice_ptr_get)] #![feature(format_args_nl)] @@ -17,9 +17,11 @@ mod render_context; pub use crate::glyphs::{GlyphRenderDescription, GlyphRenderInstructions}; pub use crate::metrics::GlyphMetrics; -pub use crate::render::{render_antialiased_glyph_onto, render_char_onto, render_glyph_onto}; +pub use crate::render::{ + render_antialiased_glyph_onto, render_char_onto, render_glyph_onto, rendered_string_size, +}; pub use crate::render_context::FontRenderContext; -use agx_definitions::Rect; +use agx_definitions::{Rect, Size}; use alloc::collections::BTreeMap; use alloc::string::{String, ToString}; use alloc::vec::Vec; @@ -27,9 +29,10 @@ use core::fmt::{Display, Formatter}; use parser::FontParser; use crate::hints::FunctionDefinition; -#[cfg(target_os = "axle")] +use crate::metrics::FontGlobalLayoutMetrics; +#[cfg(any(target_os = "axle", feature = "no_std"))] pub(crate) use axle_rt::{print, println}; -#[cfg(not(target_os = "axle"))] +#[cfg(not(any(target_os = "axle", feature = "no_std")))] pub(crate) use std::{print, println}; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] diff --git a/rust_programs/ttf_renderer/src/parser.rs b/rust_programs/ttf_renderer/src/parser.rs index 5a692483a..fb4d9f173 100644 --- a/rust_programs/ttf_renderer/src/parser.rs +++ b/rust_programs/ttf_renderer/src/parser.rs @@ -354,7 +354,7 @@ impl<'a> FontParser<'a> { let mut all_glyphs = vec![]; let mut codepoints_to_glyph_indexes = BTreeMap::new(); for i in 0..max_profile.num_glyphs { - let parsed_glyph = parse_glyph(self, i, &glyph_bounding_box, head.units_per_em as _); + let parsed_glyph = parse_glyph(self, i, &glyph_bounding_box, &horizontal_glyph_metrics); all_glyphs.push(parsed_glyph); match glyph_indexes_to_codepoints.get(&i) {