5 // Created by mshio on 11/02/05.
6 // Copyright 2011 mshio. All rights reserved.
11 @implementation Controller
14 currentCharacter = nil;
15 engine = [JavaScriptEngine instance];
19 - (void) setupFontMenu {
20 NSArray* nm = [engine getFontsNameArray];
21 NSMenuItem* menu = [glyphViewMenu itemAtIndex: 1];
22 NSMenu* p = [[NSMenu alloc] init];
24 for (int i = 0; i <= [nm count]; i++) {
25 NSString* t = i == 0 ? @"default" : [nm objectAtIndex: i - 1];
26 NSMenuItem* m = [[NSMenuItem alloc] init];
33 - (void) awakeFromNib {
37 - (NSDictionary*) genFontAttrDict: (NSString*) fontName size: (float) size {
38 NSFont* f = fontName == nil || [fontName isEqualToString: @"default"] ?
39 [NSFont messageFontOfSize: size] :
40 [NSFont fontWithName: fontName size: size];
42 return [NSDictionary dictionaryWithObjectsAndKeys:
43 f, NSFontAttributeName, nil];
46 - (void) drawOnGlyphView: (NSString*) s {
49 NSSize vsz = [glyphView bounds].size;
50 NSImage *m = [[NSImage alloc] initWithSize: vsz];
51 NSSize ssz = [s sizeWithAttributes: [self genFontAttrDict: nil size: dummy]];
53 (vsz.width >= vsz.height ? 1 / ssz.height * vsz.height :
54 1 / ssz.width * vsz.width);
55 NSDictionary *attr = [self genFontAttrDict: nil size: fsize];
56 ssz = [s sizeWithAttributes: attr];
57 int margin = (vsz.width - ssz.width) / 2;
59 [s drawAtPoint: NSMakePoint(margin, 0) withAttributes: attr];
61 [glyphView setImage: m];
64 - (NSString*) getUnicharFromText: (NSString*) text withLocation: (int) loc {
65 unichar ch = [text characterAtIndex: loc];
67 int rlen = 0, llen = 0;
68 while (rlen <= 4 && ((ch >= '0' && ch <= '9') ||
69 (ch >= 'a' && ch <= 'f') ||
70 (ch >= 'A' && ch <= 'F'))) {
72 if (loc + rlen >= [text length]) break;
73 ch = [text characterAtIndex: loc + rlen];
75 if (rlen >=1 && rlen < 5) {
77 if (loc - llen <= 0) break;
79 ch = [text characterAtIndex: loc - llen];
80 } while (rlen + llen <= 4 && ((ch >= '0' && ch <= '9') ||
81 (ch >= 'a' && ch <= 'f') ||
82 (ch >= 'A' && ch <= 'F')));
84 if (rlen + llen >= 4 && rlen + llen <= 5) {
85 char s[rlen + llen + 1];
86 for (int i = loc - llen; i < loc + rlen; i++) {
87 s[i - (loc - llen)] = (char) [text characterAtIndex: i];
89 s[rlen + llen] = '\0';
92 return [[NSString alloc] initWithCharacters: (unichar[]) {v} length: 1];
94 return [text substringWithRange: NSMakeRange(loc, 1)];
97 - (IBAction) search: (id) sender {
98 NSString *text = [searchField stringValue];
99 if ([text length] <=0) return;
101 NSText* editor = [searchField currentEditor];
102 NSRange r = [editor selectedRange];
103 int c = r.location + r.length < [text length] ? r.location + r.length : 0;
104 NSString* ch = [self getUnicharFromText: text withLocation: c];
106 currentCharacter = ch;
107 [editor setSelectedRange: NSMakeRange(c, 1)];
109 [self drawOnGlyphView: ch];
111 NSString* code = [NSString stringWithFormat: @"%04x", [ch characterAtIndex:0]];
112 [codeField setStringValue: code];
115 - (IBAction) copyGlyphCharacter: (id) sender {
116 if (currentCharacter) {
117 NSPasteboard* cb = [NSPasteboard generalPasteboard];
118 [cb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
119 [cb setString: currentCharacter forType: NSStringPboardType];