3 Part of the swftools package.
5 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #include "../../config.h"
33 #include "../gfxdevice.h"
34 #include "../gfxtools.h"
40 typedef struct _internal {
41 gfxfontlist_t* fontlist;
46 typedef struct _internal_result {
52 #define OP_SETPARAM 0x01
53 #define OP_STROKE 0x02
54 #define OP_STARTCLIP 0x03
55 #define OP_ENDCLIP 0x04
57 #define OP_FILLBITMAP 0x06
58 #define OP_FILLGRADIENT 0x07
59 #define OP_ADDFONT 0x08
60 #define OP_DRAWCHAR 0x09
61 #define OP_DRAWLINK 0x0a
62 #define OP_STARTPAGE 0x0b
63 #define OP_ENDPAGE 0x0c
64 #define OP_FINISH 0x0d
66 #define OP_MOVETO 0x0e
67 #define OP_LINETO 0x0f
68 #define OP_SPLINETO 0x10
70 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
72 internal_t*i = (internal_t*)dev->internal;
73 msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
74 writer_writeU8(&i->w, OP_SETPARAM);
75 writer_writeString(&i->w, key);
76 writer_writeString(&i->w, value);
80 static void dumpLine(writer_t*w, gfxline_t*line)
83 if(line->type == gfx_moveTo) {
84 writer_writeU8(w, OP_MOVETO);
85 writer_writeDouble(w, line->x);
86 writer_writeDouble(w, line->y);
87 } else if(line->type == gfx_lineTo) {
88 writer_writeU8(w, OP_LINETO);
89 writer_writeDouble(w, line->x);
90 writer_writeDouble(w, line->y);
91 } else if(line->type == gfx_splineTo) {
92 writer_writeU8(w, OP_SPLINETO);
93 writer_writeDouble(w, line->x);
94 writer_writeDouble(w, line->y);
95 writer_writeDouble(w, line->sx);
96 writer_writeDouble(w, line->sy);
100 writer_writeU8(w, OP_END);
102 static gfxline_t* readLine(reader_t*r)
104 gfxline_t*start = 0, *pos = 0;
106 unsigned char op = reader_readU8(r);
109 gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
116 if(op == OP_MOVETO) {
117 line->type = gfx_moveTo;
118 line->x = reader_readDouble(r);
119 line->y = reader_readDouble(r);
120 } else if(op == OP_LINETO) {
121 line->type = gfx_lineTo;
122 line->x = reader_readDouble(r);
123 line->y = reader_readDouble(r);
124 } else if(op == OP_SPLINETO) {
125 line->type = gfx_splineTo;
126 line->x = reader_readDouble(r);
127 line->y = reader_readDouble(r);
128 line->sx = reader_readDouble(r);
129 line->sy = reader_readDouble(r);
134 static gfximage_t readImage(reader_t*r)
137 img.width = reader_readU16(r);
138 img.height = reader_readU16(r);
139 img.data = (gfxcolor_t*)rfx_alloc(img.width*img.height*4);
140 r->read(r, img.data, img.width*img.height*4);
143 static gfxmatrix_t readMatrix(reader_t*r)
146 matrix.m00 = reader_readDouble(r);
147 matrix.m01 = reader_readDouble(r);
148 matrix.m10 = reader_readDouble(r);
149 matrix.m11 = reader_readDouble(r);
150 matrix.tx = reader_readDouble(r);
151 matrix.ty = reader_readDouble(r);
154 static gfxcolor_t readColor(reader_t*r)
157 col.r = reader_readU8(r);
158 col.g = reader_readU8(r);
159 col.b = reader_readU8(r);
160 col.a = reader_readU8(r);
163 static gfxgradient_t* readGradient(reader_t*r)
165 gfxgradient_t*start = 0, *pos = 0;
167 U8 op = reader_readU8(r);
170 gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
177 g->color = readColor(r);
178 g->pos = reader_readFloat(r);
182 static gfxcxform_t* readCXForm(reader_t*r)
184 U8 type = reader_readU8(r);
187 gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
188 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
189 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
190 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
191 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
195 static void dumpColor(writer_t*w, gfxcolor_t*color)
197 writer_writeU8(w, color->r);
198 writer_writeU8(w, color->g);
199 writer_writeU8(w, color->b);
200 writer_writeU8(w, color->a);
202 static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
204 writer_writeDouble(w, matrix->m00);
205 writer_writeDouble(w, matrix->m01);
206 writer_writeDouble(w, matrix->m10);
207 writer_writeDouble(w, matrix->m11);
208 writer_writeDouble(w, matrix->tx);
209 writer_writeDouble(w, matrix->ty);
211 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
214 writer_writeU8(w, 1);
215 dumpColor(w, &gradient->color);
216 writer_writeFloat(w, gradient->pos);
217 gradient = gradient->next;
219 writer_writeU8(w, 0);
221 static void dumpImage(writer_t*w, gfximage_t*image)
223 writer_writeU16(w, image->width);
224 writer_writeU16(w, image->height);
225 w->write(w, image->data, image->width*image->height*4);
227 static void dumpCXForm(writer_t*w, gfxcxform_t*c)
230 writer_writeU8(w, 0);
232 writer_writeU8(w, 1);
233 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
234 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
235 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
236 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
239 static void dumpFont(writer_t*w, gfxfont_t*font)
241 writer_writeString(w, font->id);
242 writer_writeU32(w, font->num_glyphs);
243 writer_writeU32(w, font->max_unicode);
245 for(t=0;t<font->num_glyphs;t++) {
246 dumpLine(w, font->glyphs[t].line);
247 writer_writeDouble(w, font->glyphs[t].advance);
248 writer_writeU32(w, font->glyphs[t].unicode);
249 if(font->glyphs[t].name) {
250 writer_writeString(w,font->glyphs[t].name);
255 for(t=0;t<font->max_unicode;t++) {
256 writer_writeU32(w, font->unicode2glyph[t]);
259 static gfxfont_t*readFont(reader_t*r)
261 gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
262 font->id = reader_readString(r);
263 font->num_glyphs = reader_readU32(r);
264 font->max_unicode = reader_readU32(r);
265 font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
266 font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
268 for(t=0;t<font->num_glyphs;t++) {
269 font->glyphs[t].line = readLine(r);
270 font->glyphs[t].advance = reader_readDouble(r);
271 font->glyphs[t].unicode = reader_readU32(r);
272 font->glyphs[t].name = reader_readString(r);
273 if(!font->glyphs[t].name[0]) {
274 free((void*)(font->glyphs[t].name));
275 font->glyphs[t].name = 0;
278 for(t=0;t<font->max_unicode;t++) {
279 font->unicode2glyph[t] = reader_readU32(r);
284 static void record_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
286 internal_t*i = (internal_t*)dev->internal;
287 msg("<trace> record: %08x STROKE\n", dev);
288 writer_writeU8(&i->w, OP_STROKE);
289 writer_writeDouble(&i->w, width);
290 writer_writeDouble(&i->w, miterLimit);
291 dumpColor(&i->w, color);
292 writer_writeU8(&i->w, cap_style);
293 writer_writeU8(&i->w, joint_style);
294 dumpLine(&i->w, line);
297 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
299 internal_t*i = (internal_t*)dev->internal;
300 msg("<trace> record: %08x STARTCLIP\n", dev);
301 writer_writeU8(&i->w, OP_STARTCLIP);
302 dumpLine(&i->w, line);
306 static void record_endclip(struct _gfxdevice*dev)
308 internal_t*i = (internal_t*)dev->internal;
309 msg("<trace> record: %08x ENDCLIP\n", dev);
310 writer_writeU8(&i->w, OP_ENDCLIP);
313 msg("<error> record: endclip() without startclip()");
317 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
319 internal_t*i = (internal_t*)dev->internal;
320 msg("<trace> record: %08x FILL\n", dev);
321 writer_writeU8(&i->w, OP_FILL);
322 dumpColor(&i->w, color);
323 dumpLine(&i->w, line);
326 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
328 internal_t*i = (internal_t*)dev->internal;
329 msg("<trace> record: %08x FILLBITMAP\n", dev);
330 writer_writeU8(&i->w, OP_FILLBITMAP);
331 dumpImage(&i->w, img);
332 dumpMatrix(&i->w, matrix);
333 dumpLine(&i->w, line);
334 dumpCXForm(&i->w, cxform);
337 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
339 internal_t*i = (internal_t*)dev->internal;
340 msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
341 writer_writeU8(&i->w, OP_FILLGRADIENT);
342 writer_writeU8(&i->w, type);
343 dumpGradient(&i->w, gradient);
344 dumpMatrix(&i->w, matrix);
345 dumpLine(&i->w, line);
348 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
350 internal_t*i = (internal_t*)dev->internal;
351 msg("<trace> record: %08x ADDFONT %s\n", dev);
352 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
353 writer_writeU8(&i->w, OP_ADDFONT);
354 dumpFont(&i->w, font);
355 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
359 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
361 internal_t*i = (internal_t*)dev->internal;
362 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
363 record_addfont(dev, font);
366 msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
367 writer_writeU8(&i->w, OP_DRAWCHAR);
369 writer_writeString(&i->w, font->id);
371 writer_writeString(&i->w, "*NULL*");
372 writer_writeU32(&i->w, glyphnr);
373 dumpColor(&i->w, color);
374 dumpMatrix(&i->w, matrix);
377 static void record_startpage(struct _gfxdevice*dev, int width, int height)
379 internal_t*i = (internal_t*)dev->internal;
380 msg("<trace> record: %08x STARTPAGE\n", dev);
381 writer_writeU8(&i->w, OP_STARTPAGE);
382 writer_writeU16(&i->w, width);
383 writer_writeU16(&i->w, height);
386 static void record_endpage(struct _gfxdevice*dev)
388 internal_t*i = (internal_t*)dev->internal;
389 msg("<trace> record: %08x ENDPAGE\n", dev);
390 writer_writeU8(&i->w, OP_ENDPAGE);
393 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
395 internal_t*i = (internal_t*)dev->internal;
396 msg("<trace> record: %08x DRAWLINK\n", dev);
397 writer_writeU8(&i->w, OP_DRAWLINK);
398 dumpLine(&i->w, line);
399 writer_writeString(&i->w, action);
402 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, void*data, int length)
406 i = (internal_t*)dev->internal;
411 reader_init_memreader(r, data, length);
412 gfxfontlist_t* fontlist = gfxfontlist_create();
414 while(r->pos < length) {
415 unsigned char op = reader_readU8(r);
420 msg("<trace> replay: SETPARAM");
423 key = reader_readString(r);
424 value = reader_readString(r);
425 out->setparameter(out, key, value);
431 msg("<trace> replay: STARTPAGE");
432 U16 width = reader_readU16(r);
433 U16 height = reader_readU16(r);
434 out->startpage(out, width, height);
438 msg("<trace> replay: ENDPAGE");
442 msg("<trace> replay: FINISH");
446 msg("<trace> replay: STROKE");
447 double width = reader_readDouble(r);
448 double miterlimit = reader_readDouble(r);
449 gfxcolor_t color = readColor(r);
451 int v = reader_readU8(r);
453 case 0: captype = gfx_capButt; break;
454 case 1: captype = gfx_capRound; break;
455 case 2: captype = gfx_capSquare; break;
457 gfx_joinType jointtype;
458 v = reader_readU8(r);
460 case 0: jointtype = gfx_joinMiter; break;
461 case 1: jointtype = gfx_joinRound; break;
462 case 2: jointtype = gfx_joinBevel; break;
464 gfxline_t* line = readLine(r);
465 out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
470 msg("<trace> replay: STARTCLIP");
471 gfxline_t* line = readLine(r);
472 out->startclip(out, line);
477 msg("<trace> replay: ENDCLIP");
482 msg("<trace> replay: FILL");
483 gfxcolor_t color = readColor(r);
484 gfxline_t* line = readLine(r);
485 out->fill(out, line, &color);
489 case OP_FILLBITMAP: {
490 msg("<trace> replay: FILLBITMAP");
491 gfximage_t img = readImage(r);
492 gfxmatrix_t matrix = readMatrix(r);
493 gfxline_t* line = readLine(r);
494 gfxcxform_t* cxform = readCXForm(r);
495 out->fillbitmap(out, line, &img, &matrix, cxform);
499 free(img.data);img.data=0;
502 case OP_FILLGRADIENT: {
503 msg("<trace> replay: FILLGRADIENT");
504 gfxgradienttype_t type;
505 int v = reader_readU8(r);
508 type = gfxgradient_radial; break;
510 type = gfxgradient_linear; break;
512 gfxgradient_t*gradient = readGradient(r);
513 gfxmatrix_t matrix = readMatrix(r);
514 gfxline_t* line = readLine(r);
515 out->fillgradient(out, line, gradient, type, &matrix);
519 msg("<trace> replay: DRAWLINK");
520 gfxline_t* line = readLine(r);
521 char* s = reader_readString(r);
522 out->drawlink(out,line,s);
528 msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
529 gfxfont_t*font = readFont(r);
530 fontlist = gfxfontlist_addfont(fontlist, font);
531 out->addfont(out, font);
535 char* id = reader_readString(r);
536 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
538 font = gfxfontlist_findfont(i->fontlist, id);
540 U32 glyph = reader_readU32(r);
541 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
542 gfxcolor_t color = readColor(r);
543 gfxmatrix_t matrix = readMatrix(r);
544 out->drawchar(out, font, glyph, &color, &matrix);
552 gfxfontlist_free(fontlist, 1);
554 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
556 internal_result_t*i = (internal_result_t*)result->internal;
557 replay(0, device, i->data, i->length);
560 static void record_result_write(gfxresult_t*r, int filedesc)
562 internal_result_t*i = (internal_result_t*)r->internal;
563 write(filedesc, i->data, i->length);
565 static int record_result_save(gfxresult_t*r, const char*filename)
567 internal_result_t*i = (internal_result_t*)r->internal;
568 FILE*fi = fopen(filename, "wb");
570 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
573 fwrite(i->data, i->length, 1, fi);
577 static void*record_result_get(gfxresult_t*r, const char*name)
579 internal_result_t*i = (internal_result_t*)r->internal;
580 if(!strcmp(name, "data")) {
582 } else if(!strcmp(name, "length")) {
587 static void record_result_destroy(gfxresult_t*r)
589 internal_result_t*i = (internal_result_t*)r->internal;
591 free(i->data);i->data = 0;
593 free(r->internal);r->internal = 0;
597 static unsigned char printable(unsigned char a)
599 if(a<32 || a==127) return '.';
603 static void hexdumpMem(unsigned char*data, int len)
608 printf("%02x ", data[t]);
609 ascii[t&15] = printable(data[t]);
610 if((t && ((t&15)==15)) || (t==len-1))
614 for(s=p-1;s<16;s++) {
617 printf(" %s\n", ascii);
622 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
624 internal_t*i = (internal_t*)dev->internal;
627 void*data = writer_growmemwrite_memptr(&i->w, &len);
628 replay(dev, out, data, len);
630 writer_growmemwrite_reset(&i->w);
633 static gfxresult_t* record_finish(struct _gfxdevice*dev)
635 internal_t*i = (internal_t*)dev->internal;
636 msg("<trace> record: %08x END", dev);
639 msg("<error> Warning: unclosed cliplevels");
642 writer_writeU8(&i->w, OP_END);
644 gfxfontlist_free(i->fontlist, 0);
646 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
647 ir->data = writer_growmemwrite_getmem(&i->w);
648 ir->length = i->w.pos;
651 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
652 result->save = record_result_save;
653 result->get = record_result_get;
654 result->destroy = record_result_destroy;
655 result->internal = ir;
657 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
661 void gfxdevice_record_init(gfxdevice_t*dev)
663 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
664 memset(dev, 0, sizeof(gfxdevice_t));
666 dev->name = "record";
670 writer_init_growingmemwriter(&i->w, 1048576);
671 i->fontlist = gfxfontlist_create();
674 dev->setparameter = record_setparameter;
675 dev->startpage = record_startpage;
676 dev->startclip = record_startclip;
677 dev->endclip = record_endclip;
678 dev->stroke = record_stroke;
679 dev->fill = record_fill;
680 dev->fillbitmap = record_fillbitmap;
681 dev->fillgradient = record_fillgradient;
682 dev->addfont = record_addfont;
683 dev->drawchar = record_drawchar;
684 dev->drawlink = record_drawlink;
685 dev->endpage = record_endpage;
686 dev->finish = record_finish;