1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4 * Copyright (C) 2007 Alp Toker <alp.toker@collabora.co.uk>
5 * Copyright (C) 2007 Pioneer Research Center USA, Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1.  Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 * 2.  Redistributions in binary form must reproduce the above copyright
14 *     notice, this list of conditions and the following disclaimer in the
15 *     documentation and/or other materials provided with the distribution.
16 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 *     its contributors may be used to endorse or promote products derived
18 *     from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33#include "GlyphPageTreeNode.h"
34
35#include "SimpleFontData.h"
36#include <pango/pango-font.h>
37
38namespace WebCore {
39
40static PangoGlyph pango_font_get_glyph(PangoFont* font, PangoContext* context, gunichar wc)
41{
42    PangoGlyph result = 0;
43    gchar buffer[7];
44
45    gint  length = g_unichar_to_utf8(wc, buffer);
46    g_return_val_if_fail(length, 0);
47
48    GList* items = pango_itemize(context, buffer, 0, length, NULL, NULL);
49
50    if (g_list_length(items) == 1) {
51        PangoItem* item = reinterpret_cast<PangoItem*>(items->data);
52        PangoFont* tmpFont = item->analysis.font;
53        item->analysis.font = font;
54
55        PangoGlyphString* glyphs = pango_glyph_string_new();
56        pango_shape(buffer, length, &item->analysis, glyphs);
57
58        item->analysis.font = tmpFont;
59
60        if (glyphs->num_glyphs == 1)
61            result = glyphs->glyphs[0].glyph;
62        else
63            g_warning("didn't get 1 glyph but %d", glyphs->num_glyphs);
64
65        pango_glyph_string_free(glyphs);
66    }
67
68    g_list_foreach(items, (GFunc)pango_item_free, NULL);
69    g_list_free(items);
70
71    return result;
72}
73
74bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
75{
76    // The bufferLength will be greater than the glyph page size if the buffer has Unicode supplementary characters.
77    // We won't support this for now.
78    if (bufferLength > GlyphPage::size)
79        return false;
80
81    if (!fontData->platformData().m_font || fontData->platformData().m_font == reinterpret_cast<PangoFont*>(-1))
82        return false;
83
84    bool haveGlyphs = false;
85    for (unsigned i = 0; i < length; i++) {
86        Glyph glyph = pango_font_get_glyph(fontData->platformData().m_font, fontData->platformData().m_context, buffer[i]);
87        if (!glyph)
88            setGlyphDataForIndex(offset + i, 0, 0);
89        else {
90            setGlyphDataForIndex(offset + i, glyph, fontData);
91            haveGlyphs = true;
92        }
93    }
94
95    return haveGlyphs;
96}
97
98}
99