From 479b7196cfe4bb6ecef2401b7ef079666ae20383 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Thu, 30 Oct 2025 20:15:02 +0100 Subject: [PATCH] Add support for "faux italics" --- CHANGELOG.md | 6 +++++- src/api/rendering.rs | 7 +++++-- src/fonts.rs | 8 +++++++- src/fonts/layout.rs | 23 ++++++++++++++++++++++- src/fonts/rasterizer.rs | 27 +++++++++++++++++++++++---- src/installer.rs | 3 ++- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a5dccb..c01e7fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] - ReleaseDate +### Added + +- Add support for "faux italics" + ## [0.2.4] - 2025-10-30 ### Added @@ -83,8 +87,8 @@ - First release -[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.4...HEAD +[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.4...HEAD [0.2.4]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.3...v0.2.4 [0.2.3]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.2...v0.2.3 [0.2.2]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.1...v0.2.2 diff --git a/src/api/rendering.rs b/src/api/rendering.rs index 7fc0ed0..8d0a764 100644 --- a/src/api/rendering.rs +++ b/src/api/rendering.rs @@ -2,7 +2,7 @@ use crate::{ api::image_handle::ImageHandle, color::Srgba, dpi::Uv, - fonts::{Alignment, LayoutJob}, + fonts::{Alignment, FontStyle, LayoutJob}, lua::ContextSocket, math::{Point, Quad, Rect, Size}, }; @@ -468,6 +468,7 @@ fn build_layout_job<'a>( alignment: Option, ) -> LayoutJob<'a> { let mut font_weight = None; + let mut font_style = FontStyle::default(); let font_family = match font_type { PoBFontType::Fixed => FontFamily::Named(Cow::Borrowed("Bitstream Vera Sans Mono")), PoBFontType::Var => FontFamily::Named(Cow::Borrowed("Liberation Sans")), @@ -478,7 +479,8 @@ fn build_layout_job<'a>( PoBFontType::FontinItalic => FontFamily::Named(Cow::Borrowed("Fontin")), PoBFontType::FontinSmallcaps => FontFamily::Named(Cow::Borrowed("Fontin SmallCaps")), PoBFontType::FontinSmallcapsItalic => { - // TODO: use regular Smallcaps for now, add "faux italics" later + font_style = FontStyle::Italic; + // use regular Smallcaps with "faux italics" FontFamily::Named(Cow::Borrowed("Fontin SmallCaps")) } }; @@ -492,6 +494,7 @@ fn build_layout_job<'a>( line_height as f32, alignment, font_weight, + font_style, ); for (color, segment) in PoBString(text).into_iter() { diff --git a/src/fonts.rs b/src/fonts.rs index 4490bcb..3eefcef 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -16,7 +16,7 @@ use parley::{ use std::sync::Arc; pub use atlas::FontAtlasSize; -pub use layout::{Alignment, Layout, LayoutJob}; +pub use layout::{Alignment, FontStyle, Layout, LayoutJob}; mod atlas; mod glyph_key; @@ -124,18 +124,21 @@ impl Fonts { font_size, FontFamily::Generic(GenericFamily::Monospace), None, + parley::FontStyle::Normal, ); self.preload_text( &common_chars, font_size, FontFamily::Generic(GenericFamily::SansSerif), None, + parley::FontStyle::Normal, ); self.preload_text( &common_chars, font_size, FontFamily::Generic(GenericFamily::SansSerif), Some(FontWeight::BOLD), + parley::FontStyle::Normal, ); } @@ -145,12 +148,14 @@ impl Fonts { font_size: f32, font_family: FontFamily, font_weight: Option, + font_style: parley::FontStyle, ) { profiling::scope!("preload_text"); let style = TextStyle { font_stack: FontStack::Single(font_family), font_weight: font_weight.unwrap_or(FontWeight::NORMAL), + font_style, font_size, ..Default::default() }; @@ -202,6 +207,7 @@ impl Fonts { .font_weight .map(|w| parley::FontWeight::new(w.into())) .unwrap_or(default_style.font_weight), + font_style: job.font_style.into(), ..default_style }; diff --git a/src/fonts/layout.rs b/src/fonts/layout.rs index 6640114..879d8d8 100644 --- a/src/fonts/layout.rs +++ b/src/fonts/layout.rs @@ -2,13 +2,30 @@ use crate::{color::Srgba, dpi::LogicalPoint, fonts::rasterizer::RasterizedGlyph} use ordered_float::OrderedFloat; use parley::FontFamily; -#[derive(Copy, Clone, Debug, Hash)] +#[derive(Copy, Clone, Default, Debug, Hash, PartialEq)] pub enum Alignment { + #[default] Min, Center, Max, } +#[derive(Copy, Clone, Default, Debug, Hash, PartialEq)] +pub enum FontStyle { + #[default] + Normal, + Italic, +} + +impl From for parley::FontStyle { + fn from(value: FontStyle) -> Self { + match value { + FontStyle::Normal => parley::FontStyle::Normal, + FontStyle::Italic => parley::FontStyle::Italic, + } + } +} + #[derive(Clone, Debug, Hash)] pub struct LayoutSegment<'s> { pub text: &'s str, @@ -23,6 +40,7 @@ pub struct LayoutJob<'s> { pub line_height: OrderedFloat, pub alignment: Option, pub font_weight: Option>, + pub font_style: FontStyle, } impl<'s> LayoutJob<'s> { @@ -32,6 +50,7 @@ impl<'s> LayoutJob<'s> { line_height: f32, alignment: Option, font_weight: Option, + font_style: FontStyle, ) -> Self { Self { segments: Vec::new(), @@ -40,6 +59,7 @@ impl<'s> LayoutJob<'s> { line_height: line_height.into(), alignment, font_weight: font_weight.map(OrderedFloat), + font_style, } } @@ -63,6 +83,7 @@ impl std::hash::Hash for LayoutJob<'_> { self.line_height.hash(state); self.alignment.hash(state); self.font_weight.hash(state); + self.font_style.hash(state); } } diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index 93a09db..a933c69 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -14,6 +14,7 @@ use image::GenericImage; use ordered_float::OrderedFloat; use parley::{FontData, GlyphRun}; use std::borrow::Cow; +use swash::zeno; type FontBlobId = u64; type FontIndex = u32; @@ -25,14 +26,21 @@ struct StyleKey<'a> { font_blob_id: FontBlobId, font_size: OrderedFloat, normalized_coords: Cow<'a, [i16]>, + skew: i8, } impl<'a> StyleKey<'a> { - fn new(font_blob_id: FontBlobId, font_size: f32, normalized_coords: &'a [i16]) -> Self { + fn new( + font_blob_id: FontBlobId, + font_size: f32, + normalized_coords: &'a [i16], + skew: i8, + ) -> Self { Self { font_blob_id, font_size: font_size.into(), normalized_coords: Cow::Borrowed(normalized_coords), + skew, } } @@ -135,8 +143,9 @@ impl GlyphRasterizer { font_data: &FontData, font_size: f32, norm_coords: &[i16], + skew: i8, ) -> StyleId { - let style_key = StyleKey::new(font_data.data.id(), font_size, norm_coords); + let style_key = StyleKey::new(font_data.data.id(), font_size, norm_coords, skew); match self.style_ids.get(&style_key) { Some(key) => *key, None => *self @@ -164,9 +173,16 @@ impl GlyphRasterizer { let color = glyph_run.style().brush; let font_size = run.font_size() * pixels_per_point; let normalized_coords = run.normalized_coords(); + let skew = run.synthesis().skew(); // skew angle for faux italic let font_ref = self.get_font_ref(run.font()); - let style_id = self.get_style_id(run.font(), font_size, normalized_coords); + let style_id = self.get_style_id( + run.font(), + font_size, + normalized_coords, + // parley stores skew as i8 internally so this conversion is ok + skew.unwrap_or_default() as i8, + ); let mut scaler = self .scale_context @@ -198,7 +214,10 @@ impl GlyphRasterizer { swash::scale::Source::ColorBitmap(swash::scale::StrikeWith::BestFit), swash::scale::Source::Outline, ]) - .format(swash::zeno::Format::Alpha) + .format(zeno::Format::Alpha) + .transform(skew.map(|skew| { + zeno::Transform::skew(zeno::Angle::from_degrees(skew), zeno::Angle::ZERO) + })) .offset(fract_offset) .render_into(&mut scaler, glyph.id as u16, image); diff --git a/src/installer.rs b/src/installer.rs index d3b8538..6108f9a 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -3,7 +3,7 @@ use crate::{ args::Game, color::Srgba, dpi::{LogicalPoint, LogicalRect}, - fonts::{Alignment, LayoutJob}, + fonts::{Alignment, FontStyle, LayoutJob}, mode::{AppEvent, ModeFrameOutput, ModeTransition}, renderer::primitives::{ClippedPrimitive, DrawPrimitive, TextPrimitive}, }; @@ -120,6 +120,7 @@ impl InstallMode { 34.0, Some(Alignment::Center), Some(700.0), + FontStyle::Normal, ); let progress_text = match &self.current_progress {