OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "platform/fonts/harfbuzz/HarfBuzzFace.h" | |
33 | |
34 #include "hb-coretext.h" | |
35 #include "hb.h" | |
36 #include "platform/fonts/FontPlatformData.h" | |
37 #include "platform/fonts/SimpleFontData.h" | |
38 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h" | |
39 #include <AppKit/AppKit.h> | |
40 #include <ApplicationServices/ApplicationServices.h> | |
41 | |
42 // The names of these constants were taken from history | |
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 } | |
67 | |
68 namespace blink { | |
69 | |
70 static void advanceForGlyph(Glyph glyph, const FontPlatformData& platformData, C
GSize* advance) { | |
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 | |
80 static hb_position_t floatToHarfBuzzPosition(CGFloat value) | |
81 { | |
82 return static_cast<hb_position_t>(value * (1 << 16)); | |
83 } | |
84 | |
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) | |
86 { | |
87 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont(); | |
88 UniChar characters[4]; | |
89 CGGlyph cgGlyphs[4]; | |
90 size_t length = 0; | |
91 U16_APPEND_UNSAFE(characters, length, unicode); | |
92 if (!CTFontGetGlyphsForCharacters(ctFont, characters, cgGlyphs, length)) | |
93 return false; | |
94 *glyph = cgGlyphs[0]; | |
95 return true; | |
96 } | |
97 | |
98 | |
99 static hb_position_t getGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData
, hb_codepoint_t glyph, void* userData) | |
100 { | |
101 CGSize advance; | |
102 FontPlatformData* platformData = reinterpret_cast<FontPlatformData*>(fontDat
a); | |
103 advanceForGlyph(glyph, *platformData, &advance); | |
104 float syntheticBoldOffset = platformData->m_syntheticBold ? 1.0f : 0.0f; | |
105 return floatToHarfBuzzPosition(advance.width + syntheticBoldOffset); | |
106 } | |
107 | |
108 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) | |
109 { | |
110 return true; | |
111 } | |
112 | |
113 static hb_bool_t getGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint
_t glyph, hb_glyph_extents_t* extents, void* userData) | |
114 { | |
115 CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont(); | |
116 CGRect cgRect; | |
117 CGGlyph cgGlyph = glyph; | |
118 if (CTFontGetBoundingRectsForGlyphs(ctFont, kCTFontDefaultOrientation, &cgGl
yph, &cgRect, 1) == CGRectNull) | |
119 return false; | |
120 extents->x_bearing = floatToHarfBuzzPosition(cgRect.origin.x); | |
121 extents->y_bearing = -floatToHarfBuzzPosition(cgRect.origin.y); | |
122 extents->width = floatToHarfBuzzPosition(cgRect.size.width); | |
123 extents->height = floatToHarfBuzzPosition(cgRect.size.height); | |
124 return true; | |
125 } | |
126 | |
127 static hb_font_funcs_t* harfBuzzCoreTextGetFontFuncs() | |
128 { | |
129 static hb_font_funcs_t* harfBuzzCoreTextFontFuncs = 0; | |
130 | |
131 if (!harfBuzzCoreTextFontFuncs) { | |
132 harfBuzzCoreTextFontFuncs = hb_font_funcs_create(); | |
133 hb_font_funcs_set_glyph_func(harfBuzzCoreTextFontFuncs, getGlyph, 0, 0); | |
134 hb_font_funcs_set_glyph_h_advance_func(harfBuzzCoreTextFontFuncs, getGly
phHorizontalAdvance, 0, 0); | |
135 hb_font_funcs_set_glyph_h_origin_func(harfBuzzCoreTextFontFuncs, getGlyp
hHorizontalOrigin, 0, 0); | |
136 hb_font_funcs_set_glyph_extents_func(harfBuzzCoreTextFontFuncs, getGlyph
Extents, 0, 0); | |
137 hb_font_funcs_make_immutable(harfBuzzCoreTextFontFuncs); | |
138 } | |
139 return harfBuzzCoreTextFontFuncs; | |
140 } | |
141 | |
142 hb_face_t* HarfBuzzFace::createFace() | |
143 { | |
144 hb_face_t* face = hb_coretext_face_create(m_platformData->cgFont()); | |
145 ASSERT(face); | |
146 return face; | |
147 } | |
148 | |
149 hb_font_t* HarfBuzzFace::createFont() | |
150 { | |
151 hb_font_t* font = hb_font_create(m_face); | |
152 hb_font_set_funcs(font, harfBuzzCoreTextGetFontFuncs(), m_platformData, 0); | |
153 const float size = m_platformData->m_textSize; | |
154 hb_font_set_ppem(font, size, size); | |
155 const int scale = (1 << 16) * static_cast<int>(size); | |
156 hb_font_set_scale(font, scale, scale); | |
157 hb_font_make_immutable(font); | |
158 return font; | |
159 } | |
160 | |
161 } // namespace blink | |
OLD | NEW |