forked from alexmyczko/fnt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFNT.m
More file actions
266 lines (213 loc) · 8.86 KB
/
FNT.m
File metadata and controls
266 lines (213 loc) · 8.86 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>
{
NSWindow *window;
NSTableView *tableView;
NSImageView *imageView;
NSMutableArray *fontList;
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
fontList = [[NSMutableArray alloc] init];
// Create window
NSRect frame = NSMakeRect(100, 100, 800, 600);
window = [[NSWindow alloc] initWithContentRect:frame
styleMask:(NSWindowStyleMaskTitled |
NSWindowStyleMaskResizable |
NSWindowStyleMaskClosable)
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:@"FNT"];
[window makeKeyAndOrderFront:nil];
[[NSProcessInfo processInfo] setProcessName:@"FNT"];
NSImage *icon = [[NSImage alloc] initWithContentsOfFile:@"fnt.png"];
[window setMiniwindowImage:icon];
// Split view (vertical: left and right)
NSSplitView *splitView = [[NSSplitView alloc] initWithFrame:[[window contentView] bounds]];
[splitView setVertical:YES]; // LEFT-RIGHT split
[splitView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[splitView setDividerStyle:NSSplitViewDividerStyleThin];
// Left: ScrollView with TableView
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 200, 600)];
[scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
tableView = [[NSTableView alloc] initWithFrame:scrollView.bounds];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"FontColumn"];
[column setTitle:@"Fonts"];
[column setWidth:200];
[tableView addTableColumn:column];
[tableView setDelegate:self];
[tableView setDataSource:self];
[scrollView setDocumentView:tableView];
[scrollView setHasVerticalScroller:YES];
// Right: ImageView
imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 0, 600, 600)];
[imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
[imageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
// Add subviews to split view
[splitView addSubview:scrollView];
[splitView addSubview:imageView];
[[window contentView] addSubview:splitView];
[self buildMenu];
[self loadFontList];
}
- (void)buildMenu {
NSMenu *mainMenu = [[NSMenu alloc] init];
NSMenuItem *appMenuItem = [[NSMenuItem alloc] init];
[mainMenu addItem:appMenuItem];
[NSApp setMainMenu:mainMenu];
// App Menu (named after app)
NSString *appName = @"FNT";
NSMenu *appMenu = [[NSMenu alloc] initWithTitle:appName];
// About
NSMenuItem *aboutItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"About %@", appName]
action:@selector(showAbout)
keyEquivalent:@""];
[aboutItem setTarget:self];
[appMenu addItem:aboutItem];
// Update
NSMenuItem *updateItem = [[NSMenuItem alloc] initWithTitle:@"Update Fonts"
action:@selector(runUpdate)
keyEquivalent:@"u"];
[updateItem setTarget:self];
[appMenu addItem:updateItem];
[appMenu addItem:[NSMenuItem separatorItem]];
// Quit
NSMenuItem *quitItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"Quit %@", appName]
action:@selector(terminate:)
keyEquivalent:@"q"];
[appMenu addItem:quitItem];
// Attach submenu
[appMenuItem setSubmenu:appMenu];
}
/*
- (void)buildMenu {
NSMenu *menubar = [[NSMenu alloc] init];
NSMenuItem *appMenuItem = [[NSMenuItem alloc] init];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [[NSMenu alloc] init];
// About
NSMenuItem *aboutItem = [[NSMenuItem alloc] initWithTitle:@"About"
action:@selector(showAbout)
keyEquivalent:@""];
[aboutItem setTarget:self];
[appMenu addItem:aboutItem];
// Update
NSMenuItem *updateItem = [[NSMenuItem alloc] initWithTitle:@"Update Fonts"
action:@selector(runUpdate)
keyEquivalent:@"u"];
[updateItem setTarget:self];
[appMenu addItem:updateItem];
[appMenu addItem:[NSMenuItem separatorItem]];
// Quit
NSMenuItem *quitItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:)
keyEquivalent:@"q"];
[appMenu addItem:quitItem];
[appMenuItem setSubmenu:appMenu];
}
*/
- (void)showAbout {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"FNT"];
[alert setInformativeText:@"Created by Alex Myczko <tar@debian.org>\nhttps://github.com/alexmyczko/fnt/"];
[alert addButtonWithTitle:@"OK"];
[alert runModal];
}
- (void)runUpdate {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/env"];
[task setArguments:@[@"fnt", @"update"]];
[task launch];
[task waitUntilExit];
[self loadFontList];
}
- (void)loadFontList {
[fontList removeAllObjects];
NSLog(@"fnt search...");
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/env"];
[task setArguments:@[@"fnt", @"search"]];
[task setStandardOutput:pipe];
NSFileHandle *handle = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data = [handle readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *lines = [output componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *line in lines) {
if ([line length] > 0) {
[fontList addObject:line];
}
}
NSLog(@"fnt search... done");
[tableView reloadData];
}
#pragma mark - TableView Data Source
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [fontList count];
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTextField *cell = [tableView makeViewWithIdentifier:@"Cell" owner:self];
if (!cell) {
cell = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)];
[cell setIdentifier:@"Cell"];
[cell setBezeled:NO];
[cell setDrawsBackground:NO];
[cell setEditable:NO];
[cell setSelectable:NO];
}
[cell setStringValue:fontList[row]];
return cell;
}
#pragma mark - TableView Delegate
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow < 0 || selectedRow >= [fontList count]) return;
NSString *fontName = fontList[selectedRow];
// Remove "fonts-" or "google-" prefix
NSString *cleanFontName = fontName;
if ([cleanFontName hasPrefix:@"fonts-"]) {
cleanFontName = [cleanFontName substringFromIndex:6];
} else if ([cleanFontName hasPrefix:@"google-"]) {
cleanFontName = [cleanFontName substringFromIndex:7];
}
NSLog(@"Previewing font: %@", cleanFontName);
// Run `fnt preview <cleanFontName>`
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/env"];
[task setArguments:@[@"fnt", @"preview", cleanFontName]];
// Redirect output to /dev/null to avoid blocking
NSFileHandle *nullFile = [NSFileHandle fileHandleForWritingAtPath:@"/dev/null"];
if (nullFile) {
[task setStandardOutput:nullFile];
[task setStandardError:nullFile];
}
[task launch];
[task waitUntilExit];
// Load image from ~/.fnt/preview.png
NSLog(@"Downloaded preview, showing now...");
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@".fnt/preview.png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imagePath];
[imageView setImage:image];
} else {
NSLog(@"Preview image not found at %@", imagePath);
[imageView setImage:nil];
}
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
[NSApplication sharedApplication];
AppDelegate *delegate = [[AppDelegate alloc] init];
[NSApp setDelegate:delegate];
[NSApp run];
}
return 0;
}