poppler: XMLOutputDev::endPage: update to use new API
[swftools.git] / lib / pdf / XMLOutputDev.cc
1 /* XMLOutputDev.cc
2
3    This file is part of swftools.
4
5    Swftools is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    Swftools is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with swftools; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
18
19 #include "../../config.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "XMLOutputDev.h"
23 #include "GfxState.h"
24 #ifndef HAVE_POPPLER
25   #include "gfile.h"
26 #endif
27
28 XMLOutputDev::XMLOutputDev(char*filename)
29 :TextOutputDev(mktmpname(0), false, false, false)
30 {
31   out = fopen(filename, "wb");
32   if(!out) {
33       perror(filename);
34       exit(-1);
35   }
36   fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
37   fprintf(out, "<document>\n");
38 }
39
40 XMLOutputDev::~XMLOutputDev()
41 {
42   fprintf(out, "</document>\n");
43   fclose(out);
44 }
45
46 void XMLOutputDev::startPage(int pageNum, GfxState *state)
47 {
48     TextOutputDev::startPage(pageNum, state);
49     fprintf(out, "<page nr=\"%d\" width=\"%.0f\" height=\"%.0f\">\n", pageNum,
50         state->getPageWidth(), state->getPageHeight());
51 }
52
53 void XMLOutputDev::endPage()
54 {
55     TextOutputDev::endPage();
56     TextWordList* list = makeWordList();
57     int len = list->getLength();
58     int i;
59
60     char textTag = 0;
61     GString*fontname = new GString();
62     double fontsize = -99999;
63     double base = -9999;
64     double color_r = -1;
65     double color_g = -1;
66     double color_b = -1;
67     for(i=0;i<len;i++) {
68         TextWord*word = list->get(i);
69         GString*newfont = word->getFontName();
70         double newsize = word->getFontSize();
71 #ifdef HAVE_POPPLER
72         double newbase = word->getBaseline();
73 #else
74         double newbase = word->base;
75 #endif
76         double newcolor_r;
77         double newcolor_g;
78         double newcolor_b;
79   word->getColor(&newcolor_r, &newcolor_g, &newcolor_b);
80
81         if((newfont && newfont->cmp(fontname)) || 
82            newsize != fontsize ||
83            newbase != base ||
84            newcolor_r != color_r ||
85            newcolor_g != color_g ||
86            newcolor_b != color_b
87            ) 
88         {
89             TextFontInfo*info = word->getFontInfo();
90             if(textTag)
91                 fprintf(out, "</t>\n");
92             textTag = 1;
93             GBool italic = gFalse;
94             GBool bold = gFalse;
95             GBool serif = gFalse;
96
97             if(info->isItalic()) italic = gTrue;
98             if(info->isBold()) bold = gTrue;
99             if(info->isSerif()) serif = gTrue;
100             char*name = (char*)"";
101             if(newfont) {
102                 name = newfont->lowerCase()->getCString();
103                 if(strlen(name)>7 && name[6]=='+')
104                     name += 7;
105                 if(strstr(name, "ital")) italic = gTrue;
106                 if(strstr(name, "slan")) italic = gTrue;
107                 if(strstr(name, "obli")) italic = gTrue;
108                 if(strstr(name, "bold")) bold = gTrue;
109                 if(strstr(name, "heav")) bold = gTrue;
110                 if(strstr(name, "medi")) bold = gTrue;
111                 if(strstr(name, "serif")) serif = gTrue;
112             }
113
114     double xMin,yMin,xMax,yMax;
115     word->getBBox(&xMin, &yMin, &xMax, &yMax);
116
117     int rot = word->getRotation();
118
119             fprintf(out, "<t font=\"%s\" y=\"%f\" x=\"%f\" bbox=\"%f:%f:%f:%f\" style=\"%s%s%s%s\" fontsize=\"%.0fpt\" color=\"%02x%02x%02x\">", 
120                     name,
121                     newbase,
122                     (rot&1)?yMin:xMin,
123                     (rot&1)?yMin:xMin,
124                     (rot&1)?xMin:yMin,
125                     (rot&1)?yMax:xMax,
126                     (rot&1)?xMax:yMax,
127                     info->isFixedWidth()?"fixed;":"",
128                     serif?"serif;":"",
129                     italic?"italic;":"",
130                     bold?"bold;":"",
131                     newsize,
132                     ((int)(newcolor_r*255))&0xff,
133                     ((int)(newcolor_g*255))&0xff,
134                     ((int)(newcolor_b*255))&0xff
135                     );
136             fontname = newfont->copy();
137             fontsize = newsize;
138             base = newbase;
139             color_r = newcolor_r;
140             color_g = newcolor_g;
141             color_b = newcolor_b;
142         }
143         char*s = word->getText()->getCString();
144         while(*s) {
145             switch(*s) {
146                 case '<': fprintf(out, "&lt;");break;
147                 case '>': fprintf(out, "&gt;");break;
148                 case '&': fprintf(out, "&amp;");break;
149                 default: fwrite(s, 1, 1, out);
150             }
151             s++;
152         }
153         if(word->getSpaceAfter())
154             fprintf(out, " ");
155     }
156     if(textTag) fprintf(out, "</t>\n");
157     fprintf(out, "</page>\n");
158 }
159