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"
35 #include "../gfxdevice.h"
36 #include "../gfxtools.h"
43 #include "../fastlz.h"
48 #define COMPRESS_IMAGES
49 //#define FILTER_IMAGES
51 typedef struct _state {
53 gfxcolor_t last_color[16];
54 gfxmatrix_t last_matrix[16];
67 typedef struct _internal {
68 gfxfontlist_t* fontlist;
77 typedef struct _internal_result {
85 #define OP_SETPARAM 0x01
86 #define OP_STROKE 0x02
87 #define OP_STARTCLIP 0x03
88 #define OP_ENDCLIP 0x04
90 #define OP_FILLBITMAP 0x06
91 #define OP_FILLGRADIENT 0x07
92 #define OP_ADDFONT 0x08
93 #define OP_DRAWCHAR 0x09
94 #define OP_DRAWLINK 0x0a
95 #define OP_STARTPAGE 0x0b
96 #define OP_ENDPAGE 0x0c
97 #define OP_FINISH 0x0d
99 #define FLAG_SAME_AS_LAST 0x10
100 #define FLAG_ZERO_FONT 0x20
102 #define LINE_MOVETO 0x0e
103 #define LINE_LINETO 0x0f
104 #define LINE_SPLINETO 0x10
106 /* ----------------- reading/writing of low level primitives -------------- */
108 static void dumpLine(writer_t*w, state_t*state, gfxline_t*line)
111 if(line->type == gfx_moveTo) {
112 writer_writeU8(w, LINE_MOVETO);
113 writer_writeDouble(w, line->x);
114 writer_writeDouble(w, line->y);
116 state->size_lines += 1+8+8;
118 } else if(line->type == gfx_lineTo) {
119 writer_writeU8(w, LINE_LINETO);
120 writer_writeDouble(w, line->x);
121 writer_writeDouble(w, line->y);
123 state->size_lines += 1+8+8;
125 } else if(line->type == gfx_splineTo) {
126 writer_writeU8(w, LINE_SPLINETO);
127 writer_writeDouble(w, line->x);
128 writer_writeDouble(w, line->y);
129 writer_writeDouble(w, line->sx);
130 writer_writeDouble(w, line->sy);
132 state->size_lines += 1+8+8+8+8;
137 writer_writeU8(w, OP_END);
139 state->size_lines += 1;
142 static gfxline_t* readLine(reader_t*r, state_t*s)
144 gfxline_t*start = 0, *pos = 0;
146 unsigned char op = reader_readU8(r);
149 gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
156 if(op == LINE_MOVETO) {
157 line->type = gfx_moveTo;
158 line->x = reader_readDouble(r);
159 line->y = reader_readDouble(r);
160 } else if(op == LINE_LINETO) {
161 line->type = gfx_lineTo;
162 line->x = reader_readDouble(r);
163 line->y = reader_readDouble(r);
164 } else if(op == LINE_SPLINETO) {
165 line->type = gfx_splineTo;
166 line->x = reader_readDouble(r);
167 line->y = reader_readDouble(r);
168 line->sx = reader_readDouble(r);
169 line->sy = reader_readDouble(r);
175 static void dumpImage(writer_t*w, state_t*state, gfximage_t*img)
178 writer_writeU16(w, img->width);
179 writer_writeU16(w, img->height);
180 #ifdef COMPRESS_IMAGES
181 //35.3% images (2027305 bytes) (with filter, Z_BEST_COMPRESSION)
182 //39.9% images (2458904 bytes) (with filter, Z_BEST_SPEED)
183 //45.2% images (3055340 bytes) (without filter)
184 //45.9% images (3149247 bytes) (without filter, 5)
185 //48.0% images (3480495 bytes) (with filter, fastlz)
186 //48.0% images (3488650 bytes) (without filter, Z_BEST_SPEED)
187 //55.3% images (4665889 bytes) (without filter, fastlz level 2)
188 //55.6% images (4726334 bytes) (without filter, fastlz level 1)
189 //83.0% images (18091804 bytes) (no compression)
193 unsigned char*filter = malloc(img->height);
195 image = malloc(img->width*img->height*sizeof(gfxcolor_t));
196 for(y=0;y<img->height;y++) {
197 filter[y] = png_apply_filter_32(
198 (void*)&image[y*img->width],
199 (void*)&img->data[y*img->width], img->width, y);
204 int size = img->width*img->height;
205 uLongf compressdata_size = compressBound(size*sizeof(gfxcolor_t));
206 void*compressdata = malloc(compressdata_size);
209 compressdata_size = fastlz_compress_level(2, (void*)image, size*sizeof(gfxcolor_t), compressdata);
211 compress2(compressdata, &compressdata_size, (void*)image, sizeof(gfxcolor_t)*size, Z_BEST_SPEED);
214 writer_writeU32(w, compressdata_size);
216 w->write(w, filter, img->height);
220 w->write(w, compressdata, compressdata_size);
222 w->write(w, img->data, img->width*img->height*sizeof(gfxcolor_t));
225 state->size_images += w->pos - oldpos;
228 static gfximage_t readImage(reader_t*r, state_t*state)
231 img.width = reader_readU16(r);
232 img.height = reader_readU16(r);
233 uLongf size = img.width*img.height*sizeof(gfxcolor_t);
234 img.data = malloc(size);
235 #ifdef COMPRESS_IMAGES
236 uLongf compressdata_size = reader_readU32(r);
237 void*compressdata = malloc(compressdata_size);
238 # ifdef FILTER_IMAGES
239 unsigned char*filter = malloc(img.height);
240 r->read(r, filter, img.height);
242 r->read(r, compressdata, compressdata_size);
245 fastlz_decompress(compressdata, compressdata_size, (void*)img.data, size);
247 uncompress((void*)img.data, &size, compressdata, compressdata_size);
250 # ifdef FILTER_IMAGES
252 unsigned char*line = malloc(img.width*sizeof(gfxcolor_t));
253 for(y=0;y<img.height;y++) {
254 png_inverse_filter_32(filter[y], (void*)&img.data[y*img.width],
255 y?(void*)&img.data[(y-1)*img.width]:(void*)0,
257 memcpy(&img.data[y*img.width], line, img.width*sizeof(gfxcolor_t));
264 r->read(r, img.data, size);
269 static void dumpMatrix(writer_t*w, state_t*state, gfxmatrix_t*matrix)
271 writer_writeDouble(w, matrix->m00);
272 writer_writeDouble(w, matrix->m01);
273 writer_writeDouble(w, matrix->m10);
274 writer_writeDouble(w, matrix->m11);
275 writer_writeDouble(w, matrix->tx);
276 writer_writeDouble(w, matrix->ty);
278 state->size_matrices += 6*8;
281 static gfxmatrix_t readMatrix(reader_t*r, state_t*state)
284 matrix.m00 = reader_readDouble(r);
285 matrix.m01 = reader_readDouble(r);
286 matrix.m10 = reader_readDouble(r);
287 matrix.m11 = reader_readDouble(r);
288 matrix.tx = reader_readDouble(r);
289 matrix.ty = reader_readDouble(r);
292 static void dumpXY(writer_t*w, state_t*state, gfxmatrix_t*matrix)
294 writer_writeDouble(w, matrix->tx);
295 writer_writeDouble(w, matrix->ty);
297 state->size_positions += 2*8;
300 static void readXY(reader_t*r, state_t*state, gfxmatrix_t*m)
302 m->tx = reader_readDouble(r);
303 m->ty = reader_readDouble(r);
306 static void dumpColor(writer_t*w, state_t*state, gfxcolor_t*color)
308 writer_writeU8(w, color->r);
309 writer_writeU8(w, color->g);
310 writer_writeU8(w, color->b);
311 writer_writeU8(w, color->a);
313 state->size_colors += 4;
316 static gfxcolor_t readColor(reader_t*r, state_t*state)
319 col.r = reader_readU8(r);
320 col.g = reader_readU8(r);
321 col.b = reader_readU8(r);
322 col.a = reader_readU8(r);
326 static void dumpGradient(writer_t*w, state_t*state, gfxgradient_t*gradient)
329 writer_writeU8(w, 1);
330 dumpColor(w, state, &gradient->color);
331 writer_writeFloat(w, gradient->pos);
332 gradient = gradient->next;
334 writer_writeU8(w, 0);
336 static gfxgradient_t* readGradient(reader_t*r, state_t*state)
338 gfxgradient_t*start = 0, *pos = 0;
340 U8 op = reader_readU8(r);
343 gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
350 g->color = readColor(r, state);
351 g->pos = reader_readFloat(r);
356 static void dumpCXForm(writer_t*w, state_t*state, gfxcxform_t*c)
359 writer_writeU8(w, 0);
361 writer_writeU8(w, 1);
362 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
363 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
364 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
365 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
368 static gfxcxform_t* readCXForm(reader_t*r, state_t*state)
370 U8 type = reader_readU8(r);
373 gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
374 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
375 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
376 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
377 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
381 static void dumpFont(writer_t*w, state_t*state, gfxfont_t*font)
385 int old_size_lines = state->size_lines;
387 writer_writeString(w, font->id);
388 writer_writeU32(w, font->num_glyphs);
389 writer_writeU32(w, font->max_unicode);
390 writer_writeDouble(w, font->ascent);
391 writer_writeDouble(w, font->descent);
393 for(t=0;t<font->num_glyphs;t++) {
394 dumpLine(w, state, font->glyphs[t].line);
395 writer_writeDouble(w, font->glyphs[t].advance);
396 writer_writeU32(w, font->glyphs[t].unicode);
397 if(font->glyphs[t].name) {
398 writer_writeString(w,font->glyphs[t].name);
403 for(t=0;t<font->max_unicode;t++) {
404 writer_writeU32(w, font->unicode2glyph[t]);
406 writer_writeU32(w, font->kerning_size);
407 for(t=0;t<font->kerning_size;t++) {
408 writer_writeU32(w, font->kerning[t].c1);
409 writer_writeU32(w, font->kerning[t].c2);
410 writer_writeU32(w, font->kerning[t].advance);
413 state->size_lines = old_size_lines;
414 state->size_fonts += w->pos - oldpos;
417 static gfxfont_t*readFont(reader_t*r, state_t*state)
419 gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
420 font->id = reader_readString(r);
421 font->num_glyphs = reader_readU32(r);
422 font->max_unicode = reader_readU32(r);
423 font->ascent = reader_readDouble(r);
424 font->descent = reader_readDouble(r);
425 font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
426 font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
428 for(t=0;t<font->num_glyphs;t++) {
429 font->glyphs[t].line = readLine(r, state);
430 font->glyphs[t].advance = reader_readDouble(r);
431 font->glyphs[t].unicode = reader_readU32(r);
432 font->glyphs[t].name = reader_readString(r);
433 if(!font->glyphs[t].name[0]) {
434 free((void*)(font->glyphs[t].name));
435 font->glyphs[t].name = 0;
438 for(t=0;t<font->max_unicode;t++) {
439 font->unicode2glyph[t] = reader_readU32(r);
441 font->kerning_size = reader_readU32(r);
442 if(font->kerning_size) {
443 font->kerning = malloc(sizeof(gfxkerning_t)*font->kerning_size);
444 for(t=0;t<font->kerning_size;t++) {
445 font->kerning[t].c1 = reader_readU32(r);
446 font->kerning[t].c2 = reader_readU32(r);
447 font->kerning[t].advance = reader_readU32(r);
453 /* ----------------- reading/writing of primitives with caching -------------- */
455 static char* read_string(reader_t*r, state_t*state, U8 id, U8 flags)
457 assert(id>=0 && id<16);
458 if(flags&FLAG_SAME_AS_LAST) {
459 assert(state->last_string[id]);
460 return strdup(state->last_string[id]);
462 char*s = reader_readString(r);
463 state->last_string[id] = strdup(s);
466 static gfxcolor_t read_color(reader_t*r, state_t*state, U8 id, U8 flags)
468 assert(id>=0 && id<16);
469 if(flags&FLAG_SAME_AS_LAST)
470 return state->last_color[id];
471 gfxcolor_t c = readColor(r, state);
472 state->last_color[id] = c;
475 static gfxmatrix_t read_matrix(reader_t*r, state_t*state, U8 id, U8 flags)
477 assert(id>=0 && id<16);
478 if(flags&FLAG_SAME_AS_LAST) {
479 gfxmatrix_t m = state->last_matrix[id];
480 readXY(r, state, &m);
483 gfxmatrix_t m = readMatrix(r, state);
484 state->last_matrix[id] = m;
488 /* --------------------------- record device operations ---------------------- */
490 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
492 internal_t*i = (internal_t*)dev->internal;
493 msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
494 writer_writeU8(&i->w, OP_SETPARAM);
495 writer_writeString(&i->w, key);
496 writer_writeString(&i->w, value);
500 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)
502 internal_t*i = (internal_t*)dev->internal;
503 msg("<trace> record: %08x STROKE\n", dev);
504 writer_writeU8(&i->w, OP_STROKE);
505 writer_writeDouble(&i->w, width);
506 writer_writeDouble(&i->w, miterLimit);
507 dumpColor(&i->w, &i->state, color);
508 writer_writeU8(&i->w, cap_style);
509 writer_writeU8(&i->w, joint_style);
510 dumpLine(&i->w, &i->state, line);
513 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
515 internal_t*i = (internal_t*)dev->internal;
516 msg("<trace> record: %08x STARTCLIP\n", dev);
517 writer_writeU8(&i->w, OP_STARTCLIP);
518 dumpLine(&i->w, &i->state, line);
522 static void record_endclip(struct _gfxdevice*dev)
524 internal_t*i = (internal_t*)dev->internal;
525 msg("<trace> record: %08x ENDCLIP\n", dev);
526 writer_writeU8(&i->w, OP_ENDCLIP);
529 msg("<error> record: endclip() without startclip()");
533 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
535 internal_t*i = (internal_t*)dev->internal;
536 msg("<trace> record: %08x FILL\n", dev);
537 writer_writeU8(&i->w, OP_FILL);
538 dumpColor(&i->w, &i->state, color);
539 dumpLine(&i->w, &i->state, line);
542 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
544 internal_t*i = (internal_t*)dev->internal;
545 msg("<trace> record: %08x FILLBITMAP\n", dev);
546 writer_writeU8(&i->w, OP_FILLBITMAP);
547 dumpImage(&i->w, &i->state, img);
548 dumpMatrix(&i->w, &i->state, matrix);
549 dumpLine(&i->w, &i->state, line);
550 dumpCXForm(&i->w, &i->state, cxform);
553 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
555 internal_t*i = (internal_t*)dev->internal;
556 msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
557 writer_writeU8(&i->w, OP_FILLGRADIENT);
558 writer_writeU8(&i->w, type);
559 dumpGradient(&i->w, &i->state, gradient);
560 dumpMatrix(&i->w, &i->state, matrix);
561 dumpLine(&i->w, &i->state, line);
564 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
566 internal_t*i = (internal_t*)dev->internal;
567 msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
568 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
569 writer_writeU8(&i->w, OP_ADDFONT);
570 dumpFont(&i->w, &i->state, font);
571 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
575 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
577 internal_t*i = (internal_t*)dev->internal;
578 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
579 record_addfont(dev, font);
582 msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
583 const char*font_id = (font&&font->id)?font->id:"*NULL*";
585 gfxmatrix_t*l = &i->state.last_matrix[OP_DRAWCHAR];
589 flags |= FLAG_ZERO_FONT;
591 char same_font = i->state.last_string[OP_DRAWCHAR] && !strcmp(i->state.last_string[OP_DRAWCHAR], font_id);
592 char same_matrix = (l->m00 == matrix->m00) && (l->m01 == matrix->m01) && (l->m10 == matrix->m10) && (l->m11 == matrix->m11);
593 char same_color = !memcmp(color, &i->state.last_color[OP_DRAWCHAR], sizeof(gfxcolor_t));
596 if(same_font && same_matrix && same_color)
597 flags |= FLAG_SAME_AS_LAST;
600 writer_writeU8(&i->w, OP_DRAWCHAR|flags);
601 writer_writeU32(&i->w, glyphnr);
603 i->state.size_chars += 5;
606 if(!(flags&FLAG_SAME_AS_LAST)) {
607 if(!(flags&FLAG_ZERO_FONT))
608 writer_writeString(&i->w, font_id);
609 dumpColor(&i->w, &i->state, color);
610 dumpMatrix(&i->w, &i->state, matrix);
611 i->state.last_string[OP_DRAWCHAR] = strdup(font_id);
612 i->state.last_color[OP_DRAWCHAR] = *color;
613 i->state.last_matrix[OP_DRAWCHAR] = *matrix;
615 dumpXY(&i->w, &i->state, matrix);
619 static void record_startpage(struct _gfxdevice*dev, int width, int height)
621 internal_t*i = (internal_t*)dev->internal;
622 msg("<trace> record: %08x STARTPAGE\n", dev);
623 writer_writeU8(&i->w, OP_STARTPAGE);
624 writer_writeU16(&i->w, width);
625 writer_writeU16(&i->w, height);
628 static void record_endpage(struct _gfxdevice*dev)
630 internal_t*i = (internal_t*)dev->internal;
631 msg("<trace> record: %08x ENDPAGE\n", dev);
632 writer_writeU8(&i->w, OP_ENDPAGE);
635 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
637 internal_t*i = (internal_t*)dev->internal;
638 msg("<trace> record: %08x DRAWLINK\n", dev);
639 writer_writeU8(&i->w, OP_DRAWLINK);
640 dumpLine(&i->w, &i->state, line);
641 writer_writeString(&i->w, action);
644 /* ------------------------------- replaying --------------------------------- */
646 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
650 i = (internal_t*)dev->internal;
654 memset(&state, 0, sizeof(state));
656 gfxfontlist_t* fontlist = gfxfontlist_create();
660 if(r->read(r, &op, 1)!=1)
662 unsigned char flags = op&0xf0;
669 msg("<trace> replay: SETPARAM");
672 key = reader_readString(r);
673 value = reader_readString(r);
674 out->setparameter(out, key, value);
680 msg("<trace> replay: STARTPAGE");
681 U16 width = reader_readU16(r);
682 U16 height = reader_readU16(r);
683 out->startpage(out, width, height);
687 msg("<trace> replay: ENDPAGE");
692 msg("<trace> replay: FINISH");
696 msg("<trace> replay: STROKE");
697 double width = reader_readDouble(r);
698 double miterlimit = reader_readDouble(r);
699 gfxcolor_t color = readColor(r, &state);
701 int v = reader_readU8(r);
703 case 0: captype = gfx_capButt; break;
704 case 1: captype = gfx_capRound; break;
705 case 2: captype = gfx_capSquare; break;
707 gfx_joinType jointtype;
708 v = reader_readU8(r);
710 case 0: jointtype = gfx_joinMiter; break;
711 case 1: jointtype = gfx_joinRound; break;
712 case 2: jointtype = gfx_joinBevel; break;
714 gfxline_t* line = readLine(r, &state);
715 out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
720 msg("<trace> replay: STARTCLIP");
721 gfxline_t* line = readLine(r, &state);
722 out->startclip(out, line);
727 msg("<trace> replay: ENDCLIP");
732 msg("<trace> replay: FILL");
733 gfxcolor_t color = readColor(r, &state);
734 gfxline_t* line = readLine(r, &state);
735 out->fill(out, line, &color);
739 case OP_FILLBITMAP: {
740 msg("<trace> replay: FILLBITMAP");
741 gfximage_t img = readImage(r, &state);
742 gfxmatrix_t matrix = readMatrix(r, &state);
743 gfxline_t* line = readLine(r, &state);
744 gfxcxform_t* cxform = readCXForm(r, &state);
745 out->fillbitmap(out, line, &img, &matrix, cxform);
749 free(img.data);img.data=0;
752 case OP_FILLGRADIENT: {
753 msg("<trace> replay: FILLGRADIENT");
754 gfxgradienttype_t type;
755 int v = reader_readU8(r);
758 type = gfxgradient_radial; break;
760 type = gfxgradient_linear; break;
762 gfxgradient_t*gradient = readGradient(r, &state);
763 gfxmatrix_t matrix = readMatrix(r, &state);
764 gfxline_t* line = readLine(r, &state);
765 out->fillgradient(out, line, gradient, type, &matrix);
769 msg("<trace> replay: DRAWLINK");
770 gfxline_t* line = readLine(r, &state);
771 char* s = reader_readString(r);
772 out->drawlink(out,line,s);
778 msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
779 gfxfont_t*font = readFont(r, &state);
780 fontlist = gfxfontlist_addfont(fontlist, font);
781 out->addfont(out, font);
785 U32 glyph = reader_readU32(r);
786 gfxmatrix_t m = {1,0,0, 0,1,0};
788 if(!(flags&FLAG_ZERO_FONT))
789 id = read_string(r, &state, op, flags);
790 gfxcolor_t color = read_color(r, &state, op, flags);
791 gfxmatrix_t matrix = read_matrix(r, &state, op, flags);
793 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
795 font = gfxfontlist_findfont(i->fontlist, id);
797 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
798 out->drawchar(out, font, glyph, &color, &matrix);
807 /* problem: if we just replayed into a device which stores the
808 font for later use (the record device itself is a nice example),
809 then we can't free it yet */
810 //gfxfontlist_free(fontlist, 1);
811 gfxfontlist_free(fontlist, 0);
813 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
815 internal_result_t*i = (internal_result_t*)result->internal;
818 if(i->use_tempfile) {
819 reader_init_filereader2(&r, i->filename);
821 reader_init_memreader(&r, i->data, i->length);
824 replay(0, device, &r);
827 static void record_result_write(gfxresult_t*r, int filedesc)
829 internal_result_t*i = (internal_result_t*)r->internal;
831 write(filedesc, i->data, i->length);
834 static int record_result_save(gfxresult_t*r, const char*filename)
836 internal_result_t*i = (internal_result_t*)r->internal;
837 if(i->use_tempfile) {
838 move_file(i->filename, filename);
840 FILE*fi = fopen(filename, "wb");
842 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
845 fwrite(i->data, i->length, 1, fi);
850 static void*record_result_get(gfxresult_t*r, const char*name)
852 internal_result_t*i = (internal_result_t*)r->internal;
853 if(!strcmp(name, "data")) {
855 } else if(!strcmp(name, "length")) {
860 static void record_result_destroy(gfxresult_t*r)
862 internal_result_t*i = (internal_result_t*)r->internal;
864 free(i->data);i->data = 0;
870 free(r->internal);r->internal = 0;
874 static unsigned char printable(unsigned char a)
876 if(a<32 || a==127) return '.';
880 static void hexdumpMem(unsigned char*data, int len)
885 printf("%02x ", data[t]);
886 ascii[t&15] = printable(data[t]);
887 if((t && ((t&15)==15)) || (t==len-1))
891 for(s=p-1;s<16;s++) {
894 printf(" %s\n", ascii);
899 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
901 internal_t*i = (internal_t*)dev->internal;
903 if(!i->use_tempfile) {
905 void*data = writer_growmemwrite_memptr(&i->w, &len);
907 reader_init_memreader(&r, data, len);
908 replay(dev, out, &r);
909 writer_growmemwrite_reset(&i->w);
911 msg("<fatal> Flushing not supported for file based record device");
917 static gfxresult_t* record_finish(struct _gfxdevice*dev)
919 internal_t*i = (internal_t*)dev->internal;
920 msg("<trace> record: %08x END", dev);
923 msg("<error> Warning: unclosed cliplevels");
927 int total = i->w.pos;
928 if(total && i->use_tempfile) {
929 state_t*s = &i->state;
930 msg("<notice> record device finished. stats:");
931 msg("<notice> %4.1f%% matrices (%d bytes)", s->size_matrices*100.0/total, s->size_matrices);
932 msg("<notice> %4.1f%% positions (%d bytes)", s->size_positions*100.0/total, s->size_positions);
933 msg("<notice> %4.1f%% colors (%d bytes)", s->size_colors*100.0/total, s->size_colors);
934 msg("<notice> %4.1f%% lines (%d bytes)", s->size_lines*100.0/total, s->size_lines);
935 msg("<notice> %4.1f%% fonts (%d bytes)", s->size_fonts*100.0/total, s->size_fonts);
936 msg("<notice> %4.1f%% images (%d bytes)", s->size_images*100.0/total, s->size_images);
937 msg("<notice> %4.1f%% characters (%d bytes)", s->size_chars*100.0/total, s->size_chars);
938 msg("<notice> total: %d bytes", total);
942 writer_writeU8(&i->w, OP_END);
944 gfxfontlist_free(i->fontlist, 0);
946 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
948 ir->use_tempfile = i->use_tempfile;
949 if(i->use_tempfile) {
950 ir->filename = i->filename;
952 ir->data = writer_growmemwrite_getmem(&i->w);
953 ir->length = i->w.pos;
957 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
958 result->save = record_result_save;
959 result->get = record_result_get;
960 result->destroy = record_result_destroy;
961 result->internal = ir;
963 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
968 void gfxdevice_record_init(gfxdevice_t*dev, char use_tempfile)
970 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
971 memset(dev, 0, sizeof(gfxdevice_t));
973 dev->name = "record";
977 i->use_tempfile = use_tempfile;
979 writer_init_growingmemwriter(&i->w, 1048576);
982 i->filename = strdup(mktempname(buffer, "gfx"));
983 writer_init_filewriter2(&i->w, i->filename);
985 i->fontlist = gfxfontlist_create();
988 dev->setparameter = record_setparameter;
989 dev->startpage = record_startpage;
990 dev->startclip = record_startclip;
991 dev->endclip = record_endclip;
992 dev->stroke = record_stroke;
993 dev->fill = record_fill;
994 dev->fillbitmap = record_fillbitmap;
995 dev->fillgradient = record_fillgradient;
996 dev->addfont = record_addfont;
997 dev->drawchar = record_drawchar;
998 dev->drawlink = record_drawlink;
999 dev->endpage = record_endpage;
1000 dev->finish = record_finish;