1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! A tiny low level text processing library dedicate to Ribir, use to reorder,
//! shape and do simple layout for text. It's focus
//!
//! Some detail processing learn from [usvg](https://github.com/RazrFalcon/resvg/blob/master/usvg/src/text)
#![feature(test)]
pub mod font_db;
pub mod shaper;
use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use fontdb::ID;
pub use fontdb::{Stretch as FontStretch, Style as FontStyle, Weight as FontWeight};
pub use ribir_algo::Substr;
use std::hash::Hash;
pub mod text_reorder;
pub mod typography;
use ordered_float::OrderedFloat;
pub use text_reorder::TextReorder;
use ttf_parser::GlyphId;
mod typography_store;
pub use typography_store::{TypographyStore, VisualGlyphs};

mod text_writer;
pub use text_writer::{CharacterCursor, ControlChar, TextWriter};

mod grapheme_cursor;
pub use grapheme_cursor::GraphemeCursor;

pub mod unicode_help;

/// Unit for convert between pixel and em.
pub const PIXELS_PER_EM: f32 = 16.;

/// `Pixels is an absolute length unit and relative to the view device
#[derive(
  Debug,
  Default,
  Clone,
  Copy,
  PartialEq,
  PartialOrd,
  Add,
  Sub,
  Div,
  AddAssign,
  Mul,
  SubAssign,
  Eq,
  Ord,
  Hash,
)]
pub struct Pixel(pub OrderedFloat<f32>);

///  `Em` is relative length unit relative to `Pixel`. We stipulate Em(1.) equal
/// to Pixel(16.)
#[derive(
  Debug,
  Default,
  Clone,
  Copy,
  PartialEq,
  PartialOrd,
  Add,
  Sub,
  Div,
  AddAssign,
  Mul,
  SubAssign,
  Eq,
  Ord,
  Hash,
)]
pub struct Em(OrderedFloat<f32>);

/// The size of font. `Pixels is an absolute length unit and relative to the
/// view device, and `Em` is relative length unit relative to `Pixel`. We
/// stipulate FontSize::Em(1.) equal to FontSize::Pixel(16.)
#[derive(Debug, Clone, Copy, Add)]
pub enum FontSize {
  Pixel(Pixel),
  Em(Em),
}

// Enum value descriptions are from the CSS spec.
/// A [font family](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#propdef-font-family).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum FontFamily {
  // todo: no need cow? or directly face ids
  /// The name of a font family of choice.
  Name(std::borrow::Cow<'static, str>),

  /// Serif fonts represent the formal text style for a script.
  Serif,

  /// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low
  /// contrast and have stroke endings that are plain — without any flaring,
  /// cross stroke, or other ornamentation.
  SansSerif,

  /// Glyphs in cursive fonts generally use a more informal script style,
  /// and the result looks more like handwritten pen or brush writing than
  /// printed letterwork.
  Cursive,

  /// Fantasy fonts are primarily decorative or expressive fonts that
  /// contain decorative or expressive representations of characters.
  Fantasy,

  /// The sole criterion of a monospace font is that all glyphs have the same
  /// fixed width.
  Monospace,
}

/// Encapsulates the font properties of font face.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FontFace {
  /// A prioritized list of font family names or generic family names.
  ///
  /// [font-family](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#propdef-font-family) in CSS.
  pub families: Box<[FontFamily]>,
  /// Selects a normal, condensed, or expanded face from a font family.
  ///
  /// [font-stretch](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#font-stretch-prop) in CSS.
  pub stretch: FontStretch,
  /// Allows italic or oblique faces to be selected.
  ///
  /// [font-style](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#font-style-prop) in CSS.
  pub style: FontStyle,
  /// Specifies the weight of glyphs in the font, their degree of blackness or
  /// stroke thickness.
  ///
  /// [font-weight](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#font-weight-prop) in CSS.
  pub weight: FontWeight,
}

#[derive(Debug, Clone)]
pub struct Glyph<Unit> {
  /// The font face id of the glyph.
  pub face_id: ID,
  /// How many units the line advances after drawing this glyph when setting
  /// text in horizontal direction.
  pub x_advance: Unit,
  /// How many units the line advances after drawing this glyph when setting
  /// text in vertical direction.
  pub y_advance: Unit,
  /// How many units the glyph moves on the X-axis before drawing it, this
  /// should not affect how many the line advances.
  pub x_offset: Unit,
  /// How many units the glyph moves on the Y-axis before drawing it, this
  /// should not affect how many the line advances.
  pub y_offset: Unit,
  /// The id of the glyph.
  pub glyph_id: GlyphId,
  /// An cluster of origin text as byte index.
  pub cluster: u32,
}

impl Default for FontFace {
  fn default() -> Self {
    Self {
      families: Box::new([FontFamily::Serif]),
      stretch: Default::default(),
      style: Default::default(),
      weight: Default::default(),
    }
  }
}

/// Horizontal Alignment
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum TextAlign {
  Start,
  Center,
  End,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TextDirection {
  /// Text is set horizontally from left to right.
  LeftToRight,
  /// Text is set horizontally from right to left.
  RightToLeft,
  /// Text is set vertically from top to bottom.
  TopToBottom,
  /// Text is set vertically from bottom to top.
  BottomToTop,
}

impl TextDirection {
  #[inline]
  pub fn is_vertical(&self) -> bool {
    matches!(
      self,
      TextDirection::TopToBottom | TextDirection::BottomToTop
    )
  }

  #[inline]
  pub fn is_horizontal(&self) -> bool {
    matches!(
      self,
      TextDirection::LeftToRight | TextDirection::RightToLeft
    )
  }
}

impl From<f32> for Pixel {
  #[inline]
  fn from(v: f32) -> Self { Pixel(v.into()) }
}

impl From<Pixel> for Em {
  #[inline]
  fn from(p: Pixel) -> Self { Em(p.0 / PIXELS_PER_EM) }
}

impl From<Em> for Pixel {
  #[inline]
  fn from(e: Em) -> Self { Pixel(e.0 * PIXELS_PER_EM) }
}

impl PartialEq<Em> for Pixel {
  #[inline]
  fn eq(&self, other: &Em) -> bool {
    let p: Pixel = (*other).into();
    *self == p
  }
}

impl PartialEq<Pixel> for Em {
  #[inline]
  fn eq(&self, other: &Pixel) -> bool {
    let p: Pixel = (*self).into();
    p == *other
  }
}

impl FontSize {
  #[inline]
  pub fn into_pixel(self) -> Pixel {
    match self {
      FontSize::Pixel(p) => p,
      FontSize::Em(e) => e.into(),
    }
  }

  #[inline]
  pub fn into_em(self) -> Em {
    match self {
      FontSize::Pixel(p) => p.into(),
      FontSize::Em(e) => e,
    }
  }

  /// Em scale by font size.
  #[inline]
  pub fn relative_em(self, em: f32) -> Em { self.into_em() * em }
}

impl PartialEq for FontSize {
  #[inline]
  fn eq(&self, other: &Self) -> bool { self.into_pixel() == other.into_pixel() }
}

impl lyon_path::geom::euclid::num::Zero for Em {
  #[inline]
  fn zero() -> Self { Em(f32::zero().into()) }
}

impl lyon_path::geom::euclid::num::Zero for Pixel {
  #[inline]
  fn zero() -> Self { Pixel(f32::zero().into()) }
}

impl Em {
  pub const MAX: Em = Em(OrderedFloat(f32::MAX));

  #[inline]
  pub fn value(self) -> f32 { self.0.into() }

  #[inline]
  pub fn absolute(em: f32) -> Self { Self(em.into()) }

  #[inline]
  pub fn relative_to(em: f32, font_size: FontSize) -> Self { font_size.relative_em(em) }
}

impl Pixel {
  #[inline]
  pub fn value(self) -> f32 { *self.0 }
}

impl std::ops::Mul<Em> for Em {
  type Output = Em;
  #[inline]
  fn mul(self, rhs: Em) -> Self::Output { Em(self.0 * rhs.0) }
}

impl std::ops::Mul<Pixel> for Pixel {
  type Output = Pixel;
  #[inline]
  fn mul(self, rhs: Pixel) -> Self::Output { Pixel(self.0 * rhs.0) }
}

impl std::ops::MulAssign<f32> for Em {
  #[inline]
  fn mul_assign(&mut self, rhs: f32) { self.0 *= rhs; }
}

impl std::ops::Div<Em> for Em {
  type Output = Em;

  #[inline]
  fn div(self, rhs: Em) -> Self::Output { Em(self.0 / rhs.0) }
}

impl std::ops::Div<Pixel> for Pixel {
  type Output = Pixel;

  #[inline]
  fn div(self, rhs: Pixel) -> Self::Output { Pixel(self.0 / rhs.0) }
}

impl<U> Glyph<U> {
  pub fn cast<T>(self) -> Glyph<T>
  where
    U: Into<T>,
  {
    let Glyph {
      face_id,
      x_advance,
      y_advance,
      x_offset,
      y_offset,
      glyph_id,
      cluster,
    } = self;

    Glyph {
      face_id,
      x_advance: x_advance.into(),
      y_advance: y_advance.into(),
      x_offset: x_offset.into(),
      y_offset: y_offset.into(),
      glyph_id,
      cluster,
    }
  }
}