OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkAdvancedTypefaceMetrics.h" | 9 #include "SkAdvancedTypefaceMetrics.h" |
10 #include "SkBase64.h" | 10 #include "SkBase64.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 tchar_to_skstring(fontName.get(), familyName); | 119 tchar_to_skstring(fontName.get(), familyName); |
120 } | 120 } |
121 | 121 |
122 static void make_canonical(LOGFONT* lf) { | 122 static void make_canonical(LOGFONT* lf) { |
123 lf->lfHeight = -64; | 123 lf->lfHeight = -64; |
124 lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY; | 124 lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY; |
125 lf->lfCharSet = DEFAULT_CHARSET; | 125 lf->lfCharSet = DEFAULT_CHARSET; |
126 // lf->lfClipPrecision = 64; | 126 // lf->lfClipPrecision = 64; |
127 } | 127 } |
128 | 128 |
129 static SkTypeface::Style get_style(const LOGFONT& lf) { | 129 static SkFontStyle get_style(const LOGFONT& lf) { |
130 unsigned style = 0; | 130 return SkFontStyle(lf.lfWeight, |
131 if (lf.lfWeight >= FW_BOLD) { | 131 lf.lfWidth, |
132 style |= SkTypeface::kBold; | 132 lf.lfItalic ? SkFontStyle::kItalic_Slant : SkFontStyle::k
Upright_Slant); |
133 } | |
134 if (lf.lfItalic) { | |
135 style |= SkTypeface::kItalic; | |
136 } | |
137 return static_cast<SkTypeface::Style>(style); | |
138 } | |
139 | |
140 static void setStyle(LOGFONT* lf, SkTypeface::Style style) { | |
141 lf->lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL ; | |
142 lf->lfItalic = ((style & SkTypeface::kItalic) != 0); | |
143 } | 133 } |
144 | 134 |
145 static inline FIXED SkFixedToFIXED(SkFixed x) { | 135 static inline FIXED SkFixedToFIXED(SkFixed x) { |
146 return *(FIXED*)(&x); | 136 return *(FIXED*)(&x); |
147 } | 137 } |
148 static inline SkFixed SkFIXEDToFixed(FIXED x) { | 138 static inline SkFixed SkFIXEDToFixed(FIXED x) { |
149 return *(SkFixed*)(&x); | 139 return *(SkFixed*)(&x); |
150 } | 140 } |
151 | 141 |
152 static inline FIXED SkScalarToFIXED(SkScalar x) { | 142 static inline FIXED SkScalarToFIXED(SkScalar x) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 if (0 == otmRet) { | 200 if (0 == otmRet) { |
211 call_ensure_accessible(lf); | 201 call_ensure_accessible(lf); |
212 otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); | 202 otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
213 } | 203 } |
214 | 204 |
215 return (0 == otmRet) ? 0 : otm.otmEMSquare; | 205 return (0 == otmRet) ? 0 : otm.otmEMSquare; |
216 } | 206 } |
217 | 207 |
218 class LogFontTypeface : public SkTypeface { | 208 class LogFontTypeface : public SkTypeface { |
219 public: | 209 public: |
220 LogFontTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf,
bool serializeAsStream = false) : | 210 LogFontTypeface(const SkFontStyle& style, const LOGFONT& lf, bool serializeA
sStream) |
221 SkTypeface(style, fontID, false), fLogFont(lf), fSerializeAsStream(seria
lizeAsStream) { | 211 : SkTypeface(style, SkTypefaceCache::NewFontID(), false) |
| 212 , fLogFont(lf) |
| 213 , fSerializeAsStream(serializeAsStream) |
| 214 { |
222 | 215 |
223 // If the font has cubic outlines, it will not be rendered with ClearTyp
e. | 216 // If the font has cubic outlines, it will not be rendered with ClearTyp
e. |
224 HFONT font = CreateFontIndirect(&lf); | 217 HFONT font = CreateFontIndirect(&lf); |
225 | 218 |
226 HDC deviceContext = ::CreateCompatibleDC(NULL); | 219 HDC deviceContext = ::CreateCompatibleDC(NULL); |
227 HFONT savefont = (HFONT)SelectObject(deviceContext, font); | 220 HFONT savefont = (HFONT)SelectObject(deviceContext, font); |
228 | 221 |
229 TEXTMETRIC textMetric; | 222 TEXTMETRIC textMetric; |
230 if (0 == GetTextMetrics(deviceContext, &textMetric)) { | 223 if (0 == GetTextMetrics(deviceContext, &textMetric)) { |
231 call_ensure_accessible(lf); | 224 call_ensure_accessible(lf); |
(...skipping 16 matching lines...) Expand all Loading... |
248 // Therefore all TMPF_DEVICE will be PostScript (cubic) fonts. | 241 // Therefore all TMPF_DEVICE will be PostScript (cubic) fonts. |
249 fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) && | 242 fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) && |
250 (textMetric.tmPitchAndFamily & TMPF_DEVICE)); | 243 (textMetric.tmPitchAndFamily & TMPF_DEVICE)); |
251 } | 244 } |
252 | 245 |
253 LOGFONT fLogFont; | 246 LOGFONT fLogFont; |
254 bool fSerializeAsStream; | 247 bool fSerializeAsStream; |
255 bool fCanBeLCD; | 248 bool fCanBeLCD; |
256 | 249 |
257 static LogFontTypeface* Create(const LOGFONT& lf) { | 250 static LogFontTypeface* Create(const LOGFONT& lf) { |
258 SkTypeface::Style style = get_style(lf); | 251 return new LogFontTypeface(get_style(lf), lf, false); |
259 SkFontID fontID = SkTypefaceCache::NewFontID(); | |
260 return new LogFontTypeface(style, fontID, lf); | |
261 } | 252 } |
262 | 253 |
263 static void EnsureAccessible(const SkTypeface* face) { | 254 static void EnsureAccessible(const SkTypeface* face) { |
264 call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFo
nt); | 255 call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFo
nt); |
265 } | 256 } |
266 | 257 |
267 protected: | 258 protected: |
268 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE; | 259 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE; |
269 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK
_OVERRIDE; | 260 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK
_OVERRIDE; |
270 virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE; | 261 virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE; |
(...skipping 11 matching lines...) Expand all Loading... |
282 virtual size_t onGetTableData(SkFontTableTag, size_t offset, | 273 virtual size_t onGetTableData(SkFontTableTag, size_t offset, |
283 size_t length, void* data) const SK_OVERRIDE; | 274 size_t length, void* data) const SK_OVERRIDE; |
284 }; | 275 }; |
285 | 276 |
286 class FontMemResourceTypeface : public LogFontTypeface { | 277 class FontMemResourceTypeface : public LogFontTypeface { |
287 public: | 278 public: |
288 /** | 279 /** |
289 * The created FontMemResourceTypeface takes ownership of fontMemResource. | 280 * The created FontMemResourceTypeface takes ownership of fontMemResource. |
290 */ | 281 */ |
291 static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemReso
urce) { | 282 static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemReso
urce) { |
292 SkTypeface::Style style = get_style(lf); | 283 return new FontMemResourceTypeface(get_style(lf), lf, fontMemResource); |
293 SkFontID fontID = SkTypefaceCache::NewFontID(); | |
294 return new FontMemResourceTypeface(style, fontID, lf, fontMemResource); | |
295 } | 284 } |
296 | 285 |
297 protected: | 286 protected: |
298 virtual void weak_dispose() const SK_OVERRIDE { | 287 virtual void weak_dispose() const SK_OVERRIDE { |
299 RemoveFontMemResourceEx(fFontMemResource); | 288 RemoveFontMemResourceEx(fFontMemResource); |
300 //SkTypefaceCache::Remove(this); | 289 //SkTypefaceCache::Remove(this); |
301 INHERITED::weak_dispose(); | 290 INHERITED::weak_dispose(); |
302 } | 291 } |
303 | 292 |
304 private: | 293 private: |
305 /** | 294 /** |
306 * Takes ownership of fontMemResource. | 295 * Takes ownership of fontMemResource. |
307 */ | 296 */ |
308 FontMemResourceTypeface(SkTypeface::Style style, SkFontID fontID, const LOGF
ONT& lf, HANDLE fontMemResource) : | 297 FontMemResourceTypeface(const SkFontStyle& style, const LOGFONT& lf, HANDLE
fontMemResource) |
309 LogFontTypeface(style, fontID, lf, true), fFontMemResource(fontMemResour
ce) { | 298 : LogFontTypeface(style, lf, true), fFontMemResource(fontMemResource) |
310 } | 299 { } |
311 | 300 |
312 HANDLE fFontMemResource; | 301 HANDLE fFontMemResource; |
313 | 302 |
314 typedef LogFontTypeface INHERITED; | 303 typedef LogFontTypeface INHERITED; |
315 }; | 304 }; |
316 | 305 |
317 static const LOGFONT& get_default_font() { | 306 static const LOGFONT& get_default_font() { |
318 static LOGFONT gDefaultFont; | 307 static LOGFONT gDefaultFont; |
319 return gDefaultFont; | 308 return gDefaultFont; |
320 } | 309 } |
321 | 310 |
322 static bool FindByLogFont(SkTypeface* face, SkTypeface::Style requestedStyle, vo
id* ctx) { | 311 static bool FindByLogFont(SkTypeface* face, const SkFontStyle& requestedStyle, v
oid* ctx) { |
323 LogFontTypeface* lface = static_cast<LogFontTypeface*>(face); | 312 LogFontTypeface* lface = static_cast<LogFontTypeface*>(face); |
324 const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx); | 313 const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx); |
325 | 314 |
326 return lface && | 315 return lface && |
327 get_style(lface->fLogFont) == requestedStyle && | 316 get_style(lface->fLogFont) == requestedStyle && |
328 !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT)); | 317 !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT)); |
329 } | 318 } |
330 | 319 |
331 /** | 320 /** |
332 * This guy is public. It first searches the cache, and if a match is not found
, | 321 * This guy is public. It first searches the cache, and if a match is not found
, |
(...skipping 2117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2450 */ | 2439 */ |
2451 static int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*, | 2440 static int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*, |
2452 DWORD fontType, LPARAM builderParam) { | 2441 DWORD fontType, LPARAM builderParam) { |
2453 if (valid_logfont_for_enum(*lf)) { | 2442 if (valid_logfont_for_enum(*lf)) { |
2454 SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderPara
m; | 2443 SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderPara
m; |
2455 *array->append() = *(ENUMLOGFONTEX*)lf; | 2444 *array->append() = *(ENUMLOGFONTEX*)lf; |
2456 } | 2445 } |
2457 return 1; // non-zero means continue | 2446 return 1; // non-zero means continue |
2458 } | 2447 } |
2459 | 2448 |
2460 static SkFontStyle compute_fontstyle(const LOGFONT& lf) { | |
2461 return SkFontStyle(lf.lfWeight, SkFontStyle::kNormal_Width, | |
2462 lf.lfItalic ? SkFontStyle::kItalic_Slant | |
2463 : SkFontStyle::kUpright_Slant); | |
2464 } | |
2465 | |
2466 class SkFontStyleSetGDI : public SkFontStyleSet { | 2449 class SkFontStyleSetGDI : public SkFontStyleSet { |
2467 public: | 2450 public: |
2468 SkFontStyleSetGDI(const TCHAR familyName[]) { | 2451 SkFontStyleSetGDI(const TCHAR familyName[]) { |
2469 LOGFONT lf; | 2452 LOGFONT lf; |
2470 sk_bzero(&lf, sizeof(lf)); | 2453 sk_bzero(&lf, sizeof(lf)); |
2471 lf.lfCharSet = DEFAULT_CHARSET; | 2454 lf.lfCharSet = DEFAULT_CHARSET; |
2472 _tcscpy_s(lf.lfFaceName, familyName); | 2455 _tcscpy_s(lf.lfFaceName, familyName); |
2473 | 2456 |
2474 HDC hdc = ::CreateCompatibleDC(NULL); | 2457 HDC hdc = ::CreateCompatibleDC(NULL); |
2475 ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0); | 2458 ::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0); |
2476 ::DeleteDC(hdc); | 2459 ::DeleteDC(hdc); |
2477 } | 2460 } |
2478 | 2461 |
2479 virtual int count() SK_OVERRIDE { | 2462 virtual int count() SK_OVERRIDE { |
2480 return fArray.count(); | 2463 return fArray.count(); |
2481 } | 2464 } |
2482 | 2465 |
2483 virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName) SK_OV
ERRIDE { | 2466 virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName) SK_OV
ERRIDE { |
2484 if (fs) { | 2467 if (fs) { |
2485 *fs = compute_fontstyle(fArray[index].elfLogFont); | 2468 *fs = get_style(fArray[index].elfLogFont); |
2486 } | 2469 } |
2487 if (styleName) { | 2470 if (styleName) { |
2488 const ENUMLOGFONTEX& ref = fArray[index]; | 2471 const ENUMLOGFONTEX& ref = fArray[index]; |
2489 // For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type
in the | 2472 // For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type
in the |
2490 // non-unicode version. | 2473 // non-unicode version. |
2491 // ENUMLOGFONTEX uses BYTE | 2474 // ENUMLOGFONTEX uses BYTE |
2492 // LOGFONT uses CHAR | 2475 // LOGFONT uses CHAR |
2493 // Here we assert they that the style name is logically the same (si
ze) as | 2476 // Here we assert they that the style name is logically the same (si
ze) as |
2494 // a TCHAR, so we can use the same converter function. | 2477 // a TCHAR, so we can use the same converter function. |
2495 SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0])); | 2478 SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0])); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2578 } | 2561 } |
2579 | 2562 |
2580 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 2563 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
2581 unsigned styleBits) const SK_OVER
RIDE { | 2564 unsigned styleBits) const SK_OVER
RIDE { |
2582 LOGFONT lf; | 2565 LOGFONT lf; |
2583 if (NULL == familyName) { | 2566 if (NULL == familyName) { |
2584 lf = get_default_font(); | 2567 lf = get_default_font(); |
2585 } else { | 2568 } else { |
2586 logfont_for_name(familyName, &lf); | 2569 logfont_for_name(familyName, &lf); |
2587 } | 2570 } |
2588 setStyle(&lf, (SkTypeface::Style)styleBits); | 2571 |
| 2572 SkTypeface::Style style = (SkTypeface::Style)styleBits; |
| 2573 lf.lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL; |
| 2574 lf.lfItalic = ((style & SkTypeface::kItalic) != 0); |
2589 return SkCreateTypefaceFromLOGFONT(lf); | 2575 return SkCreateTypefaceFromLOGFONT(lf); |
2590 } | 2576 } |
2591 | 2577 |
2592 private: | 2578 private: |
2593 SkTDArray<ENUMLOGFONTEX> fLogFontArray; | 2579 SkTDArray<ENUMLOGFONTEX> fLogFontArray; |
2594 }; | 2580 }; |
2595 | 2581 |
2596 /////////////////////////////////////////////////////////////////////////////// | 2582 /////////////////////////////////////////////////////////////////////////////// |
2597 | 2583 |
2598 SkFontMgr* SkFontMgr_New_GDI() { | 2584 SkFontMgr* SkFontMgr_New_GDI() { |
2599 return SkNEW(SkFontMgrGDI); | 2585 return SkNEW(SkFontMgrGDI); |
2600 } | 2586 } |
OLD | NEW |