1/*
2 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3 * Copyright (C) 2009 Maxime Simon <simon.maxime@gmail.com> All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "SimpleFontData.h"
32
33#include "FloatRect.h"
34#include "FontCache.h"
35#include "FontDescription.h"
36#include "NotImplemented.h"
37#include "TextEncoding.h"
38#include <wtf/text/CString.h>
39
40
41namespace WebCore {
42
43void SimpleFontData::platformInit()
44{
45    const BFont* font = m_platformData.font();
46    if (!font)
47        return;
48
49    font_height height;
50    font->GetHeight(&height);
51    m_fontMetrics.setAscent(height.ascent);
52    m_fontMetrics.setDescent(height.descent);
53    m_fontMetrics.setXHeight(height.ascent * 0.56f); // Hack taken from the win port.
54    m_fontMetrics.setLineGap(height.leading);
55    m_fontMetrics.setLineSpacing(lroundf(height.ascent) + lroundf(height.descent) + lroundf(height.leading));
56}
57
58void SimpleFontData::platformCharWidthInit()
59{
60    m_avgCharWidth = 0.f;
61    m_maxCharWidth = 0.f;
62    initCharWidths();
63}
64
65void SimpleFontData::platformDestroy()
66{
67}
68
69SimpleFontData* SimpleFontData::scaledFontData(const FontDescription& fontDescription, float scaleFactor) const
70{
71    FontDescription desc = FontDescription(fontDescription);
72    desc.setSpecifiedSize(scaleFactor * fontDescription.computedSize());
73    FontPlatformData fontPlatformData(desc, desc.family().family());
74    return new SimpleFontData(fontPlatformData, isCustomFont(), false);
75}
76
77SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
78{
79    if (!m_derivedFontData)
80        m_derivedFontData = DerivedFontData::create(isCustomFont());
81    if (!m_derivedFontData->smallCaps)
82        m_derivedFontData->smallCaps = scaledFontData(fontDescription, .7);
83
84    return m_derivedFontData->smallCaps.get();
85}
86
87SimpleFontData* SimpleFontData::emphasisMarkFontData(const FontDescription& fontDescription) const
88{
89    if (!m_derivedFontData)
90        m_derivedFontData = DerivedFontData::create(isCustomFont());
91    if (!m_derivedFontData->emphasisMark)
92        m_derivedFontData->emphasisMark = scaledFontData(fontDescription, .5);
93
94    return m_derivedFontData->emphasisMark.get();
95}
96
97bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
98{
99    // FIXME: We will need to implement this to load non-ASCII encoding sites
100    return true;
101}
102
103void SimpleFontData::determinePitch()
104{
105    m_treatAsFixedPitch = m_platformData.font() && m_platformData.font()->IsFixed();
106}
107
108GlyphMetrics SimpleFontData::platformMetricsForGlyph(Glyph glyph, GlyphMetricsMode) const
109{
110    GlyphMetrics metrics;
111    if (!m_platformData.font())
112        return metrics;
113
114    CString encoded = UTF8Encoding().encode(static_cast<UChar*>(&glyph), 1,
115                                            URLEncodedEntitiesForUnencodables);
116    float escapements[1];
117    m_platformData.font()->GetEscapements(encoded.data(), 1, escapements);
118    metrics.horizontalAdvance = escapements[0] * m_platformData.font()->Size();
119    return metrics;
120}
121
122} // namespace WebCore
123
124