Add support for the USE_TYPO_METRICS flag on iOS
authorfred.wang@free.fr <fred.wang@free.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 4 Nov 2015 08:50:31 +0000 (08:50 +0000)
committerfred.wang@free.fr <fred.wang@free.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 4 Nov 2015 08:50:31 +0000 (08:50 +0000)
https://bugs.webkit.org/show_bug.cgi?id=131839

Reviewed by Darin Adler.

Make the iOS Font service use the typo metrics for fonts with a MATH table when the OS/2 USE_TYPO_METRICS flag is set.
The code shared by iOS, OS X and AppleWin is moved into a separate OpenTypeCG module.

No new tests because this is already tested by fonts/use-typo-metrics-1.html

* PlatformAppleWin.cmake: Add OpenTypeCG files.
* PlatformMac.cmake: ditto.
* WebCore.vcxproj/WebCore.vcxproj: ditto.
* WebCore.vcxproj/WebCore.vcxproj.filters: ditto.
* WebCore.xcodeproj/project.pbxproj: ditto.
* platform/graphics/cocoa/FontCocoa.mm:
(WebCore::Font::platformInit): Use functions from OpenTypeCG.
(WebCore::fontHasMathTable): Deleted.
* platform/graphics/ios/FontServicesIOS.mm:
(WebCore::FontServicesIOS::FontServicesIOS): Use the typo metrics for fonts with a MATH table when the OS/2 USE_TYPO_METRICS flag is set.
* platform/graphics/opentype/OpenTypeCG.h: Added.
* platform/graphics/opentype/OpenTypeCG.cpp: Added.
(WebCore::OpenType::fontHasMathTable): Move this code from FontCocoa.mm.
(WebCore::OpenType::readShortFromTable): Inline function to read a 16-bit big endian integer from the OS/2 table and to cast it into a short integer.
(WebCore::OpenType::tryGetTypoMetrics): Move this code from FontCocoa.mm.
* platform/graphics/opentype/OpenTypeTypes.h: Add missing Glyph.h header needed by TableWithCoverage::getCoverageIndex.
* platform/graphics/win/SimpleFontDataCGWin.cpp:
(WebCore::Font::platformInit): Use functions from OpenTypeCG.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@192017 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
Source/WebCore/ChangeLog
Source/WebCore/PlatformAppleWin.cmake
Source/WebCore/PlatformMac.cmake
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj.filters
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/graphics/cocoa/FontCocoa.mm
Source/WebCore/platform/graphics/ios/FontServicesIOS.mm
Source/WebCore/platform/graphics/opentype/OpenTypeCG.cpp [new file with mode: 0644]
Source/WebCore/platform/graphics/opentype/OpenTypeCG.h [new file with mode: 0644]
Source/WebCore/platform/graphics/opentype/OpenTypeTypes.h
Source/WebCore/platform/graphics/win/SimpleFontDataCGWin.cpp

index 77e0464867e0b835194be6dd0c56462affe75c3a..b85b180472cd19770115be02ea976de9e30a0b0f 100644 (file)
@@ -1,3 +1,34 @@
+2015-11-04  Frederic Wang  <fred.wang@free.fr>
+
+        Add support for the USE_TYPO_METRICS flag on iOS
+        https://bugs.webkit.org/show_bug.cgi?id=131839
+
+        Reviewed by Darin Adler.
+
+        Make the iOS Font service use the typo metrics for fonts with a MATH table when the OS/2 USE_TYPO_METRICS flag is set.
+        The code shared by iOS, OS X and AppleWin is moved into a separate OpenTypeCG module.
+
+        No new tests because this is already tested by fonts/use-typo-metrics-1.html
+
+        * PlatformAppleWin.cmake: Add OpenTypeCG files.
+        * PlatformMac.cmake: ditto.
+        * WebCore.vcxproj/WebCore.vcxproj: ditto.
+        * WebCore.vcxproj/WebCore.vcxproj.filters: ditto.
+        * WebCore.xcodeproj/project.pbxproj: ditto.
+        * platform/graphics/cocoa/FontCocoa.mm:
+        (WebCore::Font::platformInit): Use functions from OpenTypeCG.
+        (WebCore::fontHasMathTable): Deleted.
+        * platform/graphics/ios/FontServicesIOS.mm:
+        (WebCore::FontServicesIOS::FontServicesIOS): Use the typo metrics for fonts with a MATH table when the OS/2 USE_TYPO_METRICS flag is set.
+        * platform/graphics/opentype/OpenTypeCG.h: Added.
+        * platform/graphics/opentype/OpenTypeCG.cpp: Added.
+        (WebCore::OpenType::fontHasMathTable): Move this code from FontCocoa.mm.
+        (WebCore::OpenType::readShortFromTable): Inline function to read a 16-bit big endian integer from the OS/2 table and to cast it into a short integer.
+        (WebCore::OpenType::tryGetTypoMetrics): Move this code from FontCocoa.mm.
+        * platform/graphics/opentype/OpenTypeTypes.h: Add missing Glyph.h header needed by TableWithCoverage::getCoverageIndex.
+        * platform/graphics/win/SimpleFontDataCGWin.cpp:
+        (WebCore::Font::platformInit): Use functions from OpenTypeCG.
+
 2015-11-04  Chris Dumez  <cdumez@apple.com>
 
         Regression(r191652): Colloquy doesn’t render any chat content
index 52e01dc6a63a031e9e6fa3e6fbfa783bf91cbec5..e5209cb20cf03387a252643df4e0b215149204f7 100644 (file)
@@ -82,6 +82,8 @@ list(APPEND WebCore_SOURCES
     platform/graphics/cg/SubimageCacheWithTimer.cpp
     platform/graphics/cg/TransformationMatrixCG.cpp
 
+    platform/graphics/opentype/OpenTypeCG.cpp
+
     platform/graphics/win/FontCGWin.cpp
     platform/graphics/win/FontCustomPlatformData.cpp
     platform/graphics/win/FontPlatformDataCGWin.cpp
index ff8a46edc773d5a0ef682901679cf4ce759d51c9..05d893f92bfa864e96e225521e831a6065ce04ea 100644 (file)
@@ -442,6 +442,7 @@ list(APPEND WebCore_SOURCES
     platform/graphics/opengl/GraphicsContext3DOpenGLCommon.cpp
     platform/graphics/opengl/TemporaryOpenGLSetting.cpp
 
+    platform/graphics/opentype/OpenTypeCG.cpp
     platform/graphics/opentype/OpenTypeMathData.cpp
 
     platform/mac/BlockExceptions.mm
index 162e3f4959851ae65fd20e4b13a0374f66f30e51..782f1a3d9253677576b86407dc59eee80494feda 100644 (file)
     <ClCompile Include="..\platform\graphics\win\IntRectWin.cpp" />
     <ClCompile Include="..\platform\graphics\win\IntSizeWin.cpp" />
     <ClCompile Include="..\platform\graphics\win\MediaPlayerPrivateFullscreenWindow.cpp" />
+    <ClCompile Include="..\platform\graphics\opentype\OpenTypeCG.cpp">
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug_WinCairo|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug_WinCairo|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release_WinCairo|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release_WinCairo|x64'">true</ExcludedFromBuild>
+    </ClCompile>
     <ClCompile Include="..\platform\graphics\opentype\OpenTypeMathData.cpp" />
     <ClCompile Include="..\platform\graphics\opentype\OpenTypeUtilities.cpp" />
     <ClCompile Include="..\platform\graphics\opentype\OpenTypeVerticalData.cpp" />
index 1ef3321a801470ddad8e733eb6ef1b3bd1de6c03..2e4119000f2001fe621f1624c4924f03b5a21f21 100644 (file)
     <ClCompile Include="$(ConfigurationBuildDir)\obj$(PlatformArchitecture)\WebCore\DerivedSources\JSWebGLRenderingContextBase.cpp">
       <Filter>DerivedSources</Filter>
     </ClCompile>
+    <ClCompile Include="..\platform\graphics\opentype\OpenTypeCG.cpp">
+      <Filter>platform\graphics\opentype</Filter>
+    </ClCompile>
     <ClCompile Include="..\platform\graphics\opentype\OpenTypeMathData.cpp">
       <Filter>platform\graphics\opentype</Filter>
     </ClCompile>
index 877ccc786c5ea93549bc79402ea627cc8aec5c8c..12e5c3f6e4c7a68ea0d2553caa6d8440936b2592 100644 (file)
                B2C3DA6B0D006CD600EF6F26 /* FontSelector.h in Headers */ = {isa = PBXBuildFile; fileRef = B2C3DA5A0D006CD600EF6F26 /* FontSelector.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B2C3DA6C0D006CD600EF6F26 /* GlyphBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = B2C3DA5B0D006CD600EF6F26 /* GlyphBuffer.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B2C96D8D0B3AF2B7005E80EC /* JSSVGPathSegCustom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B2C96D8C0B3AF2B7005E80EC /* JSSVGPathSegCustom.cpp */; };
+               B2D3DA640D006CD600EF6F3A /* OpenTypeCG.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B2D3DA530D006CD600EF6F3A /* OpenTypeCG.cpp */; };
+               B2D3DA650D006CD600EF6F3A /* OpenTypeCG.h in Headers */ = {isa = PBXBuildFile; fileRef = B2D3DA540D006CD600EF6F3A /* OpenTypeCG.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B2D3DA640D006CD600EF6F27 /* OpenTypeMathData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B2D3DA530D006CD600EF6F27 /* OpenTypeMathData.cpp */; };
                B2D3DA650D006CD600EF6F27 /* OpenTypeMathData.h in Headers */ = {isa = PBXBuildFile; fileRef = B2D3DA540D006CD600EF6F27 /* OpenTypeMathData.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B2D3EA650D006CD600EF6F28 /* OpenTypeTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = B2D3EA540D006CD600EF6F28 /* OpenTypeTypes.h */; settings = {ATTRIBUTES = (Private, ); }; };
                B2C3DA5A0D006CD600EF6F26 /* FontSelector.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FontSelector.h; sourceTree = "<group>"; };
                B2C3DA5B0D006CD600EF6F26 /* GlyphBuffer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = GlyphBuffer.h; sourceTree = "<group>"; };
                B2C96D8C0B3AF2B7005E80EC /* JSSVGPathSegCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = JSSVGPathSegCustom.cpp; sourceTree = "<group>"; };
+               B2D3DA530D006CD600EF6F3A /* OpenTypeCG.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = OpenTypeCG.cpp; sourceTree = "<group>"; };
+               B2D3DA540D006CD600EF6F3A /* OpenTypeCG.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeCG.h; sourceTree = "<group>"; };
                B2D3DA530D006CD600EF6F27 /* OpenTypeMathData.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = OpenTypeMathData.cpp; sourceTree = "<group>"; };
                B2D3DA540D006CD600EF6F27 /* OpenTypeMathData.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeMathData.h; sourceTree = "<group>"; };
                B2D3EA540D006CD600EF6F28 /* OpenTypeTypes.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeTypes.h; sourceTree = "<group>"; };
                3721493318F0B6D600156EDC /* opentype */ = {
                        isa = PBXGroup;
                        children = (
+                               B2D3DA530D006CD600EF6F3A /* OpenTypeCG.cpp */,
+                               B2D3DA540D006CD600EF6F3A /* OpenTypeCG.h */,
                                B2D3DA530D006CD600EF6F27 /* OpenTypeMathData.cpp */,
                                B2D3DA540D006CD600EF6F27 /* OpenTypeMathData.h */,
                                B2D3EA540D006CD600EF6F28 /* OpenTypeTypes.h */,
                                FDA3E95A134A49EF008D4B5A /* OfflineAudioCompletionEvent.h in Headers */,
                                FDA9325E16703B2A008982DC /* OfflineAudioContext.h in Headers */,
                                FDA3E95C134A49EF008D4B5A /* OfflineAudioDestinationNode.h in Headers */,
+                               B2D3DA650D006CD600EF6F3A /* OpenTypeCG.h in Headers */,
                                B2D3DA650D006CD600EF6F27 /* OpenTypeMathData.h in Headers */,
                                B2D3EA650D006CD600EF6F28 /* OpenTypeTypes.h in Headers */,
                                CDE7FC45181904B1002BBB77 /* OrderIterator.h in Headers */,
                                FDA3E959134A49EF008D4B5A /* OfflineAudioCompletionEvent.cpp in Sources */,
                                FDA9325D16703B2A008982DC /* OfflineAudioContext.cpp in Sources */,
                                FDA3E95B134A49EF008D4B5A /* OfflineAudioDestinationNode.cpp in Sources */,
+                               B2D3DA640D006CD600EF6F3A /* OpenTypeCG.cpp in Sources */,
                                B2D3DA640D006CD600EF6F27 /* OpenTypeMathData.cpp in Sources */,
                                CDE7FC44181904B1002BBB77 /* OrderIterator.cpp in Sources */,
                                0014628A103CD1DE000B20DB /* OriginAccessEntry.cpp in Sources */,
index 51edd84fda50113276e52bab43bdb30624655ba4..881361171d112a78bafecc7553d152b2fd97454f 100644 (file)
@@ -58,7 +58,7 @@
 #endif
 
 #if USE(APPKIT)
-#include "OpenTypeTypes.h"
+#import "OpenTypeCG.h"
 #endif
 
 namespace WebCore {
@@ -77,18 +77,6 @@ static bool fontHasVerticalGlyphs(CTFontRef ctFont)
 }
 
 #if USE(APPKIT)
-static bool fontHasMathTable(CTFontRef ctFont)
-{
-    RetainPtr<CFArrayRef> tableTags = adoptCF(CTFontCopyAvailableTables(ctFont, kCTFontTableOptionNoOptions));
-    CFIndex numTables = CFArrayGetCount(tableTags.get());
-    for (CFIndex index = 0; index < numTables; ++index) {
-        CTFontTableTag tag = (CTFontTableTag)(uintptr_t)CFArrayGetValueAtIndex(tableTags.get(), index);
-        if (tag == 'MATH')
-            return true;
-    }
-    return false;
-}
-
 static NSString *webFallbackFontFamily(void)
 {
     static NSString *webFallbackFontFamily = [[[NSFont systemFontOfSize:16.0f] familyName] retain];
@@ -188,25 +176,13 @@ void Font::platformInit()
 
     // The Open Font Format describes the OS/2 USE_TYPO_METRICS flag as follows:
     // "If set, it is strongly recommended to use OS/2.sTypoAscender - OS/2.sTypoDescender+ OS/2.sTypoLineGap as a value for default line spacing for this font."
-    // We only apply this rule in the important case of fonts with a MATH table.
-    if (fontHasMathTable(m_platformData.ctFont())) {
-        if (CFDataRef os2Table = CGFontCopyTableForTag(m_platformData.cgFont(), kCTFontTableOS2)) {
-            // For the structure of the OS/2 table, see
-            // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html
-            const CFIndex fsSelectionOffset = 16 * 2 + 10 + 4 * 4 + 4 * 1;
-            const CFIndex sTypoAscenderOffset = fsSelectionOffset + 3 * 2;
-            const CFIndex sTypoDescenderOffset = sTypoAscenderOffset + 2;
-            const CFIndex sTypoLineGapOffset = sTypoDescenderOffset + 2;
-            if (CFDataGetLength(os2Table) >= sTypoLineGapOffset + 2) {
-                const UInt8* os2Data = CFDataGetBytePtr(os2Table);
-                const unsigned short useTypoMetricsMask = 1 << 7;
-                if (*(reinterpret_cast<const OpenType::UInt16*>(os2Data + fsSelectionOffset)) & useTypoMetricsMask) {
-                    ascent = scaleEmToUnits(*(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoAscenderOffset)), unitsPerEm) * pointSize;
-                    descent = -scaleEmToUnits(*(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoDescenderOffset)), unitsPerEm) * pointSize;
-                    lineGap = scaleEmToUnits(*(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoLineGapOffset)), unitsPerEm) * pointSize;
-                }
-            }
-            CFRelease(os2Table);
+    // On OS X, we only apply this rule in the important case of fonts with a MATH table.
+    if (OpenType::fontHasMathTable(m_platformData.ctFont())) {
+        short typoAscent, typoDescent, typoLineGap;
+        if (OpenType::tryGetTypoMetrics(m_platformData.cgFont(), typoAscent, typoDescent, typoLineGap)) {
+            ascent = scaleEmToUnits(typoAscent, unitsPerEm) * pointSize;
+            descent = -scaleEmToUnits(typoDescent, unitsPerEm) * pointSize;
+            lineGap = scaleEmToUnits(typoLineGap, unitsPerEm) * pointSize;
         }
     }
 
index 15483c1c5b7d9056357cb85475b353bffdfb7d61..aef63defcaf1fa038a950b0d0f245d66e40fee63 100644 (file)
@@ -28,6 +28,7 @@
 #import "CoreGraphicsSPI.h"
 #import "DynamicLinkerSPI.h"
 #import "FontMetrics.h"
+#import "OpenTypeCG.h"
 #import <wtf/RetainPtr.h>
 
 namespace WebCore {
@@ -95,6 +96,19 @@ FontServicesIOS::FontServicesIOS(CTFontRef font)
             descent = (scaleEmToUnits(615, 2048) * pointSize);
         }
     }
+    // The Open Font Format describes the OS/2 USE_TYPO_METRICS flag as follows:
+    // "If set, it is strongly recommended to use OS/2.sTypoAscender - OS/2.sTypoDescender+ OS/2.sTypoLineGap as a value for default line spacing for this font."
+    // On iOS, we only apply this rule in the important case of fonts with a MATH table.
+    if (OpenType::fontHasMathTable(font)) {
+        short typoAscent, typoDescent, typoLineGap;
+        if (OpenType::tryGetTypoMetrics(cgFont.get(), typoAscent, typoDescent, typoLineGap)) {
+            unsigned unitsPerEm = CGFontGetUnitsPerEm(cgFont.get());
+            float pointSize = CTFontGetSize(font);
+            ascent = scaleEmToUnits(typoAscent, unitsPerEm) * pointSize;
+            descent = -scaleEmToUnits(typoDescent, unitsPerEm) * pointSize;
+            lineGap = scaleEmToUnits(typoLineGap, unitsPerEm) * pointSize;
+        }
+    }
     CGFloat adjustment = (shouldUseAdjustment(font, isiOS7OrLater)) ? ceil((ascent + descent) * kLineHeightAdjustment) : 0;
 
     m_ascent = ascent + adjustment;
diff --git a/Source/WebCore/platform/graphics/opentype/OpenTypeCG.cpp b/Source/WebCore/platform/graphics/opentype/OpenTypeCG.cpp
new file mode 100644 (file)
index 0000000..82826f5
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2015 Frederic Wang (fred.wang@free.fr). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "OpenTypeCG.h"
+
+#include "OpenTypeTypes.h"
+
+namespace WebCore {
+namespace OpenType {
+
+#if PLATFORM(WIN)
+static const unsigned long kCTFontTableOS2 = 'OS/2';
+#endif
+
+#if PLATFORM(COCOA)
+bool fontHasMathTable(CTFontRef ctFont)
+{
+    RetainPtr<CFArrayRef> tableTags = adoptCF(CTFontCopyAvailableTables(ctFont, kCTFontTableOptionNoOptions));
+    CFIndex numTables = CFArrayGetCount(tableTags.get());
+    for (CFIndex index = 0; index < numTables; ++index) {
+        CTFontTableTag tag = (CTFontTableTag)(uintptr_t)CFArrayGetValueAtIndex(tableTags.get(), index);
+        if (tag == 'MATH')
+            return true;
+    }
+    return false;
+}
+#endif
+
+static inline short readShortFromTable(const UInt8* os2Data, CFIndex offset)
+{
+    return *(reinterpret_cast<const OpenType::Int16*>(os2Data + offset));
+}
+
+bool tryGetTypoMetrics(CGFontRef cgFont, short& ascent, short& descent, short& lineGap)
+{
+    bool result = false;
+    if (CFDataRef os2Table = CGFontCopyTableForTag(cgFont, kCTFontTableOS2)) {
+        // For the structure of the OS/2 table, see
+        // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html
+        const CFIndex fsSelectionOffset = 16 * 2 + 10 + 4 * 4 + 4 * 1;
+        const CFIndex sTypoAscenderOffset = fsSelectionOffset + 3 * 2;
+        const CFIndex sTypoDescenderOffset = sTypoAscenderOffset + 2;
+        const CFIndex sTypoLineGapOffset = sTypoDescenderOffset + 2;
+        if (CFDataGetLength(os2Table) >= sTypoLineGapOffset + 2) {
+            const UInt8* os2Data = CFDataGetBytePtr(os2Table);
+            // We test the use typo bit on the least significant byte of fsSelection.
+            const UInt8 useTypoMetricsMask = 1 << 7;
+            if (*(os2Data + fsSelectionOffset + 1) & useTypoMetricsMask) {
+                ascent = readShortFromTable(os2Data, sTypoAscenderOffset);
+                descent = readShortFromTable(os2Data, sTypoDescenderOffset);
+                lineGap = readShortFromTable(os2Data, sTypoLineGapOffset);
+                result = true;
+            }
+        }
+        CFRelease(os2Table);
+    }
+    return result;
+}
+
+} // namespace OpenType
+} // namespace WebCore
diff --git a/Source/WebCore/platform/graphics/opentype/OpenTypeCG.h b/Source/WebCore/platform/graphics/opentype/OpenTypeCG.h
new file mode 100644 (file)
index 0000000..bd4babf
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2015 Frederic Wang (fred.wang@free.fr). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef OpenTypeCG_h
+#define OpenTypeCG_h
+
+#include <CoreGraphics/CoreGraphics.h>
+#if PLATFORM(COCOA)
+#include <CoreText/CoreText.h>
+#endif
+
+namespace WebCore {
+namespace OpenType {
+
+#if PLATFORM(COCOA)
+bool fontHasMathTable(CTFontRef);
+#endif
+bool tryGetTypoMetrics(CGFontRef, short& ascent, short& descent, short& lineGap);
+
+} // namespace OpenType
+} // namespace WebCore
+#endif // OpenTypeCG_h
index a2fba1d4aa826864ae1fe89926c13e04f8f3bbd2..e89fb59986215b9d5dae179d2d759cef74181476 100644 (file)
 #ifndef OpenTypeTypes_h
 #define OpenTypeTypes_h
 
+#if ENABLE(OPENTYPE_MATH)
+#include "Glyph.h"
+#endif
+
 #include "SharedBuffer.h"
 
 namespace WebCore {
index 240593635446c2166e7295ed33e62624f428556f..044925387178343a4c2129ba5ceda101ad1c329d 100644 (file)
@@ -34,7 +34,7 @@
 #include "FontDescription.h"
 #include "GlyphPage.h"
 #include "HWndDC.h"
-#include "OpenTypeTypes.h"
+#include "OpenTypeCG.h"
 #include <ApplicationServices/ApplicationServices.h>
 #include <WebKitSystemInterface/WebKitSystemInterface.h>
 #include <mlang.h>
@@ -67,23 +67,11 @@ void Font::platformInit()
 
     // The Open Font Format describes the OS/2 USE_TYPO_METRICS flag as follows:
     // "If set, it is strongly recommended to use OS/2.sTypoAscender - OS/2.sTypoDescender+ OS/2.sTypoLineGap as a value for default line spacing for this font."
-    if (CFDataRef os2Table = CGFontCopyTableForTag(m_platformData.cgFont(), 'OS/2')) {
-        // For the structure of the OS/2 table, see
-        // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html
-        const CFIndex fsSelectionOffset = 16 * 2 + 10 + 4 * 4 + 4 * 1;
-        const CFIndex sTypoAscenderOffset = fsSelectionOffset + 3 * 2;
-        const CFIndex sTypoDescenderOffset = sTypoAscenderOffset + 2;
-        const CFIndex sTypoLineGapOffset = sTypoDescenderOffset + 2;
-        if (CFDataGetLength(os2Table) >= sTypoLineGapOffset + 2) {
-            const UInt8* os2Data = CFDataGetBytePtr(os2Table);
-            const unsigned short useTypoMetricsMask = 1 << 7;
-            if (*(reinterpret_cast<const OpenType::UInt16*>(os2Data + fsSelectionOffset)) & useTypoMetricsMask) {
-                iAscent = *(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoAscenderOffset));
-                iDescent = *(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoDescenderOffset));
-                iLineGap = *(reinterpret_cast<const OpenType::Int16*>(os2Data + sTypoLineGapOffset));
-            }
-        }
-        CFRelease(os2Table);
+    short typoAscent, typoDescent, typoLineGap;
+    if (OpenType::tryGetTypoMetrics(m_platformData.cgFont(), typoAscent, typoDescent, typoLineGap)) {
+        iAscent = typoAscent;
+        iDescent = typoDescent;
+        iLineGap = typoLineGap;
     }
 
     unsigned unitsPerEm = CGFontGetUnitsPerEm(font);