-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCFontUtilities.m
151 lines (112 loc) · 5.28 KB
/
BCFontUtilities.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// BCFontUtilities.m
// Xynk
//
// Created by Tom Houpt on 13/8/10.
//
//
#import "BCFontUtilities.h"
void SetFontStyleControlSelection(NSSegmentedControl *fontStyleControl, NSUInteger styleFlags) {
[fontStyleControl setSelected:(BOOL)(styleFlags & kBCBoldFlag) forSegment:0];
[fontStyleControl setSelected:(BOOL)(styleFlags & kBCItalicFlag) forSegment:1];
[fontStyleControl setSelected:(BOOL)(styleFlags & kBCUnderlinedFlag) forSegment:2];
}
NSUInteger GetFontStyleFlagsFromControlSelection(NSSegmentedControl *fontStyleControl) {
NSUInteger fontStyleFlags = 0;
fontStyleFlags = kBCBoldFlag * (NSUInteger)[fontStyleControl isSelectedForSegment:0]
+ kBCItalicFlag * (NSUInteger)[fontStyleControl isSelectedForSegment:1]
+ kBCUnderlinedFlag * (NSUInteger)[fontStyleControl isSelectedForSegment:2];
return fontStyleFlags;
}
void SetJustificationControlSelection(NSSegmentedControl *justificationControl, NSInteger justification) {
NSInteger i;
for (i=LEFT_JUSTIFIED; i <= RIGHT_JUSTIFIED; i++) {
if (i != justification) {
[justificationControl setSelected:NO forSegment:i];
}
else {
[justificationControl setSelected:YES forSegment:i];
}
}
}
NSInteger GetJustificationFromControlSelection(NSSegmentedControl *justificationControl) {
NSInteger i;
for (i=LEFT_JUSTIFIED; i <= RIGHT_JUSTIFIED; i++) {
if ([justificationControl isSelectedForSegment:i]) {
return i;
};
}
return LEFT_JUSTIFIED;
}
void SetAlignmentControlSelection(NSSegmentedControl *alignmentControl, NSInteger justification) {
SetJustificationControlSelection(alignmentControl,justification);
}
NSInteger GetAlignmentFromControlSelection(NSSegmentedControl *alignmentControl) {
return GetJustificationFromControlSelection(alignmentControl);
}
static void BuildTypeFacePopUpButtonWithOptionalSystemFont(NSPopUpButton *typeFaceButton,NSString *currentTypeFace, BOOL includeSystemFont) {
// Populate font pop-up.
// problems mixing and matching pop-up button routines and menu routines (i.e. inserting separator item)
NSMutableArray *fontList = [[NSMutableArray alloc] initWithArray:[[NSFontManager
sharedFontManager] availableFontFamilies]];
[fontList sortUsingSelector:@selector(caseInsensitiveCompare:)];
[[typeFaceButton menu ] removeAllItems];
/* build menu of
- current type face (if not helvetica, Times New Roman, or systemfont
- SystemFont (if include system font)
- Helvetica
- Times New Roman
- separator line
- list of installed fonts
*/
if (![currentTypeFace isEqualToString:@"Helvetica"] && ![currentTypeFace isEqualToString:@"Times New Roman"] ) {
[[typeFaceButton menu ] addItemWithTitle:currentTypeFace action:NULL keyEquivalent:@""];
}
if (includeSystemFont) {
[[typeFaceButton menu ] addItemWithTitle:@"System Font Regular" action:NULL keyEquivalent:@""];
}
[[typeFaceButton menu ] addItemWithTitle:@"Helvetica" action:NULL keyEquivalent:@""];
[[typeFaceButton menu ] addItemWithTitle:@"Times New Roman" action:NULL keyEquivalent:@""];
// NOTE: insert list of recent fonts used here?
[[typeFaceButton menu ] addItem:[NSMenuItem separatorItem]];
for (NSString *fontName in fontList) {
[[typeFaceButton menu] addItemWithTitle:fontName action:NULL keyEquivalent:@""];
}
// put check mark next to appropriate font name
if (includeSystemFont && (nil == currentTypeFace || [currentTypeFace isEqualToString:@"System Font Regular"]) ) {
[typeFaceButton selectItemWithTitle:@"System Font Regular"];
}
else {
[typeFaceButton selectItemWithTitle:currentTypeFace];
}
/*
if (![currentTypeFace isEqualToString:@"Helvetica"] || [currentTypeFace isEqualToString:@"Times New Roman"] ) {
[typeFaceButton selectItemAtIndex:0];
}
else if ([currentTypeFace isEqualToString:@"Helvetica"]) {
[[typeFaceButton menu] addItemWithTitle:currentTypeFace action:NULL keyEquivalent:@""];
[typeFaceButton selectItemAtIndex:0];
}
else if ([currentTypeFace isEqualToString:@"Times New Roman"]) {
[[typeFaceButton menu ] addItemWithTitle:currentTypeFace action:NULL keyEquivalent:@""];
[typeFaceButton selectItemAtIndex:1];
}
*/
}
void BuildTypeFacePopUpButton(NSPopUpButton *typeFaceButton,NSString *currentTypeFace) {
BuildTypeFacePopUpButtonWithOptionalSystemFont(typeFaceButton,currentTypeFace, NO);
}
void BuildTypeFacePopUpButtonWithSystemFont(NSPopUpButton *typeFaceButton,NSString *currentTypeFace) {
BuildTypeFacePopUpButtonWithOptionalSystemFont(typeFaceButton,currentTypeFace, YES);
}
NSTextAlignment BCtoNSTextAlignment(BCFontHorzAlignmentOptions just) {
if (just == 1) { just = 2; }
else if (just == 2) {just = 1; }
return (NSTextAlignment)just;
}
NSFont *FontWithNameAndSize(NSString *fontName, CGFloat fontSize) {
if (nil == fontName || [fontName isEqualToString:@"System Font Regular"]) {
return [NSFont systemFontOfSize:fontSize];
}
return [NSFont fontWithName:fontName size:fontSize];
}