1 //========================================================================
5 // Copyright 1996-2003 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,
426 GBool embedded, GBool subst);
428 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
429 XOutputFont *tryGetTTFont(XRef *xref, GfxFont *gfxFont,
430 double m11, double m12, double m21, double m22);
431 XOutputFont *tryGetTTFontFromFile(XRef *xref, GString *fileName,
432 GBool deleteFile, GfxFont *gfxFont,
433 double m11Orig, double m12Orig,
434 double m21Orig, double m22Orig,
435 double m11, double m12,
436 double m21, double m22, GBool subst);
438 XOutputFont *tryGetServerFont(GString *xlfd, GString *encodingName,
440 double m11Orig, double m12Orig,
441 double m21Orig, double m22Orig,
442 double m11, double m12,
443 double m21, double m22);
445 Display *display; // X display pointer
447 Guint depth; // pixmap depth
450 fonts[xOutFontCacheSize];
454 FontRastControl t1libControl; // t1lib settings
455 T1FontEngine *t1Engine; // Type 1 font engine
456 GList *t1FontFiles; // list of Type 1 font files
457 // [XOutputT1FontFile]
460 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
461 FontRastControl // FreeType settings
464 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
465 FTFontEngine *ftEngine; // FreeType font engine
466 GList *ftFontFiles; // list of FreeType font files
467 // [XOutputFTFontFile]
469 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
470 TTFontEngine *ttEngine; // TrueType font engine
471 GList *ttFontFiles; // list of TrueType font files
472 // [XOutputTTFontFile]
476 //------------------------------------------------------------------------
478 //------------------------------------------------------------------------
480 struct XOutputState {
487 //------------------------------------------------------------------------
489 //------------------------------------------------------------------------
491 class XOutputDev: public OutputDev {
495 XOutputDev(Display *displayA, int screenNumA,
496 Visual *visualA, Colormap colormapA,
497 GBool reverseVideoA, unsigned long paperColorA,
498 GBool installCmap, int rgbCubeSize,
502 virtual ~XOutputDev();
504 //---- get info about output device
506 // Does this device use upside-down coordinates?
507 // (Upside-down means (0,0) is the top left corner of the page.)
508 virtual GBool upsideDown() { return gTrue; }
510 // Does this device use drawChar() or drawString()?
511 virtual GBool useDrawChar() { return gTrue; }
513 // Does this device use beginType3Char/endType3Char? Otherwise,
514 // text in Type 3 fonts will be drawn with drawChar/drawString.
515 virtual GBool interpretType3Chars() { return gTrue; }
517 //----- initialization and control
520 virtual void startPage(int pageNum, GfxState *state);
523 virtual void endPage();
526 virtual void drawLink(Link *link, Catalog *catalog);
528 //----- save/restore graphics state
529 virtual void saveState(GfxState *state);
530 virtual void restoreState(GfxState *state);
532 //----- update graphics state
533 virtual void updateAll(GfxState *state);
534 virtual void updateCTM(GfxState *state, double m11, double m12,
535 double m21, double m22, double m31, double m32);
536 virtual void updateLineDash(GfxState *state);
537 virtual void updateFlatness(GfxState *state);
538 virtual void updateLineJoin(GfxState *state);
539 virtual void updateLineCap(GfxState *state);
540 virtual void updateMiterLimit(GfxState *state);
541 virtual void updateLineWidth(GfxState *state);
542 virtual void updateFillColor(GfxState *state);
543 virtual void updateStrokeColor(GfxState *state);
545 //----- update text state
546 virtual void updateFont(GfxState *state);
548 //----- path painting
549 virtual void stroke(GfxState *state);
550 virtual void fill(GfxState *state);
551 virtual void eoFill(GfxState *state);
553 //----- path clipping
554 virtual void clip(GfxState *state);
555 virtual void eoClip(GfxState *state);
558 virtual void beginString(GfxState *state, GString *s);
559 virtual void endString(GfxState *state);
560 virtual void drawChar(GfxState *state, double x, double y,
561 double dx, double dy,
562 double originX, double originY,
563 CharCode code, Unicode *u, int uLen);
564 virtual GBool beginType3Char(GfxState *state,
565 CharCode code, Unicode *u, int uLen);
566 virtual void endType3Char(GfxState *state);
568 //----- image drawing
569 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
570 int width, int height, GBool invert,
572 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
573 int width, int height, GfxImageColorMap *colorMap,
574 int *maskColors, GBool inlineImg);
576 //----- Type 3 font operators
577 virtual void type3D0(GfxState *state, double wx, double wy);
578 virtual void type3D1(GfxState *state, double wx, double wy,
579 double llx, double lly, double urx, double ury);
581 //----- special access
583 // Called to indicate that a new PDF document has been loaded.
584 void startDoc(XRef *xrefA);
586 // Find a string. If <top> is true, starts looking at <xMin>,<yMin>;
587 // otherwise starts looking at top of page. If <bottom> is true,
588 // stops looking at <xMax>,<yMax>; otherwise stops looking at bottom
589 // of page. If found, sets the text bounding rectange and returns
590 // true; otherwise returns false.
591 GBool findText(Unicode *s, int len, GBool top, GBool bottom,
592 int *xMin, int *yMin, int *xMax, int *yMax);
594 // Get the text which is inside the specified rectangle.
595 GString *getText(int xMin, int yMin, int xMax, int yMax);
597 GBool isReverseVideo() { return reverseVideo; }
599 // Update pixmap ID after a page change.
600 void setPixmap(Pixmap pixmapA, int pixmapWA, int pixmapHA)
601 { pixmap = pixmapA; pixmapW = pixmapWA; pixmapH = pixmapHA; }
603 // Get the off-screen pixmap, its size, various display info.
604 Pixmap getPixmap() { return pixmap; }
605 int getPixmapWidth() { return pixmapW; }
606 int getPixmapHeight() { return pixmapH; }
607 Display *getDisplay() { return display; }
608 Guint getDepth() { return depth; }
610 Gulong findColor(GfxRGB *rgb);
614 XRef *xref; // the xref table for this PDF file
615 Display *display; // X display pointer
616 int screenNum; // X screen number
617 Pixmap pixmap; // pixmap to draw into
618 int pixmapW, pixmapH; // size of pixmap
619 Guint depth; // pixmap depth
620 Visual *visual; // X visual
621 Colormap colormap; // X colormap
622 int flatness; // line flatness
623 GC paperGC; // GC for background
624 GC strokeGC; // GC with stroke color
625 GC fillGC; // GC with fill color
626 Region clipRegion; // clipping region
627 GBool trueColor; // set if using a TrueColor visual
628 int rMul, gMul, bMul; // RGB multipliers (for TrueColor)
629 int rShift, gShift, bShift; // RGB shifts (for TrueColor)
631 colors[maxRGBCube * maxRGBCube * maxRGBCube];
632 unsigned long paperColor; // paper color (pixel value)
633 int numColors; // size of color cube
634 double redMap[256]; // map pixel (from color cube) to red value
635 GBool reverseVideo; // reverse video mode
636 XPoint // temporary points array
637 tmpPoints[numTmpPoints];
638 int // temporary arrays for fill/clip
639 tmpLengths[numTmpSubpaths];
641 tmpRects[numTmpSubpaths];
642 GfxFont *gfxFont; // current PDF font
643 XOutputFont *font; // current font
644 GBool needFontUpdate; // set when the font needs to be updated
645 XOutputFontCache *fontCache; // font cache
646 T3FontCache * // Type 3 font cache
647 t3FontCache[xOutT3FontCacheSize];
648 int nT3Fonts; // number of valid entries in t3FontCache
649 T3GlyphStack *t3GlyphStack; // Type 3 glyph context stack
650 XOutputState *save; // stack of saved states
652 TextPage *text; // text from the current page
654 void updateLineAttrs(GfxState *state, GBool updateDash);
655 void doFill(GfxState *state, int rule);
656 void doClip(GfxState *state, int rule);
657 int convertPath(GfxState *state, XPoint **points, int *size,
658 int *numPoints, int **lengths, GBool fillHack);
659 void convertSubpath(GfxState *state, GfxSubpath *subpath,
660 XPoint **points, int *size, int *n);
661 void doCurve(XPoint **points, int *size, int *k,
662 double x0, double y0, double x1, double y1,
663 double x2, double y2, double x3, double y3);
664 void addPoint(XPoint **points, int *size, int *k, int x, int y);
665 void drawType3Glyph(T3FontCache *t3Font,
666 T3FontCacheTag *tag, Guchar *data,
667 double x, double y, GfxRGB *color);
668 Gulong findColor(GfxRGB *x, GfxRGB *err);