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 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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.
20 *
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.
31 */
32
33 #include "config.h"
34 #include "SimpleFontData.h"
35
36 #include "FloatRect.h"
37 #include "Font.h"
38 #include "FontCache.h"
39 #include "FontDescription.h"
40 #include "GlyphBuffer.h"
41 #include <cairo-ft.h>
42 #include <cairo.h>
43 #include <fontconfig/fcfreetype.h>
44 #include <wtf/MathExtras.h>
45
46 namespace WebCore {
47
platformInit()48 void SimpleFontData::platformInit()
49 {
50 cairo_font_extents_t font_extents;
51 cairo_text_extents_t text_extents;
52 cairo_scaled_font_extents(m_platformData.scaledFont(), &font_extents);
53
54 m_fontMetrics.setAscent(font_extents.ascent);
55 m_fontMetrics.setDescent(font_extents.descent);
56
57 // There seems to be some rounding error in cairo (or in how we
58 // use cairo) with some fonts, like DejaVu Sans Mono, which makes
59 // cairo report a height smaller than ascent + descent, which is
60 // wrong and confuses WebCore's layout system. Workaround this
61 // while we figure out what's going on.
62 float lineSpacing = font_extents.height;
63 if (lineSpacing < font_extents.ascent + font_extents.descent)
64 lineSpacing = font_extents.ascent + font_extents.descent;
65
66 m_fontMetrics.setLineSpacing(lroundf(lineSpacing));
67 m_fontMetrics.setLineGap(lineSpacing - font_extents.ascent - font_extents.descent);
68
69 cairo_scaled_font_text_extents(m_platformData.scaledFont(), "x", &text_extents);
70 m_fontMetrics.setXHeight(text_extents.height);
71
72 cairo_scaled_font_text_extents(m_platformData.scaledFont(), " ", &text_extents);
73 m_spaceWidth = static_cast<float>(text_extents.x_advance);
74
75 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
76 }
77
platformCharWidthInit()78 void SimpleFontData::platformCharWidthInit()
79 {
80 m_avgCharWidth = 0.f;
81 m_maxCharWidth = 0.f;
82 initCharWidths();
83 }
84
platformDestroy()85 void SimpleFontData::platformDestroy()
86 {
87 }
88
scaledFontData(const FontDescription & fontDescription,float scaleFactor) const89 SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const
90 {
91 return new SimpleFontData(FontPlatformData(cairo_scaled_font_get_font_face(m_platformData.scaledFont()),
92 scaleFactor * fontDescription.computedSize(), m_platformData.syntheticBold(), m_platformData.syntheticOblique()),
93 isCustomFont(), false);
94 }
95
smallCapsFontData(const FontDescription & fontDescription) const96 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
97 {
98 if (!m_derivedFontData)
99 m_derivedFontData = DerivedFontData::create(isCustomFont());
100 // FIXME: I think we want to ask FontConfig for the right font again.
101 if (!m_derivedFontData->smallCaps)
102 m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7);
103
104 return m_derivedFontData->smallCaps.get();
105 }
106
emphasisMarkFontData(const FontDescription & fontDescription) const107 SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
108 {
109 if (!m_derivedFontData)
110 m_derivedFontData = DerivedFontData::create(isCustomFont());
111 if (!m_derivedFontData->emphasisMark)
112 m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5);
113
114 return m_derivedFontData->emphasisMark.get();
115 }
116
containsCharacters(const UChar * characters,int length) const117 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
118 {
119 FT_Face face = cairo_ft_scaled_font_lock_face(m_platformData.scaledFont());
120
121 if (!face)
122 return false;
123
124 for (int i = 0; i < length; i++) {
125 if (FcFreeTypeCharIndex(face, characters[i]) == 0) {
126 cairo_ft_scaled_font_unlock_face(m_platformData.scaledFont());
127 return false;
128 }
129 }
130
131 cairo_ft_scaled_font_unlock_face(m_platformData.scaledFont());
132
133 return true;
134 }
135
determinePitch()136 void SimpleFontData::determinePitch()
137 {
138 m_treatAsFixedPitch = m_platformData.isFixedPitch();
139 }
140
platformBoundsForGlyph(Glyph) const141 FloatRect SimpleFontData::platformBoundsForGlyph(Glyph) const
142 {
143 return FloatRect();
144 }
145
platformWidthForGlyph(Glyph glyph) const146 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
147 {
148 ASSERT(m_platformData.scaledFont());
149
150 cairo_glyph_t cglyph = { glyph, 0, 0 };
151 cairo_text_extents_t extents;
152 cairo_scaled_font_glyph_extents(m_platformData.scaledFont(), &cglyph, 1, &extents);
153
154 float w = (float)m_spaceWidth;
155 if (cairo_scaled_font_status(m_platformData.scaledFont()) == CAIRO_STATUS_SUCCESS && extents.x_advance)
156 w = (float)extents.x_advance;
157
158 return w;
159 }
160
161 }
162