xref: /aoo41x/main/vcl/unx/generic/gdi/pspgraphics.cxx (revision c82f2877)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 
34 #include "unx/pspgraphics.h"
35 
36 #include "vcl/jobdata.hxx"
37 #include "vcl/printerinfomanager.hxx"
38 #include "vcl/bmpacc.hxx"
39 #include "vcl/svapp.hxx"
40 #include "vcl/sysdata.hxx"
41 
42 #include "printergfx.hxx"
43 #include "salbmp.hxx"
44 #include "glyphcache.hxx"
45 #include "impfont.hxx"
46 #include "outfont.hxx"
47 #include "fontsubset.hxx"
48 #include "salprn.hxx"
49 #include "region.h"
50 
51 #ifdef ENABLE_GRAPHITE
52 #include <graphite_layout.hxx>
53 #include <graphite_serverfont.hxx>
54 #endif
55 
56 using namespace psp;
57 using namespace rtl;
58 
59 // ----- Implementation of PrinterBmp by means of SalBitmap/BitmapBuffer ---------------
60 
61 class SalPrinterBmp : public psp::PrinterBmp
62 {
63 	private:
64     BitmapBuffer*		mpBmpBuffer;
65 
66     FncGetPixel			mpFncGetPixel;
67     Scanline			mpScanAccess;
68     sal_PtrDiff			mnScanOffset;
69 
70     sal_uInt32			ColorOf (BitmapColor& rColor) const;
71     sal_uInt8			GrayOf  (BitmapColor& rColor) const;
72 
73     SalPrinterBmp ();
74 
75 	public:
76 
77 							SalPrinterBmp (BitmapBuffer* pBitmap);
78 		virtual				~SalPrinterBmp ();
79 		virtual sal_uInt32	GetPaletteColor (sal_uInt32 nIdx) const;
80 		virtual sal_uInt32	GetPaletteEntryCount () const;
81 		virtual sal_uInt32	GetPixelRGB  (sal_uInt32 nRow, sal_uInt32 nColumn) const;
82 		virtual sal_uInt8	GetPixelGray (sal_uInt32 nRow, sal_uInt32 nColumn) const;
83 		virtual sal_uInt8	GetPixelIdx  (sal_uInt32 nRow, sal_uInt32 nColumn) const;
84 		virtual sal_uInt32	GetWidth () const;
85 		virtual sal_uInt32	GetHeight() const;
86 		virtual sal_uInt32	GetDepth ()	const;
87 };
88 
89 SalPrinterBmp::SalPrinterBmp (BitmapBuffer* pBuffer) :
90 		mpBmpBuffer (pBuffer)
91 {
92 	DBG_ASSERT (mpBmpBuffer, "SalPrinterBmp::SalPrinterBmp () can't acquire Bitmap");
93 
94 	// calibrate scanline buffer
95 	if( BMP_SCANLINE_ADJUSTMENT( mpBmpBuffer->mnFormat ) == BMP_FORMAT_TOP_DOWN )
96 	{
97 		mpScanAccess = mpBmpBuffer->mpBits;
98 		mnScanOffset = mpBmpBuffer->mnScanlineSize;
99 	}
100 	else
101 	{
102 		mpScanAccess = mpBmpBuffer->mpBits
103 					   + (mpBmpBuffer->mnHeight - 1) * mpBmpBuffer->mnScanlineSize;
104 		mnScanOffset = - mpBmpBuffer->mnScanlineSize;
105 	}
106 
107 	// request read access to the pixels
108 	switch( BMP_SCANLINE_FORMAT( mpBmpBuffer->mnFormat ) )
109 	{
110 		case BMP_FORMAT_1BIT_MSB_PAL:
111 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_1BIT_MSB_PAL;	 break;
112 		case BMP_FORMAT_1BIT_LSB_PAL:
113 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_1BIT_LSB_PAL;	 break;
114 		case BMP_FORMAT_4BIT_MSN_PAL:
115 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_4BIT_MSN_PAL;	 break;
116 		case BMP_FORMAT_4BIT_LSN_PAL:
117 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_4BIT_LSN_PAL;	 break;
118 		case BMP_FORMAT_8BIT_PAL:
119 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_8BIT_PAL; 	 break;
120 		case BMP_FORMAT_8BIT_TC_MASK:
121 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_8BIT_TC_MASK;	 break;
122 		case BMP_FORMAT_16BIT_TC_MSB_MASK:
123 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_16BIT_TC_MSB_MASK; break;
124 		case BMP_FORMAT_16BIT_TC_LSB_MASK:
125 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_16BIT_TC_LSB_MASK; break;
126 		case BMP_FORMAT_24BIT_TC_BGR:
127 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_24BIT_TC_BGR;  break;
128 		case BMP_FORMAT_24BIT_TC_RGB:
129 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_24BIT_TC_RGB;  break;
130 		case BMP_FORMAT_24BIT_TC_MASK:
131 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_24BIT_TC_MASK; break;
132 		case BMP_FORMAT_32BIT_TC_ABGR:
133 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_32BIT_TC_ABGR; break;
134 		case BMP_FORMAT_32BIT_TC_ARGB:
135 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_32BIT_TC_ARGB; break;
136 		case BMP_FORMAT_32BIT_TC_BGRA:
137 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_32BIT_TC_BGRA; break;
138 		case BMP_FORMAT_32BIT_TC_RGBA:
139 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_32BIT_TC_RGBA; break;
140 		case BMP_FORMAT_32BIT_TC_MASK:
141 			mpFncGetPixel = BitmapReadAccess::GetPixelFor_32BIT_TC_MASK; break;
142 
143 		default:
144 			DBG_ERROR("Error: SalPrinterBmp::SalPrinterBmp() unknown bitmap format");
145 		break;
146 	}
147 }
148 
149 SalPrinterBmp::~SalPrinterBmp ()
150 {
151 }
152 
153 sal_uInt32
154 SalPrinterBmp::GetWidth () const
155 {
156 	return mpBmpBuffer->mnWidth;
157 }
158 
159 sal_uInt32
160 SalPrinterBmp::GetHeight () const
161 {
162 	return mpBmpBuffer->mnHeight;
163 }
164 
165 sal_uInt32
166 SalPrinterBmp::GetDepth () const
167 {
168 	sal_uInt32 nDepth;
169 
170 	switch (mpBmpBuffer->mnBitCount)
171 	{
172 		case 1:
173 			nDepth = 1;
174 			break;
175 
176 		case 4:
177 		case 8:
178 			nDepth = 8;
179 			break;
180 
181 		case 16:
182 		case 24:
183 		case 32:
184 			nDepth = 24;
185 			break;
186 
187 		default:
188 			nDepth = 1;
189 			DBG_ERROR ("Error: unsupported bitmap depth in SalPrinterBmp::GetDepth()");
190 			break;
191 	}
192 
193 	return nDepth;
194 }
195 
196 sal_uInt32
197 SalPrinterBmp::ColorOf (BitmapColor& rColor) const
198 {
199 	if (rColor.IsIndex())
200 		return ColorOf (mpBmpBuffer->maPalette[rColor.GetIndex()]);
201 	else
202 		return 	  ((rColor.GetBlue()) 		 & 0x000000ff)
203 				| ((rColor.GetGreen() <<  8) & 0x0000ff00)
204 				| ((rColor.GetRed()   << 16) & 0x00ff0000);
205 }
206 
207 sal_uInt8
208 SalPrinterBmp::GrayOf (BitmapColor& rColor) const
209 {
210 	if (rColor.IsIndex())
211 		return GrayOf (mpBmpBuffer->maPalette[rColor.GetIndex()]);
212 	else
213 		return (  rColor.GetBlue()  *  28UL
214 				+ rColor.GetGreen() * 151UL
215 				+ rColor.GetRed()   *  77UL ) >> 8;
216 }
217 
218 sal_uInt32
219 SalPrinterBmp::GetPaletteEntryCount () const
220 {
221 	return mpBmpBuffer->maPalette.GetEntryCount ();
222 }
223 
224 sal_uInt32
225 SalPrinterBmp::GetPaletteColor (sal_uInt32 nIdx) const
226 {
227 	return ColorOf (mpBmpBuffer->maPalette[nIdx]);
228 }
229 
230 sal_uInt32
231 SalPrinterBmp::GetPixelRGB (sal_uInt32 nRow, sal_uInt32 nColumn) const
232 {
233 	Scanline pScan = mpScanAccess + nRow * mnScanOffset;
234 	BitmapColor aColor = mpFncGetPixel (pScan, nColumn, mpBmpBuffer->maColorMask);
235 
236 	return ColorOf (aColor);
237 }
238 
239 sal_uInt8
240 SalPrinterBmp::GetPixelGray (sal_uInt32 nRow, sal_uInt32 nColumn) const
241 {
242 	Scanline pScan = mpScanAccess + nRow * mnScanOffset;
243 	BitmapColor aColor = mpFncGetPixel (pScan, nColumn, mpBmpBuffer->maColorMask);
244 
245 	return GrayOf (aColor);
246 }
247 
248 sal_uInt8
249 SalPrinterBmp::GetPixelIdx (sal_uInt32 nRow, sal_uInt32 nColumn) const
250 {
251 	Scanline pScan = mpScanAccess + nRow * mnScanOffset;
252 	BitmapColor aColor = mpFncGetPixel (pScan, nColumn, mpBmpBuffer->maColorMask);
253 
254 	if (aColor.IsIndex())
255 		return aColor.GetIndex();
256 	else
257 		return 0;
258 }
259 
260 /*******************************************************
261  * PspGraphics                                         *
262  *******************************************************/
263 
264 PspGraphics::~PspGraphics()
265 {
266     ReleaseFonts();
267 }
268 
269 void PspGraphics::GetResolution( sal_Int32 &rDPIX, sal_Int32 &rDPIY )
270 {
271 	if (m_pJobData != NULL)
272 	{
273 		int x = m_pJobData->m_aContext.getRenderResolution();
274 
275 		rDPIX = x;
276 		rDPIY = x;
277 	}
278 }
279 
280 sal_uInt16 PspGraphics::GetBitCount()
281 {
282     return m_pPrinterGfx->GetBitCount();
283 }
284 
285 long PspGraphics::GetGraphicsWidth() const
286 {
287     return 0;
288 }
289 
290 void PspGraphics::ResetClipRegion()
291 {
292     m_pPrinterGfx->ResetClipRegion();
293 }
294 
295 bool PspGraphics::setClipRegion( const Region& i_rClip )
296 {
297     // TODO: support polygonal clipregions here
298     m_pPrinterGfx->BeginSetClipRegion( i_rClip.GetRectCount() );
299 
300     ImplRegionInfo aInfo;
301     long nX, nY, nW, nH;
302     bool bRegionRect = i_rClip.ImplGetFirstRect(aInfo, nX, nY, nW, nH );
303     while( bRegionRect )
304     {
305         if ( nW && nH )
306         {
307             m_pPrinterGfx->UnionClipRegion( nX, nY, nW, nH );
308         }
309         bRegionRect = i_rClip.ImplGetNextRect( aInfo, nX, nY, nW, nH );
310     }
311     m_pPrinterGfx->EndSetClipRegion();
312     return true;
313 }
314 
315 void PspGraphics::SetLineColor()
316 {
317     m_pPrinterGfx->SetLineColor ();
318 }
319 
320 void PspGraphics::SetLineColor( SalColor nSalColor )
321 {
322     psp::PrinterColor aColor (SALCOLOR_RED   (nSalColor),
323                               SALCOLOR_GREEN (nSalColor),
324                               SALCOLOR_BLUE  (nSalColor));
325     m_pPrinterGfx->SetLineColor (aColor);
326 }
327 
328 void PspGraphics::SetFillColor()
329 {
330     m_pPrinterGfx->SetFillColor ();
331 }
332 
333 void PspGraphics::SetFillColor( SalColor nSalColor )
334 {
335     psp::PrinterColor aColor (SALCOLOR_RED   (nSalColor),
336                               SALCOLOR_GREEN (nSalColor),
337                               SALCOLOR_BLUE  (nSalColor));
338     m_pPrinterGfx->SetFillColor (aColor);
339 }
340 
341 void PspGraphics::SetROPLineColor( SalROPColor )
342 {
343     DBG_ASSERT( 0, "Error: PrinterGfx::SetROPLineColor() not implemented" );
344 }
345 
346 void PspGraphics::SetROPFillColor( SalROPColor )
347 {
348     DBG_ASSERT( 0, "Error: PrinterGfx::SetROPFillColor() not implemented" );
349 }
350 
351 void PspGraphics::SetXORMode( bool bSet, bool )
352 {
353     (void)bSet;
354     DBG_ASSERT( !bSet, "Error: PrinterGfx::SetXORMode() not implemented" );
355 }
356 
357 void PspGraphics::drawPixel( long nX, long nY )
358 {
359     m_pPrinterGfx->DrawPixel (Point(nX, nY));
360 }
361 
362 void PspGraphics::drawPixel( long nX, long nY, SalColor nSalColor )
363 {
364     psp::PrinterColor aColor (SALCOLOR_RED   (nSalColor),
365                               SALCOLOR_GREEN (nSalColor),
366                               SALCOLOR_BLUE  (nSalColor));
367     m_pPrinterGfx->DrawPixel (Point(nX, nY), aColor);
368 }
369 
370 void PspGraphics::drawLine( long nX1, long nY1, long nX2, long nY2 )
371 {
372     m_pPrinterGfx->DrawLine (Point(nX1, nY1), Point(nX2, nY2));
373 }
374 
375 void PspGraphics::drawRect( long nX, long nY, long nDX, long nDY )
376 {
377     m_pPrinterGfx->DrawRect (Rectangle(Point(nX, nY), Size(nDX, nDY)));
378 }
379 
380 void PspGraphics::drawPolyLine( sal_uLong nPoints, const SalPoint *pPtAry )
381 {
382     m_pPrinterGfx->DrawPolyLine (nPoints, (Point*)pPtAry);
383 }
384 
385 void PspGraphics::drawPolygon( sal_uLong nPoints, const SalPoint* pPtAry )
386 {
387 	// Point must be equal to SalPoint! see vcl/inc/salgtype.hxx
388     m_pPrinterGfx->DrawPolygon (nPoints, (Point*)pPtAry);
389 }
390 
391 void PspGraphics::drawPolyPolygon( sal_uInt32			nPoly,
392 								   const sal_uInt32   *pPoints,
393 								   PCONSTSALPOINT  *pPtAry )
394 {
395     m_pPrinterGfx->DrawPolyPolygon (nPoly, pPoints, (const Point**)pPtAry);
396 }
397 
398 bool PspGraphics::drawPolyPolygon( const ::basegfx::B2DPolyPolygon&, double /*fTransparency*/ )
399 {
400         // TODO: implement and advertise OutDevSupport_B2DDraw support
401         return false;
402 }
403 
404 bool PspGraphics::drawPolyLine( const basegfx::B2DPolygon&, double /*fTranspareny*/, const basegfx::B2DVector& /*rLineWidths*/, basegfx::B2DLineJoin /*eJoin*/)
405 {
406     // TODO: a PS printer can draw B2DPolyLines almost directly
407     return false;
408 }
409 
410 sal_Bool PspGraphics::drawPolyLineBezier( sal_uLong nPoints, const SalPoint* pPtAry, const sal_uInt8* pFlgAry )
411 {
412     m_pPrinterGfx->DrawPolyLineBezier (nPoints, (Point*)pPtAry, pFlgAry);
413     return sal_True;
414 }
415 
416 sal_Bool PspGraphics::drawPolygonBezier( sal_uLong nPoints, const SalPoint* pPtAry, const sal_uInt8* pFlgAry )
417 {
418     m_pPrinterGfx->DrawPolygonBezier (nPoints, (Point*)pPtAry, pFlgAry);
419     return sal_True;
420 }
421 
422 sal_Bool PspGraphics::drawPolyPolygonBezier( sal_uInt32 nPoly,
423                                              const sal_uInt32* pPoints,
424                                              const SalPoint* const* pPtAry,
425                                              const sal_uInt8* const* pFlgAry )
426 {
427 	// Point must be equal to SalPoint! see vcl/inc/salgtype.hxx
428     m_pPrinterGfx->DrawPolyPolygonBezier (nPoly, pPoints, (Point**)pPtAry, (sal_uInt8**)pFlgAry);
429     return sal_True;
430 }
431 
432 void PspGraphics::invert( sal_uLong,
433                           const SalPoint*,
434                           SalInvert )
435 {
436     DBG_ASSERT( 0, "Error: PrinterGfx::Invert() not implemented" );
437 }
438 sal_Bool PspGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight, void* pPtr, sal_uLong nSize )
439 {
440     return m_pPrinterGfx->DrawEPS( Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ), pPtr, nSize );
441 }
442 
443 void PspGraphics::copyBits( const SalTwoRect*,
444                             SalGraphics* )
445 {
446     DBG_ERROR( "Error: PrinterGfx::CopyBits() not implemented" );
447 }
448 
449 void PspGraphics::copyArea ( long,long,long,long,long,long,sal_uInt16 )
450 {
451     DBG_ERROR( "Error: PrinterGfx::CopyArea() not implemented" );
452 }
453 
454 void PspGraphics::drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap )
455 {
456     Rectangle aSrc (Point(pPosAry->mnSrcX, pPosAry->mnSrcY),
457                     Size(pPosAry->mnSrcWidth, pPosAry->mnSrcHeight));
458     Rectangle aDst (Point(pPosAry->mnDestX, pPosAry->mnDestY),
459                     Size(pPosAry->mnDestWidth, pPosAry->mnDestHeight));
460 
461     BitmapBuffer* pBuffer= const_cast<SalBitmap&>(rSalBitmap).AcquireBuffer(sal_True);
462 
463     SalPrinterBmp aBmp (pBuffer);
464     m_pPrinterGfx->DrawBitmap (aDst, aSrc, aBmp);
465 
466     const_cast<SalBitmap&>(rSalBitmap).ReleaseBuffer (pBuffer, sal_True);
467 }
468 
469 void PspGraphics::drawBitmap( const SalTwoRect*,
470                               const SalBitmap&,
471                               const SalBitmap& )
472 {
473     DBG_ERROR("Error: no PrinterGfx::DrawBitmap() for transparent bitmap");
474 }
475 
476 void PspGraphics::drawBitmap( const SalTwoRect*,
477                               const SalBitmap&,
478                               SalColor )
479 {
480     DBG_ERROR("Error: no PrinterGfx::DrawBitmap() for transparent color");
481 }
482 
483 void PspGraphics::drawMask( const SalTwoRect*,
484                             const SalBitmap &,
485                             SalColor )
486 {
487     DBG_ERROR("Error: PrinterGfx::DrawMask() not implemented");
488 }
489 
490 SalBitmap* PspGraphics::getBitmap( long, long, long, long )
491 {
492     DBG_WARNING ("Warning: PrinterGfx::GetBitmap() not implemented");
493     return NULL;
494 }
495 
496 SalColor PspGraphics::getPixel( long, long )
497 {
498     DBG_ERROR ("Warning: PrinterGfx::GetPixel() not implemented");
499     return 0;
500 }
501 
502 void PspGraphics::invert(long,long,long,long,SalInvert)
503 {
504     DBG_ERROR ("Warning: PrinterGfx::Invert() not implemented");
505 }
506 
507 //==========================================================================
508 
509 class ImplPspFontData : public ImplFontData
510 {
511 private:
512     enum { PSPFD_MAGIC = 0xb5bf01f0 };
513     sal_IntPtr              mnFontId;
514 
515 public:
516                             ImplPspFontData( const psp::FastPrintFontInfo& );
517     virtual sal_IntPtr      GetFontId() const { return mnFontId; }
518     virtual ImplFontData*   Clone() const { return new ImplPspFontData( *this ); }
519     virtual ImplFontEntry*  CreateFontInstance( ImplFontSelectData& ) const;
520     static bool             CheckFontData( const ImplFontData& r ) { return r.CheckMagic( PSPFD_MAGIC ); }
521 };
522 
523 //--------------------------------------------------------------------------
524 
525 ImplPspFontData::ImplPspFontData( const psp::FastPrintFontInfo& rInfo )
526 :   ImplFontData( PspGraphics::Info2DevFontAttributes(rInfo), PSPFD_MAGIC ),
527     mnFontId( rInfo.m_nID )
528 {}
529 
530 //--------------------------------------------------------------------------
531 
532 ImplFontEntry* ImplPspFontData::CreateFontInstance( ImplFontSelectData& rFSD ) const
533 {
534     ImplServerFontEntry* pEntry = new ImplServerFontEntry( rFSD );
535     return pEntry;
536 }
537 
538 //==========================================================================
539 
540 class PspFontLayout : public GenericSalLayout
541 {
542 public:
543                         PspFontLayout( ::psp::PrinterGfx& );
544     virtual bool        LayoutText( ImplLayoutArgs& );
545     virtual void        InitFont() const;
546     virtual void        DrawText( SalGraphics& ) const;
547 private:
548     ::psp::PrinterGfx&  mrPrinterGfx;
549     sal_IntPtr          mnFontID;
550     int                 mnFontHeight;
551     int                 mnFontWidth;
552     bool                mbVertical;
553     bool                mbArtItalic;
554     bool                mbArtBold;
555 };
556 
557 //--------------------------------------------------------------------------
558 
559 PspFontLayout::PspFontLayout( ::psp::PrinterGfx& rGfx )
560 :   mrPrinterGfx( rGfx )
561 {
562     mnFontID     = mrPrinterGfx.GetFontID();
563     mnFontHeight = mrPrinterGfx.GetFontHeight();
564     mnFontWidth  = mrPrinterGfx.GetFontWidth();
565     mbVertical   = mrPrinterGfx.GetFontVertical();
566     mbArtItalic	 = mrPrinterGfx.GetArtificialItalic();
567     mbArtBold	 = mrPrinterGfx.GetArtificialBold();
568 }
569 
570 //--------------------------------------------------------------------------
571 
572 bool PspFontLayout::LayoutText( ImplLayoutArgs& rArgs )
573 {
574     mbVertical = ((rArgs.mnFlags & SAL_LAYOUT_VERTICAL) != 0);
575 
576     long nUnitsPerPixel = 1;
577     int nOldGlyphId = -1;
578     long nGlyphWidth = 0;
579     int nCharPos = -1;
580     Point aNewPos( 0, 0 );
581     GlyphItem aPrevItem;
582     rtl_TextEncoding aFontEnc = mrPrinterGfx.GetFontMgr().getFontEncoding( mnFontID );
583     for(;;)
584     {
585         bool bRightToLeft;
586         if( !rArgs.GetNextPos( &nCharPos, &bRightToLeft ) )
587             break;
588 
589         sal_Unicode cChar = rArgs.mpStr[ nCharPos ];
590         if( bRightToLeft )
591             cChar = GetMirroredChar( cChar );
592         // symbol font aliasing: 0x0020-0x00ff -> 0xf020 -> 0xf0ff
593         if( aFontEnc == RTL_TEXTENCODING_SYMBOL )
594             if( cChar < 256 )
595                 cChar += 0xf000;
596         int nGlyphIndex = cChar;  // printer glyphs = unicode
597 
598         // update fallback_runs if needed
599         psp::CharacterMetric aMetric;
600         mrPrinterGfx.GetFontMgr().getMetrics( mnFontID, cChar, cChar, &aMetric, mbVertical );
601         if( aMetric.width == -1 && aMetric.height == -1 )
602             rArgs.NeedFallback( nCharPos, bRightToLeft );
603 
604         // apply pair kerning to prev glyph if requested
605         if( SAL_LAYOUT_KERNING_PAIRS & rArgs.mnFlags )
606         {
607             if( nOldGlyphId > 0 )
608             {
609                 const std::list< KernPair >& rKernPairs = mrPrinterGfx.getKernPairs(mbVertical);
610                 for( std::list< KernPair >::const_iterator it = rKernPairs.begin();
611                      it != rKernPairs.end(); ++it )
612                 {
613                     if( it->first == nOldGlyphId && it->second == nGlyphIndex )
614                     {
615                         int nTextScale = mrPrinterGfx.GetFontWidth();
616                         if( ! nTextScale )
617                             nTextScale = mrPrinterGfx.GetFontHeight();
618                         int nKern = (mbVertical ? it->kern_y : it->kern_x) * nTextScale;
619                         nGlyphWidth += nKern;
620                         aPrevItem.mnNewWidth = nGlyphWidth;
621                         break;
622                     }
623                 }
624             }
625         }
626 
627         // finish previous glyph
628         if( nOldGlyphId >= 0 )
629             AppendGlyph( aPrevItem );
630         nOldGlyphId = nGlyphIndex;
631         aNewPos.X() += nGlyphWidth;
632 
633         // prepare GlyphItem for appending it in next round
634         nUnitsPerPixel = mrPrinterGfx.GetCharWidth( cChar, cChar, &nGlyphWidth );
635         int nGlyphFlags = bRightToLeft ? GlyphItem::IS_RTL_GLYPH : 0;
636         nGlyphIndex |= GF_ISCHAR;
637         aPrevItem = GlyphItem( nCharPos, nGlyphIndex, aNewPos, nGlyphFlags, nGlyphWidth );
638     }
639 
640     // append last glyph item if any
641     if( nOldGlyphId >= 0 )
642         AppendGlyph( aPrevItem );
643 
644     SetOrientation( mrPrinterGfx.GetFontAngle() );
645     SetUnitsPerPixel( nUnitsPerPixel );
646     return (nOldGlyphId >= 0);
647 }
648 
649 class PspServerFontLayout : public ServerFontLayout
650 {
651 public:
652     PspServerFontLayout( psp::PrinterGfx&, ServerFont& rFont, const ImplLayoutArgs& rArgs );
653 
654     virtual void        InitFont() const;
655     const sal_Unicode*	getTextPtr() const { return maText.getStr() - mnMinCharPos; }
656     int					getMinCharPos() const { return mnMinCharPos; }
657     int					getMaxCharPos() const { return mnMinCharPos+maText.getLength()-1; }
658 private:
659     ::psp::PrinterGfx&  mrPrinterGfx;
660     sal_IntPtr          mnFontID;
661     int                 mnFontHeight;
662     int                 mnFontWidth;
663     bool                mbVertical;
664     bool				mbArtItalic;
665     bool				mbArtBold;
666     rtl::OUString		maText;
667     int					mnMinCharPos;
668 };
669 
670 PspServerFontLayout::PspServerFontLayout( ::psp::PrinterGfx& rGfx, ServerFont& rFont, const ImplLayoutArgs& rArgs )
671         :   ServerFontLayout( rFont ),
672             mrPrinterGfx( rGfx )
673 {
674     mnFontID     = mrPrinterGfx.GetFontID();
675     mnFontHeight = mrPrinterGfx.GetFontHeight();
676     mnFontWidth  = mrPrinterGfx.GetFontWidth();
677     mbVertical   = mrPrinterGfx.GetFontVertical();
678     mbArtItalic	 = mrPrinterGfx.GetArtificialItalic();
679     mbArtBold	 = mrPrinterGfx.GetArtificialBold();
680     maText		 = OUString( rArgs.mpStr + rArgs.mnMinCharPos, rArgs.mnEndCharPos - rArgs.mnMinCharPos+1 );
681     mnMinCharPos = rArgs.mnMinCharPos;
682 }
683 
684 void PspServerFontLayout::InitFont() const
685 {
686     mrPrinterGfx.SetFont( mnFontID, mnFontHeight, mnFontWidth,
687                           mnOrientation, mbVertical, mbArtItalic, mbArtBold );
688 }
689 
690 //--------------------------------------------------------------------------
691 
692 static void DrawPrinterLayout( const SalLayout& rLayout, ::psp::PrinterGfx& rGfx, bool bIsPspServerFontLayout )
693 {
694     const int nMaxGlyphs = 200;
695     sal_uInt32 aGlyphAry[ nMaxGlyphs ]; // TODO: use sal_GlyphId
696     sal_Int32   aWidthAry[ nMaxGlyphs ];
697     sal_Int32   aIdxAry  [ nMaxGlyphs ];
698     sal_Unicode aUnicodes[ nMaxGlyphs ];
699     int			aCharPosAry	[ nMaxGlyphs ];
700 
701     Point aPos;
702     long nUnitsPerPixel = rLayout.GetUnitsPerPixel();
703     const sal_Unicode* pText = NULL;
704     int nMinCharPos = 0;
705     int nMaxCharPos = 0;
706     if (bIsPspServerFontLayout)
707     {
708         const PspServerFontLayout * pPspLayout = dynamic_cast<const PspServerFontLayout*>(&rLayout);
709 #ifdef ENABLE_GRAPHITE
710         const GraphiteServerFontLayout * pGrLayout = dynamic_cast<const GraphiteServerFontLayout*>(&rLayout);
711 #endif
712         if (pPspLayout)
713         {
714             pText = pPspLayout->getTextPtr();
715             nMinCharPos = pPspLayout->getMinCharPos();
716             nMaxCharPos = pPspLayout->getMaxCharPos();
717         }
718 #ifdef ENABLE_GRAPHITE
719         else if (pGrLayout)
720         {
721         #if 0 // HACK: disabled for now due to #i114460#, see #desc12 there
722               // TODO: get rid of glyph->string mapping altogether for printing
723 	      // TODO: fix GraphiteServerFontLayout's returned aCharPosAry
724 	      // TODO: fix PrinterGfx's caching?
725             pText = pGrLayout->getTextPtr();
726             nMinCharPos = pGrLayout->getMinCharPos();
727             nMaxCharPos = pGrLayout->getMaxCharPos();
728 	#endif
729         }
730 #endif
731     }
732     for( int nStart = 0;; )
733     {
734         int nGlyphCount = rLayout.GetNextGlyphs( nMaxGlyphs, aGlyphAry, aPos, nStart, aWidthAry, pText ? aCharPosAry : NULL );
735         if( !nGlyphCount )
736             break;
737 
738         sal_Int32 nXOffset = 0;
739         for( int i = 0; i < nGlyphCount; ++i )
740         {
741             nXOffset += aWidthAry[ i ];
742             aIdxAry[ i ] = nXOffset / nUnitsPerPixel;
743             sal_Int32 nGlyphIdx = aGlyphAry[i] & (GF_IDXMASK | GF_ROTMASK);
744             if( pText )
745                 aUnicodes[i] = (aCharPosAry[i] >= nMinCharPos && aCharPosAry[i] <= nMaxCharPos) ? pText[ aCharPosAry[i] ] : 0;
746             else
747                 aUnicodes[i] = (aGlyphAry[i] & GF_ISCHAR) ? nGlyphIdx : 0;
748             aGlyphAry[i] = nGlyphIdx;
749         }
750 
751         rGfx.DrawGlyphs( aPos, (sal_uInt32 *)aGlyphAry, aUnicodes, nGlyphCount, aIdxAry );
752     }
753 }
754 
755 //--------------------------------------------------------------------------
756 
757 void PspFontLayout::InitFont() const
758 {
759     mrPrinterGfx.SetFont( mnFontID, mnFontHeight, mnFontWidth,
760         mnOrientation, mbVertical, mbArtItalic, mbArtBold );
761 }
762 
763 //--------------------------------------------------------------------------
764 
765 void PspFontLayout::DrawText( SalGraphics& ) const
766 {
767     DrawPrinterLayout( *this, mrPrinterGfx, false );
768 }
769 
770 void PspGraphics::DrawServerFontLayout( const ServerFontLayout& rLayout )
771 {
772     // print complex text
773     DrawPrinterLayout( rLayout, *m_pPrinterGfx, true );
774 }
775 
776 const ImplFontCharMap* PspGraphics::GetImplFontCharMap() const
777 {
778     if( !m_pServerFont[0] )
779         return NULL;
780 
781     const ImplFontCharMap* pIFCMap = m_pServerFont[0]->GetImplFontCharMap();
782     return pIFCMap;
783 }
784 
785 sal_uInt16 PspGraphics::SetFont( ImplFontSelectData *pEntry, int nFallbackLevel )
786 {
787     // release all fonts that are to be overridden
788     for( int i = nFallbackLevel; i < MAX_FALLBACK; ++i )
789     {
790         if( m_pServerFont[i] != NULL )
791         {
792             // old server side font is no longer referenced
793             GlyphCache::GetInstance().UncacheFont( *m_pServerFont[i] );
794             m_pServerFont[i] = NULL;
795         }
796     }
797 
798     // return early if there is no new font
799     if( !pEntry )
800         return 0;
801 
802     sal_IntPtr nID = pEntry->mpFontData ? pEntry->mpFontData->GetFontId() : 0;
803 
804     // determine which font attributes need to be emulated
805     bool bArtItalic = false;
806     bool bArtBold = false;
807     if( pEntry->meItalic == ITALIC_OBLIQUE || pEntry->meItalic == ITALIC_NORMAL )
808     {
809         psp::italic::type eItalic = m_pPrinterGfx->GetFontMgr().getFontItalic( nID );
810         if( eItalic != psp::italic::Italic && eItalic != psp::italic::Oblique )
811             bArtItalic = true;
812     }
813     int nWeight = (int)pEntry->meWeight;
814     int nRealWeight = (int)m_pPrinterGfx->GetFontMgr().getFontWeight( nID );
815     if( nRealWeight <= (int)psp::weight::Medium && nWeight > (int)WEIGHT_MEDIUM )
816     {
817         bArtBold = true;
818     }
819 
820     // also set the serverside font for layouting
821     m_bFontVertical = pEntry->mbVertical;
822     if( pEntry->mpFontData )
823     {
824         // requesting a font provided by builtin rasterizer
825         ServerFont* pServerFont = GlyphCache::GetInstance().CacheFont( *pEntry );
826         if( pServerFont != NULL )
827         {
828             if( pServerFont->TestFont() )
829                 m_pServerFont[ nFallbackLevel ] = pServerFont;
830             else
831                 GlyphCache::GetInstance().UncacheFont( *pServerFont );
832         }
833     }
834 
835     // set the printer font
836     return m_pPrinterGfx->SetFont( nID,
837                                    pEntry->mnHeight,
838                                    pEntry->mnWidth,
839                                    pEntry->mnOrientation,
840                                    pEntry->mbVertical,
841                                    bArtItalic,
842                                    bArtBold
843                                    );
844 }
845 
846 void PspGraphics::SetTextColor( SalColor nSalColor )
847 {
848     psp::PrinterColor aColor (SALCOLOR_RED   (nSalColor),
849                               SALCOLOR_GREEN (nSalColor),
850                               SALCOLOR_BLUE  (nSalColor));
851     m_pPrinterGfx->SetTextColor (aColor);
852 }
853 
854 bool PspGraphics::AddTempDevFont( ImplDevFontList*, const String&,const String& )
855 {
856     return false;
857 }
858 
859 void RegisterFontSubstitutors( ImplDevFontList* );
860 
861 void PspGraphics::GetDevFontList( ImplDevFontList *pList )
862 {
863     ::std::list< psp::fontID > aList;
864     psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
865     rMgr.getFontList( aList, m_pJobData->m_pParser, m_pInfoPrinter->m_bCompatMetrics );
866 
867     ::std::list< psp::fontID >::iterator it;
868     psp::FastPrintFontInfo aInfo;
869     for (it = aList.begin(); it != aList.end(); ++it)
870         if (rMgr.getFontFastInfo (*it, aInfo))
871             AnnounceFonts( pList, aInfo );
872 
873    // register platform specific font substitutions if available
874    if( rMgr.hasFontconfig() )
875 	RegisterFontSubstitutors( pList );
876 }
877 
878 void PspGraphics::GetDevFontSubstList( OutputDevice* pOutDev )
879 {
880     const psp::PrinterInfo& rInfo = psp::PrinterInfoManager::get().getPrinterInfo( m_pJobData->m_aPrinterName );
881     if( rInfo.m_bPerformFontSubstitution )
882     {
883         for( std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash >::const_iterator it = rInfo.m_aFontSubstitutes.begin(); it != rInfo.m_aFontSubstitutes.end(); ++it )
884             pOutDev->ImplAddDevFontSubstitute( it->first, it->second, FONT_SUBSTITUTE_ALWAYS );
885     }
886 }
887 
888 void PspGraphics::GetFontMetric( ImplFontMetricData *pMetric, int )
889 {
890     const psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
891     psp::PrintFontInfo aInfo;
892 
893     if (rMgr.getFontInfo (m_pPrinterGfx->GetFontID(), aInfo))
894     {
895         ImplDevFontAttributes aDFA = Info2DevFontAttributes( aInfo );
896         static_cast<ImplFontAttributes&>(*pMetric) = aDFA;
897         pMetric->mbDevice       = aDFA.mbDevice;
898         pMetric->mbScalableFont = true;
899 
900         pMetric->mnOrientation 	= m_pPrinterGfx->GetFontAngle();
901         pMetric->mnSlant		= 0;
902 
903         sal_Int32 nTextHeight	= m_pPrinterGfx->GetFontHeight();
904         sal_Int32 nTextWidth	= m_pPrinterGfx->GetFontWidth();
905         if( ! nTextWidth )
906             nTextWidth = nTextHeight;
907 
908         pMetric->mnWidth		= nTextWidth;
909         pMetric->mnAscent		= ( aInfo.m_nAscend * nTextHeight + 500 ) / 1000;
910         pMetric->mnDescent		= ( aInfo.m_nDescend * nTextHeight + 500 ) / 1000;
911         pMetric->mnIntLeading	= ( aInfo.m_nLeading * nTextHeight + 500 ) / 1000;
912         pMetric->mnExtLeading	= 0;
913     }
914 }
915 
916 sal_uLong PspGraphics::GetKernPairs( sal_uLong nPairs, ImplKernPairData *pKernPairs )
917 {
918     const ::std::list< ::psp::KernPair >& rPairs( m_pPrinterGfx->getKernPairs() );
919     sal_uLong nHavePairs = rPairs.size();
920     if( pKernPairs && nPairs )
921     {
922         ::std::list< ::psp::KernPair >::const_iterator it;
923         unsigned int i;
924         int nTextScale = m_pPrinterGfx->GetFontWidth();
925         if( ! nTextScale )
926             nTextScale = m_pPrinterGfx->GetFontHeight();
927         for( i = 0, it = rPairs.begin(); i < nPairs && i < nHavePairs; i++, ++it )
928         {
929             pKernPairs[i].mnChar1	= it->first;
930             pKernPairs[i].mnChar2	= it->second;
931             pKernPairs[i].mnKern	= it->kern_x * nTextScale / 1000;
932         }
933 
934     }
935     return nHavePairs;
936 }
937 
938 sal_Bool PspGraphics::GetGlyphBoundRect( long nGlyphIndex, Rectangle& rRect )
939 {
940     int nLevel = nGlyphIndex >> GF_FONTSHIFT;
941     if( nLevel >= MAX_FALLBACK )
942         return sal_False;
943 
944     ServerFont* pSF = m_pServerFont[ nLevel ];
945     if( !pSF )
946         return sal_False;
947 
948     nGlyphIndex &= ~GF_FONTMASK;
949     const GlyphMetric& rGM = pSF->GetGlyphMetric( nGlyphIndex );
950     rRect = Rectangle( rGM.GetOffset(), rGM.GetSize() );
951     return sal_True;
952 }
953 
954 sal_Bool PspGraphics::GetGlyphOutline( long nGlyphIndex,
955     ::basegfx::B2DPolyPolygon& rB2DPolyPoly )
956 {
957     int nLevel = nGlyphIndex >> GF_FONTSHIFT;
958     if( nLevel >= MAX_FALLBACK )
959         return sal_False;
960 
961     ServerFont* pSF = m_pServerFont[ nLevel ];
962     if( !pSF )
963         return sal_False;
964 
965     nGlyphIndex &= ~GF_FONTMASK;
966     if( pSF->GetGlyphOutline( nGlyphIndex, rB2DPolyPoly ) )
967         return sal_True;
968 
969     return sal_False;
970 }
971 
972 SalLayout* PspGraphics::GetTextLayout( ImplLayoutArgs& rArgs, int nFallbackLevel )
973 {
974     // workaround for printers not handling glyph indexing for non-TT fonts
975     int nFontId = m_pPrinterGfx->GetFontID();
976     if( psp::fonttype::TrueType != psp::PrintFontManager::get().getFontType( nFontId ) )
977         rArgs.mnFlags |= SAL_LAYOUT_DISABLE_GLYPH_PROCESSING;
978     else if( nFallbackLevel > 0 )
979         rArgs.mnFlags &= ~SAL_LAYOUT_DISABLE_GLYPH_PROCESSING;
980 
981     GenericSalLayout* pLayout = NULL;
982 
983     if( m_pServerFont[ nFallbackLevel ]
984         && !(rArgs.mnFlags & SAL_LAYOUT_DISABLE_GLYPH_PROCESSING) )
985     {
986 #ifdef ENABLE_GRAPHITE
987         // Is this a Graphite font?
988         if (GraphiteFontAdaptor::IsGraphiteEnabledFont(*m_pServerFont[nFallbackLevel]))
989         {
990             sal_Int32 xdpi, ydpi;
991             GetResolution(xdpi, ydpi);
992             GraphiteFontAdaptor * pGrfont = new GraphiteFontAdaptor( *m_pServerFont[nFallbackLevel], xdpi, ydpi);
993             if (!pGrfont) return NULL;
994             pLayout = new GraphiteServerFontLayout(pGrfont);
995         }
996         else
997 #endif
998             pLayout = new PspServerFontLayout( *m_pPrinterGfx, *m_pServerFont[nFallbackLevel], rArgs );
999     }
1000     else
1001         pLayout = new PspFontLayout( *m_pPrinterGfx );
1002 
1003     return pLayout;
1004 }
1005 
1006 //--------------------------------------------------------------------------
1007 
1008 sal_Bool PspGraphics::CreateFontSubset(
1009                                    const rtl::OUString& rToFile,
1010                                    const ImplFontData* pFont,
1011                                    sal_Int32* pGlyphIDs,
1012                                    sal_uInt8* pEncoding,
1013                                    sal_Int32* pWidths,
1014                                    int nGlyphCount,
1015                                    FontSubsetInfo& rInfo
1016                                    )
1017 {
1018     // in this context the pFont->GetFontId() is a valid PSP
1019     // font since they are the only ones left after the PDF
1020     // export has filtered its list of subsettable fonts (for
1021     // which this method was created). The correct way would
1022     // be to have the GlyphCache search for the ImplFontData pFont
1023     psp::fontID aFont = pFont->GetFontId();
1024 
1025     psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
1026     bool bSuccess = rMgr.createFontSubset( rInfo,
1027                                  aFont,
1028                                  rToFile,
1029                                  pGlyphIDs,
1030                                  pEncoding,
1031                                  pWidths,
1032                                  nGlyphCount );
1033     return bSuccess;
1034 }
1035 
1036 //--------------------------------------------------------------------------
1037 
1038 const void* PspGraphics::GetEmbedFontData( const ImplFontData* pFont, const sal_Ucs* pUnicodes, sal_Int32* pWidths, FontSubsetInfo& rInfo, long* pDataLen )
1039 {
1040     // in this context the pFont->GetFontId() is a valid PSP
1041     // font since they are the only ones left after the PDF
1042     // export has filtered its list of subsettable fonts (for
1043     // which this method was created). The correct way would
1044     // be to have the GlyphCache search for the ImplFontData pFont
1045     psp::fontID aFont = pFont->GetFontId();
1046     return PspGraphics::DoGetEmbedFontData( aFont, pUnicodes, pWidths, rInfo, pDataLen );
1047 }
1048 
1049 //--------------------------------------------------------------------------
1050 
1051 void PspGraphics::FreeEmbedFontData( const void* pData, long nLen )
1052 {
1053     PspGraphics::DoFreeEmbedFontData( pData, nLen );
1054 }
1055 
1056 //--------------------------------------------------------------------------
1057 
1058 const Ucs2SIntMap* PspGraphics::GetFontEncodingVector( const ImplFontData* pFont, const Ucs2OStrMap** pNonEncoded )
1059 {
1060     // in this context the pFont->GetFontId() is a valid PSP
1061     // font since they are the only ones left after the PDF
1062     // export has filtered its list of subsettable fonts (for
1063     // which this method was created). The correct way would
1064     // be to have the GlyphCache search for the ImplFontData pFont
1065     psp::fontID aFont = pFont->GetFontId();
1066     return PspGraphics::DoGetFontEncodingVector( aFont, pNonEncoded );
1067 }
1068 
1069 //--------------------------------------------------------------------------
1070 
1071 void PspGraphics::GetGlyphWidths( const ImplFontData* pFont,
1072                                   bool bVertical,
1073                                   Int32Vector& rWidths,
1074                                   Ucs2UIntMap& rUnicodeEnc )
1075 {
1076     // in this context the pFont->GetFontId() is a valid PSP
1077     // font since they are the only ones left after the PDF
1078     // export has filtered its list of subsettable fonts (for
1079     // which this method was created). The correct way would
1080     // be to have the GlyphCache search for the ImplFontData pFont
1081     psp::fontID aFont = pFont->GetFontId();
1082     PspGraphics::DoGetGlyphWidths( aFont, bVertical, rWidths, rUnicodeEnc );
1083 }
1084 
1085 
1086 // static helpers of PspGraphics
1087 
1088 const void* PspGraphics::DoGetEmbedFontData( fontID aFont, const sal_Ucs* pUnicodes, sal_Int32* pWidths, FontSubsetInfo& rInfo, long* pDataLen )
1089 {
1090     psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
1091 
1092     psp::PrintFontInfo aFontInfo;
1093     if( ! rMgr.getFontInfo( aFont, aFontInfo ) )
1094         return NULL;
1095 
1096     // fill in font info
1097     rInfo.m_nAscent		= aFontInfo.m_nAscend;
1098     rInfo.m_nDescent	= aFontInfo.m_nDescend;
1099     rInfo.m_aPSName		= rMgr.getPSName( aFont );
1100 
1101     int xMin, yMin, xMax, yMax;
1102     rMgr.getFontBoundingBox( aFont, xMin, yMin, xMax, yMax );
1103 
1104     psp::CharacterMetric aMetrics[256];
1105     sal_Ucs aUnicodes[256];
1106     if( aFontInfo.m_aEncoding == RTL_TEXTENCODING_SYMBOL && aFontInfo.m_eType == psp::fonttype::Type1 )
1107     {
1108         for( int i = 0; i < 256; i++ )
1109             aUnicodes[i] = pUnicodes[i] < 0x0100 ? pUnicodes[i] + 0xf000 : pUnicodes[i];
1110         pUnicodes = aUnicodes;
1111     }
1112     if( ! rMgr.getMetrics( aFont, pUnicodes, 256, aMetrics ) )
1113         return NULL;
1114 
1115     OString aSysPath = rMgr.getFontFileSysPath( aFont );
1116     struct stat aStat;
1117     if( stat( aSysPath.getStr(), &aStat ) )
1118         return NULL;
1119     int fd = open( aSysPath.getStr(), O_RDONLY );
1120     if( fd < 0 )
1121         return NULL;
1122     void* pFile = mmap( NULL, aStat.st_size, PROT_READ, MAP_SHARED, fd, 0 );
1123     close( fd );
1124     if( pFile == MAP_FAILED )
1125         return NULL;
1126 
1127     *pDataLen = aStat.st_size;
1128 
1129     rInfo.m_aFontBBox	= Rectangle( Point( xMin, yMin ), Size( xMax-xMin, yMax-yMin ) );
1130     rInfo.m_nCapHeight	= yMax; // Well ...
1131 
1132     for( int i = 0; i < 256; i++ )
1133         pWidths[i] = (aMetrics[i].width > 0 ? aMetrics[i].width : 0);
1134 
1135     switch( aFontInfo.m_eType )
1136     {
1137         case psp::fonttype::TrueType:
1138             rInfo.m_nFontType = FontSubsetInfo::SFNT_TTF;
1139             break;
1140         case psp::fonttype::Type1: {
1141             const bool bPFA = ((*(unsigned char*)pFile) < 0x80);
1142             rInfo.m_nFontType = bPFA ? FontSubsetInfo::TYPE1_PFA : FontSubsetInfo::TYPE1_PFB;
1143             }
1144             break;
1145         default:
1146             return NULL;
1147     }
1148 
1149     return pFile;
1150 }
1151 
1152 void PspGraphics::DoFreeEmbedFontData( const void* pData, long nLen )
1153 {
1154     if( pData )
1155         munmap( (char*)pData, nLen );
1156 }
1157 
1158 const Ucs2SIntMap* PspGraphics::DoGetFontEncodingVector( fontID aFont, const Ucs2OStrMap** pNonEncoded )
1159 {
1160     psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
1161 
1162     psp::PrintFontInfo aFontInfo;
1163     if( ! rMgr.getFontInfo( aFont, aFontInfo ) )
1164     {
1165         if( pNonEncoded )
1166             *pNonEncoded = NULL;
1167         return NULL;
1168     }
1169 
1170     return rMgr.getEncodingMap( aFont, pNonEncoded );
1171 }
1172 
1173 void PspGraphics::DoGetGlyphWidths( psp::fontID aFont,
1174                                     bool bVertical,
1175                                     Int32Vector& rWidths,
1176                                     Ucs2UIntMap& rUnicodeEnc )
1177 {
1178     psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
1179     rMgr.getGlyphWidths( aFont, bVertical, rWidths, rUnicodeEnc );
1180 }
1181 // ----------------------------------------------------------------------------
1182 
1183 FontWidth PspGraphics::ToFontWidth (psp::width::type eWidth)
1184 {
1185 	switch (eWidth)
1186 	{
1187 		case psp::width::UltraCondensed: return WIDTH_ULTRA_CONDENSED;
1188 		case psp::width::ExtraCondensed: return WIDTH_EXTRA_CONDENSED;
1189 		case psp::width::Condensed:		 return WIDTH_CONDENSED;
1190 		case psp::width::SemiCondensed:	 return WIDTH_SEMI_CONDENSED;
1191 		case psp::width::Normal:		 return WIDTH_NORMAL;
1192 		case psp::width::SemiExpanded:	 return WIDTH_SEMI_EXPANDED;
1193 		case psp::width::Expanded:		 return WIDTH_EXPANDED;
1194 		case psp::width::ExtraExpanded:	 return WIDTH_EXTRA_EXPANDED;
1195 		case psp::width::UltraExpanded:	 return WIDTH_ULTRA_EXPANDED;
1196         case psp::width::Unknown:        return WIDTH_DONTKNOW;
1197         default:
1198             DBG_ERROR( "unknown width mapping" );
1199             break;
1200 	}
1201 	return WIDTH_DONTKNOW;
1202 }
1203 
1204 FontWeight PspGraphics::ToFontWeight (psp::weight::type eWeight)
1205 {
1206 	switch (eWeight)
1207 	{
1208 		case psp::weight::Thin:		  return WEIGHT_THIN;
1209 		case psp::weight::UltraLight: return WEIGHT_ULTRALIGHT;
1210 		case psp::weight::Light:	  return WEIGHT_LIGHT;
1211 		case psp::weight::SemiLight:  return WEIGHT_SEMILIGHT;
1212 		case psp::weight::Normal:	  return WEIGHT_NORMAL;
1213 		case psp::weight::Medium:	  return WEIGHT_MEDIUM;
1214 		case psp::weight::SemiBold:	  return WEIGHT_SEMIBOLD;
1215 		case psp::weight::Bold:		  return WEIGHT_BOLD;
1216 		case psp::weight::UltraBold:  return WEIGHT_ULTRABOLD;
1217 		case psp::weight::Black:	  return WEIGHT_BLACK;
1218 		case psp::weight::Unknown:	  return WEIGHT_DONTKNOW;
1219         default:
1220             DBG_ERROR( "unknown weight mapping" );
1221             break;
1222 	}
1223 	return WEIGHT_DONTKNOW;
1224 }
1225 
1226 FontPitch PspGraphics::ToFontPitch (psp::pitch::type ePitch)
1227 {
1228 	switch (ePitch)
1229 	{
1230 		case psp::pitch::Fixed:		return PITCH_FIXED;
1231 		case psp::pitch::Variable:	return PITCH_VARIABLE;
1232 		case psp::pitch::Unknown:	return PITCH_DONTKNOW;
1233         default:
1234             DBG_ERROR( "unknown pitch mapping" );
1235             break;
1236 	}
1237 	return PITCH_DONTKNOW;
1238 }
1239 
1240 FontItalic PspGraphics::ToFontItalic (psp::italic::type eItalic)
1241 {
1242 	switch (eItalic)
1243 	{
1244 		case psp::italic::Upright:	return ITALIC_NONE;
1245 		case psp::italic::Oblique:	return ITALIC_OBLIQUE;
1246 		case psp::italic::Italic:	return ITALIC_NORMAL;
1247 		case psp::italic::Unknown:	return ITALIC_DONTKNOW;
1248         default:
1249             DBG_ERROR( "unknown italic mapping" );
1250             break;
1251 	}
1252 	return ITALIC_DONTKNOW;
1253 }
1254 
1255 FontFamily PspGraphics::ToFontFamily (psp::family::type eFamily)
1256 {
1257 	switch (eFamily)
1258 	{
1259 		case psp::family::Decorative: return FAMILY_DECORATIVE;
1260 		case psp::family::Modern:	  return FAMILY_MODERN;
1261 		case psp::family::Roman:	  return FAMILY_ROMAN;
1262 		case psp::family::Script:	  return FAMILY_SCRIPT;
1263 		case psp::family::Swiss:	  return FAMILY_SWISS;
1264 		case psp::family::System:	  return FAMILY_SYSTEM;
1265 		case psp::family::Unknown:	  return FAMILY_DONTKNOW;
1266         default:
1267             DBG_ERROR( "unknown family mapping" );
1268             break;
1269 	}
1270 	return FAMILY_DONTKNOW;
1271 }
1272 
1273 ImplDevFontAttributes PspGraphics::Info2DevFontAttributes( const psp::FastPrintFontInfo& rInfo )
1274 {
1275     ImplDevFontAttributes aDFA;
1276     aDFA.maName         = rInfo.m_aFamilyName;
1277     aDFA.maStyleName    = rInfo.m_aStyleName;
1278     aDFA.meFamily       = ToFontFamily (rInfo.m_eFamilyStyle);
1279     aDFA.meWeight       = ToFontWeight (rInfo.m_eWeight);
1280     aDFA.meItalic       = ToFontItalic (rInfo.m_eItalic);
1281     aDFA.meWidthType    = ToFontWidth (rInfo.m_eWidth);
1282     aDFA.mePitch        = ToFontPitch (rInfo.m_ePitch);
1283     aDFA.mbSymbolFlag   = (rInfo.m_aEncoding == RTL_TEXTENCODING_SYMBOL);
1284     aDFA.mbSubsettable  = rInfo.m_bSubsettable;
1285     aDFA.mbEmbeddable   = rInfo.m_bEmbeddable;
1286 
1287     switch( rInfo.m_eType )
1288     {
1289         case psp::fonttype::Builtin:
1290             aDFA.mnQuality       = 1024;
1291             aDFA.mbDevice        = true;
1292             break;
1293         case psp::fonttype::TrueType:
1294             aDFA.mnQuality       = 512;
1295             aDFA.mbDevice        = false;
1296             break;
1297         case psp::fonttype::Type1:
1298             aDFA.mnQuality       = 0;
1299             aDFA.mbDevice        = false;
1300             break;
1301         default:
1302             aDFA.mnQuality       = 0;
1303             aDFA.mbDevice        = false;
1304             break;
1305     }
1306 
1307     aDFA.mbOrientation   = true;
1308 
1309     // add font family name aliases
1310     ::std::list< OUString >::const_iterator it = rInfo.m_aAliases.begin();
1311     bool bHasMapNames = false;
1312     for(; it != rInfo.m_aAliases.end(); ++it )
1313     {
1314         if( bHasMapNames )
1315             aDFA.maMapNames.Append( ';' );
1316         aDFA.maMapNames.Append( (*it).getStr() );
1317 	bHasMapNames = true;
1318     }
1319 
1320 #if OSL_DEBUG_LEVEL > 2
1321     if( bHasMapNames )
1322     {
1323         ByteString aOrigName( aDFA.maName, osl_getThreadTextEncoding() );
1324         ByteString aAliasNames( aDFA.maMapNames, osl_getThreadTextEncoding() );
1325         fprintf( stderr, "using alias names \"%s\" for font family \"%s\"\n",
1326             aAliasNames.GetBuffer(), aOrigName.GetBuffer() );
1327     }
1328 #endif
1329 
1330     return aDFA;
1331 }
1332 
1333 // -----------------------------------------------------------------------
1334 
1335 void PspGraphics::AnnounceFonts( ImplDevFontList* pFontList, const psp::FastPrintFontInfo& aInfo )
1336 {
1337     int nQuality = 0;
1338 
1339     if( aInfo.m_eType == psp::fonttype::TrueType )
1340     {
1341         // asian type 1 fonts are not known
1342         psp::PrintFontManager& rMgr = psp::PrintFontManager::get();
1343         ByteString aFileName( rMgr.getFontFileSysPath( aInfo.m_nID ) );
1344         int nPos = aFileName.SearchBackward( '_' );
1345         if( nPos == STRING_NOTFOUND || aFileName.GetChar( nPos+1 ) == '.' )
1346             nQuality += 5;
1347         else
1348         {
1349             static const char* pLangBoost = NULL;
1350             static bool bOnce = true;
1351             if( bOnce )
1352             {
1353                 bOnce = false;
1354                 const LanguageType aLang = Application::GetSettings().GetUILanguage();
1355                 switch( aLang )
1356                 {
1357                     case LANGUAGE_JAPANESE:
1358                         pLangBoost = "jan";
1359                         break;
1360                     case LANGUAGE_CHINESE:
1361                     case LANGUAGE_CHINESE_SIMPLIFIED:
1362                     case LANGUAGE_CHINESE_SINGAPORE:
1363                         pLangBoost = "zhs";
1364                         break;
1365                     case LANGUAGE_CHINESE_TRADITIONAL:
1366                     case LANGUAGE_CHINESE_HONGKONG:
1367                     case LANGUAGE_CHINESE_MACAU:
1368                         pLangBoost = "zht";
1369                         break;
1370                     case LANGUAGE_KOREAN:
1371                     case LANGUAGE_KOREAN_JOHAB:
1372                         pLangBoost = "kor";
1373                         break;
1374                 }
1375             }
1376 
1377             if( pLangBoost )
1378                 if( aFileName.Copy( nPos+1, 3 ).EqualsIgnoreCaseAscii( pLangBoost ) )
1379                     nQuality += 10;
1380         }
1381     }
1382 
1383     ImplPspFontData* pFD = new ImplPspFontData( aInfo );
1384     pFD->mnQuality += nQuality;
1385     pFontList->Add( pFD );
1386 }
1387 
1388 bool PspGraphics::filterText( const String& rOrig, String& rNewText, xub_StrLen nIndex, xub_StrLen& rLen, xub_StrLen& rCutStart, xub_StrLen& rCutStop )
1389 {
1390 	if( ! m_pPhoneNr )
1391 		return false;
1392 
1393     rCutStop = rCutStart = STRING_NOTFOUND;
1394 
1395 #define FAX_PHONE_TOKEN          "@@#"
1396 #define FAX_PHONE_TOKEN_LENGTH   3
1397 #define FAX_END_TOKEN            "@@"
1398 #define FAX_END_TOKEN_LENGTH     2
1399 
1400 	bool bRet = false;
1401 	bool bStarted = false;
1402 	bool bStopped = false;
1403 	sal_uInt16 nPos;
1404 	sal_uInt16 nStart = 0;
1405 	sal_uInt16 nStop = rLen;
1406 	String aPhone = rOrig.Copy( nIndex, rLen );
1407 
1408 	if( ! m_bPhoneCollectionActive )
1409 	{
1410 		if( ( nPos = aPhone.SearchAscii( FAX_PHONE_TOKEN ) ) != STRING_NOTFOUND )
1411 		{
1412 			nStart = nPos;
1413 			m_bPhoneCollectionActive = true;
1414 			m_aPhoneCollection.Erase();
1415 			bRet = true;
1416 			bStarted = true;
1417 		}
1418 	}
1419 	if( m_bPhoneCollectionActive )
1420 	{
1421 		bRet = true;
1422 		nPos = bStarted ? nStart + FAX_PHONE_TOKEN_LENGTH : 0;
1423 		if( ( nPos = aPhone.SearchAscii( FAX_END_TOKEN, nPos ) ) != STRING_NOTFOUND )
1424 		{
1425 			m_bPhoneCollectionActive = false;
1426 			nStop = nPos + FAX_END_TOKEN_LENGTH;
1427 			bStopped = true;
1428 		}
1429 		int nTokenStart = nStart + (bStarted ? FAX_PHONE_TOKEN_LENGTH : 0);
1430 		int nTokenStop = nStop - (bStopped ? FAX_END_TOKEN_LENGTH : 0);
1431 		m_aPhoneCollection += aPhone.Copy( nTokenStart, nTokenStop - nTokenStart );
1432 		if( ! m_bPhoneCollectionActive )
1433 		{
1434             m_pPhoneNr->AppendAscii( "<Fax#>" );
1435 			m_pPhoneNr->Append( m_aPhoneCollection );
1436             m_pPhoneNr->AppendAscii( "</Fax#>" );
1437 			m_aPhoneCollection.Erase();
1438 		}
1439 	}
1440 	if( m_aPhoneCollection.Len() > 1024 )
1441 	{
1442 		m_bPhoneCollectionActive = false;
1443 		m_aPhoneCollection.Erase();
1444 		bRet = false;
1445 	}
1446 
1447     if( bRet && m_bSwallowFaxNo )
1448     {
1449         rLen -= nStop - nStart;
1450         rCutStart = nStart+nIndex;
1451         rCutStop = nStop+nIndex;
1452         if( rCutStart )
1453             rNewText = rOrig.Copy( 0, rCutStart );
1454         rNewText += rOrig.Copy( rCutStop );
1455     }
1456 
1457     return bRet && m_bSwallowFaxNo;
1458 }
1459 
1460 bool PspGraphics::drawAlphaBitmap( const SalTwoRect&,
1461                                    const SalBitmap&,
1462                                    const SalBitmap& )
1463 {
1464     return false;
1465 }
1466 
1467 bool PspGraphics::drawAlphaRect( long, long, long, long, sal_uInt8 )
1468 {
1469     return false;
1470 }
1471 
1472 SystemGraphicsData PspGraphics::GetGraphicsData() const
1473 {
1474     SystemGraphicsData aRes;
1475     aRes.nSize = sizeof(aRes);
1476         aRes.hDrawable = 0;
1477         aRes.pRenderFormat = 0;
1478     return aRes;
1479 }
1480 
1481 SystemFontData PspGraphics::GetSysFontData( int nFallbacklevel ) const
1482 {
1483     SystemFontData aSysFontData;
1484 
1485     if (nFallbacklevel >= MAX_FALLBACK) nFallbacklevel = MAX_FALLBACK - 1;
1486     if (nFallbacklevel < 0 ) nFallbacklevel = 0;
1487 
1488     aSysFontData.nSize = sizeof( SystemFontData );
1489     aSysFontData.nFontId = 0;
1490     aSysFontData.nFontFlags = 0;
1491     aSysFontData.bFakeBold = false;
1492     aSysFontData.bFakeItalic = false;
1493     aSysFontData.bAntialias = true;
1494     return aSysFontData;
1495 }
1496 
1497 bool PspGraphics::supportsOperation( OutDevSupportType ) const
1498 {
1499     return false;
1500 }
1501