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/harfbuzz/HarfBuzzShaper.h" 44 45#include "hb.h" 46#include "wtf/HashMap.h" 47 48namespace WebCore { 49 50// Our implementation of the callbacks which HarfBuzz requires by using Skia 51// calls. See the HarfBuzz source for references about what these callbacks do. 52 53struct HarfBuzzFontData { 54 HarfBuzzFontData(WTF::HashMap<uint32_t, uint16_t>* glyphCacheForFaceCacheEntry) 55 : m_glyphCacheForFaceCacheEntry(glyphCacheForFaceCacheEntry) 56 { } 57 SkPaint m_paint; 58 WTF::HashMap<uint32_t, uint16_t>* m_glyphCacheForFaceCacheEntry; 59}; 60 61static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) 62{ 63 return SkScalarToFixed(value); 64} 65 66static void SkiaGetGlyphWidthAndExtents(SkPaint* paint, hb_codepoint_t codepoint, hb_position_t* width, hb_glyph_extents_t* extents) 67{ 68 ASSERT(codepoint <= 0xFFFF); 69 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); 70 71 SkScalar skWidth; 72 SkRect skBounds; 73 uint16_t glyph = codepoint; 74 75 paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds); 76 if (width) 77 *width = SkiaScalarToHarfBuzzPosition(skWidth); 78 if (extents) { 79 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be y-grows-up. 80 extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft); 81 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop); 82 extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width()); 83 extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height()); 84 } 85} 86 87static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData) 88{ 89 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData); 90 91 WTF::HashMap<uint32_t, uint16_t>::AddResult result = hbFontData->m_glyphCacheForFaceCacheEntry->add(unicode, 0); 92 if (result.isNewEntry) { 93 SkPaint* paint = &hbFontData->m_paint; 94 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); 95 uint16_t glyph16; 96 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &glyph16); 97 result.storedValue->value = glyph16; 98 *glyph = glyph16; 99 } 100 *glyph = result.storedValue->value; 101 return !!*glyph; 102} 103 104static hb_position_t harfBuzzGetGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData) 105{ 106 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData); 107 hb_position_t advance = 0; 108 109 SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, &advance, 0); 110 return advance; 111} 112 113static hb_bool_t harfBuzzGetGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData) 114{ 115 // Just return true, following the way that HarfBuzz-FreeType 116 // implementation does. 117 return true; 118} 119 120static hb_position_t harfBuzzGetGlyphHorizontalKerning(hb_font_t*, void* fontData, hb_codepoint_t leftGlyph, hb_codepoint_t rightGlyph, void*) 121{ 122 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData); 123 if (hbFontData->m_paint.isVerticalText()) { 124 // We don't support cross-stream kerning 125 return 0; 126 } 127 128 SkTypeface* typeface = hbFontData->m_paint.getTypeface(); 129 130 const uint16_t glyphs[2] = { static_cast<uint16_t>(leftGlyph), static_cast<uint16_t>(rightGlyph) }; 131 int32_t kerningAdjustments[1] = { 0 }; 132 133 if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) { 134 SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm()); 135 SkScalar size = hbFontData->m_paint.getTextSize(); 136 return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm)); 137 } 138 139 return 0; 140} 141 142static hb_position_t harfBuzzGetGlyphVerticalKerning(hb_font_t*, void* fontData, hb_codepoint_t topGlyph, hb_codepoint_t bottomGlyph, void*) 143{ 144 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData); 145 if (!hbFontData->m_paint.isVerticalText()) { 146 // We don't support cross-stream kerning 147 return 0; 148 } 149 150 SkTypeface* typeface = hbFontData->m_paint.getTypeface(); 151 152 const uint16_t glyphs[2] = { static_cast<uint16_t>(topGlyph), static_cast<uint16_t>(bottomGlyph) }; 153 int32_t kerningAdjustments[1] = { 0 }; 154 155 if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) { 156 SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm()); 157 SkScalar size = hbFontData->m_paint.getTextSize(); 158 return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm)); 159 } 160 161 return 0; 162} 163 164static hb_bool_t harfBuzzGetGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_glyph_extents_t* extents, void* userData) 165{ 166 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData); 167 168 SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, 0, extents); 169 return true; 170} 171 172static hb_font_funcs_t* harfBuzzSkiaGetFontFuncs() 173{ 174 static hb_font_funcs_t* harfBuzzSkiaFontFuncs = 0; 175 176 // We don't set callback functions which we can't support. 177 // HarfBuzz will use the fallback implementation if they aren't set. 178 if (!harfBuzzSkiaFontFuncs) { 179 harfBuzzSkiaFontFuncs = hb_font_funcs_create(); 180 hb_font_funcs_set_glyph_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyph, 0, 0); 181 hb_font_funcs_set_glyph_h_advance_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalAdvance, 0, 0); 182 hb_font_funcs_set_glyph_h_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalKerning, 0, 0); 183 hb_font_funcs_set_glyph_h_origin_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalOrigin, 0, 0); 184 hb_font_funcs_set_glyph_v_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphVerticalKerning, 0, 0); 185 hb_font_funcs_set_glyph_extents_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphExtents, 0, 0); 186 hb_font_funcs_make_immutable(harfBuzzSkiaFontFuncs); 187 } 188 return harfBuzzSkiaFontFuncs; 189} 190 191static hb_blob_t* harfBuzzSkiaGetTable(hb_face_t* face, hb_tag_t tag, void* userData) 192{ 193 SkTypeface* typeface = reinterpret_cast<SkTypeface*>(userData); 194 195 const size_t tableSize = typeface->getTableSize(tag); 196 if (!tableSize) 197 return 0; 198 199 char* buffer = reinterpret_cast<char*>(fastMalloc(tableSize)); 200 if (!buffer) 201 return 0; 202 size_t actualSize = typeface->getTableData(tag, 0, tableSize, buffer); 203 if (tableSize != actualSize) { 204 fastFree(buffer); 205 return 0; 206 } 207 208 return hb_blob_create(const_cast<char*>(buffer), tableSize, HB_MEMORY_MODE_WRITABLE, buffer, fastFree); 209} 210 211static void destroyHarfBuzzFontData(void* userData) 212{ 213 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(userData); 214 delete hbFontData; 215} 216 217hb_face_t* HarfBuzzFace::createFace() 218{ 219 hb_face_t* face = hb_face_create_for_tables(harfBuzzSkiaGetTable, m_platformData->typeface(), 0); 220 ASSERT(face); 221 return face; 222} 223 224hb_font_t* HarfBuzzFace::createFont() 225{ 226 HarfBuzzFontData* hbFontData = new HarfBuzzFontData(m_glyphCacheForFaceCacheEntry); 227 m_platformData->setupPaint(&hbFontData->m_paint); 228 hb_font_t* font = hb_font_create(m_face); 229 hb_font_set_funcs(font, harfBuzzSkiaGetFontFuncs(), hbFontData, destroyHarfBuzzFontData); 230 float size = m_platformData->size(); 231 int scale = SkiaScalarToHarfBuzzPosition(size); 232 hb_font_set_scale(font, scale, scale); 233 hb_font_make_immutable(font); 234 return font; 235} 236 237} // namespace WebCore 238