Debian Codegraph @

phoenix/cocoa/font.cpp

raw
1 namespace phoenix {
2
3 string pFont::serif(unsigned size, string style) {
4 if(size == 0) size = 8;
5 if(style == "") style = "Normal";
6 return {"Georgia, ", size, ", ", style};
7 }
8
9 string pFont::sans(unsigned size, string style) {
10 if(size == 0) size = 8;
11 if(style == "") style = "Normal";
12 return {"Lucida Grande, ", size, ", ", style};
13 }
14
15 string pFont::monospace(unsigned size, string style) {
16 if(size == 0) size = 8;
17 if(style == "") style = "Normal";
18 return {"Menlo, ", size, ", ", style};
19 }
20
21 Size pFont::size(string font, string text) {
22 @autoreleasepool {
23 if(NSFont* nsFont = cocoaFont(font)) {
24 return size(nsFont, text);
25 }
26 }
27 return {0, 0};
28 }
29
30 NSFont* pFont::cocoaFont(string description) {
31 lstring part = description.split<2>(",").strip();
32
33 NSString* family = @"Lucida Grande";
34 NSFontTraitMask traits = 0;
35 CGFloat size = 8.0;
36
37 if(!part(0).empty()) family = [NSString stringWithUTF8String:part(0)];
38 if(!part(1).empty()) size = decimal(part(1));
39 if(part(2).ifind("bold")) traits |= NSBoldFontMask;
40 if(part(2).ifind("italic")) traits |= NSItalicFontMask;
41 if(part(2).ifind("narrow")) traits |= NSNarrowFontMask;
42 if(part(2).ifind("expanded")) traits |= NSExpandedFontMask;
43 if(part(2).ifind("condensed")) traits |= NSCondensedFontMask;
44 if(part(2).ifind("smallcaps")) traits |= NSSmallCapsFontMask;
45
46 size *= 1.5; //scale to point sizes (for consistency with other operating systems)
47 return [[NSFontManager sharedFontManager] fontWithFamily:family traits:traits weight:5 size:size];
48 }
49
50 Size pFont::size(NSFont* font, string text) {
51 @autoreleasepool {
52 NSString* cocoaText = [NSString stringWithUTF8String:text];
53 NSDictionary* fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
54 NSSize size = [cocoaText sizeWithAttributes:fontAttributes];
55 return {size.width, size.height};
56 }
57 }
58
59 }