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"
41 typedef struct _internal {
42 gfxfontlist_t* fontlist;
49 typedef struct _internal_result {
57 #define OP_SETPARAM 0x01
58 #define OP_STROKE 0x02
59 #define OP_STARTCLIP 0x03
60 #define OP_ENDCLIP 0x04
62 #define OP_FILLBITMAP 0x06
63 #define OP_FILLGRADIENT 0x07
64 #define OP_ADDFONT 0x08
65 #define OP_DRAWCHAR 0x09
66 #define OP_DRAWLINK 0x0a
67 #define OP_STARTPAGE 0x0b
68 #define OP_ENDPAGE 0x0c
69 #define OP_FINISH 0x0d
71 #define OP_MOVETO 0x0e
72 #define OP_LINETO 0x0f
73 #define OP_SPLINETO 0x10
75 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
77 internal_t*i = (internal_t*)dev->internal;
78 msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
79 writer_writeU8(&i->w, OP_SETPARAM);
80 writer_writeString(&i->w, key);
81 writer_writeString(&i->w, value);
85 static void dumpLine(writer_t*w, gfxline_t*line)
88 if(line->type == gfx_moveTo) {
89 writer_writeU8(w, OP_MOVETO);
90 writer_writeDouble(w, line->x);
91 writer_writeDouble(w, line->y);
92 } else if(line->type == gfx_lineTo) {
93 writer_writeU8(w, OP_LINETO);
94 writer_writeDouble(w, line->x);
95 writer_writeDouble(w, line->y);
96 } else if(line->type == gfx_splineTo) {
97 writer_writeU8(w, OP_SPLINETO);
98 writer_writeDouble(w, line->x);
99 writer_writeDouble(w, line->y);
100 writer_writeDouble(w, line->sx);
101 writer_writeDouble(w, line->sy);
105 writer_writeU8(w, OP_END);
107 static gfxline_t* readLine(reader_t*r)
109 gfxline_t*start = 0, *pos = 0;
111 unsigned char op = reader_readU8(r);
114 gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
121 if(op == OP_MOVETO) {
122 line->type = gfx_moveTo;
123 line->x = reader_readDouble(r);
124 line->y = reader_readDouble(r);
125 } else if(op == OP_LINETO) {
126 line->type = gfx_lineTo;
127 line->x = reader_readDouble(r);
128 line->y = reader_readDouble(r);
129 } else if(op == OP_SPLINETO) {
130 line->type = gfx_splineTo;
131 line->x = reader_readDouble(r);
132 line->y = reader_readDouble(r);
133 line->sx = reader_readDouble(r);
134 line->sy = reader_readDouble(r);
139 static gfximage_t readImage(reader_t*r)
142 img.width = reader_readU16(r);
143 img.height = reader_readU16(r);
144 img.data = (gfxcolor_t*)rfx_alloc(img.width*img.height*4);
145 r->read(r, img.data, img.width*img.height*4);
148 static gfxmatrix_t readMatrix(reader_t*r)
151 matrix.m00 = reader_readDouble(r);
152 matrix.m01 = reader_readDouble(r);
153 matrix.m10 = reader_readDouble(r);
154 matrix.m11 = reader_readDouble(r);
155 matrix.tx = reader_readDouble(r);
156 matrix.ty = reader_readDouble(r);
159 static gfxcolor_t readColor(reader_t*r)
162 col.r = reader_readU8(r);
163 col.g = reader_readU8(r);
164 col.b = reader_readU8(r);
165 col.a = reader_readU8(r);
168 static gfxgradient_t* readGradient(reader_t*r)
170 gfxgradient_t*start = 0, *pos = 0;
172 U8 op = reader_readU8(r);
175 gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
182 g->color = readColor(r);
183 g->pos = reader_readFloat(r);
187 static gfxcxform_t* readCXForm(reader_t*r)
189 U8 type = reader_readU8(r);
192 gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
193 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
194 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
195 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
196 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
200 static void dumpColor(writer_t*w, gfxcolor_t*color)
202 writer_writeU8(w, color->r);
203 writer_writeU8(w, color->g);
204 writer_writeU8(w, color->b);
205 writer_writeU8(w, color->a);
207 static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
209 writer_writeDouble(w, matrix->m00);
210 writer_writeDouble(w, matrix->m01);
211 writer_writeDouble(w, matrix->m10);
212 writer_writeDouble(w, matrix->m11);
213 writer_writeDouble(w, matrix->tx);
214 writer_writeDouble(w, matrix->ty);
216 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
219 writer_writeU8(w, 1);
220 dumpColor(w, &gradient->color);
221 writer_writeFloat(w, gradient->pos);
222 gradient = gradient->next;
224 writer_writeU8(w, 0);
226 static void dumpImage(writer_t*w, gfximage_t*image)
228 writer_writeU16(w, image->width);
229 writer_writeU16(w, image->height);
230 w->write(w, image->data, image->width*image->height*4);
232 static void dumpCXForm(writer_t*w, gfxcxform_t*c)
235 writer_writeU8(w, 0);
237 writer_writeU8(w, 1);
238 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
239 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
240 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
241 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
244 static void dumpFont(writer_t*w, gfxfont_t*font)
246 writer_writeString(w, font->id);
247 writer_writeU32(w, font->num_glyphs);
248 writer_writeU32(w, font->max_unicode);
249 writer_writeDouble(w, font->ascent);
250 writer_writeDouble(w, font->descent);
252 for(t=0;t<font->num_glyphs;t++) {
253 dumpLine(w, font->glyphs[t].line);
254 writer_writeDouble(w, font->glyphs[t].advance);
255 writer_writeU32(w, font->glyphs[t].unicode);
256 if(font->glyphs[t].name) {
257 writer_writeString(w,font->glyphs[t].name);
262 for(t=0;t<font->max_unicode;t++) {
263 writer_writeU32(w, font->unicode2glyph[t]);
265 writer_writeU32(w, font->kerning_size);
266 for(t=0;t<font->kerning_size;t++) {
267 writer_writeU32(w, font->kerning[t].c1);
268 writer_writeU32(w, font->kerning[t].c2);
269 writer_writeU32(w, font->kerning[t].advance);
272 static gfxfont_t*readFont(reader_t*r)
274 gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
275 font->id = reader_readString(r);
276 font->num_glyphs = reader_readU32(r);
277 font->max_unicode = reader_readU32(r);
278 font->ascent = reader_readDouble(r);
279 font->descent = reader_readDouble(r);
280 font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
281 font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
283 for(t=0;t<font->num_glyphs;t++) {
284 font->glyphs[t].line = readLine(r);
285 font->glyphs[t].advance = reader_readDouble(r);
286 font->glyphs[t].unicode = reader_readU32(r);
287 font->glyphs[t].name = reader_readString(r);
288 if(!font->glyphs[t].name[0]) {
289 free((void*)(font->glyphs[t].name));
290 font->glyphs[t].name = 0;
293 for(t=0;t<font->max_unicode;t++) {
294 font->unicode2glyph[t] = reader_readU32(r);
296 font->kerning_size = reader_readU32(r);
297 if(font->kerning_size) {
298 font->kerning = malloc(sizeof(gfxkerning_t)*font->kerning_size);
299 for(t=0;t<font->kerning_size;t++) {
300 font->kerning[t].c1 = reader_readU32(r);
301 font->kerning[t].c2 = reader_readU32(r);
302 font->kerning[t].advance = reader_readU32(r);
308 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)
310 internal_t*i = (internal_t*)dev->internal;
311 msg("<trace> record: %08x STROKE\n", dev);
312 writer_writeU8(&i->w, OP_STROKE);
313 writer_writeDouble(&i->w, width);
314 writer_writeDouble(&i->w, miterLimit);
315 dumpColor(&i->w, color);
316 writer_writeU8(&i->w, cap_style);
317 writer_writeU8(&i->w, joint_style);
318 dumpLine(&i->w, line);
321 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
323 internal_t*i = (internal_t*)dev->internal;
324 msg("<trace> record: %08x STARTCLIP\n", dev);
325 writer_writeU8(&i->w, OP_STARTCLIP);
326 dumpLine(&i->w, line);
330 static void record_endclip(struct _gfxdevice*dev)
332 internal_t*i = (internal_t*)dev->internal;
333 msg("<trace> record: %08x ENDCLIP\n", dev);
334 writer_writeU8(&i->w, OP_ENDCLIP);
337 msg("<error> record: endclip() without startclip()");
341 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
343 internal_t*i = (internal_t*)dev->internal;
344 msg("<trace> record: %08x FILL\n", dev);
345 writer_writeU8(&i->w, OP_FILL);
346 dumpColor(&i->w, color);
347 dumpLine(&i->w, line);
350 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
352 internal_t*i = (internal_t*)dev->internal;
353 msg("<trace> record: %08x FILLBITMAP\n", dev);
354 writer_writeU8(&i->w, OP_FILLBITMAP);
355 dumpImage(&i->w, img);
356 dumpMatrix(&i->w, matrix);
357 dumpLine(&i->w, line);
358 dumpCXForm(&i->w, cxform);
361 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
363 internal_t*i = (internal_t*)dev->internal;
364 msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
365 writer_writeU8(&i->w, OP_FILLGRADIENT);
366 writer_writeU8(&i->w, type);
367 dumpGradient(&i->w, gradient);
368 dumpMatrix(&i->w, matrix);
369 dumpLine(&i->w, line);
372 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
374 internal_t*i = (internal_t*)dev->internal;
375 msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
376 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
377 writer_writeU8(&i->w, OP_ADDFONT);
378 dumpFont(&i->w, font);
379 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
383 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
385 internal_t*i = (internal_t*)dev->internal;
386 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
387 record_addfont(dev, font);
390 msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
391 writer_writeU8(&i->w, OP_DRAWCHAR);
393 writer_writeString(&i->w, font->id);
395 writer_writeString(&i->w, "*NULL*");
396 writer_writeU32(&i->w, glyphnr);
397 dumpColor(&i->w, color);
398 dumpMatrix(&i->w, matrix);
401 static void record_startpage(struct _gfxdevice*dev, int width, int height)
403 internal_t*i = (internal_t*)dev->internal;
404 msg("<trace> record: %08x STARTPAGE\n", dev);
405 writer_writeU8(&i->w, OP_STARTPAGE);
406 writer_writeU16(&i->w, width);
407 writer_writeU16(&i->w, height);
410 static void record_endpage(struct _gfxdevice*dev)
412 internal_t*i = (internal_t*)dev->internal;
413 msg("<trace> record: %08x ENDPAGE\n", dev);
414 writer_writeU8(&i->w, OP_ENDPAGE);
417 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
419 internal_t*i = (internal_t*)dev->internal;
420 msg("<trace> record: %08x DRAWLINK\n", dev);
421 writer_writeU8(&i->w, OP_DRAWLINK);
422 dumpLine(&i->w, line);
423 writer_writeString(&i->w, action);
426 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
430 i = (internal_t*)dev->internal;
433 gfxfontlist_t* fontlist = gfxfontlist_create();
437 if(r->read(r, &op, 1)!=1)
444 msg("<trace> replay: SETPARAM");
447 key = reader_readString(r);
448 value = reader_readString(r);
449 out->setparameter(out, key, value);
455 msg("<trace> replay: STARTPAGE");
456 U16 width = reader_readU16(r);
457 U16 height = reader_readU16(r);
458 out->startpage(out, width, height);
462 msg("<trace> replay: ENDPAGE");
467 msg("<trace> replay: FINISH");
471 msg("<trace> replay: STROKE");
472 double width = reader_readDouble(r);
473 double miterlimit = reader_readDouble(r);
474 gfxcolor_t color = readColor(r);
476 int v = reader_readU8(r);
478 case 0: captype = gfx_capButt; break;
479 case 1: captype = gfx_capRound; break;
480 case 2: captype = gfx_capSquare; break;
482 gfx_joinType jointtype;
483 v = reader_readU8(r);
485 case 0: jointtype = gfx_joinMiter; break;
486 case 1: jointtype = gfx_joinRound; break;
487 case 2: jointtype = gfx_joinBevel; break;
489 gfxline_t* line = readLine(r);
490 out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
495 msg("<trace> replay: STARTCLIP");
496 gfxline_t* line = readLine(r);
497 out->startclip(out, line);
502 msg("<trace> replay: ENDCLIP");
507 msg("<trace> replay: FILL");
508 gfxcolor_t color = readColor(r);
509 gfxline_t* line = readLine(r);
510 out->fill(out, line, &color);
514 case OP_FILLBITMAP: {
515 msg("<trace> replay: FILLBITMAP");
516 gfximage_t img = readImage(r);
517 gfxmatrix_t matrix = readMatrix(r);
518 gfxline_t* line = readLine(r);
519 gfxcxform_t* cxform = readCXForm(r);
520 out->fillbitmap(out, line, &img, &matrix, cxform);
524 free(img.data);img.data=0;
527 case OP_FILLGRADIENT: {
528 msg("<trace> replay: FILLGRADIENT");
529 gfxgradienttype_t type;
530 int v = reader_readU8(r);
533 type = gfxgradient_radial; break;
535 type = gfxgradient_linear; break;
537 gfxgradient_t*gradient = readGradient(r);
538 gfxmatrix_t matrix = readMatrix(r);
539 gfxline_t* line = readLine(r);
540 out->fillgradient(out, line, gradient, type, &matrix);
544 msg("<trace> replay: DRAWLINK");
545 gfxline_t* line = readLine(r);
546 char* s = reader_readString(r);
547 out->drawlink(out,line,s);
553 msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
554 gfxfont_t*font = readFont(r);
555 fontlist = gfxfontlist_addfont(fontlist, font);
556 out->addfont(out, font);
560 char* id = reader_readString(r);
561 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
563 font = gfxfontlist_findfont(i->fontlist, id);
565 U32 glyph = reader_readU32(r);
566 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
567 gfxcolor_t color = readColor(r);
568 gfxmatrix_t matrix = readMatrix(r);
569 out->drawchar(out, font, glyph, &color, &matrix);
577 /* problem: if we just replayed into a device which stores the
578 font for later use (the record device itself is a nice example),
579 then we can't free it yet */
580 //gfxfontlist_free(fontlist, 1);
581 gfxfontlist_free(fontlist, 0);
583 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
585 internal_result_t*i = (internal_result_t*)result->internal;
588 if(i->use_tempfile) {
589 reader_init_filereader2(&r, i->filename);
591 reader_init_memreader(&r, i->data, i->length);
594 replay(0, device, &r);
597 static void record_result_write(gfxresult_t*r, int filedesc)
599 internal_result_t*i = (internal_result_t*)r->internal;
601 write(filedesc, i->data, i->length);
604 static int record_result_save(gfxresult_t*r, const char*filename)
606 internal_result_t*i = (internal_result_t*)r->internal;
607 if(i->use_tempfile) {
608 move_file(i->filename, filename);
610 FILE*fi = fopen(filename, "wb");
612 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
615 fwrite(i->data, i->length, 1, fi);
620 static void*record_result_get(gfxresult_t*r, const char*name)
622 internal_result_t*i = (internal_result_t*)r->internal;
623 if(!strcmp(name, "data")) {
625 } else if(!strcmp(name, "length")) {
630 static void record_result_destroy(gfxresult_t*r)
632 internal_result_t*i = (internal_result_t*)r->internal;
634 free(i->data);i->data = 0;
640 free(r->internal);r->internal = 0;
644 static unsigned char printable(unsigned char a)
646 if(a<32 || a==127) return '.';
650 static void hexdumpMem(unsigned char*data, int len)
655 printf("%02x ", data[t]);
656 ascii[t&15] = printable(data[t]);
657 if((t && ((t&15)==15)) || (t==len-1))
661 for(s=p-1;s<16;s++) {
664 printf(" %s\n", ascii);
669 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
671 internal_t*i = (internal_t*)dev->internal;
673 if(!i->use_tempfile) {
675 void*data = writer_growmemwrite_memptr(&i->w, &len);
677 reader_init_memreader(&r, data, len);
678 replay(dev, out, &r);
679 writer_growmemwrite_reset(&i->w);
681 msg("<fatal> Flushing not supported for file based record device");
687 static gfxresult_t* record_finish(struct _gfxdevice*dev)
689 internal_t*i = (internal_t*)dev->internal;
690 msg("<trace> record: %08x END", dev);
693 msg("<error> Warning: unclosed cliplevels");
696 writer_writeU8(&i->w, OP_END);
698 gfxfontlist_free(i->fontlist, 0);
700 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
702 ir->use_tempfile = i->use_tempfile;
703 if(i->use_tempfile) {
704 ir->filename = i->filename;
706 ir->data = writer_growmemwrite_getmem(&i->w);
707 ir->length = i->w.pos;
711 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
712 result->save = record_result_save;
713 result->get = record_result_get;
714 result->destroy = record_result_destroy;
715 result->internal = ir;
717 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
722 void gfxdevice_record_init(gfxdevice_t*dev, char use_tempfile)
724 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
725 memset(dev, 0, sizeof(gfxdevice_t));
727 dev->name = "record";
731 i->use_tempfile = use_tempfile;
733 writer_init_growingmemwriter(&i->w, 1048576);
736 i->filename = strdup(mktempname(buffer, "gfx"));
737 writer_init_filewriter2(&i->w, i->filename);
739 i->fontlist = gfxfontlist_create();
742 dev->setparameter = record_setparameter;
743 dev->startpage = record_startpage;
744 dev->startclip = record_startclip;
745 dev->endclip = record_endclip;
746 dev->stroke = record_stroke;
747 dev->fill = record_fill;
748 dev->fillbitmap = record_fillbitmap;
749 dev->fillgradient = record_fillgradient;
750 dev->addfont = record_addfont;
751 dev->drawchar = record_drawchar;
752 dev->drawlink = record_drawlink;
753 dev->endpage = record_endpage;
754 dev->finish = record_finish;