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.h>
42 #include <unicode/uchar.h>
43 #include <unicode/unorm.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.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 // 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 if (m_lineSpacing < m_ascent + m_descent)
62 m_lineSpacing = m_ascent + m_descent;
63 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, "x", &text_extents);
64 m_xHeight = text_extents.height;
65 cairo_scaled_font_text_extents(m_platformData.m_scaledFont, " ", &text_extents);
66 m_spaceWidth = static_cast<int>(text_extents.x_advance);
67 m_lineGap = m_lineSpacing - m_ascent - m_descent;
68 m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
69 }
70
platformCharWidthInit()71 void SimpleFontData::platformCharWidthInit()
72 {
73 m_avgCharWidth = 0.f;
74 m_maxCharWidth = 0.f;
75 initCharWidths();
76 }
77
platformDestroy()78 void SimpleFontData::platformDestroy()
79 {
80 delete m_smallCapsFontData;
81 m_smallCapsFontData = 0;
82 }
83
smallCapsFontData(const FontDescription & fontDescription) const84 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
85 {
86 if (!m_smallCapsFontData) {
87 FontDescription desc = FontDescription(fontDescription);
88 desc.setComputedSize(0.70f*fontDescription.computedSize());
89 const FontPlatformData* pdata = new FontPlatformData(desc, desc.family().family());
90 m_smallCapsFontData = new SimpleFontData(*pdata);
91 }
92 return m_smallCapsFontData;
93 }
94
containsCharacters(const UChar * characters,int length) const95 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
96 {
97 FT_Face face = cairo_ft_scaled_font_lock_face(m_platformData.m_scaledFont);
98
99 if (!face)
100 return false;
101
102 for (int i = 0; i < length; i++) {
103 if (FcFreeTypeCharIndex(face, characters[i]) == 0) {
104 cairo_ft_scaled_font_unlock_face(m_platformData.m_scaledFont);
105 return false;
106 }
107 }
108
109 cairo_ft_scaled_font_unlock_face(m_platformData.m_scaledFont);
110
111 return true;
112 }
113
determinePitch()114 void SimpleFontData::determinePitch()
115 {
116 m_treatAsFixedPitch = m_platformData.isFixedPitch();
117 }
118
platformWidthForGlyph(Glyph glyph) const119 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
120 {
121 ASSERT(m_platformData.m_scaledFont);
122
123 cairo_glyph_t cglyph = { glyph, 0, 0 };
124 cairo_text_extents_t extents;
125 cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents);
126
127 float w = (float)m_spaceWidth;
128 if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0)
129 w = (float)extents.x_advance;
130 return w;
131 }
132
setFont(cairo_t * cr) const133 void SimpleFontData::setFont(cairo_t* cr) const
134 {
135 ASSERT(cr);
136 m_platformData.setFont(cr);
137 }
138
139 }
140