1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/win/scoped_hdc.h"
11 #include "ppapi/c/dev/ppb_truetype_font_dev.h"
12 #include "ppapi/proxy/serialized_structs.h"
18 typedef std::vector<std::string> FontFamilyList;
19 typedef std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> FontDescList;
21 static int CALLBACK EnumFontFamiliesProc(ENUMLOGFONTEXW* logical_font,
22 NEWTEXTMETRICEXW* physical_font,
25 FontFamilyList* font_families = reinterpret_cast<FontFamilyList*>(lparam);
27 const LOGFONTW& lf = logical_font->elfLogFont;
28 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@' &&
29 lf.lfOutPrecision == OUT_STROKE_PRECIS) { // Outline fonts only.
30 std::string face_name(base::UTF16ToUTF8(lf.lfFaceName));
31 font_families->push_back(face_name);
37 static int CALLBACK EnumFontsInFamilyProc(ENUMLOGFONTEXW* logical_font,
38 NEWTEXTMETRICEXW* physical_font,
41 FontDescList* fonts_in_family = reinterpret_cast<FontDescList*>(lparam);
42 if (fonts_in_family) {
43 const LOGFONTW& lf = logical_font->elfLogFont;
44 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@' &&
45 lf.lfOutPrecision == OUT_STROKE_PRECIS) { // Outline fonts only.
46 ppapi::proxy::SerializedTrueTypeFontDesc desc;
47 desc.family = base::UTF16ToUTF8(lf.lfFaceName);
49 desc.style = PP_TRUETYPEFONTSTYLE_ITALIC;
50 desc.weight = static_cast<PP_TrueTypeFontWeight_Dev>(lf.lfWeight);
51 desc.width = PP_TRUETYPEFONTWIDTH_NORMAL; // TODO(bbudge) support widths.
52 desc.charset = static_cast<PP_TrueTypeFontCharset_Dev>(lf.lfCharSet);
53 fonts_in_family->push_back(desc);
61 void GetFontFamilies_SlowBlocking(FontFamilyList* font_families) {
63 memset(&logfont, 0, sizeof(logfont));
64 logfont.lfCharSet = DEFAULT_CHARSET;
65 base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
66 ::EnumFontFamiliesExW(hdc.Get(),
68 (FONTENUMPROCW) & EnumFontFamiliesProc,
69 (LPARAM)font_families,
73 void GetFontsInFamily_SlowBlocking(const std::string& family,
74 FontDescList* fonts_in_family) {
76 memset(&logfont, 0, sizeof(logfont));
77 logfont.lfCharSet = DEFAULT_CHARSET;
78 base::string16 family16 = base::UTF8ToUTF16(family);
79 memcpy(&logfont.lfFaceName, &family16[0], sizeof(logfont.lfFaceName));
80 base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
81 ::EnumFontFamiliesExW(hdc.Get(),
83 (FONTENUMPROCW) & EnumFontsInFamilyProc,
84 (LPARAM)fonts_in_family,
88 } // namespace content