/
GlyphSILEWindow.m
180 lines (156 loc) · 6.08 KB
/
GlyphSILEWindow.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// GlyphSILEWindow.m
// GlyphSILE
//
// Created by Georg Seifert on 28/10/2015.
// Copyright (c) 2015 Simon Cozens. All rights reserved.
//
#import "GlyphSILEWindow.h"
#import <GlyphsCore/GSFont.h>
#import <GlyphsCore/GSInstance.h>
#import <GlyphsCore/GSFontMaster.h>
#import "NSLua.h"
#import "LuaBridgedFunctions.h"
@interface GSApplication : NSApplication
@property (weak, nonatomic, nullable) GSDocument* currentFontDocument;
@end
@interface GSDocument : NSDocument
@property (nonatomic, retain) GSFont* font;
@end
@interface GlyphSILEWindow ()
@end
@interface GSExportInstanceOperation : NSOperation {
}
@property (nonatomic, readonly) NSString* finalFontFile;
@property (nonatomic, strong) NSURL* installFontURL;
@property (weak, nonatomic) id delegate;
@property (nonatomic, retain) NSString* tempPath;
@property (nonatomic) BOOL autohint;
@property (nonatomic) BOOL removeOverlap;
@property (nonatomic) BOOL useSubroutines;
@property (nonatomic) BOOL useProductionNames;
- (instancetype)initWithFont:(GSFont*)Font instance:(GSInstance*)Instance format:(int)Format;
@end
@implementation GlyphSILEWindow
- (void)windowDidLoad {
[super windowDidLoad];
[_SILEInput setTextContainerInset:NSMakeSize(10,5)];
}
- (void)windowDidBecomeKey:(NSNotification *)notification{
[self setupBehaviorMenu];
[self drawSILEPreview:self];
}
- (void)setupBehaviorMenu {
NSInteger sel = [_SILEMode indexOfSelectedItem];
[_SILEMode setAutoenablesItems:NO];
[_SILEMode removeAllItems];
[_SILEMode addItemWithTitle:@"Instances"];
[[_SILEMode itemAtIndex:0] setEnabled:FALSE];
GSDocument* doc = [(GSApplication *)[NSApplication sharedApplication] currentFontDocument];
if ([doc isKindOfClass:NSClassFromString(@"GSDocument")]) {
GSFont* f = [(GSDocument*)doc font];
for (GSInstance* ins in [f instances]) {
NSMutableDictionary* robj = [[NSMutableDictionary alloc] init];
NSMenuItem *i = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ %@", [f valueForKey:@"familyName"], [ins valueForKey:@"name"]] action:NULL keyEquivalent:@""];
[robj setObject:f forKey:@"font"];
[robj setObject:ins forKey:@"instance"];
[i setRepresentedObject:robj];
[[_SILEMode menu] addItem:i];
}
}
[[_SILEMode menu] addItem:[NSMenuItem separatorItem]];
[_SILEMode addItemWithTitle:@"Masters"];
[[_SILEMode itemAtIndex:([[_SILEMode itemArray]count]-1)] setEnabled:FALSE];
for (NSDocument* doc in [[NSApplication sharedApplication] orderedDocuments]) {
if ([doc isKindOfClass:NSClassFromString(@"GSDocument")]) {
GSFont* f = (GSFont *)[(GSDocument*)doc font];
for (GSFontMaster* master in [f fontMasters]) {
NSMutableDictionary* robj = [[NSMutableDictionary alloc] init];
NSMenuItem *i = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ %@", [f valueForKey:@"familyName"], [master name]] action:NULL keyEquivalent:@""];
[robj setObject:f forKey:@"font"];
[robj setObject:master forKey:@"master"];
[i setRepresentedObject:robj];
[[_SILEMode menu] addItem:i];
}
}
}
[_SILEMode selectItemAtIndex:sel];
}
- (NSString *)_exportInstance:(GSInstance *)instance {
int Format = 0;
GSFont *Font = instance.font;
GSExportInstanceOperation *Exporter = [[NSClassFromString(@"GSExportInstanceOperation") alloc] initWithFont:Font instance:instance format:Format];
Exporter.installFontURL = [NSURL fileURLWithPath:[@"~/Library/Application Support/Glyphs/Temp/" stringByExpandingTildeInPath]];
// the following parameters can be set here or directly read from the instance.
Exporter.autohint = NO;
Exporter.removeOverlap = NO;
Exporter.useSubroutines = NO;
Exporter.useProductionNames = NO;
Exporter.tempPath = [@"~/Library/Application Support/Glyphs/Temp/" stringByExpandingTildeInPath]; // this has to be set correctly.
//Delegate = [[ExporterDelegate.alloc] init]; // the collectResults method of this object will be called on case the exporter has to report a problem.
//Exporter.delegate = Delegate;
[Exporter main];
return [Exporter finalFontFile];
}
- (void) callSILE:(bool)toScreen {
NSString *code = [_SILEInput string];
SILEPreviewView *view = _SILEOutput;
NSMenuItem* mode = [_SILEMode selectedItem];
if (!mode) return;
NSString *fileName = [NSString stringWithFormat:@"%@_%@", [[NSProcessInfo processInfo] globallyUniqueString], @"sile.pdf"];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
NSMutableDictionary* d = [mode representedObject];
NSError *Error = nil;
GSFont *f = [d objectForKey:@"font"];
UKLog(@"f: %@", f);
if (!f) return;
if ([d objectForKey:@"instance"]) {
GSInstance *i = [d objectForKey:@"instance"];
NSString *filename = [self _exportInstance:i];
if (filename) {
d[@"filename"] = filename;
} else {
NSLog(@"Error exporting instance");
return;
}
} else if ([d objectForKey:@"master"]) {
[f compileTempFontError:&Error];
NSString *family = [NSString stringWithFormat: @"Glyphs:Master:%@", [[d objectForKey:@"master"] valueForKey:@"id"]];
[d setValue:family forKey:@"family"];
}
if (toScreen) {
d[@"toscreen"] = @"YES";
} else {
d[@"toscreen"] = @"NO";
d[@"output"] = filePath;
}
lua_State *L = [[NSLua sharedLua] getLuaState];
lua_getglobal(L, "doGlyphSILE");
lua_pushstring(L, [code UTF8String]);
to_lua(L, view, true);
lua_pushinteger(L, [_fontSizeSelection integerValue]);
UKLog(@"d: %@", d);
to_lua(L, d, true);
if (lua_pcall(L, 4, 1, 0) != 0)
NSLog(@"GlyphsSILE error running function `f': %s", lua_tostring(L, -1));
if (!toScreen && [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSWorkspace sharedWorkspace] openFile:filePath];
}
}
- (IBAction)drawSILEPreview:(id)sender {
[self callSILE:true];
}
- (IBAction)createPDF:(id)sender {
[self callSILE:false];
}
- (IBAction)onSILEModeSelect:(id)sender {
NSMenuItem* mode = [_SILEMode selectedItem];
if (!mode) return;
NSMutableDictionary* d = [mode representedObject];
if ([d objectForKey:@"instance"]) {
[_PDFButton setEnabled:true];
} else if ([d objectForKey:@"master"]) {
[_PDFButton setEnabled:false];
}
}
@end