1use 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#[derive(Default)]
20pub struct AlphaImage;
21
22pub type GlyphID = Char16;
24pub type GlyphIDs = BTreeSet<GlyphID>;
26
27pub 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#[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#[derive(Default)]
53pub struct Glyph {
54 pub id: GlyphID,
57
58 pub bitmap: AlphaImage,
60
61 pub metrics: GlyphMetrics,
63}
64
65impl Glyph {
66 pub const BORDER_SIZE: u8 = 3;
67}
68
69pub type Glyphs = BTreeMap<GlyphID, Option<Glyph>>;
71pub type GlyphMap = BTreeMap<FontStackHash, Glyphs>;
73
74#[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 pub section_index: usize,
88}
89
90#[derive(Default, Clone)]
92pub struct PositionedLine {
93 pub positioned_glyphs: Vec<PositionedGlyph>,
94 pub line_offset: f64,
95}
96
97#[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 pub const Y_OFFSET: i32 = -17;
113
114 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 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 #[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 fn default() -> Self {
148 WritingModeType::None
149 }
150}
151
152pub type GlyphDependencies = BTreeMap<FontStack, GlyphIDs>;
154pub type GlyphRangeDependencies = BTreeMap<FontStack, HashSet<GlyphRange>>;