#include "PDFDoc.h" #include "OutputDev.h" #include "GlobalParams.h" #include "GfxState.h" #include "GfxFont.h" #include #include #include static std::string UniToUT8(unsigned ch) { std::string result; const unsigned n = ch; if(n < 0x80) // <=7 bits result += (char)n; else { if(n < 0x800) // <=11 bits result += (char)(0xC0 + (n>>6)); else { if(n < 0x10000) // <=16 bits result += (char)(0xE0 + (n>>12)); else // <=21 bits { result += (char)(0xF0 + (n>>18)); result += (char)(0x80 + ((n>>12)&63)); } result += (char)(0x80 + ((n>>6)&63)); } result += (char)(0x80 + (n&63)); } return result; } #define msg(...) static std::string getFontID(GfxFont*font) { Ref*ref = font->getID(); GString*gstr = font->getName(); char* fname = gstr==0?0:gstr->getCString(); char buf[4096]; if(fname==0) { sprintf(buf, "font-%d-%d", ref->num, ref->gen); } else { sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen); } return buf; } static std::string getFontName(GfxFont*font) { GString* gstr = font->getName(); if(gstr == 0) return "NULL"; const char* fname = gstr->getCString(); const char* plus = std::strchr(fname, '+'); if(plus) return plus+1; return fname; } class OutDev: public OutputDev { private: double LastY; std::string style; void SwitchStyle(const std::string& f) { if(f == style)return; if(!style.empty()) printf(""); printf("", f.c_str()); style=f; } public: OutDev(): OutputDev() { LastY=1000; style=""; } virtual GBool upsideDown() {return gFalse;} virtual GBool useDrawChar() {return gTrue;} virtual GBool useGradients() {return gFalse;} virtual GBool interpretType3Chars() {return gFalse;} virtual GBool needNonText() { return gFalse; } virtual void updateFont(GfxState *state) { GfxFont*font = state->getFont(); if(!font) return; std::string name = getFontName(font); std::string style = "font-family:"+name; if(font->isFixedWidth()) style += ",monospace"; if(font->isSerif()) style += ",serif"; else style += ",sans-serif"; if(font->isItalic()) style += ";font-style:italic"; if(font->isBold()) style += ";text-emphasis:bold"; char Buf[4096]; sprintf(Buf, ";font-size:%gpt", state->getTransformedFontSize()); style += Buf; SwitchStyle(style); } //----- text drawing virtual void beginStringOp(GfxState *state) { msg(" beginStringOp\n"); } virtual void endStringOp(GfxState *state) { msg(" beginStringOp\n"); } virtual void beginString(GfxState *state, GString *s) { msg(" beginString '%s'\n", s->getCString()); } virtual void endString(GfxState *state) { msg(" endString\n"); std::fflush(stdout); } virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, int nBytes, Unicode *u, int uLen) { msg(" drawChar %.1f, %.1f; %.1f, %.1f; %.1f, %.1f; code=%u (%c) (%u),bytes=%d,len=%d\n", x,y, dx,dy, originX,originY, code,*u, *u, nBytes,uLen); if(y != LastY) { double diff = LastY-y; if(diff < 25) printf("
\n"); else printf("\n

\n\n"); //printf(" {diff %g}\n", diff); LastY = y; } printf("%s", UniToUT8(*u).c_str()); } virtual void drawString(GfxState *state, GString *s) { std::string str; unsigned pos = 0; for(;;) { unsigned ch = (s->getChar(pos+0) << 8) | (s->getChar(pos+1)); if(!ch) break; str += (char)ch; pos += 2; } msg(" drawString '%s'\n", str.c_str()); } virtual void startPage(int pageNum, GfxState *state, double x1,double y1,double x2,double y2) { //printf("\n\n----\n\n"); LastY=1000; std::fflush(stdout); } }; static OutDev out; int main(void) { {char p[]=""; globalParams = new GlobalParams(p);} GString tmp("/home/bisqwit/amd64-opt.pdf"); PDFDoc pdf(&tmp); std::fprintf(stderr, "%d sivua\n", pdf.getNumPages()); std::printf(""); std::printf(""); pdf.displayPages(&out, 1,pdf.getNumPages(), 72,72, 0, true, true, 0); std::fflush(stdout); std::fprintf(stderr, "done\n"); return 0; }