]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/XOutputDev.h
copy from ggv.
[evince.git] / pdf / xpdf / XOutputDev.h
1 //========================================================================
2 //
3 // XOutputDev.h
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef XOUTPUTDEV_H
10 #define XOUTPUTDEV_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include <stddef.h>
17 #include <X11/Xlib.h>
18 #include <X11/Xutil.h>
19 #include "config.h"
20 #include "Object.h"
21 #include "CharTypes.h"
22 #include "GlobalParams.h"
23 #include "OutputDev.h"
24
25 class GString;
26 class GList;
27 struct GfxRGB;
28 class GfxFont;
29 class GfxSubpath;
30 class TextPage;
31 class XOutputFontCache;
32 struct T3FontCacheTag;
33 class T3FontCache;
34 struct T3GlyphStack;
35 class XOutputDev;
36 class Link;
37 class Catalog;
38 class DisplayFontParam;
39 class UnicodeMap;
40 class CharCodeToUnicode;
41
42 #if HAVE_T1LIB_H
43 class T1FontEngine;
44 class T1FontFile;
45 class T1Font;
46 #endif
47
48 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
49 class FTFontEngine;
50 class FTFontFile;
51 class FTFont;
52 #endif
53
54 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
55 class TTFontEngine;
56 class TTFontFile;
57 class TTFont;
58 #endif
59
60 //------------------------------------------------------------------------
61 // Constants
62 //------------------------------------------------------------------------
63
64 #define maxRGBCube 7            // max size of RGB color cube
65
66 #define numTmpPoints 256        // number of XPoints in temporary array
67 #define numTmpSubpaths 16       // number of elements in temporary arrays
68                                 //   for fill/clip
69
70 //------------------------------------------------------------------------
71 // Misc types
72 //------------------------------------------------------------------------
73
74 struct BoundingRect {
75   short xMin, xMax;             // min/max x values
76   short yMin, yMax;             // min/max y values
77 };
78
79 //------------------------------------------------------------------------
80 // XOutputFont
81 //------------------------------------------------------------------------
82
83 class XOutputFont {
84 public:
85
86   XOutputFont(Ref *idA, double m11OrigA, double m12OrigA,
87               double m21OrigA, double m22OrigA,
88               double m11A, double m12A, double m21A, double m22A,
89               Display *displayA, XOutputDev *xOutA);
90
91   virtual ~XOutputFont();
92
93   // Does this font match the ID and transform?
94   GBool matches(Ref *idA, double m11OrigA, double m12OrigA,
95                 double m21OrigA, double m22OrigA)
96     { return id.num == idA->num && id.gen == idA->gen &&
97              m11Orig == m11OrigA && m12Orig == m12OrigA &&
98              m21Orig == m21OrigA && m22Orig == m22OrigA; }
99
100   // Was font created successfully?
101   virtual GBool isOk() = 0;
102
103   // Update <gc> with this font.
104   virtual void updateGC(GC gc) = 0;
105
106   // Draw character <c>/<u> at <x>,<y> (in device space).
107   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
108                         GC gc, GfxRGB *rgb,
109                         double x, double y, double dx, double dy,
110                         CharCode c, Unicode *u, int uLen) = 0;
111
112   // Returns true if this XOutputFont subclass provides the
113   // getCharPath function.
114   virtual GBool hasGetCharPath() { return gFalse; }
115
116   // Add the character outline for <c>/<u> to the current path.
117   virtual void getCharPath(GfxState *state,
118                            CharCode c, Unicode *u, int ulen);
119
120 protected:
121
122   Ref id;                       // font ID
123   double m11Orig, m12Orig,      // original transform matrix
124          m21Orig, m22Orig;
125   double m11, m12, m21, m22;    // actual transform matrix (possibly
126                                 //   modified for font substitution)
127   Display *display;             // X display
128   XOutputDev *xOut;
129 };
130
131 #if HAVE_T1LIB_H
132 //------------------------------------------------------------------------
133 // XOutputT1Font
134 //------------------------------------------------------------------------
135
136 class XOutputT1Font: public XOutputFont {
137 public:
138
139   XOutputT1Font(Ref *idA, T1FontFile *fontFileA,
140                 double m11OrigA, double m12OrigA,
141                 double m21OrigA, double m22OrigA,
142                 double m11A, double m12A,
143                 double m21A, double m22A,
144                 Display *displayA, XOutputDev *xOutA);
145
146   virtual ~XOutputT1Font();
147
148   // Was font created successfully?
149   virtual GBool isOk();
150
151   // Update <gc> with this font.
152   virtual void updateGC(GC gc);
153
154   // Draw character <c>/<u> at <x>,<y>.
155   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
156                         GC gc, GfxRGB *rgb,
157                         double x, double y, double dx, double dy,
158                         CharCode c, Unicode *u, int uLen);
159
160   // Returns true if this XOutputFont subclass provides the
161   // getCharPath function.
162   virtual GBool hasGetCharPath() { return gTrue; }
163
164   // Add the character outline for <c>/<u> to the current path.
165   virtual void getCharPath(GfxState *state,
166                            CharCode c, Unicode *u, int ulen);
167
168 private:
169
170   T1FontFile *fontFile;
171   T1Font *font;
172 };
173 #endif // HAVE_T1LIB_H
174
175 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
176 //------------------------------------------------------------------------
177 // XOutputFTFont
178 //------------------------------------------------------------------------
179
180 class XOutputFTFont: public XOutputFont {
181 public:
182
183   XOutputFTFont(Ref *idA, FTFontFile *fontFileA,
184                 double m11OrigA, double m12OrigA,
185                 double m21OrigA, double m22OrigA,
186                 double m11A, double m12A,
187                 double m21A, double m22A,
188                 Display *displayA, XOutputDev *xOutA);
189
190   virtual ~XOutputFTFont();
191
192   // Was font created successfully?
193   virtual GBool isOk();
194
195   // Update <gc> with this font.
196   virtual void updateGC(GC gc);
197
198   // Draw character <c>/<u> at <x>,<y>.
199   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
200                         GC gc, GfxRGB *rgb,
201                         double x, double y, double dx, double dy,
202                         CharCode c, Unicode *u, int uLen);
203
204   // Returns true if this XOutputFont subclass provides the
205   // getCharPath function.
206   virtual GBool hasGetCharPath() { return gTrue; }
207
208   // Add the character outline for <c>/<u> to the current path.
209   virtual void getCharPath(GfxState *state,
210                            CharCode c, Unicode *u, int ulen);
211
212 private:
213
214   FTFontFile *fontFile;
215   FTFont *font;
216 };
217 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
218
219 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
220 //------------------------------------------------------------------------
221 // XOutputTTFont
222 //------------------------------------------------------------------------
223
224 class XOutputTTFont: public XOutputFont {
225 public:
226
227   XOutputTTFont(Ref *idA, TTFontFile *fontFileA,
228                 double m11OrigA, double m12OrigA,
229                 double m21OrigA, double m22OrigA,
230                 double m11A, double m12A,
231                 double m21A, double m22A,
232                 Display *displayA, XOutputDev *xOutA);
233
234   virtual ~XOutputTTFont();
235
236   // Was font created successfully?
237   virtual GBool isOk();
238
239   // Update <gc> with this font.
240   virtual void updateGC(GC gc);
241
242   // Draw character <c>/<u> at <x>,<y>.
243   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
244                         GC gc, GfxRGB *rgb,
245                         double x, double y, double dx, double dy,
246                         CharCode c, Unicode *u, int uLen);
247
248 private:
249
250   TTFontFile *fontFile;
251   TTFont *font;
252 };
253 #endif // !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
254
255 //------------------------------------------------------------------------
256 // XOutputServer8BitFont
257 //------------------------------------------------------------------------
258
259 class XOutputServer8BitFont: public XOutputFont {
260 public:
261
262   XOutputServer8BitFont(Ref *idA, GString *xlfdFmt,
263                         UnicodeMap *xUMapA, CharCodeToUnicode *fontUMap,
264                         double m11OrigA, double m12OrigA,
265                         double m21OrigA, double m22OrigA,
266                         double m11A, double m12A, double m21A, double m22A,
267                         Display *displayA, XOutputDev *xOutA);
268
269   virtual ~XOutputServer8BitFont();
270
271   // Was font created successfully?
272   virtual GBool isOk();
273
274   // Update <gc> with this font.
275   virtual void updateGC(GC gc);
276
277   // Draw character <c>/<u> at <x>,<y>.
278   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
279                         GC gc, GfxRGB *rgb,
280                         double x, double y, double dx, double dy,
281                         CharCode c, Unicode *u, int uLen);
282
283 private:
284
285   XFontStruct *xFont;           // the X font
286   Gushort map[256];             // forward map (char code -> X font code)
287   UnicodeMap *xUMap;
288 };
289
290 //------------------------------------------------------------------------
291 // XOutputServer16BitFont
292 //------------------------------------------------------------------------
293
294 class XOutputServer16BitFont: public XOutputFont {
295 public:
296
297   XOutputServer16BitFont(Ref *idA, GString *xlfdFmt,
298                          UnicodeMap *xUMapA, CharCodeToUnicode *fontUMap,
299                          double m11OrigA, double m12OrigA,
300                          double m21OrigA, double m22OrigA,
301                          double m11A, double m12A, double m21A, double m22A,
302                          Display *displayA, XOutputDev *xOutA);
303
304   virtual ~XOutputServer16BitFont();
305
306   // Was font created successfully?
307   virtual GBool isOk();
308
309   // Update <gc> with this font.
310   virtual void updateGC(GC gc);
311
312   // Draw character <c>/<u> at <x>,<y>.
313   virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
314                         GC gc, GfxRGB *rgb,
315                         double x, double y, double dx, double dy,
316                         CharCode c, Unicode *u, int uLen);
317
318 private:
319
320   XFontStruct *xFont;           // the X font
321   UnicodeMap *xUMap;
322 };
323
324 //------------------------------------------------------------------------
325 // XOutputFontCache
326 //------------------------------------------------------------------------
327
328 #if HAVE_T1LIB_H
329 class XOutputT1FontFile {
330 public:
331   XOutputT1FontFile(int numA, int genA, GBool substA, T1FontFile *fontFileA)
332     { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
333   ~XOutputT1FontFile();
334   int num, gen;
335   GBool subst;
336   T1FontFile *fontFile;
337 };
338 #endif
339
340 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
341 class XOutputFTFontFile {
342 public:
343   XOutputFTFontFile(int numA, int genA, GBool substA, FTFontFile *fontFileA)
344     { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
345   ~XOutputFTFontFile();
346   int num, gen;
347   GBool subst;
348   FTFontFile *fontFile;
349 };
350 #endif
351
352 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
353 class XOutputTTFontFile {
354 public:
355   XOutputTTFontFile(int numA, int genA, GBool substA, TTFontFile *fontFileA)
356     { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
357   ~XOutputTTFontFile();
358   int num, gen;
359   GBool subst;
360   TTFontFile *fontFile;
361 };
362 #endif
363
364 class XOutputFontCache {
365 public:
366
367   // Constructor.
368   XOutputFontCache(Display *displayA, Guint depthA,
369                    XOutputDev *xOutA,
370                    FontRastControl t1libControlA,
371                    FontRastControl freetypeControlA);
372
373   // Destructor.
374   ~XOutputFontCache();
375
376   // Initialize (or re-initialize) the font cache for a new document.
377   void startDoc(int screenNum, Colormap colormap,
378                 GBool trueColor,
379                 int rMul, int gMul, int bMul,
380                 int rShift, int gShift, int bShift,
381                 Gulong *colors, int numColors);
382
383   // Get a font.  This creates a new font if necessary.
384   XOutputFont *getFont(XRef *xref, GfxFont *gfxFont, double m11, double m12,
385                        double m21, double m22);
386
387 private:
388
389   void delFonts();
390   void clear();
391   XOutputFont *tryGetFont(XRef *xref, DisplayFontParam *dfp, GfxFont *gfxFont,
392                           double m11Orig, double m12Orig,
393                           double m21Orig, double m22Orig,
394                           double m11, double m12, double m21, double m22,
395                           GBool subst);
396 #if HAVE_T1LIB_H
397   XOutputFont *tryGetT1Font(XRef *xref, GfxFont *gfxFont,
398                             double m11, double m12, double m21, double m22);
399   XOutputFont *tryGetT1FontFromFile(XRef *xref, GString *fileName,
400                                     GfxFont *gfxFont,
401                                     double m11Orig, double m12Orig,
402                                     double m21Orig, double m22Orig,
403                                     double m11, double m12,
404                                     double m21, double m22, GBool subst);
405 #endif
406 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
407   XOutputFont *tryGetFTFont(XRef *xref, GfxFont *gfxFont,
408                             double m11, double m12, double m21, double m22);
409   XOutputFont *tryGetFTFontFromFile(XRef *xref, GString *fileName,
410                                     GfxFont *gfxFont,
411                                     double m11Orig, double m12Orig,
412                                     double m21Orig, double m22Orig,
413                                     double m11, double m12,
414                                     double m21, double m22, GBool subst);
415 #endif
416 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
417   XOutputFont *tryGetTTFont(XRef *xref, GfxFont *gfxFont,
418                             double m11, double m12, double m21, double m22);
419   XOutputFont *tryGetTTFontFromFile(XRef *xref, GString *fileName,
420                                     GfxFont *gfxFont,
421                                     double m11Orig, double m12Orig,
422                                     double m21Orig, double m22Orig,
423                                     double m11, double m12,
424                                     double m21, double m22, GBool subst);
425 #endif
426   XOutputFont *tryGetServerFont(GString *xlfd, GString *encodingName,
427                                 GfxFont *gfxFont,
428                                 double m11Orig, double m12Orig,
429                                 double m21Orig, double m22Orig,
430                                 double m11, double m12,
431                                 double m21, double m22);
432
433   Display *display;             // X display pointer
434   XOutputDev *xOut;
435   Guint depth;                  // pixmap depth
436
437   XOutputFont *
438     fonts[xOutFontCacheSize];
439   int nFonts;
440
441 #if HAVE_T1LIB_H
442   FontRastControl t1libControl; // t1lib settings
443   T1FontEngine *t1Engine;       // Type 1 font engine
444   GList *t1FontFiles;           // list of Type 1 font files
445                                 //   [XOutputT1FontFile]
446 #endif
447
448 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
449   FontRastControl               // FreeType settings
450     freetypeControl;
451 #endif
452 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
453   FTFontEngine *ftEngine;       // FreeType font engine
454   GList *ftFontFiles;           // list of FreeType font files
455                                 //   [XOutputFTFontFile]
456 #endif
457 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
458   TTFontEngine *ttEngine;       // TrueType font engine
459   GList *ttFontFiles;           // list of TrueType font files
460                                 //   [XOutputTTFontFile]
461 #endif
462 };
463
464 //------------------------------------------------------------------------
465 // XOutputState
466 //------------------------------------------------------------------------
467
468 struct XOutputState {
469   GC strokeGC;
470   GC fillGC;
471   Region clipRegion;
472   XOutputState *next;
473 };
474
475 //------------------------------------------------------------------------
476 // XOutputDev
477 //------------------------------------------------------------------------
478
479 class XOutputDev: public OutputDev {
480 public:
481
482   // Constructor.
483   XOutputDev(Display *displayA, Pixmap pixmapA, Guint depthA,
484              Colormap colormapA, GBool reverseVideoA,
485              unsigned long paperColor, GBool installCmap,
486              int rgbCubeSize);
487
488   // Destructor.
489   virtual ~XOutputDev();
490
491   //---- get info about output device
492
493   // Does this device use upside-down coordinates?
494   // (Upside-down means (0,0) is the top left corner of the page.)
495   virtual GBool upsideDown() { return gTrue; }
496
497   // Does this device use drawChar() or drawString()?
498   virtual GBool useDrawChar() { return gTrue; }
499
500   // Does this device use beginType3Char/endType3Char?  Otherwise,
501   // text in Type 3 fonts will be drawn with drawChar/drawString.
502   virtual GBool interpretType3Chars() { return gTrue; }
503
504   //----- initialization and control
505
506   // Start a page.
507   virtual void startPage(int pageNum, GfxState *state);
508
509   // End a page.
510   virtual void endPage();
511
512   //----- link borders
513   virtual void drawLink(Link *link, Catalog *catalog);
514
515   //----- save/restore graphics state
516   virtual void saveState(GfxState *state);
517   virtual void restoreState(GfxState *state);
518
519   //----- update graphics state
520   virtual void updateAll(GfxState *state);
521   virtual void updateCTM(GfxState *state, double m11, double m12,
522                          double m21, double m22, double m31, double m32);
523   virtual void updateLineDash(GfxState *state);
524   virtual void updateFlatness(GfxState *state);
525   virtual void updateLineJoin(GfxState *state);
526   virtual void updateLineCap(GfxState *state);
527   virtual void updateMiterLimit(GfxState *state);
528   virtual void updateLineWidth(GfxState *state);
529   virtual void updateFillColor(GfxState *state);
530   virtual void updateStrokeColor(GfxState *state);
531
532   //----- update text state
533   virtual void updateFont(GfxState *state);
534
535   //----- path painting
536   virtual void stroke(GfxState *state);
537   virtual void fill(GfxState *state);
538   virtual void eoFill(GfxState *state);
539
540   //----- path clipping
541   virtual void clip(GfxState *state);
542   virtual void eoClip(GfxState *state);
543
544   //----- text drawing
545   virtual void beginString(GfxState *state, GString *s);
546   virtual void endString(GfxState *state);
547   virtual void drawChar(GfxState *state, double x, double y,
548                         double dx, double dy,
549                         double originX, double originY,
550                         CharCode code, Unicode *u, int uLen);
551   virtual GBool beginType3Char(GfxState *state,
552                                CharCode code, Unicode *u, int uLen);
553   virtual void endType3Char(GfxState *state);
554
555   //----- image drawing
556   virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
557                              int width, int height, GBool invert,
558                              GBool inlineImg);
559   virtual void drawImage(GfxState *state, Object *ref, Stream *str,
560                          int width, int height, GfxImageColorMap *colorMap,
561                          int *maskColors, GBool inlineImg);
562
563   //----- Type 3 font operators
564   virtual void type3D0(GfxState *state, double wx, double wy);
565   virtual void type3D1(GfxState *state, double wx, double wy,
566                        double llx, double lly, double urx, double ury);
567
568   //----- special access
569
570   // Called to indicate that a new PDF document has been loaded.
571   void startDoc(XRef *xrefA);
572
573   // Find a string.  If <top> is true, starts looking at <xMin>,<yMin>;
574   // otherwise starts looking at top of page.  If <bottom> is true,
575   // stops looking at <xMax>,<yMax>; otherwise stops looking at bottom
576   // of page.  If found, sets the text bounding rectange and returns
577   // true; otherwise returns false.
578   GBool findText(Unicode *s, int len, GBool top, GBool bottom,
579                  int *xMin, int *yMin, int *xMax, int *yMax);
580
581   // Get the text which is inside the specified rectangle.
582   GString *getText(int xMin, int yMin, int xMax, int yMax);
583
584   GBool isReverseVideo() { return reverseVideo; }
585
586 protected:
587
588   // Update pixmap ID after a page change.
589   void setPixmap(Pixmap pixmap1, int pixmapW1, int pixmapH1)
590     { pixmap = pixmap1; pixmapW = pixmapW1; pixmapH = pixmapH1; }
591
592 private:
593
594   XRef *xref;                   // the xref table for this PDF file
595   Display *display;             // X display pointer
596   int screenNum;                // X screen number
597   Pixmap pixmap;                // pixmap to draw into
598   int pixmapW, pixmapH;         // size of pixmap
599   Guint depth;                  // pixmap depth
600   Colormap colormap;            // X colormap
601   int flatness;                 // line flatness
602   GC paperGC;                   // GC for background
603   GC strokeGC;                  // GC with stroke color
604   GC fillGC;                    // GC with fill color
605   Region clipRegion;            // clipping region
606   GBool trueColor;              // set if using a TrueColor visual
607   int rMul, gMul, bMul;         // RGB multipliers (for TrueColor)
608   int rShift, gShift, bShift;   // RGB shifts (for TrueColor)
609   Gulong                        // color cube
610     colors[maxRGBCube * maxRGBCube * maxRGBCube];
611   int numColors;                // size of color cube
612   double redMap[256];           // map pixel (from color cube) to red value
613   GBool reverseVideo;           // reverse video mode
614   XPoint                        // temporary points array
615     tmpPoints[numTmpPoints];
616   int                           // temporary arrays for fill/clip
617     tmpLengths[numTmpSubpaths];
618   BoundingRect
619     tmpRects[numTmpSubpaths];
620   GfxFont *gfxFont;             // current PDF font
621   XOutputFont *font;            // current font
622   XOutputFontCache *fontCache;  // font cache
623   T3FontCache *                 // Type 3 font cache
624     t3FontCache[xOutT3FontCacheSize];
625   int nT3Fonts;                 // number of valid entries in t3FontCache
626   T3GlyphStack *t3GlyphStack;   // Type 3 glyph context stack
627   XOutputState *save;           // stack of saved states
628
629   TextPage *text;               // text from the current page
630
631   void updateLineAttrs(GfxState *state, GBool updateDash);
632   void doFill(GfxState *state, int rule);
633   void doClip(GfxState *state, int rule);
634   int convertPath(GfxState *state, XPoint **points, int *size,
635                   int *numPoints, int **lengths, GBool fillHack);
636   void convertSubpath(GfxState *state, GfxSubpath *subpath,
637                       XPoint **points, int *size, int *n);
638   void doCurve(XPoint **points, int *size, int *k,
639                double x0, double y0, double x1, double y1,
640                double x2, double y2, double x3, double y3);
641   void addPoint(XPoint **points, int *size, int *k, int x, int y);
642   void drawType3Glyph(T3FontCache *t3Font,
643                       T3FontCacheTag *tag, Guchar *data,
644                       double x, double y, GfxRGB *color);
645   Gulong findColor(GfxRGB *rgb);
646   Gulong findColor(GfxRGB *x, GfxRGB *err);
647 };
648
649 #endif