public
Description: A Cocoa View to draw Annotated Pinyin above Hanzi text
Homepage:
Clone URL: git://github.com/jjgod/pinyinview.git
pinyinview / PYView.m
100644 176 lines (133 sloc) 4.041 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
// PYView.m: PinyinView Implementation
// by Jjgod Jiang <gzjjgod@gmail.com>
 
#import "PYView.h"
 
@implementation PYMarkerItem
 
+ (id) itemWithHanzi: (NSString *) hanzi
              pinyin: (NSArray *) pinyin
                type: (int) type
{
    return [[[PYMarkerItem alloc] initWithHanzi: hanzi
                                         pinyin: pinyin
                                           type: type] autorelease];
}
 
- (id) initWithHanzi: (NSString *) hanzi
              pinyin: (NSArray *) pinyin
                type: (int) type
{
    self = [super init];
 
    if (self)
    {
        _han = [hanzi retain];
        _py = [pinyin retain];
        _type = type;
    }
 
    return self;
}
 
- (void) dealloc
{
    [_han release];
    [_py release];
 
    [super dealloc];
}
 
- (NSString *) hanzi
{
    return _han;
}
 
- (NSArray *) pinyin
{
    return _py;
}
 
- (int) type
{
    return _type;
}
 
@end
 
@implementation PYView
 
- (id) initWithFrame: (NSRect) frameRect
            fontName: (NSString *) name
               color: (NSColor *) color
{
    self = [super initWithFrame: frameRect];
 
    if (self)
    {
        float size = (frameRect.size.height - kPinyinViewBorder * 2 -
                      kPinyinHanziLeading) / (1 + kPinyinFontSizeRatio);
        _advance = size * kFontAdvanceRatio;
 
        NSDictionary *fontAttributes =
            [NSDictionary dictionaryWithObjectsAndKeys:
                name, NSFontNameAttribute,
                [NSNumber numberWithFloat: _advance], NSFontFixedAdvanceAttribute,
                nil];
    
        NSFontDescriptor *descriptor =
            [NSFontDescriptor fontDescriptorWithFontAttributes: fontAttributes];
            
        if (! descriptor)
            return nil;
 
        NSFont *font = [NSFont fontWithDescriptor: descriptor
                                             size: size];
        if (! font)
            return nil;
            
        _hanAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
            font, NSFontAttributeName,
            color, NSForegroundColorAttributeName, nil];
        
        _pyAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
            [NSFont fontWithName: @"Helvetica Neue Light" size: kPinyinFontSizeRatio * size], NSFontAttributeName,
            color, NSForegroundColorAttributeName, nil];
 
        _size = size;
        // Relax for a few more pixels
        _pinyinSize = kPinyinFontSizeRatio * size + 3;
 
        _items = [[NSMutableArray alloc] initWithCapacity: kMarkerItemsInitialCapacity];
    }
 
    return self;
}
 
- (float) drawItem: (PYMarkerItem *) item
           atPoint: (NSPoint) point
{
    NSString *hanzi = [item hanzi];
 
    [hanzi drawAtPoint: point
        withAttributes: _hanAttributes];
 
    NSArray *pinyin = [item pinyin];
 
    float x = point.x;
    float y = point.y + _size + kPinyinHanziLeading;
 
    if (pinyin)
    {
        NSEnumerator *enumerator = [pinyin objectEnumerator];
        NSString *py;
 
        while (py = [enumerator nextObject])
        {
            // Center the pinyin
            float shift = (_advance - [py sizeWithAttributes: _pyAttributes].width) / 2;
 
            [py drawAtPoint: NSMakePoint(x + shift, y)
             withAttributes: _pyAttributes];
 
            x += _advance;
        }
    }
 
    return x;
}
 
- (void) drawRect: (NSRect) rect
{
    [[NSColor blackColor] set];
    NSRectFill(rect);
 
    NSEnumerator *enumerator = [_items objectEnumerator];
    PYMarkerItem *item;
 
    NSPoint p = NSMakePoint(kPinyinViewBorder,
                            kPinyinViewBorder);
 
    while (item = [enumerator nextObject])
        if ([item type] == 1)
            p.x = [self drawItem: item atPoint: p] + kMarkerItemInterspacing;
}
 
- (void) appendMarkerItem: (PYMarkerItem *) item
{
    [_items addObject: item];
}
 
- (void) clearMarkerItems
{
    [_items removeAllObjects];
}
 
- (void) dealloc
{
    [_items release];
    [_hanAttributes release];
    [_pyAttributes release];
    
    [super dealloc];
}
 
@end