1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
20 #include <X11/Xutil.h>
23 #include "CharTypes.h"
24 #include "GlobalParams.h"
25 #include "OutputDev.h"
33 class XOutputFontCache;
34 struct T3FontCacheTag;
40 class DisplayFontParam;
42 class CharCodeToUnicode;
50 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
56 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
62 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
66 #define maxRGBCube 7 // max size of RGB color cube
68 #define numTmpPoints 256 // number of XPoints in temporary array
69 #define numTmpSubpaths 16 // number of elements in temporary arrays
72 //------------------------------------------------------------------------
74 //------------------------------------------------------------------------
77 short xMin, xMax; // min/max x values
78 short yMin, yMax; // min/max y values
81 //------------------------------------------------------------------------
83 //------------------------------------------------------------------------
88 XOutputFont(Ref *idA, double m11OrigA, double m12OrigA,
89 double m21OrigA, double m22OrigA,
90 double m11A, double m12A, double m21A, double m22A,
91 Display *displayA, XOutputDev *xOutA);
93 virtual ~XOutputFont();
95 // Does this font match the ID and transform?
96 GBool matches(Ref *idA, double m11OrigA, double m12OrigA,
97 double m21OrigA, double m22OrigA)
98 { return id.num == idA->num && id.gen == idA->gen &&
99 m11Orig == m11OrigA && m12Orig == m12OrigA &&
100 m21Orig == m21OrigA && m22Orig == m22OrigA; }
102 // Was font created successfully?
103 virtual GBool isOk() = 0;
105 // Update <gc> with this font.
106 virtual void updateGC(GC gc) = 0;
108 // Draw character <c>/<u> at <x>,<y> (in device space).
109 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
111 double x, double y, double dx, double dy,
112 CharCode c, Unicode *u, int uLen) = 0;
114 // Returns true if this XOutputFont subclass provides the
115 // getCharPath function.
116 virtual GBool hasGetCharPath() { return gFalse; }
118 // Add the character outline for <c>/<u> to the current path.
119 virtual void getCharPath(GfxState *state,
120 CharCode c, Unicode *u, int ulen);
125 double m11Orig, m12Orig, // original transform matrix
127 double m11, m12, m21, m22; // actual transform matrix (possibly
128 // modified for font substitution)
129 Display *display; // X display
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
138 class XOutputT1Font: public XOutputFont {
141 XOutputT1Font(Ref *idA, T1FontFile *fontFileA,
142 double m11OrigA, double m12OrigA,
143 double m21OrigA, double m22OrigA,
144 double m11A, double m12A,
145 double m21A, double m22A,
146 Display *displayA, XOutputDev *xOutA);
148 virtual ~XOutputT1Font();
150 // Was font created successfully?
151 virtual GBool isOk();
153 // Update <gc> with this font.
154 virtual void updateGC(GC gc);
156 // Draw character <c>/<u> at <x>,<y>.
157 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
159 double x, double y, double dx, double dy,
160 CharCode c, Unicode *u, int uLen);
162 // Returns true if this XOutputFont subclass provides the
163 // getCharPath function.
164 virtual GBool hasGetCharPath() { return gTrue; }
166 // Add the character outline for <c>/<u> to the current path.
167 virtual void getCharPath(GfxState *state,
168 CharCode c, Unicode *u, int ulen);
172 T1FontFile *fontFile;
175 #endif // HAVE_T1LIB_H
177 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
178 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
182 class XOutputFTFont: public XOutputFont {
185 XOutputFTFont(Ref *idA, FTFontFile *fontFileA,
186 double m11OrigA, double m12OrigA,
187 double m21OrigA, double m22OrigA,
188 double m11A, double m12A,
189 double m21A, double m22A,
190 Display *displayA, XOutputDev *xOutA);
192 virtual ~XOutputFTFont();
194 // Was font created successfully?
195 virtual GBool isOk();
197 // Update <gc> with this font.
198 virtual void updateGC(GC gc);
200 // Draw character <c>/<u> at <x>,<y>.
201 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
203 double x, double y, double dx, double dy,
204 CharCode c, Unicode *u, int uLen);
206 // Returns true if this XOutputFont subclass provides the
207 // getCharPath function.
208 virtual GBool hasGetCharPath() { return gTrue; }
210 // Add the character outline for <c>/<u> to the current path.
211 virtual void getCharPath(GfxState *state,
212 CharCode c, Unicode *u, int ulen);
216 FTFontFile *fontFile;
219 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
221 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
222 //------------------------------------------------------------------------
224 //------------------------------------------------------------------------
226 class XOutputTTFont: public XOutputFont {
229 XOutputTTFont(Ref *idA, TTFontFile *fontFileA,
230 double m11OrigA, double m12OrigA,
231 double m21OrigA, double m22OrigA,
232 double m11A, double m12A,
233 double m21A, double m22A,
234 Display *displayA, XOutputDev *xOutA);
236 virtual ~XOutputTTFont();
238 // Was font created successfully?
239 virtual GBool isOk();
241 // Update <gc> with this font.
242 virtual void updateGC(GC gc);
244 // Draw character <c>/<u> at <x>,<y>.
245 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
247 double x, double y, double dx, double dy,
248 CharCode c, Unicode *u, int uLen);
252 TTFontFile *fontFile;
255 #endif // !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
257 //------------------------------------------------------------------------
258 // XOutputServer8BitFont
259 //------------------------------------------------------------------------
261 class XOutputServer8BitFont: public XOutputFont {
264 XOutputServer8BitFont(Ref *idA, GString *xlfdFmt,
265 UnicodeMap *xUMapA, CharCodeToUnicode *fontUMap,
266 double m11OrigA, double m12OrigA,
267 double m21OrigA, double m22OrigA,
268 double m11A, double m12A, double m21A, double m22A,
269 Display *displayA, XOutputDev *xOutA);
271 virtual ~XOutputServer8BitFont();
273 // Was font created successfully?
274 virtual GBool isOk();
276 // Update <gc> with this font.
277 virtual void updateGC(GC gc);
279 // Draw character <c>/<u> at <x>,<y>.
280 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
282 double x, double y, double dx, double dy,
283 CharCode c, Unicode *u, int uLen);
287 XFontStruct *xFont; // the X font
288 Gushort map[256]; // forward map (char code -> X font code)
292 //------------------------------------------------------------------------
293 // XOutputServer16BitFont
294 //------------------------------------------------------------------------
296 class XOutputServer16BitFont: public XOutputFont {
299 XOutputServer16BitFont(Ref *idA, GString *xlfdFmt,
300 UnicodeMap *xUMapA, CharCodeToUnicode *fontUMap,
301 double m11OrigA, double m12OrigA,
302 double m21OrigA, double m22OrigA,
303 double m11A, double m12A, double m21A, double m22A,
304 Display *displayA, XOutputDev *xOutA);
306 virtual ~XOutputServer16BitFont();
308 // Was font created successfully?
309 virtual GBool isOk();
311 // Update <gc> with this font.
312 virtual void updateGC(GC gc);
314 // Draw character <c>/<u> at <x>,<y>.
315 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
317 double x, double y, double dx, double dy,
318 CharCode c, Unicode *u, int uLen);
322 XFontStruct *xFont; // the X font
326 //------------------------------------------------------------------------
328 //------------------------------------------------------------------------
331 class XOutputT1FontFile {
333 XOutputT1FontFile(int numA, int genA, GBool substA,
334 T1FontFile *fontFileA, GString *tmpFileNameA)
335 { num = numA; gen = genA; subst = substA;
336 fontFile = fontFileA; tmpFileName = tmpFileNameA; }
337 ~XOutputT1FontFile();
340 T1FontFile *fontFile;
341 GString *tmpFileName;
345 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
346 class XOutputFTFontFile {
348 XOutputFTFontFile(int numA, int genA, GBool substA,
349 FTFontFile *fontFileA, GString *tmpFileNameA)
350 { num = numA; gen = genA; subst = substA;
351 fontFile = fontFileA; tmpFileName = tmpFileNameA; }
352 ~XOutputFTFontFile();
355 FTFontFile *fontFile;
356 GString *tmpFileName;
360 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
361 class XOutputTTFontFile {
363 XOutputTTFontFile(int numA, int genA, GBool substA,
364 TTFontFile *fontFileA, GString *tmpFileNameA)
365 { num = numA; gen = genA; subst = substA;
366 fontFile = fontFileA; tmpFileName = tmpFileNameA; }
367 ~XOutputTTFontFile();
370 TTFontFile *fontFile;
371 GString *tmpFileName;
375 class XOutputFontCache {
379 XOutputFontCache(Display *displayA, Guint depthA,
381 FontRastControl t1libControlA,
382 FontRastControl freetypeControlA);
387 // Initialize (or re-initialize) the font cache for a new document.
388 void startDoc(int screenNum, Visual *visual,
389 Colormap colormap, GBool trueColor,
390 int rMul, int gMul, int bMul,
391 int rShift, int gShift, int bShift,
392 Gulong *colors, int numColors);
394 // Get a font. This creates a new font if necessary.
395 XOutputFont *getFont(XRef *xref, GfxFont *gfxFont, double m11, double m12,
396 double m21, double m22);
402 XOutputFont *tryGetFont(XRef *xref, DisplayFontParam *dfp, GfxFont *gfxFont,
403 double m11Orig, double m12Orig,
404 double m21Orig, double m22Orig,
405 double m11, double m12, double m21, double m22,
408 XOutputFont *tryGetT1Font(XRef *xref, GfxFont *gfxFont,
409 double m11, double m12, double m21, double m22);
410 XOutputFont *tryGetT1FontFromFile(XRef *xref, GString *fileName,
411 GBool deleteFile, GfxFont *gfxFont,
412 double m11Orig, double m12Orig,
413 double m21Orig, double m22Orig,
414 double m11, double m12,
415 double m21, double m22, GBool subst);
417 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
418 XOutputFont *tryGetFTFont(XRef *xref, GfxFont *gfxFont,
419 double m11, double m12, double m21, double m22);
420 XOutputFont *tryGetFTFontFromFile(XRef *xref, GString *fileName,
421 GBool deleteFile, GfxFont *gfxFont,
422 double m11Orig, double m12Orig,
423 double m21Orig, double m22Orig,
424 double m11, double m12,
425 double m21, double m22, GBool subst);
427 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
428 XOutputFont *tryGetTTFont(XRef *xref, GfxFont *gfxFont,
429 double m11, double m12, double m21, double m22);
430 XOutputFont *tryGetTTFontFromFile(XRef *xref, GString *fileName,
431 GBool deleteFile, GfxFont *gfxFont,
432 double m11Orig, double m12Orig,
433 double m21Orig, double m22Orig,
434 double m11, double m12,
435 double m21, double m22, GBool subst);
437 XOutputFont *tryGetServerFont(GString *xlfd, GString *encodingName,
439 double m11Orig, double m12Orig,
440 double m21Orig, double m22Orig,
441 double m11, double m12,
442 double m21, double m22);
444 Display *display; // X display pointer
446 Guint depth; // pixmap depth
449 fonts[xOutFontCacheSize];
453 FontRastControl t1libControl; // t1lib settings
454 T1FontEngine *t1Engine; // Type 1 font engine
455 GList *t1FontFiles; // list of Type 1 font files
456 // [XOutputT1FontFile]
459 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
460 FontRastControl // FreeType settings
463 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
464 FTFontEngine *ftEngine; // FreeType font engine
465 GList *ftFontFiles; // list of FreeType font files
466 // [XOutputFTFontFile]
468 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
469 TTFontEngine *ttEngine; // TrueType font engine
470 GList *ttFontFiles; // list of TrueType font files
471 // [XOutputTTFontFile]
475 //------------------------------------------------------------------------
477 //------------------------------------------------------------------------
479 struct XOutputState {
486 //------------------------------------------------------------------------
488 //------------------------------------------------------------------------
490 class XOutputDev: public OutputDev {
494 XOutputDev(Display *displayA, int screenNumA,
495 Visual *visualA, Colormap colormapA,
496 GBool reverseVideoA, unsigned long paperColorA,
497 GBool installCmap, int rgbCubeSize,
501 virtual ~XOutputDev();
503 //---- get info about output device
505 // Does this device use upside-down coordinates?
506 // (Upside-down means (0,0) is the top left corner of the page.)
507 virtual GBool upsideDown() { return gTrue; }
509 // Does this device use drawChar() or drawString()?
510 virtual GBool useDrawChar() { return gTrue; }
512 // Does this device use beginType3Char/endType3Char? Otherwise,
513 // text in Type 3 fonts will be drawn with drawChar/drawString.
514 virtual GBool interpretType3Chars() { return gTrue; }
516 //----- initialization and control
519 virtual void startPage(int pageNum, GfxState *state);
522 virtual void endPage();
525 virtual void drawLink(Link *link, Catalog *catalog);
527 //----- save/restore graphics state
528 virtual void saveState(GfxState *state);
529 virtual void restoreState(GfxState *state);
531 //----- update graphics state
532 virtual void updateAll(GfxState *state);
533 virtual void updateCTM(GfxState *state, double m11, double m12,
534 double m21, double m22, double m31, double m32);
535 virtual void updateLineDash(GfxState *state);
536 virtual void updateFlatness(GfxState *state);
537 virtual void updateLineJoin(GfxState *state);
538 virtual void updateLineCap(GfxState *state);
539 virtual void updateMiterLimit(GfxState *state);
540 virtual void updateLineWidth(GfxState *state);
541 virtual void updateFillColor(GfxState *state);
542 virtual void updateStrokeColor(GfxState *state);
544 //----- update text state
545 virtual void updateFont(GfxState *state);
547 //----- path painting
548 virtual void stroke(GfxState *state);
549 virtual void fill(GfxState *state);
550 virtual void eoFill(GfxState *state);
552 //----- path clipping
553 virtual void clip(GfxState *state);
554 virtual void eoClip(GfxState *state);
557 virtual void beginString(GfxState *state, GString *s);
558 virtual void endString(GfxState *state);
559 virtual void drawChar(GfxState *state, double x, double y,
560 double dx, double dy,
561 double originX, double originY,
562 CharCode code, Unicode *u, int uLen);
563 virtual GBool beginType3Char(GfxState *state,
564 CharCode code, Unicode *u, int uLen);
565 virtual void endType3Char(GfxState *state);
567 //----- image drawing
568 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
569 int width, int height, GBool invert,
571 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
572 int width, int height, GfxImageColorMap *colorMap,
573 int *maskColors, GBool inlineImg);
575 //----- Type 3 font operators
576 virtual void type3D0(GfxState *state, double wx, double wy);
577 virtual void type3D1(GfxState *state, double wx, double wy,
578 double llx, double lly, double urx, double ury);
580 //----- special access
582 // Called to indicate that a new PDF document has been loaded.
583 void startDoc(XRef *xrefA);
585 // Find a string. If <top> is true, starts looking at <xMin>,<yMin>;
586 // otherwise starts looking at top of page. If <bottom> is true,
587 // stops looking at <xMax>,<yMax>; otherwise stops looking at bottom
588 // of page. If found, sets the text bounding rectange and returns
589 // true; otherwise returns false.
590 GBool findText(Unicode *s, int len, GBool top, GBool bottom,
591 int *xMin, int *yMin, int *xMax, int *yMax);
593 // Get the text which is inside the specified rectangle.
594 GString *getText(int xMin, int yMin, int xMax, int yMax);
596 GBool isReverseVideo() { return reverseVideo; }
598 // Update pixmap ID after a page change.
599 void setPixmap(Pixmap pixmapA, int pixmapWA, int pixmapHA)
600 { pixmap = pixmapA; pixmapW = pixmapWA; pixmapH = pixmapHA; }
602 // Get the off-screen pixmap, its size, various display info.
603 Pixmap getPixmap() { return pixmap; }
604 int getPixmapWidth() { return pixmapW; }
605 int getPixmapHeight() { return pixmapH; }
606 Display *getDisplay() { return display; }
607 Guint getDepth() { return depth; }
609 Gulong findColor(GfxRGB *rgb);
613 XRef *xref; // the xref table for this PDF file
614 Display *display; // X display pointer
615 int screenNum; // X screen number
616 Pixmap pixmap; // pixmap to draw into
617 int pixmapW, pixmapH; // size of pixmap
618 Guint depth; // pixmap depth
619 Visual *visual; // X visual
620 Colormap colormap; // X colormap
621 int flatness; // line flatness
622 GC paperGC; // GC for background
623 GC strokeGC; // GC with stroke color
624 GC fillGC; // GC with fill color
625 Region clipRegion; // clipping region
626 GBool trueColor; // set if using a TrueColor visual
627 int rMul, gMul, bMul; // RGB multipliers (for TrueColor)
628 int rShift, gShift, bShift; // RGB shifts (for TrueColor)
630 colors[maxRGBCube * maxRGBCube * maxRGBCube];
631 unsigned long paperColor; // paper color (pixel value)
632 int numColors; // size of color cube
633 double redMap[256]; // map pixel (from color cube) to red value
634 GBool reverseVideo; // reverse video mode
635 XPoint // temporary points array
636 tmpPoints[numTmpPoints];
637 int // temporary arrays for fill/clip
638 tmpLengths[numTmpSubpaths];
640 tmpRects[numTmpSubpaths];
641 GfxFont *gfxFont; // current PDF font
642 XOutputFont *font; // current font
643 XOutputFontCache *fontCache; // font cache
644 T3FontCache * // Type 3 font cache
645 t3FontCache[xOutT3FontCacheSize];
646 int nT3Fonts; // number of valid entries in t3FontCache
647 T3GlyphStack *t3GlyphStack; // Type 3 glyph context stack
648 XOutputState *save; // stack of saved states
650 TextPage *text; // text from the current page
652 void updateLineAttrs(GfxState *state, GBool updateDash);
653 void doFill(GfxState *state, int rule);
654 void doClip(GfxState *state, int rule);
655 int convertPath(GfxState *state, XPoint **points, int *size,
656 int *numPoints, int **lengths, GBool fillHack);
657 void convertSubpath(GfxState *state, GfxSubpath *subpath,
658 XPoint **points, int *size, int *n);
659 void doCurve(XPoint **points, int *size, int *k,
660 double x0, double y0, double x1, double y1,
661 double x2, double y2, double x3, double y3);
662 void addPoint(XPoint **points, int *size, int *k, int x, int y);
663 void drawType3Glyph(T3FontCache *t3Font,
664 T3FontCacheTag *tag, Guchar *data,
665 double x, double y, GfxRGB *color);
666 Gulong findColor(GfxRGB *x, GfxRGB *err);