summaryrefslogtreecommitdiff
path: root/frontends/cocoa/plotter.m
blob: dea3245bb8a4cb5bdb914294a91a3a977486ddc2 (plain)
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
/*
 * Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */

#include <Cocoa/Cocoa.h>

#import "utils/log.h"
#import "utils/utils.h"
#import "netsurf/browser_window.h"
#import "netsurf/plotters.h"

#import "cocoa/font.h"
#import "cocoa/coordinates.h"
#import "cocoa/plotter.h"
#import "cocoa/bitmap.h"

static void cocoa_plot_render_path(NSBezierPath *path,const plot_style_t *pstyle);
static void cocoa_plot_path_set_stroke_pattern(NSBezierPath *path,const plot_style_t *pstyle);
static inline void cocoa_center_pixel( bool x, bool y );

static NSRect cocoa_plot_clip_rect;

#define colour_red_component( c )		(((c) >>  0) & 0xFF)
#define colour_green_component( c )		(((c) >>  8) & 0xFF)
#define colour_blue_component( c )		(((c) >> 16) & 0xFF)
#define colour_alpha_component( c )		(((c) >> 24) & 0xFF)
#define colour_from_rgba( r, g, b, a)	((((colour)(r)) <<  0) | \
										 (((colour)(g)) <<  8) | \
										 (((colour)(b)) << 16) | \
										 (((colour)(a)) << 24))
#define colour_from_rgb( r, g, b ) colour_from_rgba( (r), (g), (b), 0xFF )

NSColor *cocoa_convert_colour( colour clr )
{
	return [NSColor colorWithDeviceRed: (float)colour_red_component( clr ) / 0xFF 
								 green: (float)colour_green_component( clr ) / 0xFF 
								  blue: (float)colour_blue_component( clr ) / 0xFF 
								 alpha: 1.0];
}

static void cocoa_plot_path_set_stroke_pattern(NSBezierPath *path,const plot_style_t *pstyle) 
{
	static const CGFloat dashed_pattern[2] = { 5.0, 2.0 };
	static const CGFloat dotted_pattern[2] = { 2.0, 2.0 };
	
	switch (pstyle->stroke_type) {
		case PLOT_OP_TYPE_DASH: 
			[path setLineDash: dashed_pattern count: 2 phase: 0];
			break;
			
		case PLOT_OP_TYPE_DOT: 
			[path setLineDash: dotted_pattern count: 2 phase: 0];
			break;
			
		default:
			// ignore
			break;
	}

	[path setLineWidth: cocoa_px_to_pt( pstyle->stroke_width > 0 ? pstyle->stroke_width : 1 )];
}

static bool plot_line(int x0, int y0, int x1, int y1, const plot_style_t *pstyle)
{
	if (pstyle->stroke_type == PLOT_OP_TYPE_NONE) return true;
	
	[NSGraphicsContext saveGraphicsState];
	[NSBezierPath clipRect: cocoa_plot_clip_rect];
	
	NSBezierPath *path = [NSBezierPath bezierPath];
	[path moveToPoint: cocoa_point( x0, y0 )];
	[path lineToPoint: cocoa_point( x1, y1 )];
	cocoa_plot_path_set_stroke_pattern( path, pstyle );
	
	const bool horizontal = y0 == y1;
	const bool vertical = x0 == x1;
	const bool oddThickness = pstyle->stroke_width != 0 ? (pstyle->stroke_width % 2) != 0 : true;
	
	if (oddThickness) cocoa_center_pixel( !horizontal, !vertical );
	
	[cocoa_convert_colour( pstyle->stroke_colour ) set];
	[path stroke];
	
	[NSGraphicsContext restoreGraphicsState];
	
	return true;
}

static bool plot_rectangle(int x0, int y0, int x1, int y1, const plot_style_t *pstyle)
{
	NSRect rect = cocoa_rect( x0, y0, x1, y1 );
	NSBezierPath *path = [NSBezierPath bezierPathWithRect: rect];
	cocoa_plot_render_path( path, pstyle );
	
	return true;
}

static bool plot_text(int x, int y, const char *text, size_t length,
			 const plot_font_style_t *fstyle)
{
	[NSGraphicsContext saveGraphicsState];
	[NSBezierPath clipRect: cocoa_plot_clip_rect];
	
	cocoa_draw_string( cocoa_px_to_pt( x ), cocoa_px_to_pt( y ), text, length, fstyle );
	
	[NSGraphicsContext restoreGraphicsState];
	
	return true;
}

void cocoa_set_clip( NSRect rect )
{
	cocoa_plot_clip_rect = rect;
}

static bool plot_clip(const struct rect *clip)
{
	cocoa_plot_clip_rect = cocoa_rect( clip->x0, clip->y0, clip->x1, clip->y1 );
	return true;
}

void cocoa_plot_render_path(NSBezierPath *path,const plot_style_t *pstyle) 
{
	[NSGraphicsContext saveGraphicsState];
	[NSBezierPath clipRect: cocoa_plot_clip_rect];
	
	if (pstyle->fill_type != PLOT_OP_TYPE_NONE) {
		[cocoa_convert_colour( pstyle->fill_colour ) setFill];
		[path fill];
	}
	
	if (pstyle->stroke_type != PLOT_OP_TYPE_NONE) {
		if (pstyle->stroke_width == 0 || pstyle->stroke_width % 2 != 0) cocoa_center_pixel( true, true );
		
		cocoa_plot_path_set_stroke_pattern(path,pstyle);
		
		[cocoa_convert_colour( pstyle->stroke_colour ) set];
		
		[path stroke];
	}
	
	[NSGraphicsContext restoreGraphicsState];
}

static bool plot_arc(int x, int y, int radius, int angle1, int angle2, const plot_style_t *pstyle)
{
	NSBezierPath *path = [NSBezierPath bezierPath];
	[path appendBezierPathWithArcWithCenter: NSMakePoint( x, y ) radius: radius 
								 startAngle: angle1 endAngle: angle2 
								  clockwise: NO];
	
	cocoa_plot_render_path( path, pstyle);
	
	return true;
}

static bool plot_disc(int x, int y, int radius, const plot_style_t *pstyle)
{
	NSBezierPath *path  = [NSBezierPath bezierPathWithOvalInRect: 
						   NSMakeRect( x - radius, y-radius, 2*radius, 2*radius )];
	
	cocoa_plot_render_path( path, pstyle );
	
	return true;
}

static bool plot_polygon(const int *p, unsigned int n, const plot_style_t *pstyle)
{
	if (n <= 1) return true;
	
	NSBezierPath *path = [NSBezierPath bezierPath];
	[path moveToPoint: cocoa_point( p[0], p[1] )];
	for (unsigned i = 1; i < n; i++) {
		[path lineToPoint: cocoa_point( p[2*i], p[2*i+1] )];
	}
	[path closePath];
	
	cocoa_plot_render_path( path, pstyle );
	
	return true;
}

/* complex path (for SVG) */
static bool plot_path(const float *p, unsigned int n, colour fill, float width,
			 colour c, const float transform[6])
{
	if (n == 0) return true;
	
	if (*p != PLOTTER_PATH_MOVE) {
		LOG("Path does not start with move");
		return false;
	}
	
	NSBezierPath *path = [NSBezierPath bezierPath];

#define NEXT_POINT() NSMakePoint( *p++, *p++ )
	
	while (n--) {
		switch ((int)*p++) {
			case PLOTTER_PATH_MOVE: {
				const NSPoint pt = NEXT_POINT();
				[path moveToPoint: pt];
				break;
			}
				
			case PLOTTER_PATH_LINE: {
				const NSPoint pt = NEXT_POINT();
				[path lineToPoint: pt];
				break;
			}
				
			case PLOTTER_PATH_BEZIER: {
				const NSPoint cp1 = NEXT_POINT();
				const NSPoint cp2 = NEXT_POINT();
				const NSPoint ep = NEXT_POINT();
				[path curveToPoint: ep controlPoint1: cp1 controlPoint2: cp2];
				break;
			}
				
			case PLOTTER_PATH_CLOSE:
				[path closePath];
				break;
				
			default:
				LOG("Invalid path");
				return false;
		}
	}
	
#undef NEXT_POINT
	
	[path setLineWidth: width];

	CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
	CGContextSaveGState( context );
	
	CGContextClipToRect( context, NSRectToCGRect( cocoa_plot_clip_rect ) );
	
	CGContextConcatCTM( context, CGAffineTransformMake( transform[0], transform[1], transform[2], 
													    transform[3], transform[4], transform[5] ) );
	
	if (fill != NS_TRANSPARENT) {
		[cocoa_convert_colour( fill ) setFill];
		[path fill];
	}

	if (c != NS_TRANSPARENT) {
		cocoa_center_pixel( true, true );
		[cocoa_convert_colour( c ) set];
		[path stroke];
	}
	
	CGContextRestoreGState( context );

	return true;
}

/* Image */
static bool plot_bitmap(int x, int y, int width, int height,
			   struct bitmap *bitmap, colour bg,
			   bitmap_flags_t flags)
{
	CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
	CGContextSaveGState( context );

	CGContextClipToRect( context, NSRectToCGRect( cocoa_plot_clip_rect ) );
	
	const bool tileX = flags & BITMAPF_REPEAT_X;
	const bool tileY = flags & BITMAPF_REPEAT_Y;

	CGImageRef img = cocoa_get_cgimage( bitmap );

	CGRect rect = NSRectToCGRect( cocoa_rect_wh( x, y, width, height ) );
	
	if (tileX || tileY) {
		CGContextDrawTiledImage( context, rect, img );
	} else {
		CGContextDrawImage( context, rect, img );
	}
	
	CGContextRestoreGState( context );
	
	return true;
}

const struct plotter_table cocoa_plotters = {
	.clip = plot_clip,
	.arc = plot_arc,
	.disc = plot_disc,
	.rectangle = plot_rectangle,
	.line = plot_line,
	.polygon = plot_polygon,
	
	.path = plot_path,
	
	.bitmap = plot_bitmap,
	
	.text = plot_text,

	.option_knockout = true
};


CGFloat cocoa_scale_factor;
static const CGFloat points_per_inch = 72.0;
static CGFloat cocoa_half_pixel;

void cocoa_update_scale_factor( void )
{
	const CGFloat scale = [[NSScreen mainScreen] userSpaceScaleFactor];
	cocoa_scale_factor = scale == 1.0 ? 1.0 : 1.0 / scale;
	cocoa_half_pixel = 0.5 * cocoa_scale_factor;
        browser_set_dpi( points_per_inch * scale );
}

static inline void cocoa_center_pixel( bool x, bool y ) 
{
	NSAffineTransform *transform = [NSAffineTransform transform];
	[transform translateXBy: x ? cocoa_half_pixel : 0.0 yBy: y ? cocoa_half_pixel : 0.0];
	[transform concat];
}