Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/font_list.h" | 5 #include "content/common/font_list.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsautorelease_pool.h" | 9 #include "base/mac/scoped_nsautorelease_pool.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 static void AppendNewFontItemToList(NSString* font_value, | |
| 16 NSString* font_label, ListValue* font_list) { | |
| 17 ListValue* font_item = new ListValue(); | |
| 18 string16 value_string = base::SysNSStringToUTF16(font_value); | |
| 19 font_item->Append(Value::CreateStringValue(value_string)); | |
| 20 string16 label_string = base::SysNSStringToUTF16(font_label); | |
| 21 font_item->Append(Value::CreateStringValue(label_string)); | |
| 22 font_list->Append(font_item); | |
| 23 } | |
| 24 | |
| 15 ListValue* GetFontList_SlowBlocking() { | 25 ListValue* GetFontList_SlowBlocking() { |
| 16 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 26 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 17 ListValue* font_list = new ListValue; | 27 ListValue* font_list = new ListValue; |
| 18 NSFontManager* fontManager = [[[NSFontManager alloc] init] autorelease]; | 28 NSFontManager* font_manager = [[[NSFontManager alloc] init] autorelease]; |
| 19 NSArray* fonts = [fontManager availableFontFamilies]; | 29 NSArray* family_names = [font_manager availableFontFamilies]; |
| 20 for (NSString* family_name in fonts) { | 30 NSMutableArray* font_families = [NSMutableArray array]; |
| 21 NSString* localized_family_name = | 31 for (NSString* family_name in family_names) { |
| 22 [fontManager localizedNameForFamily:family_name face:nil]; | 32 NSString* localized_family_name = [font_manager |
| 23 ListValue* font_item = new ListValue(); | 33 localizedNameForFamily:family_name face:nil]; |
| 24 string16 family = base::SysNSStringToUTF16(family_name); | 34 [font_families addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
| 25 font_item->Append(Value::CreateStringValue(family)); | 35 family_name, @"name", localized_family_name, @"localizedName", nil]]; |
| 26 string16 loc_family = base::SysNSStringToUTF16(localized_family_name); | 36 } |
| 27 font_item->Append(Value::CreateStringValue(loc_family)); | 37 NSSortDescriptor* sort_descriptor = [[NSSortDescriptor alloc] |
| 28 font_list->Append(font_item); | 38 initWithKey:@"localizedName" ascending:YES]; |
| 39 [font_families sortUsingDescriptors: | |
| 40 [NSArray arrayWithObject:sort_descriptor]]; | |
| 41 [sort_descriptor release]; | |
| 42 for (NSDictionary* family in font_families) { | |
| 43 NSString* family_name = [family objectForKey:@"name"]; | |
| 44 AppendNewFontItemToList(family_name, [family objectForKey:@"localizedName"], | |
| 45 font_list); | |
| 46 // Osaka-Mono is the only monospace Japanese font on the Mac, | |
| 47 // but since it is included as a member of the Osaka family, it cannot | |
| 48 // be selected. The code below exceptionally picks out monospace members in | |
| 49 // a proportionaly spaced family (or vice versa) to included. | |
| 50 BOOL is_family_fixed_point = [font_manager fontNamed:family_name | |
| 51 hasTraits:NSFixedPitchFontMask]; | |
| 52 NSArray* familyMembers = [font_manager availableMembersOfFontFamily: | |
|
csilv
2011/07/06 19:42:51
Nit: change variable name to family_members
| |
| 53 [family objectForKey:@"name"]]; | |
| 54 for (NSArray* member in familyMembers) { | |
| 55 NSString* font_name = [member objectAtIndex:0]; | |
| 56 if (is_family_fixed_point != [font_manager fontNamed:font_name | |
| 57 hasTraits:NSFixedPitchFontMask]) { | |
| 58 NSFont* font = [NSFont fontWithName:font_name size:0.0]; | |
| 59 NSString* localized_font_name = [font displayName]; | |
| 60 AppendNewFontItemToList(font_name, localized_font_name, font_list); | |
| 61 } | |
| 62 } | |
| 29 } | 63 } |
| 30 return font_list; | 64 return font_list; |
| 31 } | 65 } |
| 32 | 66 |
| 33 } // namespace content | 67 } // namespace content |
| OLD | NEW |