1 /* 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2006 Michael Emmel mike.emmel (at) gmail.com 4 * Copyright (C) 2007, 2008 Alp Toker <alp (at) 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 45 namespace WebCore { 46 47 void 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 m_ascent = static_cast<int>(lroundf(font_extents.ascent)); 53 m_descent = static_cast<int>(lroundf(font_extents.descent)); 54 m_lineSpacing = static_cast<int>(lroundf(font_extents.height)); 55 // There seems to be some rounding error in cairo (or in how we 56 // use cairo) with some fonts, like DejaVu Sans Mono, which makes 57 // cairo report a height smaller than ascent + descent, which is 58 // wrong and confuses WebCore's layout system. Workaround this 59 // while we figure out what's going on. 60 if (m_lineSpacing < m_ascent + m_descent) 61 m_lineSpacing = m_ascent + m_descent; 62 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, "x", &text_extents); 63 m_xHeight = text_extents.height; 64 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, " ", &text_extents); 65 m_spaceWidth = static_cast<float>(text_extents.x_advance); 66 m_lineGap = m_lineSpacing - m_ascent - m_descent; 67 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; 68 } 69 70 void SimpleFontData::platformCharWidthInit() 71 { 72 m_avgCharWidth = 0.f; 73 m_maxCharWidth = 0.f; 74 initCharWidths(); 75 } 76 77 void SimpleFontData::platformDestroy() 78 { 79 delete m_smallCapsFontData; 80 m_smallCapsFontData = 0; 81 } 82 83 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const 84 { 85 if (!m_smallCapsFontData) { 86 FontDescription desc = FontDescription(fontDescription); 87 desc.setSpecifiedSize(0.70f*fontDescription.computedSize()); 88 const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family()); 89 m_smallCapsFontData = new SimpleFontData(*pdata); 90 } 91 return m_smallCapsFontData; 92 } 93 94 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const 95 { 96 bool result = true; 97 98 PangoCoverage* coverage = pango_font_get_coverage(m_platformData.m_font, pango_language_get_default()); 99 100 for (int i = 0; i < length; i++) { 101 if (PANGO_COVERAGE_NONE == pango_coverage_get(coverage, characters[i])) { 102 result = false; 103 break; 104 } 105 } 106 107 pango_coverage_unref(coverage); 108 109 return result; 110 } 111 112 void SimpleFontData::determinePitch() 113 { 114 if (isCustomFont()) { 115 m_treatAsFixedPitch = false; 116 return; 117 } 118 119 m_treatAsFixedPitch = m_platformData.isFixedPitch(); 120 } 121 122 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const 123 { 124 ASSERT(m_platformData.m_scaledFont); 125 126 cairo_glyph_t cglyph = { glyph, 0, 0 }; 127 cairo_text_extents_t extents; 128 cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents); 129 130 float w = (float)m_spaceWidth; 131 if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0) 132 w = (float)extents.x_advance; 133 return w; 134 } 135 136 } 137