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(base::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 = base::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 = static_cast<PP_TrueTypeFontCharset_Dev>(lf.lfCharSet);
     53       fonts_in_family->push_back(desc);
     54     }
     55   }
     56   return 1;
     57 }
     58 
     59 }  // namespace
     60 
     61 void GetFontFamilies_SlowBlocking(FontFamilyList* font_families) {
     62   LOGFONTW logfont;
     63   memset(&logfont, 0, sizeof(logfont));
     64   logfont.lfCharSet = DEFAULT_CHARSET;
     65   base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
     66   ::EnumFontFamiliesExW(hdc.Get(),
     67                         &logfont,
     68                         (FONTENUMPROCW) & EnumFontFamiliesProc,
     69                         (LPARAM)font_families,
     70                         0);
     71 }
     72 
     73 void GetFontsInFamily_SlowBlocking(const std::string& family,
     74                                    FontDescList* fonts_in_family) {
     75   LOGFONTW logfont;
     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(),
     82                         &logfont,
     83                         (FONTENUMPROCW) & EnumFontsInFamilyProc,
     84                         (LPARAM)fonts_in_family,
     85                         0);
     86 }
     87 
     88 }  // namespace content
     89