| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | use crate::graphics::{BitmapFont, BitmapGlyphs}; |
| 5 | use crate::software_renderer::fixed::Fixed; |
| 6 | use crate::software_renderer::PhysicalLength; |
| 7 | use crate::textlayout::{FontMetrics, Glyph, TextShaper}; |
| 8 | |
| 9 | use super::{GlyphRenderer, RenderableGlyph}; |
| 10 | |
| 11 | impl BitmapGlyphs { |
| 12 | /// Returns the size of the pre-rendered font in pixels. |
| 13 | pub fn pixel_size(&self) -> PhysicalLength { |
| 14 | PhysicalLength::new(self.pixel_size) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // A font that is resolved to a specific pixel size. |
| 19 | pub struct PixelFont { |
| 20 | pub bitmap_font: &'static BitmapFont, |
| 21 | pub glyphs: &'static BitmapGlyphs, |
| 22 | pub pixel_size: PhysicalLength, |
| 23 | } |
| 24 | |
| 25 | impl PixelFont { |
| 26 | pub fn glyph_index_to_glyph_id(index: usize) -> core::num::NonZeroU16 { |
| 27 | core::num::NonZeroU16::new(index as u16 + 1).unwrap() |
| 28 | } |
| 29 | pub fn glyph_id_to_glyph_index(id: core::num::NonZeroU16) -> usize { |
| 30 | id.get() as usize - 1 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl GlyphRenderer for PixelFont { |
| 35 | fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> Option<RenderableGlyph> { |
| 36 | let glyph_index = Self::glyph_id_to_glyph_index(glyph_id); |
| 37 | let bitmap_glyph = &self.glyphs.glyph_data[glyph_index]; |
| 38 | if bitmap_glyph.data.len() == 0 { |
| 39 | // For example, ' ' has no glyph data |
| 40 | return None; |
| 41 | } |
| 42 | // t represent the target coordinate system, and s the source glyph coordinate system. |
| 43 | // We want to align the glyph such that Δ(hₜ+yₜ)+offset = hₛ+yₛ |
| 44 | // where hₜ is the integer height of the glyph in the target coordinate system |
| 45 | // and offset is smaller than Δ |
| 46 | // We also want that Δ(hₜ-1)+offset ≤ hₛ-1 |
| 47 | // Similar for x but that's easier since x is not subtracted from the width |
| 48 | let delta = Fixed::<i32, 8>::from_fixed(self.scale_delta()); |
| 49 | let src_x = Fixed::<i32, 8>::from_fixed(Fixed::<_, 6>(bitmap_glyph.x)); |
| 50 | let src_y = Fixed::<i32, 8>::from_fixed(Fixed::<_, 6>(bitmap_glyph.y)); |
| 51 | let h_plus_y = Fixed::<i32, 8>::from_integer(bitmap_glyph.height as i32) + src_y; |
| 52 | let h_plus_y = Fixed::<i32, 8>::from_fraction(h_plus_y.0, delta.0); |
| 53 | let off_y = Fixed::<i32, 8>(h_plus_y.0 & 0xff); |
| 54 | let height = (Fixed::from_integer(bitmap_glyph.height as i32 - 1) - off_y) / delta + 1; |
| 55 | let x = Fixed::from_fraction(src_x.0, delta.0); |
| 56 | let off_x = Fixed::<i32, 8>(-x.0 & 0xff); |
| 57 | let width = (Fixed::from_integer(bitmap_glyph.width as i32 - 1) - off_x) / delta + 1; |
| 58 | Some(RenderableGlyph { |
| 59 | x, |
| 60 | y: h_plus_y - Fixed::from_integer(height), |
| 61 | width: PhysicalLength::new(width as i16), |
| 62 | height: PhysicalLength::new(height as i16), |
| 63 | alpha_map: bitmap_glyph.data.as_slice().into(), |
| 64 | pixel_stride: bitmap_glyph.width as u16, |
| 65 | sdf: self.bitmap_font.sdf, |
| 66 | }) |
| 67 | } |
| 68 | fn scale_delta(&self) -> Fixed<u16, 8> { |
| 69 | Fixed::try_from_fixed(Fixed::<u32, 8>::from_fraction( |
| 70 | self.glyphs.pixel_size as u32, |
| 71 | self.pixel_size.get() as u32, |
| 72 | )) |
| 73 | .unwrap() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl TextShaper for PixelFont { |
| 78 | type LengthPrimitive = i16; |
| 79 | type Length = PhysicalLength; |
| 80 | fn shape_text<GlyphStorage: core::iter::Extend<Glyph<PhysicalLength>>>( |
| 81 | &self, |
| 82 | text: &str, |
| 83 | glyphs: &mut GlyphStorage, |
| 84 | ) { |
| 85 | let glyphs_iter = text.char_indices().map(|(byte_offset, char)| { |
| 86 | let glyph_index = self |
| 87 | .bitmap_font |
| 88 | .character_map |
| 89 | .binary_search_by_key(&char, |char_map_entry| char_map_entry.code_point) |
| 90 | .ok() |
| 91 | .map(|char_map_index| { |
| 92 | self.bitmap_font.character_map[char_map_index].glyph_index as usize |
| 93 | }); |
| 94 | let x_advance = glyph_index.map_or_else( |
| 95 | || self.pixel_size, |
| 96 | |glyph_index| { |
| 97 | ((self.pixel_size.cast() |
| 98 | * self.glyphs.glyph_data[glyph_index].x_advance as i32 |
| 99 | / self.glyphs.pixel_size as i32 |
| 100 | + euclid::Length::new(32)) |
| 101 | / 64) |
| 102 | .cast() |
| 103 | }, |
| 104 | ); |
| 105 | Glyph { |
| 106 | glyph_id: glyph_index.map(Self::glyph_index_to_glyph_id), |
| 107 | advance: x_advance, |
| 108 | text_byte_offset: byte_offset, |
| 109 | ..Default::default() |
| 110 | } |
| 111 | }); |
| 112 | glyphs.extend(glyphs_iter); |
| 113 | } |
| 114 | |
| 115 | fn glyph_for_char(&self, ch: char) -> Option<Glyph<PhysicalLength>> { |
| 116 | self.bitmap_font |
| 117 | .character_map |
| 118 | .binary_search_by_key(&ch, |char_map_entry| char_map_entry.code_point) |
| 119 | .ok() |
| 120 | .map(|char_map_index| { |
| 121 | let glyph_index = |
| 122 | self.bitmap_font.character_map[char_map_index].glyph_index as usize; |
| 123 | let bitmap_glyph = &self.glyphs.glyph_data[glyph_index]; |
| 124 | let x_advance = ((self.pixel_size.cast() * bitmap_glyph.x_advance as i32 |
| 125 | / self.glyphs.pixel_size as i32 |
| 126 | + euclid::Length::new(32)) |
| 127 | / 64) |
| 128 | .cast(); |
| 129 | Glyph { |
| 130 | glyph_id: Some(Self::glyph_index_to_glyph_id(glyph_index)), |
| 131 | advance: x_advance, |
| 132 | text_byte_offset: 0, |
| 133 | ..Default::default() |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | fn max_lines(&self, max_height: PhysicalLength) -> usize { |
| 139 | (max_height / self.height()).get() as _ |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl FontMetrics<PhysicalLength> for PixelFont { |
| 144 | fn ascent(&self) -> PhysicalLength { |
| 145 | (self.pixel_size.cast() * self.bitmap_font.ascent / self.bitmap_font.units_per_em).cast() |
| 146 | } |
| 147 | |
| 148 | fn descent(&self) -> PhysicalLength { |
| 149 | (self.pixel_size.cast() * self.bitmap_font.descent / self.bitmap_font.units_per_em).cast() |
| 150 | } |
| 151 | |
| 152 | fn height(&self) -> PhysicalLength { |
| 153 | // The descent is negative (relative to the baseline) |
| 154 | (self.pixel_size.cast() * (self.bitmap_font.ascent - self.bitmap_font.descent) |
| 155 | / self.bitmap_font.units_per_em) |
| 156 | .cast() |
| 157 | } |
| 158 | |
| 159 | fn x_height(&self) -> PhysicalLength { |
| 160 | (self.pixel_size.cast() * self.bitmap_font.x_height / self.bitmap_font.units_per_em).cast() |
| 161 | } |
| 162 | |
| 163 | fn cap_height(&self) -> PhysicalLength { |
| 164 | (self.pixel_size.cast() * self.bitmap_font.cap_height / self.bitmap_font.units_per_em) |
| 165 | .cast() |
| 166 | } |
| 167 | } |
| 168 | |