PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/nib2cib/NSFont.j

http://github.com/cacaodev/cappuccino
Unknown | 126 lines | 99 code | 27 blank | 0 comment | 0 complexity | ad0d2c20b764f6f74b058d14561e2e35 MD5 | raw file
  1/*
  2 * NSFont.j
  3 * nib2cib
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2008, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23@import <AppKit/CPFont.j>
 24
 25IBDefaultFontFace = @"Lucida Grande";
 26IBDefaultFontSize = 13.0;
 27
 28var OS = require("os"),
 29    fontinfo = require("cappuccino/fontinfo").fontinfo;
 30
 31@implementation CPFont (NSCoding)
 32
 33- (id)NS_initWithCoder:(CPCoder)aCoder
 34{
 35    var name = [aCoder decodeObjectForKey:@"NSName"],
 36        size = [aCoder decodeDoubleForKey:@"NSSize"],
 37        isBold = false,
 38        isItalic = false,
 39        info = fontinfo(name, size);
 40
 41    if (info)
 42    {
 43        name = info.familyName;
 44        isBold = info.bold;
 45        isItalic = info.italic;
 46    }
 47
 48    // NOTE: We save the nib fonts as is, and determine system status later
 49    var font = [self _initWithName:name
 50                          size:size
 51                          bold:isBold
 52                        italic:isItalic
 53                        system:NO];
 54
 55    CPLog.debug("NSFont: %s", [NSFont descriptorForFont:font]);
 56
 57    return font;
 58}
 59
 60- (id)cibFontForNibFont
 61{
 62    if (_name === IBDefaultFontFace)
 63    {
 64        if (_size === IBDefaultFontSize && !_isBold && !_isItalic)
 65            return nil;
 66        else
 67            return [[CPFont alloc] _initWithName:_CPFontSystemFacePlaceholder
 68                                            size:_size == IBDefaultFontSize ? CPFontCurrentSystemSize : _size
 69                                            bold:_isBold
 70                                          italic:_isItalic
 71                                          system:YES];
 72    }
 73
 74    return self;
 75}
 76
 77@end
 78
 79@implementation NSFont : CPFont
 80
 81+ (void)initialize
 82{
 83    if (self !== [NSFont class])
 84        return;
 85
 86    CPLog.debug("NSFont: default IB font: %s %f", IBDefaultFontFace, IBDefaultFontSize);
 87}
 88
 89+ (CPString)descriptorForFont:(CPFont)aFont
 90{
 91    var styleAttributes = [];
 92
 93    if ([aFont isBold])
 94        styleAttributes.push("bold");
 95
 96    if ([aFont isItalic])
 97        styleAttributes.push("italic");
 98
 99    styleAttributes = styleAttributes.join(" ");
100
101    var systemAttributes = [];
102
103    if ([aFont isSystem])
104    {
105        systemAttributes.push("system face");
106
107        if ([aFont size] === IBDefaultFontSize)
108            systemAttributes.push("system size");
109    }
110
111    systemAttributes = systemAttributes.join(", ");
112
113    return [CPString stringWithFormat:@"%s%s %d%s", [aFont familyName], styleAttributes ? " " + styleAttributes : "", [aFont size], systemAttributes ? " (" + systemAttributes + ")" : ""];
114}
115
116- (id)initWithCoder:(CPCoder)aCoder
117{
118    return [self NS_initWithCoder:aCoder];
119}
120
121- (Class)classForKeyedArchiver
122{
123    return [CPFont class];
124}
125
126@end