]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/GDKSplashOutputDev.cc
Import of xpdf code from gpdf.
[evince.git] / pdf / xpdf / GDKSplashOutputDev.cc
1 //========================================================================
2 //
3 // GDKSplashOutputDev.cc
4 //
5 // Copyright 2003 Glyph & Cog, LLC
6 // Copyright 2004 Red Hat, Inc. (GDK port)
7 //
8 //========================================================================
9
10 #include <aconf.h>
11
12 #ifdef USE_GCC_PRAGMAS
13 #pragma implementation
14 #endif
15
16 #include "gmem.h"
17 #include "SplashTypes.h"
18 #include "SplashBitmap.h"
19 #include "Object.h"
20 #include "GfxState.h"
21 #include "TextOutputDev.h"
22 #include "GDKSplashOutputDev.h"
23
24 //------------------------------------------------------------------------
25 // Constants and macros
26 //------------------------------------------------------------------------
27
28 #define xoutRound(x) ((int)(x + 0.5))
29
30 //------------------------------------------------------------------------
31 // GDKSplashOutputDev
32 //------------------------------------------------------------------------
33
34 static SplashColor makeSplashColor(int r, int g, int b)
35 {
36   SplashColor c;
37   c.rgb8 = splashMakeRGB8 (r, g, b);
38   return c;
39 }
40
41 GDKSplashOutputDev::GDKSplashOutputDev(GdkScreen *screen,
42                                        void (*redrawCbkA)(void *data),
43                                        void *redrawCbkDataA):
44   SplashOutputDev(splashModeRGB8, gFalse, makeSplashColor (255,255,255)),
45   incrementalUpdate (1)
46 {
47   redrawCbk = redrawCbkA;
48   redrawCbkData = redrawCbkDataA;
49
50   // create text object
51   text = new TextPage(gFalse);
52 }
53
54 GDKSplashOutputDev::~GDKSplashOutputDev() {
55   delete text;
56 }
57
58 void GDKSplashOutputDev::drawChar(GfxState *state, double x, double y,
59                                   double dx, double dy,
60                                   double originX, double originY,
61                                   CharCode code, Unicode *u, int uLen) {
62   text->addChar(state, x, y, dx, dy, code, u, uLen);
63   SplashOutputDev::drawChar(state, x, y, dx, dy, originX, originY,
64                             code, u, uLen);
65 }
66
67 GBool GDKSplashOutputDev::beginType3Char(GfxState *state, double x, double y,
68                                        double dx, double dy,
69                                        CharCode code, Unicode *u, int uLen) {
70   text->addChar(state, x, y, dx, dy, code, u, uLen);
71   return SplashOutputDev::beginType3Char(state, x, y, dx, dy, code, u, uLen);
72 }
73
74 void GDKSplashOutputDev::clear() {
75   startDoc(NULL);
76   startPage(0, NULL);
77 }
78
79 void GDKSplashOutputDev::startPage(int pageNum, GfxState *state) {
80   SplashOutputDev::startPage(pageNum, state);
81   text->startPage(state);
82 }
83
84 void GDKSplashOutputDev::endPage() {
85   SplashOutputDev::endPage();
86   if (!incrementalUpdate) {
87     (*redrawCbk)(redrawCbkData);
88   }
89   text->coalesce(gTrue);
90 }
91
92 void GDKSplashOutputDev::dump() {
93   if (incrementalUpdate) {
94     (*redrawCbk)(redrawCbkData);
95   }
96 }
97
98 void GDKSplashOutputDev::updateFont(GfxState *state) {
99   SplashOutputDev::updateFont(state);
100   text->updateFont(state);
101 }
102
103 void GDKSplashOutputDev::redraw(int srcX, int srcY,
104                                 GdkDrawable *drawable,
105                                 int destX, int destY,
106                                 int width, int height) {
107   SplashColorPtr dataPtr;
108   GdkGC *gc;
109   guchar *gdk_buf;
110   int gdk_rowstride;
111   int bw, x, y, r, g, b;
112
113   gdk_rowstride = width * 3;
114   
115   /* better to draw nothing than crash on a huge image */
116   gdk_buf = (guchar*) g_try_malloc (height * gdk_rowstride);
117   if (gdk_buf == NULL)
118     return;
119   
120   bw = getBitmap()->getWidth();
121   dataPtr = getBitmap()->getDataPtr();
122   
123   for (y = 0; y < height; ++y)
124     {
125       SplashRGB8 *p;
126       SplashRGB8 rgb;
127       guchar *gdk_p;
128       
129       p = dataPtr.rgb8 + (y + srcY) * bw + srcX;
130       gdk_p = gdk_buf + y * gdk_rowstride;
131       for (x = 0; x < width; ++x)
132         {
133           rgb = *p++;
134           r = splashRGB8R(rgb);
135           g = splashRGB8G(rgb);
136           b = splashRGB8B(rgb);
137
138           *gdk_p++ = r;
139           *gdk_p++ = g;
140           *gdk_p++ = b;
141         }
142     }
143   
144   gc = gdk_gc_new (drawable);
145   
146   gdk_draw_rgb_image (drawable, gc,
147                       destX, destY,
148                       width, height,
149                       GDK_RGB_DITHER_NORMAL,
150                       gdk_buf,
151                       gdk_rowstride);
152
153   g_object_unref (gc);
154
155   g_free (gdk_buf);
156 }
157
158 GBool GDKSplashOutputDev::findText(Unicode *s, int len,
159                                    GBool startAtTop, GBool stopAtBottom,
160                                    GBool startAtLast, GBool stopAtLast,
161                                    int *xMin, int *yMin,
162                                    int *xMax, int *yMax) {
163   double xMin1, yMin1, xMax1, yMax1;
164   
165   xMin1 = (double)*xMin;
166   yMin1 = (double)*yMin;
167   xMax1 = (double)*xMax;
168   yMax1 = (double)*yMax;
169   if (text->findText(s, len, startAtTop, stopAtBottom,
170                      startAtLast, stopAtLast,
171                      &xMin1, &yMin1, &xMax1, &yMax1)) {
172     *xMin = xoutRound(xMin1);
173     *xMax = xoutRound(xMax1);
174     *yMin = xoutRound(yMin1);
175     *yMax = xoutRound(yMax1);
176     return gTrue;
177   }
178   return gFalse;
179 }
180
181 GString *GDKSplashOutputDev::getText(int xMin, int yMin, int xMax, int yMax) {
182   return text->getText((double)xMin, (double)yMin,
183                        (double)xMax, (double)yMax);
184 }