Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / widget / cocoa / nsLookAndFeel.mm
blobf0733b1f378c8b4ff9d59301add653d33be5fb6c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsLookAndFeel.h"
7 #include "nsCocoaFeatures.h"
8 #include "nsIServiceManager.h"
9 #include "nsNativeThemeColors.h"
10 #include "nsStyleConsts.h"
11 #include "nsCocoaFeatures.h"
12 #include "gfxFont.h"
13 #include "gfxFontConstants.h"
14 #include "mozilla/gfx/2D.h"
16 #import <Cocoa/Cocoa.h>
18 // This must be included last:
19 #include "nsObjCExceptions.h"
21 enum {
22   mozNSScrollerStyleLegacy       = 0,
23   mozNSScrollerStyleOverlay      = 1
25 typedef NSInteger mozNSScrollerStyle;
27 @interface NSScroller(AvailableSinceLion)
28 + (mozNSScrollerStyle)preferredScrollerStyle;
29 @end
31 nsLookAndFeel::nsLookAndFeel() : nsXPLookAndFeel()
35 nsLookAndFeel::~nsLookAndFeel()
39 static nscolor GetColorFromNSColor(NSColor* aColor)
41   NSColor* deviceColor = [aColor colorUsingColorSpaceName:NSDeviceRGBColorSpace];
42   return NS_RGB((unsigned int)([deviceColor redComponent] * 255.0),
43                 (unsigned int)([deviceColor greenComponent] * 255.0),
44                 (unsigned int)([deviceColor blueComponent] * 255.0));
47 nsresult
48 nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor)
50   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
52   nsresult res = NS_OK;
53   
54   switch (aID) {
55     case eColorID_WindowBackground:
56       aColor = NS_RGB(0xff,0xff,0xff);
57       break;
58     case eColorID_WindowForeground:
59       aColor = NS_RGB(0x00,0x00,0x00);        
60       break;
61     case eColorID_WidgetBackground:
62       aColor = NS_RGB(0xdd,0xdd,0xdd);
63       break;
64     case eColorID_WidgetForeground:
65       aColor = NS_RGB(0x00,0x00,0x00);        
66       break;
67     case eColorID_WidgetSelectBackground:
68       aColor = NS_RGB(0x80,0x80,0x80);
69       break;
70     case eColorID_WidgetSelectForeground:
71       aColor = NS_RGB(0x00,0x00,0x80);
72       break;
73     case eColorID_Widget3DHighlight:
74       aColor = NS_RGB(0xa0,0xa0,0xa0);
75       break;
76     case eColorID_Widget3DShadow:
77       aColor = NS_RGB(0x40,0x40,0x40);
78       break;
79     case eColorID_TextBackground:
80       aColor = NS_RGB(0xff,0xff,0xff);
81       break;
82     case eColorID_TextForeground:
83       aColor = NS_RGB(0x00,0x00,0x00);
84       break;
85     case eColorID_TextSelectBackground:
86       aColor = GetColorFromNSColor([NSColor selectedTextBackgroundColor]);
87       break;
88     case eColorID_highlight: // CSS2 color
89       aColor = GetColorFromNSColor([NSColor alternateSelectedControlColor]);
90       break;
91     case eColorID__moz_menuhover:
92       aColor = GetColorFromNSColor([NSColor alternateSelectedControlColor]);
93       break;      
94     case eColorID_TextSelectForeground:
95       GetColor(eColorID_TextSelectBackground, aColor);
96       if (aColor == 0x000000)
97         aColor = NS_RGB(0xff,0xff,0xff);
98       else
99         aColor = NS_DONT_CHANGE_COLOR;
100       break;
101     case eColorID_highlighttext:  // CSS2 color
102     case eColorID__moz_menuhovertext:
103       aColor = GetColorFromNSColor([NSColor alternateSelectedControlTextColor]);
104       break;
105     case eColorID_IMESelectedRawTextBackground:
106     case eColorID_IMESelectedConvertedTextBackground:
107     case eColorID_IMERawInputBackground:
108     case eColorID_IMEConvertedTextBackground:
109       aColor = NS_TRANSPARENT;
110       break;
111     case eColorID_IMESelectedRawTextForeground:
112     case eColorID_IMESelectedConvertedTextForeground:
113     case eColorID_IMERawInputForeground:
114     case eColorID_IMEConvertedTextForeground:
115       aColor = NS_SAME_AS_FOREGROUND_COLOR;
116       break;
117     case eColorID_IMERawInputUnderline:
118     case eColorID_IMEConvertedTextUnderline:
119       aColor = NS_40PERCENT_FOREGROUND_COLOR;
120       break;
121     case eColorID_IMESelectedRawTextUnderline:
122     case eColorID_IMESelectedConvertedTextUnderline:
123       aColor = NS_SAME_AS_FOREGROUND_COLOR;
124       break;
125     case eColorID_SpellCheckerUnderline:
126       aColor = NS_RGB(0xff, 0, 0);
127       break;
129       //
130       // css2 system colors http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
131       //
132       // It's really hard to effectively map these to the Appearance Manager properly,
133       // since they are modeled word for word after the win32 system colors and don't have any 
134       // real counterparts in the Mac world. I'm sure we'll be tweaking these for 
135       // years to come. 
136       //
137       // Thanks to mpt26@student.canterbury.ac.nz for the hardcoded values that form the defaults
138       //  if querying the Appearance Manager fails ;)
139       //
140     case eColorID__moz_mac_buttonactivetext:
141     case eColorID__moz_mac_defaultbuttontext:
142       if (nsCocoaFeatures::OnYosemiteOrLater()) {
143         aColor = NS_RGB(0xFF,0xFF,0xFF);
144         break;
145       }
146       // Otherwise fall through and return the regular button text:
147       
148     case eColorID_buttontext:
149     case eColorID__moz_buttonhovertext:
150       aColor = GetColorFromNSColor([NSColor controlTextColor]);
151       break;
152     case eColorID_captiontext:
153     case eColorID_menutext:
154     case eColorID_infotext:
155     case eColorID__moz_menubartext:
156       aColor = GetColorFromNSColor([NSColor textColor]);
157       break;
158     case eColorID_windowtext:
159       aColor = GetColorFromNSColor([NSColor windowFrameTextColor]);
160       break;
161     case eColorID_activecaption:
162       aColor = GetColorFromNSColor([NSColor gridColor]);
163       break;
164     case eColorID_activeborder:
165       aColor = NS_RGB(0x00,0x00,0x00);
166       break;
167      case eColorID_appworkspace:
168       aColor = NS_RGB(0xFF,0xFF,0xFF);
169       break;
170     case eColorID_background:
171       aColor = NS_RGB(0x63,0x63,0xCE);
172       break;
173     case eColorID_buttonface:
174     case eColorID__moz_buttonhoverface:
175       aColor = NS_RGB(0xF0,0xF0,0xF0);
176       break;
177     case eColorID_buttonhighlight:
178       aColor = NS_RGB(0xFF,0xFF,0xFF);
179       break;
180     case eColorID_buttonshadow:
181       aColor = NS_RGB(0xDC,0xDC,0xDC);
182       break;
183     case eColorID_graytext:
184       aColor = GetColorFromNSColor([NSColor disabledControlTextColor]);
185       break;
186     case eColorID_inactiveborder:
187       aColor = GetColorFromNSColor([NSColor controlBackgroundColor]);
188       break;
189     case eColorID_inactivecaption:
190       aColor = GetColorFromNSColor([NSColor controlBackgroundColor]);
191       break;
192     case eColorID_inactivecaptiontext:
193       aColor = NS_RGB(0x45,0x45,0x45);
194       break;
195     case eColorID_scrollbar:
196       aColor = GetColorFromNSColor([NSColor scrollBarColor]);
197       break;
198     case eColorID_threeddarkshadow:
199       aColor = NS_RGB(0xDC,0xDC,0xDC);
200       break;
201     case eColorID_threedshadow:
202       aColor = NS_RGB(0xE0,0xE0,0xE0);
203       break;
204     case eColorID_threedface:
205       aColor = NS_RGB(0xF0,0xF0,0xF0);
206       break;
207     case eColorID_threedhighlight:
208       aColor = GetColorFromNSColor([NSColor highlightColor]);
209       break;
210     case eColorID_threedlightshadow:
211       aColor = NS_RGB(0xDA,0xDA,0xDA);
212       break;
213     case eColorID_menu:
214       aColor = GetColorFromNSColor([NSColor alternateSelectedControlTextColor]);
215       break;
216     case eColorID_infobackground:
217       aColor = NS_RGB(0xFF,0xFF,0xC7);
218       break;
219     case eColorID_windowframe:
220       aColor = GetColorFromNSColor([NSColor gridColor]);
221       break;
222     case eColorID_window:
223     case eColorID__moz_field:
224     case eColorID__moz_combobox:
225       aColor = NS_RGB(0xff,0xff,0xff);
226       break;
227     case eColorID__moz_fieldtext:
228     case eColorID__moz_comboboxtext:
229       aColor = GetColorFromNSColor([NSColor controlTextColor]);
230       break;
231     case eColorID__moz_dialog:
232       aColor = GetColorFromNSColor([NSColor controlHighlightColor]);
233       break;
234     case eColorID__moz_dialogtext:
235     case eColorID__moz_cellhighlighttext:
236     case eColorID__moz_html_cellhighlighttext:
237       aColor = GetColorFromNSColor([NSColor controlTextColor]);
238       break;
239     case eColorID__moz_dragtargetzone:
240       aColor = GetColorFromNSColor([NSColor selectedControlColor]);
241       break;
242     case eColorID__moz_mac_chrome_active:
243     case eColorID__moz_mac_chrome_inactive: {
244       int grey = NativeGreyColorAsInt(toolbarFillGrey, (aID == eColorID__moz_mac_chrome_active));
245       aColor = NS_RGB(grey, grey, grey);
246     }
247       break;
248     case eColorID__moz_mac_focusring:
249       aColor = GetColorFromNSColor([NSColor keyboardFocusIndicatorColor]);
250       break;
251     case eColorID__moz_mac_menushadow:
252       aColor = NS_RGB(0xA3,0xA3,0xA3);
253       break;          
254     case eColorID__moz_mac_menutextdisable:
255       aColor = NS_RGB(0x98,0x98,0x98);
256       break;      
257     case eColorID__moz_mac_menutextselect:
258       aColor = GetColorFromNSColor([NSColor selectedMenuItemTextColor]);
259       break;      
260     case eColorID__moz_mac_disabledtoolbartext:
261       aColor = GetColorFromNSColor([NSColor disabledControlTextColor]);
262       break;
263     case eColorID__moz_mac_menuselect:
264       aColor = GetColorFromNSColor([NSColor alternateSelectedControlColor]);
265       break;
266     case eColorID__moz_buttondefault:
267       aColor = NS_RGB(0xDC,0xDC,0xDC);
268       break;
269     case eColorID__moz_cellhighlight:
270     case eColorID__moz_html_cellhighlight:
271     case eColorID__moz_mac_secondaryhighlight:
272       // For inactive list selection
273       aColor = GetColorFromNSColor([NSColor secondarySelectedControlColor]);
274       break;
275     case eColorID__moz_eventreerow:
276       // Background color of even list rows.
277       aColor = GetColorFromNSColor([[NSColor controlAlternatingRowBackgroundColors] objectAtIndex:0]);
278       break;
279     case eColorID__moz_oddtreerow:
280       // Background color of odd list rows.
281       aColor = GetColorFromNSColor([[NSColor controlAlternatingRowBackgroundColors] objectAtIndex:1]);
282       break;
283     case eColorID__moz_nativehyperlinktext:
284       // There appears to be no available system defined color. HARDCODING to the appropriate color.
285       aColor = NS_RGB(0x14,0x4F,0xAE);
286       break;
287     default:
288       NS_WARNING("Someone asked nsILookAndFeel for a color I don't know about");
289       aColor = NS_RGB(0xff,0xff,0xff);
290       res = NS_ERROR_FAILURE;
291       break;
292     }
293   
294   return res;
296   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
299 nsresult
300 nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult)
302   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
304   nsresult res = nsXPLookAndFeel::GetIntImpl(aID, aResult);
305   if (NS_SUCCEEDED(res))
306     return res;
307   res = NS_OK;
308   
309   switch (aID) {
310     case eIntID_CaretBlinkTime:
311       aResult = 567;
312       break;
313     case eIntID_CaretWidth:
314       aResult = 1;
315       break;
316     case eIntID_ShowCaretDuringSelection:
317       aResult = 0;
318       break;
319     case eIntID_SelectTextfieldsOnKeyFocus:
320       // Select textfield content when focused by kbd
321       // used by EventStateManager::sTextfieldSelectModel
322       aResult = 1;
323       break;
324     case eIntID_SubmenuDelay:
325       aResult = 200;
326       break;
327     case eIntID_TooltipDelay:
328       aResult = 500;
329       break;
330     case eIntID_MenusCanOverlapOSBar:
331       // xul popups are not allowed to overlap the menubar.
332       aResult = 0;
333       break;
334     case eIntID_SkipNavigatingDisabledMenuItem:
335       aResult = 1;
336       break;
337     case eIntID_DragThresholdX:
338     case eIntID_DragThresholdY:
339       aResult = 4;
340       break;
341     case eIntID_ScrollArrowStyle:
342       if (nsCocoaFeatures::OnLionOrLater()) {
343         // OS X Lion's scrollbars have no arrows
344         aResult = eScrollArrow_None;
345       } else {
346         NSString *buttonPlacement = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleScrollBarVariant"];
347         if ([buttonPlacement isEqualToString:@"Single"]) {
348           aResult = eScrollArrowStyle_Single;
349         } else if ([buttonPlacement isEqualToString:@"DoubleMin"]) {
350           aResult = eScrollArrowStyle_BothAtTop;
351         } else if ([buttonPlacement isEqualToString:@"DoubleBoth"]) {
352           aResult = eScrollArrowStyle_BothAtEachEnd;
353         } else {
354           aResult = eScrollArrowStyle_BothAtBottom; // The default is BothAtBottom.
355         }
356       }
357       break;
358     case eIntID_ScrollSliderStyle:
359       aResult = eScrollThumbStyle_Proportional;
360       break;
361     case eIntID_UseOverlayScrollbars:
362       aResult = SystemWantsOverlayScrollbars() ? 1 : 0;
363       break;
364     case eIntID_AllowOverlayScrollbarsOverlap:
365       aResult = AllowOverlayScrollbarsOverlap() ? 1 : 0;
366       break;
367     case eIntID_ScrollbarDisplayOnMouseMove:
368       aResult = 0;
369       break;
370     case eIntID_ScrollbarFadeBeginDelay:
371       aResult = 450;
372       break;
373     case eIntID_ScrollbarFadeDuration:
374       aResult = 200;
375       break;
376     case eIntID_TreeOpenDelay:
377       aResult = 1000;
378       break;
379     case eIntID_TreeCloseDelay:
380       aResult = 1000;
381       break;
382     case eIntID_TreeLazyScrollDelay:
383       aResult = 150;
384       break;
385     case eIntID_TreeScrollDelay:
386       aResult = 100;
387       break;
388     case eIntID_TreeScrollLinesMax:
389       aResult = 3;
390       break;
391     case eIntID_DWMCompositor:
392     case eIntID_WindowsClassic:
393     case eIntID_WindowsDefaultTheme:
394     case eIntID_TouchEnabled:
395     case eIntID_WindowsThemeIdentifier:
396     case eIntID_OperatingSystemVersionIdentifier:
397       aResult = 0;
398       res = NS_ERROR_NOT_IMPLEMENTED;
399       break;
400     case eIntID_MacGraphiteTheme:
401       aResult = [NSColor currentControlTint] == NSGraphiteControlTint;
402       break;
403     case eIntID_MacLionTheme:
404       aResult = nsCocoaFeatures::OnLionOrLater();
405       break;
406     case eIntID_MacYosemiteTheme:
407       aResult = nsCocoaFeatures::OnYosemiteOrLater();
408       break;
409     case eIntID_AlertNotificationOrigin:
410       aResult = NS_ALERT_TOP;
411       break;
412     case eIntID_TabFocusModel:
413     {
414       // we should probably cache this
415       CFPropertyListRef fullKeyboardAccessProperty;
416       fullKeyboardAccessProperty = ::CFPreferencesCopyValue(CFSTR("AppleKeyboardUIMode"),
417                                                             kCFPreferencesAnyApplication,
418                                                             kCFPreferencesCurrentUser,
419                                                             kCFPreferencesAnyHost);
420       aResult = 1;    // default to just textboxes
421       if (fullKeyboardAccessProperty) {
422         int32_t fullKeyboardAccessPrefVal;
423         if (::CFNumberGetValue((CFNumberRef) fullKeyboardAccessProperty, kCFNumberIntType, &fullKeyboardAccessPrefVal)) {
424           // the second bit means  "Full keyboard access" is on
425           if (fullKeyboardAccessPrefVal & (1 << 1))
426             aResult = 7; // everything that can be focused
427         }
428         ::CFRelease(fullKeyboardAccessProperty);
429       }
430     }
431       break;
432     case eIntID_ScrollToClick:
433     {
434       aResult = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollerPagingBehavior"];
435     }
436       break;
437     case eIntID_ChosenMenuItemsShouldBlink:
438       aResult = 1;
439       break;
440     case eIntID_IMERawInputUnderlineStyle:
441     case eIntID_IMEConvertedTextUnderlineStyle:
442     case eIntID_IMESelectedRawTextUnderlineStyle:
443     case eIntID_IMESelectedConvertedTextUnderline:
444       aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
445       break;
446     case eIntID_SpellCheckerUnderlineStyle:
447       aResult = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED;
448       break;
449     case eIntID_ScrollbarButtonAutoRepeatBehavior:
450       aResult = 0;
451       break;
452     case eIntID_SwipeAnimationEnabled:
453       aResult = 0;
454       if ([NSEvent respondsToSelector:@selector(
455             isSwipeTrackingFromScrollEventsEnabled)]) {
456         aResult = [NSEvent isSwipeTrackingFromScrollEventsEnabled] ? 1 : 0;
457       }
458       break;
459     case eIntID_ColorPickerAvailable:
460       aResult = 1;
461       break;
462     default:
463       aResult = 0;
464       res = NS_ERROR_FAILURE;
465   }
466   return res;
468   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
471 nsresult
472 nsLookAndFeel::GetFloatImpl(FloatID aID, float &aResult)
474   nsresult res = nsXPLookAndFeel::GetFloatImpl(aID, aResult);
475   if (NS_SUCCEEDED(res))
476     return res;
477   res = NS_OK;
478   
479   switch (aID) {
480     case eFloatID_IMEUnderlineRelativeSize:
481       aResult = 2.0f;
482       break;
483     case eFloatID_SpellCheckerUnderlineRelativeSize:
484       aResult = 2.0f;
485       break;
486     default:
487       aResult = -1.0;
488       res = NS_ERROR_FAILURE;
489   }
491   return res;
494 bool nsLookAndFeel::UseOverlayScrollbars()
496   return GetInt(eIntID_UseOverlayScrollbars) != 0;
499 bool nsLookAndFeel::SystemWantsOverlayScrollbars()
501   return ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] &&
502           [NSScroller preferredScrollerStyle] == mozNSScrollerStyleOverlay);
505 bool nsLookAndFeel::AllowOverlayScrollbarsOverlap()
507   return (UseOverlayScrollbars() && nsCocoaFeatures::OnMountainLionOrLater());
510 // copied from gfxQuartzFontCache.mm, maybe should go in a Cocoa utils
511 // file somewhere
512 static void GetStringForNSString(const NSString *aSrc, nsAString& aDest)
514     aDest.SetLength([aSrc length]);
515     [aSrc getCharacters:reinterpret_cast<unichar*>(aDest.BeginWriting())];
518 bool
519 nsLookAndFeel::GetFontImpl(FontID aID, nsString &aFontName,
520                            gfxFontStyle &aFontStyle,
521                            float aDevPixPerCSSPixel)
523     NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
525     // hack for now
526     if (aID == eFont_Window || aID == eFont_Document) {
527         aFontStyle.style      = NS_FONT_STYLE_NORMAL;
528         aFontStyle.weight     = NS_FONT_WEIGHT_NORMAL;
529         aFontStyle.stretch    = NS_FONT_STRETCH_NORMAL;
530         aFontStyle.size       = 14 * aDevPixPerCSSPixel;
531         aFontStyle.systemFont = true;
533         aFontName.AssignLiteral("sans-serif");
534         return true;
535     }
537 /* possibilities, see NSFont Class Reference:
538     [NSFont boldSystemFontOfSize:     0.0]
539     [NSFont controlContentFontOfSize: 0.0]
540     [NSFont labelFontOfSize:          0.0]
541     [NSFont menuBarFontOfSize:        0.0]
542     [NSFont menuFontOfSize:           0.0]
543     [NSFont messageFontOfSize:        0.0]
544     [NSFont paletteFontOfSize:        0.0]
545     [NSFont systemFontOfSize:         0.0]
546     [NSFont titleBarFontOfSize:       0.0]
547     [NSFont toolTipsFontOfSize:       0.0]
548     [NSFont userFixedPitchFontOfSize: 0.0]
549     [NSFont userFontOfSize:           0.0]
550     [NSFont systemFontOfSize:         [NSFont smallSystemFontSize]]
551     [NSFont boldSystemFontOfSize:     [NSFont smallSystemFontSize]]
554     NSFont *font = nullptr;
555     switch (aID) {
556         // css2
557         case eFont_Caption:
558             font = [NSFont systemFontOfSize:0.0];
559             break;
560         case eFont_Icon: // used in urlbar; tried labelFont, but too small
561             font = [NSFont controlContentFontOfSize:0.0];
562             break;
563         case eFont_Menu:
564             font = [NSFont systemFontOfSize:0.0];
565             break;
566         case eFont_MessageBox:
567             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
568             break;
569         case eFont_SmallCaption:
570             font = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
571             break;
572         case eFont_StatusBar:
573             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
574             break;
575         // css3
576         //case eFont_Window:     = 'sans-serif'
577         //case eFont_Document:   = 'sans-serif'
578         case eFont_Workspace:
579             font = [NSFont controlContentFontOfSize:0.0];
580             break;
581         case eFont_Desktop:
582             font = [NSFont controlContentFontOfSize:0.0];
583             break;
584         case eFont_Info:
585             font = [NSFont controlContentFontOfSize:0.0];
586             break;
587         case eFont_Dialog:
588             font = [NSFont systemFontOfSize:0.0];
589             break;
590         case eFont_Button:
591             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
592             break;
593         case eFont_PullDownMenu:
594             font = [NSFont menuBarFontOfSize:0.0];
595             break;
596         case eFont_List:
597             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
598             break;
599         case eFont_Field:
600             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
601             break;
602         // moz
603         case eFont_Tooltips:
604             font = [NSFont toolTipsFontOfSize:0.0];
605             break;
606         case eFont_Widget:
607             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
608             break;
609         default:
610             break;
611     }
613     if (!font) {
614         NS_WARNING("failed to find a system font!");
615         return false;
616     }
618     NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits];
619     aFontStyle.style =
620         (traits & NSFontItalicTrait) ?  NS_FONT_STYLE_ITALIC : NS_FONT_STYLE_NORMAL;
621     aFontStyle.weight =
622         (traits & NSFontBoldTrait) ? NS_FONT_WEIGHT_BOLD : NS_FONT_WEIGHT_NORMAL;
623     aFontStyle.stretch =
624         (traits & NSFontExpandedTrait) ?
625             NS_FONT_STRETCH_EXPANDED : (traits & NSFontCondensedTrait) ?
626                 NS_FONT_STRETCH_CONDENSED : NS_FONT_STRETCH_NORMAL;
627     // convert size from css pixels to device pixels
628     aFontStyle.size = [font pointSize] * aDevPixPerCSSPixel;
629     aFontStyle.systemFont = true;
631     GetStringForNSString([font familyName], aFontName);
632     return true;
634     NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);