-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPreView.mm
More file actions
81 lines (65 loc) · 2.26 KB
/
Copy pathPreView.mm
File metadata and controls
81 lines (65 loc) · 2.26 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
#include <string.h>
#import "PreView.h"
@implementation PreView
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
// Add initialization code here
count = 0;
}
return self;
}
- (void)drawRect:(NSRect)rect
{
CGRect region = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSetGrayFillColor(context, 0.6, 1.0);
CGContextFillRect(context, region);
CGContextSetGrayFillColor(context, 0.0, 1.0);
NeoFont *font = [neoFontEditor font];
int character_height = font->height();
float display_height = region.size.height;
float pixel_size = 2;
if ((pixel_size * character_height) + 1 >= display_height)
{
pixel_size = (display_height - 1) / character_height;
}
float vertical_offset = (display_height - ((float)character_height * pixel_size)) / 2.0;
CGContextSetGrayFillColor(context, 0.3, 1.0);
CGContextSetGrayStrokeColor(context, 0.5, 1.0);
float x = 4.0; // Leave some space to the left of the display.
NSData *encoded_data = [[neoFontEditor previewString] dataUsingEncoding:NSWindowsCP1252StringEncoding allowLossyConversion:YES];
const unsigned char *text = (const unsigned char *) [encoded_data bytes];
const unsigned char *end = text + [encoded_data length];
count = 0;
while (text < end)
{
int code = *text ++;
if (count < NeoFontEditorPreviewMaxCharacters)
{
positions[count] = x;
characters[count] = code;
count ++;
}
NeoCharacter *character = font->character(code);
[neoFontEditor renderCharacter:character context:context x:x y:vertical_offset size:pixel_size];
x += character->width() * pixel_size;
}
positions[count] = x;
}
- (void)mouseDown:(NSEvent *)event
{
NSPoint eventLocation = [event locationInWindow];
NSPoint hit = [self convertPoint:eventLocation fromView:nil];
for (unsigned i = 0; i < count; i++)
{
if (hit.x >= positions[i] && hit.x < positions[i+1])
{
[neoFontEditor setCharacterNumber:characters[i]];
break;
}
}
}
@end