aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUtsav Oza <utsavoza96@gmail.com>2020-06-04 01:32:25 +0530
committerUtsav Oza <utsavoza96@gmail.com>2020-06-10 22:11:24 +0530
commitc21fde375184c367f923b9e3776ba3adbe7f53dd (patch)
treeb23c310ff6abd3fdfac9dd09e0432e0574880ce4
parent15fd256302cc4401e0c4e2d154d473bfaa16223d (diff)
downloadservo-c21fde375184c367f923b9e3776ba3adbe7f53dd.tar.gz
servo-c21fde375184c367f923b9e3776ba3adbe7f53dd.zip
Implement CanvasRenderingContext2D.font property
-rw-r--r--Cargo.lock2
-rw-r--r--components/canvas/Cargo.toml1
-rw-r--r--components/canvas/canvas_data.rs6
-rw-r--r--components/canvas/canvas_paint_thread.rs1
-rw-r--r--components/canvas/raqote_backend.rs1
-rw-r--r--components/canvas_traits/Cargo.toml1
-rw-r--r--components/canvas_traits/canvas.rs2
-rw-r--r--components/script/canvas_state.rs38
-rw-r--r--components/style/properties/properties.mako.rs2
-rw-r--r--components/style/values/computed/font.rs16
-rw-r--r--components/style/values/specified/font.rs4
11 files changed, 68 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bb0d5ddf870..d080e4a7c39 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -515,6 +515,7 @@ dependencies = [
"raqote",
"servo_config",
"sparkle",
+ "style",
"surfman",
"surfman-chains",
"surfman-chains-api",
@@ -542,6 +543,7 @@ dependencies = [
"serde_bytes",
"servo_config",
"sparkle",
+ "style",
"time",
"webrender_api",
"webxr-api",
diff --git a/components/canvas/Cargo.toml b/components/canvas/Cargo.toml
index 918e2971f9a..3d7fa7f38e0 100644
--- a/components/canvas/Cargo.toml
+++ b/components/canvas/Cargo.toml
@@ -34,6 +34,7 @@ pixels = { path = "../pixels" }
raqote = { version = "0.8", features = ["text"] }
servo_config = { path = "../config" }
sparkle = "0.1.24"
+style = { path = "../style" }
# NOTE: the sm-angle feature only enables ANGLE on Windows, not other platforms!
surfman = { version = "0.2", features = ["sm-angle", "sm-angle-default"] }
surfman-chains = "0.3"
diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs
index 6ff87eca291..c90fca964f7 100644
--- a/components/canvas/canvas_data.rs
+++ b/components/canvas/canvas_data.rs
@@ -13,6 +13,7 @@ use num_traits::ToPrimitive;
use std::marker::PhantomData;
use std::mem;
use std::sync::Arc;
+use style::properties::style_structs::Font as FontStyleStruct;
use webrender_api::units::RectExt as RectExt_;
/// The canvas data stores a state machine for the current status of
@@ -1067,6 +1068,10 @@ impl<'a> CanvasData<'a> {
self.backend.set_shadow_color(value, &mut self.state);
}
+ pub fn set_font(&mut self, font_style: FontStyleStruct) {
+ self.state.font_style = Some(font_style)
+ }
+
// https://html.spec.whatwg.org/multipage/#when-shadows-are-drawn
fn need_to_draw_shadow(&self) -> bool {
self.backend.need_to_draw_shadow(&self.state.shadow_color) &&
@@ -1158,6 +1163,7 @@ pub struct CanvasPaintState<'a> {
pub shadow_offset_y: f64,
pub shadow_blur: f64,
pub shadow_color: Color,
+ pub font_style: Option<FontStyleStruct>,
}
/// It writes an image to the destination target
diff --git a/components/canvas/canvas_paint_thread.rs b/components/canvas/canvas_paint_thread.rs
index 6832a5cddc8..9d9f8350e1d 100644
--- a/components/canvas/canvas_paint_thread.rs
+++ b/components/canvas/canvas_paint_thread.rs
@@ -247,6 +247,7 @@ impl<'a> CanvasPaintThread<'a> {
},
Canvas2dMsg::SetShadowBlur(value) => self.canvas(canvas_id).set_shadow_blur(value),
Canvas2dMsg::SetShadowColor(color) => self.canvas(canvas_id).set_shadow_color(color),
+ Canvas2dMsg::SetFont(font_style) => self.canvas(canvas_id).set_font(font_style),
}
}
diff --git a/components/canvas/raqote_backend.rs b/components/canvas/raqote_backend.rs
index 451656d7708..3c30c0f1a19 100644
--- a/components/canvas/raqote_backend.rs
+++ b/components/canvas/raqote_backend.rs
@@ -90,6 +90,7 @@ impl<'a> CanvasPaintState<'a> {
shadow_offset_y: 0.0,
shadow_blur: 0.0,
shadow_color: Color::Raqote(raqote::SolidSource::from_unpremultiplied_argb(0, 0, 0, 0)),
+ font_style: None,
}
}
}
diff --git a/components/canvas_traits/Cargo.toml b/components/canvas_traits/Cargo.toml
index 0625221a484..8414349939c 100644
--- a/components/canvas_traits/Cargo.toml
+++ b/components/canvas_traits/Cargo.toml
@@ -27,6 +27,7 @@ serde = "1.0"
serde_bytes = "0.11"
servo_config = { path = "../config" }
sparkle = "0.1"
+style = { path = "../style" }
time = { version = "0.1.0", optional = true }
webrender_api = { git = "https://github.com/servo/webrender" }
webxr-api = { git = "https://github.com/servo/webxr", features = ["ipc"] }
diff --git a/components/canvas_traits/canvas.rs b/components/canvas_traits/canvas.rs
index 13fc6eaa800..1750e7d8e99 100644
--- a/components/canvas_traits/canvas.rs
+++ b/components/canvas_traits/canvas.rs
@@ -8,6 +8,7 @@ use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSender, IpcSharedMem
use serde_bytes::ByteBuf;
use std::default::Default;
use std::str::FromStr;
+use style::properties::style_structs::Font;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum FillRule {
@@ -70,6 +71,7 @@ pub enum Canvas2dMsg {
SetShadowOffsetY(f64),
SetShadowBlur(f64),
SetShadowColor(RGBA),
+ SetFont(Font),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
diff --git a/components/script/canvas_state.rs b/components/script/canvas_state.rs
index dc40ad3203d..9685d719157 100644
--- a/components/script/canvas_state.rs
+++ b/components/script/canvas_state.rs
@@ -49,7 +49,10 @@ use std::cell::Cell;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
+use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
use style::properties::style_structs::Font;
+use style::values::computed::font::FontStyle;
+use style_traits::values::ToCss;
#[unrooted_must_root_lint::must_root]
#[derive(Clone, JSTraceable, MallocSizeOf)]
@@ -1026,16 +1029,24 @@ impl CanvasState {
};
let node = canvas.upcast::<Node>();
let window = window_from_node(&*canvas);
- let font_style = match window.resolved_font_style_query(&node, value.to_string()) {
+ let resolved_font_style = match window.resolved_font_style_query(&node, value.to_string()) {
Some(value) => value,
None => return, // syntax error
};
- self.state.borrow_mut().font_style = Some((*font_style).clone());
+ self.state.borrow_mut().font_style = Some((*resolved_font_style).clone());
+ self.send_canvas_2d_msg(Canvas2dMsg::SetFont((*resolved_font_style).clone()));
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-font
pub fn font(&self) -> DOMString {
- unimplemented!()
+ self.state.borrow().font_style.as_ref().map_or_else(
+ || DOMString::from("10px sans-serif"),
+ |style| {
+ let mut result = String::new();
+ serialize_font(style, &mut result).unwrap();
+ DOMString::from(result)
+ },
+ )
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
@@ -1663,3 +1674,24 @@ pub fn adjust_size_sign(
}
(origin, size.to_u32())
}
+
+fn serialize_font<W>(style: &Font, dest: &mut W) -> fmt::Result
+where
+ W: fmt::Write,
+{
+ if style.font_style == FontStyle::Italic {
+ write!(dest, "{} ", style.font_style.to_css_string())?;
+ }
+ if style.font_weight.is_bold() {
+ write!(dest, "{} ", style.font_weight.to_css_string())?;
+ }
+ if style.font_variant_caps == FontVariantCaps::SmallCaps {
+ write!(dest, "{} ", style.font_variant_caps.to_css_string())?;
+ }
+ write!(
+ dest,
+ "{} {}",
+ style.font_size.to_css_string(),
+ style.font_family.to_css_string()
+ )
+}
diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs
index a098534793f..7bf6813994b 100644
--- a/components/style/properties/properties.mako.rs
+++ b/components/style/properties/properties.mako.rs
@@ -2601,7 +2601,7 @@ pub mod style_structs {
% for style_struct in data.active_style_structs():
% if style_struct.name == "Font":
- #[derive(Clone, Debug, MallocSizeOf)]
+ #[derive(Clone, Debug, MallocSizeOf, Serialize, Deserialize)]
% else:
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
% endif
diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs
index ed00f42d678..cc1b7f67d79 100644
--- a/components/style/values/computed/font.rs
+++ b/components/style/values/computed/font.rs
@@ -80,6 +80,8 @@ impl ToAnimatedValue for FontWeight {
ToAnimatedZero,
ToCss,
ToResolvedValue,
+ Serialize,
+ Deserialize,
)]
/// The computed value of font-size
pub struct FontSize {
@@ -179,7 +181,7 @@ impl ToAnimatedValue for FontSize {
}
#[derive(Clone, Debug, Eq, PartialEq, ToComputedValue, ToResolvedValue)]
-#[cfg_attr(feature = "servo", derive(Hash, MallocSizeOf))]
+#[cfg_attr(feature = "servo", derive(Hash, MallocSizeOf, Serialize, Deserialize))]
/// Specifies a prioritized list of font family names or generic family names.
pub struct FontFamily {
/// The actual list of family names.
@@ -445,7 +447,17 @@ impl SingleFontFamily {
#[cfg(feature = "servo")]
#[derive(
- Clone, Debug, Eq, Hash, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem,
+ Clone,
+ Debug,
+ Eq,
+ Hash,
+ MallocSizeOf,
+ PartialEq,
+ ToComputedValue,
+ ToResolvedValue,
+ ToShmem,
+ Serialize,
+ Deserialize,
)]
/// A list of SingleFontFamily
pub struct FontFamilyList(Box<[SingleFontFamily]>);
diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs
index dd9f9d3b86d..9d70648ce71 100644
--- a/components/style/values/specified/font.rs
+++ b/components/style/values/specified/font.rs
@@ -496,6 +496,8 @@ impl ToComputedValue for FontStretch {
ToCss,
ToResolvedValue,
ToShmem,
+ Serialize,
+ Deserialize,
)]
#[allow(missing_docs)]
pub enum KeywordSize {
@@ -540,6 +542,8 @@ impl Default for KeywordSize {
ToCss,
ToResolvedValue,
ToShmem,
+ Serialize,
+ Deserialize,
)]
/// Additional information for keyword-derived font sizes.
pub struct KeywordInfo {