Add support for "faux italics"

This commit is contained in:
meehl committed 2025-10-30 20:15:02 +01:00
1 parent a338ff02f1
commit 479b7196cf
6 files changed
+64 -10

No files matched your search

+5 -1
View File
@@ -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
<!-- next-url -->
[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
+5 -2
View File
@@ -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<Alignment>,
) -> 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() {
+7 -1
View File
@@ -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<FontWeight>,
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
};
+22 -1
View File
@@ -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<FontStyle> 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<f32>,
pub alignment: Option<Alignment>,
pub font_weight: Option<OrderedFloat<f32>>,
pub font_style: FontStyle,
}
impl<'s> LayoutJob<'s> {
@@ -32,6 +50,7 @@ impl<'s> LayoutJob<'s> {
line_height: f32,
alignment: Option<Alignment>,
font_weight: Option<f32>,
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);
}
}
+23 -4
View File
@@ -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<f32>,
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);
+2 -1
View File
@@ -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 {