/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. @import <AppKit/CPFont.j>
  23. IBDefaultFontFace = @"Lucida Grande";
  24. IBDefaultFontSize = 13.0;
  25. var OS = require("os"),
  26. fontinfo = require("cappuccino/fontinfo").fontinfo;
  27. @implementation CPFont (NSCoding)
  28. - (id)NS_initWithCoder:(CPCoder)aCoder
  29. {
  30. var name = [aCoder decodeObjectForKey:@"NSName"],
  31. size = [aCoder decodeDoubleForKey:@"NSSize"],
  32. isBold = false,
  33. isItalic = false,
  34. info = fontinfo(name, size);
  35. if (info)
  36. {
  37. name = info.familyName;
  38. isBold = info.bold;
  39. isItalic = info.italic;
  40. }
  41. // NOTE: We save the nib fonts as is, and determine system status later
  42. var font = [self _initWithName:name
  43. size:size
  44. bold:isBold
  45. italic:isItalic
  46. system:NO];
  47. CPLog.debug("NSFont: %s", [NSFont descriptorForFont:font]);
  48. return font;
  49. }
  50. - (id)cibFontForNibFont
  51. {
  52. if (_name === IBDefaultFontFace)
  53. {
  54. if (_size === IBDefaultFontSize && !_isBold && !_isItalic)
  55. return nil;
  56. else
  57. return [[CPFont alloc] _initWithName:_CPFontSystemFacePlaceholder
  58. size:_size == IBDefaultFontSize ? CPFontCurrentSystemSize : _size
  59. bold:_isBold
  60. italic:_isItalic
  61. system:YES];
  62. }
  63. return self;
  64. }
  65. @end
  66. @implementation NSFont : CPFont
  67. + (void)initialize
  68. {
  69. if (self !== [NSFont class])
  70. return;
  71. CPLog.debug("NSFont: default IB font: %s %f", IBDefaultFontFace, IBDefaultFontSize);
  72. }
  73. + (CPString)descriptorForFont:(CPFont)aFont
  74. {
  75. var styleAttributes = [];
  76. if ([aFont isBold])
  77. styleAttributes.push("bold");
  78. if ([aFont isItalic])
  79. styleAttributes.push("italic");
  80. styleAttributes = styleAttributes.join(" ");
  81. var systemAttributes = [];
  82. if ([aFont isSystem])
  83. {
  84. systemAttributes.push("system face");
  85. if ([aFont size] === IBDefaultFontSize)
  86. systemAttributes.push("system size");
  87. }
  88. systemAttributes = systemAttributes.join(", ");
  89. return [CPString stringWithFormat:@"%s%s %d%s", [aFont familyName], styleAttributes ? " " + styleAttributes : "", [aFont size], systemAttributes ? " (" + systemAttributes + ")" : ""];
  90. }
  91. - (id)initWithCoder:(CPCoder)aCoder
  92. {
  93. return [self NS_initWithCoder:aCoder];
  94. }
  95. - (Class)classForKeyedArchiver
  96. {
  97. return [CPFont class];
  98. }
  99. @end