Bug 1403198 - support WR font descriptors on Mac. r=jrmuizel
authorLee Salzman <lsalzman@mozilla.com>
Mon, 06 Nov 2017 20:21:06 -0500
changeset 390419 8e450204ab2ea252f370020a936c92cc9a90dc7f
parent 390418 d619753f890bc744c68bf7948d2e87930bdd9ecc
child 390420 5a38089c03751d5b123b41645167d5858c3a6a5f
push id97040
push userlsalzman@mozilla.com
push dateTue, 07 Nov 2017 01:21:59 +0000
treeherdermozilla-inbound@5a38089c0375 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel
bugs1403198
milestone58.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 1403198 - support WR font descriptors on Mac. r=jrmuizel MozReview-Commit-ID: BqN51yY88oJ
gfx/2d/NativeFontResourceMac.cpp
gfx/2d/ScaledFontMac.cpp
gfx/2d/UnscaledFontMac.h
gfx/thebes/gfxMacPlatformFontList.mm
gfx/webrender_bindings/Cargo.toml
gfx/webrender_bindings/src/bindings.rs
gfx/webrender_bindings/src/lib.rs
toolkit/library/gtest/rust/Cargo.lock
toolkit/library/rust/Cargo.lock
--- a/gfx/2d/NativeFontResourceMac.cpp
+++ b/gfx/2d/NativeFontResourceMac.cpp
@@ -52,15 +52,15 @@ NativeFontResourceMac::Create(uint8_t *a
   return fontResource.forget();
 }
 
 already_AddRefed<UnscaledFont>
 NativeFontResourceMac::CreateUnscaledFont(uint32_t aIndex,
                                           const uint8_t* aInstanceData,
                                           uint32_t aInstanceDataLength)
 {
-  RefPtr<UnscaledFont> unscaledFont = new UnscaledFontMac(mFontRef);
+  RefPtr<UnscaledFont> unscaledFont = new UnscaledFontMac(mFontRef, true);
 
   return unscaledFont.forget();
 }
 
 } // gfx
 } // mozilla
--- a/gfx/2d/ScaledFontMac.cpp
+++ b/gfx/2d/ScaledFontMac.cpp
@@ -320,16 +320,41 @@ UnscaledFontMac::GetFontFileData(FontFil
     memcpy(&buf.data[checkSumAdjustmentOffset], &fontChecksum, sizeof(fontChecksum));
 
     // we always use an index of 0
     aDataCallback(buf.data, buf.offset, 0, aBaton);
 
     return true;
 }
 
+bool
+UnscaledFontMac::GetWRFontDescriptor(WRFontDescriptorOutput aCb, void* aBaton)
+{
+  if (mIsDataFont) {
+    return false;
+  }
+
+  CFStringRef psname = CGFontCopyPostScriptName(mFont);
+  if (!psname) {
+    return false;
+  }
+
+  char buf[256];
+  const char* cstr = CFStringGetCStringPtr(psname, kCFStringEncodingUTF8);
+  if (!cstr) {
+    if (!CFStringGetCString(psname, buf, sizeof(buf), kCFStringEncodingUTF8)) {
+      return false;
+    }
+    cstr = buf;
+  }
+
+  aCb(reinterpret_cast<const uint8_t*>(cstr), strlen(cstr), 0, aBaton);
+  return true;
+}
+
 static void
 CollectVariationsFromDictionary(const void* aKey, const void* aValue, void* aContext)
 {
   auto keyPtr = static_cast<const CFTypeRef>(aKey);
   auto valuePtr = static_cast<const CFTypeRef>(aValue);
   auto outVariations = static_cast<std::vector<FontVariation>*>(aContext);
   if (CFGetTypeID(keyPtr) == CFNumberGetTypeID() &&
       CFGetTypeID(valuePtr) == CFNumberGetTypeID()) {
--- a/gfx/2d/UnscaledFontMac.h
+++ b/gfx/2d/UnscaledFontMac.h
@@ -18,18 +18,19 @@
 
 namespace mozilla {
 namespace gfx {
 
 class UnscaledFontMac final : public UnscaledFont
 {
 public:
   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(UnscaledFontMac, override)
-  explicit UnscaledFontMac(CGFontRef aFont)
+  explicit UnscaledFontMac(CGFontRef aFont, bool aIsDataFont = false)
     : mFont(aFont)
+    , mIsDataFont(aIsDataFont)
   {
     CFRetain(mFont);
   }
   ~UnscaledFontMac()
   {
     CFRelease(mFont);
   }
 
@@ -46,17 +47,20 @@ public:
                      const FontVariation* aVariations,
                      uint32_t aNumVariations) override;
 
   static CGFontRef
     CreateCGFontWithVariations(CGFontRef aFont,
                                uint32_t aVariationCount,
                                const FontVariation* aVariations);
 
+  bool GetWRFontDescriptor(WRFontDescriptorOutput aCb, void* aBaton) override;
+
 private:
   CGFontRef mFont;
+  bool mIsDataFont;
 };
 
 } // namespace gfx
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_UNSCALEDFONTMAC_H_ */
 
--- a/gfx/thebes/gfxMacPlatformFontList.mm
+++ b/gfx/thebes/gfxMacPlatformFontList.mm
@@ -265,17 +265,17 @@ gfxFont*
 MacOSFontEntry::CreateFontInstance(const gfxFontStyle *aFontStyle, bool aNeedsBold)
 {
     RefPtr<UnscaledFontMac> unscaledFont(mUnscaledFont);
     if (!unscaledFont) {
         CGFontRef baseFont = GetFontRef();
         if (!baseFont) {
             return nullptr;
         }
-        unscaledFont = new UnscaledFontMac(baseFont);
+        unscaledFont = new UnscaledFontMac(baseFont, mIsDataUserFont);
         mUnscaledFont = unscaledFont;
     }
 
     return new gfxMacFont(unscaledFont, this, aFontStyle, aNeedsBold);
 }
 
 bool
 MacOSFontEntry::HasVariations()
--- a/gfx/webrender_bindings/Cargo.toml
+++ b/gfx/webrender_bindings/Cargo.toml
@@ -16,8 +16,12 @@ log = "0.3"
 [dependencies.webrender]
 path = "../webrender"
 version = "0.53.1"
 default-features = false
 
 [target.'cfg(target_os = "windows")'.dependencies]
 dwrote = "0.4"
 
+[target.'cfg(target_os = "macos")'.dependencies]
+core-foundation = "0.4"
+core-graphics = "0.9"
+
--- a/gfx/webrender_bindings/src/bindings.rs
+++ b/gfx/webrender_bindings/src/bindings.rs
@@ -16,16 +16,21 @@ use moz2d_renderer::Moz2dImageRenderer;
 use app_units::Au;
 use rayon;
 use euclid::SideOffsets2D;
 use log::{set_logger, shutdown_logger, LogLevelFilter, Log, LogLevel, LogMetadata, LogRecord};
 
 #[cfg(target_os = "windows")]
 use dwrote::{FontDescriptor, FontWeight, FontStretch, FontStyle};
 
+#[cfg(target_os = "macos")]
+use core_foundation::string::CFString;
+#[cfg(target_os = "macos")]
+use core_graphics::font::CGFont;
+
 extern crate webrender_api;
 
 /// cbindgen:field-names=[mNamespace, mHandle]
 type WrExternalImageBufferType = ExternalImageType;
 
 /// cbindgen:field-names=[mHandle]
 /// cbindgen:derive-lt=true
 /// cbindgen:derive-lte=true
@@ -994,16 +999,27 @@ fn read_font_descriptor(
     FontDescriptor {
         family_name: String::from_utf16(&wchars).unwrap(),
         weight: FontWeight::from_u32(index & 0xffff),
         stretch: FontStretch::from_u32((index >> 16) & 0xff),
         style: FontStyle::from_u32((index >> 24) & 0xff),
     }
 }
 
+#[cfg(target_os = "macos")]
+fn read_font_descriptor(
+    bytes: &mut WrVecU8,
+    _index: u32
+) -> NativeFontHandle {
+    let chars = bytes.flush_into_vec();
+    let name = String::from_utf8(chars).unwrap();
+    let font = CGFont::from_name(&CFString::new(&*name)).unwrap();
+    NativeFontHandle(font)
+}
+
 #[cfg(not(any(target_os = "macos", target_os = "windows")))]
 fn read_font_descriptor(
     bytes: &mut WrVecU8,
     index: u32
 ) -> NativeFontHandle {
     let cstr = CString::new(bytes.flush_into_vec()).unwrap();
     NativeFontHandle {
         pathname: String::from(cstr.to_str().unwrap()),
--- a/gfx/webrender_bindings/src/lib.rs
+++ b/gfx/webrender_bindings/src/lib.rs
@@ -11,11 +11,16 @@ extern crate app_units;
 extern crate gleam;
 extern crate rayon;
 extern crate thread_profiler;
 extern crate log;
 
 #[cfg(target_os = "windows")]
 extern crate dwrote;
 
+#[cfg(target_os = "macos")]
+extern crate core_foundation;
+#[cfg(target_os = "macos")]
+extern crate core_graphics;
+
 #[allow(non_snake_case)]
 pub mod bindings;
 pub mod moz2d_renderer;
--- a/toolkit/library/gtest/rust/Cargo.lock
+++ b/toolkit/library/gtest/rust/Cargo.lock
@@ -1512,16 +1512,18 @@ dependencies = [
  "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "webrender_bindings"
 version = "0.1.0"
 dependencies = [
  "app_units 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "core-foundation 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "core-graphics 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "dwrote 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "euclid 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "gleam 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "thread_profiler 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "webrender 0.53.1",
  "webrender_api 0.53.1",
--- a/toolkit/library/rust/Cargo.lock
+++ b/toolkit/library/rust/Cargo.lock
@@ -1524,16 +1524,18 @@ dependencies = [
  "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "webrender_bindings"
 version = "0.1.0"
 dependencies = [
  "app_units 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "core-foundation 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "core-graphics 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "dwrote 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "euclid 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "gleam 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "thread_profiler 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "webrender 0.53.1",
  "webrender_api 0.53.1",