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;
45 typedef struct _internal_result {
51 #define OP_SETPARAM 0x01
52 #define OP_STROKE 0x02
53 #define OP_STARTCLIP 0x03
54 #define OP_ENDCLIP 0x04
56 #define OP_FILLBITMAP 0x06
57 #define OP_FILLGRADIENT 0x07
58 #define OP_ADDFONT 0x08
59 #define OP_DRAWCHAR 0x09
60 #define OP_DRAWLINK 0x0a
61 #define OP_STARTPAGE 0x0b
62 #define OP_ENDPAGE 0x0c
63 #define OP_FINISH 0x0d
65 #define OP_MOVETO 0x0e
66 #define OP_LINETO 0x0f
67 #define OP_SPLINETO 0x10
69 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
71 internal_t*i = (internal_t*)dev->internal;
72 msg("<trace> record: SETPARAM %s %s\n", key, value);
73 writer_writeU8(&i->w, OP_SETPARAM);
74 writer_writeString(&i->w, key);
75 writer_writeString(&i->w, value);
79 static void dumpLine(writer_t*w, gfxline_t*line)
82 if(line->type == gfx_moveTo) {
83 writer_writeU8(w, OP_MOVETO);
84 writer_writeDouble(w, line->x);
85 writer_writeDouble(w, line->y);
86 } else if(line->type == gfx_lineTo) {
87 writer_writeU8(w, OP_LINETO);
88 writer_writeDouble(w, line->x);
89 writer_writeDouble(w, line->y);
90 } else if(line->type == gfx_splineTo) {
91 writer_writeU8(w, OP_SPLINETO);
92 writer_writeDouble(w, line->x);
93 writer_writeDouble(w, line->y);
94 writer_writeDouble(w, line->sx);
95 writer_writeDouble(w, line->sy);
99 writer_writeU8(w, OP_END);
101 static gfxline_t* readLine(reader_t*r)
103 gfxline_t*start = 0, *pos = 0;
105 unsigned char op = reader_readU8(r);
108 gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
115 if(op == OP_MOVETO) {
116 line->type = gfx_moveTo;
117 line->x = reader_readDouble(r);
118 line->y = reader_readDouble(r);
119 } else if(op == OP_LINETO) {
120 line->type = gfx_lineTo;
121 line->x = reader_readDouble(r);
122 line->y = reader_readDouble(r);
123 } else if(op == OP_SPLINETO) {
124 line->type = gfx_splineTo;
125 line->x = reader_readDouble(r);
126 line->y = reader_readDouble(r);
127 line->sx = reader_readDouble(r);
128 line->sy = reader_readDouble(r);
133 static gfximage_t readImage(reader_t*r)
136 img.width = reader_readU16(r);
137 img.height = reader_readU16(r);
138 img.data = (gfxcolor_t*)rfx_alloc(img.width*img.height*4);
139 r->read(r, img.data, img.width*img.height*4);
142 static gfxmatrix_t readMatrix(reader_t*r)
145 matrix.m00 = reader_readDouble(r);
146 matrix.m01 = reader_readDouble(r);
147 matrix.m10 = reader_readDouble(r);
148 matrix.m11 = reader_readDouble(r);
149 matrix.tx = reader_readDouble(r);
150 matrix.ty = reader_readDouble(r);
153 static gfxcolor_t readColor(reader_t*r)
156 col.r = reader_readU8(r);
157 col.g = reader_readU8(r);
158 col.b = reader_readU8(r);
159 col.a = reader_readU8(r);
162 static gfxgradient_t* readGradient(reader_t*r)
164 gfxgradient_t*start = 0, *pos = 0;
166 U8 op = reader_readU8(r);
169 gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
176 g->color = readColor(r);
177 g->pos = reader_readFloat(r);
181 static gfxcxform_t* readCXForm(reader_t*r)
183 U8 type = reader_readU8(r);
186 gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
187 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
188 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
189 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
190 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
194 static void dumpColor(writer_t*w, gfxcolor_t*color)
196 writer_writeU8(w, color->r);
197 writer_writeU8(w, color->g);
198 writer_writeU8(w, color->b);
199 writer_writeU8(w, color->a);
201 static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
203 writer_writeDouble(w, matrix->m00);
204 writer_writeDouble(w, matrix->m01);
205 writer_writeDouble(w, matrix->m10);
206 writer_writeDouble(w, matrix->m11);
207 writer_writeDouble(w, matrix->tx);
208 writer_writeDouble(w, matrix->ty);
210 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
213 writer_writeU8(w, 1);
214 dumpColor(w, &gradient->color);
215 writer_writeFloat(w, gradient->pos);
216 gradient = gradient->next;
218 writer_writeU8(w, 0);
220 static void dumpImage(writer_t*w, gfximage_t*image)
222 writer_writeU16(w, image->width);
223 writer_writeU16(w, image->height);
224 w->write(w, image->data, image->width*image->height*4);
226 static void dumpCXForm(writer_t*w, gfxcxform_t*c)
229 writer_writeU8(w, 0);
231 writer_writeU8(w, 1);
232 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
233 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
234 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
235 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
238 static void dumpFont(writer_t*w, gfxfont_t*font)
240 writer_writeString(w, font->id);
241 writer_writeU32(w, font->num_glyphs);
242 writer_writeU32(w, font->max_unicode);
244 for(t=0;t<font->num_glyphs;t++) {
245 dumpLine(w, font->glyphs[t].line);
246 writer_writeDouble(w, font->glyphs[t].advance);
247 writer_writeU32(w, font->glyphs[t].unicode);
248 if(font->glyphs[t].name) {
249 writer_writeString(w,font->glyphs[t].name);
254 for(t=0;t<font->max_unicode;t++) {
255 writer_writeU32(w, font->unicode2glyph[t]);
258 static gfxfont_t*readFont(reader_t*r)
260 gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
261 font->id = reader_readString(r);
262 font->num_glyphs = reader_readU32(r);
263 font->max_unicode = reader_readU32(r);
264 font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
265 font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
267 for(t=0;t<font->num_glyphs;t++) {
268 font->glyphs[t].line = readLine(r);
269 font->glyphs[t].advance = reader_readDouble(r);
270 font->glyphs[t].unicode = reader_readU32(r);
271 font->glyphs[t].name = reader_readString(r);
272 if(!font->glyphs[t].name[0]) {
273 free((void*)(font->glyphs[t].name));
274 font->glyphs[t].name = 0;
277 for(t=0;t<font->max_unicode;t++) {
278 font->unicode2glyph[t] = reader_readU32(r);
283 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)
285 internal_t*i = (internal_t*)dev->internal;
286 msg("<trace> record: STROKE\n");
287 writer_writeU8(&i->w, OP_STROKE);
288 writer_writeDouble(&i->w, width);
289 writer_writeDouble(&i->w, miterLimit);
290 dumpColor(&i->w, color);
291 writer_writeU8(&i->w, cap_style);
292 writer_writeU8(&i->w, joint_style);
293 dumpLine(&i->w, line);
296 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
298 internal_t*i = (internal_t*)dev->internal;
299 msg("<trace> record: STARTCLIP\n");
300 writer_writeU8(&i->w, OP_STARTCLIP);
301 dumpLine(&i->w, line);
304 static void record_endclip(struct _gfxdevice*dev)
306 internal_t*i = (internal_t*)dev->internal;
307 msg("<trace> record: ENDCLIP\n");
308 writer_writeU8(&i->w, OP_ENDCLIP);
311 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
313 internal_t*i = (internal_t*)dev->internal;
314 msg("<trace> record: FILL\n");
315 writer_writeU8(&i->w, OP_FILL);
316 dumpColor(&i->w, color);
317 dumpLine(&i->w, line);
320 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
322 internal_t*i = (internal_t*)dev->internal;
323 msg("<trace> record: FILLBITMAP\n");
324 writer_writeU8(&i->w, OP_FILLBITMAP);
325 dumpImage(&i->w, img);
326 dumpMatrix(&i->w, matrix);
327 dumpLine(&i->w, line);
328 dumpCXForm(&i->w, cxform);
331 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
333 internal_t*i = (internal_t*)dev->internal;
334 msg("<trace> record: FILLGRADIENT\n");
335 writer_writeU8(&i->w, OP_FILLGRADIENT);
336 writer_writeU8(&i->w, type);
337 dumpGradient(&i->w, gradient);
338 dumpMatrix(&i->w, matrix);
339 dumpLine(&i->w, line);
342 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
344 internal_t*i = (internal_t*)dev->internal;
345 msg("<trace> record: ADDFONT\n");
346 writer_writeU8(&i->w, OP_ADDFONT);
347 dumpFont(&i->w, font);
348 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
351 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
353 internal_t*i = (internal_t*)dev->internal;
354 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
355 record_addfont(dev, font);
356 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
359 msg("<trace> record: DRAWCHAR %d\n", glyphnr);
360 writer_writeU8(&i->w, OP_DRAWCHAR);
362 writer_writeString(&i->w, font->id);
364 writer_writeString(&i->w, "*NULL*");
365 writer_writeU32(&i->w, glyphnr);
366 dumpColor(&i->w, color);
367 dumpMatrix(&i->w, matrix);
370 static void record_startpage(struct _gfxdevice*dev, int width, int height)
372 internal_t*i = (internal_t*)dev->internal;
373 msg("<trace> record: STARTPAGE\n");
374 writer_writeU8(&i->w, OP_STARTPAGE);
375 writer_writeU16(&i->w, width);
376 writer_writeU16(&i->w, height);
379 static void record_endpage(struct _gfxdevice*dev)
381 internal_t*i = (internal_t*)dev->internal;
382 msg("<trace> record: ENDPAGE\n");
383 writer_writeU8(&i->w, OP_ENDPAGE);
386 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
388 internal_t*i = (internal_t*)dev->internal;
389 msg("<trace> record: DRAWLINK\n");
390 writer_writeU8(&i->w, OP_DRAWLINK);
391 dumpLine(&i->w, line);
392 writer_writeString(&i->w, action);
395 static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, int length)
399 i = (internal_t*)recorddev->internal;
404 reader_init_memreader(r, data, length);
405 gfxfontlist_t* fontlist = gfxfontlist_create();
407 while(r->pos < length) {
408 unsigned char op = reader_readU8(r);
411 msg("<trace> replay: END");
414 msg("<trace> replay: SETPARAM");
417 key = reader_readString(r);
418 value = reader_readString(r);
419 device->setparameter(device, key, value);
425 msg("<trace> replay: STARTPAGE");
426 U16 width = reader_readU16(r);
427 U16 height = reader_readU16(r);
428 device->startpage(device, width, height);
432 msg("<trace> replay: ENDPAGE");
436 msg("<trace> replay: FINISH");
440 msg("<trace> replay: STROKE");
441 double width = reader_readDouble(r);
442 double miterlimit = reader_readDouble(r);
443 gfxcolor_t color = readColor(r);
445 int v = reader_readU8(r);
447 case 0: captype = gfx_capButt; break;
448 case 1: captype = gfx_capRound; break;
449 case 2: captype = gfx_capSquare; break;
451 gfx_joinType jointtype;
452 v = reader_readU8(r);
454 case 0: jointtype = gfx_joinMiter; break;
455 case 1: jointtype = gfx_joinRound; break;
456 case 2: jointtype = gfx_joinBevel; break;
458 gfxline_t* line = readLine(r);
459 device->stroke(device, line, width, &color, captype, jointtype,miterlimit);
464 msg("<trace> replay: STARTCLIP");
465 gfxline_t* line = readLine(r);
466 device->startclip(device, line);
471 msg("<trace> replay: ENDCLIP");
472 device->endclip(device);
476 msg("<trace> replay: FILL");
477 gfxcolor_t color = readColor(r);
478 gfxline_t* line = readLine(r);
479 device->fill(device, line, &color);
483 case OP_FILLBITMAP: {
484 msg("<trace> replay: FILLBITMAP");
485 gfximage_t img = readImage(r);
486 gfxmatrix_t matrix = readMatrix(r);
487 gfxline_t* line = readLine(r);
488 gfxcxform_t* cxform = readCXForm(r);
489 device->fillbitmap(device, line, &img, &matrix, cxform);
494 case OP_FILLGRADIENT: {
495 msg("<trace> replay: FILLGRADIENT");
496 gfxgradienttype_t type;
497 int v = reader_readU8(r);
500 type = gfxgradient_radial; break;
502 type = gfxgradient_linear; break;
504 gfxgradient_t*gradient = readGradient(r);
505 gfxmatrix_t matrix = readMatrix(r);
506 gfxline_t* line = readLine(r);
507 device->fillgradient(device, line, gradient, type, &matrix);
511 msg("<trace> replay: DRAWLINK");
512 gfxline_t* line = readLine(r);
513 char* s = reader_readString(r);
514 device->drawlink(device,line,s);
520 msg("<trace> replay: ADDFONT");
521 gfxfont_t*font = readFont(r);
522 fontlist = gfxfontlist_addfont(fontlist, font);
523 device->addfont(device, font);
527 char* id = reader_readString(r);
528 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
530 font = gfxfontlist_findfont(i->fontlist, id);
532 U32 glyph = reader_readU32(r);
533 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
534 gfxcolor_t color = readColor(r);
535 gfxmatrix_t matrix = readMatrix(r);
536 device->drawchar(device, font, glyph, &color, &matrix);
543 gfxfontlist_free(fontlist, 1);
545 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
547 internal_result_t*i = (internal_result_t*)result->internal;
548 replay(0, device, i->data, i->length);
551 static void record_result_write(gfxresult_t*r, int filedesc)
553 internal_result_t*i = (internal_result_t*)r->internal;
554 write(filedesc, i->data, i->length);
556 static int record_result_save(gfxresult_t*r, const char*filename)
558 internal_result_t*i = (internal_result_t*)r->internal;
559 FILE*fi = fopen(filename, "wb");
561 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
564 fwrite(i->data, i->length, 1, fi);
568 static void*record_result_get(gfxresult_t*r, const char*name)
570 internal_result_t*i = (internal_result_t*)r->internal;
571 if(!strcmp(name, "data")) {
573 } else if(!strcmp(name, "length")) {
578 static void record_result_destroy(gfxresult_t*r)
580 internal_result_t*i = (internal_result_t*)r->internal;
582 free(i->data);i->data = 0;
584 free(r->internal);r->internal = 0;
588 static unsigned char printable(unsigned char a)
590 if(a<32 || a==127) return '.';
594 static void hexdumpMem(unsigned char*data, int len)
599 printf("%02x ", data[t]);
600 ascii[t&15] = printable(data[t]);
601 if((t && ((t&15)==15)) || (t==len-1))
605 for(s=p-1;s<16;s++) {
608 printf(" %s\n", ascii);
613 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
615 internal_t*i = (internal_t*)dev->internal;
618 void*data = writer_growmemwrite_memptr(&i->w, &len);
619 replay(dev, out, data, len);
621 writer_growmemwrite_reset(&i->w);
624 static gfxresult_t* record_finish(struct _gfxdevice*dev)
626 internal_t*i = (internal_t*)dev->internal;
627 msg("<trace> record: END\n");
629 writer_writeU8(&i->w, OP_END);
631 gfxfontlist_free(i->fontlist, 0);
633 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
634 ir->data = writer_growmemwrite_getmem(&i->w);
635 ir->length = i->w.pos;
638 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
639 result->save = record_result_save;
640 result->get = record_result_get;
641 result->destroy = record_result_destroy;
642 result->internal = ir;
644 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
648 void gfxdevice_record_init(gfxdevice_t*dev)
650 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
651 memset(dev, 0, sizeof(gfxdevice_t));
653 dev->name = "record";
657 writer_init_growingmemwriter(&i->w, 1048576);
658 i->fontlist = gfxfontlist_create();
660 dev->setparameter = record_setparameter;
661 dev->startpage = record_startpage;
662 dev->startclip = record_startclip;
663 dev->endclip = record_endclip;
664 dev->stroke = record_stroke;
665 dev->fill = record_fill;
666 dev->fillbitmap = record_fillbitmap;
667 dev->fillgradient = record_fillgradient;
668 dev->addfont = record_addfont;
669 dev->drawchar = record_drawchar;
670 dev->drawlink = record_drawlink;
671 dev->endpage = record_endpage;
672 dev->finish = record_finish;