1 |
/******************************************************************************* |
= |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
|
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
|
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
|
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
|
7 |
* |
8 |
* Contributors: |
|
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
|
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
|
10 |
*******************************************************************************/ |
11 |
|
|
11 |
|
|
|
<> |
12 |
/******************************************************************************* |
|
|
|
13 |
* Additions/modifications to this source file by Oracle America, Inc. 2011 |
|
|
|
14 |
*******************************************************************************/ |
|
|
|
15 |
|
12 |
#include "ugl_win32.h" |
= |
16 |
#include "ugl_win32.h" |
13 |
|
|
17 |
|
14 |
#include <math.h> |
|
18 |
#include <math.h> |
15 |
#include "UGL_Win32_Display.h" |
|
19 |
#include "UGL_Win32_Display.h" |
16 |
#include "UGL_Win32_DC.h" |
|
20 |
#include "UGL_Win32_DC.h" |
17 |
|
|
21 |
|
18 |
#include "Font.h" |
|
22 |
#include "Font.h" |
19 |
|
|
23 |
|
20 |
/* |
|
24 |
/* |
21 |
* The recommended macro MulDiv is not defined |
|
25 |
* The recommended macro MulDiv is not defined |
22 |
* in WinCE so a quick implementation has to be provided |
|
26 |
* in WinCE so a quick implementation has to be provided |
23 |
*/ |
|
27 |
*/ |
24 |
#ifndef UNDER_NT |
|
28 |
#ifndef UNDER_NT |
25 |
#define MulDiv(num, numerator, denominator) ((num*numerator)/denominator) |
|
29 |
#define MulDiv(num, numerator, denominator) ((num*numerator)/denominator) |
26 |
#endif |
|
30 |
#endif |
27 |
|
|
31 |
|
28 |
//======================================================================================== |
|
32 |
//======================================================================================== |
29 |
PUGL_Font _internal_new_Font(HFONT fontHandle, UGL_Error uglError); |
|
33 |
PUGL_Font _internal_new_Font(HFONT fontHandle, UGL_Error uglError); |
30 |
//======================================================================================== |
|
34 |
//======================================================================================== |
31 |
|
|
35 |
|
32 |
void _Font_FillInFont(LPLOGFONT fontData, int height, int style) { |
|
36 |
void _Font_FillInFont(LPLOGFONT fontData, int height, int style) { |
33 |
HDC hdc = GetDC(NULL); |
|
37 |
HDC hdc = GetDC(NULL); |
34 |
|
|
38 |
|
35 |
if (IS_FLAG_SET(style, UGL_FONT_STYLE_BOLD)) fontData->lfWeight = FW_BOLD; |
|
39 |
if (IS_FLAG_SET(style, UGL_FONT_STYLE_BOLD)) fontData->lfWeight = FW_BOLD; |
36 |
else fontData->lfWeight = FW_NORMAL; |
|
40 |
else fontData->lfWeight = FW_NORMAL; |
37 |
|
|
41 |
|
38 |
if (IS_FLAG_SET(style, UGL_FONT_STYLE_ITALIC)) fontData->lfItalic = TRUE; |
|
42 |
if (IS_FLAG_SET(style, UGL_FONT_STYLE_ITALIC)) fontData->lfItalic = TRUE; |
39 |
else fontData->lfItalic = FALSE; |
|
43 |
else fontData->lfItalic = FALSE; |
|
|
<> |
44 |
|
|
|
|
45 |
if (IS_FLAG_SET(style, UGL_FONT_STYLE_UNDERLINED)) fontData->lfUnderline = TRUE; |
|
|
|
46 |
else fontData->lfUnderline = FALSE; |
40 |
|
= |
47 |
|
41 |
// We set the charset field so that |
|
48 |
// We set the charset field so that |
42 |
// wildcard searching will work properly |
|
49 |
// wildcard searching will work properly |
43 |
// out of the box |
|
50 |
// out of the box |
44 |
fontData->lfCharSet = DEFAULT_CHARSET; |
|
51 |
fontData->lfCharSet = DEFAULT_CHARSET; |
45 |
|
|
52 |
|
46 |
#ifdef _WIN32_WCE |
|
53 |
#ifdef _WIN32_WCE |
47 |
/** |
|
54 |
/** |
48 |
* MulDiv() does not seem to exist in Windows CE, regardless of documentation. |
|
55 |
* MulDiv() does not seem to exist in Windows CE, regardless of documentation. |
49 |
* Workaround is to accept the possible rounding error. |
|
56 |
* Workaround is to accept the possible rounding error. |
50 |
*/ |
|
57 |
*/ |
51 |
fontData->lfHeight = (height * GetDeviceCaps(hdc, LOGPIXELSY)) / 72; |
|
58 |
fontData->lfHeight = (height * GetDeviceCaps(hdc, LOGPIXELSY)) / 72; |
52 |
#else |
|
59 |
#else |
53 |
fontData->lfHeight = MulDiv(height, GetDeviceCaps(hdc, LOGPIXELSY), 72); |
|
60 |
fontData->lfHeight = MulDiv(height, GetDeviceCaps(hdc, LOGPIXELSY), 72); |
54 |
#endif |
|
61 |
#endif |
55 |
ReleaseDC(NULL, hdc); |
|
62 |
ReleaseDC(NULL, hdc); |
56 |
} |
|
63 |
} |
57 |
|
|
64 |
|
58 |
|
|
65 |
|
59 |
UGL_Int Font_New(UGL_Int deviceHandle, UGL_String name, UGL_Int height, UGL_Int style, UGL_Error uglError) { |
|
66 |
UGL_Int Font_New(UGL_Int deviceHandle, UGL_String name, UGL_Int height, UGL_Int style, UGL_Error uglError) { |
60 |
PUGL_Display ugl_display = UGL_DISPLAY(deviceHandle); |
|
67 |
PUGL_Display ugl_display = UGL_DISPLAY(deviceHandle); |
61 |
LOGFONT logFont; |
|
68 |
LOGFONT logFont; |
62 |
HFONT fontHandle; |
|
69 |
HFONT fontHandle; |
63 |
PUGL_Font ugl_font = NULL; |
|
70 |
PUGL_Font ugl_font = NULL; |
64 |
|
|
71 |
|
65 |
memset(&logFont, 0, sizeof(LOGFONT)); |
|
72 |
memset(&logFont, 0, sizeof(LOGFONT)); |
66 |
|
|
73 |
|
67 |
if (lstrcmpi(name, _T("fixed")) == 0) { |
|
74 |
if (lstrcmpi(name, _T("fixed")) == 0) { |
68 |
// This here because some older PPro applcations |
|
75 |
// This here because some older PPro applcations |
69 |
// depend on "fixed" resolving to something monospaced |
|
76 |
// depend on "fixed" resolving to something monospaced |
70 |
// and windows will resolve "fixed" to something |
|
77 |
// and windows will resolve "fixed" to something |
71 |
// that is not visible when displayed. |
|
78 |
// that is not visible when displayed. |
72 |
// see bugs 108764 and 108003. |
|
79 |
// see bugs 108764 and 108003. |
73 |
logFont.lfPitchAndFamily = FF_MODERN; |
|
80 |
logFont.lfPitchAndFamily = FF_MODERN; |
74 |
} else { |
|
81 |
} else { |
75 |
_tcsncpy(logFont.lfFaceName, name, LF_FACESIZE); |
|
82 |
_tcsncpy(logFont.lfFaceName, name, LF_FACESIZE); |
76 |
} |
|
83 |
} |
77 |
_Font_FillInFont(&logFont, height, style); |
|
84 |
_Font_FillInFont(&logFont, height, style); |
78 |
|
|
85 |
|
79 |
fontHandle = CreateFontIndirect(&logFont); |
|
86 |
fontHandle = CreateFontIndirect(&logFont); |
80 |
if (!fontHandle) { |
|
87 |
if (!fontHandle) { |
81 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to createfont")); |
|
88 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to createfont")); |
82 |
return 0; |
|
89 |
return 0; |
83 |
} |
|
90 |
} |
84 |
|
|
91 |
|
85 |
ugl_font = _internal_new_Font(fontHandle, uglError); |
|
92 |
ugl_font = _internal_new_Font(fontHandle, uglError); |
86 |
|
|
93 |
|
87 |
return (UGL_Int)ugl_font; |
|
94 |
return (UGL_Int)ugl_font; |
88 |
} |
|
95 |
} |
89 |
//======================================================================================== |
|
96 |
//======================================================================================== |
90 |
|
|
97 |
|
91 |
void Font_Dispose(UGL_Int handle, UGL_Error uglError) { |
|
98 |
void Font_Dispose(UGL_Int handle, UGL_Error uglError) { |
92 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
99 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
93 |
|
|
100 |
|
94 |
DeleteObject(ugl_font->font_handle); |
|
101 |
DeleteObject(ugl_font->font_handle); |
95 |
memset(ugl_font, 0, sizeof(UGL_Font)); |
|
102 |
memset(ugl_font, 0, sizeof(UGL_Font)); |
96 |
free(ugl_font); |
|
103 |
free(ugl_font); |
97 |
} |
|
104 |
} |
98 |
//======================================================================================== |
|
105 |
//======================================================================================== |
99 |
|
|
106 |
|
100 |
UGL_Int Font_GetAdvanceWidth(UGL_Int handle, UGL_Char ch, UGL_Error uglError) { |
|
107 |
UGL_Int Font_GetAdvanceWidth(UGL_Int handle, UGL_Char ch, UGL_Error uglError) { |
101 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
108 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
102 |
HDC hdc = GetDC(NULL); |
|
109 |
HDC hdc = GetDC(NULL); |
103 |
|
|
110 |
|
104 |
#ifdef _WIN32_WCE |
|
111 |
#ifdef _WIN32_WCE |
105 |
SIZE size;; |
|
112 |
SIZE size;; |
106 |
SelectObject(hdc, ugl_font->font_handle); |
|
113 |
SelectObject(hdc, ugl_font->font_handle); |
107 |
|
|
114 |
|
108 |
GetTextExtentPoint32W(hdc, &ch, 1, &size); |
|
115 |
GetTextExtentPoint32W(hdc, &ch, 1, &size); |
109 |
ReleaseDC(NULL, hdc); |
|
116 |
ReleaseDC(NULL, hdc); |
110 |
return size.cx; |
|
117 |
return size.cx; |
111 |
#else |
|
118 |
#else |
112 |
int tch = ch; |
|
119 |
int tch = ch; |
113 |
int width; |
|
120 |
int width; |
114 |
|
|
121 |
|
115 |
SelectObject(hdc, ugl_font->font_handle); |
|
122 |
SelectObject(hdc, ugl_font->font_handle); |
116 |
GetCharWidth(hdc, tch, tch, &width); |
|
123 |
GetCharWidth(hdc, tch, tch, &width); |
117 |
ReleaseDC(NULL, hdc); |
|
124 |
ReleaseDC(NULL, hdc); |
118 |
return width; |
|
125 |
return width; |
119 |
#endif |
|
126 |
#endif |
120 |
} |
|
127 |
} |
121 |
//======================================================================================== |
|
128 |
//======================================================================================== |
122 |
|
|
129 |
|
123 |
UGL_Int Font_GetAscent(UGL_Int handle, UGL_Error uglError) { |
|
130 |
UGL_Int Font_GetAscent(UGL_Int handle, UGL_Error uglError) { |
124 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
131 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
125 |
|
|
132 |
|
126 |
return ugl_font->font_metrics.tmAscent |
|
133 |
return ugl_font->font_metrics.tmAscent |
127 |
- ugl_font->font_metrics.tmInternalLeading; |
|
134 |
- ugl_font->font_metrics.tmInternalLeading; |
128 |
} |
|
135 |
} |
129 |
//======================================================================================== |
|
136 |
//======================================================================================== |
130 |
|
|
137 |
|
131 |
UGL_Int Font_GetDescent(UGL_Int handle, UGL_Error uglError) { |
|
138 |
UGL_Int Font_GetDescent(UGL_Int handle, UGL_Error uglError) { |
132 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
139 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
133 |
|
|
140 |
|
134 |
return ugl_font->font_metrics.tmDescent; |
|
141 |
return ugl_font->font_metrics.tmDescent; |
135 |
} |
|
142 |
} |
136 |
//======================================================================================== |
|
143 |
//======================================================================================== |
137 |
|
|
144 |
|
138 |
UGL_Int Font_GetHeight(UGL_Int handle, UGL_Error uglError) { |
|
145 |
UGL_Int Font_GetHeight(UGL_Int handle, UGL_Error uglError) { |
139 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
146 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
140 |
|
|
147 |
|
141 |
return ugl_font->font_metrics.tmHeight; |
|
148 |
return ugl_font->font_metrics.tmHeight; |
142 |
} |
|
149 |
} |
143 |
//======================================================================================== |
|
150 |
//======================================================================================== |
144 |
|
|
151 |
|
145 |
UGL_Int Font_GetLeading(UGL_Int handle, UGL_Error uglError) { |
|
152 |
UGL_Int Font_GetLeading(UGL_Int handle, UGL_Error uglError) { |
146 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
153 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
147 |
|
|
154 |
|
148 |
return ugl_font->font_metrics.tmInternalLeading; |
|
155 |
return ugl_font->font_metrics.tmInternalLeading; |
149 |
} |
|
156 |
} |
150 |
//======================================================================================== |
|
157 |
//======================================================================================== |
151 |
|
|
158 |
|
152 |
UGL_Int Font_StringAdvance(UGL_Int handle, UGL_String string, UGL_Error uglError){ |
|
159 |
UGL_Int Font_StringAdvance(UGL_Int handle, UGL_String string, UGL_Error uglError){ |
153 |
HDC hdc; |
|
160 |
HDC hdc; |
154 |
if (string == NULL) return 0; |
|
161 |
if (string == NULL) return 0; |
155 |
|
|
162 |
|
156 |
hdc = GetDC(NULL); |
|
163 |
hdc = GetDC(NULL); |
157 |
if (hdc != NULL) { |
|
164 |
if (hdc != NULL) { |
158 |
int advance=0; |
|
165 |
int advance=0; |
159 |
|
|
166 |
|
160 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
167 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
161 |
HGDIOBJ oldFont = SelectObject(hdc, ugl_font->font_handle); |
|
168 |
HGDIOBJ oldFont = SelectObject(hdc, ugl_font->font_handle); |
162 |
if (oldFont != NULL) { |
|
169 |
if (oldFont != NULL) { |
163 |
SIZE size; |
|
170 |
SIZE size; |
164 |
int length = lstrlen(string); |
|
171 |
int length = lstrlen(string); |
165 |
if (!GetTextExtentPoint32(hdc, string, length, &size)) { |
|
172 |
if (!GetTextExtentPoint32(hdc, string, length, &size)) { |
166 |
Win32Error_SetError(uglError, GetLastError(), _T("Failed to calculate text extent")); |
|
173 |
Win32Error_SetError(uglError, GetLastError(), _T("Failed to calculate text extent")); |
167 |
} else { |
|
174 |
} else { |
168 |
advance = size.cx; |
|
175 |
advance = size.cx; |
169 |
} |
|
176 |
} |
170 |
|
|
177 |
|
171 |
SelectObject(hdc, oldFont); |
|
178 |
SelectObject(hdc, oldFont); |
172 |
} else { |
|
179 |
} else { |
173 |
Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Failed to select font")); |
|
180 |
Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Failed to select font")); |
174 |
} |
|
181 |
} |
175 |
|
|
182 |
|
176 |
ReleaseDC(NULL, hdc); |
|
183 |
ReleaseDC(NULL, hdc); |
177 |
return advance; |
|
184 |
return advance; |
178 |
} |
|
185 |
} |
179 |
|
|
186 |
|
180 |
Win32Error_SetError(uglError, GetLastError(), _T("Failed to get screen DC")); |
|
187 |
Win32Error_SetError(uglError, GetLastError(), _T("Failed to get screen DC")); |
181 |
return 0; |
|
188 |
return 0; |
182 |
} |
|
189 |
} |
183 |
|
|
190 |
|
184 |
//======================================================================================== |
|
191 |
//======================================================================================== |
185 |
|
|
192 |
|
186 |
void Font_StringExtent(UGL_Int handle, UGL_String string, UGL_Int flags, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) { |
|
193 |
void Font_StringExtent(UGL_Int handle, UGL_String string, UGL_Int flags, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) { |
187 |
HDC hdc; |
|
194 |
HDC hdc; |
188 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
195 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
189 |
HGDIOBJ oldFont; |
|
196 |
HGDIOBJ oldFont; |
190 |
|
|
197 |
|
191 |
int uFormat = 0; |
|
198 |
int uFormat = 0; |
192 |
RECT rect; |
|
199 |
RECT rect; |
193 |
|
|
200 |
|
194 |
if (string == NULL || string[0] == '\0') { |
|
201 |
if (string == NULL || string[0] == '\0') { |
195 |
// If the string is null or the length of the string is 0 then |
|
202 |
// If the string is null or the length of the string is 0 then |
196 |
// a shortcut can be taken to avoid doing more work than needed. |
|
203 |
// a shortcut can be taken to avoid doing more work than needed. |
197 |
|
|
204 |
|
198 |
// In addition to being quicker, this shortcut is required because |
|
205 |
// In addition to being quicker, this shortcut is required because |
199 |
// DrawText does not properly handle the 0 length string case properly. |
|
206 |
// DrawText does not properly handle the 0 length string case properly. |
200 |
// The return value for DrawText is the height of the string or 0 if an |
|
207 |
// The return value for DrawText is the height of the string or 0 if an |
201 |
// error occurred, and since the height of an empty string is 0 it is not possible |
|
208 |
// error occurred, and since the height of an empty string is 0 it is not possible |
202 |
// to tell whether the method failed or completed successfully. |
|
209 |
// to tell whether the method failed or completed successfully. |
203 |
|
|
210 |
|
204 |
// One solution would be to check GetLastError() for ERROR_SUCCESS after calling |
|
211 |
// One solution would be to check GetLastError() for ERROR_SUCCESS after calling |
205 |
// the method. That does not work either becacuse the error value is not reset |
|
212 |
// the method. That does not work either becacuse the error value is not reset |
206 |
// to SUCCESS when a 0 length string is passed in. Calling SetLastError(ERROR_SUCCESS) |
|
213 |
// to SUCCESS when a 0 length string is passed in. Calling SetLastError(ERROR_SUCCESS) |
207 |
// before DrawText and checking GetLastError() afterwards will fix the problem, but all |
|
214 |
// before DrawText and checking GetLastError() afterwards will fix the problem, but all |
208 |
// that extra work is too much for a case that can be avoided so easily. |
|
215 |
// that extra work is too much for a case that can be avoided so easily. |
209 |
|
|
216 |
|
210 |
*returnWidth = 0; |
|
217 |
*returnWidth = 0; |
211 |
*returnHeight = 0; |
|
218 |
*returnHeight = 0; |
212 |
return; |
|
219 |
return; |
213 |
} |
|
220 |
} |
214 |
|
|
221 |
|
215 |
hdc = GetDC(NULL); |
|
222 |
hdc = GetDC(NULL); |
216 |
|
|
223 |
|
217 |
SetRect(&rect, 0, 0, 1, 1); |
|
224 |
SetRect(&rect, 0, 0, 1, 1); |
218 |
|
|
225 |
|
219 |
oldFont = SelectObject(hdc, ugl_font->font_handle); |
|
226 |
oldFont = SelectObject(hdc, ugl_font->font_handle); |
220 |
|
|
227 |
|
221 |
SET_FLAG(uFormat, DT_LEFT); |
|
228 |
SET_FLAG(uFormat, DT_LEFT); |
222 |
SET_FLAG(uFormat, DT_CALCRECT); |
|
229 |
SET_FLAG(uFormat, DT_CALCRECT); |
223 |
|
|
230 |
|
224 |
if (IS_FLAG_SET(flags, UGL_STRING_WRAP)) { |
|
231 |
if (IS_FLAG_SET(flags, UGL_STRING_WRAP)) { |
225 |
// String wrapping is the default for the DrawText |
|
232 |
// String wrapping is the default for the DrawText |
226 |
// API so nothing needs to be done. DT_WORDBREAK is |
|
233 |
// API so nothing needs to be done. DT_WORDBREAK is |
227 |
// not correct to be set here since that will break |
|
234 |
// not correct to be set here since that will break |
228 |
// on spaces as well as line breaks. |
|
235 |
// on spaces as well as line breaks. |
229 |
} else { |
|
236 |
} else { |
230 |
SET_FLAG(uFormat, DT_SINGLELINE); |
|
237 |
SET_FLAG(uFormat, DT_SINGLELINE); |
231 |
} |
|
238 |
} |
232 |
|
|
239 |
|
233 |
if (IS_FLAG_SET(flags, UGL_STRING_EXPAND_TAB)) SET_FLAG(uFormat, DT_EXPANDTABS); |
|
240 |
if (IS_FLAG_SET(flags, UGL_STRING_EXPAND_TAB)) SET_FLAG(uFormat, DT_EXPANDTABS); |
234 |
if (!IS_FLAG_SET(flags, UGL_STRING_MNEMONIC)) SET_FLAG(uFormat, DT_NOPREFIX); |
|
241 |
if (!IS_FLAG_SET(flags, UGL_STRING_MNEMONIC)) SET_FLAG(uFormat, DT_NOPREFIX); |
235 |
|
|
242 |
|
236 |
if (!DrawText(hdc, string, -1, &rect, uFormat)) { |
|
243 |
if (!DrawText(hdc, string, -1, &rect, uFormat)) { |
237 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to calculcate string extent")); |
|
244 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to calculcate string extent")); |
238 |
} |
|
245 |
} |
239 |
SelectObject(hdc, oldFont); |
|
246 |
SelectObject(hdc, oldFont); |
240 |
ReleaseDC(NULL, hdc); |
|
247 |
ReleaseDC(NULL, hdc); |
241 |
|
|
248 |
|
242 |
*returnWidth = rect.right; |
|
249 |
*returnWidth = rect.right; |
243 |
*returnHeight = rect.bottom; |
|
250 |
*returnHeight = rect.bottom; |
244 |
|
|
251 |
|
245 |
} |
|
252 |
} |
246 |
//======================================================================================== |
|
253 |
//======================================================================================== |
247 |
|
|
254 |
|
248 |
|
|
255 |
|
249 |
|
|
256 |
|
250 |
//======================================================================================== |
|
257 |
//======================================================================================== |
251 |
|
|
258 |
|
252 |
UGL_Int Font_GetAverageCharWidth(UGL_Int handle, UGL_Error uglError) { |
|
259 |
UGL_Int Font_GetAverageCharWidth(UGL_Int handle, UGL_Error uglError) { |
253 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
260 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
254 |
|
|
261 |
|
255 |
return ugl_font->font_metrics.tmAveCharWidth; |
|
262 |
return ugl_font->font_metrics.tmAveCharWidth; |
256 |
} |
|
263 |
} |
257 |
|
|
264 |
|
258 |
//======================================================================================== |
|
265 |
//======================================================================================== |
259 |
|
|
266 |
|
260 |
UGL_Int Font_GetStyle(UGL_Int handle, UGL_Error uglError) { |
|
267 |
UGL_Int Font_GetStyle(UGL_Int handle, UGL_Error uglError) { |
261 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
268 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
262 |
LPTEXTMETRIC tm = &ugl_font->font_metrics; |
|
269 |
LPTEXTMETRIC tm = &ugl_font->font_metrics; |
263 |
int ugl_style = 0; |
|
270 |
int ugl_style = 0; |
264 |
|
|
271 |
|
265 |
if (tm->tmItalic) ugl_style |= UGL_FONT_STYLE_ITALIC; |
|
272 |
if (tm->tmItalic) ugl_style |= UGL_FONT_STYLE_ITALIC; |
266 |
if (tm->tmWeight == FW_BOLD) ugl_style |= UGL_FONT_STYLE_BOLD; |
|
273 |
if (tm->tmWeight == FW_BOLD) ugl_style |= UGL_FONT_STYLE_BOLD; |
267 |
|
|
274 |
|
268 |
return ugl_style; |
|
275 |
return ugl_style; |
269 |
} |
|
276 |
} |
270 |
//======================================================================================== |
|
277 |
//======================================================================================== |
271 |
|
|
278 |
|
272 |
UGL_String Font_GetName(UGL_Int handle, UGL_Boolean* shouldFreeResult, UGL_Error uglError) { |
|
279 |
UGL_String Font_GetName(UGL_Int handle, UGL_Boolean* shouldFreeResult, UGL_Error uglError) { |
273 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
280 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
274 |
LPTSTR font_name=NULL; |
|
281 |
LPTSTR font_name=NULL; |
275 |
HDC hdc = GetDC(NULL); |
|
282 |
HDC hdc = GetDC(NULL); |
276 |
if (hdc != NULL) { |
|
283 |
if (hdc != NULL) { |
277 |
HGDIOBJ selectedFont; |
|
284 |
HGDIOBJ selectedFont; |
278 |
int length = 0; |
|
285 |
int length = 0; |
279 |
|
|
286 |
|
280 |
selectedFont = SelectObject(hdc, ugl_font->font_handle); |
|
287 |
selectedFont = SelectObject(hdc, ugl_font->font_handle); |
281 |
|
|
288 |
|
282 |
length = GetTextFace(hdc, 0, NULL); |
|
289 |
length = GetTextFace(hdc, 0, NULL); |
283 |
if (length != 0) { |
|
290 |
if (length != 0) { |
284 |
font_name = (LPTSTR)calloc(sizeof(TCHAR), length); |
|
291 |
font_name = (LPTSTR)calloc(sizeof(TCHAR), length); |
285 |
|
|
292 |
|
286 |
if (font_name != NULL) { |
|
293 |
if (font_name != NULL) { |
287 |
if (GetTextFace(hdc, length, font_name) == 0) { |
|
294 |
if (GetTextFace(hdc, length, font_name) == 0) { |
288 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to copy font face name into buffer")); |
|
295 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to copy font face name into buffer")); |
289 |
free(font_name); |
|
296 |
free(font_name); |
290 |
font_name = NULL; |
|
297 |
font_name = NULL; |
291 |
} else { |
|
298 |
} else { |
292 |
*shouldFreeResult = TRUE; |
|
299 |
*shouldFreeResult = TRUE; |
293 |
} |
|
300 |
} |
294 |
} else { |
|
301 |
} else { |
295 |
Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to store font face name")); |
|
302 |
Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to store font face name")); |
296 |
} |
|
303 |
} |
297 |
} else { |
|
304 |
} else { |
298 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get font face length")); |
|
305 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get font face length")); |
299 |
} |
|
306 |
} |
300 |
|
|
307 |
|
301 |
SelectObject(hdc, selectedFont); |
|
308 |
SelectObject(hdc, selectedFont); |
302 |
ReleaseDC(NULL, hdc); |
|
309 |
ReleaseDC(NULL, hdc); |
303 |
} else { |
|
310 |
} else { |
304 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get screen dc")); |
|
311 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get screen dc")); |
305 |
} |
|
312 |
} |
306 |
|
|
313 |
|
307 |
return font_name; |
|
314 |
return font_name; |
308 |
} |
|
315 |
} |
309 |
//======================================================================================== |
|
316 |
//======================================================================================== |
310 |
|
|
317 |
|
311 |
UGL_Int Font_GetSizeInPoints(UGL_Int handle, UGL_Error uglError) { |
|
318 |
UGL_Int Font_GetSizeInPoints(UGL_Int handle, UGL_Error uglError) { |
312 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
|
319 |
PUGL_Font ugl_font = (PUGL_Font)handle; |
313 |
|
|
320 |
|
314 |
HDC hDC = GetDC(NULL); |
|
321 |
HDC hDC = GetDC(NULL); |
315 |
if (hDC != NULL) { |
|
322 |
if (hDC != NULL) { |
316 |
int size=MulDiv(ugl_font->font_metrics.tmHeight, 72, GetDeviceCaps(hDC, LOGPIXELSY)); |
|
323 |
int size=MulDiv(ugl_font->font_metrics.tmHeight, 72, GetDeviceCaps(hDC, LOGPIXELSY)); |
317 |
ReleaseDC(NULL, hDC); |
|
324 |
ReleaseDC(NULL, hDC); |
318 |
return size; |
|
325 |
return size; |
319 |
} else { |
|
326 |
} else { |
320 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get screen dc")); |
|
327 |
Win32Error_SetError(uglError, GetLastError(), _T("Unable to get screen dc")); |
321 |
return 0; |
|
328 |
return 0; |
322 |
} |
|
329 |
} |
323 |
} |
|
330 |
} |
324 |
//======================================================================================== |
|
331 |
//======================================================================================== |
325 |
|
|
332 |
|
326 |
PUGL_Font _internal_new_Font(HFONT fontHandle, UGL_Error uglError) { |
|
333 |
PUGL_Font _internal_new_Font(HFONT fontHandle, UGL_Error uglError) { |
327 |
PUGL_Font ugl_font = NULL; |
|
334 |
PUGL_Font ugl_font = NULL; |
328 |
HDC hdc = GetDC(NULL); |
|
335 |
HDC hdc = GetDC(NULL); |
329 |
HGDIOBJ selectedFont; |
|
336 |
HGDIOBJ selectedFont; |
330 |
|
|
337 |
|
331 |
|
|
338 |
|
332 |
ugl_font = (PUGL_Font)calloc(sizeof(UGL_Font), 1); |
|
339 |
ugl_font = (PUGL_Font)calloc(sizeof(UGL_Font), 1); |
333 |
if (ugl_font == NULL) { |
|
340 |
if (ugl_font == NULL) { |
334 |
Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to allocate font")); |
|
341 |
Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to allocate font")); |
335 |
return 0; |
|
342 |
return 0; |
336 |
} |
|
343 |
} |
337 |
ugl_font->font_handle = fontHandle; |
|
344 |
ugl_font->font_handle = fontHandle; |
338 |
|
|
345 |
|
339 |
selectedFont = SelectObject(hdc, fontHandle); |
|
346 |
selectedFont = SelectObject(hdc, fontHandle); |
340 |
// caches text metric information here while we have a valid dc |
|
347 |
// caches text metric information here while we have a valid dc |
341 |
GetTextMetrics(hdc,&(ugl_font->font_metrics)); |
|
348 |
GetTextMetrics(hdc,&(ugl_font->font_metrics)); |
342 |
|
|
349 |
|
343 |
SelectObject(hdc, selectedFont); |
|
350 |
SelectObject(hdc, selectedFont); |
344 |
ReleaseDC(NULL, hdc); |
|
351 |
ReleaseDC(NULL, hdc); |
345 |
|
|
352 |
|
346 |
return ugl_font; |
|
353 |
return ugl_font; |
347 |
} |
|
354 |
} |
348 |
//======================================================================================== |
|
355 |
//======================================================================================== |