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 */
26 #include "../gfxdevice.h"
27 #include "../gfxtools.h"
31 typedef struct _internal {
32 gfxfontlist_t* fontlist;
36 typedef struct _internal_result {
42 #define OP_SETPARAM 0x01
43 #define OP_STROKE 0x02
44 #define OP_STARTCLIP 0x03
45 #define OP_ENDCLIP 0x04
47 #define OP_FILLBITMAP 0x06
48 #define OP_FILLGRADIENT 0x07
49 #define OP_ADDFONT 0x08
50 #define OP_DRAWCHAR 0x09
51 #define OP_DRAWLINK 0x0a
52 #define OP_STARTPAGE 0x0b
53 #define OP_ENDPAGE 0x0c
54 #define OP_FINISH 0x0d
56 #define OP_MOVETO 0x0e
57 #define OP_LINETO 0x0f
58 #define OP_SPLINETO 0x10
60 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
62 internal_t*i = (internal_t*)dev->internal;
63 writer_writeU8(&i->w, OP_SETPARAM);
64 writer_writeString(&i->w, key);
65 writer_writeString(&i->w, value);
69 static void dumpLine(writer_t*w, gfxline_t*line)
72 if(line->type == gfx_moveTo) {
73 writer_writeU8(w, OP_MOVETO);
74 writer_writeDouble(w, line->x);
75 writer_writeDouble(w, line->y);
76 } else if(line->type == gfx_lineTo) {
77 writer_writeU8(w, OP_LINETO);
78 writer_writeDouble(w, line->x);
79 writer_writeDouble(w, line->y);
80 } else if(line->type == gfx_splineTo) {
81 writer_writeU8(w, OP_SPLINETO);
82 writer_writeDouble(w, line->x);
83 writer_writeDouble(w, line->y);
84 writer_writeDouble(w, line->sx);
85 writer_writeDouble(w, line->sy);
89 writer_writeU8(w, OP_END);
91 static gfxline_t* readLine(reader_t*r)
93 gfxline_t*start = 0, *pos = 0;
95 unsigned char op = reader_readU8(r);
98 gfxline_t*line = rfx_calloc(sizeof(gfxline_t));
105 if(op == OP_MOVETO) {
106 line->type = gfx_moveTo;
107 line->x = reader_readDouble(r);
108 line->y = reader_readDouble(r);
109 } else if(op == OP_LINETO) {
110 line->type = gfx_lineTo;
111 line->x = reader_readDouble(r);
112 line->y = reader_readDouble(r);
113 } else if(op == OP_SPLINETO) {
114 line->type = gfx_splineTo;
115 line->x = reader_readDouble(r);
116 line->y = reader_readDouble(r);
117 line->sx = reader_readDouble(r);
118 line->sy = reader_readDouble(r);
123 static gfximage_t readImage(reader_t*r)
126 img.width = reader_readU16(r);
127 img.height = reader_readU16(r);
128 img.data = rfx_alloc(img.width*img.height*4);
129 r->read(r, img.data, img.width*img.height*4);
132 static gfxmatrix_t readMatrix(reader_t*r)
135 matrix.m00 = reader_readDouble(r);
136 matrix.m01 = reader_readDouble(r);
137 matrix.m10 = reader_readDouble(r);
138 matrix.m11 = reader_readDouble(r);
139 matrix.tx = reader_readDouble(r);
140 matrix.ty = reader_readDouble(r);
143 static gfxcolor_t readColor(reader_t*r)
146 col.r = reader_readU8(r);
147 col.g = reader_readU8(r);
148 col.b = reader_readU8(r);
149 col.a = reader_readU8(r);
152 static gfxgradient_t* readGradient(reader_t*r)
154 gfxgradient_t*start = 0, *pos = 0;
156 U8 op = reader_readU8(r);
159 gfxgradient_t*g = rfx_calloc(sizeof(gfxgradient_t));
166 g->color = readColor(r);
167 g->pos = reader_readFloat(r);
171 static gfxcxform_t* readCXForm(reader_t*r)
173 U8 type = reader_readU8(r);
176 gfxcxform_t* c = rfx_calloc(sizeof(gfxcxform_t));
177 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
178 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
179 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
180 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
184 static void dumpColor(writer_t*w, gfxcolor_t*color)
186 writer_writeU8(w, color->r);
187 writer_writeU8(w, color->g);
188 writer_writeU8(w, color->b);
189 writer_writeU8(w, color->a);
191 static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
193 writer_writeDouble(w, matrix->m00);
194 writer_writeDouble(w, matrix->m01);
195 writer_writeDouble(w, matrix->m10);
196 writer_writeDouble(w, matrix->m11);
197 writer_writeDouble(w, matrix->tx);
198 writer_writeDouble(w, matrix->ty);
200 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
203 writer_writeU8(w, 1);
204 dumpColor(w, &gradient->color);
205 writer_writeFloat(w, gradient->pos);
206 gradient = gradient->next;
208 writer_writeU8(w, 0);
210 static void dumpImage(writer_t*w, gfximage_t*image)
212 writer_writeU16(w, image->width);
213 writer_writeU16(w, image->height);
214 w->write(w, image->data, image->width*image->height*4);
216 static void dumpCXForm(writer_t*w, gfxcxform_t*c)
219 writer_writeU8(w, 0);
221 writer_writeU8(w, 1);
222 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
223 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
224 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
225 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
228 static void dumpFont(writer_t*w, gfxfont_t*font)
230 writer_writeString(w, font->id);
231 writer_writeU32(w, font->num_glyphs);
232 writer_writeU32(w, font->max_unicode);
234 for(t=0;t<font->num_glyphs;t++) {
235 dumpLine(w, font->glyphs[t].line);
236 writer_writeDouble(w, font->glyphs[t].advance);
237 writer_writeU32(w, font->glyphs[t].unicode);
238 if(font->glyphs[t].name) {
239 writer_writeString(w,font->glyphs[t].name);
244 for(t=0;t<font->max_unicode;t++) {
245 writer_writeU32(w, font->unicode2glyph[t]);
248 static gfxfont_t*readFont(reader_t*r)
250 gfxfont_t* font = rfx_calloc(sizeof(gfxfont_t));
251 font->id = reader_readString(r);
252 font->num_glyphs = reader_readU32(r);
253 font->max_unicode = reader_readU32(r);
254 font->glyphs = rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
255 font->unicode2glyph = rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
257 for(t=0;t<font->num_glyphs;t++) {
258 font->glyphs[t].line = readLine(r);
259 font->glyphs[t].advance = reader_readDouble(r);
260 font->glyphs[t].unicode = reader_readU32(r);
261 font->glyphs[t].name = reader_readString(r);
262 if(!font->glyphs[t].name[0]) {
263 free(font->glyphs[t].name);
264 font->glyphs[t].name = 0;
267 for(t=0;t<font->max_unicode;t++) {
268 font->unicode2glyph[t] = reader_readU32(r);
273 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)
275 internal_t*i = (internal_t*)dev->internal;
276 writer_writeU8(&i->w, OP_STROKE);
277 writer_writeDouble(&i->w, width);
278 writer_writeDouble(&i->w, miterLimit);
279 dumpColor(&i->w, color);
280 writer_writeU8(&i->w, cap_style);
281 writer_writeU8(&i->w, joint_style);
282 dumpLine(&i->w, line);
285 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
287 internal_t*i = (internal_t*)dev->internal;
288 writer_writeU8(&i->w, OP_STARTCLIP);
289 dumpLine(&i->w, line);
292 static void record_endclip(struct _gfxdevice*dev)
294 internal_t*i = (internal_t*)dev->internal;
295 writer_writeU8(&i->w, OP_ENDCLIP);
298 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
300 internal_t*i = (internal_t*)dev->internal;
301 writer_writeU8(&i->w, OP_FILL);
302 dumpColor(&i->w, color);
303 dumpLine(&i->w, line);
306 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
308 internal_t*i = (internal_t*)dev->internal;
309 writer_writeU8(&i->w, OP_FILLBITMAP);
310 dumpImage(&i->w, img);
311 dumpMatrix(&i->w, matrix);
312 dumpLine(&i->w, line);
313 dumpCXForm(&i->w, cxform);
316 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
318 internal_t*i = (internal_t*)dev->internal;
319 writer_writeU8(&i->w, OP_FILLGRADIENT);
320 writer_writeU8(&i->w, type);
321 dumpGradient(&i->w, gradient);
322 dumpMatrix(&i->w, matrix);
323 dumpLine(&i->w, line);
326 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
328 internal_t*i = (internal_t*)dev->internal;
329 writer_writeU8(&i->w, OP_ADDFONT);
330 dumpFont(&i->w, font);
331 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
334 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
336 internal_t*i = (internal_t*)dev->internal;
337 if(!gfxfontlist_hasfont(i->fontlist, font))
338 record_addfont(dev, font);
340 writer_writeU8(&i->w, OP_DRAWCHAR);
341 writer_writeString(&i->w, font->id);
342 writer_writeU32(&i->w, glyphnr);
343 dumpColor(&i->w, color);
344 dumpMatrix(&i->w, matrix);
347 static void record_startpage(struct _gfxdevice*dev, int width, int height)
349 internal_t*i = (internal_t*)dev->internal;
350 writer_writeU8(&i->w, OP_STARTPAGE);
351 writer_writeU16(&i->w, width);
352 writer_writeU16(&i->w, height);
355 static void record_endpage(struct _gfxdevice*dev)
357 internal_t*i = (internal_t*)dev->internal;
358 writer_writeU8(&i->w, OP_ENDPAGE);
361 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
363 internal_t*i = (internal_t*)dev->internal;
364 writer_writeU8(&i->w, OP_DRAWLINK);
365 dumpLine(&i->w, line);
366 writer_writeString(&i->w, action);
369 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
371 internal_result_t*i = (internal_result_t*)result->internal;
374 reader_init_memreader(r, i->data, i->length);
375 gfxfontlist_t* fontlist = gfxfontlist_create();
378 unsigned char op = reader_readU8(r);
385 key = reader_readString(r);
386 value = reader_readString(r);
387 device->setparameter(device, key, value);
393 U16 width = reader_readU16(r);
394 U16 height = reader_readU16(r);
395 device->startpage(device, width, height);
405 double width = reader_readDouble(r);
406 double miterlimit = reader_readDouble(r);
407 gfxcolor_t color = readColor(r);
408 gfx_capType captype = reader_readU8(r);
409 gfx_joinType jointtype = reader_readU8(r);
410 gfxline_t* line = readLine(r);
411 device->stroke(device, line, width, &color, captype, jointtype,miterlimit);
416 gfxline_t* line = readLine(r);
417 device->startclip(device, line);
422 device->endclip(device);
426 gfxcolor_t color = readColor(r);
427 gfxline_t* line = readLine(r);
428 device->fill(device, line, &color);
432 case OP_FILLBITMAP: {
433 gfximage_t img = readImage(r);
434 gfxmatrix_t matrix = readMatrix(r);
435 gfxline_t* line = readLine(r);
436 gfxcxform_t* cxform = readCXForm(r);
437 device->fillbitmap(device, line, &img, &matrix, cxform);
442 case OP_FILLGRADIENT: {
443 gfxgradienttype_t type = reader_readU8(r);
444 gfxgradient_t*gradient = readGradient(r);
445 gfxmatrix_t matrix = readMatrix(r);
446 gfxline_t* line = readLine(r);
447 device->fillgradient(device, line, gradient, type, &matrix);
451 gfxline_t* line = readLine(r);
452 char* s = reader_readString(r);
453 device->drawlink(device,line,s);
459 gfxfont_t*font = readFont(r);
460 fontlist = gfxfontlist_addfont(fontlist, font);
461 device->addfont(device, font);
465 char* id = reader_readString(r);
466 gfxfont_t*font = gfxfontlist_findfont(fontlist, id);
467 U32 glyph = reader_readU32(r);
468 gfxcolor_t color = readColor(r);
469 gfxmatrix_t matrix = readMatrix(r);
470 device->drawchar(device, font, glyph, &color, &matrix);
478 static void record_result_write(gfxresult_t*r, int filedesc)
480 internal_result_t*i = (internal_result_t*)r->internal;
481 write(filedesc, i->data, i->length);
483 static int record_result_save(gfxresult_t*r, char*filename)
485 internal_result_t*i = (internal_result_t*)r->internal;
486 FILE*fi = fopen(filename, "wb");
488 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
491 fwrite(i->data, i->length, 1, fi);
495 static void*record_result_get(gfxresult_t*r, char*name)
497 internal_result_t*i = (internal_result_t*)r->internal;
498 if(!strcmp(name, "data")) {
500 } else if(!strcmp(name, "length")) {
505 static void record_result_destroy(gfxresult_t*r)
507 internal_result_t*i = (internal_result_t*)r->internal;
508 free(i->data);i->data = 0;
509 free(r->internal);r->internal = 0;
514 static gfxresult_t* record_finish(struct _gfxdevice*dev)
516 internal_t*i = (internal_t*)dev->internal;
518 writer_writeU8(&i->w, OP_END);
520 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
521 ir->data = writer_growmemwrite_getmem(&i->w);
522 ir->length = i->w.pos;
524 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
525 result->save = record_result_save;
526 result->get = record_result_get;
527 result->destroy = record_result_destroy;
528 result->internal = ir;
530 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
534 void gfxdevice_record_init(gfxdevice_t*dev)
536 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
537 memset(dev, 0, sizeof(gfxdevice_t));
539 dev->name = "record";
543 writer_init_growingmemwriter(&i->w, 1048576);
544 i->fontlist = gfxfontlist_create();
546 dev->setparameter = record_setparameter;
547 dev->startpage = record_startpage;
548 dev->startclip = record_startclip;
549 dev->endclip = record_endclip;
550 dev->stroke = record_stroke;
551 dev->fill = record_fill;
552 dev->fillbitmap = record_fillbitmap;
553 dev->fillgradient = record_fillgradient;
554 dev->addfont = record_addfont;
555 dev->drawchar = record_drawchar;
556 dev->drawlink = record_drawlink;
557 dev->endpage = record_endpage;
558 dev->finish = record_finish;