+ DICT_ITERATE_DATA(i->matrices, transformedfont_t*, fd) {
+ gfxfont_t*font = fd->font = rfx_calloc(sizeof(gfxfont_t));
+ char id[80];
+ static int fontcount=0;
+ sprintf(id, "font%d", fontcount++);
+ font->id = strdup(id);
+ int t;
+ int count=0;
+ for(t=0;t<fd->orig->num_glyphs;t++) {
+ if(fd->used[t])
+ count++;
+ }
+ font->num_glyphs = count;
+ font->glyphs = rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
+ count = 0;
+ for(t=0;t<fd->orig->num_glyphs;t++) {
+ if(fd->used[t]) {
+ font->glyphs[count] = fd->orig->glyphs[t];
+ glyph_transform(&font->glyphs[count], &fd->matrix);
+ fd->used[t] = count + 1;
+ count++;
+ }
+ }
+
+ /* adjust the origin so that every character is to the
+ right of the origin */
+ gfxbbox_t total = {0,0,0,0};
+ double average_xmax = 0;
+ for(t=0;t<count;t++) {
+ gfxline_t*line = font->glyphs[t].line;
+ gfxbbox_t b = gfxline_getbbox(line);
+ total = gfxbbox_expand_to_bbox(total, b);
+ }
+ if(count)
+ average_xmax /= count;
+
+ fd->dx = 0;//-total.xmin;
+
+ font->ascent = total.ymax;
+ font->descent = -total.ymin;
+
+ for(t=0;t<count;t++) {
+ gfxglyph_t*g = &font->glyphs[t];
+ gfxline_t*line = font->glyphs[t].line;
+
+ if(fd->matrix.alpha) {
+ while(line) {
+ line->x += fd->dx;
+ line->sx += fd->dx;
+ line = line->next;
+ }
+ } else {
+ gfxline_free(g->line);
+ /* for OCR: remove the outlines of characters that are only
+ ever displayed with alpha=0 */
+ g->line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
+ g->line->type = gfx_moveTo;
+ g->line->x = g->advance;
+ }
+ }
+
+ if(fd->matrix.m00>0) {
+ /* subset kerning table */
+ count = 0;
+ for(t=0;t<fd->orig->kerning_size;t++) {
+ int char1 = fd->used[fd->orig->kerning[t].c1]-1;
+ int char2 = fd->used[fd->orig->kerning[t].c2]-1;
+ if(char1>=0 && char2>=0) {
+ count++;
+ }
+ }
+ font->kerning = malloc(sizeof(font->kerning[0])*count);
+ font->kerning_size = count;
+ count = 0;
+ for(t=0;t<fd->orig->kerning_size;t++) {
+ int char1 = fd->used[fd->orig->kerning[t].c1]-1;
+ int char2 = fd->used[fd->orig->kerning[t].c2]-1;
+ if(char1>=0 && char2>=0) {
+ font->kerning[count].c1 = char1;
+ font->kerning[count].c2 = char2;
+ font->kerning[count].advance = fd->orig->kerning[t].advance * fd->matrix.m00;
+ count++;
+ }
+ }
+ }
+ gfxfont_fix_unicode(font);
+ }
+ return out->finish(out);
+}
+
+static void pass2_addfont(gfxfilter_t*f, gfxfont_t*font, gfxdevice_t*out)
+{
+ /* we throw away original fonts, and do the addfont() for the transformed
+ fonts in the first drawchar() */
+}
+
+static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*_color, gfxmatrix_t*matrix, gfxdevice_t*out)
+{
+ internal_t*i = (internal_t*)f->internal;
+ gfxcolor_t color = *_color;
+
+ if(i->first) {
+ i->first = 0;
+ DICT_ITERATE_DATA(i->matrices, transformedfont_t*, fd) {
+ out->addfont(out, fd->font);
+ }
+ }
+
+ mymatrix_t m;
+ gfxmatrix_t scalematrix;
+ matrix_convert(matrix, font->id?font->id:"unknown", &m, &scalematrix, color.a);
+ transformedfont_t*d = dict_lookup(i->matrices, &m);
+ scalematrix.tx -= d->dx*scalematrix.m00;
+
+ /* if this character is invisible (alpha=0), then we will have removed the
+ outline, so we make set the alpha color channel to "fully visible" again to allow
+ output devices to be more performant (transparency is expensive) */
+ if(!m.alpha)
+ color.a = 255;
+
+ out->drawchar(out, d->font, d->used[glyphnr]-1, &color, &scalematrix);
+}
+
+static gfxresult_t* pass2_finish(gfxfilter_t*f, gfxdevice_t*out)
+{
+ internal_t*i = (internal_t*)f->internal;
+ DICT_ITERATE_DATA(i->matrices, transformedfont_t*, fd) {
+ if(fd->used) {
+ free(fd->used);fd->used=0;
+ }
+ free(fd);
+ }
+ dict_destroy(i->matrices);i->matrices=0;
+ return out->finish(out);