Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / harfbuzz / HarfBuzzFaceSkia.cpp
1 /*
2  * Copyright (c) 2012 Google Inc. All rights reserved.
3  * Copyright (c) 2014 BlackBerry Limited. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
34
35 #include "SkPaint.h"
36 #include "SkPath.h"
37 #include "SkPoint.h"
38 #include "SkRect.h"
39 #include "SkTypeface.h"
40 #include "SkUtils.h"
41 #include "platform/fonts/FontPlatformData.h"
42 #include "platform/fonts/SimpleFontData.h"
43 #include "platform/fonts/GlyphBuffer.h"
44 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
45
46 #include "hb.h"
47 #include "wtf/HashMap.h"
48
49 namespace WebCore {
50
51 // Our implementation of the callbacks which HarfBuzz requires by using Skia
52 // calls. See the HarfBuzz source for references about what these callbacks do.
53
54 struct HarfBuzzFontData {
55     HarfBuzzFontData(WTF::HashMap<uint32_t, uint16_t>* glyphCacheForFaceCacheEntry)
56         : m_glyphCacheForFaceCacheEntry(glyphCacheForFaceCacheEntry)
57     { }
58     SkPaint m_paint;
59     WTF::HashMap<uint32_t, uint16_t>* m_glyphCacheForFaceCacheEntry;
60 };
61
62 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value)
63 {
64     return SkScalarToFixed(value);
65 }
66
67 static void SkiaGetGlyphWidthAndExtents(SkPaint* paint, hb_codepoint_t codepoint, hb_position_t* width, hb_glyph_extents_t* extents)
68 {
69     ASSERT(codepoint <= 0xFFFF);
70     paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
71
72     SkScalar skWidth;
73     SkRect skBounds;
74     uint16_t glyph = codepoint;
75
76     paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds);
77     if (width)
78         *width = SkiaScalarToHarfBuzzPosition(skWidth);
79     if (extents) {
80         // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be y-grows-up.
81         extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft);
82         extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop);
83         extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width());
84         extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height());
85     }
86 }
87
88 static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
89 {
90     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
91
92     WTF::HashMap<uint32_t, uint16_t>::AddResult result = hbFontData->m_glyphCacheForFaceCacheEntry->add(unicode, 0);
93     if (result.isNewEntry) {
94         SkPaint* paint = &hbFontData->m_paint;
95         paint->setTextEncoding(SkPaint::kUTF32_TextEncoding);
96         uint16_t glyph16;
97         paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &glyph16);
98         result.storedValue->value = glyph16;
99         *glyph = glyph16;
100     }
101     *glyph = result.storedValue->value;
102     return !!*glyph;
103 }
104
105 static hb_position_t harfBuzzGetGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData)
106 {
107     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
108     hb_position_t advance = 0;
109
110     SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, &advance, 0);
111     return advance;
112 }
113
114 static hb_bool_t harfBuzzGetGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData)
115 {
116     // Just return true, following the way that HarfBuzz-FreeType
117     // implementation does.
118     return true;
119 }
120
121 static hb_position_t harfBuzzGetGlyphHorizontalKerning(hb_font_t*, void* fontData, hb_codepoint_t leftGlyph, hb_codepoint_t rightGlyph, void*)
122 {
123     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
124     if (hbFontData->m_paint.isVerticalText()) {
125         // We don't support cross-stream kerning
126         return 0;
127     }
128
129     SkTypeface* typeface = hbFontData->m_paint.getTypeface();
130
131     const uint16_t glyphs[2] = { leftGlyph, rightGlyph };
132     int32_t kerningAdjustments[1] = { 0 };
133
134     if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) {
135         SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm());
136         SkScalar size = hbFontData->m_paint.getTextSize();
137         return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm));
138     }
139
140     return 0;
141 }
142
143 static hb_position_t harfBuzzGetGlyphVerticalKerning(hb_font_t*, void* fontData, hb_codepoint_t topGlyph, hb_codepoint_t bottomGlyph, void*)
144 {
145     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
146     if (!hbFontData->m_paint.isVerticalText()) {
147         // We don't support cross-stream kerning
148         return 0;
149     }
150
151     SkTypeface* typeface = hbFontData->m_paint.getTypeface();
152
153     const uint16_t glyphs[2] = { topGlyph, bottomGlyph };
154     int32_t kerningAdjustments[1] = { 0 };
155
156     if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) {
157         SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm());
158         SkScalar size = hbFontData->m_paint.getTextSize();
159         return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm));
160     }
161
162     return 0;
163 }
164
165 static hb_bool_t harfBuzzGetGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_glyph_extents_t* extents, void* userData)
166 {
167     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
168
169     SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, 0, extents);
170     return true;
171 }
172
173 static hb_font_funcs_t* harfBuzzSkiaGetFontFuncs()
174 {
175     static hb_font_funcs_t* harfBuzzSkiaFontFuncs = 0;
176
177     // We don't set callback functions which we can't support.
178     // HarfBuzz will use the fallback implementation if they aren't set.
179     if (!harfBuzzSkiaFontFuncs) {
180         harfBuzzSkiaFontFuncs = hb_font_funcs_create();
181         hb_font_funcs_set_glyph_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyph, 0, 0);
182         hb_font_funcs_set_glyph_h_advance_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalAdvance, 0, 0);
183         hb_font_funcs_set_glyph_h_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalKerning, 0, 0);
184         hb_font_funcs_set_glyph_h_origin_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalOrigin, 0, 0);
185         hb_font_funcs_set_glyph_v_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphVerticalKerning, 0, 0);
186         hb_font_funcs_set_glyph_extents_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphExtents, 0, 0);
187         hb_font_funcs_make_immutable(harfBuzzSkiaFontFuncs);
188     }
189     return harfBuzzSkiaFontFuncs;
190 }
191
192 static hb_blob_t* harfBuzzSkiaGetTable(hb_face_t* face, hb_tag_t tag, void* userData)
193 {
194     SkTypeface* typeface = reinterpret_cast<SkTypeface*>(userData);
195
196     const size_t tableSize = typeface->getTableSize(tag);
197     if (!tableSize)
198         return 0;
199
200     char* buffer = reinterpret_cast<char*>(fastMalloc(tableSize));
201     if (!buffer)
202         return 0;
203     size_t actualSize = typeface->getTableData(tag, 0, tableSize, buffer);
204     if (tableSize != actualSize) {
205         fastFree(buffer);
206         return 0;
207     }
208
209     return hb_blob_create(const_cast<char*>(buffer), tableSize, HB_MEMORY_MODE_WRITABLE, buffer, fastFree);
210 }
211
212 static void destroyHarfBuzzFontData(void* userData)
213 {
214     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(userData);
215     delete hbFontData;
216 }
217
218 hb_face_t* HarfBuzzFace::createFace()
219 {
220     hb_face_t* face = hb_face_create_for_tables(harfBuzzSkiaGetTable, m_platformData->typeface(), 0);
221     ASSERT(face);
222     return face;
223 }
224
225 hb_font_t* HarfBuzzFace::createFont()
226 {
227     HarfBuzzFontData* hbFontData = new HarfBuzzFontData(m_glyphCacheForFaceCacheEntry);
228     m_platformData->setupPaint(&hbFontData->m_paint);
229     hb_font_t* font = hb_font_create(m_face);
230     hb_font_set_funcs(font, harfBuzzSkiaGetFontFuncs(), hbFontData, destroyHarfBuzzFontData);
231     float size = m_platformData->size();
232     int scale = SkiaScalarToHarfBuzzPosition(size);
233     hb_font_set_scale(font, scale, scale);
234     hb_font_make_immutable(font);
235     return font;
236 }
237
238 GlyphBufferAdvance HarfBuzzShaper::createGlyphBufferAdvance(float width, float height)
239 {
240     return GlyphBufferAdvance(width, height);
241 }
242
243 } // namespace WebCore