Skip to content

Commit

Permalink
[TTF] Flip glyph coordinate system by ascent, not by em-square-units
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Feb 7, 2024
1 parent 2b032a2 commit c336113
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
8 changes: 4 additions & 4 deletions rust_programs/ttf_renderer/src/glyphs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions rust_programs/ttf_renderer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -17,19 +17,22 @@ 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;
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)]
Expand Down
2 changes: 1 addition & 1 deletion rust_programs/ttf_renderer/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c336113

Please sign in to comment.