This source file includes following definitions.
- floatToHarfBuzzPosition
- getGlyph
- getGlyphHorizontalAdvance
- getGlyphHorizontalOrigin
- getGlyphExtents
- harfBuzzCoreTextGetFontFuncs
- releaseTableData
- harfBuzzCoreTextGetTable
- createFace
- createFont
- createGlyphBufferAdvance
#include "config.h"
#include "platform/fonts/harfbuzz/HarfBuzzFace.h"
#include "platform/fonts/FontPlatformData.h"
#include "platform/fonts/SimpleFontData.h"
#include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
#include <ApplicationServices/ApplicationServices.h>
#include "hb.h"
namespace WebCore {
static hb_position_t floatToHarfBuzzPosition(CGFloat value)
{
return static_cast<hb_position_t>(value * (1 << 16));
}
static hb_bool_t getGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
{
CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
UniChar characters[4];
CGGlyph cgGlyphs[4];
size_t length = 0;
U16_APPEND_UNSAFE(characters, length, unicode);
if (!CTFontGetGlyphsForCharacters(ctFont, characters, cgGlyphs, length))
return false;
*glyph = cgGlyphs[0];
return true;
}
static hb_position_t getGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData)
{
CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
CGGlyph cgGlyph = glyph;
CGFloat advance = CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrientation, &cgGlyph, 0, 1);
return floatToHarfBuzzPosition(advance);
}
static hb_bool_t getGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData)
{
return true;
}
static hb_bool_t getGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_glyph_extents_t* extents, void* userData)
{
CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
CGRect cgRect;
CGGlyph cgGlyph = glyph;
if (CTFontGetBoundingRectsForGlyphs(ctFont, kCTFontDefaultOrientation, &cgGlyph, &cgRect, 1) == CGRectNull)
return false;
extents->x_bearing = floatToHarfBuzzPosition(cgRect.origin.x);
extents->y_bearing = -floatToHarfBuzzPosition(cgRect.origin.y);
extents->width = floatToHarfBuzzPosition(cgRect.size.width);
extents->height = floatToHarfBuzzPosition(cgRect.size.height);
return true;
}
static hb_font_funcs_t* harfBuzzCoreTextGetFontFuncs()
{
static hb_font_funcs_t* harfBuzzCoreTextFontFuncs = 0;
if (!harfBuzzCoreTextFontFuncs) {
harfBuzzCoreTextFontFuncs = hb_font_funcs_create();
hb_font_funcs_set_glyph_func(harfBuzzCoreTextFontFuncs, getGlyph, 0, 0);
hb_font_funcs_set_glyph_h_advance_func(harfBuzzCoreTextFontFuncs, getGlyphHorizontalAdvance, 0, 0);
hb_font_funcs_set_glyph_h_origin_func(harfBuzzCoreTextFontFuncs, getGlyphHorizontalOrigin, 0, 0);
hb_font_funcs_set_glyph_extents_func(harfBuzzCoreTextFontFuncs, getGlyphExtents, 0, 0);
hb_font_funcs_make_immutable(harfBuzzCoreTextFontFuncs);
}
return harfBuzzCoreTextFontFuncs;
}
static void releaseTableData(void* userData)
{
CFDataRef cfData = reinterpret_cast<CFDataRef>(userData);
CFRelease(cfData);
}
static hb_blob_t* harfBuzzCoreTextGetTable(hb_face_t* face, hb_tag_t tag, void* userData)
{
CGFontRef cgFont = reinterpret_cast<CGFontRef>(userData);
CFDataRef cfData = CGFontCopyTableForTag(cgFont, tag);
if (!cfData)
return 0;
const char* data = reinterpret_cast<const char*>(CFDataGetBytePtr(cfData));
const size_t length = CFDataGetLength(cfData);
if (!data || !length)
return 0;
return hb_blob_create(data, length, HB_MEMORY_MODE_READONLY, reinterpret_cast<void*>(const_cast<__CFData*>(cfData)), releaseTableData);
}
hb_face_t* HarfBuzzFace::createFace()
{
hb_face_t* face = hb_face_create_for_tables(harfBuzzCoreTextGetTable, m_platformData->cgFont(), 0);
ASSERT(face);
return face;
}
hb_font_t* HarfBuzzFace::createFont()
{
hb_font_t* font = hb_font_create(m_face);
hb_font_set_funcs(font, harfBuzzCoreTextGetFontFuncs(), m_platformData, 0);
const float size = m_platformData->m_size;
hb_font_set_ppem(font, size, size);
const int scale = (1 << 16) * static_cast<int>(size);
hb_font_set_scale(font, scale, scale);
hb_font_make_immutable(font);
return font;
}
GlyphBufferAdvance HarfBuzzShaper::createGlyphBufferAdvance(float width, float height)
{
return CGSizeMake(width, height);
}
}