5 // Created by mshio on 11/02/05.
6 // Copyright 2011 mshio. All rights reserved.
11 @implementation Controller
14 engine = [JavaScriptEngine instance];
15 charUtil = [UnicharUtil instance];
19 - (void) setupFontMenu {
20 NSArray* nm = [engine fontNamesWithArray];
21 NSMenuItem* menu = [glyphViewMenu itemAtIndex: 1];
22 NSMenu* p = [[NSMenu alloc] init];
24 [p setAutoenablesItems: NO];
27 NSString* t = i == 0 ? @"default" : [nm objectAtIndex: i - 1];
28 NSMenuItem* m = [[NSMenuItem alloc] init];
30 [m setAction: @selector(changeFont:)];
34 } while (nm != NULL && ++i <= [nm count]);
38 - (void) awakeFromNib {
42 - (NSDictionary*) genFontAttrDict: (NSString*) fontName size: (float) size {
43 NSFont* f = fontName == nil || [fontName isEqualToString: @"default"] ?
44 [NSFont messageFontOfSize: size] :
45 [NSFont fontWithName: fontName size: size];
47 return [NSDictionary dictionaryWithObjectsAndKeys:
48 f, NSFontAttributeName, nil];
51 - (void) drawOnGlyphView: (NSString*) string {
54 NSSize viewSize = [glyphView bounds].size;
55 NSImage* image = [[NSImage alloc] initWithSize: viewSize];
56 NSSize stringSize = [string sizeWithAttributes:
57 [self genFontAttrDict: currentFont size: dummy]];
58 float fontSize = dummy *
59 (viewSize.width >= viewSize.height ? 1 / stringSize.height * viewSize.height :
60 1 / stringSize.width * viewSize.width);
61 NSDictionary *attr = [self genFontAttrDict: currentFont size: fontSize];
62 stringSize = [string sizeWithAttributes: attr];
63 int margin = (viewSize.width - stringSize.width) / 2;
65 [string drawAtPoint: NSMakePoint(margin, 0) withAttributes: attr];
67 [glyphView setImage: image];
70 - (NSRange) getTargetRangeFrom: (NSString*) text withLocation: (int) location {
71 unichar ch = [text characterAtIndex: location];
73 int rlen = 0, llen = 0;
74 while (rlen <= 4 && [charUtil characterIsHexCharacter: ch]) {
76 if (location + rlen >= [text length]) break;
77 ch = [text characterAtIndex: location + rlen];
79 if (rlen >= 1 && rlen < 5) {
80 while (rlen + llen <= 4 && location - llen > 0) {
81 ch = [text characterAtIndex: location - llen - 1];
82 if (! [charUtil characterIsHexCharacter: ch]) break;
87 return NSMakeRange(location - llen, rlen + llen);
90 - (NSString*) getCharacterFrom: (NSString*) text withRange: (NSRange) range andLocation: (int) location {
91 if (range.length >= 4) {
92 return [charUtil getUnicharFromCharCode: [text substringWithRange: range]];
94 unichar ch = [text characterAtIndex: range.location];
95 BOOL high = [charUtil characterIsHighSurrogate: ch];
96 BOOL low = [charUtil characterIsLowSurrogate: ch];
97 int p = location - (low ? 1 : 0);
98 return [text substringWithRange:
99 NSMakeRange(p < 0 ? 0 : location, high || low ? 2 : 1)];
103 - (BOOL) control: (NSControl *) control textView: (NSTextView *) textView doCommandBySelector: (SEL) command {
104 if (command == @selector(insertNewline:)) {
105 [self search: control];
111 - (IBAction) search: (id) sender {
112 NSString* text = [searchField stringValue];
113 if ([text length] <= 0) return;
115 NSText* editor = [searchField currentEditor];
116 NSRange range = [editor selectedRange];
117 int loc = range.location + range.length < [text length] ? range.location + range.length : 0;
118 NSRange tr = [self getTargetRangeFrom: text withLocation: loc];
119 NSString* ch = [self getCharacterFrom: text withRange: tr andLocation: loc];
121 currentCharacter = ch;
122 [editor setSelectedRange:
123 tr.length < 4 ? NSMakeRange(loc, [ch length]) : NSMakeRange(tr.location, tr.length)];
125 [self drawOnGlyphView: ch];
127 NSString* code = [charUtil getCharCodeFromUnichar: ch];
128 [codeField setStringValue: code];
131 - (IBAction) clickScriptButton: (id) sender {
132 int tag = [sender tag];
133 NSString* p = [tag == 0 ? codeField : nameField stringValue];
134 NSUInteger m = [NSEvent modifierFlags];
135 [engine executeScriptAtIndex: tag withProperty: p AndModifier: m];
138 - (IBAction) copyGlyphCharacter: (id) sender {
139 if (currentCharacter) {
140 NSPasteboard* cb = [NSPasteboard generalPasteboard];
141 [cb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
142 [cb setString: currentCharacter forType: NSStringPboardType];
146 - (IBAction) changeFont: (id) sender {
147 currentFont = [sender title];
148 if (currentCharacter != nil) {
149 [self drawOnGlyphView: currentCharacter];