michael@0: michael@0: /* michael@0: * Copyright 2009 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkHarfBuzzFont.h" michael@0: #include "SkFontHost.h" michael@0: #include "SkPaint.h" michael@0: #include "SkPath.h" michael@0: michael@0: // HB_Fixed is a 26.6 fixed point format. michael@0: static inline HB_Fixed SkScalarToHarfbuzzFixed(SkScalar value) { michael@0: return static_cast(value * 64); michael@0: } michael@0: michael@0: static HB_Bool stringToGlyphs(HB_Font hbFont, const HB_UChar16* characters, michael@0: hb_uint32 length, HB_Glyph* glyphs, michael@0: hb_uint32* glyphsSize, HB_Bool isRTL) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: michael@0: paint.setTypeface(font->getTypeface()); michael@0: paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); michael@0: int numGlyphs = paint.textToGlyphs(characters, length * sizeof(uint16_t), michael@0: reinterpret_cast(glyphs)); michael@0: michael@0: // HB_Glyph is 32-bit, but Skia outputs only 16-bit numbers. So our michael@0: // |glyphs| array needs to be converted. michael@0: for (int i = numGlyphs - 1; i >= 0; --i) { michael@0: uint16_t value; michael@0: // We use a memcpy to avoid breaking strict aliasing rules. michael@0: memcpy(&value, reinterpret_cast(glyphs) + sizeof(uint16_t) * i, sizeof(uint16_t)); michael@0: glyphs[i] = value; michael@0: } michael@0: michael@0: *glyphsSize = numGlyphs; michael@0: return 1; michael@0: } michael@0: michael@0: static void glyphsToAdvances(HB_Font hbFont, const HB_Glyph* glyphs, michael@0: hb_uint32 numGlyphs, HB_Fixed* advances, int flags) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: michael@0: font->setupPaint(&paint); michael@0: paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); michael@0: michael@0: SkAutoMalloc storage(numGlyphs * (sizeof(SkScalar) + sizeof(uint16_t))); michael@0: SkScalar* scalarWidths = reinterpret_cast(storage.get()); michael@0: uint16_t* glyphs16 = reinterpret_cast(scalarWidths + numGlyphs); michael@0: michael@0: // convert HB 32bit glyphs to skia's 16bit michael@0: for (hb_uint32 i = 0; i < numGlyphs; ++i) { michael@0: glyphs16[i] = SkToU16(glyphs[i]); michael@0: } michael@0: paint.getTextWidths(glyphs16, numGlyphs * sizeof(uint16_t), scalarWidths); michael@0: michael@0: for (hb_uint32 i = 0; i < numGlyphs; ++i) { michael@0: advances[i] = SkScalarToHarfbuzzFixed(scalarWidths[i]); michael@0: } michael@0: } michael@0: michael@0: static HB_Bool canRender(HB_Font hbFont, const HB_UChar16* characters, michael@0: hb_uint32 length) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: michael@0: paint.setTypeface(font->getTypeface()); michael@0: paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); michael@0: return paint.containsText(characters, length * sizeof(uint16_t)); michael@0: } michael@0: michael@0: static HB_Error getOutlinePoint(HB_Font hbFont, HB_Glyph glyph, int flags, michael@0: hb_uint32 index, HB_Fixed* xPos, HB_Fixed* yPos, michael@0: hb_uint32* resultingNumPoints) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: michael@0: font->setupPaint(&paint); michael@0: paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); michael@0: if (flags & HB_ShaperFlag_UseDesignMetrics) { michael@0: paint.setHinting(SkPaint::kNo_Hinting); michael@0: } michael@0: michael@0: SkPath path; michael@0: uint16_t glyph16 = SkToU16(glyph); michael@0: paint.getTextPath(&glyph16, sizeof(glyph16), 0, 0, &path); michael@0: int numPoints = path.countPoints(); michael@0: if (index >= numPoints) { michael@0: return HB_Err_Invalid_SubTable; michael@0: } michael@0: michael@0: SkPoint pt = path.getPoint(index); michael@0: *xPos = SkScalarToHarfbuzzFixed(pt.fX); michael@0: *yPos = SkScalarToHarfbuzzFixed(pt.fY); michael@0: *resultingNumPoints = numPoints; michael@0: michael@0: return HB_Err_Ok; michael@0: } michael@0: michael@0: static void getGlyphMetrics(HB_Font hbFont, HB_Glyph glyph, michael@0: HB_GlyphMetrics* metrics) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: michael@0: font->setupPaint(&paint); michael@0: paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); michael@0: michael@0: SkScalar width; michael@0: SkRect bounds; michael@0: uint16_t glyph16 = SkToU16(glyph); michael@0: paint.getTextWidths(&glyph16, sizeof(glyph16), &width, &bounds); michael@0: michael@0: metrics->x = SkScalarToHarfbuzzFixed(bounds.fLeft); michael@0: metrics->y = SkScalarToHarfbuzzFixed(bounds.fTop); michael@0: metrics->width = SkScalarToHarfbuzzFixed(bounds.width()); michael@0: metrics->height = SkScalarToHarfbuzzFixed(bounds.height()); michael@0: michael@0: metrics->xOffset = SkScalarToHarfbuzzFixed(width); michael@0: // We can't actually get the |y| correct because Skia doesn't export michael@0: // the vertical advance. However, nor we do ever render vertical text at michael@0: // the moment so it's unimportant. michael@0: metrics->yOffset = 0; michael@0: } michael@0: michael@0: static HB_Fixed getFontMetric(HB_Font hbFont, HB_FontMetric metric) michael@0: { michael@0: SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); michael@0: SkPaint paint; michael@0: SkPaint::FontMetrics skiaMetrics; michael@0: michael@0: font->setupPaint(&paint); michael@0: paint.getFontMetrics(&skiaMetrics); michael@0: michael@0: switch (metric) { michael@0: case HB_FontAscent: michael@0: return SkScalarToHarfbuzzFixed(-skiaMetrics.fAscent); michael@0: default: michael@0: SkDebugf("--- unknown harfbuzz metric enum %d\n", metric); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static HB_FontClass gSkHarfBuzzFontClass = { michael@0: stringToGlyphs, michael@0: glyphsToAdvances, michael@0: canRender, michael@0: getOutlinePoint, michael@0: getGlyphMetrics, michael@0: getFontMetric, michael@0: }; michael@0: michael@0: const HB_FontClass& SkHarfBuzzFont::GetFontClass() { michael@0: return gSkHarfBuzzFontClass; michael@0: } michael@0: michael@0: HB_Error SkHarfBuzzFont::GetFontTableFunc(void* voidface, const HB_Tag tag, michael@0: HB_Byte* buffer, HB_UInt* len) { michael@0: SkHarfBuzzFont* font = reinterpret_cast(voidface); michael@0: SkTypeface* typeface = font->getTypeface(); michael@0: michael@0: const size_t tableSize = typeface->getTableSize(tag); michael@0: if (!tableSize) { michael@0: return HB_Err_Invalid_Argument; michael@0: } michael@0: // If Harfbuzz specified a NULL buffer then it's asking for the size. michael@0: if (!buffer) { michael@0: *len = tableSize; michael@0: return HB_Err_Ok; michael@0: } michael@0: michael@0: if (*len < tableSize) { michael@0: // is this right, or should we just copy less than the full table? michael@0: return HB_Err_Invalid_Argument; michael@0: } michael@0: typeface->getTableData(tag, 0, tableSize, buffer); michael@0: return HB_Err_Ok; michael@0: }