OSDN Git Service

git-svn-id: svn+ssh://svn.osdn.net/svnroot/sawarabi-fonts/trunk@16 54a90f34-5e62...
[sawarabi-fonts/sawarabi-fonts.git] / chartool / objective-c / Controller.m
1 //
2 //  Controller.m
3 //  chartool
4 //
5 //  Created by mshio on 11/02/05.
6 //  Copyright 2011 mshio. All rights reserved.
7 //
8 #include <stdio.h>
9 #import "Controller.h"
10
11 @implementation Controller
12
13 - (id) init {
14         currentCharacter = nil;
15         engine = [JavaScriptEngine instance];
16         return [super init];
17 }
18
19 - (void) setupFontMenu {
20         NSArray* nm = [engine getFontsNameArray];
21         NSMenuItem* menu = [glyphViewMenu itemAtIndex: 1];
22         NSMenu* p = [[NSMenu alloc] init];
23         
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];
27                 [m setTitle: t];
28                 [p addItem: m];
29         }
30         [menu setSubmenu: p];
31 }
32
33 - (void) awakeFromNib {
34         [self setupFontMenu];
35 }
36
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];
41
42         return [NSDictionary dictionaryWithObjectsAndKeys:
43                         f, NSFontAttributeName, nil];
44 }
45
46 - (void) drawOnGlyphView: (NSString*) s {
47         float dummy = 24.0;
48         
49         NSSize vsz = [glyphView bounds].size;
50         NSImage *m = [[NSImage alloc] initWithSize: vsz];
51         NSSize ssz = [s sizeWithAttributes: [self genFontAttrDict: nil size: dummy]];
52         float fsize = 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;
58         [m lockFocus];
59         [s drawAtPoint: NSMakePoint(margin, 0) withAttributes: attr];
60         [m unlockFocus];
61         [glyphView setImage: m];
62 }
63
64 - (NSString*) getUnicharFromText: (NSString*) text withLocation: (int) loc {
65         unichar ch = [text characterAtIndex: loc];
66
67         int rlen = 0, llen = 0;
68         while (rlen <= 4 && ((ch >= '0' && ch <= '9') ||
69                                                  (ch >= 'a' && ch <= 'f') ||
70                                                  (ch >= 'A' && ch <= 'F'))) {
71                 rlen++;
72                 if (loc + rlen >= [text length]) break;
73                 ch = [text characterAtIndex: loc + rlen];
74         }
75         if (rlen >=1 && rlen < 5) {
76                 do {
77                         if (loc - llen <= 0) break;
78                         llen++;
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')));
83         }
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];
88                 }
89                 s[rlen + llen] = '\0';
90                 int v;
91                 sscanf(s, "%x", &v);
92                 return [[NSString alloc] initWithCharacters: (unichar[]) {v} length: 1];
93         }
94         return [text substringWithRange: NSMakeRange(loc, 1)];
95 }
96
97 - (IBAction) search: (id) sender {
98         NSString *text = [searchField stringValue];
99         if ([text length] <=0) return;
100
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];
105         
106         currentCharacter = ch;
107         [editor setSelectedRange: NSMakeRange(c, 1)];
108
109         [self drawOnGlyphView: ch];
110
111         NSString* code = [NSString stringWithFormat: @"%04x", [ch characterAtIndex:0]];
112         [codeField setStringValue: code];
113 }
114
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];
120         }
121 }
122
123 @end