Implement CanvasRenderingContext2D.font property

This commit is contained in:
Utsav Oza 2020-06-04 01:32:25 +05:30
parent 15fd256302
commit c21fde3751
11 changed files with 68 additions and 6 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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

View file

@ -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),
}
}

View file

@ -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,
}
}
}

View file

@ -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"] }

View file

@ -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)]

View file

@ -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()
)
}

View file

@ -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

View file

@ -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]>);

View file

@ -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 {