diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2019-09-25 11:38:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-25 11:38:36 -0400 |
commit | cf725fc3f59bc1b94e3b35ab4cfe60b3d41559cc (patch) | |
tree | e6725a85455c6b9cbebbab1074937f34c5f8723a /components | |
parent | cfc05e1d27016f8eeccf1ca0e9b74be844b3535a (diff) | |
parent | 3695fb1eb4182b47b80ed31983a5651c8148aef6 (diff) | |
download | servo-cf725fc3f59bc1b94e3b35ab4cfe60b3d41559cc.tar.gz servo-cf725fc3f59bc1b94e3b35ab4cfe60b3d41559cc.zip |
Auto merge of #24261 - paulrouget:fakeMeasureTExt, r=jdm
Temporary implementation of Canvas.MeasureText
@jdm: that will help with the babylonjs demos. Another approach is to implement that in JS and add it to the demos.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24261)
<!-- Reviewable:end -->
Diffstat (limited to 'components')
-rw-r--r-- | components/script/dom/canvasrenderingcontext2d.rs | 15 | ||||
-rw-r--r-- | components/script/dom/mod.rs | 1 | ||||
-rw-r--r-- | components/script/dom/offscreencanvasrenderingcontext2d.rs | 6 | ||||
-rw-r--r-- | components/script/dom/textmetrics.rs | 158 | ||||
-rw-r--r-- | components/script/dom/webidls/CanvasRenderingContext2D.webidl | 3 | ||||
-rw-r--r-- | components/script/dom/webidls/TextMetrics.webidl | 23 |
6 files changed, 205 insertions, 1 deletions
diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index 2a8f14295c0..1faf9878b53 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -24,6 +24,7 @@ use crate::dom::globalscope::GlobalScope; use crate::dom::htmlcanvaselement::{CanvasContext, HTMLCanvasElement}; use crate::dom::imagedata::ImageData; use crate::dom::node::{Node, NodeDamage}; +use crate::dom::textmetrics::TextMetrics; use crate::unpremultiplytable::UNPREMULTIPLY_TABLE; use canvas_traits::canvas::{Canvas2dMsg, CanvasId, CanvasMsg}; use canvas_traits::canvas::{CompositionOrBlending, FillOrStrokeStyle, FillRule}; @@ -873,6 +874,15 @@ impl CanvasState { self.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width)); } + // https://html.spec.whatwg.org/multipage/#textmetrics + pub fn MeasureText(&self, global: &GlobalScope, _text: DOMString) -> DomRoot<TextMetrics> { + // FIXME: for now faking the implementation of MeasureText(). + // See https://github.com/servo/servo/issues/5411#issuecomment-533776291 + TextMetrics::new( + global, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + ) + } + // https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth pub fn LineWidth(&self) -> f64 { self.state.borrow().line_width @@ -1671,6 +1681,11 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D { self.mark_as_dirty(); } + // https://html.spec.whatwg.org/multipage/#textmetrics + fn MeasureText(&self, text: DOMString) -> DomRoot<TextMetrics> { + self.canvas_state.borrow().MeasureText(&self.global(), text) + } + // https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult { self.canvas_state diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index 34777a20fe0..d698fcc709f 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -484,6 +484,7 @@ pub mod text; pub mod textcontrol; pub mod textdecoder; pub mod textencoder; +pub mod textmetrics; pub mod texttrack; pub mod texttrackcue; pub mod texttrackcuelist; diff --git a/components/script/dom/offscreencanvasrenderingcontext2d.rs b/components/script/dom/offscreencanvasrenderingcontext2d.rs index 461af3b882f..d18cd00df54 100644 --- a/components/script/dom/offscreencanvasrenderingcontext2d.rs +++ b/components/script/dom/offscreencanvasrenderingcontext2d.rs @@ -23,6 +23,7 @@ use crate::dom::globalscope::GlobalScope; use crate::dom::htmlcanvaselement::HTMLCanvasElement; use crate::dom::imagedata::ImageData; use crate::dom::offscreencanvas::OffscreenCanvas; +use crate::dom::textmetrics::TextMetrics; use dom_struct::dom_struct; use euclid::default::Size2D; @@ -244,6 +245,11 @@ impl OffscreenCanvasRenderingContext2DMethods for OffscreenCanvasRenderingContex self.canvas_state.borrow().FillText(text, x, y, max_width) } + // https://html.spec.whatwg.org/multipage/#textmetrics + fn MeasureText(&self, text: DOMString) -> DomRoot<TextMetrics> { + self.canvas_state.borrow().MeasureText(&self.global(), text) + } + // https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth fn LineWidth(&self) -> f64 { self.canvas_state.borrow().LineWidth() diff --git a/components/script/dom/textmetrics.rs b/components/script/dom/textmetrics.rs new file mode 100644 index 00000000000..867071bd0aa --- /dev/null +++ b/components/script/dom/textmetrics.rs @@ -0,0 +1,158 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::codegen::Bindings::TextMetricsBinding; +use crate::dom::bindings::codegen::Bindings::TextMetricsBinding::TextMetricsMethods; +use crate::dom::bindings::num::Finite; +use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; +use crate::dom::bindings::root::DomRoot; +use crate::dom::globalscope::GlobalScope; +use dom_struct::dom_struct; + +#[dom_struct] +pub struct TextMetrics { + reflector_: Reflector, + width: Finite<f64>, + actualBoundingBoxLeft: Finite<f64>, + actualBoundingBoxRight: Finite<f64>, + fontBoundingBoxAscent: Finite<f64>, + fontBoundingBoxDescent: Finite<f64>, + actualBoundingBoxAscent: Finite<f64>, + actualBoundingBoxDescent: Finite<f64>, + emHeightAscent: Finite<f64>, + emHeightDescent: Finite<f64>, + hangingBaseline: Finite<f64>, + alphabeticBaseline: Finite<f64>, + ideographicBaseline: Finite<f64>, +} + +impl TextMetrics { + fn new_inherited( + width: f64, + actualBoundingBoxLeft: f64, + actualBoundingBoxRight: f64, + fontBoundingBoxAscent: f64, + fontBoundingBoxDescent: f64, + actualBoundingBoxAscent: f64, + actualBoundingBoxDescent: f64, + emHeightAscent: f64, + emHeightDescent: f64, + hangingBaseline: f64, + alphabeticBaseline: f64, + ideographicBaseline: f64, + ) -> TextMetrics { + TextMetrics { + reflector_: Reflector::new(), + width: Finite::wrap(width), + actualBoundingBoxLeft: Finite::wrap(actualBoundingBoxLeft), + actualBoundingBoxRight: Finite::wrap(actualBoundingBoxRight), + fontBoundingBoxAscent: Finite::wrap(fontBoundingBoxAscent), + fontBoundingBoxDescent: Finite::wrap(fontBoundingBoxDescent), + actualBoundingBoxAscent: Finite::wrap(actualBoundingBoxAscent), + actualBoundingBoxDescent: Finite::wrap(actualBoundingBoxDescent), + emHeightAscent: Finite::wrap(emHeightAscent), + emHeightDescent: Finite::wrap(emHeightDescent), + hangingBaseline: Finite::wrap(hangingBaseline), + alphabeticBaseline: Finite::wrap(alphabeticBaseline), + ideographicBaseline: Finite::wrap(ideographicBaseline), + } + } + + pub fn new( + global: &GlobalScope, + width: f64, + actualBoundingBoxLeft: f64, + actualBoundingBoxRight: f64, + fontBoundingBoxAscent: f64, + fontBoundingBoxDescent: f64, + actualBoundingBoxAscent: f64, + actualBoundingBoxDescent: f64, + emHeightAscent: f64, + emHeightDescent: f64, + hangingBaseline: f64, + alphabeticBaseline: f64, + ideographicBaseline: f64, + ) -> DomRoot<TextMetrics> { + reflect_dom_object( + Box::new(TextMetrics::new_inherited( + width, + actualBoundingBoxLeft, + actualBoundingBoxRight, + fontBoundingBoxAscent, + fontBoundingBoxDescent, + actualBoundingBoxAscent, + actualBoundingBoxDescent, + emHeightAscent, + emHeightDescent, + hangingBaseline, + alphabeticBaseline, + ideographicBaseline, + )), + global, + TextMetricsBinding::Wrap, + ) + } +} + +impl TextMetricsMethods for TextMetrics { + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-width + fn Width(&self) -> Finite<f64> { + self.width + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxleft + fn ActualBoundingBoxLeft(&self) -> Finite<f64> { + self.actualBoundingBoxLeft + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxright + fn ActualBoundingBoxRight(&self) -> Finite<f64> { + self.actualBoundingBoxRight + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent + fn FontBoundingBoxAscent(&self) -> Finite<f64> { + self.fontBoundingBoxAscent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent + fn FontBoundingBoxDescent(&self) -> Finite<f64> { + self.fontBoundingBoxDescent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxascent + fn ActualBoundingBoxAscent(&self) -> Finite<f64> { + self.actualBoundingBoxAscent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxdescent + fn ActualBoundingBoxDescent(&self) -> Finite<f64> { + self.actualBoundingBoxDescent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightascent + fn EmHeightAscent(&self) -> Finite<f64> { + self.emHeightAscent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightdescent + fn EmHeightDescent(&self) -> Finite<f64> { + self.emHeightDescent + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-hangingbaseline + fn HangingBaseline(&self) -> Finite<f64> { + self.hangingBaseline + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-alphabeticbaseline + fn AlphabeticBaseline(&self) -> Finite<f64> { + self.alphabeticBaseline + } + + /// https://html.spec.whatwg.org/multipage/#dom-textmetrics-ideographicbaseline + fn IdeographicBaseline(&self) -> Finite<f64> { + self.ideographicBaseline + } +} diff --git a/components/script/dom/webidls/CanvasRenderingContext2D.webidl b/components/script/dom/webidls/CanvasRenderingContext2D.webidl index 92e8b80d5ef..2da0372a9ba 100644 --- a/components/script/dom/webidls/CanvasRenderingContext2D.webidl +++ b/components/script/dom/webidls/CanvasRenderingContext2D.webidl @@ -153,7 +153,8 @@ interface CanvasText { optional unrestricted double maxWidth); //void strokeText(DOMString text, unrestricted double x, unrestricted double y, // optional unrestricted double maxWidth); - //TextMetrics measureText(DOMString text); + [Pref="dom.canvas-text.enabled"] + TextMetrics measureText(DOMString text); }; [Exposed=(PaintWorklet, Window, Worker), NoInterfaceObject] diff --git a/components/script/dom/webidls/TextMetrics.webidl b/components/script/dom/webidls/TextMetrics.webidl new file mode 100644 index 00000000000..f3d90450824 --- /dev/null +++ b/components/script/dom/webidls/TextMetrics.webidl @@ -0,0 +1,23 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +// https://html.spec.whatwg.org/multipage/#textmetrics +[Exposed=(PaintWorklet, Window, Worker), Pref="dom.canvas-text.enabled"] +interface TextMetrics { + // x-direction + readonly attribute double width; // advance width + readonly attribute double actualBoundingBoxLeft; + readonly attribute double actualBoundingBoxRight; + + // y-direction + readonly attribute double fontBoundingBoxAscent; + readonly attribute double fontBoundingBoxDescent; + readonly attribute double actualBoundingBoxAscent; + readonly attribute double actualBoundingBoxDescent; + readonly attribute double emHeightAscent; + readonly attribute double emHeightDescent; + readonly attribute double hangingBaseline; + readonly attribute double alphabeticBaseline; + readonly attribute double ideographicBaseline; +}; |