Home | History | Annotate | Download | only in pepper
      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.
      4 
      5 #include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"
      6 
      7 #include <windows.h>
      8 
      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"
     13 
     14 namespace content {
     15 
     16 namespace {
     17 
     18 typedef std::vector<std::string> FontFamilyList;
     19 typedef std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> FontDescList;
     20 
     21 static int CALLBACK EnumFontFamiliesProc(ENUMLOGFONTEXW* logical_font,
     22                                          NEWTEXTMETRICEXW* physical_font,
     23                                          DWORD font_type,
     24                                          LPARAM lparam) {
     25   FontFamilyList* font_families = reinterpret_cast<FontFamilyList*>(lparam);
     26   if (font_families) {
     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(UTF16ToUTF8(lf.lfFaceName));
     31       font_families->push_back(face_name);
     32     }
     33   }
     34   return 1;
     35 }
     36 
     37 static int CALLBACK EnumFontsInFamilyProc(ENUMLOGFONTEXW* logical_font,
     38                                           NEWTEXTMETRICEXW* physical_font,
     39                                           DWORD font_type,
     40                                           LPARAM lparam) {
     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 = UTF16ToUTF8(lf.lfFaceName);
     48       if (lf.lfItalic)
     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 =
     53           static_cast<PP_TrueTypeFontCharset_Dev>(lf.lfCharSet);
     54       fonts_in_family->push_back(desc);
     55     }
     56   }
     57   return 1;
     58 }
     59 
     60 }  // namespace
     61 
     62 void GetFontFamilies_SlowBlocking(FontFamilyList* font_families) {
     63   LOGFONTW logfont;
     64   memset(&logfont, 0, sizeof(logfont));
     65   logfont.lfCharSet = DEFAULT_CHARSET;
     66   base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
     67   ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamiliesProc,
     68                         (LPARAM)font_families, 0);
     69 }
     70 
     71 void GetFontsInFamily_SlowBlocking(const std::string& family,
     72                                    FontDescList* fonts_in_family) {
     73   LOGFONTW logfont;
     74   memset(&logfont, 0, sizeof(logfont));
     75   logfont.lfCharSet = DEFAULT_CHARSET;
     76   string16 family16 = UTF8ToUTF16(family);
     77   memcpy(&logfont.lfFaceName, &family16[0], sizeof(logfont.lfFaceName));
     78   base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
     79   ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontsInFamilyProc,
     80                         (LPARAM)fonts_in_family, 0);
     81 }
     82 
     83 }  // namespace content
     84