maplibre/legacy/
glyph.rs

1//! Translated from https://github.com/maplibre/maplibre-native/blob/4add9ea/src/mbgl/text/glyph.cpp
2
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4
5use bitflags::bitflags;
6
7use crate::{
8    euclid::Rect,
9    legacy::{
10        bidi::Char16,
11        font_stack::{FontStack, FontStackHash},
12        glyph_range::GlyphRange,
13        TileSpace,
14    },
15};
16
17// TODO structs
18/// maplibre/maplibre-native#4add9ea original name: AlphaImage
19#[derive(Default)]
20pub struct AlphaImage;
21
22/// maplibre/maplibre-native#4add9ea original name: GlyphID
23pub type GlyphID = Char16;
24/// maplibre/maplibre-native#4add9ea original name: GlyphIDs
25pub type GlyphIDs = BTreeSet<GlyphID>;
26
27// Note: this only works for the BMP
28/// maplibre/maplibre-native#4add9ea original name: getGlyphRange
29pub fn get_glyph_range(glyph: GlyphID) -> GlyphRange {
30    let mut start: u16 = (glyph / 256) * 256;
31    let mut end = start + 255;
32    if start > 65280 {
33        start = 65280;
34    }
35    if end > 65535 {
36        end = 65535;
37    }
38    start..end
39}
40
41/// maplibre/maplibre-native#4add9ea original name: GlyphMetrics
42#[derive(PartialEq, Default, Copy, Clone)]
43pub struct GlyphMetrics {
44    pub width: u32,
45    pub height: u32,
46    pub left: i32,
47    pub top: i32,
48    pub advance: u32,
49}
50
51/// maplibre/maplibre-native#4add9ea original name: Glyph
52#[derive(Default)]
53pub struct Glyph {
54    // We're using this value throughout the Mapbox GL ecosystem. If this is
55    // different, the glyphs also need to be reencoded.
56    pub id: GlyphID,
57
58    // A signed distance field of the glyph with a border (see above).
59    pub bitmap: AlphaImage,
60
61    // Glyph metrics
62    pub metrics: GlyphMetrics,
63}
64
65impl Glyph {
66    pub const BORDER_SIZE: u8 = 3;
67}
68
69/// maplibre/maplibre-native#4add9ea original name: Glyphs
70pub type Glyphs = BTreeMap<GlyphID, Option<Glyph>>;
71/// maplibre/maplibre-native#4add9ea original name: GlyphMap
72pub type GlyphMap = BTreeMap<FontStackHash, Glyphs>;
73
74/// maplibre/maplibre-native#4add9ea original name: PositionedGlyph
75#[derive(Clone)]
76pub struct PositionedGlyph {
77    pub glyph: GlyphID,
78    pub x: f64,
79    pub y: f64,
80    pub vertical: bool,
81    pub font: FontStackHash,
82    pub scale: f64,
83    pub rect: Rect<u16, TileSpace>,
84    pub metrics: GlyphMetrics,
85    pub image_id: Option<String>,
86    // Maps positioned glyph to TaggedString section
87    pub section_index: usize,
88}
89
90/// maplibre/maplibre-native#4add9ea original name: PositionedLine
91#[derive(Default, Clone)]
92pub struct PositionedLine {
93    pub positioned_glyphs: Vec<PositionedGlyph>,
94    pub line_offset: f64,
95}
96
97/// maplibre/maplibre-native#4add9ea original name: Shaping
98#[derive(Clone, Default)]
99pub struct Shaping {
100    pub positioned_lines: Vec<PositionedLine>,
101    pub top: f64,
102    pub bottom: f64,
103    pub left: f64,
104    pub right: f64,
105    pub writing_mode: WritingModeType,
106
107    pub verticalizable: bool,
108    pub icons_in_text: bool,
109}
110impl Shaping {
111    // The y offset *should* be part of the font metadata.
112    pub const Y_OFFSET: i32 = -17;
113
114    /// maplibre/maplibre-native#4add9ea original name: new
115    pub fn new(x: f64, y: f64, writing_mode: WritingModeType) -> Self {
116        Self {
117            positioned_lines: vec![],
118            top: y,
119            bottom: y,
120            left: x,
121            right: x,
122            writing_mode,
123            verticalizable: false,
124            icons_in_text: false,
125        }
126    }
127    /// maplibre/maplibre-native#4add9ea original name: isAnyLineNotEmpty
128    pub fn is_any_line_not_empty(&self) -> bool {
129        self.positioned_lines
130            .iter()
131            .any(|line| !line.positioned_glyphs.is_empty())
132    }
133}
134
135bitflags! {
136    /// maplibre/maplibre-native#4add9ea original name: WritingModeType:
137    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
138    pub struct WritingModeType: u8 {
139        const None = 0;
140        const Horizontal = 1 << 0;
141        const Vertical = 1 << 1;
142    }
143}
144
145impl Default for WritingModeType {
146    /// maplibre/maplibre-native#4add9ea original name: default
147    fn default() -> Self {
148        WritingModeType::None
149    }
150}
151
152/// maplibre/maplibre-native#4add9ea original name: GlyphDependencies
153pub type GlyphDependencies = BTreeMap<FontStack, GlyphIDs>;
154/// maplibre/maplibre-native#4add9ea original name: GlyphRangeDependencies
155pub type GlyphRangeDependencies = BTreeMap<FontStack, HashSet<GlyphRange>>;