summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/pepper/pepper_truetype_font_list_mac.mm
blob: 761572305abbf9c3e1a235b7988dc7fd00967639 (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
// 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"

#import <Cocoa/Cocoa.h>

#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/strings/sys_string_conversions.h"
#include "ppapi/c/dev/ppb_truetype_font_dev.h"
#include "ppapi/proxy/serialized_structs.h"

namespace content {

namespace {

// Table to map AppKit weights to Pepper ones.
const PP_TrueTypeFontWeight_Dev kPepperFontWeights[] = {
    PP_TRUETYPEFONTWEIGHT_THIN,         // 0 is the minimum AppKit weight.
    PP_TRUETYPEFONTWEIGHT_ULTRALIGHT,
    PP_TRUETYPEFONTWEIGHT_ULTRALIGHT,
    PP_TRUETYPEFONTWEIGHT_LIGHT,
    PP_TRUETYPEFONTWEIGHT_LIGHT,
    PP_TRUETYPEFONTWEIGHT_NORMAL,       // 5 is a 'normal' AppKit weight.
    PP_TRUETYPEFONTWEIGHT_MEDIUM,
    PP_TRUETYPEFONTWEIGHT_MEDIUM,
    PP_TRUETYPEFONTWEIGHT_SEMIBOLD,
    PP_TRUETYPEFONTWEIGHT_BOLD,         // 9 is a 'bold' AppKit weight.
    PP_TRUETYPEFONTWEIGHT_ULTRABOLD,
    PP_TRUETYPEFONTWEIGHT_HEAVY,
};
const NSInteger kPepperFontWeightsLength = arraysize(kPepperFontWeights);

}  // namespace

void GetFontFamilies_SlowBlocking(std::vector<std::string>* font_families) {
  base::mac::ScopedNSAutoreleasePool autorelease_pool;
  NSFontManager* fontManager = [[[NSFontManager alloc] init] autorelease];
  NSArray* fonts = [fontManager availableFontFamilies];
  font_families->reserve([fonts count]);
  for (NSString* family_name in fonts)
    font_families->push_back(base::SysNSStringToUTF8(family_name));
}

void GetFontsInFamily_SlowBlocking(
    const std::string& family,
    std::vector<ppapi::proxy::SerializedTrueTypeFontDesc>* fonts_in_family) {
  base::mac::ScopedNSAutoreleasePool autorelease_pool;
  NSFontManager* fontManager = [[[NSFontManager alloc] init] autorelease];
  NSString* ns_family = base::SysUTF8ToNSString(family);
  NSArray* ns_fonts_in_family =
      [fontManager availableMembersOfFontFamily:ns_family];

  for (NSArray* font_info in ns_fonts_in_family) {
    ppapi::proxy::SerializedTrueTypeFontDesc desc;
    desc.family = family;
    NSInteger font_weight = [[font_info objectAtIndex:2] intValue];
    font_weight = std::max(static_cast<NSInteger>(0), font_weight);
    font_weight = std::min(kPepperFontWeightsLength - 1, font_weight);
    desc.weight = kPepperFontWeights[font_weight];

    NSFontTraitMask font_traits =
        [[font_info objectAtIndex:3] unsignedIntValue];
    desc.style = PP_TRUETYPEFONTSTYLE_NORMAL;
    if (font_traits & NSItalicFontMask)
      desc.style = PP_TRUETYPEFONTSTYLE_ITALIC;

    desc.width = PP_TRUETYPEFONTWIDTH_NORMAL;
    if (font_traits & NSCondensedFontMask)
      desc.width = PP_TRUETYPEFONTWIDTH_CONDENSED;
    else if (font_traits & NSExpandedFontMask)
      desc.width = PP_TRUETYPEFONTWIDTH_EXPANDED;

    // Mac doesn't support requesting non-default character sets.
    desc.charset = PP_TRUETYPEFONTCHARSET_DEFAULT;

    fonts_in_family->push_back(desc);
  }
}

}  // namespace content