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#import <Cocoa/Cocoa.h> 8 9#include "base/stl_util.h" 10#include "base/strings/sys_string_conversions.h" 11#include "ppapi/c/dev/ppb_truetype_font_dev.h" 12#include "ppapi/proxy/serialized_structs.h" 13 14namespace content { 15 16namespace { 17 18// Table to map AppKit weights to Pepper ones. 19const PP_TrueTypeFontWeight_Dev kPepperFontWeights[] = { 20 PP_TRUETYPEFONTWEIGHT_THIN, // 0 is the minimum AppKit weight. 21 PP_TRUETYPEFONTWEIGHT_ULTRALIGHT, 22 PP_TRUETYPEFONTWEIGHT_ULTRALIGHT, 23 PP_TRUETYPEFONTWEIGHT_LIGHT, 24 PP_TRUETYPEFONTWEIGHT_LIGHT, 25 PP_TRUETYPEFONTWEIGHT_NORMAL, // 5 is a 'normal' AppKit weight. 26 PP_TRUETYPEFONTWEIGHT_MEDIUM, 27 PP_TRUETYPEFONTWEIGHT_MEDIUM, 28 PP_TRUETYPEFONTWEIGHT_SEMIBOLD, 29 PP_TRUETYPEFONTWEIGHT_BOLD, // 9 is a 'bold' AppKit weight. 30 PP_TRUETYPEFONTWEIGHT_ULTRABOLD, 31 PP_TRUETYPEFONTWEIGHT_HEAVY, 32}; 33const NSInteger kPepperFontWeightsLength = base::size(kPepperFontWeights); 34 35} // namespace 36 37void GetFontFamilies_SlowBlocking(std::vector<std::string>* font_families) { 38 @autoreleasepool { 39 NSFontManager* fontManager = [[[NSFontManager alloc] init] autorelease]; 40 NSArray* fonts = [fontManager availableFontFamilies]; 41 font_families->reserve([fonts count]); 42 for (NSString* family_name in fonts) 43 font_families->push_back(base::SysNSStringToUTF8(family_name)); 44 } 45} 46 47void GetFontsInFamily_SlowBlocking( 48 const std::string& family, 49 std::vector<ppapi::proxy::SerializedTrueTypeFontDesc>* fonts_in_family) { 50 @autoreleasepool { 51 NSFontManager* fontManager = [[[NSFontManager alloc] init] autorelease]; 52 NSString* ns_family = base::SysUTF8ToNSString(family); 53 NSArray* ns_fonts_in_family = 54 [fontManager availableMembersOfFontFamily:ns_family]; 55 56 for (NSArray* font_info in ns_fonts_in_family) { 57 ppapi::proxy::SerializedTrueTypeFontDesc desc; 58 desc.family = family; 59 NSInteger font_weight = [[font_info objectAtIndex:2] intValue]; 60 font_weight = std::max(static_cast<NSInteger>(0), font_weight); 61 font_weight = std::min(kPepperFontWeightsLength - 1, font_weight); 62 desc.weight = kPepperFontWeights[font_weight]; 63 64 NSFontTraitMask font_traits = 65 [[font_info objectAtIndex:3] unsignedIntValue]; 66 desc.style = PP_TRUETYPEFONTSTYLE_NORMAL; 67 if (font_traits & NSItalicFontMask) 68 desc.style = PP_TRUETYPEFONTSTYLE_ITALIC; 69 70 desc.width = PP_TRUETYPEFONTWIDTH_NORMAL; 71 if (font_traits & NSCondensedFontMask) 72 desc.width = PP_TRUETYPEFONTWIDTH_CONDENSED; 73 else if (font_traits & NSExpandedFontMask) 74 desc.width = PP_TRUETYPEFONTWIDTH_EXPANDED; 75 76 // Mac doesn't support requesting non-default character sets. 77 desc.charset = PP_TRUETYPEFONTCHARSET_DEFAULT; 78 79 fonts_in_family->push_back(desc); 80 } 81 } 82} 83 84} // namespace content 85