filters

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