]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PBMOutputDev.cc
disable font embedding hack introduced on 2002-12-09 to fix build with
[evince.git] / pdf / xpdf / PBMOutputDev.cc
1 //========================================================================
2 //
3 // PBMOutputDev.cc
4 //
5 // Copyright 1998-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include <aconf.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include "gmem.h"
19 #include "GString.h"
20 #include "Object.h"
21 #include "Stream.h"
22 #include "GfxState.h"
23 #include "GfxFont.h"
24 #include "Error.h"
25 #include "PBMOutputDev.h"
26
27 //------------------------------------------------------------------------
28
29 PBMOutputDev *PBMOutputDev::makePBMOutputDev(char *displayName,
30                                              char *fileRootA) {
31   Display *displayA;
32   Pixmap pixmapA;
33   Window dummyWinA;
34   int screenA;
35   int invertA;
36   unsigned long black, white;
37   PBMOutputDev *out;
38
39   if (!(displayA = XOpenDisplay(displayName))) {
40     fprintf(stderr, "Couldn't open display '%s'\n", displayName);
41     exit(1);
42   }
43   screenA = DefaultScreen(displayA);
44
45   black = BlackPixel(displayA, screenA);
46   white = WhitePixel(displayA, screenA);
47   if ((black & 1) == (white & 1)) {
48     fprintf(stderr, "Weird black/white pixel colors\n");
49     XCloseDisplay(displayA);
50     return NULL;
51   } 
52   invertA = (white & 1) == 1 ? 0xff : 0x00;
53
54   dummyWinA = XCreateSimpleWindow(displayA, RootWindow(displayA, screenA),
55                                   0, 0, 1, 1, 0,
56                                   black, white);
57   pixmapA = XCreatePixmap(displayA, dummyWinA, 1, 1, 1);
58   out = new PBMOutputDev(displayA, screenA, pixmapA, dummyWinA,
59                          invertA, fileRootA);
60   return out;
61 }
62
63 void PBMOutputDev::killPBMOutputDev(PBMOutputDev *out) {
64   Display *displayA;
65   Pixmap pixmapA;
66   Window dummyWinA;
67
68   displayA = out->display;
69   pixmapA = out->pixmap;
70   dummyWinA = out->dummyWin;
71
72   delete out;
73
74   // these have to be done *after* the XOutputDev (parent of the
75   // PBMOutputDev) is deleted, since XOutputDev::~XOutputDev() needs
76   // them
77   XFreePixmap(displayA, pixmapA);
78   XDestroyWindow(displayA, dummyWinA);
79   XCloseDisplay(displayA);
80 }
81
82 PBMOutputDev::PBMOutputDev(Display *displayA, int screenA,
83                            Pixmap pixmapA, Window dummyWinA,
84                            int invertA, char *fileRootA):
85   XOutputDev(displayA, pixmapA, 1,
86              DefaultColormap(displayA, screenA), gFalse,
87              WhitePixel(displayA, DefaultScreen(displayA)),
88              gFalse, 1)
89 {
90   display = displayA;
91   screen = screenA;
92   pixmap = pixmapA;
93   dummyWin = dummyWinA;
94   invert = invertA;
95   fileRoot = fileRootA;
96   fileName = (char *)gmalloc(strlen(fileRoot) + 20);
97 }
98
99 PBMOutputDev::~PBMOutputDev() {
100   gfree(fileName);
101 }
102
103 void PBMOutputDev::startPage(int pageNum, GfxState *state) {
104   curPage = pageNum;
105   width = (int)(state->getPageWidth() + 0.5);
106   height = (int)(state->getPageHeight() + 0.5);
107   XFreePixmap(display, pixmap);
108   pixmap = XCreatePixmap(display, dummyWin, width, height, 1);
109   setPixmap(pixmap, width, height);
110   XOutputDev::startPage(pageNum, state);
111 }
112
113 void PBMOutputDev::endPage() {
114   XImage *image;
115   FILE *f;
116   int p;
117   int x, y, i;
118
119   image = XCreateImage(display, DefaultVisual(display, screen),
120                        1, ZPixmap, 0, NULL, width, height, 8, 0);
121   image->data = (char *)gmalloc(height * image->bytes_per_line);
122   XGetSubImage(display, pixmap, 0, 0, width, height, 1, ZPixmap,
123                image, 0, 0);
124
125   sprintf(fileName, "%s-%06d.pbm", fileRoot, curPage);
126   if (!(f = fopen(fileName, "wb"))) {
127     fprintf(stderr, "Couldn't open output file '%s'\n", fileName);
128     goto err;
129   }
130   fprintf(f, "P4\n");
131   fprintf(f, "%d %d\n", width, height);
132
133   for (y = 0; y < height; ++y) {
134     for (x = 0; x+8 <= width; x += 8) {
135       p = 0;
136       for (i = 0; i < 8; ++i)
137         p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
138       p ^= invert;
139       fputc((char)p, f);
140     }
141     if (width & 7) {
142       p = 0;
143       for (i = 0; i < (width & 7); ++i)
144         p = (p << 1) + (XGetPixel(image, x+i, y) & 1);
145       p <<= 8 - (width & 7);
146       p ^= invert;
147       fputc((char)p, f);
148     }
149   }
150
151   fclose(f);
152
153  err:
154   gfree(image->data);
155   image->data = NULL;
156   XDestroyImage(image);
157
158   XOutputDev::endPage();
159 }