+ finish();
+ return result->get(result, name);
+}
+
+SWFOutputDev::~SWFOutputDev()
+{
+ finish();
+
+ if(this->result) {
+ this->result->destroy(this->result);
+ this->result = 0;
+ }
+
+ if(this->pages) {
+ free(this->pages); this->pages = 0;
+ }
+
+ fontlist_t*l = this->fontlist;
+ while(l) {
+ fontlist_t*next = l->next;
+ l->next = 0;
+ gfxfont_free(l->font);
+ free(l->id);
+ free(l->filename);
+ free(l);
+ l = next;
+ }
+ this->fontlist = 0;
+};
+GBool SWFOutputDev::upsideDown()
+{
+ return gTrue;
+};
+GBool SWFOutputDev::useDrawChar()
+{
+ return gTrue;
+}
+GBool SWFOutputDev::useGradients()
+{
+ if(!gradientinfo)
+ {
+ msg("<notice> File contains gradients");
+ gradientinfo = 1;
+ }
+ return gTrue;
+}
+
+char*renderModeDesc[]= {"fill", "stroke", "fill+stroke", "invisible",
+ "clip+fill", "stroke+clip", "fill+stroke+clip", "clip"};
+
+#define RENDER_FILL 0
+#define RENDER_STROKE 1
+#define RENDER_FILLSTROKE 2
+#define RENDER_INVISIBLE 3
+#define RENDER_CLIP 4
+
+static char tmp_printstr[4096];
+char* makeStringPrintable(char*str)
+{
+ int len = strlen(str);
+ int dots = 0;
+ if(len>=80) {
+ len = 80;
+ dots = 1;
+ }
+ int t;
+ for(t=0;t<len;t++) {
+ char c = str[t];
+ if(c<32 || c>124) {
+ c = '.';
+ }
+ tmp_printstr[t] = c;
+ }
+ if(dots) {
+ tmp_printstr[len++] = '.';
+ tmp_printstr[len++] = '.';
+ tmp_printstr[len++] = '.';
+ }
+ tmp_printstr[len] = 0;
+ return tmp_printstr;
+}
+
+
+int getGfxCharID(gfxfont_t*font, int charnr, char *charname, int u)
+{
+ int t;
+ if(charname) {
+ for(t=0;t<font->num_glyphs;t++) {
+ if(font->glyphs[t].name && !strcmp(font->glyphs[t].name,charname)) {
+ msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
+ return t;
+ }
+ }
+ /* if we didn't find the character, maybe
+ we can find the capitalized version */
+ for(t=0;t<font->num_glyphs;t++) {
+ if(font->glyphs[t].name && !strcasecmp(font->glyphs[t].name,charname)) {
+ msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
+ return t;
+ }
+ }
+ }
+
+ /* try to use the unicode id */
+ if(u>=0 && u<font->max_unicode && font->unicode2glyph[u]>=0) {
+ msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->unicode2glyph[u]);
+ return font->unicode2glyph[u];
+ }
+
+ /* we don't need to "draw" space characters, so don't overdo the search
+ for a matching glyph */
+ if(charname && !strcasecmp(charname, "space"))
+ return -1;
+
+ if(charnr>=0 && charnr<font->num_glyphs) {
+ msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
+ return charnr;
+ }
+
+ return -1;
+}
+
+
+void SWFOutputDev::beginString(GfxState *state, GString *s)
+{
+ int render = state->getRender();
+ if(current_text_stroke) {
+ msg("<error> Error: Incompatible change of text rendering to %d while inside cliptext", render);
+ }
+
+ msg("<trace> beginString(%s) render=%d", makeStringPrintable(s->getCString()), render);
+ double m11,m21,m12,m22;
+// msg("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
+ state->getFontTransMat(&m11, &m12, &m21, &m22);
+ m11 *= state->getHorizScaling();
+ m21 *= state->getHorizScaling();
+
+ this->current_font_matrix.m00 = m11 / 1024.0;
+ this->current_font_matrix.m01 = m12 / 1024.0;
+ this->current_font_matrix.m10 = -m21 / 1024.0;
+ this->current_font_matrix.m11 = -m22 / 1024.0;
+ this->current_font_matrix.tx = 0;
+ this->current_font_matrix.ty = 0;
+
+ gfxmatrix_t m = this->current_font_matrix;
+
+ /*if(render != 3 && render != 0)
+ msg("<warning> Text rendering mode %d (%s) not fully supported yet (for text \"%s\")", render, renderModeDesc[render&7], makeStringPrintable(s->getCString()));*/
+ states[statepos].textRender = render;
+}
+
+void SWFOutputDev::drawChar(GfxState *state, double x, double y,
+ double dx, double dy,
+ double originX, double originY,
+ CharCode c, int nBytes, Unicode *_u, int uLen)
+{
+ int render = state->getRender();
+ // check for invisible text -- this is used by Acrobat Capture
+ if (render == 3) {
+ msg("<debug> Ignoring invisible text: char %d at %f,%f", c, x, y);
+ return;
+ }
+
+ if(states[statepos].textRender != render)
+ msg("<error> Internal error: drawChar.render!=beginString.render");
+
+ gfxcolor_t col = getFillColor(state);
+
+ Gushort *CIDToGIDMap = 0;
+ GfxFont*font = state->getFont();
+
+ if(font->getType() == fontType3) {
+ /* type 3 chars are passed as graphics */
+ msg("<debug> type3 char at %f/%f", x, y);
+ return;
+ }
+
+ Unicode u=0;
+ char*name=0;
+
+ if(_u && uLen) {
+ u = *_u;
+ if (u) {
+ int t;
+ /* find out char name from unicode index
+ TODO: should be precomputed
+ */
+ for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
+ if(nameToUnicodeTab[t].u == u) {
+ name = nameToUnicodeTab[t].name;
+ break;
+ }
+ }
+ }
+ }
+
+ if(font->isCIDFont()) {
+ GfxCIDFont*cfont = (GfxCIDFont*)font;
+
+ if(font->getType() == fontCIDType2)
+ CIDToGIDMap = cfont->getCIDToGID();
+ } else {
+ Gfx8BitFont*font8;
+ font8 = (Gfx8BitFont*)font;
+ char**enc=font8->getEncoding();
+ if(enc && enc[c] && strcasecmp(enc[c], "space")) {
+ name = enc[c];
+ }
+ }
+ if (CIDToGIDMap) {
+ msg("<debug> drawChar(%f, %f, c='%c' (%d), GID=%d, u=%d <%d>) CID=%d name=\"%s\" render=%d\n", x, y, (c&127)>=32?c:'?', c, CIDToGIDMap[c], u, uLen, font->isCIDFont(), FIXNULL(name), render);
+ c = CIDToGIDMap[c];
+ } else {
+ msg("<debug> drawChar(%f,%f,c='%c' (%d), u=%d <%d>) CID=%d name=\"%s\" render=%d\n",x,y,(c&127)>=32?c:'?',c,u, uLen, font->isCIDFont(), FIXNULL(name), render);
+ }
+
+ int charid = -1;
+
+ if(uLen<=1) {
+ charid = getGfxCharID(current_gfxfont, c, name, u);
+ } else {
+ charid = getGfxCharID(current_gfxfont, c, 0, -1);
+ if(charid < 0) {
+ /* multiple unicodes- should usually map to a ligature.
+ if the ligature doesn't exist, we need to draw
+ the characters one-by-one. */
+ int t;
+ msg("<warning> ligature %d missing in font %s\n", c, current_font_id);
+ for(t=0;t<uLen;t++) {
+ drawChar(state, x, y, dx, dy, originX, originY, c, nBytes, _u+t, 1);
+ }
+ return;
+ }
+ }
+
+ if(charid<0) {
+ if(!name || strcasecmp(name, "space")) {
+ msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
+ FIXNULL(name),c, u, FIXNULL((char*)current_font_id), current_gfxfont->num_glyphs);
+ }
+ return;
+ }
+
+ gfxmatrix_t m = this->current_font_matrix;
+ state->transform(x, y, &m.tx, &m.ty);
+ m.tx += user_movex;
+ m.ty += user_movey;
+
+ if(render == RENDER_FILL) {
+ output->drawchar(output, current_font_id, charid, &col, &m);
+ } else {
+ msg("<debug> Drawing glyph %d as shape", charid);
+ if(!textmodeinfo) {
+ msg("<notice> Some texts will be rendered as shape");
+ textmodeinfo = 1;
+ }
+ gfxline_t*glyph = current_gfxfont->glyphs[charid].line;
+ gfxline_t*tglyph = gfxline_clone(glyph);
+ gfxline_transform(tglyph, &m);
+ if((render&3) != RENDER_INVISIBLE) {
+ gfxline_t*add = gfxline_clone(tglyph);
+ current_text_stroke = gfxline_append(current_text_stroke, add);
+ }
+ if(render&RENDER_CLIP) {
+ gfxline_t*add = gfxline_clone(tglyph);
+ current_text_clip = gfxline_append(current_text_clip, add);
+ }
+ gfxline_free(tglyph);
+ }
+}
+
+void SWFOutputDev::endString(GfxState *state)
+{
+ int render = state->getRender();
+ msg("<trace> endString() render=%d textstroke=%08x", render, current_text_stroke);
+ if(states[statepos].textRender != render)
+ msg("<error> Internal error: drawChar.render!=beginString.render");
+
+ if(current_text_stroke) {
+ /* fillstroke and stroke text rendering objects we can process right
+ now (as there may be texts of other rendering modes in this
+ text object)- clipping objects have to wait until endTextObject,
+ however */
+ if((render&3) == RENDER_FILL) {
+ fillGfxLine(state, current_text_stroke);
+ gfxline_free(current_text_stroke);
+ current_text_stroke = 0;
+ } else if((render&3) == RENDER_FILLSTROKE) {
+ fillGfxLine(state, current_text_stroke);
+ strokeGfxline(state, current_text_stroke);
+ gfxline_free(current_text_stroke);
+ current_text_stroke = 0;
+ } else if((render&3) == RENDER_STROKE) {
+ strokeGfxline(state, current_text_stroke);
+ gfxline_free(current_text_stroke);
+ current_text_stroke = 0;
+ }
+ }
+}
+
+void SWFOutputDev::endTextObject(GfxState *state)
+{
+ int render = state->getRender();
+ msg("<trace> endTextObject() render=%d textstroke=%08x clipstroke=%08x", render, current_text_stroke, current_text_clip);
+ if(states[statepos].textRender != render)
+ msg("<error> Internal error: drawChar.render!=beginString.render");
+
+ if(current_text_clip) {
+ clipToGfxLine(state, current_text_clip);
+ gfxline_free(current_text_clip);
+ current_text_clip = 0;
+ }
+}
+
+/* the logic seems to be as following:
+ first, beginType3Char is called, with the charcode and the coordinates.
+ if this function returns true, it already knew about the char and has now drawn it.
+ if the function returns false, it's a new char, and type3D1 is called with some parameters-
+ the all draw operations until endType3Char are part of the char (which in this moment is
+ at the position first passed to beginType3Char). the char ends with endType3Char.
+
+ The drawing operations between beginType3Char and endType3Char are somewhat different to
+ the normal ones. For example, the fillcolor equals the stroke color.
+*/
+
+GBool SWFOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
+{
+ msg("<debug> beginType3Char %d, %08x, %d", code, *u, uLen);
+ type3active = 1;
+ /* the character itself is going to be passed using the draw functions */
+ return gFalse; /* gTrue= is_in_cache? */
+}
+
+void SWFOutputDev::type3D0(GfxState *state, double wx, double wy) {
+ msg("<debug> type3D0 width=%f height=%f", wx, wy);
+}
+void SWFOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury) {
+ msg("<debug> type3D1 width=%f height=%f bbox=(%f,%f,%f,%f)", wx, wy,
+ llx,lly,urx,ury);
+}
+
+void SWFOutputDev::endType3Char(GfxState *state)
+{
+ type3active = 0;
+ msg("<debug> endType3Char");
+}
+
+void SWFOutputDev::startFrame(int width, int height)
+{
+ output->startpage(output, width, height);
+}
+
+void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
+{
+ this->currentpage = pageNum;
+ double x1,y1,x2,y2;
+ int rot = doc->getPageRotate(1);
+ gfxcolor_t white;
+ laststate = state;
+ gfxline_t clippath[5];
+
+ white.r = white.g = white.b = white.a = 255;
+
+ /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
+ state->transform(state->getX2(),state->getY2(),&x2,&y2);
+ Use CropBox, not MediaBox, as page size
+ */
+
+ /*x1 = crop_x1;
+ y1 = crop_y1;
+ x2 = crop_x2;
+ y2 = crop_y2;*/
+ state->transform(crop_x1,crop_y1,&x1,&y1); //x1 += user_movex; y1 += user_movey;
+ state->transform(crop_x2,crop_y2,&x2,&y2); //x2 += user_movex; y2 += user_movey;
+
+ if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
+ if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
+
+
+ /* apply user clip box */
+ if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
+ /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
+ /*if(user_clipx2 < x2)*/ x2 = user_clipx2;
+ /*if(user_clipy1 > y1)*/ y1 = user_clipy1;
+ /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
+ }
+
+ //msg("<verbose> Bounding box is (%f,%f)-(%f,%f) [shifted by %d/%d]", x1,y1,x2,y2, user_movex, user_movey);
+
+ if(outer_clip_box) {
+ output->endclip(output);
+ outer_clip_box = 0;
+ }
+
+ msg("<notice> processing PDF page %d (%dx%d:%d:%d) (move:%d:%d)", pageNum, (int)x2-(int)x1,(int)y2-(int)y1, (int)x1, (int)y1, user_movex, user_movey);
+ if(rot!=0)
+ msg("<verbose> page is rotated %d degrees\n", rot);
+
+ clippath[0].type = gfx_moveTo;clippath[0].x = x1; clippath[0].y = y1; clippath[0].next = &clippath[1];
+ clippath[1].type = gfx_lineTo;clippath[1].x = x2; clippath[1].y = y1; clippath[1].next = &clippath[2];
+ clippath[2].type = gfx_lineTo;clippath[2].x = x2; clippath[2].y = y2; clippath[2].next = &clippath[3];
+ clippath[3].type = gfx_lineTo;clippath[3].x = x1; clippath[3].y = y2; clippath[3].next = &clippath[4];
+ clippath[4].type = gfx_lineTo;clippath[4].x = x1; clippath[4].y = y1; clippath[4].next = 0;
+ output->startclip(output, clippath); outer_clip_box = 1;
+ output->fill(output, clippath, &white);