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