1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkHarfBuzzFont.h"
18#include "SkFontHost.h"
19#include "SkPaint.h"
20#include "SkPath.h"
21
22// HB_Fixed is a 26.6 fixed point format.
23static inline HB_Fixed SkScalarToHarfbuzzFixed(SkScalar value) {
24#ifdef SK_SCALAR_IS_FLOAT
25    return static_cast<HB_Fixed>(value * 64);
26#else
27    // convert .16 to .6
28    return value >> (16 - 6);
29#endif
30}
31
32static HB_Bool stringToGlyphs(HB_Font hbFont, const HB_UChar16* characters,
33                              hb_uint32 length, HB_Glyph* glyphs,
34                              hb_uint32* glyphsSize, HB_Bool isRTL) {
35    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
36    SkPaint paint;
37
38    paint.setTypeface(font->getTypeface());
39    paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
40    int numGlyphs = paint.textToGlyphs(characters, length * sizeof(uint16_t),
41                                       reinterpret_cast<uint16_t*>(glyphs));
42
43    // HB_Glyph is 32-bit, but Skia outputs only 16-bit numbers. So our
44    // |glyphs| array needs to be converted.
45    for (int i = numGlyphs - 1; i >= 0; --i) {
46        uint16_t value;
47        // We use a memcpy to avoid breaking strict aliasing rules.
48        memcpy(&value, reinterpret_cast<char*>(glyphs) + sizeof(uint16_t) * i, sizeof(uint16_t));
49        glyphs[i] = value;
50    }
51
52    *glyphsSize = numGlyphs;
53    return 1;
54}
55
56static void glyphsToAdvances(HB_Font hbFont, const HB_Glyph* glyphs,
57                         hb_uint32 numGlyphs, HB_Fixed* advances, int flags) {
58    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
59    SkPaint paint;
60
61    font->setupPaint(&paint);
62    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
63
64    SkAutoMalloc storage(numGlyphs * (sizeof(SkScalar) + sizeof(uint16_t)));
65    SkScalar* scalarWidths = reinterpret_cast<SkScalar*>(storage.get());
66    uint16_t* glyphs16 = reinterpret_cast<uint16_t*>(scalarWidths + numGlyphs);
67
68    // convert HB 32bit glyphs to skia's 16bit
69    for (hb_uint32 i = 0; i < numGlyphs; ++i) {
70        glyphs16[i] = SkToU16(glyphs[i]);
71    }
72    paint.getTextWidths(glyphs16, numGlyphs * sizeof(uint16_t), scalarWidths);
73
74    for (hb_uint32 i = 0; i < numGlyphs; ++i) {
75        advances[i] = SkScalarToHarfbuzzFixed(scalarWidths[i]);
76    }
77}
78
79static HB_Bool canRender(HB_Font hbFont, const HB_UChar16* characters,
80                         hb_uint32 length) {
81    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
82    SkPaint paint;
83
84    paint.setTypeface(font->getTypeface());
85    paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
86    return paint.containsText(characters, length * sizeof(uint16_t));
87}
88
89static HB_Error getOutlinePoint(HB_Font hbFont, HB_Glyph glyph, int flags,
90                                hb_uint32 index, HB_Fixed* xPos, HB_Fixed* yPos,
91                                hb_uint32* resultingNumPoints) {
92    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
93    SkPaint paint;
94
95    font->setupPaint(&paint);
96    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
97    if (flags & HB_ShaperFlag_UseDesignMetrics) {
98        paint.setHinting(SkPaint::kNo_Hinting);
99    }
100
101    SkPath path;
102    uint16_t glyph16 = SkToU16(glyph);
103    paint.getTextPath(&glyph16, sizeof(glyph16), 0, 0, &path);
104    int numPoints = path.countPoints();
105    if (index >= numPoints) {
106        return HB_Err_Invalid_SubTable;
107    }
108
109    SkPoint pt = path.getPoint(index);
110    *xPos = SkScalarToHarfbuzzFixed(pt.fX);
111    *yPos = SkScalarToHarfbuzzFixed(pt.fY);
112    *resultingNumPoints = numPoints;
113
114    return HB_Err_Ok;
115}
116
117static void getGlyphMetrics(HB_Font hbFont, HB_Glyph glyph,
118                            HB_GlyphMetrics* metrics) {
119    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
120    SkPaint paint;
121
122    font->setupPaint(&paint);
123    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
124
125    SkScalar width;
126    SkRect bounds;
127    uint16_t glyph16 = SkToU16(glyph);
128    paint.getTextWidths(&glyph16, sizeof(glyph16), &width, &bounds);
129
130    metrics->x = SkScalarToHarfbuzzFixed(bounds.fLeft);
131    metrics->y = SkScalarToHarfbuzzFixed(bounds.fTop);
132    metrics->width = SkScalarToHarfbuzzFixed(bounds.width());
133    metrics->height = SkScalarToHarfbuzzFixed(bounds.height());
134
135    metrics->xOffset = SkScalarToHarfbuzzFixed(width);
136    // We can't actually get the |y| correct because Skia doesn't export
137    // the vertical advance. However, nor we do ever render vertical text at
138    // the moment so it's unimportant.
139    metrics->yOffset = 0;
140}
141
142static HB_Fixed getFontMetric(HB_Font hbFont, HB_FontMetric metric)
143{
144    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(hbFont->userData);
145    SkPaint paint;
146    SkPaint::FontMetrics skiaMetrics;
147
148    font->setupPaint(&paint);
149    paint.getFontMetrics(&skiaMetrics);
150
151    switch (metric) {
152    case HB_FontAscent:
153        return SkScalarToHarfbuzzFixed(-skiaMetrics.fAscent);
154    default:
155        SkDebugf("--- unknown harfbuzz metric enum %d\n", metric);
156        return 0;
157    }
158}
159
160static HB_FontClass gSkHarfBuzzFontClass = {
161    stringToGlyphs,
162    glyphsToAdvances,
163    canRender,
164    getOutlinePoint,
165    getGlyphMetrics,
166    getFontMetric,
167};
168
169const HB_FontClass& SkHarfBuzzFont::GetFontClass() {
170    return gSkHarfBuzzFontClass;
171}
172
173HB_Error SkHarfBuzzFont::GetFontTableFunc(void* voidface, const HB_Tag tag,
174                                          HB_Byte* buffer, HB_UInt* len) {
175    SkHarfBuzzFont* font = reinterpret_cast<SkHarfBuzzFont*>(voidface);
176    uint32_t uniqueID = SkTypeface::UniqueID(font->getTypeface());
177
178    const size_t tableSize = SkFontHost::GetTableSize(uniqueID, tag);
179    if (!tableSize) {
180        return HB_Err_Invalid_Argument;
181    }
182    // If Harfbuzz specified a NULL buffer then it's asking for the size.
183    if (!buffer) {
184        *len = tableSize;
185        return HB_Err_Ok;
186    }
187
188    if (*len < tableSize) {
189        // is this right, or should we just copy less than the full table?
190        return HB_Err_Invalid_Argument;
191    }
192    SkFontHost::GetTableData(uniqueID, tag, 0, tableSize, buffer);
193    return HB_Err_Ok;
194}
195
196