Bug 1646811 - servo: Enable textAlign, textBaseline and direction attributes for canvas.
authorUtsav Oza <utsavoza96@gmail.com>
Thu, 18 Jun 2020 18:12:56 +0000
changeset 536368 086c24ae56898b5c785099e433ce81d59917297e
parent 536367 366e36da6a7a77057a479b8bd20c246d78435d1f
child 536369 82715114ac5bfa857f871162685409cb763c12fc
push id119420
push userealvarez@mozilla.com
push dateThu, 18 Jun 2020 18:18:12 +0000
treeherderautoland@206011a5cc20 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1646811
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1646811 - servo: Enable textAlign, textBaseline and direction attributes for canvas. Depends on D80238 Differential Revision: https://phabricator.services.mozilla.com/D80239
servo/components/style/lib.rs
servo/components/style/values/computed/font.rs
--- a/servo/components/style/lib.rs
+++ b/servo/components/style/lib.rs
@@ -35,16 +35,18 @@ extern crate byteorder;
 #[macro_use]
 extern crate cssparser;
 #[macro_use]
 extern crate debug_unreachable;
 #[macro_use]
 extern crate derive_more;
 extern crate euclid;
 extern crate fallible;
+#[cfg(feature = "servo")]
+extern crate font_kit;
 extern crate fxhash;
 #[cfg(feature = "gecko")]
 #[macro_use]
 pub mod gecko_string_cache;
 extern crate hashglobe;
 #[cfg(feature = "servo")]
 #[macro_use]
 extern crate html5ever;
--- a/servo/components/style/values/computed/font.rs
+++ b/servo/components/style/values/computed/font.rs
@@ -17,16 +17,22 @@ use crate::values::generics::font::{Feat
 use crate::values::generics::{font as generics, NonNegative};
 use crate::values::specified::font::{
     self as specified, KeywordInfo, MAX_FONT_WEIGHT, MIN_FONT_WEIGHT,
 };
 use crate::values::specified::length::{FontBaseSize, NoCalcLength};
 use crate::values::CSSFloat;
 use crate::Atom;
 use cssparser::{serialize_identifier, CssStringWriter, Parser};
+#[cfg(feature = "servo")]
+use font_kit::family_name::FamilyName as FontKitFamilyName;
+#[cfg(feature = "servo")]
+use font_kit::properties::{
+    Stretch as FontKitFontStretch, Style as FontKitFontStyle, Weight as FontKitFontWeight,
+};
 #[cfg(feature = "gecko")]
 use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
 use std::fmt::{self, Write};
 use std::hash::{Hash, Hasher};
 #[cfg(feature = "gecko")]
 use std::mem::{self, ManuallyDrop};
 #[cfg(feature = "servo")]
 use std::slice;
@@ -64,16 +70,23 @@ impl ToAnimatedValue for FontWeight {
     }
 
     #[inline]
     fn from_animated_value(animated: Self::AnimatedValue) -> Self {
         FontWeight(animated.max(MIN_FONT_WEIGHT).min(MAX_FONT_WEIGHT))
     }
 }
 
+#[cfg(feature = "servo")]
+impl From<FontWeight> for FontKitFontWeight {
+    fn from(font_weight: FontWeight) -> Self {
+        FontKitFontWeight(font_weight.0)
+    }
+}
+
 #[derive(
     Animate,
     Clone,
     ComputeSquaredDistance,
     Copy,
     Debug,
     MallocSizeOf,
     PartialEq,
@@ -441,16 +454,36 @@ impl SingleFontFamily {
         SingleFontFamily::FamilyName(FamilyName {
             name,
             syntax: family.mSyntax,
         })
     }
 }
 
 #[cfg(feature = "servo")]
+impl From<&SingleFontFamily> for FontKitFamilyName {
+    fn from(font_family: &SingleFontFamily) -> Self {
+        match font_family {
+            SingleFontFamily::FamilyName(family_name) => {
+                FontKitFamilyName::Title(family_name.to_css_string())
+            },
+            SingleFontFamily::Generic(GenericFontFamily::Serif) => FontKitFamilyName::Serif,
+            SingleFontFamily::Generic(GenericFontFamily::SansSerif) => FontKitFamilyName::SansSerif,
+            SingleFontFamily::Generic(GenericFontFamily::Monospace) => FontKitFamilyName::Monospace,
+            SingleFontFamily::Generic(GenericFontFamily::Fantasy) => FontKitFamilyName::Fantasy,
+            SingleFontFamily::Generic(GenericFontFamily::Cursive) => FontKitFamilyName::Cursive,
+            SingleFontFamily::Generic(family_name) => {
+                warn!("unsupported font family name: {:?}", family_name);
+                FontKitFamilyName::SansSerif
+            },
+        }
+    }
+}
+
+#[cfg(feature = "servo")]
 #[derive(
     Clone,
     Debug,
     Eq,
     Hash,
     MallocSizeOf,
     PartialEq,
     ToComputedValue,
@@ -938,16 +971,27 @@ impl ToCss for FontStyle {
                     angle.to_css(dest)?;
                 }
                 Ok(())
             },
         }
     }
 }
 
+#[cfg(feature = "servo")]
+impl From<FontStyle> for FontKitFontStyle {
+    fn from(font_style: FontStyle) -> Self {
+        match font_style {
+            FontStyle::Normal => FontKitFontStyle::Normal,
+            FontStyle::Italic => FontKitFontStyle::Italic,
+            FontStyle::Oblique(_) => FontKitFontStyle::Oblique,
+        }
+    }
+}
+
 /// A value for the font-stretch property per:
 ///
 /// https://drafts.csswg.org/css-fonts-4/#propdef-font-stretch
 #[derive(
     Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue,
 )]
 #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
 pub struct FontStretch(pub NonNegativePercentage);
@@ -960,16 +1004,23 @@ impl FontStretch {
 
     /// The float value of the percentage
     #[inline]
     pub fn value(&self) -> CSSFloat {
         ((self.0).0).0
     }
 }
 
+#[cfg(feature = "servo")]
+impl From<FontStretch> for FontKitFontStretch {
+    fn from(stretch: FontStretch) -> Self {
+        FontKitFontStretch(stretch.value())
+    }
+}
+
 impl ToAnimatedValue for FontStretch {
     type AnimatedValue = Percentage;
 
     #[inline]
     fn to_animated_value(self) -> Self::AnimatedValue {
         self.0.to_animated_value()
     }