-
Notifications
You must be signed in to change notification settings - Fork 0
/
paintapi-cocoa.m
407 lines (324 loc) · 12.9 KB
/
paintapi-cocoa.m
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* iPlotter - Port of plot on Mac OS X Cocoa
* Copyright (C) 2006 Teemu Ikonen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* paintapi-cocoa.c
* Plotter
*
* Created by Teemu Ikonen on 3/3/06.
*
*/
#include "paintapi-cocoa.h"
#include "whls.h"
typedef struct {
// pointer to supersructure
paintapi_t api;
// plot instructions and the datasets
my_plot_draw_t *plotdraw;
} paintapi_cocoa_t;
typedef struct {
ATSUTextLayout layout;
// text in UTF-16 format
char *utext;
} paintapi_cocoa_textlayout_t;
typedef struct {
paintapi_rgb_t color; // current color
float *dashes;
int dash_count;
} paintapi_cocoa_gc_t;
static paintapi_rgb_t gWhiteColor = { 0xffff, 0xffff, 0xffff };
static paintapi_gc_t *lastcontext = 0;
static void gc_set_foreground(paintapi_t *api_instance, paintapi_gc_t *gc, paintapi_rgb_t *color) {
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_rgb_t c = *color;
if(cocoaapi->plotdraw->inverse_colors) {
WHLSColor whls;
rgb_to_whls(&c, &whls);
whls.wv = 1 - whls.wv;
whls_to_rgb(&whls, &c);
}
context->color = c;
}
static paintapi_gc_t *gc_new(paintapi_t *api_instance) {
//paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
paintapi_cocoa_gc_t *gc = zmalloc_c(sizeof(paintapi_cocoa_gc_t));
gc_set_foreground(api_instance, (paintapi_gc_t*)gc, &gWhiteColor);
return (paintapi_gc_t*)gc;
}
static void gc_free(paintapi_t *api_instance, paintapi_gc_t *gc) {
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
if(context) {
free(context->dashes);
free(context);
}
}
static void setContextLineDash(CGContextRef myContext, float *dasharray, int dash_list_len)
{
CGContextSetLineDash(myContext, 0 , dasharray, dash_list_len);
}
static void gc_set_dashes(paintapi_t *api_instance, paintapi_gc_t *gc, int dash_offset, const char *dash_list, int dash_list_len) {
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
if(!context->dashes) {
context->dashes = malloc(sizeof(float)*10);
}
context->dash_count = dash_list_len;
int i;
for(i=0; i < dash_list_len && i < 10; i++) {
context->dashes[i] = dash_list[i];
}
}
static void setContextColor(CGContextRef myContext, paintapi_rgb_t *color)
{
paintapi_rgb_t c = *color;
float r = c.r;
float g = c.g;
float b = c.b;
r /= 0xffff;
g /= 0xffff;
b /= 0xffff;
CGContextSetRGBStrokeColor(myContext,r,g,b,1);
CGContextSetRGBFillColor(myContext,r,g,b,1);
}
static void gc_set_function(paintapi_t *api_instance, paintapi_gc_t *gc, paintapi_function_t function) {
/*
CGContextRef myContext = [(NSGraphicsContext *)gc graphicsPort];
switch(function) {
case PAINTAPI_FUNCTION_SET:
CGContextSetBlendMode(myContext,kCGBlendModeNormal);
break;
case PAINTAPI_FUNCTION_XOR:
CGContextSetBlendMode(myContext,kCGBlendModeScreen);
break;
}
*/
}
static void draw_line(paintapi_t *api_instance, paintapi_gc_t *gc, int x1, int y1, int x2, int y2) {
// set some defaults
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
setContextLineDash(myContext,context->dashes, context->dash_count);
}
lastcontext = gc;
CGContextMoveToPoint(myContext,x1,-y1+cocoaapi->plotdraw->plot_ye);
CGContextAddLineToPoint(myContext,x2,-y2+cocoaapi->plotdraw->plot_ye);
CGContextDrawPath(myContext,kCGPathStroke);
}
static void draw_segments(paintapi_t *api_instance, paintapi_gc_t *gc, paintapi_point_t *segment_list, int segment_list_len)
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
setContextLineDash(myContext,context->dashes, context->dash_count);
}
lastcontext = gc;
CGContextBeginPath(myContext);
int idx;
for(idx = 0; idx < 2*segment_list_len ; idx+=2) {
CGContextMoveToPoint(myContext,segment_list[idx].x,-segment_list[idx].y + cocoaapi->plotdraw->plot_ye);
CGContextAddLineToPoint(myContext,segment_list[idx+1].x,-segment_list[idx+1].y + cocoaapi->plotdraw->plot_ye);
}
CGContextDrawPath(myContext,kCGPathStroke);
}
static void draw_lines(paintapi_t *api_instance, paintapi_gc_t *gc, paintapi_point_t *point_list, int point_list_len) {
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
setContextLineDash(myContext,context->dashes, context->dash_count);
}
lastcontext = gc;
CGContextBeginPath(myContext);
CGContextMoveToPoint(myContext,point_list[0].x,-point_list[0].y + cocoaapi->plotdraw->plot_ye);
int idx;
for(idx = 1; idx < point_list_len ; idx++) {
CGContextAddLineToPoint(myContext,point_list[idx].x,-point_list[idx].y + cocoaapi->plotdraw->plot_ye);
}
CGContextDrawPath(myContext,kCGPathStroke);
}
static void draw_closed_path(paintapi_t *api_instance, paintapi_gc_t *gc, pa_boolean filled, paintapi_point_t *point_list, int point_list_len)
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
setContextLineDash(myContext,context->dashes, context->dash_count);
}
lastcontext = gc;
CGContextMoveToPoint(myContext,point_list[0].x,-point_list[0].y + cocoaapi->plotdraw->plot_ye);
int idx;
for(idx = 1; idx < point_list_len ; idx++) {
CGContextAddLineToPoint(myContext,point_list[idx].x,-point_list[idx].y + cocoaapi->plotdraw->plot_ye);
}
CGContextClosePath(myContext);
if(filled) {
CGContextFillPath(myContext);
} else {
CGContextDrawPath(myContext,kCGPathStroke);
}
}
static void draw_rectangle(paintapi_t *api_instance, paintapi_gc_t *gc, pa_boolean filled, int x1, int y1, int x2, int y2) {
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
setContextLineDash(myContext,context->dashes, context->dash_count);
}
lastcontext = gc;
int height = abs(y2-y1)+1;
int width = abs(x2-x1)+1;
// x1/y1 = top/left x2/y2 = right/bottom
y1 = -y1 + cocoaapi->plotdraw->plot_ye;
y2 = -y2 + cocoaapi->plotdraw->plot_ye;
if(filled) {
// use ready function
CGContextFillRect(myContext,CGRectMake(x1,y1-height,width+1,height+1));
} else {
// draw using lines
CGContextMoveToPoint(myContext,x1,y1);
CGContextAddLineToPoint(myContext,x2,y1);
CGContextAddLineToPoint(myContext,x2,y2);
CGContextAddLineToPoint(myContext,x1,y2);
CGContextAddLineToPoint(myContext,x1,y1);
CGContextDrawPath(myContext,kCGPathStroke);
}
}
static paintapi_textlayout_t *textlayout_create(paintapi_t *api_instance, paintapi_font_t *font, const char *text) {
paintapi_cocoa_textlayout_t *layout = zmalloc_c(sizeof(paintapi_cocoa_textlayout_t));
// convert input text (UTF-8) to UTF-16
CFRange r;
CFStringRef cfstr = CFStringCreateWithBytes(NULL, (unsigned char *)text, strlen(text),kCFStringEncodingUTF8, 0);
r.location = 0;
r.length = CFStringGetLength(cfstr);
char *textu = malloc(r.length*2);
CFIndex len;
CFStringGetBytes(cfstr, r,kCFStringEncodingUTF16,0,0,(unsigned char*)textu,r.length*2,&len);
CFRelease(cfstr);
OSStatus status = noErr;
// style runs
UniCharCount styleRuns[] = { kATSUToTextEnd };
status = ATSUCreateTextLayoutWithTextPtr ((UniChar*) textu,
kATSUFromTextBeginning, // offset from beginning
kATSUToTextEnd, // length of text range
r.length, // length of text buffer
1, // number of style runs
styleRuns, // length of the style run
(ATSUStyle *)&font, // array of styles
&layout->layout);
ATSUAttributeTag theTags[] = {kATSULineFlushFactorTag,
kATSULineJustificationFactorTag};
ByteCount theSizes[] = {sizeof(Fract), sizeof(Fract)};
Fract myFlushFactor = kATSUStartAlignment;
Fract myJustFactor = kATSUFullJustification;
ATSUAttributeValuePtr theValues[] = {&myFlushFactor, &myJustFactor};
status = ATSUSetLayoutControls (layout->layout,
2,
theTags,
theSizes,
theValues);
layout->utext = textu;
return (paintapi_textlayout_t *)layout;
}
static void textlayout_free(paintapi_t *api_instance, paintapi_textlayout_t *layout) {
paintapi_cocoa_textlayout_t *clayout = (paintapi_cocoa_textlayout_t *)layout;
ATSUDisposeTextLayout(clayout->layout);
free(clayout->utext);
free(clayout);
}
static void textlayout_calculate_size(paintapi_t *api_instance, paintapi_textlayout_t *layout, paintapi_textlayout_extents_t *size) {
paintapi_cocoa_textlayout_t *clayout = (paintapi_cocoa_textlayout_t *)layout;
OSStatus status = noErr;
Rect outrect;
status = ATSUMeasureTextImage(clayout->layout,
kATSUFromTextBeginning,
kATSUToTextEnd,
0, // only size
0, // only size
&outrect);
size->xl = outrect.left;
size->yt = outrect.top;
size->xr = outrect.right+1;
size->yb = outrect.bottom+1;
}
static void textlayout_set_strikeout(paintapi_t *api_instance, paintapi_textlayout_t *layout) {
// TODO: implement
}
static void draw_textlayout(paintapi_t *api_instance, paintapi_gc_t *gc, int x1, int y1, paintapi_textlayout_t *layout) {
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
paintapi_cocoa_gc_t *context = (paintapi_cocoa_gc_t *)gc;
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
paintapi_cocoa_textlayout_t *clayout = (paintapi_cocoa_textlayout_t*)layout;
if(lastcontext != gc) {
setContextColor(myContext,&context->color);
}
lastcontext = gc;
OSStatus status = noErr;
ATSUAttributeTag theTags[] = { kATSUCGContextTag };
ByteCount theSizes[] = { sizeof (CGContextRef) };
ATSUAttributeValuePtr theValues[] = { &myContext };
ATSUSetLayoutControls (clayout->layout,
1,
theTags,
theSizes,
theValues);
status = ATSUDrawText(clayout->layout,
kATSUFromTextBeginning,
kATSUToTextEnd,
Long2Fix(x1),
Long2Fix(-y1+cocoaapi->plotdraw->plot_ye));
}
static void draw_text(paintapi_t *api_instance, paintapi_gc_t *gc, paintapi_font_t *font, int x1, int y1, const char *text) {
paintapi_textlayout_t *layout = textlayout_create(api_instance, font, text);
draw_textlayout(api_instance, gc, x1, y1, layout);
textlayout_free(api_instance,layout);
}
static void api_free(paintapi_t *api_instance) {
paintapi_cocoa_t *cocoaapi = (paintapi_cocoa_t*)api_instance;
free(cocoaapi);
}
paintapi_t *paintapi_cocoa_new(my_plot_draw_t *plotdraw) {
paintapi_cocoa_t *cocoaapi = zmalloc_c(sizeof(paintapi_cocoa_t));
cocoaapi->api.api_free = api_free;
cocoaapi->api.gc_new = gc_new;
cocoaapi->api.gc_free = gc_free;
cocoaapi->api.gc_set_dashes = gc_set_dashes;
cocoaapi->api.gc_set_foreground = gc_set_foreground;
cocoaapi->api.gc_set_function = gc_set_function;
cocoaapi->api.draw_line = draw_line;
cocoaapi->api.draw_lines = draw_lines;
cocoaapi->api.draw_segments = draw_segments;
cocoaapi->api.draw_closed_path = draw_closed_path;
cocoaapi->api.draw_rectangle = draw_rectangle;
cocoaapi->api.draw_text = draw_text;
cocoaapi->api.draw_textlayout = draw_textlayout;
cocoaapi->api.textlayout_create = textlayout_create;
cocoaapi->api.textlayout_free = textlayout_free;
cocoaapi->api.textlayout_calculate_size = textlayout_calculate_size;
cocoaapi->api.textlayout_set_strikeout = textlayout_set_strikeout;
cocoaapi->plotdraw = plotdraw;
return (paintapi_t*)cocoaapi;
}