2 * Copyright (C) 2006 Apple Computer, 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 Holger Hans Peter Freyther
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "FloatRect.h"
38 #include "FontCache.h"
39 #include "FontDescription.h"
40 #include "GlyphBuffer.h"
42 #include <unicode/uchar.h>
43 #include <unicode/unorm.h>
44 #include <wtf/MathExtras.h>
48 void FontData::platformInit()
50 cairo_font_extents_t font_extents;
51 cairo_text_extents_t text_extents;
52 cairo_scaled_font_extents(m_font.m_scaledFont, &font_extents);
53 m_ascent = static_cast<int>(font_extents.ascent);
54 m_descent = static_cast<int>(font_extents.descent);
55 m_lineSpacing = static_cast<int>(font_extents.height);
56 cairo_scaled_font_text_extents(m_font.m_scaledFont, "x", &text_extents);
57 m_xHeight = text_extents.height;
58 cairo_scaled_font_text_extents(m_font.m_scaledFont, " ", &text_extents);
59 m_spaceWidth = static_cast<int>(text_extents.x_advance);
60 m_lineGap = m_lineSpacing - m_ascent - m_descent;
63 void FontData::platformDestroy()
65 if (m_font.m_pattern && ((FcPattern*)-1 != m_font.m_pattern))
66 FcPatternDestroy(m_font.m_pattern);
68 if (m_font.m_scaledFont)
69 cairo_scaled_font_destroy(m_font.m_scaledFont);
70 delete m_smallCapsFontData;
73 FontData* FontData::smallCapsFontData(const FontDescription& fontDescription) const
75 if (!m_smallCapsFontData) {
76 FontDescription desc = FontDescription(fontDescription);
77 desc.setSpecifiedSize(0.70f*fontDescription.computedSize());
78 const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family());
79 m_smallCapsFontData = new FontData(*pdata);
81 return m_smallCapsFontData;
84 bool FontData::containsCharacters(const UChar* characters, int length) const
86 FT_Face face = cairo_ft_scaled_font_lock_face(m_font.m_scaledFont);
91 for (unsigned i = 0; i < length; i++) {
92 if (FcFreeTypeCharIndex(face, characters[i]) == 0) {
93 cairo_ft_scaled_font_unlock_face(m_font.m_scaledFont);
98 cairo_ft_scaled_font_unlock_face(m_font.m_scaledFont);
103 void FontData::determinePitch()
105 m_treatAsFixedPitch = m_font.isFixedPitch();
108 float FontData::platformWidthForGlyph(Glyph glyph) const
110 ASSERT(m_font.m_scaledFont);
112 cairo_glyph_t cglyph = { glyph, 0, 0 };
113 cairo_text_extents_t extents;
114 cairo_scaled_font_glyph_extents(m_font.m_scaledFont, &cglyph, 1, &extents);
116 float w = (float)m_spaceWidth;
117 if (cairo_scaled_font_status(m_font.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0)
118 w = (float)extents.x_advance;
122 void FontData::setFont(cairo_t* cr) const