1/* 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com 4 * Copyright (C) 2007, 2008 Alp Toker <alp@atoker.com> 5 * Copyright (C) 2007 Holger Hans Peter Freyther 6 * Copyright (C) 2007 Pioneer Research Center USA, Inc. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 19 * its contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include "config.h" 35#include "SimpleFontData.h" 36 37#include "FloatRect.h" 38#include "Font.h" 39#include "FontCache.h" 40#include "FontDescription.h" 41#include "GlyphBuffer.h" 42#include <cairo.h> 43#include <wtf/MathExtras.h> 44 45namespace WebCore { 46 47void SimpleFontData::platformInit() 48{ 49 cairo_font_extents_t font_extents; 50 cairo_text_extents_t text_extents; 51 cairo_scaled_font_extents(m_platformData.m_scaledFont, &font_extents); 52 53 m_fontMetrics.setAscent(font_extents.ascent); 54 m_fontMetrics.setDescent(font_extents.descent); 55 56 // There seems to be some rounding error in cairo (or in how we 57 // use cairo) with some fonts, like DejaVu Sans Mono, which makes 58 // cairo report a height smaller than ascent + descent, which is 59 // wrong and confuses WebCore's layout system. Workaround this 60 // while we figure out what's going on. 61 float lineSpacing = font_extents.height; 62 if (lineSpacing < font_extents.ascent + font_extents.descent) 63 lineSpacing = font_extents.ascent + font_extents.descent; 64 m_fontMetrics.setLineSpacing(lroundf(lineSpacing)); 65 m_fontMetrics.setLineGap(lineSpacing - font_extents.ascent - font_extents.descent); 66 67 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, "x", &text_extents); 68 m_fontMetrics.setXHeight(text_extents.height); 69 70 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, " ", &text_extents); 71 m_spaceWidth = static_cast<float>(text_extents.x_advance); 72 73 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; 74} 75 76void SimpleFontData::platformCharWidthInit() 77{ 78 m_avgCharWidth = 0.f; 79 m_maxCharWidth = 0.f; 80 initCharWidths(); 81} 82 83void SimpleFontData::platformDestroy() 84{ 85} 86 87SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const 88{ 89 FontDescription desc = FontDescription(fontDescription); 90 desc.setSpecifiedSize(scaleFactor * fontDescription.computedSize()); 91 FontPlatformData platformData(desc, desc.family().family()); 92 return new SimpleFontData(platformData); 93} 94 95SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const 96{ 97 if (!m_derivedFontData) 98 m_derivedFontData = DerivedFontData::create(isCustomFont()); 99 if (!m_derivedFontData->smallCaps) 100 m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7); 101 102 return m_derivedFontData->smallCaps.get(); 103} 104 105SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const 106{ 107 if (!m_derivedFontData) 108 m_derivedFontData = DerivedFontData::create(isCustomFont()); 109 if (!m_derivedFontData->emphasisMark) 110 m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5); 111 112 return m_derivedFontData->emphasisMark.get(); 113} 114 115bool SimpleFontData::containsCharacters(const UChar* characters, int length) const 116{ 117 bool result = true; 118 119 PangoCoverage* coverage = pango_font_get_coverage(m_platformData.m_font, pango_language_get_default()); 120 121 for (int i = 0; i < length; i++) { 122 if (PANGO_COVERAGE_NONE == pango_coverage_get(coverage, characters[i])) { 123 result = false; 124 break; 125 } 126 } 127 128 pango_coverage_unref(coverage); 129 130 return result; 131} 132 133void SimpleFontData::determinePitch() 134{ 135 if (isCustomFont()) { 136 m_treatAsFixedPitch = false; 137 return; 138 } 139 140 m_treatAsFixedPitch = m_platformData.isFixedPitch(); 141} 142 143FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const 144{ 145 return FloatRect(); 146} 147 148float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 149{ 150 ASSERT(m_platformData.m_scaledFont); 151 152 cairo_glyph_t cglyph = { glyph, 0, 0 }; 153 cairo_text_extents_t extents; 154 cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents); 155 156 float width = (float)m_spaceWidth; 157 if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0) 158 width = (float)extents.x_advance; 159 160 return width; 161} 162 163} 164