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"
46 typedef struct _state {
48 gfxcolor_t last_color[16];
49 gfxmatrix_t last_matrix[16];
62 typedef struct _internal {
63 gfxfontlist_t* fontlist;
72 typedef struct _internal_result {
80 #define OP_SETPARAM 0x01
81 #define OP_STROKE 0x02
82 #define OP_STARTCLIP 0x03
83 #define OP_ENDCLIP 0x04
85 #define OP_FILLBITMAP 0x06
86 #define OP_FILLGRADIENT 0x07
87 #define OP_ADDFONT 0x08
88 #define OP_DRAWCHAR 0x09
89 #define OP_DRAWLINK 0x0a
90 #define OP_STARTPAGE 0x0b
91 #define OP_ENDPAGE 0x0c
92 #define OP_FINISH 0x0d
94 #define FLAG_SAME_AS_LAST 0x10
96 #define LINE_MOVETO 0x0e
97 #define LINE_LINETO 0x0f
98 #define LINE_SPLINETO 0x10
100 /* ----------------- reading/writing of low level primitives -------------- */
102 static void dumpLine(writer_t*w, state_t*state, gfxline_t*line)
105 if(line->type == gfx_moveTo) {
106 writer_writeU8(w, LINE_MOVETO);
107 writer_writeDouble(w, line->x);
108 writer_writeDouble(w, line->y);
110 state->size_lines += 1+8+8;
112 } else if(line->type == gfx_lineTo) {
113 writer_writeU8(w, LINE_LINETO);
114 writer_writeDouble(w, line->x);
115 writer_writeDouble(w, line->y);
117 state->size_lines += 1+8+8;
119 } else if(line->type == gfx_splineTo) {
120 writer_writeU8(w, LINE_SPLINETO);
121 writer_writeDouble(w, line->x);
122 writer_writeDouble(w, line->y);
123 writer_writeDouble(w, line->sx);
124 writer_writeDouble(w, line->sy);
126 state->size_lines += 1+8+8+8+8;
131 writer_writeU8(w, OP_END);
133 state->size_lines += 1;
136 static gfxline_t* readLine(reader_t*r, state_t*s)
138 gfxline_t*start = 0, *pos = 0;
140 unsigned char op = reader_readU8(r);
143 gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
150 if(op == LINE_MOVETO) {
151 line->type = gfx_moveTo;
152 line->x = reader_readDouble(r);
153 line->y = reader_readDouble(r);
154 } else if(op == LINE_LINETO) {
155 line->type = gfx_lineTo;
156 line->x = reader_readDouble(r);
157 line->y = reader_readDouble(r);
158 } else if(op == LINE_SPLINETO) {
159 line->type = gfx_splineTo;
160 line->x = reader_readDouble(r);
161 line->y = reader_readDouble(r);
162 line->sx = reader_readDouble(r);
163 line->sy = reader_readDouble(r);
169 #define COMPRESS_IMAGES
170 static void dumpImage(writer_t*w, state_t*state, gfximage_t*img)
173 writer_writeU16(w, img->width);
174 writer_writeU16(w, img->height);
175 #ifdef COMPRESS_IMAGES
176 //45.2% images (3055340 bytes) (without filter)
177 //39.9% images (2458904 bytes) (with filter, Z_BEST_SPEED)
178 //35.3% images (2027305 bytes) (with filter, Z_BEST_COMPRESSION)
179 unsigned char*filter = malloc(img->height);
181 gfxcolor_t*image = malloc(img->width*img->height*sizeof(gfxcolor_t));
182 for(y=0;y<img->height;y++) {
183 filter[y] = png_apply_filter_32(
184 (void*)&image[y*img->width],
185 (void*)&img->data[y*img->width], img->width, y);
187 int size = img->width*img->height;
188 uLongf compressdata_size = compressBound(size*sizeof(gfxcolor_t));
189 void*compressdata = malloc(compressdata_size);
190 compress2(compressdata, &compressdata_size, (void*)image, sizeof(gfxcolor_t)*size, Z_BEST_SPEED);
191 writer_writeU32(w, compressdata_size);
192 w->write(w, filter, img->height);
193 w->write(w, compressdata, compressdata_size);
197 w->write(w, img->data, img->width*img->height*sizeof(gfxcolor_t));
200 state->size_images += w->pos - oldpos;
203 static gfximage_t readImage(reader_t*r, state_t*state)
206 img.width = reader_readU16(r);
207 img.height = reader_readU16(r);
208 uLongf size = img.width*img.height*sizeof(gfxcolor_t);
209 img.data = malloc(size);
210 #ifdef COMPRESS_IMAGES
211 uLongf compressdata_size = reader_readU32(r);
212 void*compressdata = malloc(compressdata_size);
213 unsigned char*filter = malloc(img.height);
214 r->read(r, filter, img.height);
215 r->read(r, compressdata, compressdata_size);
216 uncompress((void*)img.data, &size, compressdata, compressdata_size);
218 unsigned char*line = malloc(img.width*sizeof(gfxcolor_t));
219 for(y=0;y<img.height;y++) {
220 png_inverse_filter_32(filter[y], (void*)&img.data[y*img.width],
221 y?(void*)&img.data[(y-1)*img.width]:(void*)0,
223 memcpy(&img.data[y*img.width], line, img.width*sizeof(gfxcolor_t));
227 r->read(r, img.data, size);
232 static void dumpMatrix(writer_t*w, state_t*state, gfxmatrix_t*matrix)
234 writer_writeDouble(w, matrix->m00);
235 writer_writeDouble(w, matrix->m01);
236 writer_writeDouble(w, matrix->m10);
237 writer_writeDouble(w, matrix->m11);
238 writer_writeDouble(w, matrix->tx);
239 writer_writeDouble(w, matrix->ty);
241 state->size_matrices += 6*8;
244 static gfxmatrix_t readMatrix(reader_t*r, state_t*state)
247 matrix.m00 = reader_readDouble(r);
248 matrix.m01 = reader_readDouble(r);
249 matrix.m10 = reader_readDouble(r);
250 matrix.m11 = reader_readDouble(r);
251 matrix.tx = reader_readDouble(r);
252 matrix.ty = reader_readDouble(r);
255 static void dumpXY(writer_t*w, state_t*state, gfxmatrix_t*matrix)
257 writer_writeDouble(w, matrix->tx);
258 writer_writeDouble(w, matrix->ty);
260 state->size_positions += 2*8;
263 static void readXY(reader_t*r, state_t*state, gfxmatrix_t*m)
265 m->tx = reader_readDouble(r);
266 m->ty = reader_readDouble(r);
269 static void dumpColor(writer_t*w, state_t*state, gfxcolor_t*color)
271 writer_writeU8(w, color->r);
272 writer_writeU8(w, color->g);
273 writer_writeU8(w, color->b);
274 writer_writeU8(w, color->a);
276 state->size_colors += 4;
279 static gfxcolor_t readColor(reader_t*r, state_t*state)
282 col.r = reader_readU8(r);
283 col.g = reader_readU8(r);
284 col.b = reader_readU8(r);
285 col.a = reader_readU8(r);
289 static void dumpGradient(writer_t*w, state_t*state, gfxgradient_t*gradient)
292 writer_writeU8(w, 1);
293 dumpColor(w, state, &gradient->color);
294 writer_writeFloat(w, gradient->pos);
295 gradient = gradient->next;
297 writer_writeU8(w, 0);
299 static gfxgradient_t* readGradient(reader_t*r, state_t*state)
301 gfxgradient_t*start = 0, *pos = 0;
303 U8 op = reader_readU8(r);
306 gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
313 g->color = readColor(r, state);
314 g->pos = reader_readFloat(r);
319 static void dumpCXForm(writer_t*w, state_t*state, gfxcxform_t*c)
322 writer_writeU8(w, 0);
324 writer_writeU8(w, 1);
325 writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
326 writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
327 writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
328 writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
331 static gfxcxform_t* readCXForm(reader_t*r, state_t*state)
333 U8 type = reader_readU8(r);
336 gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
337 c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
338 c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
339 c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
340 c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
344 static void dumpFont(writer_t*w, state_t*state, gfxfont_t*font)
348 int old_size_lines = state->size_lines;
350 writer_writeString(w, font->id);
351 writer_writeU32(w, font->num_glyphs);
352 writer_writeU32(w, font->max_unicode);
353 writer_writeDouble(w, font->ascent);
354 writer_writeDouble(w, font->descent);
356 for(t=0;t<font->num_glyphs;t++) {
357 dumpLine(w, state, font->glyphs[t].line);
358 writer_writeDouble(w, font->glyphs[t].advance);
359 writer_writeU32(w, font->glyphs[t].unicode);
360 if(font->glyphs[t].name) {
361 writer_writeString(w,font->glyphs[t].name);
366 for(t=0;t<font->max_unicode;t++) {
367 writer_writeU32(w, font->unicode2glyph[t]);
369 writer_writeU32(w, font->kerning_size);
370 for(t=0;t<font->kerning_size;t++) {
371 writer_writeU32(w, font->kerning[t].c1);
372 writer_writeU32(w, font->kerning[t].c2);
373 writer_writeU32(w, font->kerning[t].advance);
376 state->size_lines = old_size_lines;
377 state->size_fonts += w->pos - oldpos;
380 static gfxfont_t*readFont(reader_t*r, state_t*state)
382 gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
383 font->id = reader_readString(r);
384 font->num_glyphs = reader_readU32(r);
385 font->max_unicode = reader_readU32(r);
386 font->ascent = reader_readDouble(r);
387 font->descent = reader_readDouble(r);
388 font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
389 font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
391 for(t=0;t<font->num_glyphs;t++) {
392 font->glyphs[t].line = readLine(r, state);
393 font->glyphs[t].advance = reader_readDouble(r);
394 font->glyphs[t].unicode = reader_readU32(r);
395 font->glyphs[t].name = reader_readString(r);
396 if(!font->glyphs[t].name[0]) {
397 free((void*)(font->glyphs[t].name));
398 font->glyphs[t].name = 0;
401 for(t=0;t<font->max_unicode;t++) {
402 font->unicode2glyph[t] = reader_readU32(r);
404 font->kerning_size = reader_readU32(r);
405 if(font->kerning_size) {
406 font->kerning = malloc(sizeof(gfxkerning_t)*font->kerning_size);
407 for(t=0;t<font->kerning_size;t++) {
408 font->kerning[t].c1 = reader_readU32(r);
409 font->kerning[t].c2 = reader_readU32(r);
410 font->kerning[t].advance = reader_readU32(r);
416 /* ----------------- reading/writing of primitives with caching -------------- */
418 static char* read_string(reader_t*r, state_t*state, U8 id, U8 flags)
420 assert(id>=0 && id<16);
421 if(flags&FLAG_SAME_AS_LAST) {
422 assert(state->last_string[id]);
423 return strdup(state->last_string[id]);
425 char*s = reader_readString(r);
426 state->last_string[id] = strdup(s);
429 static gfxcolor_t read_color(reader_t*r, state_t*state, U8 id, U8 flags)
431 assert(id>=0 && id<16);
432 if(flags&FLAG_SAME_AS_LAST)
433 return state->last_color[id];
434 gfxcolor_t c = readColor(r, state);
435 state->last_color[id] = c;
438 static gfxmatrix_t read_matrix(reader_t*r, state_t*state, U8 id, U8 flags)
440 assert(id>=0 && id<16);
441 if(flags&FLAG_SAME_AS_LAST) {
442 gfxmatrix_t m = state->last_matrix[id];
443 readXY(r, state, &m);
446 gfxmatrix_t m = readMatrix(r, state);
447 state->last_matrix[id] = m;
451 /* --------------------------- record device operations ---------------------- */
453 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
455 internal_t*i = (internal_t*)dev->internal;
456 msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
457 writer_writeU8(&i->w, OP_SETPARAM);
458 writer_writeString(&i->w, key);
459 writer_writeString(&i->w, value);
463 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)
465 internal_t*i = (internal_t*)dev->internal;
466 msg("<trace> record: %08x STROKE\n", dev);
467 writer_writeU8(&i->w, OP_STROKE);
468 writer_writeDouble(&i->w, width);
469 writer_writeDouble(&i->w, miterLimit);
470 dumpColor(&i->w, &i->state, color);
471 writer_writeU8(&i->w, cap_style);
472 writer_writeU8(&i->w, joint_style);
473 dumpLine(&i->w, &i->state, line);
476 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
478 internal_t*i = (internal_t*)dev->internal;
479 msg("<trace> record: %08x STARTCLIP\n", dev);
480 writer_writeU8(&i->w, OP_STARTCLIP);
481 dumpLine(&i->w, &i->state, line);
485 static void record_endclip(struct _gfxdevice*dev)
487 internal_t*i = (internal_t*)dev->internal;
488 msg("<trace> record: %08x ENDCLIP\n", dev);
489 writer_writeU8(&i->w, OP_ENDCLIP);
492 msg("<error> record: endclip() without startclip()");
496 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
498 internal_t*i = (internal_t*)dev->internal;
499 msg("<trace> record: %08x FILL\n", dev);
500 writer_writeU8(&i->w, OP_FILL);
501 dumpColor(&i->w, &i->state, color);
502 dumpLine(&i->w, &i->state, line);
505 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
507 internal_t*i = (internal_t*)dev->internal;
508 msg("<trace> record: %08x FILLBITMAP\n", dev);
509 writer_writeU8(&i->w, OP_FILLBITMAP);
510 dumpImage(&i->w, &i->state, img);
511 dumpMatrix(&i->w, &i->state, matrix);
512 dumpLine(&i->w, &i->state, line);
513 dumpCXForm(&i->w, &i->state, cxform);
516 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
518 internal_t*i = (internal_t*)dev->internal;
519 msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
520 writer_writeU8(&i->w, OP_FILLGRADIENT);
521 writer_writeU8(&i->w, type);
522 dumpGradient(&i->w, &i->state, gradient);
523 dumpMatrix(&i->w, &i->state, matrix);
524 dumpLine(&i->w, &i->state, line);
527 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
529 internal_t*i = (internal_t*)dev->internal;
530 msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
531 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
532 writer_writeU8(&i->w, OP_ADDFONT);
533 dumpFont(&i->w, &i->state, font);
534 i->fontlist = gfxfontlist_addfont(i->fontlist, font);
538 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
540 internal_t*i = (internal_t*)dev->internal;
541 if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
542 record_addfont(dev, font);
545 msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
546 const char*font_id = font->id?font->id:"*NULL*";
548 gfxmatrix_t*l = &i->state.last_matrix[OP_DRAWCHAR];
550 char same_font = i->state.last_string[OP_DRAWCHAR] && !strcmp(i->state.last_string[OP_DRAWCHAR], font_id);
551 char same_matrix = (l->m00 == matrix->m00) && (l->m01 == matrix->m01) && (l->m10 == matrix->m10) && (l->m11 == matrix->m11);
552 char same_color = !memcmp(color, &i->state.last_color[OP_DRAWCHAR], sizeof(gfxcolor_t));
554 if(same_font && same_matrix && same_color)
555 flags |= FLAG_SAME_AS_LAST;
557 writer_writeU8(&i->w, OP_DRAWCHAR|flags);
558 writer_writeU32(&i->w, glyphnr);
560 i->state.size_chars += 5;
563 if(!(flags&FLAG_SAME_AS_LAST)) {
564 writer_writeString(&i->w, font_id);
565 dumpColor(&i->w, &i->state, color);
566 dumpMatrix(&i->w, &i->state, matrix);
567 i->state.last_string[OP_DRAWCHAR] = strdup(font_id);
568 i->state.last_color[OP_DRAWCHAR] = *color;
569 i->state.last_matrix[OP_DRAWCHAR] = *matrix;
571 dumpXY(&i->w, &i->state, matrix);
575 static void record_startpage(struct _gfxdevice*dev, int width, int height)
577 internal_t*i = (internal_t*)dev->internal;
578 msg("<trace> record: %08x STARTPAGE\n", dev);
579 writer_writeU8(&i->w, OP_STARTPAGE);
580 writer_writeU16(&i->w, width);
581 writer_writeU16(&i->w, height);
584 static void record_endpage(struct _gfxdevice*dev)
586 internal_t*i = (internal_t*)dev->internal;
587 msg("<trace> record: %08x ENDPAGE\n", dev);
588 writer_writeU8(&i->w, OP_ENDPAGE);
591 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
593 internal_t*i = (internal_t*)dev->internal;
594 msg("<trace> record: %08x DRAWLINK\n", dev);
595 writer_writeU8(&i->w, OP_DRAWLINK);
596 dumpLine(&i->w, &i->state, line);
597 writer_writeString(&i->w, action);
600 /* ------------------------------- replaying --------------------------------- */
602 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
606 i = (internal_t*)dev->internal;
610 memset(&state, 0, sizeof(state));
612 gfxfontlist_t* fontlist = gfxfontlist_create();
616 if(r->read(r, &op, 1)!=1)
618 unsigned char flags = op&0xf0;
625 msg("<trace> replay: SETPARAM");
628 key = reader_readString(r);
629 value = reader_readString(r);
630 out->setparameter(out, key, value);
636 msg("<trace> replay: STARTPAGE");
637 U16 width = reader_readU16(r);
638 U16 height = reader_readU16(r);
639 out->startpage(out, width, height);
643 msg("<trace> replay: ENDPAGE");
648 msg("<trace> replay: FINISH");
652 msg("<trace> replay: STROKE");
653 double width = reader_readDouble(r);
654 double miterlimit = reader_readDouble(r);
655 gfxcolor_t color = readColor(r, &state);
657 int v = reader_readU8(r);
659 case 0: captype = gfx_capButt; break;
660 case 1: captype = gfx_capRound; break;
661 case 2: captype = gfx_capSquare; break;
663 gfx_joinType jointtype;
664 v = reader_readU8(r);
666 case 0: jointtype = gfx_joinMiter; break;
667 case 1: jointtype = gfx_joinRound; break;
668 case 2: jointtype = gfx_joinBevel; break;
670 gfxline_t* line = readLine(r, &state);
671 out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
676 msg("<trace> replay: STARTCLIP");
677 gfxline_t* line = readLine(r, &state);
678 out->startclip(out, line);
683 msg("<trace> replay: ENDCLIP");
688 msg("<trace> replay: FILL");
689 gfxcolor_t color = readColor(r, &state);
690 gfxline_t* line = readLine(r, &state);
691 out->fill(out, line, &color);
695 case OP_FILLBITMAP: {
696 msg("<trace> replay: FILLBITMAP");
697 gfximage_t img = readImage(r, &state);
698 gfxmatrix_t matrix = readMatrix(r, &state);
699 gfxline_t* line = readLine(r, &state);
700 gfxcxform_t* cxform = readCXForm(r, &state);
701 out->fillbitmap(out, line, &img, &matrix, cxform);
705 free(img.data);img.data=0;
708 case OP_FILLGRADIENT: {
709 msg("<trace> replay: FILLGRADIENT");
710 gfxgradienttype_t type;
711 int v = reader_readU8(r);
714 type = gfxgradient_radial; break;
716 type = gfxgradient_linear; break;
718 gfxgradient_t*gradient = readGradient(r, &state);
719 gfxmatrix_t matrix = readMatrix(r, &state);
720 gfxline_t* line = readLine(r, &state);
721 out->fillgradient(out, line, gradient, type, &matrix);
725 msg("<trace> replay: DRAWLINK");
726 gfxline_t* line = readLine(r, &state);
727 char* s = reader_readString(r);
728 out->drawlink(out,line,s);
734 msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
735 gfxfont_t*font = readFont(r, &state);
736 fontlist = gfxfontlist_addfont(fontlist, font);
737 out->addfont(out, font);
741 U32 glyph = reader_readU32(r);
742 gfxmatrix_t m = {1,0,0, 0,1,0};
743 char* id = read_string(r, &state, op, flags);
744 gfxcolor_t color = read_color(r, &state, op, flags);
745 gfxmatrix_t matrix = read_matrix(r, &state, op, flags);
747 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
749 font = gfxfontlist_findfont(i->fontlist, id);
751 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
752 out->drawchar(out, font, glyph, &color, &matrix);
760 /* problem: if we just replayed into a device which stores the
761 font for later use (the record device itself is a nice example),
762 then we can't free it yet */
763 //gfxfontlist_free(fontlist, 1);
764 gfxfontlist_free(fontlist, 0);
766 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
768 internal_result_t*i = (internal_result_t*)result->internal;
771 if(i->use_tempfile) {
772 reader_init_filereader2(&r, i->filename);
774 reader_init_memreader(&r, i->data, i->length);
777 replay(0, device, &r);
780 static void record_result_write(gfxresult_t*r, int filedesc)
782 internal_result_t*i = (internal_result_t*)r->internal;
784 write(filedesc, i->data, i->length);
787 static int record_result_save(gfxresult_t*r, const char*filename)
789 internal_result_t*i = (internal_result_t*)r->internal;
790 if(i->use_tempfile) {
791 move_file(i->filename, filename);
793 FILE*fi = fopen(filename, "wb");
795 fprintf(stderr, "Couldn't open file %s for writing\n", filename);
798 fwrite(i->data, i->length, 1, fi);
803 static void*record_result_get(gfxresult_t*r, const char*name)
805 internal_result_t*i = (internal_result_t*)r->internal;
806 if(!strcmp(name, "data")) {
808 } else if(!strcmp(name, "length")) {
813 static void record_result_destroy(gfxresult_t*r)
815 internal_result_t*i = (internal_result_t*)r->internal;
817 free(i->data);i->data = 0;
823 free(r->internal);r->internal = 0;
827 static unsigned char printable(unsigned char a)
829 if(a<32 || a==127) return '.';
833 static void hexdumpMem(unsigned char*data, int len)
838 printf("%02x ", data[t]);
839 ascii[t&15] = printable(data[t]);
840 if((t && ((t&15)==15)) || (t==len-1))
844 for(s=p-1;s<16;s++) {
847 printf(" %s\n", ascii);
852 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
854 internal_t*i = (internal_t*)dev->internal;
856 if(!i->use_tempfile) {
858 void*data = writer_growmemwrite_memptr(&i->w, &len);
860 reader_init_memreader(&r, data, len);
861 replay(dev, out, &r);
862 writer_growmemwrite_reset(&i->w);
864 msg("<fatal> Flushing not supported for file based record device");
870 static gfxresult_t* record_finish(struct _gfxdevice*dev)
872 internal_t*i = (internal_t*)dev->internal;
873 msg("<trace> record: %08x END", dev);
876 msg("<error> Warning: unclosed cliplevels");
880 int total = i->w.pos;
881 if(total && i->use_tempfile) {
882 state_t*s = &i->state;
883 msg("<notice> record device finished. stats:");
884 msg("<notice> %4.1f%% matrices (%d bytes)", s->size_matrices*100.0/total, s->size_matrices);
885 msg("<notice> %4.1f%% positions (%d bytes)", s->size_positions*100.0/total, s->size_positions);
886 msg("<notice> %4.1f%% colors (%d bytes)", s->size_colors*100.0/total, s->size_colors);
887 msg("<notice> %4.1f%% lines (%d bytes)", s->size_lines*100.0/total, s->size_lines);
888 msg("<notice> %4.1f%% fonts (%d bytes)", s->size_fonts*100.0/total, s->size_fonts);
889 msg("<notice> %4.1f%% images (%d bytes)", s->size_images*100.0/total, s->size_images);
890 msg("<notice> %4.1f%% characters (%d bytes)", s->size_chars*100.0/total, s->size_chars);
891 msg("<notice> total: %d bytes", total);
895 writer_writeU8(&i->w, OP_END);
897 gfxfontlist_free(i->fontlist, 0);
899 internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
901 ir->use_tempfile = i->use_tempfile;
902 if(i->use_tempfile) {
903 ir->filename = i->filename;
905 ir->data = writer_growmemwrite_getmem(&i->w);
906 ir->length = i->w.pos;
910 gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
911 result->save = record_result_save;
912 result->get = record_result_get;
913 result->destroy = record_result_destroy;
914 result->internal = ir;
916 free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
921 void gfxdevice_record_init(gfxdevice_t*dev, char use_tempfile)
923 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
924 memset(dev, 0, sizeof(gfxdevice_t));
926 dev->name = "record";
930 i->use_tempfile = use_tempfile;
932 writer_init_growingmemwriter(&i->w, 1048576);
935 i->filename = strdup(mktempname(buffer, "gfx"));
936 writer_init_filewriter2(&i->w, i->filename);
938 i->fontlist = gfxfontlist_create();
941 dev->setparameter = record_setparameter;
942 dev->startpage = record_startpage;
943 dev->startclip = record_startclip;
944 dev->endclip = record_endclip;
945 dev->stroke = record_stroke;
946 dev->fill = record_fill;
947 dev->fillbitmap = record_fillbitmap;
948 dev->fillgradient = record_fillgradient;
949 dev->addfont = record_addfont;
950 dev->drawchar = record_drawchar;
951 dev->drawlink = record_drawlink;
952 dev->endpage = record_endpage;
953 dev->finish = record_finish;