diff options
author | Steve Block <steveblock@google.com> | 2011-05-06 11:45:16 +0100 |
---|---|---|
committer | Steve Block <steveblock@google.com> | 2011-05-12 13:44:10 +0100 |
commit | cad810f21b803229eb11403f9209855525a25d57 (patch) | |
tree | 29a6fd0279be608e0fe9ffe9841f722f0f4e4269 /Source/WebCore/platform/graphics/pango | |
parent | 121b0cf4517156d0ac5111caf9830c51b69bae8f (diff) | |
download | external_webkit-cad810f21b803229eb11403f9209855525a25d57.zip external_webkit-cad810f21b803229eb11403f9209855525a25d57.tar.gz external_webkit-cad810f21b803229eb11403f9209855525a25d57.tar.bz2 |
Merge WebKit at r75315: Initial merge by git.
Change-Id: I570314b346ce101c935ed22a626b48c2af266b84
Diffstat (limited to 'Source/WebCore/platform/graphics/pango')
6 files changed, 756 insertions, 0 deletions
diff --git a/Source/WebCore/platform/graphics/pango/FontCachePango.cpp b/Source/WebCore/platform/graphics/pango/FontCachePango.cpp new file mode 100644 index 0000000..fad29e0 --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/FontCachePango.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2008 Alp Toker <alp@atoker.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#include "config.h" +#include "FontCache.h" + +#include "CString.h" +#include "Font.h" +#include "OwnPtrCairo.h" +#include "SimpleFontData.h" +#include <wtf/Assertions.h> + +namespace WebCore { + +void FontCache::platformInit() +{ + if (!FontPlatformData::init()) + ASSERT_NOT_REACHED(); +} + +const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length) +{ + return 0; +} + +SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font) +{ + return 0; +} + +SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription) +{ + // FIXME: Would be even better to somehow get the user's default font here. + // For now we'll pick the default that the user would get without changing any prefs. + static AtomicString timesStr("Times New Roman"); + return getCachedFontData(fontDescription, timesStr); +} + +void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks) +{ +} + +FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family) +{ + return new FontPlatformData(fontDescription, family); +} + +} diff --git a/Source/WebCore/platform/graphics/pango/FontCustomPlatformDataPango.cpp b/Source/WebCore/platform/graphics/pango/FontCustomPlatformDataPango.cpp new file mode 100644 index 0000000..a158689 --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/FontCustomPlatformDataPango.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2008 Alp Toker <alp@atoker.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#include "config.h" +#include "FontCustomPlatformData.h" + +#include "SharedBuffer.h" +#include "FontPlatformData.h" + +namespace WebCore { + +FontCustomPlatformData::~FontCustomPlatformData() +{ +} + +FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontOrientation, FontRenderingMode) +{ + return FontPlatformData(m_fontFace, size, bold, italic); +} + +static void releaseData(void* data) +{ + static_cast<SharedBuffer*>(data)->deref(); +} + +FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer) +{ + // FIXME: we need support in pango to read fonts from memory to implement this.y + return 0; +} + +bool FontCustomPlatformData::supportsFormat(const String& /* format */) +{ + return false; +} + +} diff --git a/Source/WebCore/platform/graphics/pango/FontPlatformData.h b/Source/WebCore/platform/graphics/pango/FontPlatformData.h new file mode 100644 index 0000000..2929a3f --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/FontPlatformData.h @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2006, 2007, 2008 Apple Inc. + * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com + * Copyright (C) 2007 Holger Hans Peter Freyther + * Copyright (C) 2007 Pioneer Research Center USA, Inc. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef FontPlatformDataPango_h +#define FontPlatformDataPango_h + +#include "FontDescription.h" +#include "FontOrientation.h" +#include "GlyphBuffer.h" +#include <cairo.h> +#include <pango/pangocairo.h> +#include <wtf/Forward.h> + +namespace WebCore { + +class FontPlatformData { +public: + FontPlatformData(WTF::HashTableDeletedValueType) + : m_context(0) + , m_font(hashTableDeletedFontValue()) + , m_size(0) + , m_syntheticBold(false) + , m_syntheticOblique(false) + , m_scaledFont(0) + { } + + FontPlatformData() + : m_context(0) + , m_font(0) + , m_size(0) + , m_syntheticBold(false) + , m_syntheticOblique(false) + , m_scaledFont(0) + { } + + FontPlatformData(const FontDescription&, const AtomicString& family); + FontPlatformData(cairo_font_face_t* fontFace, float size, bool bold, bool italic); + FontPlatformData(float size, bool bold, bool italic); + FontPlatformData(const FontPlatformData&); + ~FontPlatformData(); + + static bool init(); + bool isFixedPitch(); + float size() const { return m_size; } + void setSize(float size) { m_size = size; } + bool syntheticBold() const { return m_syntheticBold; } + bool syntheticOblique() const { return m_syntheticOblique; } + + FontOrientation orientation() const { return Horizontal; } // FIXME: Implement. + + cairo_scaled_font_t* scaledFont() const { return m_scaledFont; } + + unsigned hash() const + { + uintptr_t hashCodes[1] = { reinterpret_cast<uintptr_t>(m_scaledFont) }; + return WTF::StringHasher::createBlobHash<sizeof(hashCodes)>(hashCodes); + } + + bool operator==(const FontPlatformData&) const; + FontPlatformData& operator=(const FontPlatformData&); + bool isHashTableDeletedValue() const + { + return m_font == hashTableDeletedFontValue(); + } + +#ifndef NDEBUG + String description() const; +#endif + + static PangoFontMap* m_fontMap; + static GHashTable* m_hashTable; + PangoContext* m_context; + PangoFont* m_font; + float m_size; + bool m_syntheticBold; + bool m_syntheticOblique; + cairo_scaled_font_t* m_scaledFont; +private: + static PangoFont *hashTableDeletedFontValue() { return reinterpret_cast<PangoFont*>(-1); } +}; + +} + +#endif // FontPlatformDataPango_h diff --git a/Source/WebCore/platform/graphics/pango/FontPlatformDataPango.cpp b/Source/WebCore/platform/graphics/pango/FontPlatformDataPango.cpp new file mode 100644 index 0000000..03dc33d --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/FontPlatformDataPango.cpp @@ -0,0 +1,277 @@ +/* + * Copyright (C) 2006 Apple Computer, Inc. + * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com + * Copyright (C) 2007, 2008 Alp Toker <alp@atoker.com> + * Copyright (C) 2007 Holger Hans Peter Freyther + * Copyright (C) 2007 Pioneer Research Center USA, Inc. + * Copyright (C) 2009 Igalia S.L. + * All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include "config.h" +#include "FontPlatformData.h" + +#include "PlatformString.h" +#include "FontDescription.h" +#include <wtf/text/CString.h> +#include <cairo.h> +#include <assert.h> + +#include <pango/pango.h> +#include <pango/pangocairo.h> + +// Use cairo-ft i a recent enough Pango version isn't available +#if !PANGO_VERSION_CHECK(1,18,0) +#include <cairo-ft.h> +#include <pango/pangofc-fontmap.h> +#endif + +namespace WebCore { + +PangoFontMap* FontPlatformData::m_fontMap = 0; +GHashTable* FontPlatformData::m_hashTable = 0; + +FontPlatformData::FontPlatformData(const FontDescription& fontDescription, const AtomicString& familyName) + : m_context(0) + , m_font(0) + , m_size(fontDescription.computedSize()) + , m_syntheticBold(false) + , m_syntheticOblique(false) + , m_scaledFont(0) +{ + FontPlatformData::init(); + + CString stored_family = familyName.string().utf8(); + char const* families[] = { + stored_family.data(), + NULL + }; + + switch (fontDescription.genericFamily()) { + case FontDescription::SerifFamily: + families[1] = "serif"; + break; + case FontDescription::SansSerifFamily: + families[1] = "sans"; + break; + case FontDescription::MonospaceFamily: + families[1] = "monospace"; + break; + case FontDescription::NoFamily: + case FontDescription::StandardFamily: + default: + families[1] = "sans"; + break; + } + + PangoFontDescription* description = pango_font_description_new(); + pango_font_description_set_absolute_size(description, fontDescription.computedSize() * PANGO_SCALE); + + // FIXME: Map all FontWeight values to Pango font weights. + if (fontDescription.weight() >= FontWeight600) + pango_font_description_set_weight(description, PANGO_WEIGHT_BOLD); + if (fontDescription.italic()) + pango_font_description_set_style(description, PANGO_STYLE_ITALIC); + +#if PANGO_VERSION_CHECK(1,21,5) // deprecated in 1.21 + m_context = pango_font_map_create_context(m_fontMap); +#else + m_context = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(m_fontMap)); +#endif + for (unsigned int i = 0; !m_font && i < G_N_ELEMENTS(families); i++) { + pango_font_description_set_family(description, families[i]); + pango_context_set_font_description(m_context, description); + m_font = pango_font_map_load_font(m_fontMap, m_context, description); + } + +#if PANGO_VERSION_CHECK(1,18,0) + if (m_font) + m_scaledFont = cairo_scaled_font_reference(pango_cairo_font_get_scaled_font(PANGO_CAIRO_FONT(m_font))); +#else + // This compatibility code for older versions of Pango is not well-tested. + if (m_font) { + PangoFcFont* fcfont = PANGO_FC_FONT(m_font); + cairo_font_face_t* face = cairo_ft_font_face_create_for_pattern(fcfont->font_pattern); + double size; + if (FcPatternGetDouble(fcfont->font_pattern, FC_PIXEL_SIZE, 0, &size) != FcResultMatch) + size = 12.0; + cairo_matrix_t fontMatrix; + cairo_matrix_init_scale(&fontMatrix, size, size); + cairo_font_options_t* fontOptions; + if (pango_cairo_context_get_font_options(m_context)) + fontOptions = cairo_font_options_copy(pango_cairo_context_get_font_options(m_context)); + else + fontOptions = cairo_font_options_create(); + cairo_matrix_t ctm; + cairo_matrix_init_identity(&ctm); + m_scaledFont = cairo_scaled_font_create(face, &fontMatrix, &ctm, fontOptions); + cairo_font_options_destroy(fontOptions); + cairo_font_face_destroy(face); + } +#endif + pango_font_description_free(description); +} + +FontPlatformData::FontPlatformData(float size, bool bold, bool italic) + : m_context(0) + , m_font(0) + , m_size(size) + , m_syntheticBold(bold) + , m_syntheticOblique(italic) + , m_scaledFont(0) +{ +} + +FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, float size, bool bold, bool italic) + : m_context(0) + , m_font(0) + , m_size(size) + , m_syntheticBold(bold) + , m_syntheticOblique(italic) + , m_scaledFont(0) +{ + cairo_matrix_t fontMatrix; + cairo_matrix_init_scale(&fontMatrix, size, size); + cairo_matrix_t ctm; + cairo_matrix_init_identity(&ctm); + cairo_font_options_t* options = cairo_font_options_create(); + + // We force antialiasing and disable hinting to provide consistent + // typographic qualities for custom fonts on all platforms. + cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE); + cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY); + + m_scaledFont = cairo_scaled_font_create(fontFace, &fontMatrix, &ctm, options); + cairo_font_options_destroy(options); +} + +bool FontPlatformData::init() +{ + static bool initialized = false; + if (initialized) + return true; + initialized = true; + + if (!m_fontMap) + m_fontMap = pango_cairo_font_map_get_default(); + if (!m_hashTable) { + PangoFontFamily** families = 0; + int n_families = 0; + + m_hashTable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref); + + pango_font_map_list_families(m_fontMap, &families, &n_families); + + for (int family = 0; family < n_families; family++) + g_hash_table_insert(m_hashTable, + g_strdup(pango_font_family_get_name(families[family])), + g_object_ref(families[family])); + + g_free(families); + } + + return true; +} + +FontPlatformData::~FontPlatformData() +{ + if (m_font && m_font != reinterpret_cast<PangoFont*>(-1)) { + g_object_unref(m_font); + m_font = 0; + } + + if (m_context) { + g_object_unref(m_context); + m_context = 0; + } + + if (m_scaledFont) { + cairo_scaled_font_destroy(m_scaledFont); + m_scaledFont = 0; + } +} + +bool FontPlatformData::isFixedPitch() +{ + PangoFontDescription* description = pango_font_describe_with_absolute_size(m_font); + PangoFontFamily* family = reinterpret_cast<PangoFontFamily*>(g_hash_table_lookup(m_hashTable, pango_font_description_get_family(description))); + pango_font_description_free(description); + return pango_font_family_is_monospace(family); +} + +FontPlatformData& FontPlatformData::operator=(const FontPlatformData& other) +{ + // Check for self-assignment. + if (this == &other) + return *this; + + m_size = other.m_size; + m_syntheticBold = other.m_syntheticBold; + m_syntheticOblique = other.m_syntheticOblique; + + if (other.m_scaledFont) + cairo_scaled_font_reference(other.m_scaledFont); + if (m_scaledFont) + cairo_scaled_font_destroy(m_scaledFont); + m_scaledFont = other.m_scaledFont; + + if (other.m_font) + g_object_ref(other.m_font); + if (m_font) + g_object_unref(m_font); + m_font = other.m_font; + + if (other.m_context) + g_object_ref(other.m_context); + if (m_context) + g_object_unref(m_context); + m_context = other.m_context; + + return *this; +} + +FontPlatformData::FontPlatformData(const FontPlatformData& other) + : m_context(0) + , m_font(0) + , m_scaledFont(0) +{ + *this = other; +} + +bool FontPlatformData::operator==(const FontPlatformData& other) const +{ + if (m_font == other.m_font) + return true; + if (m_font == 0 || m_font == reinterpret_cast<PangoFont*>(-1) + || other.m_font == 0 || other.m_font == reinterpret_cast<PangoFont*>(-1)) + return false; + PangoFontDescription* thisDesc = pango_font_describe(m_font); + PangoFontDescription* otherDesc = pango_font_describe(other.m_font); + bool result = pango_font_description_equal(thisDesc, otherDesc); + pango_font_description_free(otherDesc); + pango_font_description_free(thisDesc); + return result; +} + +#ifndef NDEBUG +String FontPlatformData::description() const +{ + return String(); +} +#endif + +} diff --git a/Source/WebCore/platform/graphics/pango/GlyphPageTreeNodePango.cpp b/Source/WebCore/platform/graphics/pango/GlyphPageTreeNodePango.cpp new file mode 100644 index 0000000..8d0baa6 --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/GlyphPageTreeNodePango.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. + * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com + * Copyright (C) 2007 Alp Toker <alp.toker@collabora.co.uk> + * Copyright (C) 2007 Pioneer Research Center USA, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "GlyphPageTreeNode.h" + +#include "SimpleFontData.h" +#include <pango/pango-font.h> + +namespace WebCore { + +static PangoGlyph pango_font_get_glyph(PangoFont* font, PangoContext* context, gunichar wc) +{ + PangoGlyph result = 0; + gchar buffer[7]; + + gint length = g_unichar_to_utf8(wc, buffer); + g_return_val_if_fail(length, 0); + + GList* items = pango_itemize(context, buffer, 0, length, NULL, NULL); + + if (g_list_length(items) == 1) { + PangoItem* item = reinterpret_cast<PangoItem*>(items->data); + PangoFont* tmpFont = item->analysis.font; + item->analysis.font = font; + + PangoGlyphString* glyphs = pango_glyph_string_new(); + pango_shape(buffer, length, &item->analysis, glyphs); + + item->analysis.font = tmpFont; + + if (glyphs->num_glyphs == 1) + result = glyphs->glyphs[0].glyph; + else + g_warning("didn't get 1 glyph but %d", glyphs->num_glyphs); + + pango_glyph_string_free(glyphs); + } + + g_list_foreach(items, (GFunc)pango_item_free, NULL); + g_list_free(items); + + return result; +} + +bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData) +{ + // The bufferLength will be greater than the glyph page size if the buffer has Unicode supplementary characters. + // We won't support this for now. + if (bufferLength > GlyphPage::size) + return false; + + if (!fontData->platformData().m_font || fontData->platformData().m_font == reinterpret_cast<PangoFont*>(-1)) + return false; + + bool haveGlyphs = false; + for (unsigned i = 0; i < length; i++) { + Glyph glyph = pango_font_get_glyph(fontData->platformData().m_font, fontData->platformData().m_context, buffer[i]); + if (!glyph) + setGlyphDataForIndex(offset + i, 0, 0); + else { + setGlyphDataForIndex(offset + i, glyph, fontData); + haveGlyphs = true; + } + } + + return haveGlyphs; +} + +} diff --git a/Source/WebCore/platform/graphics/pango/SimpleFontDataPango.cpp b/Source/WebCore/platform/graphics/pango/SimpleFontDataPango.cpp new file mode 100644 index 0000000..d0bf836 --- /dev/null +++ b/Source/WebCore/platform/graphics/pango/SimpleFontDataPango.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com + * Copyright (C) 2007, 2008 Alp Toker <alp@atoker.com> + * Copyright (C) 2007 Holger Hans Peter Freyther + * Copyright (C) 2007 Pioneer Research Center USA, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "SimpleFontData.h" + +#include "FloatRect.h" +#include "Font.h" +#include "FontCache.h" +#include "FontDescription.h" +#include "GlyphBuffer.h" +#include <cairo.h> +#include <wtf/MathExtras.h> + +namespace WebCore { + +void SimpleFontData::platformInit() +{ + cairo_font_extents_t font_extents; + cairo_text_extents_t text_extents; + cairo_scaled_font_extents(m_platformData.m_scaledFont, &font_extents); + m_ascent = static_cast<int>(lroundf(font_extents.ascent)); + m_descent = static_cast<int>(lroundf(font_extents.descent)); + m_lineSpacing = static_cast<int>(lroundf(font_extents.height)); + // There seems to be some rounding error in cairo (or in how we + // use cairo) with some fonts, like DejaVu Sans Mono, which makes + // cairo report a height smaller than ascent + descent, which is + // wrong and confuses WebCore's layout system. Workaround this + // while we figure out what's going on. + if (m_lineSpacing < m_ascent + m_descent) + m_lineSpacing = m_ascent + m_descent; + cairo_scaled_font_text_extents(m_platformData.m_scaledFont, "x", &text_extents); + m_xHeight = text_extents.height; + cairo_scaled_font_text_extents(m_platformData.m_scaledFont, " ", &text_extents); + m_spaceWidth = static_cast<float>(text_extents.x_advance); + m_lineGap = m_lineSpacing - m_ascent - m_descent; + m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; +} + +void SimpleFontData::platformCharWidthInit() +{ + m_avgCharWidth = 0.f; + m_maxCharWidth = 0.f; + initCharWidths(); +} + +void SimpleFontData::platformDestroy() +{ +} + +SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const +{ + FontDescription desc = FontDescription(fontDescription); + desc.setSpecifiedSize(scaleFactor * fontDescription.computedSize()); + FontPlatformData platformData(desc, desc.family().family()); + return new SimpleFontData(platformData); +} + +SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const +{ + if (!m_derivedFontData) + m_derivedFontData = DerivedFontData::create(isCustomFont()); + if (!m_derivedFontData->smallCaps) + m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7); + + return m_derivedFontData->smallCaps.get(); +} + +SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const +{ + if (!m_derivedFontData) + m_derivedFontData = DerivedFontData::create(isCustomFont()); + if (!m_derivedFontData->emphasisMark) + m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5); + + return m_derivedFontData->emphasisMark.get(); +} + +bool SimpleFontData::containsCharacters(const UChar* characters, int length) const +{ + bool result = true; + + PangoCoverage* coverage = pango_font_get_coverage(m_platformData.m_font, pango_language_get_default()); + + for (int i = 0; i < length; i++) { + if (PANGO_COVERAGE_NONE == pango_coverage_get(coverage, characters[i])) { + result = false; + break; + } + } + + pango_coverage_unref(coverage); + + return result; +} + +void SimpleFontData::determinePitch() +{ + if (isCustomFont()) { + m_treatAsFixedPitch = false; + return; + } + + m_treatAsFixedPitch = m_platformData.isFixedPitch(); +} + +FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const +{ + return FloatRect(); +} + +float SimpleFontData::platformWidthForGlyph(Glyph glyph) const +{ + ASSERT(m_platformData.m_scaledFont); + + cairo_glyph_t cglyph = { glyph, 0, 0 }; + cairo_text_extents_t extents; + cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents); + + float width = (float)m_spaceWidth; + if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0) + width = (float)extents.x_advance; + + return width; +} + +} |