1/*
2 * Copyright (c) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "HarfBuzzFace.h"
33
34#include "FontPlatformData.h"
35#include "HarfBuzzShaper.h"
36#include "SimpleFontData.h"
37
38#include "hb.h"
39#include <ApplicationServices/ApplicationServices.h>
40
41namespace WebCore {
42
43static hb_position_t floatToHarfBuzzPosition(CGFloat value)
44{
45    return static_cast<hb_position_t>(value * (1 << 16));
46}
47
48static hb_bool_t getGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
49{
50    CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
51    UniChar characters[4];
52    CGGlyph cgGlyphs[4];
53    size_t length = 0;
54    U16_APPEND_UNSAFE(characters, length, unicode);
55    if (!CTFontGetGlyphsForCharacters(ctFont, characters, cgGlyphs, length))
56        return false;
57    *glyph = cgGlyphs[0];
58    return true;
59}
60
61static hb_position_t getGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData)
62{
63    CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
64    CGGlyph cgGlyph = glyph;
65    CGFloat advance = CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrientation, &cgGlyph, 0, 1);
66    return floatToHarfBuzzPosition(advance);
67}
68
69static hb_bool_t getGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData)
70{
71    return true;
72}
73
74static hb_bool_t getGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_glyph_extents_t* extents, void* userData)
75{
76    CTFontRef ctFont = reinterpret_cast<FontPlatformData*>(fontData)->ctFont();
77    CGRect cgRect;
78    CGGlyph cgGlyph = glyph;
79    if (CTFontGetBoundingRectsForGlyphs(ctFont, kCTFontDefaultOrientation, &cgGlyph, &cgRect, 1) == CGRectNull)
80        return false;
81    extents->x_bearing = floatToHarfBuzzPosition(cgRect.origin.x);
82    extents->y_bearing = -floatToHarfBuzzPosition(cgRect.origin.y);
83    extents->width = floatToHarfBuzzPosition(cgRect.size.width);
84    extents->height = floatToHarfBuzzPosition(cgRect.size.height);
85    return true;
86}
87
88static hb_font_funcs_t* harfBuzzCoreTextGetFontFuncs()
89{
90    static hb_font_funcs_t* harfBuzzCoreTextFontFuncs = 0;
91
92    if (!harfBuzzCoreTextFontFuncs) {
93        harfBuzzCoreTextFontFuncs = hb_font_funcs_create();
94        hb_font_funcs_set_glyph_func(harfBuzzCoreTextFontFuncs, getGlyph, 0, 0);
95        hb_font_funcs_set_glyph_h_advance_func(harfBuzzCoreTextFontFuncs, getGlyphHorizontalAdvance, 0, 0);
96        hb_font_funcs_set_glyph_h_origin_func(harfBuzzCoreTextFontFuncs, getGlyphHorizontalOrigin, 0, 0);
97        hb_font_funcs_set_glyph_extents_func(harfBuzzCoreTextFontFuncs, getGlyphExtents, 0, 0);
98        hb_font_funcs_make_immutable(harfBuzzCoreTextFontFuncs);
99    }
100    return harfBuzzCoreTextFontFuncs;
101}
102
103static void releaseTableData(void* userData)
104{
105    CFDataRef cfData = reinterpret_cast<CFDataRef>(userData);
106    CFRelease(cfData);
107}
108
109static hb_blob_t* harfBuzzCoreTextGetTable(hb_face_t* face, hb_tag_t tag, void* userData)
110{
111    CGFontRef cgFont = reinterpret_cast<CGFontRef>(userData);
112    CFDataRef cfData = CGFontCopyTableForTag(cgFont, tag);
113    if (!cfData)
114        return 0;
115
116    const char* data = reinterpret_cast<const char*>(CFDataGetBytePtr(cfData));
117    const size_t length = CFDataGetLength(cfData);
118    if (!data || !length)
119        return 0;
120    return hb_blob_create(data, length, HB_MEMORY_MODE_READONLY, reinterpret_cast<void*>(const_cast<__CFData*>(cfData)), releaseTableData);
121}
122
123hb_face_t* HarfBuzzFace::createFace()
124{
125    // It seems that CTFontCopyTable of MacOSX10.5 sdk doesn't work for
126    // OpenType layout tables(GDEF, GSUB, GPOS). Use CGFontCopyTableForTag instead.
127    hb_face_t* face = hb_face_create_for_tables(harfBuzzCoreTextGetTable, m_platformData->cgFont(), 0);
128    ASSERT(face);
129    return face;
130}
131
132hb_font_t* HarfBuzzFace::createFont()
133{
134    hb_font_t* font = hb_font_create(m_face);
135    hb_font_set_funcs(font, harfBuzzCoreTextGetFontFuncs(), m_platformData, 0);
136    const float size = m_platformData->m_size;
137    hb_font_set_ppem(font, size, size);
138    const int scale = (1 << 16) * static_cast<int>(size);
139    hb_font_set_scale(font, scale, scale);
140    hb_font_make_immutable(font);
141    return font;
142}
143
144GlyphBufferAdvance HarfBuzzShaper::createGlyphBufferAdvance(float width, float height)
145{
146    return CGSizeMake(width, height);
147}
148
149} // namespace WebCore
150