Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: Source/platform/fonts/harfbuzz/HarfBuzzFaceCoreText.mm

Issue 175253002: Switch to HarfBuzz on Mac and remove CoreText shaper (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Marking word-spacing-characters-complex-text.html as Skip, adding canvas-normalize-string.html reba… Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 Google Inc. All rights reserved. 2 * Copyright (c) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "platform/fonts/harfbuzz/HarfBuzzFace.h" 32 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
33 33
34 #include "hb-coretext.h"
35 #include "hb.h"
34 #include "platform/fonts/FontPlatformData.h" 36 #include "platform/fonts/FontPlatformData.h"
35 #include "platform/fonts/SimpleFontData.h" 37 #include "platform/fonts/SimpleFontData.h"
36 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h" 38 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
39 #include <AppKit/AppKit.h>
40 #include <ApplicationServices/ApplicationServices.h>
37 41
38 #include <ApplicationServices/ApplicationServices.h> 42 // The names of these constants were taken from history
39 #include "hb.h" 43 // /trunk/WebKit/WebCoreSupport.subproj/WebTextRenderer.m@9311. The values
44 // were derived from the assembly of libWebKitSystemInterfaceLeopard.a.
45 enum CGFontRenderingMode {
46 kCGFontRenderingMode1BitPixelAligned = 0x0,
47 kCGFontRenderingModeAntialiasedPixelAligned = 0x1,
48 kCGFontRenderingModeAntialiased = 0xd
49 };
50
51 // Forward declare Mac SPIs.
52 extern "C" {
53 // Request for public API: rdar://13803586
54 bool CGFontGetGlyphAdvancesForStyle(CGFontRef font, CGAffineTransform* transform , CGFontRenderingMode renderingMode, ATSGlyphRef* glyph, size_t count, CGSize* a dvance);
55 }
56
57 static CGFontRenderingMode cgFontRenderingModeForNSFont(NSFont* font) {
58 if (!font)
59 return kCGFontRenderingModeAntialiasedPixelAligned;
60
61 switch ([font renderingMode]) {
62 case NSFontIntegerAdvancementsRenderingMode: return kCGFontRenderingMode 1BitPixelAligned;
63 case NSFontAntialiasedIntegerAdvancementsRenderingMode: return kCGFontRe nderingModeAntialiasedPixelAligned;
64 default: return kCGFontRenderingModeAntialiased;
65 }
66 }
40 67
41 namespace blink { 68 namespace blink {
42 69
70 void advanceForGlyph(Glyph glyph, const FontPlatformData& platformData, CGSize* advance) {
esprehn 2014/09/29 20:27:32 static
71 float pointSize = platformData.m_textSize;
72 NSFont *font = platformData.font();
73 CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
74 if (!CGFontGetGlyphAdvancesForStyle(platformData.cgFont(), &m, cgFontRenderi ngModeForNSFont(font), &glyph, 1, advance)) {
75 WTF_LOG_ERROR("Unable to retrieve glyph advance for %@ %f", [font displa yName], pointSize);
76 advance->width = 0;
77 }
78 }
79
43 static hb_position_t floatToHarfBuzzPosition(CGFloat value) 80 static hb_position_t floatToHarfBuzzPosition(CGFloat value)
44 { 81 {
45 return static_cast<hb_position_t>(value * (1 << 16)); 82 return static_cast<hb_position_t>(value * (1 << 16));
46 } 83 }
47 84
48 static hb_bool_t getGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unic ode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData) 85 static hb_bool_t getGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unic ode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
49 { 86 {
50 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont(); 87 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
51 UniChar characters[4]; 88 UniChar characters[4];
52 CGGlyph cgGlyphs[4]; 89 CGGlyph cgGlyphs[4];
53 size_t length = 0; 90 size_t length = 0;
54 U16_APPEND_UNSAFE(characters, length, unicode); 91 U16_APPEND_UNSAFE(characters, length, unicode);
55 if (!CTFontGetGlyphsForCharacters(ctFont, characters, cgGlyphs, length)) 92 if (!CTFontGetGlyphsForCharacters(ctFont, characters, cgGlyphs, length))
56 return false; 93 return false;
57 *glyph = cgGlyphs[0]; 94 *glyph = cgGlyphs[0];
58 return true; 95 return true;
59 } 96 }
60 97
98
99 void advanceForGlyph(Glyph, const FontPlatformData&, CGSize* advance);
100
61 static hb_position_t getGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData , hb_codepoint_t glyph, void* userData) 101 static hb_position_t getGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData , hb_codepoint_t glyph, void* userData)
62 { 102 {
63 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont(); 103 CGSize advance;
64 CGGlyph cgGlyph = glyph; 104 FontPlatformData* platformData = reinterpret_cast<FontPlatformData*>(fontDat a);
65 CGFloat advance = CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrient ation, &cgGlyph, 0, 1); 105 advanceForGlyph(glyph, *platformData, &advance);
66 return floatToHarfBuzzPosition(advance); 106 float syntheticBoldOffset = platformData->m_syntheticBold ? 1.0f : 0.0f;
107 return floatToHarfBuzzPosition(advance.width + syntheticBoldOffset);
67 } 108 }
68 109
69 static hb_bool_t getGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_ codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData) 110 static hb_bool_t getGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_ codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData)
70 { 111 {
71 return true; 112 return true;
72 } 113 }
73 114
74 static hb_bool_t getGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint _t glyph, hb_glyph_extents_t* extents, void* userData) 115 static hb_bool_t getGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint _t glyph, hb_glyph_extents_t* extents, void* userData)
75 { 116 {
76 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont(); 117 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
(...skipping 16 matching lines...) Expand all
93 harfBuzzCoreTextFontFuncs = hb_font_funcs_create(); 134 harfBuzzCoreTextFontFuncs = hb_font_funcs_create();
94 hb_font_funcs_set_glyph_func(harfBuzzCoreTextFontFuncs, getGlyph, 0, 0); 135 hb_font_funcs_set_glyph_func(harfBuzzCoreTextFontFuncs, getGlyph, 0, 0);
95 hb_font_funcs_set_glyph_h_advance_func(harfBuzzCoreTextFontFuncs, getGly phHorizontalAdvance, 0, 0); 136 hb_font_funcs_set_glyph_h_advance_func(harfBuzzCoreTextFontFuncs, getGly phHorizontalAdvance, 0, 0);
96 hb_font_funcs_set_glyph_h_origin_func(harfBuzzCoreTextFontFuncs, getGlyp hHorizontalOrigin, 0, 0); 137 hb_font_funcs_set_glyph_h_origin_func(harfBuzzCoreTextFontFuncs, getGlyp hHorizontalOrigin, 0, 0);
97 hb_font_funcs_set_glyph_extents_func(harfBuzzCoreTextFontFuncs, getGlyph Extents, 0, 0); 138 hb_font_funcs_set_glyph_extents_func(harfBuzzCoreTextFontFuncs, getGlyph Extents, 0, 0);
98 hb_font_funcs_make_immutable(harfBuzzCoreTextFontFuncs); 139 hb_font_funcs_make_immutable(harfBuzzCoreTextFontFuncs);
99 } 140 }
100 return harfBuzzCoreTextFontFuncs; 141 return harfBuzzCoreTextFontFuncs;
101 } 142 }
102 143
103 static void releaseTableData(void* userData)
104 {
105 CFDataRef cfData = reinterpret_cast<CFDataRef>(userData);
106 CFRelease(cfData);
107 }
108
109 static hb_blob_t* harfBuzzCoreTextGetTable(hb_face_t* face, hb_tag_t tag, void* userData)
110 {
111 CGFontRef cgFont = reinterpret_cast<CGFontRef>(userData);
112 if (!cgFont)
113 return 0;
114 CFDataRef cfData = CGFontCopyTableForTag(cgFont, tag);
115 if (!cfData)
116 return 0;
117
118 const char* data = reinterpret_cast<const char*>(CFDataGetBytePtr(cfData));
119 const size_t length = CFDataGetLength(cfData);
120 if (!data || !length)
121 return 0;
122 return hb_blob_create(data, length, HB_MEMORY_MODE_READONLY, reinterpret_cas t<void*>(const_cast<__CFData*>(cfData)), releaseTableData);
123 }
124
125 hb_face_t* HarfBuzzFace::createFace() 144 hb_face_t* HarfBuzzFace::createFace()
126 { 145 {
127 // It seems that CTFontCopyTable of MacOSX10.5 sdk doesn't work for 146 hb_face_t* face = hb_coretext_face_create(m_platformData->cgFont());
128 // OpenType layout tables(GDEF, GSUB, GPOS). Use CGFontCopyTableForTag inste ad.
129 hb_face_t* face = hb_face_create_for_tables(harfBuzzCoreTextGetTable, m_plat formData->cgFont(), 0);
130 ASSERT(face); 147 ASSERT(face);
131 return face; 148 return face;
132 } 149 }
133 150
134 hb_font_t* HarfBuzzFace::createFont() 151 hb_font_t* HarfBuzzFace::createFont()
135 { 152 {
136 hb_font_t* font = hb_font_create(m_face); 153 hb_font_t* font = hb_font_create(m_face);
137 hb_font_set_funcs(font, harfBuzzCoreTextGetFontFuncs(), m_platformData, 0); 154 hb_font_set_funcs(font, harfBuzzCoreTextGetFontFuncs(), m_platformData, 0);
138 const float size = m_platformData->m_textSize; 155 const float size = m_platformData->m_textSize;
139 hb_font_set_ppem(font, size, size); 156 hb_font_set_ppem(font, size, size);
140 const int scale = (1 << 16) * static_cast<int>(size); 157 const int scale = (1 << 16) * static_cast<int>(size);
141 hb_font_set_scale(font, scale, scale); 158 hb_font_set_scale(font, scale, scale);
142 hb_font_make_immutable(font); 159 hb_font_make_immutable(font);
143 return font; 160 return font;
144 } 161 }
145 162
146 } // namespace blink 163 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/fonts/harfbuzz/HarfBuzzFaceCoreText.cpp ('k') | Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698