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