gfx/skia/trunk/src/ports/SkHarfBuzzFont.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial