~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/wx/wxcode/win/fontprops.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#include <wtf/MathExtras.h>
 
29
// Unicode.h needs to be included before fontprops.h for UChar* to be defined.
 
30
// FIXME: This is wrong, fontprops.h should just forward-declare UChar.
 
31
#include <wtf/unicode/Unicode.h>
 
32
 
 
33
#include "fontprops.h"
 
34
#include "math.h"
 
35
 
 
36
#include <wx/defs.h>
 
37
#include <wx/gdicmn.h>
 
38
#include <wx/wx.h>
 
39
#include "wx/msw/private.h"
 
40
 
 
41
#include <mlang.h>
 
42
#include <usp10.h>
 
43
 
 
44
inline long  my_round(double x)
 
45
{
 
46
    return (long)(x < 0 ? x - 0.5 : x + 0.5);
 
47
}
 
48
 
 
49
wxFontProperties::wxFontProperties(wxFont* font):
 
50
m_ascent(0), m_descent(0), m_lineGap(0), m_lineSpacing(0), m_xHeight(0)
 
51
{
 
52
    HDC dc = GetDC(0);
 
53
    WXHFONT hFont = font->GetHFONT();
 
54
    ::SelectObject(dc, hFont);
 
55
    if (font){
 
56
       
 
57
        int height = font->GetPointSize();
 
58
 
 
59
        TEXTMETRIC tm;
 
60
        GetTextMetrics(dc, &tm);
 
61
        m_ascent = lroundf(tm.tmAscent);
 
62
        m_descent = lroundf(tm.tmDescent);
 
63
        m_xHeight = m_ascent * 0.56f;  // Best guess for xHeight for non-Truetype fonts.
 
64
        m_lineGap = lroundf(tm.tmExternalLeading);
 
65
        m_lineSpacing = m_lineGap + m_ascent + m_descent;
 
66
    }
 
67
    RestoreDC(dc, -1);
 
68
    ReleaseDC(0, dc);
 
69
}
 
70
 
 
71
bool wxFontContainsCharacters(void* font, const UChar* characters, int length)
 
72
{
 
73
    // FIXME: Microsoft documentation seems to imply that characters can be output using a given font and DC
 
74
    // merely by testing code page intersection.  This seems suspect though.  Can't a font only partially
 
75
    // cover a given code page?
 
76
    static IMultiLanguage *multiLanguage;
 
77
    if (!multiLanguage) {
 
78
        if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
 
79
            return true;
 
80
    }
 
81
 
 
82
    static IMLangFontLink2* langFontLink;
 
83
    if (!langFontLink) {
 
84
        if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
 
85
            return true;
 
86
    }
 
87
 
 
88
    HDC dc = GetDC(0);
 
89
    
 
90
    DWORD acpCodePages;
 
91
    langFontLink->CodePageToCodePages(CP_ACP, &acpCodePages);
 
92
 
 
93
    DWORD fontCodePages;
 
94
    langFontLink->GetFontCodePages(dc, (HFONT)font, &fontCodePages);
 
95
 
 
96
    DWORD actualCodePages;
 
97
    long numCharactersProcessed;
 
98
    long offset = 0;
 
99
    while (offset < length) {
 
100
        langFontLink->GetStrCodePages(characters, length, acpCodePages, &actualCodePages, &numCharactersProcessed);
 
101
        if ((actualCodePages & fontCodePages))
 
102
            return false;
 
103
        offset += numCharactersProcessed;
 
104
    }
 
105
 
 
106
    ReleaseDC(0, dc);
 
107
 
 
108
    return true;
 
109
}
 
110
 
 
111
void GetTextExtent( const wxFont& font, const wxString& str, wxCoord *width, wxCoord *height,
 
112
                            wxCoord *descent, wxCoord *externalLeading )
 
113
{
 
114
    HDC dc = GetDC(0);
 
115
    WXHFONT hFont = font.GetHFONT();
 
116
    ::SelectObject(dc, hFont);
 
117
 
 
118
    HFONT hfontOld;
 
119
    if ( font != wxNullFont )
 
120
    {
 
121
        wxASSERT_MSG( font.Ok(), _T("invalid font in wxDC::GetTextExtent") );
 
122
 
 
123
        hfontOld = (HFONT)::SelectObject(dc, hFont);
 
124
    }
 
125
    else // don't change the font
 
126
    {
 
127
        hfontOld = 0;
 
128
    }
 
129
 
 
130
    SIZE sizeRect;
 
131
    const size_t len = str.length();
 
132
    if ( !::GetTextExtentPoint32(dc, str, len, &sizeRect) )
 
133
    {
 
134
        wxLogLastError(_T("GetTextExtentPoint32()"));
 
135
    }
 
136
 
 
137
#if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
 
138
    // the result computed by GetTextExtentPoint32() may be too small as it
 
139
    // accounts for under/overhang of the first/last character while we want
 
140
    // just the bounding rect for this string so adjust the width as needed
 
141
    // (using API not available in 2002 SDKs of WinCE)
 
142
    if ( len > 1 )
 
143
    {
 
144
        ABC width;
 
145
        const wxChar chFirst = *str.begin();
 
146
        if ( ::GetCharABCWidths(dc, chFirst, chFirst, &width) )
 
147
        {
 
148
            if ( width.abcA < 0 )
 
149
                sizeRect.cx -= width.abcA;
 
150
 
 
151
            if ( len > 1 )
 
152
            {
 
153
                const wxChar chLast = *str.rbegin();
 
154
                ::GetCharABCWidths(dc, chLast, chLast, &width);
 
155
            }
 
156
            //else: we already have the width of the last character
 
157
 
 
158
            if ( width.abcC < 0 )
 
159
                sizeRect.cx -= width.abcC;
 
160
        }
 
161
        //else: GetCharABCWidths() failed, not a TrueType font?
 
162
    }
 
163
#endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
 
164
 
 
165
    TEXTMETRIC tm;
 
166
    ::GetTextMetrics(dc, &tm);
 
167
 
 
168
    if (width)
 
169
        *width = sizeRect.cx;
 
170
    if (height)
 
171
        *height = sizeRect.cy;
 
172
    if (descent)
 
173
        *descent = tm.tmDescent;
 
174
    if (externalLeading)
 
175
        *externalLeading = tm.tmExternalLeading;
 
176
 
 
177
    if ( hfontOld )
 
178
    {
 
179
        ::SelectObject(dc, hfontOld);
 
180
    }
 
181
    
 
182
    ReleaseDC(0, dc);
 
183
}