-
-
Notifications
You must be signed in to change notification settings - Fork 935
web: Render device fonts using offscreen canvas #22254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kjarosh
wants to merge
7
commits into
ruffle-rs:master
Choose a base branch
from
kjarosh:canvas-font-renderer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92c34a7
text: Add FontMetrics struct for font metrics
kjarosh 0e59876
text: Add GlyphRef for referencing borrowed data
kjarosh 012a7e9
text: Add support for bitmap glyphs
kjarosh bff76be
text: Add support for external font renderers
kjarosh b2dc3f7
web: Implement CanvasFontRenderer
kjarosh 1bba5ab
web: Add deviceFontRenderer config option
kjarosh 49745c4
web: Use CanvasFontRenderer when enabled
kjarosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| 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, | ||
| 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> { | ||
| let canvas = OffscreenCanvas::new(1024, 1024)?; | ||
|
|
||
| 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:?}")))?; | ||
|
|
||
| ctx.set_fill_style_str("white"); | ||
| ctx.set_font(&Self::to_font_str(italic, bold, Self::SIZE_PX, font_family)); | ||
|
|
||
| let measurement = ctx.measure_text("Myjg")?; | ||
| let ascent = measurement.font_bounding_box_ascent(); | ||
| let descent = measurement.font_bounding_box_descent(); | ||
|
|
||
| Ok(Self { | ||
| canvas, | ||
| ctx, | ||
| ascent, | ||
| descent, | ||
| }) | ||
| } | ||
|
|
||
| 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 { "" }; | ||
| format!("{italic}{bold}{size}px {font_family}") | ||
| } | ||
|
|
||
| fn calculate_width(&self, text: &str) -> Result<f64, JsValue> { | ||
| Ok(self.ctx.measure_text(text)?.width()) | ||
| } | ||
|
|
||
| fn render_glyph_internal(&self, character: char) -> Result<Glyph, JsValue> { | ||
| let text = &character.to_string(); | ||
|
|
||
| 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 width = self.calculate_width(text)?; | ||
| let height = self.ascent + self.descent; | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might missing quotes around the family?