2022-10-25 12:52:46 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2022-12-20 12:48:37 -07:00
|
|
|
use core::fmt::Display;
|
|
|
|
|
|
2022-11-08 08:43:27 -07:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
|
2022-10-26 14:16:48 -06:00
|
|
|
use crate::{CacheKey, Color};
|
2022-10-25 12:52:46 -06:00
|
|
|
|
|
|
|
|
/// A laid out glyph
|
2022-10-27 10:29:19 -06:00
|
|
|
#[derive(Debug)]
|
2022-10-25 12:52:46 -06:00
|
|
|
pub struct LayoutGlyph {
|
|
|
|
|
/// Start index of cluster in original line
|
|
|
|
|
pub start: usize,
|
|
|
|
|
/// End index of cluster in original line
|
|
|
|
|
pub end: usize,
|
2023-06-20 06:07:24 +02:00
|
|
|
/// Font size of the glyph
|
|
|
|
|
pub font_size: f32,
|
|
|
|
|
/// Font id of the glyph
|
|
|
|
|
pub font_id: fontdb::ID,
|
|
|
|
|
/// Font id of the glyph
|
|
|
|
|
pub glyph_id: u16,
|
2022-10-25 12:52:46 -06:00
|
|
|
/// X offset of hitbox
|
|
|
|
|
pub x: f32,
|
2023-06-20 06:07:24 +02:00
|
|
|
/// Y offset of hitbox
|
|
|
|
|
pub y: f32,
|
|
|
|
|
/// Width of hitbox
|
2022-10-25 12:52:46 -06:00
|
|
|
pub w: f32,
|
2022-12-20 13:00:29 -07:00
|
|
|
/// Unicode BiDi embedding level, character is left-to-right if `level` is divisible by 2
|
2022-12-16 16:49:29 -07:00
|
|
|
pub level: unicode_bidi::Level,
|
2022-12-16 19:36:43 +01:00
|
|
|
/// X offset in line
|
|
|
|
|
///
|
2023-06-20 06:07:24 +02:00
|
|
|
/// If you are dealing with physical coordinates, use [`Self::physical`] to obtain a
|
|
|
|
|
/// [`PhysicalGlyph`] for rendering.
|
2022-12-16 19:36:43 +01:00
|
|
|
///
|
|
|
|
|
/// This offset is useful when you are dealing with logical units and you do not care or
|
|
|
|
|
/// cannot guarantee pixel grid alignment. For instance, when you want to use the glyphs
|
|
|
|
|
/// for vectorial text, apply linear transformations to the layout, etc.
|
2022-12-15 05:04:39 +01:00
|
|
|
pub x_offset: f32,
|
2022-12-16 19:36:43 +01:00
|
|
|
/// Y offset in line
|
|
|
|
|
///
|
2023-06-20 06:07:24 +02:00
|
|
|
/// If you are dealing with physical coordinates, use [`Self::physical`] to obtain a
|
|
|
|
|
/// [`PhysicalGlyph`] for rendering.
|
2022-12-16 19:36:43 +01:00
|
|
|
///
|
|
|
|
|
/// This offset is useful when you are dealing with logical units and you do not care or
|
|
|
|
|
/// cannot guarantee pixel grid alignment. For instance, when you want to use the glyphs
|
|
|
|
|
/// for vectorial text, apply linear transformations to the layout, etc.
|
2022-12-15 05:04:39 +01:00
|
|
|
pub y_offset: f32,
|
2022-10-26 14:16:48 -06:00
|
|
|
/// Optional color override
|
|
|
|
|
pub color_opt: Option<Color>,
|
2022-12-14 09:19:03 -07:00
|
|
|
/// Metadata from `Attrs`
|
|
|
|
|
pub metadata: usize,
|
2022-10-25 12:52:46 -06:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2023-06-20 06:07:24 +02:00
|
|
|
pub struct PhysicalGlyph {
|
|
|
|
|
/// Cache key, see [CacheKey]
|
|
|
|
|
pub cache_key: CacheKey,
|
|
|
|
|
/// Integer component of X offset in line
|
|
|
|
|
pub x: i32,
|
|
|
|
|
/// Integer component of Y offset in line
|
|
|
|
|
pub y: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LayoutGlyph {
|
|
|
|
|
pub fn physical(&self, offset: (f32, f32), scale: f32) -> PhysicalGlyph {
|
|
|
|
|
let x_offset = self.font_size * self.x_offset;
|
|
|
|
|
let y_offset = self.font_size * self.y_offset;
|
|
|
|
|
|
|
|
|
|
let (cache_key, x, y) = CacheKey::new(
|
|
|
|
|
self.font_id,
|
|
|
|
|
self.glyph_id,
|
|
|
|
|
self.font_size * scale,
|
|
|
|
|
(
|
|
|
|
|
(self.x + x_offset) * scale + offset.0,
|
2023-07-06 06:47:12 -06:00
|
|
|
libm::truncf((self.y - y_offset) * scale + offset.1), // Hinting in Y axis
|
2023-06-20 06:07:24 +02:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
PhysicalGlyph { cache_key, x, y }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
/// A line of laid out glyphs
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2022-10-25 12:52:46 -06:00
|
|
|
pub struct LayoutLine {
|
2022-12-15 04:59:31 +01:00
|
|
|
/// Width of the line
|
|
|
|
|
pub w: f32,
|
2023-06-16 01:47:35 +02:00
|
|
|
/// Maximum ascent of the glyphs in line
|
|
|
|
|
pub max_ascent: f32,
|
|
|
|
|
/// Maximum descent of the glyphs in line
|
|
|
|
|
pub max_descent: f32,
|
2022-10-25 12:52:46 -06:00
|
|
|
/// Glyphs in line
|
|
|
|
|
pub glyphs: Vec<LayoutGlyph>,
|
|
|
|
|
}
|
2022-12-20 12:48:37 -07:00
|
|
|
|
|
|
|
|
/// Wrapping mode
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
|
pub enum Wrap {
|
|
|
|
|
/// No wrapping
|
|
|
|
|
None,
|
|
|
|
|
/// Wraps at a glyph level
|
|
|
|
|
Glyph,
|
|
|
|
|
/// Word Wrapping
|
|
|
|
|
Word,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for Wrap {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::None => write!(f, "No Wrap"),
|
|
|
|
|
Self::Word => write!(f, "Word Wrap"),
|
|
|
|
|
Self::Glyph => write!(f, "Character"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-22 18:31:49 -07:00
|
|
|
|
|
|
|
|
/// Align or justify
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
|
pub enum Align {
|
|
|
|
|
Left,
|
|
|
|
|
Right,
|
|
|
|
|
Center,
|
|
|
|
|
Justified,
|
2023-07-07 21:31:17 -07:00
|
|
|
End,
|
2023-02-22 18:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for Align {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Left => write!(f, "Left"),
|
|
|
|
|
Self::Right => write!(f, "Right"),
|
|
|
|
|
Self::Center => write!(f, "Center"),
|
|
|
|
|
Self::Justified => write!(f, "Justified"),
|
2023-07-07 21:31:17 -07:00
|
|
|
Self::End => write!(f, "End"),
|
2023-02-22 18:31:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|