Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pdf/xpdf/XSplashOutputDev.cc
blob: 3e2bfc46b133484df4acc320f4603ba70e708849 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//========================================================================
//
// XSplashOutputDev.cc
//
// Copyright 2003 Glyph & Cog, LLC
//
//========================================================================

#include <aconf.h>

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "gmem.h"
#include "SplashTypes.h"
#include "SplashBitmap.h"
#include "Object.h"
#include "GfxState.h"
#include "TextOutputDev.h"
#include "XSplashOutputDev.h"

//------------------------------------------------------------------------
// Constants and macros
//------------------------------------------------------------------------

#define xoutRound(x) ((int)(x + 0.5))

//------------------------------------------------------------------------
// XSplashOutputDev
//------------------------------------------------------------------------

XSplashOutputDev::XSplashOutputDev(Display *displayA, int screenNumA,
				   Visual *visualA, Colormap colormapA,
				   GBool reverseVideoA,
				   SplashColor paperColorA,
				   GBool installCmapA, int rgbCubeSizeA,
				   GBool incrementalUpdateA,
				   void (*redrawCbkA)(void *data),
				   void *redrawCbkDataA):
  SplashOutputDev(splashModeRGB8, reverseVideoA, paperColorA)
{
  XVisualInfo visualTempl;
  XVisualInfo *visualList;
  Gulong mask;
  int nVisuals;
  XColor xcolor;
  XColor *xcolors;
  int r, g, b, n, m;
  GBool ok;

  incrementalUpdate = incrementalUpdateA;
  redrawCbk = redrawCbkA;
  redrawCbkData = redrawCbkDataA;

  // create text object
  text = new TextPage(gFalse);

  //----- set up the X color stuff

  display = displayA;
  visual = visualA;

  // check for TrueColor visual
  //~ this should scan the list, not just look at the first one
  visualTempl.visualid = XVisualIDFromVisual(visual);
  visualList = XGetVisualInfo(display, VisualIDMask,
			      &visualTempl, &nVisuals);
  if (nVisuals < 1) {
    // this shouldn't happen
    XFree((XPointer)visualList);
    visualList = XGetVisualInfo(display, VisualNoMask, &visualTempl,
				&nVisuals);
  }
  depth = visualList->depth;
  if (visualList->c_class == TrueColor) {
    trueColor = gTrue;
    for (mask = visualList->red_mask, rShift = 0;
	 mask && !(mask & 1);
	 mask >>= 1, ++rShift) ;
    for (rDiv = 8; mask; mask >>= 1, --rDiv) ;
    for (mask = visualList->green_mask, gShift = 0;
	 mask && !(mask & 1);
	 mask >>= 1, ++gShift) ;
    for (gDiv = 8; mask; mask >>= 1, --gDiv) ;
    for (mask = visualList->blue_mask, bShift = 0;
	 mask && !(mask & 1);
	 mask >>= 1, ++bShift) ;
    for (bDiv = 8; mask; mask >>= 1, --bDiv) ;
  } else {
    trueColor = gFalse;
  }
  XFree((XPointer)visualList);

  // allocate a color cube
  if (!trueColor) {

    // set colors in private colormap
    if (installCmapA) {
      for (rgbCubeSize = xOutMaxRGBCube; rgbCubeSize >= 2; --rgbCubeSize) {
	m = rgbCubeSize * rgbCubeSize * rgbCubeSize;
	if (XAllocColorCells(display, colormapA, False, NULL, 0, colors, m)) {
	  break;
	}
      }
      if (rgbCubeSize >= 2) {
	m = rgbCubeSize * rgbCubeSize * rgbCubeSize;
	xcolors = (XColor *)gmalloc(m * sizeof(XColor));
	n = 0;
	for (r = 0; r < rgbCubeSize; ++r) {
	  for (g = 0; g < rgbCubeSize; ++g) {
	    for (b = 0; b < rgbCubeSize; ++b) {
	      xcolors[n].pixel = colors[n];
	      xcolors[n].red = (r * 65535) / (rgbCubeSize - 1);
	      xcolors[n].green = (g * 65535) / (rgbCubeSize - 1);
	      xcolors[n].blue = (b * 65535) / (rgbCubeSize - 1);
	      xcolors[n].flags = DoRed | DoGreen | DoBlue;
	      ++n;
	    }
	  }
	}
	XStoreColors(display, colormapA, xcolors, m);
	gfree(xcolors);
      } else {
	rgbCubeSize = 1;
	colors[0] = BlackPixel(display, screenNumA);
	colors[1] = WhitePixel(display, screenNumA);
      }

    // allocate colors in shared colormap
    } else {
      if (rgbCubeSize > xOutMaxRGBCube) {
	rgbCubeSize = xOutMaxRGBCube;
      }
      ok = gFalse;
      for (rgbCubeSize = rgbCubeSizeA; rgbCubeSize >= 2; --rgbCubeSize) {
	ok = gTrue;
	n = 0;
	for (r = 0; r < rgbCubeSize && ok; ++r) {
	  for (g = 0; g < rgbCubeSize && ok; ++g) {
	    for (b = 0; b < rgbCubeSize && ok; ++b) {
	      if (n == 0) {
		colors[n] = BlackPixel(display, screenNumA);
		++n;
	      } else {
		xcolor.red = (r * 65535) / (rgbCubeSize - 1);
		xcolor.green = (g * 65535) / (rgbCubeSize - 1);
		xcolor.blue = (b * 65535) / (rgbCubeSize - 1);
		if (XAllocColor(display, colormapA, &xcolor)) {
		  colors[n++] = xcolor.pixel;
		} else {
		  ok = gFalse;
		}
	      }
	    }
	  }
	}
	if (ok) {
	  break;
	}
	XFreeColors(display, colormapA, &colors[1], n-1, 0);
      }
      if (!ok) {
	rgbCubeSize = 1;
	colors[0] = BlackPixel(display, screenNumA);
	colors[1] = WhitePixel(display, screenNumA);
      }
    }
  }
}

XSplashOutputDev::~XSplashOutputDev() {
  delete text;
}

void XSplashOutputDev::drawChar(GfxState *state, double x, double y,
				double dx, double dy,
				double originX, double originY,
				CharCode code, Unicode *u, int uLen) {
  text->addChar(state, x, y, dx, dy, code, u, uLen);
  SplashOutputDev::drawChar(state, x, y, dx, dy, originX, originY,
			    code, u, uLen);
}

GBool XSplashOutputDev::beginType3Char(GfxState *state, double x, double y,
				       double dx, double dy,
				       CharCode code, Unicode *u, int uLen) {
  text->addChar(state, x, y, dx, dy, code, u, uLen);
  return SplashOutputDev::beginType3Char(state, x, y, dx, dy, code, u, uLen);
}

void XSplashOutputDev::clear() {
  startDoc(NULL);
  startPage(0, NULL);
}

void XSplashOutputDev::startPage(int pageNum, GfxState *state) {
  SplashOutputDev::startPage(pageNum, state);
  text->startPage(state);
}

void XSplashOutputDev::endPage() {
  SplashOutputDev::endPage();
  if (!incrementalUpdate) {
    (*redrawCbk)(redrawCbkData);
  }
  text->coalesce(gTrue);
}

void XSplashOutputDev::dump() {
  if (incrementalUpdate) {
    (*redrawCbk)(redrawCbkData);
  }
}

void XSplashOutputDev::updateFont(GfxState *state) {
  SplashOutputDev::updateFont(state);
  text->updateFont(state);
}

void XSplashOutputDev::redraw(int srcX, int srcY,
			      Drawable destDrawable, GC destGC,
			      int destX, int destY,
			      int width, int height) {
  XImage *image;
  SplashColorPtr dataPtr;
  SplashRGB8 *p;
  SplashRGB8 rgb;
  Gulong pixel;
  int bw, x, y, r, g, b, gray;

  //~ allocate this image once (whenever the window changes size)
  //~ use XShm
  image = XCreateImage(display, visual, depth, ZPixmap, 0, NULL,
		       width, height, 8, 0);
  image->data = (char *)gmalloc(height * image->bytes_per_line);

  //~ optimize for known XImage formats
  bw = getBitmap()->getWidth();
  dataPtr = getBitmap()->getDataPtr();

  if (trueColor) {
    for (y = 0; y < height; ++y) {
      p = dataPtr.rgb8 + (y + srcY) * bw + srcX;
      for (x = 0; x < width; ++x) {
	rgb = *p++;
	r = splashRGB8R(rgb) >> rDiv;
	g = splashRGB8G(rgb) >> gDiv;
	b = splashRGB8B(rgb) >> bDiv;
	pixel = ((Gulong)r << rShift) +
	        ((Gulong)g << gShift) +
	        ((Gulong)b << bShift);
	XPutPixel(image, x, y, pixel);
      }
    }
  } else if (rgbCubeSize == 1) {
    //~ this should really use splashModeMono, with non-clustered dithering
    for (y = 0; y < height; ++y) {
      p = dataPtr.rgb8 + (y + srcY) * bw + srcX;
      for (x = 0; x < width; ++x) {
	rgb = *p++;
	gray = xoutRound(0.299 * splashRGB8R(rgb) +
			 0.587 * splashRGB8G(rgb) +
			 0.114 * splashRGB8B(rgb));
	if (gray < 128) {
	  pixel = colors[0];
	} else {
	  pixel = colors[1];
	}
	XPutPixel(image, x, y, pixel);
      }
    }
  } else {
    for (y = 0; y < height; ++y) {
      p = dataPtr.rgb8 + (y + srcY) * bw + srcX;
      for (x = 0; x < width; ++x) {
	rgb = *p++;
	r = (splashRGB8R(rgb) * (rgbCubeSize - 1)) / 255;
	g = (splashRGB8G(rgb) * (rgbCubeSize - 1)) / 255;
	b = (splashRGB8B(rgb) * (rgbCubeSize - 1)) / 255;
	pixel = colors[(r * rgbCubeSize + g) * rgbCubeSize + b];
	XPutPixel(image, x, y, pixel);
      }
    }
  }

  XPutImage(display, destDrawable, destGC, image,
	    0, 0, destX, destY, width, height);

  gfree(image->data);
  image->data = NULL;
  XDestroyImage(image);
}

GBool XSplashOutputDev::findText(Unicode *s, int len,
				 GBool startAtTop, GBool stopAtBottom,
				 GBool startAtLast, GBool stopAtLast,
				 int *xMin, int *yMin,
				 int *xMax, int *yMax) {
  double xMin1, yMin1, xMax1, yMax1;
  
  xMin1 = (double)*xMin;
  yMin1 = (double)*yMin;
  xMax1 = (double)*xMax;
  yMax1 = (double)*yMax;
  if (text->findText(s, len, startAtTop, stopAtBottom,
		     startAtLast, stopAtLast,
		     &xMin1, &yMin1, &xMax1, &yMax1)) {
    *xMin = xoutRound(xMin1);
    *xMax = xoutRound(xMax1);
    *yMin = xoutRound(yMin1);
    *yMax = xoutRound(yMax1);
    return gTrue;
  }
  return gFalse;
}

GString *XSplashOutputDev::getText(int xMin, int yMin, int xMax, int yMax) {
  return text->getText((double)xMin, (double)yMin,
		       (double)xMax, (double)yMax);
}