-
-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathfont_renderer.rs
More file actions
162 lines (135 loc) · 5.55 KB
/
font_renderer.rs
File metadata and controls
162 lines (135 loc) · 5.55 KB
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
use js_sys::JSON;
use ruffle_core::font::FontMetrics;
use ruffle_core::font::FontRenderer;
use ruffle_core::font::Glyph;
use ruffle_core::swf::Twips;
use ruffle_render::bitmap::Bitmap;
use ruffle_render::bitmap::BitmapFormat;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use web_sys::OffscreenCanvas;
use web_sys::OffscreenCanvasRenderingContext2d;
#[derive(Debug)]
pub struct CanvasFontRenderer {
canvas: OffscreenCanvas,
ctx: OffscreenCanvasRenderingContext2d,
font_str: String,
ascent: f64,
descent: f64,
}
impl CanvasFontRenderer {
/// Render fonts with size 64px. It affects the bitmap size.
const SIZE_PX: f64 = 64.0;
/// Divide each pixel into 20 (use twips precision). It affects metrics.
const SCALE: f64 = 20.0;
pub fn new(italic: bool, bold: bool, font_family: &str) -> Result<Self, JsValue> {
if !Self::is_offscreen_canvas_supported() {
return Err(JsValue::from_str("OffscreenCanvas unsupported"));
}
let canvas = OffscreenCanvas::new(1, 1)?;
let ctx = canvas.get_context("2d")?.expect("2d context");
let ctx = ctx
.dyn_into::<OffscreenCanvasRenderingContext2d>()
.map_err(|err| JsValue::from_str(&format!("Not a 2d context: {err:?}")))?;
let font_str = Self::to_font_str(italic, bold, Self::SIZE_PX, font_family);
tracing::debug!("Using the following font string: {font_str}");
Self::apply_style(&ctx, &font_str);
let measurement = ctx.measure_text("Myjg")?;
let ascent = measurement.font_bounding_box_ascent();
let descent = measurement.font_bounding_box_descent();
Ok(Self {
canvas,
ctx,
font_str,
ascent,
descent,
})
}
// TODO Remove it when we stop supporting Firefox <105, Safari <16.4
fn is_offscreen_canvas_supported() -> bool {
let global = js_sys::global();
match js_sys::Reflect::get(&global, &JsValue::from_str("OffscreenCanvas")) {
Ok(value) => !value.is_undefined(),
Err(_) => false,
}
}
fn to_font_str(italic: bool, bold: bool, size: f64, font_family: &str) -> String {
let italic = if italic { "italic " } else { "" };
let bold = if bold { "bold " } else { "" };
// Escape font family properly
let font_family = JSON::stringify(&JsValue::from_str(font_family))
.ok()
.and_then(|js_str| js_str.as_string())
.unwrap_or_else(|| format!("\"{font_family}\""));
format!("{italic}{bold}{size}px {font_family}")
}
fn apply_style(ctx: &OffscreenCanvasRenderingContext2d, font_str: &str) {
ctx.set_fill_style_str("white");
ctx.set_font(font_str);
}
fn calculate_width(&self, text: &str) -> Result<f64, JsValue> {
Ok(self.ctx.measure_text(text)?.width())
}
fn ensure_canvas_large_enough(&self, width: f64, height: f64) {
let width = width.ceil() as u32;
let height = height.ceil() as u32;
if self.canvas.width() < width || self.canvas.height() < height {
self.canvas.set_width(width);
self.canvas.set_height(height);
// After changing canvas size, we need to reapply the style.
// Somehow, when canvas size is too small for the text, the
// text is rendered smaller, but its reported metrics are correct.
Self::apply_style(&self.ctx, &self.font_str);
}
}
fn render_glyph_internal(&self, character: char) -> Result<Glyph, JsValue> {
let text = &character.to_string();
let width = self.calculate_width(text)?;
let height = self.ascent + self.descent;
self.ensure_canvas_large_enough(width, height);
self.ctx.clear_rect(
0.0,
0.0,
self.canvas.width() as f64,
self.canvas.height() as f64,
);
self.ctx.fill_text(text, 0.0, self.ascent)?;
let image_data = self.ctx.get_image_data(0.0, 0.0, width, height)?;
let width = image_data.width();
let height = image_data.height();
let pixels = image_data.data().0;
let bitmap = Bitmap::new(width, height, BitmapFormat::Rgba, pixels);
let advance = Twips::from_pixels(width as f64);
Ok(Glyph::from_bitmap(character, bitmap, advance))
}
fn calculate_kerning_internal(&self, left: char, right: char) -> Result<Twips, JsValue> {
let left_width = self.calculate_width(&left.to_string())?;
let right_width = self.calculate_width(&right.to_string())?;
let both_width = self.calculate_width(&format!("{left}{right}"))?;
let kern = both_width - left_width - right_width;
Ok(Twips::from_pixels(kern))
}
}
impl FontRenderer for CanvasFontRenderer {
fn get_font_metrics(&self) -> FontMetrics {
FontMetrics {
scale: (Self::SIZE_PX * Self::SCALE) as f32,
ascent: (self.ascent * Self::SCALE) as i32,
descent: (self.descent * Self::SCALE) as i32,
leading: 0,
}
}
fn has_kerning_info(&self) -> bool {
true
}
fn render_glyph(&self, character: char) -> Option<Glyph> {
self.render_glyph_internal(character)
.map_err(|err| tracing::error!("Failed to render a glyph: {err:?}"))
.ok()
}
fn calculate_kerning(&self, left: char, right: char) -> Twips {
self.calculate_kerning_internal(left, right)
.map_err(|err| tracing::error!("Failed to calculate kerning: {err:?}"))
.unwrap_or(Twips::ZERO)
}
}