summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/graphics/win/FontPlatformDataWin.cpp
blob: f209dbfa1e6c623ad57ed41ec897abdbee20dd2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * This file is part of the internal font implementation.  It should not be included by anyone other than
 * FontMac.cpp, FontWin.cpp and Font.cpp.
 *
 * Copyright (C) 2006, 2007, 2008 Apple Inc.
 * Copyright (C) 2008 Brent Fulgham
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "FontPlatformData.h"

#include "PlatformString.h"
#include "StringHash.h"
#include <wtf/HashMap.h>
#include <wtf/RetainPtr.h>
#include <wtf/Vector.h>

using std::min;

namespace WebCore {

static const int Bold = (1 << 0);
static const int Italic = (1 << 1);
static const int BoldOblique = (1 << 2);

static int CALLBACK enumStylesCallback(const LOGFONT* logFont, const TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
{
    int *style = reinterpret_cast<int*>(lParam);

    // FIXME: In order to accommodate Lucida we go ahead and consider a weight of 600 to be bold.
    // This does mean we'll consider demibold and semibold fonts on windows to also be bold.  This
    // is rare enough that it seems like an ok hack for now.
    if (logFont->lfWeight >= 600) {
        if (logFont->lfItalic)
            *style |= BoldOblique;
        else
            *style |= Bold;
    } else if (logFont->lfItalic)
        *style |= Italic;

    return 1;
}

FontPlatformData::FontPlatformData(HFONT font, float size, bool bold, bool oblique, bool useGDI)
    : m_font(font)
    , m_size(size)
#if PLATFORM(CG)
    , m_cgFont(0)
#elif PLATFORM(CAIRO)
    , m_fontFace(0)
    , m_scaledFont(0)
#endif
    , m_syntheticBold(false)
    , m_syntheticOblique(false)
    , m_useGDI(useGDI)
{
    HDC hdc = GetDC(0);
    SaveDC(hdc);
    
    SelectObject(hdc, font);
    UINT bufferSize = GetOutlineTextMetrics(hdc, 0, NULL);

    ASSERT_WITH_MESSAGE(bufferSize != 0, "Bitmap fonts not supported with CoreGraphics.");

    if (bufferSize != 0) {
        OUTLINETEXTMETRICW* metrics = (OUTLINETEXTMETRICW*)malloc(bufferSize);

        GetOutlineTextMetricsW(hdc, bufferSize, metrics);
        WCHAR* faceName = (WCHAR*)((uintptr_t)metrics + (uintptr_t)metrics->otmpFaceName);

        if (!useGDI && (bold || oblique)) {
            LOGFONT logFont;

            int len = min((int)wcslen(faceName), LF_FACESIZE - 1);
            memcpy(logFont.lfFaceName, faceName, len * sizeof(WORD));
            logFont.lfFaceName[len] = '\0';
            logFont.lfCharSet = metrics->otmTextMetrics.tmCharSet;
            logFont.lfPitchAndFamily = 0;

            int styles = 0;
            EnumFontFamiliesEx(hdc, &logFont, enumStylesCallback, reinterpret_cast<LPARAM>(&styles), 0);

            // Check if we need to synthesize bold or oblique. The rule that complicates things here
            // is that if the requested font is bold and oblique, and both a bold font and an oblique font
            // exist, the bold font should be used, and oblique synthesized.
            if (bold && oblique) {
                if (styles == 0) {
                    m_syntheticBold = true;
                    m_syntheticOblique = true;
                } else if (styles & Bold)
                    m_syntheticOblique = true;
                else if (styles & Italic)
                    m_syntheticBold = true;
            } else if (bold && (!(styles & Bold)))
                    m_syntheticBold = true;
              else if (oblique && !(styles & Italic))
                    m_syntheticOblique = true;
        }

        platformDataInit(font, size, hdc, faceName);

        free(metrics);
    }

    RestoreDC(hdc, -1);
    ReleaseDC(0, hdc);
}

FontPlatformData::FontPlatformData(float size, bool bold, bool oblique)
    : m_font(0)
    , m_size(size)
#if PLATFORM(CG)
    , m_cgFont(0)
#elif PLATFORM(CAIRO)
    , m_fontFace(0)
    , m_scaledFont(0)
#endif
    , m_syntheticBold(bold)
    , m_syntheticOblique(oblique)
    , m_useGDI(false)
{
}

FontPlatformData::~FontPlatformData()
{
}

}