2
* Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
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>
33
#include "fontprops.h"
37
#include <wx/gdicmn.h>
39
#include "wx/msw/private.h"
44
inline long my_round(double x)
46
return (long)(x < 0 ? x - 0.5 : x + 0.5);
49
wxFontProperties::wxFontProperties(wxFont* font):
50
m_ascent(0), m_descent(0), m_lineGap(0), m_lineSpacing(0), m_xHeight(0)
53
WXHFONT hFont = font->GetHFONT();
54
::SelectObject(dc, hFont);
57
int height = font->GetPointSize();
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;
71
bool wxFontContainsCharacters(void* font, const UChar* characters, int length)
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;
78
if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
82
static IMLangFontLink2* langFontLink;
84
if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
91
langFontLink->CodePageToCodePages(CP_ACP, &acpCodePages);
94
langFontLink->GetFontCodePages(dc, (HFONT)font, &fontCodePages);
96
DWORD actualCodePages;
97
long numCharactersProcessed;
99
while (offset < length) {
100
langFontLink->GetStrCodePages(characters, length, acpCodePages, &actualCodePages, &numCharactersProcessed);
101
if ((actualCodePages & fontCodePages))
103
offset += numCharactersProcessed;
111
void GetTextExtent( const wxFont& font, const wxString& str, wxCoord *width, wxCoord *height,
112
wxCoord *descent, wxCoord *externalLeading )
115
WXHFONT hFont = font.GetHFONT();
116
::SelectObject(dc, hFont);
119
if ( font != wxNullFont )
121
wxASSERT_MSG( font.Ok(), _T("invalid font in wxDC::GetTextExtent") );
123
hfontOld = (HFONT)::SelectObject(dc, hFont);
125
else // don't change the font
131
const size_t len = str.length();
132
if ( !::GetTextExtentPoint32(dc, str, len, &sizeRect) )
134
wxLogLastError(_T("GetTextExtentPoint32()"));
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)
145
const wxChar chFirst = *str.begin();
146
if ( ::GetCharABCWidths(dc, chFirst, chFirst, &width) )
148
if ( width.abcA < 0 )
149
sizeRect.cx -= width.abcA;
153
const wxChar chLast = *str.rbegin();
154
::GetCharABCWidths(dc, chLast, chLast, &width);
156
//else: we already have the width of the last character
158
if ( width.abcC < 0 )
159
sizeRect.cx -= width.abcC;
161
//else: GetCharABCWidths() failed, not a TrueType font?
163
#endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
166
::GetTextMetrics(dc, &tm);
169
*width = sizeRect.cx;
171
*height = sizeRect.cy;
173
*descent = tm.tmDescent;
175
*externalLeading = tm.tmExternalLeading;
179
::SelectObject(dc, hfontOld);