summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/pepper/pepper_truetype_font_list_win.cc
blob: e5cdcc6b01fb92d832bc7eaba81ad034de25e313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"

#include <windows.h>

#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_hdc.h"
#include "ppapi/c/dev/ppb_truetype_font_dev.h"
#include "ppapi/proxy/serialized_structs.h"

namespace content {

namespace {

typedef std::vector<std::string> FontFamilyList;
typedef std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> FontDescList;

static int CALLBACK EnumFontFamiliesProc(ENUMLOGFONTEXW* logical_font,
                                         NEWTEXTMETRICEXW* physical_font,
                                         DWORD font_type,
                                         LPARAM lparam) {
  FontFamilyList* font_families = reinterpret_cast<FontFamilyList*>(lparam);
  if (font_families) {
    const LOGFONTW& lf = logical_font->elfLogFont;
    if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@' &&
        lf.lfOutPrecision == OUT_STROKE_PRECIS) {  // Outline fonts only.
      std::string face_name(UTF16ToUTF8(lf.lfFaceName));
      font_families->push_back(face_name);
    }
  }
  return 1;
}

static int CALLBACK EnumFontsInFamilyProc(ENUMLOGFONTEXW* logical_font,
                                          NEWTEXTMETRICEXW* physical_font,
                                          DWORD font_type,
                                          LPARAM lparam) {
  FontDescList* fonts_in_family = reinterpret_cast<FontDescList*>(lparam);
  if (fonts_in_family) {
    const LOGFONTW& lf = logical_font->elfLogFont;
    if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@' &&
        lf.lfOutPrecision == OUT_STROKE_PRECIS) {  // Outline fonts only.
      ppapi::proxy::SerializedTrueTypeFontDesc desc;
      desc.family = UTF16ToUTF8(lf.lfFaceName);
      if (lf.lfItalic)
        desc.style = PP_TRUETYPEFONTSTYLE_ITALIC;
      desc.weight = static_cast<PP_TrueTypeFontWeight_Dev>(lf.lfWeight);
      desc.width = PP_TRUETYPEFONTWIDTH_NORMAL;  // TODO(bbudge) support widths.
      desc.charset =
          static_cast<PP_TrueTypeFontCharset_Dev>(lf.lfCharSet);
      fonts_in_family->push_back(desc);
    }
  }
  return 1;
}

}  // namespace

void GetFontFamilies_SlowBlocking(FontFamilyList* font_families) {
  LOGFONTW logfont;
  memset(&logfont, 0, sizeof(logfont));
  logfont.lfCharSet = DEFAULT_CHARSET;
  base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
  ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamiliesProc,
                        (LPARAM)font_families, 0);
}

void GetFontsInFamily_SlowBlocking(const std::string& family,
                                   FontDescList* fonts_in_family) {
  LOGFONTW logfont;
  memset(&logfont, 0, sizeof(logfont));
  logfont.lfCharSet = DEFAULT_CHARSET;
  string16 family16 = UTF8ToUTF16(family);
  memcpy(&logfont.lfFaceName, &family16[0], sizeof(logfont.lfFaceName));
  base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
  ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontsInFamilyProc,
                        (LPARAM)fonts_in_family, 0);
}

}  // namespace content