summaryrefslogtreecommitdiff
path: root/moo/dev_ghost.c
blob: 30ba5410f5db82b39241de9434fa750b541bc49d (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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include "mooscript.h"

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H

typedef struct fz_ghost_device_s fz_ghost_device;

struct fz_ghost_device_s
{
	gs_memory_t *memory;
	gs_state *pgs;
	gs_font_dir *fontdir;
};

static gs_matrix
gs_matrix_from_fz_matrix(fz_matrix ctm)
{
	gs_matrix m;
	m.xx = ctm.a;
	m.xy = ctm.b;
	m.yx = ctm.c;
	m.yy = ctm.d;
	m.tx = ctm.e;
	m.ty = ctm.f;
	return m;
}

static void
fz_ghost_matrix(fz_context *ctx, gs_state *pgs, fz_matrix ctm)
{
	gs_matrix m = gs_matrix_from_fz_matrix(ctm);
	gs_setmatrix(pgs, &m);
}

static void
fz_ghost_color(fz_context *ctx, gs_state *pgs, fz_colorspace *colorspace, float *color, float alpha)
{
	gs_setopacityalpha(pgs, alpha);
	if (colorspace == fz_device_gray)
		gs_setgray(pgs, color[0]);
	else if (colorspace == fz_device_rgb)
		gs_setrgbcolor(pgs, color[0], color[1], color[2]);
	else if (colorspace == fz_device_cmyk)
		gs_setcmykcolor(pgs, color[0], color[1], color[2], color[3]);
	else {
		float rgb[3];
		fz_convert_color(ctx, colorspace, color, fz_device_rgb, rgb);
		gs_setrgbcolor(pgs, rgb[0], rgb[1], rgb[2]);
	}
}

static void
fz_ghost_path(fz_context *ctx, gs_state *pgs, fz_path *path, int indent)
{
	float x, y, x2, y2, x3, y3;
	int i = 0;
	while (i < path->len)
	{
		switch (path->items[i++].k)
		{
		case FZ_MOVETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			gs_moveto(pgs, x, y);
			break;
		case FZ_LINETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			gs_lineto(pgs, x, y);
			break;
		case FZ_CURVETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			x2 = path->items[i++].v;
			y2 = path->items[i++].v;
			x3 = path->items[i++].v;
			y3 = path->items[i++].v;
			gs_curveto(pgs, x, y, x2, y2, x3, y3);
			break;
		case FZ_CLOSE_PATH:
			gs_closepath(pgs);
			break;
		}
	}
}

static int move_to(const FT_Vector *p, void *pgs)
{
	gs_moveto(pgs, p->x / 64.0f, p->y / 64.0f);
	return 0;
}

static int line_to(const FT_Vector *p, void *pgs)
{
	gs_lineto(pgs, p->x / 64.0f, p->y / 64.0f);
	return 0;
}

static int conic_to(const FT_Vector *c, const FT_Vector *p, void *pgs)
{
	gs_point s, c1, c2;
	float cx = c->x / 64.0f, cy = c->y / 64.0f;
	float px = p->x / 64.0f, py = p->y / 64.0f;
	gs_currentpoint(pgs, &s);
	c1.x = (s.x + cx * 2) / 3;
	c1.y = (s.y + cy * 2) / 3;
	c2.x = (px + cx * 2) / 3;
	c2.y = (py + cy * 2) / 3;
	gs_curveto(pgs, c1.x, c1.y, c2.x, c2.y, px, py);
	return 0;
}

static int cubic_to(const FT_Vector *c1, const FT_Vector *c2, const FT_Vector *p, void *pgs)
{
	gs_curveto(pgs, c1->x/64.0f, c1->y/64.0f, c2->x/64.0f, c2->y/64.0f, p->x/64.0f, p->y/64.0f);
	return 0;
}

static const FT_Outline_Funcs outline_funcs = {
	move_to, line_to, conic_to, cubic_to, 0, 0
};

static int
build_char(gs_show_enum *penum, gs_state *pgs, gs_font *gsfont, gs_char cid, gs_glyph gid)
{
	fz_font *fzfont = gsfont->client_data;
	float w2[6] = { 0, 1, -1, -1, 2, 2 };
	gs_fixed_point saved_adjust;

	gs_setcachedevice(penum, pgs, w2);

	if (fzfont->ft_face) {	
		FT_Face face = fzfont->ft_face;
		FT_Set_Char_Size(face, 64, 64, 72, 72);
		FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
		gs_moveto(pgs, 0, 0);
		FT_Outline_Decompose(&face->glyph->outline, &outline_funcs, pgs);
		gs_closepath(pgs);
	} else {
		// TODO: type3 fonts
	}

	/* enable dropout prevention by fiddling with fill_adjust */
	saved_adjust = pgs->fill_adjust;
	pgs->fill_adjust.x = -1;
	pgs->fill_adjust.y = -1;
	gs_fill(pgs);
	pgs->fill_adjust = saved_adjust;

	return 0;
}

static gs_font *
make_gs_font(fz_context *ctx, fz_font *fzfont, gs_memory_t *mem, gs_font_dir *dir)
{
	// gs_font is not safe, gxccache assumes all fonts derive from gs_font_base
	gs_font_base *gsfont = fz_malloc(ctx, sizeof *gsfont);

	gsfont->next = 0;
	gsfont->prev = 0;
	gsfont->memory = mem;
	gsfont->dir = dir;
	gsfont->is_resource = 0;
	gs_notify_init(&gsfont->notify_list, gs_memory_stable(mem));
	gsfont->id = gs_next_ids(mem, 1);
	gsfont->base = (gs_font*)gsfont;
	gsfont->client_data = fz_keep_font(ctx, fzfont);

	gs_make_identity(&gsfont->FontMatrix);
	gs_make_identity(&gsfont->orig_FontMatrix);

	gsfont->FontType = ft_user_defined;
	gsfont->BitmapWidths = 0;
	gsfont->ExactSize = fbit_use_outlines;
	gsfont->InBetweenSize = fbit_use_outlines;
	gsfont->TransformedChar = fbit_use_outlines;
	gsfont->WMode = 0;
	gsfont->PaintType = 0;
	gsfont->StrokeWidth = 0;
	gsfont->is_cached = 0;

	gsfont->font_name.size = 0;
	gsfont->key_name.size = 0;

	gsfont->procs.define_font = gs_no_define_font;
	gsfont->procs.make_font = gs_no_make_font;
	gsfont->procs.font_info = gs_default_font_info;
	gsfont->procs.same_font = gs_default_same_font;
	gsfont->procs.encode_char = gs_no_encode_char;
	gsfont->procs.decode_glyph = gs_no_decode_glyph;
	gsfont->procs.enumerate_glyph = gs_no_enumerate_glyph;
	gsfont->procs.glyph_info = gs_default_glyph_info;
	gsfont->procs.glyph_outline = gs_no_glyph_outline;
	gsfont->procs.glyph_name = gs_no_glyph_name;
	gsfont->procs.init_fstack = gs_default_init_fstack;
	gsfont->procs.next_char_glyph = gs_default_next_char_glyph;
	gsfont->procs.build_char = build_char;

	gsfont->FontBBox.p.x = 0;
	gsfont->FontBBox.p.y = 0;
	gsfont->FontBBox.q.x = 0;
	gsfont->FontBBox.q.y = 0;

	uid_set_UniqueID(&gsfont->UID, gsfont->id);

	gsfont->FAPI = 0;
	gsfont->FAPI_font_data = 0;
	gsfont->encoding_index = ENCODING_INDEX_UNKNOWN;
	gsfont->nearest_encoding_index = ENCODING_INDEX_ISOLATIN1;

	gs_definefont(dir, (gs_font*)gsfont);

	return (gs_font*)gsfont;
}

struct entry
{
	fz_font *fzfont;
	gs_font *gsfont;
	struct entry *next;
};

gs_font *
gs_font_from_fz_font(fz_context *ctx, fz_font *fzfont, gs_memory_t *mem, gs_font_dir *dir)
{
	static struct entry *root = NULL;
	struct entry *entry;
	for (entry = root; entry; entry = entry->next)
		if (entry->fzfont == fzfont)
			return entry->gsfont;
	entry = fz_malloc(ctx, sizeof *entry);
	entry->fzfont = fzfont;
	entry->gsfont = make_gs_font(ctx, fzfont, mem, dir);
	entry->next = root;
	root = entry;
	return entry->gsfont;
}

static void
fz_ghost_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm,
	fz_colorspace *colorspace, float *color, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	fz_ghost_color(dev->ctx, ghost->pgs, colorspace, color, alpha);
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	fz_ghost_path(dev->ctx, ghost->pgs, path, 0);
	if (even_odd)
		gs_eofill(ghost->pgs);
	else
		gs_fill(ghost->pgs);
}

static void
fz_ghost_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
	fz_colorspace *colorspace, float *color, float alpha)
{
	fz_ghost_device *ghost = dev->user;

	gs_setlinewidth(ghost->pgs, stroke->linewidth);
	gs_setmiterlimit(ghost->pgs, stroke->miterlimit);
	gs_setlinestartcap(ghost->pgs, stroke->start_cap);
	gs_setlinedashcap(ghost->pgs, stroke->dash_cap);
	gs_setlineendcap(ghost->pgs, stroke->end_cap);
	gs_setlinejoin(ghost->pgs, stroke->linejoin);
	if (stroke->dash_len)
		gs_setdash(ghost->pgs, stroke->dash_list, stroke->dash_len, stroke->dash_phase);

	fz_ghost_color(dev->ctx, ghost->pgs, colorspace, color, alpha);
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	fz_ghost_path(dev->ctx, ghost->pgs, path, 0);

	gs_stroke(ghost->pgs);
}

static void
fz_ghost_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
{
	fz_ghost_device *ghost = dev->user;
	gs_gsave(ghost->pgs);
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	fz_ghost_path(dev->ctx, ghost->pgs, path, 0);
	if (even_odd)
		gs_eoclip(ghost->pgs);
	else
		gs_clip(ghost->pgs);
}

static void
fz_ghost_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
{
	fz_ghost_device *ghost = dev->user;

	gs_gsave(ghost->pgs);

	gs_setlinewidth(ghost->pgs, stroke->linewidth);
	gs_setmiterlimit(ghost->pgs, stroke->miterlimit);
	gs_setlinestartcap(ghost->pgs, stroke->start_cap);
	gs_setlinedashcap(ghost->pgs, stroke->dash_cap);
	gs_setlineendcap(ghost->pgs, stroke->end_cap);
	gs_setlinejoin(ghost->pgs, stroke->linejoin);
	if (stroke->dash_len)
		gs_setdash(ghost->pgs, stroke->dash_list, stroke->dash_len, stroke->dash_phase);

	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	fz_ghost_path(dev->ctx, ghost->pgs, path, 0);

	gs_strokepath(ghost->pgs);
	gs_clip(ghost->pgs);
}

static void
fz_ghost_show_text(fz_device *dev, fz_text *text, fz_matrix ctm, int operation)
{
	fz_ghost_device *ghost = dev->user;
	gs_font *font;
	gs_text_params_t params;
	gs_text_enum_t *textenum;
	gs_glyph *gbuf;
	gs_matrix matrix;
	float *xbuf, *ybuf;
	int i;

	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);

	font = gs_font_from_fz_font(dev->ctx, text->font, ghost->memory, ghost->fontdir);
	gs_setfont(ghost->pgs, font);

	matrix = gs_matrix_from_fz_matrix(text->trm);
	gs_setcharmatrix(ghost->pgs, &matrix);
	gs_matrix_multiply(&matrix, &font->orig_FontMatrix, &font->FontMatrix);

	xbuf = fz_calloc(dev->ctx, text->len, sizeof *xbuf);
	ybuf = fz_calloc(dev->ctx, text->len, sizeof *ybuf);
	gbuf = fz_calloc(dev->ctx, text->len, sizeof *gbuf);

	for (i = 0; i < text->len - 1; i++) {
		gbuf[i] = text->items[i].gid;
		xbuf[i] = text->items[i+1].x - text->items[i].x;
		ybuf[i] = text->items[i+1].y - text->items[i].y;
	}
	gbuf[i] = text->items[i].gid;
	xbuf[i] = 0;
	ybuf[i] = 0;

	params.operation = TEXT_FROM_GLYPHS | TEXT_REPLACE_WIDTHS | operation;
	params.data.glyphs = gbuf;
	params.size = text->len;
	params.x_widths = xbuf;
	params.y_widths = ybuf;
	params.widths_size = text->len;

	gs_moveto(ghost->pgs, text->items[0].x, text->items[0].y);
	gs_text_begin(ghost->pgs, &params, ghost->memory, &textenum);
	gs_text_process(textenum);
	gs_text_release(textenum, "fz_ghost_fill_text");

	fz_free(dev->ctx, gbuf);
	fz_free(dev->ctx, ybuf);
	fz_free(dev->ctx, xbuf);
}

static void
fz_ghost_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
	fz_colorspace *colorspace, float *color, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	fz_ghost_color(dev->ctx, ghost->pgs, colorspace, color, alpha);
	fz_ghost_show_text(dev, text, ctm, TEXT_DO_DRAW);
}

static void
fz_ghost_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
	fz_colorspace *colorspace, float *color, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	printf("<stroke_text font=\"%s\" wmode=\"%d\"\n", text->font->name, text->wmode);
	fz_ghost_color(dev->ctx, ghost->pgs, colorspace, color, alpha);
	fz_ghost_show_text(dev, text, ctm, TEXT_DO_DRAW);
}

static void
fz_ghost_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate)
{
	fz_ghost_device *ghost = dev->user;
	gs_gsave(ghost->pgs);
	printf("<clip_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
	printf("accumulate=\"%d\"\n", accumulate);
}

static void
fz_ghost_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
	fz_ghost_device *ghost = dev->user;
	gs_gsave(ghost->pgs);
	printf("<clip_stroke_text font=\"%s\" wmode=\"%d\"\n", text->font->name, text->wmode);
}

static void
fz_ghost_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
	printf("ignore_text font=\"%s\" wmode=\"%d\"\n", text->font->name, text->wmode);
}

static void
fz_ghost_fill_image(fz_device *dev, fz_image *image, fz_matrix ctm, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
}

static void
fz_ghost_fill_image_mask(fz_device *dev, fz_image *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	fz_ghost_color(dev->ctx, ghost->pgs, colorspace, color, alpha);
}

static void
fz_ghost_clip_image_mask(fz_device *dev, fz_image *image, fz_rect *rect, fz_matrix ctm)
{
	fz_ghost_device *ghost = dev->user;
	gs_gsave(ghost->pgs);
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
}

static void
fz_ghost_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha)
{
	fz_ghost_device *ghost = dev->user;
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
}

static void
fz_ghost_pop_clip(fz_device *dev)
{
	fz_ghost_device *ghost = dev->user;
	gs_grestore(ghost->pgs);
}

static void
fz_ghost_begin_mask(fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, float *color)
{
	printf("<mask bbox=\"%g %g %g %g\" s=\"%s\" ",
		bbox.x0, bbox.y0, bbox.x1, bbox.y1,
		luminosity ? "luminosity" : "alpha");
	printf(">\n");
}

static void
fz_ghost_end_mask(fz_device *dev)
{
	printf("</mask>\n");
}

static void
fz_ghost_begin_group(fz_device *dev, fz_rect bbox, int isolated, int knockout, int blendmode, float alpha)
{
	printf("<group bbox=\"%g %g %g %g\" isolated=\"%d\" knockout=\"%d\" blendmode=\"%s\" alpha=\"%g\">\n",
		bbox.x0, bbox.y0, bbox.x1, bbox.y1,
		isolated, knockout, fz_blendmode_name(blendmode), alpha);
}

static void
fz_ghost_end_group(fz_device *dev)
{
	printf("</group>\n");
}

static void
fz_ghost_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
{
	fz_ghost_device *ghost = dev->user;
	printf("<tile ");
	printf("area=\"%g %g %g %g\" ", area.x0, area.y0, area.x1, area.y1);
	printf("view=\"%g %g %g %g\" ", view.x0, view.y0, view.x1, view.y1);
	printf("xstep=\"%g\" ystep=\"%g\" ", xstep, ystep);
	fz_ghost_matrix(dev->ctx, ghost->pgs, ctm);
	printf(">\n");
}

static void
fz_ghost_end_tile(fz_device *dev)
{
	printf("</tile>\n");
}

static void
fz_ghost_free_user(fz_device *dev)
{
	// free fontdir
}

fz_device *fz_new_ghost_device(fz_context *ctx, gs_memory_t *memory, gs_state *pgs)
{
	fz_device *dev;

	fz_ghost_device *ghost = fz_malloc(ctx, sizeof *ghost);
	ghost->memory = memory;
	ghost->pgs = pgs;

	ghost->fontdir = gs_font_dir_alloc(memory);
	gs_setaligntopixels(ghost->fontdir, 1); /* no subpixels */
	gs_setgridfittt(ghost->fontdir, 1); /* see gx_ttf_outline in gxttfn.c for values */

	dev = fz_new_device(ctx, ghost);
	dev->free_user = fz_ghost_free_user;

	dev->fill_path = fz_ghost_fill_path;
	dev->stroke_path = fz_ghost_stroke_path;
	dev->clip_path = fz_ghost_clip_path;
	dev->clip_stroke_path = fz_ghost_clip_stroke_path;

	dev->fill_text = fz_ghost_fill_text;
	dev->stroke_text = fz_ghost_stroke_text;
	dev->clip_text = fz_ghost_clip_text;
	dev->clip_stroke_text = fz_ghost_clip_stroke_text;
	dev->ignore_text = fz_ghost_ignore_text;

	dev->fill_shade = fz_ghost_fill_shade;
	dev->fill_image = fz_ghost_fill_image;
	dev->fill_image_mask = fz_ghost_fill_image_mask;
	dev->clip_image_mask = fz_ghost_clip_image_mask;

	dev->pop_clip = fz_ghost_pop_clip;

	dev->begin_mask = fz_ghost_begin_mask;
	dev->end_mask = fz_ghost_end_mask;
	dev->begin_group = fz_ghost_begin_group;
	dev->end_group = fz_ghost_end_group;

	dev->begin_tile = fz_ghost_begin_tile;
	dev->end_tile = fz_ghost_end_tile;

	return dev;
}