1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
18 #include <X11/Xutil.h>
21 #include "CharTypes.h"
22 #include "GlobalParams.h"
23 #include "OutputDev.h"
31 class XOutputFontCache;
32 struct T3FontCacheTag;
38 class DisplayFontParam;
40 class CharCodeToUnicode;
48 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
54 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
60 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
64 #define maxRGBCube 7 // max size of RGB color cube
66 #define numTmpPoints 256 // number of XPoints in temporary array
67 #define numTmpSubpaths 16 // number of elements in temporary arrays
70 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
75 short xMin, xMax; // min/max x values
76 short yMin, yMax; // min/max y values
79 //------------------------------------------------------------------------
81 //------------------------------------------------------------------------
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);
91 virtual ~XOutputFont();
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; }
100 // Was font created successfully?
101 virtual GBool isOk() = 0;
103 // Update <gc> with this font.
104 virtual void updateGC(GC gc) = 0;
106 // Draw character <c>/<u> at <x>,<y> (in device space).
107 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
109 double x, double y, double dx, double dy,
110 CharCode c, Unicode *u, int uLen) = 0;
112 // Returns true if this XOutputFont subclass provides the
113 // getCharPath function.
114 virtual GBool hasGetCharPath() { return gFalse; }
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);
123 double m11Orig, m12Orig, // original transform matrix
125 double m11, m12, m21, m22; // actual transform matrix (possibly
126 // modified for font substitution)
127 Display *display; // X display
132 //------------------------------------------------------------------------
134 //------------------------------------------------------------------------
136 class XOutputT1Font: public XOutputFont {
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);
146 virtual ~XOutputT1Font();
148 // Was font created successfully?
149 virtual GBool isOk();
151 // Update <gc> with this font.
152 virtual void updateGC(GC gc);
154 // Draw character <c>/<u> at <x>,<y>.
155 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
157 double x, double y, double dx, double dy,
158 CharCode c, Unicode *u, int uLen);
160 // Returns true if this XOutputFont subclass provides the
161 // getCharPath function.
162 virtual GBool hasGetCharPath() { return gTrue; }
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);
170 T1FontFile *fontFile;
173 #endif // HAVE_T1LIB_H
175 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
176 //------------------------------------------------------------------------
178 //------------------------------------------------------------------------
180 class XOutputFTFont: public XOutputFont {
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);
190 virtual ~XOutputFTFont();
192 // Was font created successfully?
193 virtual GBool isOk();
195 // Update <gc> with this font.
196 virtual void updateGC(GC gc);
198 // Draw character <c>/<u> at <x>,<y>.
199 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
201 double x, double y, double dx, double dy,
202 CharCode c, Unicode *u, int uLen);
204 // Returns true if this XOutputFont subclass provides the
205 // getCharPath function.
206 virtual GBool hasGetCharPath() { return gTrue; }
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);
214 FTFontFile *fontFile;
217 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
219 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
220 //------------------------------------------------------------------------
222 //------------------------------------------------------------------------
224 class XOutputTTFont: public XOutputFont {
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);
234 virtual ~XOutputTTFont();
236 // Was font created successfully?
237 virtual GBool isOk();
239 // Update <gc> with this font.
240 virtual void updateGC(GC gc);
242 // Draw character <c>/<u> at <x>,<y>.
243 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
245 double x, double y, double dx, double dy,
246 CharCode c, Unicode *u, int uLen);
250 TTFontFile *fontFile;
253 #endif // !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
255 //------------------------------------------------------------------------
256 // XOutputServer8BitFont
257 //------------------------------------------------------------------------
259 class XOutputServer8BitFont: public XOutputFont {
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);
269 virtual ~XOutputServer8BitFont();
271 // Was font created successfully?
272 virtual GBool isOk();
274 // Update <gc> with this font.
275 virtual void updateGC(GC gc);
277 // Draw character <c>/<u> at <x>,<y>.
278 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
280 double x, double y, double dx, double dy,
281 CharCode c, Unicode *u, int uLen);
285 XFontStruct *xFont; // the X font
286 Gushort map[256]; // forward map (char code -> X font code)
290 //------------------------------------------------------------------------
291 // XOutputServer16BitFont
292 //------------------------------------------------------------------------
294 class XOutputServer16BitFont: public XOutputFont {
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);
304 virtual ~XOutputServer16BitFont();
306 // Was font created successfully?
307 virtual GBool isOk();
309 // Update <gc> with this font.
310 virtual void updateGC(GC gc);
312 // Draw character <c>/<u> at <x>,<y>.
313 virtual void drawChar(GfxState *state, Pixmap pixmap, int w, int h,
315 double x, double y, double dx, double dy,
316 CharCode c, Unicode *u, int uLen);
320 XFontStruct *xFont; // the X font
324 //------------------------------------------------------------------------
326 //------------------------------------------------------------------------
329 class XOutputT1FontFile {
331 XOutputT1FontFile(int numA, int genA, GBool substA, T1FontFile *fontFileA)
332 { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
333 ~XOutputT1FontFile();
336 T1FontFile *fontFile;
340 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
341 class XOutputFTFontFile {
343 XOutputFTFontFile(int numA, int genA, GBool substA, FTFontFile *fontFileA)
344 { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
345 ~XOutputFTFontFile();
348 FTFontFile *fontFile;
352 #if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
353 class XOutputTTFontFile {
355 XOutputTTFontFile(int numA, int genA, GBool substA, TTFontFile *fontFileA)
356 { num = numA; gen = genA; subst = substA; fontFile = fontFileA; }
357 ~XOutputTTFontFile();
360 TTFontFile *fontFile;
364 class XOutputFontCache {
368 XOutputFontCache(Display *displayA, Guint depthA,
370 FontRastControl t1libControlA,
371 FontRastControl freetypeControlA);
376 // Initialize (or re-initialize) the font cache for a new document.
377 void startDoc(int screenNum, Colormap colormap,
379 int rMul, int gMul, int bMul,
380 int rShift, int gShift, int bShift,
381 Gulong *colors, int numColors);
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);
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,
397 XOutputFont *tryGetT1Font(XRef *xref, GfxFont *gfxFont,
398 double m11, double m12, double m21, double m22);
399 XOutputFont *tryGetT1FontFromFile(XRef *xref, GString *fileName,
401 double m11Orig, double m12Orig,
402 double m21Orig, double m22Orig,
403 double m11, double m12,
404 double m21, double m22, GBool subst);
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,
411 double m11Orig, double m12Orig,
412 double m21Orig, double m22Orig,
413 double m11, double m12,
414 double m21, double m22, GBool subst);
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,
421 double m11Orig, double m12Orig,
422 double m21Orig, double m22Orig,
423 double m11, double m12,
424 double m21, double m22, GBool subst);
426 XOutputFont *tryGetServerFont(GString *xlfd, GString *encodingName,
428 double m11Orig, double m12Orig,
429 double m21Orig, double m22Orig,
430 double m11, double m12,
431 double m21, double m22);
433 Display *display; // X display pointer
435 Guint depth; // pixmap depth
438 fonts[xOutFontCacheSize];
442 FontRastControl t1libControl; // t1lib settings
443 T1FontEngine *t1Engine; // Type 1 font engine
444 GList *t1FontFiles; // list of Type 1 font files
445 // [XOutputT1FontFile]
448 #if HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H
449 FontRastControl // FreeType settings
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]
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]
464 //------------------------------------------------------------------------
466 //------------------------------------------------------------------------
468 struct XOutputState {
475 //------------------------------------------------------------------------
477 //------------------------------------------------------------------------
479 class XOutputDev: public OutputDev {
483 XOutputDev(Display *displayA, Pixmap pixmapA, Guint depthA,
484 Colormap colormapA, GBool reverseVideoA,
485 unsigned long paperColor, GBool installCmap,
489 virtual ~XOutputDev();
491 //---- get info about output device
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; }
497 // Does this device use drawChar() or drawString()?
498 virtual GBool useDrawChar() { return gTrue; }
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; }
504 //----- initialization and control
507 virtual void startPage(int pageNum, GfxState *state);
510 virtual void endPage();
513 virtual void drawLink(Link *link, Catalog *catalog);
515 //----- save/restore graphics state
516 virtual void saveState(GfxState *state);
517 virtual void restoreState(GfxState *state);
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);
532 //----- update text state
533 virtual void updateFont(GfxState *state);
535 //----- path painting
536 virtual void stroke(GfxState *state);
537 virtual void fill(GfxState *state);
538 virtual void eoFill(GfxState *state);
540 //----- path clipping
541 virtual void clip(GfxState *state);
542 virtual void eoClip(GfxState *state);
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);
555 //----- image drawing
556 virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
557 int width, int height, GBool invert,
559 virtual void drawImage(GfxState *state, Object *ref, Stream *str,
560 int width, int height, GfxImageColorMap *colorMap,
561 int *maskColors, GBool inlineImg);
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);
568 //----- special access
570 // Called to indicate that a new PDF document has been loaded.
571 void startDoc(XRef *xrefA);
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);
581 // Get the text which is inside the specified rectangle.
582 GString *getText(int xMin, int yMin, int xMax, int yMax);
584 GBool isReverseVideo() { return reverseVideo; }
588 // Update pixmap ID after a page change.
589 void setPixmap(Pixmap pixmap1, int pixmapW1, int pixmapH1)
590 { pixmap = pixmap1; pixmapW = pixmapW1; pixmapH = pixmapH1; }
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)
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];
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
629 TextPage *text; // text from the current page
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);