-
Notifications
You must be signed in to change notification settings - Fork 84
/
font.rs
460 lines (411 loc) · 14.1 KB
/
font.rs
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! A basic font manager with fallback support
//!
//! # Example
//!
//! ```rust
//! use image::{RgbImage, Rgb};
//! use silicon::font::{FontCollection, FontStyle};
//!
//! let mut image = RgbImage::new(250, 100);
//! let font = FontCollection::new(&[("Hack", 27.0), ("FiraCode", 27.0)]).unwrap();
//!
//! font.draw_text_mut(&mut image, Rgb([255, 0, 0]), 0, 0, FontStyle::REGULAR, "Hello, world");
//! ```
use crate::error::FontError;
#[cfg(feature = "harfbuzz")]
use crate::hb_wrapper::{feature_from_tag, HBBuffer, HBFont};
use anyhow::Result;
use conv::ValueInto;
use font_kit::canvas::{Canvas, Format, RasterizationOptions};
use font_kit::font::Font;
use font_kit::hinting::HintingOptions;
use font_kit::properties::{Properties, Style, Weight};
use font_kit::source::SystemSource;
use image::{GenericImage, Pixel, Rgba, RgbaImage};
use imageproc::definitions::Clamp;
use imageproc::pixelops::weighted_sum;
use pathfinder_geometry::transform2d::Transform2F;
use std::collections::HashMap;
use std::sync::Arc;
use syntect::highlighting;
/// a single line text drawer
pub trait TextLineDrawer {
/// get the height of the text
fn height(&mut self, text: &str) -> u32;
/// get the width of the text
fn width(&mut self, text: &str) -> u32;
/// draw the text
fn draw_text(
&mut self,
image: &mut RgbaImage,
color: Rgba<u8>,
x: u32,
y: u32,
font_style: FontStyle,
text: &str,
);
}
impl TextLineDrawer for FontCollection {
fn height(&mut self, _text: &str) -> u32 {
self.get_font_height()
}
fn width(&mut self, text: &str) -> u32 {
self.layout(text, REGULAR).1
}
fn draw_text(
&mut self,
image: &mut RgbaImage,
color: Rgba<u8>,
x: u32,
y: u32,
font_style: FontStyle,
text: &str,
) {
self.draw_text_mut(image, color, x, y, font_style, text);
}
}
/// Font style
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum FontStyle {
REGULAR,
ITALIC,
BOLD,
BOLDITALIC,
}
impl From<highlighting::FontStyle> for FontStyle {
fn from(style: highlighting::FontStyle) -> Self {
if style.contains(highlighting::FontStyle::BOLD) {
if style.contains(highlighting::FontStyle::ITALIC) {
BOLDITALIC
} else {
BOLD
}
} else if style.contains(highlighting::FontStyle::ITALIC) {
ITALIC
} else {
REGULAR
}
}
}
use pathfinder_geometry::rect::RectI;
use pathfinder_geometry::vector::Vector2I;
use FontStyle::*;
/// A single font with specific size
#[derive(Debug)]
pub struct ImageFont {
pub fonts: HashMap<FontStyle, Font>,
pub size: f32,
}
impl Default for ImageFont {
/// It will use Hack font (size: 26.0) by default
fn default() -> Self {
let l = vec![
(
REGULAR,
include_bytes!("../assets/fonts/Hack-Regular.ttf").to_vec(),
),
(
ITALIC,
include_bytes!("../assets/fonts/Hack-Italic.ttf").to_vec(),
),
(
BOLD,
include_bytes!("../assets/fonts/Hack-Bold.ttf").to_vec(),
),
(
BOLDITALIC,
include_bytes!("../assets/fonts/Hack-BoldItalic.ttf").to_vec(),
),
];
let mut fonts = HashMap::new();
for (style, bytes) in l {
let font = Font::from_bytes(Arc::new(bytes), 0).unwrap();
fonts.insert(style, font);
}
Self { fonts, size: 26.0 }
}
}
impl ImageFont {
pub fn new(name: &str, size: f32) -> Result<Self, FontError> {
// Silicon already contains Hack font
if name == "Hack" {
let font = ImageFont {
size,
..Default::default()
};
return Ok(font);
}
let mut fonts = HashMap::new();
let family = SystemSource::new().select_family_by_name(name)?;
let handles = family.fonts();
debug!("{:?}", handles);
for handle in handles {
let font = handle.load()?;
let properties: Properties = font.properties();
debug!("{:?} - {:?}", font, properties);
// cannot use match because `Weight` didn't derive `Eq`
match properties.style {
Style::Normal => {
if properties.weight == Weight::NORMAL {
fonts.insert(REGULAR, font);
} else if properties.weight == Weight::BOLD {
fonts.insert(BOLD, font);
} else if properties.weight == Weight::MEDIUM && !fonts.contains_key(®ULAR) {
fonts.insert(REGULAR, font);
}
}
Style::Italic => {
if properties.weight == Weight::NORMAL {
fonts.insert(ITALIC, font);
} else if properties.weight == Weight::BOLD {
fonts.insert(BOLDITALIC, font);
} else if properties.weight == Weight::MEDIUM && !fonts.contains_key(&ITALIC) {
fonts.insert(ITALIC, font);
}
}
_ => (),
}
}
Ok(Self { fonts, size })
}
/// Get a font by style. If there is no such a font, it will return the REGULAR font.
pub fn get_by_style(&self, style: FontStyle) -> &Font {
self.fonts
.get(&style)
.unwrap_or_else(|| self.fonts.get(®ULAR).unwrap())
}
/// Get the regular font
pub fn get_regular(&self) -> &Font {
self.fonts.get(®ULAR).unwrap()
}
/// Get the height of the font
pub fn get_font_height(&self) -> u32 {
let font = self.get_regular();
let metrics = font.metrics();
((metrics.ascent - metrics.descent) / metrics.units_per_em as f32 * self.size).ceil() as u32
}
}
/// A collection of font
///
/// It can be used to draw text on the image.
#[derive(Debug)]
pub struct FontCollection {
fonts: Vec<ImageFont>,
}
impl Default for FontCollection {
fn default() -> Self {
Self {
fonts: vec![ImageFont::default()],
}
}
}
impl FontCollection {
/// Create a FontCollection with several fonts.
pub fn new<S: AsRef<str>>(font_list: &[(S, f32)]) -> Result<Self, FontError> {
let mut fonts = vec![];
for (name, size) in font_list {
let name = name.as_ref();
match ImageFont::new(name, *size) {
Ok(font) => fonts.push(font),
Err(err) => eprintln!("[error] Error occurs when load font `{}`: {}", name, err),
}
}
Ok(Self { fonts })
}
fn glyph_for_char(&self, c: char, style: FontStyle) -> Option<(u32, &ImageFont, &Font)> {
for font in &self.fonts {
let result = font.get_by_style(style);
if let Some(id) = result.glyph_for_char(c) {
return Some((id, font, result));
}
}
eprintln!("[warning] No font found for character `{}`", c);
None
}
/// get max height of all the fonts
pub fn get_font_height(&self) -> u32 {
self.fonts
.iter()
.map(|font| font.get_font_height())
.max()
.unwrap()
}
#[cfg(feature = "harfbuzz")]
fn shape_text(&self, font: &mut HBFont, text: &str) -> Result<Vec<u32>> {
// feature tags
let features = vec![
feature_from_tag("kern")?,
feature_from_tag("clig")?,
feature_from_tag("liga")?,
];
let mut buf = HBBuffer::new()?;
buf.add_str(text);
buf.guess_segments_properties();
font.shape(&buf, features.as_slice());
let hb_infos = buf.get_glyph_infos();
let mut glyph_ids = Vec::new();
for info in hb_infos.iter() {
glyph_ids.push(info.codepoint);
}
Ok(glyph_ids)
}
#[cfg(feature = "harfbuzz")]
fn split_by_font(&self, text: &str, style: FontStyle) -> Vec<(&ImageFont, &Font, String)> {
let mut result: Vec<(&ImageFont, &Font, String)> = vec![];
for c in text.chars() {
if let Some((_, imfont, font)) = self.glyph_for_char(c, style) {
if result.is_empty() || !std::ptr::eq(result.last().unwrap().0, imfont) {
result.push((imfont, font, String::new()));
}
if std::ptr::eq(result.last().unwrap().0, imfont) {
result.last_mut().unwrap().2.push(c);
}
}
}
log::trace!("{:#?}", &result);
result
}
#[cfg(feature = "harfbuzz")]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0;
let height = self.get_font_height();
let mut glyphs = Vec::with_capacity(text.len());
for (imfont, font, text) in self.split_by_font(text, style) {
let mut hb_font = HBFont::new(font);
// apply font features especially ligature with a shape engine
let shaped_glyphs = self.shape_text(&mut hb_font, &text).unwrap();
glyphs.extend(shaped_glyphs.iter().map(|id| {
let raster_rect = font
.raster_bounds(
*id,
imfont.size,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
let position = Vector2I::new(delta_x as i32, height as i32) + raster_rect.origin();
delta_x += Self::get_glyph_width(font, *id, imfont.size);
PositionedGlyph {
id: *id,
font: font.clone(),
size: imfont.size,
raster_rect,
position,
}
}))
}
(glyphs, delta_x)
}
#[cfg(not(feature = "harfbuzz"))]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0;
let height = self.get_font_height();
let glyphs = text
.chars()
.filter_map(|c| {
self.glyph_for_char(c, style).map(|(id, imfont, font)| {
let raster_rect = font
.raster_bounds(
id,
imfont.size,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
let position =
Vector2I::new(delta_x as i32, height as i32) + raster_rect.origin();
delta_x += Self::get_glyph_width(font, id, imfont.size);
PositionedGlyph {
id,
font: font.clone(),
size: imfont.size,
raster_rect,
position,
}
})
})
.collect();
(glyphs, delta_x)
}
/// Get the width of the given glyph
fn get_glyph_width(font: &Font, id: u32, size: f32) -> u32 {
let metrics = font.metrics();
let advance = font.advance(id).unwrap();
(advance / metrics.units_per_em as f32 * size).x().ceil() as u32
}
/// Get the width of the given text
pub fn get_text_len(&self, text: &str) -> u32 {
self.layout(text, REGULAR).1
}
/// Draw the text to a image
/// return the width of written text
pub fn draw_text_mut<I>(
&self,
image: &mut I,
color: I::Pixel,
x: u32,
y: u32,
style: FontStyle,
text: &str,
) -> u32
where
I: GenericImage,
<I::Pixel as Pixel>::Subpixel: ValueInto<f32> + Clamp<f32>,
{
let metrics = self.fonts[0].get_regular().metrics();
let offset =
(metrics.descent / metrics.units_per_em as f32 * self.fonts[0].size).round() as i32;
let (glyphs, width) = self.layout(text, style);
for glyph in glyphs {
glyph.draw(offset, |px, py, v| {
if v <= std::f32::EPSILON {
return;
}
let (x, y) = ((px + x as i32) as u32, (py + y as i32) as u32);
let pixel = image.get_pixel(x, y);
let weighted_color = weighted_sum(pixel, color, 1.0 - v, v);
image.put_pixel(x, y, weighted_color);
})
}
width
}
}
#[derive(Debug)]
struct PositionedGlyph {
id: u32,
font: Font,
size: f32,
position: Vector2I,
raster_rect: RectI,
}
impl PositionedGlyph {
fn draw<O: FnMut(i32, i32, f32)>(&self, offset: i32, mut o: O) {
let mut canvas = Canvas::new(self.raster_rect.size(), Format::A8);
// don't rasterize whitespace(https://github.com/pcwalton/font-kit/issues/7)
if canvas.size != Vector2I::new(0, 0) {
self.font
.rasterize_glyph(
&mut canvas,
self.id,
self.size,
Transform2F::from_translation(-self.raster_rect.origin().to_f32()),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
}
for y in (0..self.raster_rect.height()).rev() {
let (row_start, row_end) =
(y as usize * canvas.stride, (y + 1) as usize * canvas.stride);
let row = &canvas.pixels[row_start..row_end];
for x in 0..self.raster_rect.width() {
let val = f32::from(row[x as usize]) / 255.0;
let px = self.position.x() + x;
let py = self.position.y() + y + offset;
o(px, py, val);
}
}
}
}