xref: /aoo42x/main/vcl/inc/printergfx.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _PSPRINT_PRINTERGFX_HXX_
29 #define _PSPRINT_PRINTERGFX_HXX_
30 
31 #include "vcl/helper.hxx"
32 #include "sallayout.hxx"
33 #include "osl/file.hxx"
34 #include "tools/gen.hxx"
35 
36 #include <list>
37 #include <hash_map>
38 
39 namespace psp {
40 
41 // forwards
42 class JobData;
43 
44 /*
45  * lightweight container to handle RGB values
46  */
47 
48 class PrinterColor
49 {
50 public:
51 
52     enum    ColorSpace { eInvalid, eRGB };
53 
54 private:
55 
56     sal_uInt8       mnRed;
57     sal_uInt8       mnGreen;
58     sal_uInt8       mnBlue;
59     ColorSpace      meColorspace;
60 
61 public:
62 
63     PrinterColor () :
64             meColorspace(eInvalid)
65     {}
66     PrinterColor (sal_uInt16 nRed, sal_uInt16 nGreen,
67                   sal_uInt16 nBlue) :
68             mnRed   (nRed),
69             mnGreen (nGreen),
70             mnBlue  (nBlue),
71             meColorspace (eRGB)
72     {}
73     PrinterColor (sal_uInt32 nRGB) :
74             mnRed   ((nRGB & 0x00ff0000) >> 16),
75             mnGreen ((nRGB & 0x0000ff00) >>  8),
76             mnBlue  ((nRGB & 0x000000ff)      ),
77             meColorspace (eRGB)
78     {}
79     ~PrinterColor ()
80     {}
81 
82     sal_Bool        Is () const
83     { return meColorspace != eInvalid; }
84 
85     ColorSpace      GetColorSpace () const
86     { return meColorspace; }
87     sal_uInt16      GetRed () const
88     { return mnRed; }
89     sal_uInt16      GetGreen () const
90     { return mnGreen; }
91     sal_uInt16      GetBlue () const
92     { return mnBlue; }
93     sal_Bool        operator== (const PrinterColor& aColor) const
94     {
95         return aColor.Is() && this->Is()
96             && mnRed   == aColor.mnRed
97             && mnGreen == aColor.mnGreen
98             && mnBlue  == aColor.mnBlue;
99     }
100     sal_Bool        operator!= (const PrinterColor& aColor) const
101     { return ! (aColor==*this); }
102     PrinterColor&   operator= (const PrinterColor& aColor)
103     {
104         meColorspace = aColor.meColorspace;
105         mnRed   = aColor.mnRed;
106         mnGreen = aColor.mnGreen;
107         mnBlue  = aColor.mnBlue;
108 
109         return *this;
110     }
111 
112     PrinterColor&   operator= (sal_uInt32 nRGB)
113     {
114         meColorspace = eRGB;
115         mnBlue  = (nRGB & 0x000000ff);
116         mnGreen = (nRGB & 0x0000ff00) >>  8;
117         mnRed   = (nRGB & 0x00ff0000) >> 16;
118 
119         return *this;
120     }
121 };
122 
123 /*
124  * forward declarations
125  */
126 
127 class Font3;
128 class GlyphSet;
129 class PrinterJob;
130 class PrintFontManager;
131 class KernPair;
132 struct CharacterMetric;
133 
134 /*
135  * Bitmap Interface, this has to be filled with your actual bitmap implementation
136  * sample implementations can be found in:
137  *      psprint/workben/cui/pspdem.cxx
138  *      vcl/unx/source/gdi/salgdi2.cxx
139  */
140 
141 class PrinterBmp
142 {
143 public:
144 
145     virtual             ~PrinterBmp ()  = 0;
146     virtual sal_uInt32  GetPaletteColor (sal_uInt32 nIdx) const = 0;
147     virtual sal_uInt32  GetPaletteEntryCount ()           const = 0;
148     virtual sal_uInt32  GetPixelRGB  (sal_uInt32 nRow, sal_uInt32 nColumn) const = 0;
149     virtual sal_uInt8   GetPixelGray (sal_uInt32 nRow, sal_uInt32 nColumn) const = 0;
150     virtual sal_uInt8   GetPixelIdx  (sal_uInt32 nRow, sal_uInt32 nColumn) const = 0;
151     virtual sal_uInt32  GetWidth ()     const = 0;
152     virtual sal_uInt32  GetHeight ()    const = 0;
153     virtual sal_uInt32  GetDepth ()     const = 0;
154 };
155 
156 typedef enum {
157     InvalidType = 0,
158     TrueColorImage,
159     MonochromeImage,
160     PaletteImage,
161     GrayScaleImage
162 } ImageType;
163 
164 /*
165  * printer raster operations
166  */
167 
168 struct GraphicsStatus
169 {
170     rtl::OString        maFont;
171     rtl_TextEncoding	maEncoding;
172     bool				mbArtItalic;
173     bool				mbArtBold;
174     sal_Int32           mnTextHeight;
175     sal_Int32           mnTextWidth;
176     PrinterColor        maColor;
177     double             mfLineWidth;
178 
179     GraphicsStatus();
180 };
181 
182 class Font3;
183 
184 class PrinterGfx
185 {
186 private:
187 
188     /* common settings */
189 
190     double          mfScaleX;
191     double          mfScaleY;
192 
193     sal_uInt32      mnDpi;
194     sal_uInt16      mnDepth;
195 
196     sal_uInt16      mnPSLevel;
197     sal_Bool        mbColor;
198     sal_Bool        mbUploadPS42Fonts;
199 
200     osl::File*      mpPageHeader;
201     osl::File*      mpPageBody;
202 
203     void            TranslateCoordinates (sal_Int32 &rXOut, sal_Int32 &rYOut,
204                                           sal_Int32 nXIn, sal_Int32 nYIn )
205     { rXOut = nXIn; rYOut = nYIn; }
206     void            TranslateCoordinates (Point& rOut, const Point& rIn)
207     { rOut = rIn; }
208 
209     /* text/font related data, for a type1 font it has to be checked
210        whether this font has already been downloaded. A TrueType font
211        will be converted into one or more Type3 fonts, containing glyphs
212        in no particular order. In addition to the existence of the
213        glyph in one of the subfonts, the mapping from unicode to the
214        glyph has to be remembered */
215 
216     std::list< sal_Int32 > maPS1Font;
217     std::list< GlyphSet > maPS3Font;
218 
219     sal_Int32       mnFontID;
220     sal_Int32       mnFallbackID;
221     sal_Int32       mnTextAngle;
222     bool           mbTextVertical;
223     PrintFontManager& mrFontMgr;
224 
225     /* bitmap drawing implementation */
226 
227     sal_Bool    mbCompressBmp;
228 
229     void    DrawPS1GrayImage      (const PrinterBmp& rBitmap, const Rectangle& rArea);
230     void    writePS2ImageHeader   (const Rectangle& rArea, psp::ImageType nType);
231     void    writePS2Colorspace    (const PrinterBmp& rBitmap, psp::ImageType nType);
232     void    DrawPS2GrayImage      (const PrinterBmp& rBitmap, const Rectangle& rArea);
233     void    DrawPS2PaletteImage   (const PrinterBmp& rBitmap, const Rectangle& rArea);
234     void    DrawPS2TrueColorImage (const PrinterBmp& rBitmap, const Rectangle& rArea);
235     void    DrawPS2MonoImage      (const PrinterBmp& rBitmap, const Rectangle& rArea);
236 
237     /* clip region */
238 
239     std::list< Rectangle > maClipRegion;
240     sal_Bool JoinVerticalClipRectangles( std::list< Rectangle >::iterator& it,
241                                          Point& aOldPoint, sal_Int32& nColumn );
242 
243     /* color settings */
244     PrinterColor    maFillColor;
245     PrinterColor    maTextColor;
246     PrinterColor    maLineColor;
247 
248     /* graphics state */
249     GraphicsStatus                  maVirtualStatus;
250     std::list< GraphicsStatus >     maGraphicsStack;
251     GraphicsStatus& currentState() { return maGraphicsStack.front(); }
252 
253     /* font / font substitution */
254     friend class Font3;
255     const ::std::hash_map< fontID, fontID >*    mpFontSubstitutes;
256     int             getCharWidth (sal_Bool b_vert, sal_Unicode n_char,
257                                   CharacterMetric *p_bbox);
258     fontID          getCharMetric (const Font3 &rFont, sal_Unicode n_char,
259                                    CharacterMetric *p_bbox);
260     fontID          getFontSubstitute () const;
261     fontID          getFallbackID () const { return mnFallbackID; }
262 
263     bool            mbStrictSO52Compatibility;
264 public:
265     /* grahics status update */
266     void            PSSetColor ();
267     void            PSSetLineWidth ();
268     void            PSSetFont ();
269 
270     /* graphics status functions */
271     void            PSSetColor (const PrinterColor& rColor)
272     { maVirtualStatus.maColor = rColor; }
273 
274     void            PSUploadPS1Font (sal_Int32 nFontID);
275     void            PSSetFont (const rtl::OString& rName,
276                                rtl_TextEncoding nEncoding = RTL_TEXTENCODING_DONTKNOW)
277     { maVirtualStatus.maFont = rName; maVirtualStatus.maEncoding = nEncoding; }
278 
279     /* graphics status stack */
280     void            PSGSave ();
281     void            PSGRestore ();
282 
283 
284     /* PS helpers */
285     enum pspath_t { moveto = 0, lineto = 1 };
286     void            PSBinLineTo (const Point& rCurrent, Point& rOld,
287                                  sal_Int32& nColumn);
288     void            PSBinMoveTo (const Point& rCurrent, Point& rOld,
289                                  sal_Int32& nColumn);
290     void            PSBinStartPath ();
291     void            PSBinEndPath ();
292     void            PSBinCurrentPath (sal_uInt32 nPoints, const Point* pPath);
293     void            PSBinPath (const Point& rCurrent, Point& rOld,
294                                pspath_t eType, sal_Int32& nColumn);
295 
296     void            PSRotate (sal_Int32 nAngle);
297     void            PSTranslate (const Point& rPoint);
298     void            PSMoveTo (const Point& rPoint);
299     void            PSRMoveTo (sal_Int32 nDx, sal_Int32 nDy = 0);
300     void            PSScale (double fScaleX, double fScaleY);
301     void            PSLineTo(const Point& rPoint );
302     void            PSPointOp (const Point& rPoint, const sal_Char* pOperator);
303     void            PSHexString (const sal_uChar* pString, sal_Int16 nLen);
304     void            PSDeltaArray (const sal_Int32 *pArray, sal_Int16 nEntries);
305     void            PSShowText (const sal_uChar* pString,
306                                 sal_Int16 nGlyphs, sal_Int16 nBytes,
307                                 const sal_Int32* pDeltaArray = NULL);
308     void			PSComment (const sal_Char* pComment );
309     void            LicenseWarning (const Point& rPoint, const sal_Unicode* pStr,
310                                     sal_Int16 nLen, const sal_Int32* pDeltaArray);
311 
312     void			OnEndPage ();
313     void			OnEndJob ();
314     void			writeResources( osl::File* pFile, std::list< rtl::OString >& rSuppliedFonts, std::list< rtl::OString >& rNeededFonts );
315     PrintFontManager& GetFontMgr () { return mrFontMgr; }
316 
317     void            drawVerticalizedText (const Point& rPoint,
318                                           const sal_Unicode* pStr,
319                                           sal_Int16 nLen,
320                                           const sal_Int32* pDeltaArray );
321     void            drawText (const Point& rPoint,
322                               const sal_Unicode* pStr, sal_Int16 nLen,
323                               const sal_Int32* pDeltaArray = NULL);
324 
325     void			drawGlyphs( const Point& rPoint,
326                                 sal_GlyphId* pGlyphIds,
327                                 sal_Unicode* pUnicodes,
328                                 sal_Int16 nLen,
329                                 sal_Int32* pDeltaArray );
330 public:
331     PrinterGfx();
332     ~PrinterGfx();
333     sal_Bool        Init (PrinterJob &rPrinterSpec);
334     sal_Bool        Init (const JobData& rData);
335     void            Clear();
336 
337     // query depth and size
338     void            GetResolution (sal_Int32 &rDpiX, sal_Int32 &rDpiY) const;
339     sal_uInt16      GetBitCount ();
340 
341     // clip region
342     void            ResetClipRegion ();
343     void            BeginSetClipRegion (sal_uInt32);
344     sal_Bool        UnionClipRegion (sal_Int32 nX, sal_Int32 nY,
345                                      sal_Int32 nDX, sal_Int32 nDY);
346     void            EndSetClipRegion ();
347 
348     // set xy color
349     void            SetLineColor (const PrinterColor& rLineColor = PrinterColor())
350     { maLineColor = rLineColor; }
351     void            SetFillColor (const PrinterColor& rFillColor = PrinterColor())
352     { maFillColor = rFillColor; }
353 
354     // drawing primitives
355     void            DrawPixel (const Point& rPoint, const PrinterColor& rPixelColor);
356     void            DrawPixel (const Point& rPoint)
357     { DrawPixel (rPoint, maLineColor); }
358     void            DrawLine  (const Point& rFrom, const Point& rTo);
359     void            DrawRect  (const Rectangle& rRectangle);
360     void            DrawPolyLine (sal_uInt32 nPoints, const Point* pPath );
361     void            DrawPolygon  (sal_uInt32 nPoints, const Point* pPath);
362     void            DrawPolyPolygon (sal_uInt32 nPoly,
363                                      const sal_uInt32 *pPolygonSize,
364                                      const Point** pPolygonList);
365     void            DrawPolyLineBezier (sal_uInt32 nPoints,
366                                      const Point* pPath,
367                                      const sal_uInt8* pFlgAry );
368     void            DrawPolygonBezier  (sal_uInt32 nPoints,
369                                      const Point* pPath,
370                                      const sal_uInt8* pFlgAry);
371     void            DrawPolyPolygonBezier  (sal_uInt32 nPoly,
372                                      const sal_uInt32* pPoints,
373                                      const Point* const* pPtAry,
374                                      const sal_uInt8* const* pFlgAry);
375 
376     // eps
377     sal_Bool        DrawEPS ( const Rectangle& rBoundingBox, void* pPtr, sal_uInt32 nSize);
378 
379     // image drawing
380     void            DrawBitmap (const Rectangle& rDest, const Rectangle& rSrc,
381                                 const PrinterBmp& rBitmap);
382     void            DrawBitmap (const Rectangle& rDest, const Rectangle& rSrc,
383                                 const PrinterBmp& rBitmap,
384                                 const PrinterBmp& rTransBitmap);
385     void            DrawMask   (const Rectangle& rDest, const Rectangle& rSrc,
386                                 const PrinterBmp &rBitmap, PrinterColor& rMaskColor);
387 
388     // font and text handling
389     sal_uInt16      SetFont (
390                              sal_Int32 nFontID,
391                              sal_Int32 nPointHeight,
392                              sal_Int32 nPointWidth,
393                              sal_Int32 nAngle,
394                              bool bVertical,
395                              bool bArtItalic,
396                              bool bArtBold
397                              );
398     sal_uInt16      SetFallbackFont ( sal_Int32 nFontID );
399     sal_Int32       GetFontAngle () const
400     { return mnTextAngle; }
401     sal_Int32       GetFontID () const
402     { return mnFontID; }
403     bool			GetFontVertical() const
404     { return mbTextVertical; }
405     sal_Int32       GetFontHeight () const
406     { return maVirtualStatus.mnTextHeight; }
407     sal_Int32       GetFontWidth () const
408     { return maVirtualStatus.mnTextWidth; }
409     bool			GetArtificialItalic() const
410     { return maVirtualStatus.mbArtItalic; }
411     bool			GetArtificialBold() const
412     { return maVirtualStatus.mbArtBold; }
413     void            DrawText (const Point& rPoint,
414                               const sal_Unicode* pStr, sal_Int16 nLen,
415                               const sal_Int32* pDeltaArray = NULL);
416     void            SetTextColor (PrinterColor& rTextColor)
417     { maTextColor = rTextColor; }
418     sal_Int32       GetCharWidth (sal_uInt16 nFrom, sal_uInt16 nTo,
419                                   long *pWidthArray);
420     const ::std::list< KernPair >& getKernPairs( bool bVertical = false ) const;
421     // advanced font handling
422     sal_Bool        GetGlyphBoundRect (sal_Unicode c, Rectangle& rOutRect);
423     sal_uInt32      GetGlyphOutline (sal_Unicode c,
424                                      sal_uInt16 **ppPolySizes, Point **ppPoints,
425                                      sal_uInt8 **ppFlags);
426 
427     // for CTL
428     void			DrawGlyphs( const Point& rPoint,
429                                 sal_GlyphId* pGlyphIds,
430                                 sal_Unicode* pUnicodes,
431                                 sal_Int16 nLen,
432                                 sal_Int32* pDeltaArray );
433 
434     bool getStrictSO52Compatibility() const;
435     void setStrictSO52Compatibility( bool );
436 };
437 
438 } /* namespace psp */
439 
440 
441 #endif /* _PSPRINT_PRINTERGFX_HXX_ */
442 
443