xref: /aoo41x/main/vcl/aqua/source/gdi/salgdi.cxx (revision 5aaf853b)
19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
109f62ea84SAndrew Rist  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
199f62ea84SAndrew Rist  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "osl/file.hxx"
28cdf0e10cSrcweir #include "osl/process.h"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "vos/mutex.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "rtl/bootstrap.h"
33cdf0e10cSrcweir #include "rtl/strbuf.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "basegfx/range/b2drectangle.hxx"
36cdf0e10cSrcweir #include "basegfx/polygon/b2dpolygon.hxx"
37cdf0e10cSrcweir #include "basegfx/polygon/b2dpolygontools.hxx"
38cdf0e10cSrcweir #include "basegfx/matrix/b2dhommatrix.hxx"
39cdf0e10cSrcweir #include "basegfx/matrix/b2dhommatrixtools.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include "vcl/sysdata.hxx"
42cdf0e10cSrcweir #include "vcl/svapp.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "aqua/salconst.h"
45cdf0e10cSrcweir #include "aqua/salgdi.h"
46cdf0e10cSrcweir #include "aqua/salbmp.h"
47cdf0e10cSrcweir #include "aqua/salframe.h"
48cdf0e10cSrcweir #include "aqua/salcolorutils.hxx"
49cdf0e10cSrcweir #include "aqua/salatsuifontutils.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include "fontsubset.hxx"
52cdf0e10cSrcweir #include "impfont.hxx"
53cdf0e10cSrcweir #include "region.h"
54cdf0e10cSrcweir #include "sallayout.hxx"
55cdf0e10cSrcweir #include "sft.hxx"
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
58cdf0e10cSrcweir using namespace vcl;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //typedef unsigned char Boolean; // copied from MacTypes.h, should be properly included
61cdf0e10cSrcweir typedef std::vector<unsigned char> ByteVector;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // =======================================================================
65cdf0e10cSrcweir 
66cdf0e10cSrcweir ImplMacFontData::ImplMacFontData( const ImplDevFontAttributes& rDFA, ATSUFontID nFontId )
67cdf0e10cSrcweir :   ImplFontData( rDFA, 0 )
68cdf0e10cSrcweir ,   mnFontId( nFontId )
69cdf0e10cSrcweir ,	mpCharMap( NULL )
70cdf0e10cSrcweir ,	mbOs2Read( false )
71cdf0e10cSrcweir ,	mbHasOs2Table( false )
72cdf0e10cSrcweir ,	mbCmapEncodingRead( false )
73cdf0e10cSrcweir ,	mbHasCJKSupport( false )
74cdf0e10cSrcweir {}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir // -----------------------------------------------------------------------
77cdf0e10cSrcweir 
78cdf0e10cSrcweir ImplMacFontData::~ImplMacFontData()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	if( mpCharMap )
81cdf0e10cSrcweir 		mpCharMap->DeReference();
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // -----------------------------------------------------------------------
85cdf0e10cSrcweir 
86cdf0e10cSrcweir sal_IntPtr ImplMacFontData::GetFontId() const
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     return (sal_IntPtr)mnFontId;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir // -----------------------------------------------------------------------
92cdf0e10cSrcweir 
93cdf0e10cSrcweir ImplFontData* ImplMacFontData::Clone() const
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	ImplMacFontData* pClone = new ImplMacFontData(*this);
96cdf0e10cSrcweir 	if( mpCharMap )
97cdf0e10cSrcweir 		mpCharMap->AddReference();
98cdf0e10cSrcweir     return pClone;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir // -----------------------------------------------------------------------
102cdf0e10cSrcweir 
103cdf0e10cSrcweir ImplFontEntry* ImplMacFontData::CreateFontInstance(ImplFontSelectData& rFSD) const
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     return new ImplFontEntry(rFSD);
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir // -----------------------------------------------------------------------
109cdf0e10cSrcweir 
110cdf0e10cSrcweir inline FourCharCode GetTag(const char aTagName[5])
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	return (aTagName[0]<<24)+(aTagName[1]<<16)+(aTagName[2]<<8)+(aTagName[3]);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir static unsigned GetUShort( const unsigned char* p ){return((p[0]<<8)+p[1]);}
116cdf0e10cSrcweir static unsigned GetUInt( const unsigned char* p ) { return((p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]);}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir const ImplFontCharMap* ImplMacFontData::GetImplFontCharMap() const
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	// return the cached charmap
121cdf0e10cSrcweir 	if( mpCharMap )
122cdf0e10cSrcweir 		return mpCharMap;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	// set the default charmap
125cdf0e10cSrcweir 	mpCharMap = ImplFontCharMap::GetDefaultMap();
126cdf0e10cSrcweir 	mpCharMap->AddReference();
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	// get the CMAP byte size
129cdf0e10cSrcweir 	ATSFontRef rFont = FMGetATSFontRefFromFont( mnFontId );
130cdf0e10cSrcweir 	ByteCount nBufSize = 0;
131cdf0e10cSrcweir     OSStatus eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, 0, NULL, &nBufSize );
132cdf0e10cSrcweir 	DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::GetImplFontCharMap : ATSFontGetTable1 failed!\n");
133cdf0e10cSrcweir 	if( eStatus != noErr )
134cdf0e10cSrcweir 		return mpCharMap;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	// allocate a buffer for the CMAP raw data
137cdf0e10cSrcweir 	ByteVector aBuffer( nBufSize );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	// get the CMAP raw data
140cdf0e10cSrcweir 	ByteCount nRawLength = 0;
141cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, nBufSize, (void*)&aBuffer[0], &nRawLength );
142cdf0e10cSrcweir 	DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::GetImplFontCharMap : ATSFontGetTable2 failed!\n");
143cdf0e10cSrcweir 	if( eStatus != noErr )
144cdf0e10cSrcweir 		return mpCharMap;
145cdf0e10cSrcweir 	DBG_ASSERT( (nBufSize==nRawLength), "ImplMacFontData::GetImplFontCharMap : ByteCount mismatch!\n");
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	// parse the CMAP
148cdf0e10cSrcweir 	CmapResult aCmapResult;
149cdf0e10cSrcweir 	if( ParseCMAP( &aBuffer[0], nRawLength, aCmapResult ) )
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 		// create the matching charmap
152cdf0e10cSrcweir 		mpCharMap->DeReference();
153cdf0e10cSrcweir 		mpCharMap = new ImplFontCharMap( aCmapResult );
154cdf0e10cSrcweir 		mpCharMap->AddReference();
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	return mpCharMap;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // -----------------------------------------------------------------------
161cdf0e10cSrcweir 
162cdf0e10cSrcweir void ImplMacFontData::ReadOs2Table( void ) const
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	// read this only once per font
165cdf0e10cSrcweir 	if( mbOs2Read )
166cdf0e10cSrcweir 		return;
167cdf0e10cSrcweir 	mbOs2Read = true;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	// prepare to get the OS/2 table raw data
170cdf0e10cSrcweir 	ATSFontRef rFont = FMGetATSFontRefFromFont( mnFontId );
171cdf0e10cSrcweir 	ByteCount nBufSize = 0;
172cdf0e10cSrcweir 	OSStatus eStatus = ATSFontGetTable( rFont, GetTag("OS/2"), 0, 0, NULL, &nBufSize );
173cdf0e10cSrcweir 	DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::ReadOs2Table : ATSFontGetTable1 failed!\n");
174cdf0e10cSrcweir 	if( eStatus != noErr )
175cdf0e10cSrcweir 		return;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	// allocate a buffer for the OS/2 raw data
178cdf0e10cSrcweir 	ByteVector aBuffer( nBufSize );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	// get the OS/2 raw data
181cdf0e10cSrcweir 	ByteCount nRawLength = 0;
182cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("OS/2"), 0, nBufSize, (void*)&aBuffer[0], &nRawLength );
183cdf0e10cSrcweir 	DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::ReadOs2Table : ATSFontGetTable2 failed!\n");
184cdf0e10cSrcweir 	if( eStatus != noErr )
185cdf0e10cSrcweir 		return;
186cdf0e10cSrcweir 	DBG_ASSERT( (nBufSize==nRawLength), "ImplMacFontData::ReadOs2Table : ByteCount mismatch!\n");
187cdf0e10cSrcweir 	mbHasOs2Table = true;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	// parse the OS/2 raw data
190cdf0e10cSrcweir 	// TODO: also analyze panose info, etc.
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	// check if the fonts needs the "CJK extra leading" heuristic
193cdf0e10cSrcweir 	const unsigned char* pOS2map = &aBuffer[0];
194cdf0e10cSrcweir     const sal_uInt32 nVersion = GetUShort( pOS2map );
195cdf0e10cSrcweir     if( nVersion >= 0x0001 )
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         sal_uInt32 ulUnicodeRange2 = GetUInt( pOS2map + 46 );
198cdf0e10cSrcweir         if( ulUnicodeRange2 & 0x2DF00000 )
199cdf0e10cSrcweir             mbHasCJKSupport = true;
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir void ImplMacFontData::ReadMacCmapEncoding( void ) const
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     // read this only once per font
206cdf0e10cSrcweir     if( mbCmapEncodingRead )
207cdf0e10cSrcweir         return;
208cdf0e10cSrcweir     mbCmapEncodingRead = true;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     ATSFontRef rFont = FMGetATSFontRefFromFont( mnFontId );
211cdf0e10cSrcweir     ByteCount nBufSize = 0;
212cdf0e10cSrcweir     OSStatus eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, 0, NULL, &nBufSize );
213cdf0e10cSrcweir     DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::ReadMacCmapEncoding : ATSFontGetTable1 failed!\n");
214cdf0e10cSrcweir     if( eStatus != noErr )
215cdf0e10cSrcweir         return;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     ByteVector aBuffer( nBufSize );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     ByteCount nRawLength = 0;
220cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, nBufSize, (void*)&aBuffer[0], &nRawLength );
221cdf0e10cSrcweir     DBG_ASSERT( (eStatus==noErr), "ImplMacFontData::ReadMacCmapEncoding : ATSFontGetTable2 failed!\n");
222cdf0e10cSrcweir     if( eStatus != noErr )
223cdf0e10cSrcweir         return;
224cdf0e10cSrcweir     DBG_ASSERT( (nBufSize==nRawLength), "ImplMacFontData::ReadMacCmapEncoding : ByteCount mismatch!\n");
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     const unsigned char* pCmap = &aBuffer[0];
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     if (nRawLength < 24 )
229cdf0e10cSrcweir         return;
230cdf0e10cSrcweir     if( GetUShort( pCmap ) != 0x0000 )
231cdf0e10cSrcweir         return;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     // check if the fonts needs the "CJK extra leading" heuristic
234cdf0e10cSrcweir     int nSubTables = GetUShort( pCmap + 2 );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     for( const unsigned char* p = pCmap + 4; --nSubTables >= 0; p += 8 )
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         int nPlatform = GetUShort( p );
239cdf0e10cSrcweir         if( nPlatform == kFontMacintoshPlatform ) {
240cdf0e10cSrcweir             int nEncoding = GetUShort (p + 2 );
241cdf0e10cSrcweir             if( nEncoding == kFontJapaneseScript ||
242cdf0e10cSrcweir                 nEncoding == kFontTraditionalChineseScript ||
243cdf0e10cSrcweir                 nEncoding == kFontKoreanScript ||
244cdf0e10cSrcweir                 nEncoding == kFontSimpleChineseScript )
245cdf0e10cSrcweir             {
246cdf0e10cSrcweir                 mbHasCJKSupport = true;
247cdf0e10cSrcweir                 break;
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir // -----------------------------------------------------------------------
254cdf0e10cSrcweir 
255cdf0e10cSrcweir bool ImplMacFontData::HasCJKSupport( void ) const
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	ReadOs2Table();
258cdf0e10cSrcweir     if( !mbHasOs2Table )
259cdf0e10cSrcweir         ReadMacCmapEncoding();
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	return mbHasCJKSupport;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir // =======================================================================
265cdf0e10cSrcweir 
266cdf0e10cSrcweir AquaSalGraphics::AquaSalGraphics()
267cdf0e10cSrcweir     : mpFrame( NULL )
268cdf0e10cSrcweir     , mxLayer( NULL )
269cdf0e10cSrcweir     , mrContext( NULL )
270cdf0e10cSrcweir     , mpXorEmulation( NULL )
271cdf0e10cSrcweir     , mnXorMode( 0 )
272cdf0e10cSrcweir     , mnWidth( 0 )
273cdf0e10cSrcweir     , mnHeight( 0 )
274cdf0e10cSrcweir     , mnBitmapDepth( 0 )
275cdf0e10cSrcweir     , mnRealDPIX( 0 )
276cdf0e10cSrcweir     , mnRealDPIY( 0 )
277cdf0e10cSrcweir     , mfFakeDPIScale( 1.0 )
278cdf0e10cSrcweir     , mxClipPath( NULL )
279cdf0e10cSrcweir     , maLineColor( COL_WHITE )
280cdf0e10cSrcweir     , maFillColor( COL_BLACK )
281cdf0e10cSrcweir 	, mpMacFontData( NULL )
282cdf0e10cSrcweir     , mnATSUIRotation( 0 )
283cdf0e10cSrcweir     , mfFontScale( 1.0 )
284cdf0e10cSrcweir     , mfFontStretch( 1.0 )
285cdf0e10cSrcweir     , mbNonAntialiasedText( false )
286cdf0e10cSrcweir     , mbPrinter( false )
287cdf0e10cSrcweir     , mbVirDev( false )
288cdf0e10cSrcweir     , mbWindow( false )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     // create the style object for font attributes
291cdf0e10cSrcweir     ATSUCreateStyle( &maATSUStyle );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir // -----------------------------------------------------------------------
295cdf0e10cSrcweir 
296cdf0e10cSrcweir AquaSalGraphics::~AquaSalGraphics()
297cdf0e10cSrcweir {
298cdf0e10cSrcweir /*
299cdf0e10cSrcweir 	if( mnUpdateGraphicsEvent )
300cdf0e10cSrcweir 	{
301cdf0e10cSrcweir 		Application::RemoveUserEvent( mnUpdateGraphicsEvent );
302cdf0e10cSrcweir 	}
303cdf0e10cSrcweir */
304cdf0e10cSrcweir     CGPathRelease( mxClipPath );
305cdf0e10cSrcweir     ATSUDisposeStyle( maATSUStyle );
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	if( mpXorEmulation )
308cdf0e10cSrcweir 		delete mpXorEmulation;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 	if( mxLayer )
311cdf0e10cSrcweir     	CGLayerRelease( mxLayer );
312cdf0e10cSrcweir 	else if( mrContext && mbWindow )
313cdf0e10cSrcweir 	{
314cdf0e10cSrcweir 		// destroy backbuffer bitmap context that we created ourself
315cdf0e10cSrcweir 		CGContextRelease( mrContext );
316cdf0e10cSrcweir 		mrContext = NULL;
317cdf0e10cSrcweir         // memory is freed automatically by maOwnContextMemory
318cdf0e10cSrcweir 	}
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir bool AquaSalGraphics::supportsOperation( OutDevSupportType eType ) const
322cdf0e10cSrcweir {
323cdf0e10cSrcweir     bool bRet = false;
324cdf0e10cSrcweir     switch( eType )
325cdf0e10cSrcweir     {
326cdf0e10cSrcweir     case OutDevSupport_TransparentRect:
327cdf0e10cSrcweir     case OutDevSupport_B2DClip:
328cdf0e10cSrcweir     case OutDevSupport_B2DDraw:
329cdf0e10cSrcweir         bRet = true;
330cdf0e10cSrcweir         break;
331cdf0e10cSrcweir     default: break;
332cdf0e10cSrcweir     }
333cdf0e10cSrcweir     return bRet;
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir // =======================================================================
337cdf0e10cSrcweir 
338cdf0e10cSrcweir void AquaSalGraphics::updateResolution()
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     DBG_ASSERT( mbWindow, "updateResolution on inappropriate graphics" );
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     initResolution( (mbWindow && mpFrame) ?  mpFrame->mpWindow : nil );
343cdf0e10cSrcweir }
344cdf0e10cSrcweir 
345cdf0e10cSrcweir void AquaSalGraphics::initResolution( NSWindow* )
346cdf0e10cSrcweir {
347cdf0e10cSrcweir     // #i100617# read DPI only once; there is some kind of weird caching going on
348cdf0e10cSrcweir     // if the main screen changes
349cdf0e10cSrcweir     // FIXME: this is really unfortunate and needs to be investigated
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     SalData* pSalData = GetSalData();
352cdf0e10cSrcweir     if( pSalData->mnDPIX == 0 || pSalData->mnDPIY == 0 )
353cdf0e10cSrcweir     {
354cdf0e10cSrcweir         NSScreen* pScreen = nil;
355cdf0e10cSrcweir 
356cdf0e10cSrcweir         /* #i91301#
357cdf0e10cSrcweir         many woes went into the try to have different resolutions
358cdf0e10cSrcweir         on different screens. The result of these trials is that OOo is not ready
359cdf0e10cSrcweir         for that yet, vcl and applications would need to be adapted.
360cdf0e10cSrcweir 
361cdf0e10cSrcweir         Unfortunately this is not possible in the 3.0 timeframe.
362cdf0e10cSrcweir         So let's stay with one resolution for all Windows and VirtualDevices
363cdf0e10cSrcweir         which is the resolution of the main screen
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         This of course also means that measurements are exact only on the main screen.
366cdf0e10cSrcweir         For activating different resolutions again just comment out the two lines below.
367cdf0e10cSrcweir 
368cdf0e10cSrcweir         if( pWin )
369cdf0e10cSrcweir         pScreen = [pWin screen];
370cdf0e10cSrcweir         */
371cdf0e10cSrcweir         if( pScreen == nil )
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             NSArray* pScreens = [NSScreen screens];
374cdf0e10cSrcweir             if( pScreens )
375cdf0e10cSrcweir                 pScreen = [pScreens objectAtIndex: 0];
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir         mnRealDPIX = mnRealDPIY = 96;
379cdf0e10cSrcweir         if( pScreen )
380cdf0e10cSrcweir         {
381cdf0e10cSrcweir             NSDictionary* pDev = [pScreen deviceDescription];
382cdf0e10cSrcweir             if( pDev )
383cdf0e10cSrcweir             {
384cdf0e10cSrcweir                 NSNumber* pVal = [pDev objectForKey: @"NSScreenNumber"];
385cdf0e10cSrcweir                 if( pVal )
386cdf0e10cSrcweir                 {
387cdf0e10cSrcweir                     // FIXME: casting a long to CGDirectDisplayID is evil, but
388cdf0e10cSrcweir                     // Apple suggest to do it this way
389cdf0e10cSrcweir                     const CGDirectDisplayID nDisplayID = (CGDirectDisplayID)[pVal longValue];
390cdf0e10cSrcweir                     const CGSize aSize = CGDisplayScreenSize( nDisplayID ); // => result is in millimeters
391cdf0e10cSrcweir                     mnRealDPIX = static_cast<long>((CGDisplayPixelsWide( nDisplayID ) * 25.4) / aSize.width);
392cdf0e10cSrcweir                     mnRealDPIY = static_cast<long>((CGDisplayPixelsHigh( nDisplayID ) * 25.4) / aSize.height);
393cdf0e10cSrcweir                 }
394cdf0e10cSrcweir                 else
395cdf0e10cSrcweir                 {
396cdf0e10cSrcweir                     DBG_ERROR( "no resolution found in device description" );
397cdf0e10cSrcweir                 }
398cdf0e10cSrcweir             }
399cdf0e10cSrcweir             else
400cdf0e10cSrcweir             {
401cdf0e10cSrcweir                 DBG_ERROR( "no device description" );
402cdf0e10cSrcweir             }
403cdf0e10cSrcweir         }
404cdf0e10cSrcweir         else
405cdf0e10cSrcweir         {
406cdf0e10cSrcweir             DBG_ERROR( "no screen found" );
407cdf0e10cSrcweir         }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir         // #i107076# maintaining size-WYSIWYG-ness causes many problems for
410cdf0e10cSrcweir         //           low-DPI, high-DPI or for mis-reporting devices
411cdf0e10cSrcweir         //           => it is better to limit the calculation result then
412cdf0e10cSrcweir         static const int nMinDPI = 72;
413cdf0e10cSrcweir         if( (mnRealDPIX < nMinDPI) || (mnRealDPIY < nMinDPI) )
414cdf0e10cSrcweir             mnRealDPIX = mnRealDPIY = nMinDPI;
415cdf0e10cSrcweir         static const int nMaxDPI = 200;
416cdf0e10cSrcweir         if( (mnRealDPIX > nMaxDPI) || (mnRealDPIY > nMaxDPI) )
417cdf0e10cSrcweir             mnRealDPIX = mnRealDPIY = nMaxDPI;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir         // for OSX any anisotropy reported for the display resolution is best ignored (e.g. TripleHead2Go)
420cdf0e10cSrcweir         mnRealDPIX = mnRealDPIY = (mnRealDPIX + mnRealDPIY + 1) / 2;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir         pSalData->mnDPIX = mnRealDPIX;
423cdf0e10cSrcweir         pSalData->mnDPIY = mnRealDPIY;
424cdf0e10cSrcweir     }
425cdf0e10cSrcweir     else
426cdf0e10cSrcweir     {
427cdf0e10cSrcweir         mnRealDPIX = pSalData->mnDPIX;
428cdf0e10cSrcweir         mnRealDPIY = pSalData->mnDPIY;
429cdf0e10cSrcweir     }
430cdf0e10cSrcweir 
431cdf0e10cSrcweir     mfFakeDPIScale = 1.0;
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir void AquaSalGraphics::GetResolution( long& rDPIX, long& rDPIY )
435cdf0e10cSrcweir {
436cdf0e10cSrcweir     if( !mnRealDPIY )
437cdf0e10cSrcweir         initResolution( (mbWindow && mpFrame) ? mpFrame->mpWindow : nil );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     rDPIX = static_cast<long>(mfFakeDPIScale * mnRealDPIX);
440cdf0e10cSrcweir     rDPIY = static_cast<long>(mfFakeDPIScale * mnRealDPIY);
441cdf0e10cSrcweir }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir void AquaSalGraphics::copyResolution( AquaSalGraphics& rGraphics )
444cdf0e10cSrcweir {
445cdf0e10cSrcweir 	if( !rGraphics.mnRealDPIY && rGraphics.mbWindow && rGraphics.mpFrame )
446cdf0e10cSrcweir 		rGraphics.initResolution( rGraphics.mpFrame->mpWindow );
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 	mnRealDPIX = rGraphics.mnRealDPIX;
449cdf0e10cSrcweir 	mnRealDPIY = rGraphics.mnRealDPIY;
450cdf0e10cSrcweir 	mfFakeDPIScale = rGraphics.mfFakeDPIScale;
451cdf0e10cSrcweir }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir // -----------------------------------------------------------------------
454cdf0e10cSrcweir 
455cdf0e10cSrcweir sal_uInt16 AquaSalGraphics::GetBitCount()
456cdf0e10cSrcweir {
457cdf0e10cSrcweir     sal_uInt16 nBits = mnBitmapDepth ? mnBitmapDepth : 32;//24;
458cdf0e10cSrcweir     return nBits;
459cdf0e10cSrcweir }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir // -----------------------------------------------------------------------
462cdf0e10cSrcweir 
463cdf0e10cSrcweir static const basegfx::B2DPoint aHalfPointOfs ( 0.5, 0.5 );
464cdf0e10cSrcweir 
465cdf0e10cSrcweir static void AddPolygonToPath( CGMutablePathRef xPath,
466cdf0e10cSrcweir 	const ::basegfx::B2DPolygon& rPolygon, bool bClosePath, bool bPixelSnap, bool bLineDraw )
467cdf0e10cSrcweir {
468cdf0e10cSrcweir 	// short circuit if there is nothing to do
469cdf0e10cSrcweir 	const int nPointCount = rPolygon.count();
470cdf0e10cSrcweir 	if( nPointCount <= 0 )
471cdf0e10cSrcweir 		return;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir 	(void)bPixelSnap; // TODO
474cdf0e10cSrcweir 	const CGAffineTransform* pTransform = NULL;
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	const bool bHasCurves = rPolygon.areControlPointsUsed();
477cdf0e10cSrcweir 	for( int nPointIdx = 0, nPrevIdx = 0;; nPrevIdx = nPointIdx++ )
478cdf0e10cSrcweir 	{
479cdf0e10cSrcweir 		int nClosedIdx = nPointIdx;
480cdf0e10cSrcweir 		if( nPointIdx >= nPointCount )
481cdf0e10cSrcweir 		{
482cdf0e10cSrcweir 			// prepare to close last curve segment if needed
483cdf0e10cSrcweir 			if( bClosePath && (nPointIdx == nPointCount) )
484cdf0e10cSrcweir 				nClosedIdx = 0;
485cdf0e10cSrcweir 			else
486cdf0e10cSrcweir 				break;
487cdf0e10cSrcweir 		}
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 		::basegfx::B2DPoint aPoint = rPolygon.getB2DPoint( nClosedIdx );
490cdf0e10cSrcweir 
491cdf0e10cSrcweir 		if( bPixelSnap)
492cdf0e10cSrcweir 		{
493cdf0e10cSrcweir 			// snap device coordinates to full pixels
494cdf0e10cSrcweir 			aPoint.setX( basegfx::fround( aPoint.getX() ) );
495cdf0e10cSrcweir 			aPoint.setY( basegfx::fround( aPoint.getY() ) );
496cdf0e10cSrcweir 		}
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 		if( bLineDraw )
499cdf0e10cSrcweir 			aPoint += aHalfPointOfs;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir 		if( !nPointIdx ) { // first point => just move there
502cdf0e10cSrcweir 			CGPathMoveToPoint( xPath, pTransform, aPoint.getX(), aPoint.getY() );
503cdf0e10cSrcweir 			continue;
504cdf0e10cSrcweir 		}
505cdf0e10cSrcweir 
506cdf0e10cSrcweir 		bool bPendingCurve = false;
507cdf0e10cSrcweir 		if( bHasCurves )
508cdf0e10cSrcweir 		{
509cdf0e10cSrcweir 			bPendingCurve = rPolygon.isNextControlPointUsed( nPrevIdx );
510cdf0e10cSrcweir 			bPendingCurve |= rPolygon.isPrevControlPointUsed( nClosedIdx );
511cdf0e10cSrcweir 		}
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 		if( !bPendingCurve )	// line segment
514cdf0e10cSrcweir 			CGPathAddLineToPoint( xPath, pTransform, aPoint.getX(), aPoint.getY() );
515cdf0e10cSrcweir 		else						// cubic bezier segment
516cdf0e10cSrcweir 		{
517cdf0e10cSrcweir 			basegfx::B2DPoint aCP1 = rPolygon.getNextControlPoint( nPrevIdx );
518cdf0e10cSrcweir 			basegfx::B2DPoint aCP2 = rPolygon.getPrevControlPoint( nClosedIdx );
519cdf0e10cSrcweir 			if( bLineDraw )
520cdf0e10cSrcweir 			{
521cdf0e10cSrcweir 				aCP1 += aHalfPointOfs;
522cdf0e10cSrcweir 				aCP2 += aHalfPointOfs;
523cdf0e10cSrcweir 			}
524cdf0e10cSrcweir 			CGPathAddCurveToPoint( xPath, pTransform, aCP1.getX(), aCP1.getY(),
525cdf0e10cSrcweir 				aCP2.getX(), aCP2.getY(), aPoint.getX(), aPoint.getY() );
526cdf0e10cSrcweir 		}
527cdf0e10cSrcweir 	}
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 	if( bClosePath )
530cdf0e10cSrcweir 		CGPathCloseSubpath( xPath );
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir static void AddPolyPolygonToPath( CGMutablePathRef xPath,
534cdf0e10cSrcweir 	const ::basegfx::B2DPolyPolygon& rPolyPoly, bool bPixelSnap, bool bLineDraw )
535cdf0e10cSrcweir {
536cdf0e10cSrcweir 	// short circuit if there is nothing to do
537cdf0e10cSrcweir 	const int nPolyCount = rPolyPoly.count();
538cdf0e10cSrcweir 	if( nPolyCount <= 0 )
539cdf0e10cSrcweir 		return;
540cdf0e10cSrcweir 
541cdf0e10cSrcweir 	for( int nPolyIdx = 0; nPolyIdx < nPolyCount; ++nPolyIdx )
542cdf0e10cSrcweir 	{
543cdf0e10cSrcweir 		const ::basegfx::B2DPolygon rPolygon = rPolyPoly.getB2DPolygon( nPolyIdx );
544cdf0e10cSrcweir 		AddPolygonToPath( xPath, rPolygon, true, bPixelSnap, bLineDraw );
545cdf0e10cSrcweir 	}
546cdf0e10cSrcweir }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir // -----------------------------------------------------------------------
549cdf0e10cSrcweir 
550cdf0e10cSrcweir void AquaSalGraphics::ResetClipRegion()
551cdf0e10cSrcweir {
552cdf0e10cSrcweir     // release old path and indicate no clipping
553cdf0e10cSrcweir     if( mxClipPath )
554cdf0e10cSrcweir     {
555cdf0e10cSrcweir         CGPathRelease( mxClipPath );
556cdf0e10cSrcweir         mxClipPath = NULL;
557cdf0e10cSrcweir     }
558cdf0e10cSrcweir     if( CheckContext() )
559cdf0e10cSrcweir         SetState();
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir // -----------------------------------------------------------------------
563cdf0e10cSrcweir 
564cdf0e10cSrcweir bool AquaSalGraphics::setClipRegion( const Region& i_rClip )
565cdf0e10cSrcweir {
566cdf0e10cSrcweir     // release old clip path
567cdf0e10cSrcweir     if( mxClipPath )
568cdf0e10cSrcweir     {
569cdf0e10cSrcweir         CGPathRelease( mxClipPath );
570cdf0e10cSrcweir         mxClipPath = NULL;
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir     mxClipPath = CGPathCreateMutable();
573cdf0e10cSrcweir 
574cdf0e10cSrcweir     // set current path, either as polypolgon or sequence of rectangles
575cdf0e10cSrcweir     if( i_rClip.HasPolyPolygon() )
576cdf0e10cSrcweir     {
577cdf0e10cSrcweir         basegfx::B2DPolyPolygon aClip( const_cast<Region&>(i_rClip).ConvertToB2DPolyPolygon() );
578cdf0e10cSrcweir         AddPolyPolygonToPath( mxClipPath, aClip, !getAntiAliasB2DDraw(), false );
579cdf0e10cSrcweir     }
580cdf0e10cSrcweir     else
581cdf0e10cSrcweir     {
582cdf0e10cSrcweir         long nX, nY, nW, nH;
583cdf0e10cSrcweir         ImplRegionInfo aInfo;
584cdf0e10cSrcweir         bool bRegionRect = i_rClip.ImplGetFirstRect(aInfo, nX, nY, nW, nH );
585cdf0e10cSrcweir         while( bRegionRect )
586cdf0e10cSrcweir         {
587cdf0e10cSrcweir             if( nW && nH )
588cdf0e10cSrcweir             {
589cdf0e10cSrcweir                 CGRect aRect = {{nX,nY}, {nW,nH}};
590cdf0e10cSrcweir                 CGPathAddRect( mxClipPath, NULL, aRect );
591cdf0e10cSrcweir             }
592cdf0e10cSrcweir             bRegionRect = i_rClip.ImplGetNextRect( aInfo, nX, nY, nW, nH );
593cdf0e10cSrcweir         }
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir     // set the current path as clip region
596cdf0e10cSrcweir 	if( CheckContext() )
597cdf0e10cSrcweir 	    SetState();
598cdf0e10cSrcweir 	return true;
599cdf0e10cSrcweir }
600cdf0e10cSrcweir 
601cdf0e10cSrcweir // -----------------------------------------------------------------------
602cdf0e10cSrcweir 
603cdf0e10cSrcweir void AquaSalGraphics::SetLineColor()
604cdf0e10cSrcweir {
605cdf0e10cSrcweir     maLineColor.SetAlpha( 0.0 );   // transparent
606cdf0e10cSrcweir     if( CheckContext() )
607cdf0e10cSrcweir         CGContextSetStrokeColor( mrContext, maLineColor.AsArray() );
608cdf0e10cSrcweir }
609cdf0e10cSrcweir 
610cdf0e10cSrcweir // -----------------------------------------------------------------------
611cdf0e10cSrcweir 
612cdf0e10cSrcweir void AquaSalGraphics::SetLineColor( SalColor nSalColor )
613cdf0e10cSrcweir {
614cdf0e10cSrcweir 	maLineColor = RGBAColor( nSalColor );
615cdf0e10cSrcweir     if( CheckContext() )
616cdf0e10cSrcweir         CGContextSetStrokeColor( mrContext, maLineColor.AsArray() );
617cdf0e10cSrcweir }
618cdf0e10cSrcweir 
619cdf0e10cSrcweir // -----------------------------------------------------------------------
620cdf0e10cSrcweir 
621cdf0e10cSrcweir void AquaSalGraphics::SetFillColor()
622cdf0e10cSrcweir {
623cdf0e10cSrcweir     maFillColor.SetAlpha( 0.0 );   // transparent
624cdf0e10cSrcweir     if( CheckContext() )
625cdf0e10cSrcweir         CGContextSetFillColor( mrContext, maFillColor.AsArray() );
626cdf0e10cSrcweir }
627cdf0e10cSrcweir 
628cdf0e10cSrcweir // -----------------------------------------------------------------------
629cdf0e10cSrcweir 
630cdf0e10cSrcweir void AquaSalGraphics::SetFillColor( SalColor nSalColor )
631cdf0e10cSrcweir {
632cdf0e10cSrcweir 	maFillColor = RGBAColor( nSalColor );
633cdf0e10cSrcweir     if( CheckContext() )
634cdf0e10cSrcweir         CGContextSetFillColor( mrContext, maFillColor.AsArray() );
635cdf0e10cSrcweir }
636cdf0e10cSrcweir 
637cdf0e10cSrcweir // -----------------------------------------------------------------------
638cdf0e10cSrcweir 
639cdf0e10cSrcweir static SalColor ImplGetROPSalColor( SalROPColor nROPColor )
640cdf0e10cSrcweir {
641cdf0e10cSrcweir 	SalColor nSalColor;
642cdf0e10cSrcweir 	if ( nROPColor == SAL_ROP_0 )
643cdf0e10cSrcweir 		nSalColor = MAKE_SALCOLOR( 0, 0, 0 );
644cdf0e10cSrcweir 	else
645cdf0e10cSrcweir 		nSalColor = MAKE_SALCOLOR( 255, 255, 255 );
646cdf0e10cSrcweir 	return nSalColor;
647cdf0e10cSrcweir }
648cdf0e10cSrcweir 
649cdf0e10cSrcweir void AquaSalGraphics::SetROPLineColor( SalROPColor nROPColor )
650cdf0e10cSrcweir {
651cdf0e10cSrcweir     if( ! mbPrinter )
652cdf0e10cSrcweir         SetLineColor( ImplGetROPSalColor( nROPColor ) );
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
655cdf0e10cSrcweir // -----------------------------------------------------------------------
656cdf0e10cSrcweir 
657cdf0e10cSrcweir void AquaSalGraphics::SetROPFillColor( SalROPColor nROPColor )
658cdf0e10cSrcweir {
659cdf0e10cSrcweir     if( ! mbPrinter )
660cdf0e10cSrcweir         SetFillColor( ImplGetROPSalColor( nROPColor ) );
661cdf0e10cSrcweir }
662cdf0e10cSrcweir 
663cdf0e10cSrcweir // -----------------------------------------------------------------------
664cdf0e10cSrcweir 
665cdf0e10cSrcweir void AquaSalGraphics::ImplDrawPixel( long nX, long nY, const RGBAColor& rColor )
666cdf0e10cSrcweir {
667cdf0e10cSrcweir 	if( !CheckContext() )
668cdf0e10cSrcweir 		return;
669cdf0e10cSrcweir 
670cdf0e10cSrcweir 	// overwrite the fill color
671cdf0e10cSrcweir 	CGContextSetFillColor( mrContext, rColor.AsArray() );
672cdf0e10cSrcweir 	// draw 1x1 rect, there is no pixel drawing in Quartz
673cdf0e10cSrcweir 	CGRect aDstRect = {{nX,nY,},{1,1}};
674cdf0e10cSrcweir 	CGContextFillRect( mrContext, aDstRect );
675cdf0e10cSrcweir     RefreshRect( aDstRect );
676cdf0e10cSrcweir     // reset the fill color
677cdf0e10cSrcweir 	CGContextSetFillColor( mrContext, maFillColor.AsArray() );
678cdf0e10cSrcweir }
679cdf0e10cSrcweir 
680cdf0e10cSrcweir void AquaSalGraphics::drawPixel( long nX, long nY )
681cdf0e10cSrcweir {
682cdf0e10cSrcweir     // draw pixel with current line color
683cdf0e10cSrcweir     ImplDrawPixel( nX, nY, maLineColor );
684cdf0e10cSrcweir }
685cdf0e10cSrcweir 
686cdf0e10cSrcweir void AquaSalGraphics::drawPixel( long nX, long nY, SalColor nSalColor )
687cdf0e10cSrcweir {
688cdf0e10cSrcweir 	const RGBAColor aPixelColor( nSalColor );
689cdf0e10cSrcweir     ImplDrawPixel( nX, nY, aPixelColor );
690cdf0e10cSrcweir }
691cdf0e10cSrcweir 
692cdf0e10cSrcweir // -----------------------------------------------------------------------
693cdf0e10cSrcweir 
694cdf0e10cSrcweir void AquaSalGraphics::drawLine( long nX1, long nY1, long nX2, long nY2 )
695cdf0e10cSrcweir {
696cdf0e10cSrcweir     if( nX1 == nX2 && nY1 == nY2 )
697cdf0e10cSrcweir     {
698cdf0e10cSrcweir         // #i109453# platform independent code expects at least one pixel to be drawn
699cdf0e10cSrcweir         drawPixel( nX1, nY1 );
700cdf0e10cSrcweir         return;
701cdf0e10cSrcweir     }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir 	if( !CheckContext() )
704cdf0e10cSrcweir 		return;
705cdf0e10cSrcweir 
706cdf0e10cSrcweir 	CGContextBeginPath( mrContext );
707cdf0e10cSrcweir 	CGContextMoveToPoint( mrContext, static_cast<float>(nX1)+0.5, static_cast<float>(nY1)+0.5 );
708cdf0e10cSrcweir 	CGContextAddLineToPoint( mrContext, static_cast<float>(nX2)+0.5, static_cast<float>(nY2)+0.5 );
709cdf0e10cSrcweir 	CGContextDrawPath( mrContext, kCGPathStroke );
710cdf0e10cSrcweir 
711cdf0e10cSrcweir     Rectangle aRefreshRect( nX1, nY1, nX2, nY2 );
712cdf0e10cSrcweir }
713cdf0e10cSrcweir 
714cdf0e10cSrcweir // -----------------------------------------------------------------------
715cdf0e10cSrcweir 
716cdf0e10cSrcweir void AquaSalGraphics::drawRect( long nX, long nY, long nWidth, long nHeight )
717cdf0e10cSrcweir {
718cdf0e10cSrcweir 	if( !CheckContext() )
719cdf0e10cSrcweir 		return;
720cdf0e10cSrcweir 
721cdf0e10cSrcweir 	 CGRect aRect( CGRectMake(nX, nY, nWidth, nHeight) );
722cdf0e10cSrcweir 	 if( IsPenVisible() )
723cdf0e10cSrcweir 	 {
724cdf0e10cSrcweir 	     aRect.origin.x      += 0.5;
725cdf0e10cSrcweir 	     aRect.origin.y      += 0.5;
726cdf0e10cSrcweir 	     aRect.size.width    -= 1;
727cdf0e10cSrcweir 	     aRect.size.height -= 1;
728cdf0e10cSrcweir 	 }
729cdf0e10cSrcweir 
730cdf0e10cSrcweir 	 if( IsBrushVisible() )
731cdf0e10cSrcweir 	     CGContextFillRect( mrContext, aRect );
732cdf0e10cSrcweir 
733cdf0e10cSrcweir 	 if( IsPenVisible() )
734cdf0e10cSrcweir 	     CGContextStrokeRect( mrContext, aRect );
735cdf0e10cSrcweir 
736cdf0e10cSrcweir 	RefreshRect( nX, nY, nWidth, nHeight );
737cdf0e10cSrcweir }
738cdf0e10cSrcweir 
739cdf0e10cSrcweir // -----------------------------------------------------------------------
740cdf0e10cSrcweir 
741cdf0e10cSrcweir static void getBoundRect( sal_uLong nPoints, const SalPoint *pPtAry, long &rX, long& rY, long& rWidth, long& rHeight )
742cdf0e10cSrcweir {
743cdf0e10cSrcweir     long nX1 = pPtAry->mnX;
744cdf0e10cSrcweir     long nX2 = nX1;
745cdf0e10cSrcweir     long nY1 = pPtAry->mnY;
746cdf0e10cSrcweir     long nY2 = nY1;
747cdf0e10cSrcweir     for( sal_uLong n = 1; n < nPoints; n++ )
748cdf0e10cSrcweir     {
749cdf0e10cSrcweir         if( pPtAry[n].mnX < nX1 )
750cdf0e10cSrcweir             nX1 = pPtAry[n].mnX;
751cdf0e10cSrcweir         else if( pPtAry[n].mnX > nX2 )
752cdf0e10cSrcweir             nX2 = pPtAry[n].mnX;
753cdf0e10cSrcweir 
754cdf0e10cSrcweir         if( pPtAry[n].mnY < nY1 )
755cdf0e10cSrcweir             nY1 = pPtAry[n].mnY;
756cdf0e10cSrcweir         else if( pPtAry[n].mnY > nY2 )
757cdf0e10cSrcweir             nY2 = pPtAry[n].mnY;
758cdf0e10cSrcweir     }
759cdf0e10cSrcweir     rX = nX1;
760cdf0e10cSrcweir     rY = nY1;
761cdf0e10cSrcweir     rWidth = nX2 - nX1 + 1;
762cdf0e10cSrcweir     rHeight = nY2 - nY1 + 1;
763cdf0e10cSrcweir }
764cdf0e10cSrcweir 
765cdf0e10cSrcweir static inline void alignLinePoint( const SalPoint* i_pIn, float& o_fX, float& o_fY )
766cdf0e10cSrcweir {
767cdf0e10cSrcweir     o_fX = static_cast<float>(i_pIn->mnX ) + 0.5;
768cdf0e10cSrcweir     o_fY = static_cast<float>(i_pIn->mnY ) + 0.5;
769cdf0e10cSrcweir }
770cdf0e10cSrcweir 
771cdf0e10cSrcweir void AquaSalGraphics::drawPolyLine( sal_uLong nPoints, const SalPoint *pPtAry )
772cdf0e10cSrcweir {
773cdf0e10cSrcweir 	if( nPoints < 1 )
774cdf0e10cSrcweir 		return;
775cdf0e10cSrcweir 	if( !CheckContext() )
776cdf0e10cSrcweir 		return;
777cdf0e10cSrcweir 
778cdf0e10cSrcweir 	long nX = 0, nY = 0, nWidth = 0, nHeight = 0;
779cdf0e10cSrcweir 	getBoundRect( nPoints, pPtAry, nX, nY, nWidth, nHeight );
780cdf0e10cSrcweir 
781cdf0e10cSrcweir 	float fX, fY;
782cdf0e10cSrcweir 
783cdf0e10cSrcweir 	CGContextBeginPath( mrContext );
784cdf0e10cSrcweir 	alignLinePoint( pPtAry, fX, fY );
785cdf0e10cSrcweir 	CGContextMoveToPoint( mrContext, fX, fY );
786cdf0e10cSrcweir 	pPtAry++;
787cdf0e10cSrcweir 	for( sal_uLong nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
788cdf0e10cSrcweir 	{
789cdf0e10cSrcweir 	    alignLinePoint( pPtAry, fX, fY );
790cdf0e10cSrcweir 	    CGContextAddLineToPoint( mrContext, fX, fY );
791cdf0e10cSrcweir 	}
792cdf0e10cSrcweir 	CGContextDrawPath( mrContext, kCGPathStroke );
793cdf0e10cSrcweir 
794cdf0e10cSrcweir 	RefreshRect( nX, nY, nWidth, nHeight );
795cdf0e10cSrcweir }
796cdf0e10cSrcweir 
797cdf0e10cSrcweir // -----------------------------------------------------------------------
798cdf0e10cSrcweir 
799cdf0e10cSrcweir void AquaSalGraphics::drawPolygon( sal_uLong nPoints, const SalPoint *pPtAry )
800cdf0e10cSrcweir {
801cdf0e10cSrcweir 	if( nPoints <= 1 )
802cdf0e10cSrcweir 		return;
803cdf0e10cSrcweir 	if( !CheckContext() )
804cdf0e10cSrcweir 		return;
805cdf0e10cSrcweir 
806cdf0e10cSrcweir 	long nX = 0, nY = 0, nWidth = 0, nHeight = 0;
807cdf0e10cSrcweir 	getBoundRect( nPoints, pPtAry, nX, nY, nWidth, nHeight );
808cdf0e10cSrcweir 
809cdf0e10cSrcweir     CGPathDrawingMode eMode;
810cdf0e10cSrcweir     if( IsBrushVisible() && IsPenVisible() )
811cdf0e10cSrcweir         eMode = kCGPathEOFillStroke;
812cdf0e10cSrcweir     else if( IsPenVisible() )
813cdf0e10cSrcweir         eMode = kCGPathStroke;
814cdf0e10cSrcweir     else if( IsBrushVisible() )
815cdf0e10cSrcweir         eMode = kCGPathEOFill;
816cdf0e10cSrcweir     else
817cdf0e10cSrcweir         return;
818cdf0e10cSrcweir 
819cdf0e10cSrcweir 	CGContextBeginPath( mrContext );
820cdf0e10cSrcweir 
821cdf0e10cSrcweir     if( IsPenVisible() )
822cdf0e10cSrcweir     {
823cdf0e10cSrcweir         float fX, fY;
824cdf0e10cSrcweir         alignLinePoint( pPtAry, fX, fY );
825cdf0e10cSrcweir         CGContextMoveToPoint( mrContext, fX, fY );
826cdf0e10cSrcweir         pPtAry++;
827cdf0e10cSrcweir         for( sal_uLong nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
828cdf0e10cSrcweir         {
829cdf0e10cSrcweir             alignLinePoint( pPtAry, fX, fY );
830cdf0e10cSrcweir             CGContextAddLineToPoint( mrContext, fX, fY );
831cdf0e10cSrcweir         }
832cdf0e10cSrcweir     }
833cdf0e10cSrcweir     else
834cdf0e10cSrcweir     {
835cdf0e10cSrcweir         CGContextMoveToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
836cdf0e10cSrcweir         pPtAry++;
837cdf0e10cSrcweir         for( sal_uLong nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
838cdf0e10cSrcweir             CGContextAddLineToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
839cdf0e10cSrcweir     }
840cdf0e10cSrcweir 
841cdf0e10cSrcweir     CGContextDrawPath( mrContext, eMode );
842cdf0e10cSrcweir 	RefreshRect( nX, nY, nWidth, nHeight );
843cdf0e10cSrcweir }
844cdf0e10cSrcweir 
845cdf0e10cSrcweir // -----------------------------------------------------------------------
846cdf0e10cSrcweir 
847cdf0e10cSrcweir void AquaSalGraphics::drawPolyPolygon( sal_uLong nPolyCount, const sal_uLong *pPoints, PCONSTSALPOINT  *ppPtAry )
848cdf0e10cSrcweir {
849cdf0e10cSrcweir     if( nPolyCount <= 0 )
850cdf0e10cSrcweir 		return;
851cdf0e10cSrcweir     if( !CheckContext() )
852cdf0e10cSrcweir 		return;
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 	// find bound rect
855cdf0e10cSrcweir 	long leftX = 0, topY = 0, maxWidth = 0, maxHeight = 0;
856cdf0e10cSrcweir 	getBoundRect( pPoints[0], ppPtAry[0], leftX, topY, maxWidth, maxHeight );
857cdf0e10cSrcweir 	for( sal_uLong n = 1; n < nPolyCount; n++ )
858cdf0e10cSrcweir 	{
859cdf0e10cSrcweir 	    long nX = leftX, nY = topY, nW = maxWidth, nH = maxHeight;
860cdf0e10cSrcweir 	    getBoundRect( pPoints[n], ppPtAry[n], nX, nY, nW, nH );
861cdf0e10cSrcweir 	    if( nX < leftX )
862cdf0e10cSrcweir 	    {
863cdf0e10cSrcweir 	        maxWidth += leftX - nX;
864cdf0e10cSrcweir 	        leftX = nX;
865cdf0e10cSrcweir 	    }
866cdf0e10cSrcweir 	    if( nY < topY )
867cdf0e10cSrcweir 	    {
868cdf0e10cSrcweir 	        maxHeight += topY - nY;
869cdf0e10cSrcweir 	        topY = nY;
870cdf0e10cSrcweir 	    }
871cdf0e10cSrcweir 	    if( nX + nW > leftX + maxWidth )
872cdf0e10cSrcweir 	        maxWidth = nX + nW - leftX;
873cdf0e10cSrcweir 	    if( nY + nH > topY + maxHeight )
874cdf0e10cSrcweir 	        maxHeight = nY + nH - topY;
875cdf0e10cSrcweir 	}
876cdf0e10cSrcweir 
877cdf0e10cSrcweir 	// prepare drawing mode
878cdf0e10cSrcweir 	CGPathDrawingMode eMode;
879cdf0e10cSrcweir 	if( IsBrushVisible() && IsPenVisible() )
880cdf0e10cSrcweir 	    eMode = kCGPathEOFillStroke;
881cdf0e10cSrcweir 	else if( IsPenVisible() )
882cdf0e10cSrcweir 	    eMode = kCGPathStroke;
883cdf0e10cSrcweir 	else if( IsBrushVisible() )
884cdf0e10cSrcweir 	    eMode = kCGPathEOFill;
885cdf0e10cSrcweir 	else
886cdf0e10cSrcweir 	    return;
887cdf0e10cSrcweir 
888cdf0e10cSrcweir 	// convert to CGPath
889cdf0e10cSrcweir 	CGContextBeginPath( mrContext );
890cdf0e10cSrcweir 	if( IsPenVisible() )
891cdf0e10cSrcweir 	{
892cdf0e10cSrcweir 	    for( sal_uLong nPoly = 0; nPoly < nPolyCount; nPoly++ )
893cdf0e10cSrcweir 	    {
894cdf0e10cSrcweir 	        const sal_uLong nPoints = pPoints[nPoly];
895cdf0e10cSrcweir 	        if( nPoints > 1 )
896cdf0e10cSrcweir 	        {
897cdf0e10cSrcweir 	            const SalPoint *pPtAry = ppPtAry[nPoly];
898cdf0e10cSrcweir 	            float fX, fY;
899cdf0e10cSrcweir 	            alignLinePoint( pPtAry, fX, fY );
900cdf0e10cSrcweir 	            CGContextMoveToPoint( mrContext, fX, fY );
901cdf0e10cSrcweir 	            pPtAry++;
902cdf0e10cSrcweir 	            for( sal_uLong nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
903cdf0e10cSrcweir 	            {
904cdf0e10cSrcweir 	                alignLinePoint( pPtAry, fX, fY );
905cdf0e10cSrcweir 	                CGContextAddLineToPoint( mrContext, fX, fY );
906cdf0e10cSrcweir 	            }
907cdf0e10cSrcweir 	            CGContextClosePath(mrContext);
908cdf0e10cSrcweir 	        }
909cdf0e10cSrcweir 	    }
910cdf0e10cSrcweir 	}
911cdf0e10cSrcweir 	else
912cdf0e10cSrcweir 	{
913cdf0e10cSrcweir 	    for( sal_uLong nPoly = 0; nPoly < nPolyCount; nPoly++ )
914cdf0e10cSrcweir 	    {
915cdf0e10cSrcweir 	        const sal_uLong nPoints = pPoints[nPoly];
916cdf0e10cSrcweir 	        if( nPoints > 1 )
917cdf0e10cSrcweir 	        {
918cdf0e10cSrcweir 	            const SalPoint *pPtAry = ppPtAry[nPoly];
919cdf0e10cSrcweir 	            CGContextMoveToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
920cdf0e10cSrcweir 	            pPtAry++;
921cdf0e10cSrcweir 	            for( sal_uLong nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
922cdf0e10cSrcweir 	                CGContextAddLineToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
923cdf0e10cSrcweir 	            CGContextClosePath(mrContext);
924cdf0e10cSrcweir 	        }
925cdf0e10cSrcweir 	    }
926cdf0e10cSrcweir 	}
927cdf0e10cSrcweir 
928cdf0e10cSrcweir 	CGContextDrawPath( mrContext, eMode );
929cdf0e10cSrcweir 
930cdf0e10cSrcweir 	RefreshRect( leftX, topY, maxWidth, maxHeight );
931cdf0e10cSrcweir }
932cdf0e10cSrcweir 
933cdf0e10cSrcweir // -----------------------------------------------------------------------
934cdf0e10cSrcweir 
935cdf0e10cSrcweir bool AquaSalGraphics::drawPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly,
936cdf0e10cSrcweir 	double fTransparency )
937cdf0e10cSrcweir {
938cdf0e10cSrcweir 	// short circuit if there is nothing to do
939cdf0e10cSrcweir 	const int nPolyCount = rPolyPoly.count();
940cdf0e10cSrcweir 	if( nPolyCount <= 0 )
941cdf0e10cSrcweir 		return true;
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 	// ignore invisible polygons
944cdf0e10cSrcweir 	if( (fTransparency >= 1.0) || (fTransparency < 0) )
945cdf0e10cSrcweir 		return true;
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 	// setup poly-polygon path
948cdf0e10cSrcweir 	CGMutablePathRef xPath = CGPathCreateMutable();
949cdf0e10cSrcweir 	for( int nPolyIdx = 0; nPolyIdx < nPolyCount; ++nPolyIdx )
950cdf0e10cSrcweir 	{
951cdf0e10cSrcweir 		const ::basegfx::B2DPolygon rPolygon = rPolyPoly.getB2DPolygon( nPolyIdx );
952cdf0e10cSrcweir 		AddPolygonToPath( xPath, rPolygon, true, !getAntiAliasB2DDraw(), IsPenVisible() );
953cdf0e10cSrcweir 	}
954cdf0e10cSrcweir 
955cdf0e10cSrcweir 	const CGRect aRefreshRect = CGPathGetBoundingBox( xPath );
956cdf0e10cSrcweir #ifndef NO_I97317_WORKAROUND
957cdf0e10cSrcweir 	// #i97317# workaround for Quartz having problems with drawing small polygons
958cdf0e10cSrcweir 	if( ! ((aRefreshRect.size.width <= 0.125) && (aRefreshRect.size.height <= 0.125)) )
959cdf0e10cSrcweir #endif
960cdf0e10cSrcweir     {
961cdf0e10cSrcweir         // use the path to prepare the graphics context
962cdf0e10cSrcweir         CGContextSaveGState( mrContext );
963cdf0e10cSrcweir         CGContextBeginPath( mrContext );
964cdf0e10cSrcweir         CGContextAddPath( mrContext, xPath );
965cdf0e10cSrcweir 
966cdf0e10cSrcweir         // draw path with antialiased polygon
967cdf0e10cSrcweir         CGContextSetShouldAntialias( mrContext, true );
968cdf0e10cSrcweir         CGContextSetAlpha( mrContext, 1.0 - fTransparency );
969cdf0e10cSrcweir         CGContextDrawPath( mrContext, kCGPathEOFillStroke );
970cdf0e10cSrcweir         CGContextRestoreGState( mrContext );
971cdf0e10cSrcweir 
972cdf0e10cSrcweir         // mark modified rectangle as updated
973cdf0e10cSrcweir         RefreshRect( aRefreshRect );
974cdf0e10cSrcweir     }
975cdf0e10cSrcweir 
976cdf0e10cSrcweir 	CGPathRelease( xPath );
977cdf0e10cSrcweir 
978cdf0e10cSrcweir 	return true;
979cdf0e10cSrcweir }
980cdf0e10cSrcweir 
981cdf0e10cSrcweir // -----------------------------------------------------------------------
982cdf0e10cSrcweir 
983*5aaf853bSArmin Le Grand bool AquaSalGraphics::drawPolyLine(
984*5aaf853bSArmin Le Grand     const ::basegfx::B2DPolygon& rPolyLine,
985cdf0e10cSrcweir 	double fTransparency,
986cdf0e10cSrcweir 	const ::basegfx::B2DVector& rLineWidths,
987*5aaf853bSArmin Le Grand 	basegfx::B2DLineJoin eLineJoin,
988*5aaf853bSArmin Le Grand     com::sun::star::drawing::LineCap eLineCap)
989cdf0e10cSrcweir {
990cdf0e10cSrcweir 	// short circuit if there is nothing to do
991cdf0e10cSrcweir 	const int nPointCount = rPolyLine.count();
992cdf0e10cSrcweir 	if( nPointCount <= 0 )
993cdf0e10cSrcweir 		return true;
994cdf0e10cSrcweir 
995cdf0e10cSrcweir 	// reject requests that cannot be handled yet
996cdf0e10cSrcweir 	if( rLineWidths.getX() != rLineWidths.getY() )
997cdf0e10cSrcweir 		return false;
998cdf0e10cSrcweir 
999cdf0e10cSrcweir 	// #i101491# Aqua does not support B2DLINEJOIN_NONE; return false to use
1000cdf0e10cSrcweir 	// the fallback (own geometry preparation)
1001cdf0e10cSrcweir 	// #i104886# linejoin-mode and thus the above only applies to "fat" lines
1002cdf0e10cSrcweir 	if( (basegfx::B2DLINEJOIN_NONE == eLineJoin)
1003cdf0e10cSrcweir 	&& (rLineWidths.getX() > 1.3) )
1004cdf0e10cSrcweir 		return false;
1005cdf0e10cSrcweir 
1006cdf0e10cSrcweir 	// setup line attributes
1007cdf0e10cSrcweir 	CGLineJoin aCGLineJoin = kCGLineJoinMiter;
1008cdf0e10cSrcweir 	switch( eLineJoin ) {
1009cdf0e10cSrcweir 		case ::basegfx::B2DLINEJOIN_NONE:		aCGLineJoin = /*TODO?*/kCGLineJoinMiter; break;
1010cdf0e10cSrcweir 		case ::basegfx::B2DLINEJOIN_MIDDLE:		aCGLineJoin = /*TODO?*/kCGLineJoinMiter; break;
1011cdf0e10cSrcweir 		case ::basegfx::B2DLINEJOIN_BEVEL:		aCGLineJoin = kCGLineJoinBevel; break;
1012cdf0e10cSrcweir 		case ::basegfx::B2DLINEJOIN_MITER:		aCGLineJoin = kCGLineJoinMiter; break;
1013cdf0e10cSrcweir 		case ::basegfx::B2DLINEJOIN_ROUND:		aCGLineJoin = kCGLineJoinRound; break;
1014cdf0e10cSrcweir 	}
1015cdf0e10cSrcweir 
1016cdf0e10cSrcweir 	// setup poly-polygon path
1017cdf0e10cSrcweir 	CGMutablePathRef xPath = CGPathCreateMutable();
1018cdf0e10cSrcweir 	AddPolygonToPath( xPath, rPolyLine, rPolyLine.isClosed(), !getAntiAliasB2DDraw(), true );
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir 	const CGRect aRefreshRect = CGPathGetBoundingBox( xPath );
1021cdf0e10cSrcweir #ifndef NO_I97317_WORKAROUND
1022cdf0e10cSrcweir 	// #i97317# workaround for Quartz having problems with drawing small polygons
1023cdf0e10cSrcweir 	if( ! ((aRefreshRect.size.width <= 0.125) && (aRefreshRect.size.height <= 0.125)) )
1024cdf0e10cSrcweir #endif
1025cdf0e10cSrcweir     {
1026cdf0e10cSrcweir         // use the path to prepare the graphics context
1027cdf0e10cSrcweir         CGContextSaveGState( mrContext );
1028cdf0e10cSrcweir         CGContextAddPath( mrContext, xPath );
1029cdf0e10cSrcweir         // draw path with antialiased line
1030cdf0e10cSrcweir         CGContextSetShouldAntialias( mrContext, true );
1031cdf0e10cSrcweir         CGContextSetAlpha( mrContext, 1.0 - fTransparency );
1032cdf0e10cSrcweir         CGContextSetLineJoin( mrContext, aCGLineJoin );
1033cdf0e10cSrcweir         CGContextSetLineWidth( mrContext, rLineWidths.getX() );
1034cdf0e10cSrcweir         CGContextDrawPath( mrContext, kCGPathStroke );
1035cdf0e10cSrcweir         CGContextRestoreGState( mrContext );
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir         // mark modified rectangle as updated
1038cdf0e10cSrcweir         RefreshRect( aRefreshRect );
1039cdf0e10cSrcweir     }
1040cdf0e10cSrcweir 
1041cdf0e10cSrcweir 	CGPathRelease( xPath );
1042cdf0e10cSrcweir 
1043cdf0e10cSrcweir 	return true;
1044cdf0e10cSrcweir }
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir // -----------------------------------------------------------------------
1047cdf0e10cSrcweir 
1048cdf0e10cSrcweir sal_Bool AquaSalGraphics::drawPolyLineBezier( sal_uLong, const SalPoint*, const sal_uInt8* )
1049cdf0e10cSrcweir {
1050cdf0e10cSrcweir     return sal_False;
1051cdf0e10cSrcweir }
1052cdf0e10cSrcweir 
1053cdf0e10cSrcweir // -----------------------------------------------------------------------
1054cdf0e10cSrcweir 
1055cdf0e10cSrcweir sal_Bool AquaSalGraphics::drawPolygonBezier( sal_uLong, const SalPoint*, const sal_uInt8* )
1056cdf0e10cSrcweir {
1057cdf0e10cSrcweir     return sal_False;
1058cdf0e10cSrcweir }
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir // -----------------------------------------------------------------------
1061cdf0e10cSrcweir 
1062cdf0e10cSrcweir sal_Bool AquaSalGraphics::drawPolyPolygonBezier( sal_uLong, const sal_uLong*,
1063cdf0e10cSrcweir                                              const SalPoint* const*, const sal_uInt8* const* )
1064cdf0e10cSrcweir {
1065cdf0e10cSrcweir     return sal_False;
1066cdf0e10cSrcweir }
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir // -----------------------------------------------------------------------
1069cdf0e10cSrcweir 
1070cdf0e10cSrcweir void AquaSalGraphics::copyBits( const SalTwoRect *pPosAry, SalGraphics *pSrcGraphics )
1071cdf0e10cSrcweir {
1072cdf0e10cSrcweir 	if( !pSrcGraphics )
1073cdf0e10cSrcweir 		pSrcGraphics = this;
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir     //from unix salgdi2.cxx
1076cdf0e10cSrcweir     //[FIXME] find a better way to prevent calc from crashing when width and height are negative
1077cdf0e10cSrcweir     if( pPosAry->mnSrcWidth <= 0
1078cdf0e10cSrcweir         || pPosAry->mnSrcHeight <= 0
1079cdf0e10cSrcweir         || pPosAry->mnDestWidth <= 0
1080cdf0e10cSrcweir         || pPosAry->mnDestHeight <= 0 )
1081cdf0e10cSrcweir     {
1082cdf0e10cSrcweir         return;
1083cdf0e10cSrcweir     }
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir 	// accelerate trivial operations
1086cdf0e10cSrcweir 	/*const*/ AquaSalGraphics* pSrc = static_cast<AquaSalGraphics*>(pSrcGraphics);
1087cdf0e10cSrcweir 	const bool bSameGraphics = (this == pSrc) || (mbWindow && mpFrame && pSrc->mbWindow && (mpFrame == pSrc->mpFrame));
1088cdf0e10cSrcweir 	if( bSameGraphics
1089cdf0e10cSrcweir 	&&  (pPosAry->mnSrcWidth == pPosAry->mnDestWidth)
1090cdf0e10cSrcweir 	&&  (pPosAry->mnSrcHeight == pPosAry->mnDestHeight))
1091cdf0e10cSrcweir 	{
1092cdf0e10cSrcweir 		// short circuit if there is nothing to do
1093cdf0e10cSrcweir 		if( (pPosAry->mnSrcX == pPosAry->mnDestX)
1094cdf0e10cSrcweir 		&&  (pPosAry->mnSrcY == pPosAry->mnDestY))
1095cdf0e10cSrcweir 			return;
1096cdf0e10cSrcweir 		// use copyArea() if source and destination context are identical
1097cdf0e10cSrcweir 		copyArea( pPosAry->mnDestX, pPosAry->mnDestY, pPosAry->mnSrcX, pPosAry->mnSrcY,
1098cdf0e10cSrcweir 			pPosAry->mnSrcWidth, pPosAry->mnSrcHeight, 0 );
1099cdf0e10cSrcweir 		return;
1100cdf0e10cSrcweir 	}
1101cdf0e10cSrcweir 
1102cdf0e10cSrcweir 	ApplyXorContext();
1103cdf0e10cSrcweir 	pSrc->ApplyXorContext();
1104cdf0e10cSrcweir 
1105cdf0e10cSrcweir 	DBG_ASSERT( pSrc->mxLayer!=NULL, "AquaSalGraphics::copyBits() from non-layered graphics" );
1106cdf0e10cSrcweir 
1107cdf0e10cSrcweir 	const CGPoint aDstPoint = { +pPosAry->mnDestX - pPosAry->mnSrcX, pPosAry->mnDestY - pPosAry->mnSrcY };
1108cdf0e10cSrcweir 	if( (pPosAry->mnSrcWidth == pPosAry->mnDestWidth && pPosAry->mnSrcHeight == pPosAry->mnDestHeight) &&
1109cdf0e10cSrcweir 	    (!mnBitmapDepth || (aDstPoint.x + pSrc->mnWidth) <= mnWidth) ) // workaround a Quartz crasher
1110cdf0e10cSrcweir     {
1111cdf0e10cSrcweir 	    // in XOR mode the drawing context is redirected to the XOR mask
1112cdf0e10cSrcweir 	    // if source and target are identical then copyBits() paints onto the target context though
1113cdf0e10cSrcweir 	    CGContextRef xCopyContext = mrContext;
1114cdf0e10cSrcweir 	    if( mpXorEmulation && mpXorEmulation->IsEnabled() )
1115cdf0e10cSrcweir 		    if( pSrcGraphics == this )
1116cdf0e10cSrcweir 			    xCopyContext = mpXorEmulation->GetTargetContext();
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir 	    CGContextSaveGState( xCopyContext );
1119cdf0e10cSrcweir 	    const CGRect aDstRect = { {pPosAry->mnDestX, pPosAry->mnDestY}, {pPosAry->mnDestWidth, pPosAry->mnDestHeight} };
1120cdf0e10cSrcweir 	    CGContextClipToRect( xCopyContext, aDstRect );
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir 	    // draw at new destination
1123cdf0e10cSrcweir 	    // NOTE: flipped drawing gets disabled for this, else the subimage would be drawn upside down
1124cdf0e10cSrcweir 	    if( pSrc->IsFlipped() )
1125cdf0e10cSrcweir 		    { CGContextTranslateCTM( xCopyContext, 0, +mnHeight ); CGContextScaleCTM( xCopyContext, +1, -1 ); }
1126cdf0e10cSrcweir 	    // TODO: pSrc->size() != this->size()
1127cdf0e10cSrcweir 		    ::CGContextDrawLayerAtPoint( xCopyContext, aDstPoint, pSrc->mxLayer );
1128cdf0e10cSrcweir 	    CGContextRestoreGState( xCopyContext );
1129cdf0e10cSrcweir 	    // mark the destination rectangle as updated
1130cdf0e10cSrcweir    	    RefreshRect( aDstRect );
1131cdf0e10cSrcweir     }
1132cdf0e10cSrcweir     else
1133cdf0e10cSrcweir     {
1134cdf0e10cSrcweir 	    SalBitmap* pBitmap = pSrc->getBitmap( pPosAry->mnSrcX, pPosAry->mnSrcY, pPosAry->mnSrcWidth, pPosAry->mnSrcHeight );
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir 	    if( pBitmap )
1137cdf0e10cSrcweir 	    {
1138cdf0e10cSrcweir 		    SalTwoRect aPosAry( *pPosAry );
1139cdf0e10cSrcweir 		    aPosAry.mnSrcX = 0;
1140cdf0e10cSrcweir 		    aPosAry.mnSrcY = 0;
1141cdf0e10cSrcweir 		    drawBitmap( &aPosAry, *pBitmap );
1142cdf0e10cSrcweir 		    delete pBitmap;
1143cdf0e10cSrcweir 	    }
1144cdf0e10cSrcweir     }
1145cdf0e10cSrcweir }
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir // -----------------------------------------------------------------------
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir void AquaSalGraphics::copyArea( long nDstX, long nDstY,long nSrcX, long nSrcY, long nSrcWidth, long nSrcHeight, sal_uInt16 /*nFlags*/ )
1150cdf0e10cSrcweir {
1151cdf0e10cSrcweir 	ApplyXorContext();
1152cdf0e10cSrcweir 
1153cdf0e10cSrcweir #if 0 // TODO: make AquaSalBitmap as fast as the alternative implementation below
1154cdf0e10cSrcweir 	SalBitmap* pBitmap = getBitmap( nSrcX, nSrcY, nSrcWidth, nSrcHeight );
1155cdf0e10cSrcweir 	if( pBitmap )
1156cdf0e10cSrcweir 	{
1157cdf0e10cSrcweir 		SalTwoRect aPosAry;
1158cdf0e10cSrcweir 		aPosAry.mnSrcX = 0;
1159cdf0e10cSrcweir 		aPosAry.mnSrcY = 0;
1160cdf0e10cSrcweir 		aPosAry.mnSrcWidth = nSrcWidth;
1161cdf0e10cSrcweir 		aPosAry.mnSrcHeight = nSrcHeight;
1162cdf0e10cSrcweir 		aPosAry.mnDestX = nDstX;
1163cdf0e10cSrcweir 		aPosAry.mnDestY = nDstY;
1164cdf0e10cSrcweir 		aPosAry.mnDestWidth = nSrcWidth;
1165cdf0e10cSrcweir 		aPosAry.mnDestHeight = nSrcHeight;
1166cdf0e10cSrcweir 		drawBitmap( &aPosAry, *pBitmap );
1167cdf0e10cSrcweir 		delete pBitmap;
1168cdf0e10cSrcweir 	}
1169cdf0e10cSrcweir #else
1170cdf0e10cSrcweir 	DBG_ASSERT( mxLayer!=NULL, "AquaSalGraphics::copyArea() for non-layered graphics" );
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir 	// in XOR mode the drawing context is redirected to the XOR mask
1173cdf0e10cSrcweir 	// copyArea() always works on the target context though
1174cdf0e10cSrcweir 	CGContextRef xCopyContext = mrContext;
1175cdf0e10cSrcweir 	if( mpXorEmulation && mpXorEmulation->IsEnabled() )
1176cdf0e10cSrcweir 		xCopyContext = mpXorEmulation->GetTargetContext();
1177cdf0e10cSrcweir 
1178cdf0e10cSrcweir 	// drawing a layer onto its own context causes trouble on OSX => copy it first
1179cdf0e10cSrcweir 	// TODO: is it possible to get rid of this unneeded copy more often?
1180cdf0e10cSrcweir 	//       e.g. on OSX>=10.5 only this situation causes problems:
1181cdf0e10cSrcweir 	//			mnBitmapDepth && (aDstPoint.x + pSrc->mnWidth) > mnWidth
1182cdf0e10cSrcweir 	CGLayerRef xSrcLayer = mxLayer;
1183cdf0e10cSrcweir 	// TODO: if( mnBitmapDepth > 0 )
1184cdf0e10cSrcweir 	{
1185cdf0e10cSrcweir 		const CGSize aSrcSize = { nSrcWidth, nSrcHeight };
1186cdf0e10cSrcweir 		xSrcLayer = ::CGLayerCreateWithContext( xCopyContext, aSrcSize, NULL );
1187cdf0e10cSrcweir 		const CGContextRef xSrcContext = CGLayerGetContext( xSrcLayer );
1188cdf0e10cSrcweir 		CGPoint aSrcPoint = { -nSrcX, -nSrcY };
1189cdf0e10cSrcweir 		if( IsFlipped() )
1190cdf0e10cSrcweir 		{
1191cdf0e10cSrcweir 			::CGContextTranslateCTM( xSrcContext, 0, +nSrcHeight );
1192cdf0e10cSrcweir 			::CGContextScaleCTM( xSrcContext, +1, -1 );
1193cdf0e10cSrcweir 			aSrcPoint.y = (nSrcY + nSrcHeight) - mnHeight;
1194cdf0e10cSrcweir 		}
1195cdf0e10cSrcweir 		::CGContextDrawLayerAtPoint( xSrcContext, aSrcPoint, mxLayer );
1196cdf0e10cSrcweir 	}
1197cdf0e10cSrcweir 
1198cdf0e10cSrcweir 	// draw at new destination
1199cdf0e10cSrcweir 	const CGPoint aDstPoint = { +nDstX, +nDstY };
1200cdf0e10cSrcweir 	::CGContextDrawLayerAtPoint( xCopyContext, aDstPoint, xSrcLayer );
1201cdf0e10cSrcweir 
1202cdf0e10cSrcweir 	// cleanup
1203cdf0e10cSrcweir 	if( xSrcLayer != mxLayer )
1204cdf0e10cSrcweir 		CGLayerRelease( xSrcLayer );
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir 	// mark the destination rectangle as updated
1207cdf0e10cSrcweir     RefreshRect( nDstX, nDstY, nSrcWidth, nSrcHeight );
1208cdf0e10cSrcweir #endif
1209cdf0e10cSrcweir }
1210cdf0e10cSrcweir 
1211cdf0e10cSrcweir // -----------------------------------------------------------------------
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir void AquaSalGraphics::drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap )
1214cdf0e10cSrcweir {
1215cdf0e10cSrcweir 	if( !CheckContext() )
1216cdf0e10cSrcweir 		return;
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir 	const AquaSalBitmap& rBitmap = static_cast<const AquaSalBitmap&>(rSalBitmap);
1219cdf0e10cSrcweir 	CGImageRef xImage = rBitmap.CreateCroppedImage( (int)pPosAry->mnSrcX, (int)pPosAry->mnSrcY, (int)pPosAry->mnSrcWidth, (int)pPosAry->mnSrcHeight );
1220cdf0e10cSrcweir 	if( !xImage )
1221cdf0e10cSrcweir 		return;
1222cdf0e10cSrcweir 
1223cdf0e10cSrcweir 	const CGRect aDstRect = {{pPosAry->mnDestX, pPosAry->mnDestY}, {pPosAry->mnDestWidth, pPosAry->mnDestHeight}};
1224cdf0e10cSrcweir 	CGContextDrawImage( mrContext, aDstRect, xImage );
1225cdf0e10cSrcweir 	CGImageRelease( xImage );
1226cdf0e10cSrcweir 	RefreshRect( aDstRect );
1227cdf0e10cSrcweir }
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir // -----------------------------------------------------------------------
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir void AquaSalGraphics::drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap,SalColor )
1232cdf0e10cSrcweir {
1233cdf0e10cSrcweir 	DBG_ERROR("not implemented for color masking!");
1234cdf0e10cSrcweir 	drawBitmap( pPosAry, rSalBitmap );
1235cdf0e10cSrcweir }
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir // -----------------------------------------------------------------------
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir void AquaSalGraphics::drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap, const SalBitmap& rTransparentBitmap )
1240cdf0e10cSrcweir {
1241cdf0e10cSrcweir 	if( !CheckContext() )
1242cdf0e10cSrcweir 		return;
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir 	const AquaSalBitmap& rBitmap = static_cast<const AquaSalBitmap&>(rSalBitmap);
1245cdf0e10cSrcweir 	const AquaSalBitmap& rMask = static_cast<const AquaSalBitmap&>(rTransparentBitmap);
1246cdf0e10cSrcweir 	CGImageRef xMaskedImage( rBitmap.CreateWithMask( rMask, pPosAry->mnSrcX, pPosAry->mnSrcY, pPosAry->mnSrcWidth, pPosAry->mnSrcHeight ) );
1247cdf0e10cSrcweir 	if( !xMaskedImage )
1248cdf0e10cSrcweir 		return;
1249cdf0e10cSrcweir 
1250cdf0e10cSrcweir 	const CGRect aDstRect = {{pPosAry->mnDestX, pPosAry->mnDestY}, {pPosAry->mnDestWidth, pPosAry->mnDestHeight}};
1251cdf0e10cSrcweir 	CGContextDrawImage( mrContext, aDstRect, xMaskedImage );
1252cdf0e10cSrcweir 	CGImageRelease( xMaskedImage );
1253cdf0e10cSrcweir 	RefreshRect( aDstRect );
1254cdf0e10cSrcweir }
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir // -----------------------------------------------------------------------
1257cdf0e10cSrcweir 
1258cdf0e10cSrcweir void AquaSalGraphics::drawMask( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap, SalColor nMaskColor )
1259cdf0e10cSrcweir {
1260cdf0e10cSrcweir 	if( !CheckContext() )
1261cdf0e10cSrcweir 		return;
1262cdf0e10cSrcweir 
1263cdf0e10cSrcweir 	const AquaSalBitmap& rBitmap = static_cast<const AquaSalBitmap&>(rSalBitmap);
1264cdf0e10cSrcweir 	CGImageRef xImage = rBitmap.CreateColorMask( pPosAry->mnSrcX, pPosAry->mnSrcY, pPosAry->mnSrcWidth, pPosAry->mnSrcHeight, nMaskColor );
1265cdf0e10cSrcweir 	if( !xImage )
1266cdf0e10cSrcweir 		return;
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir 	const CGRect aDstRect = {{pPosAry->mnDestX, pPosAry->mnDestY}, {pPosAry->mnDestWidth, pPosAry->mnDestHeight}};
1269cdf0e10cSrcweir 	CGContextDrawImage( mrContext, aDstRect, xImage );
1270cdf0e10cSrcweir 	CGImageRelease( xImage );
1271cdf0e10cSrcweir 	RefreshRect( aDstRect );
1272cdf0e10cSrcweir }
1273cdf0e10cSrcweir 
1274cdf0e10cSrcweir // -----------------------------------------------------------------------
1275cdf0e10cSrcweir 
1276cdf0e10cSrcweir SalBitmap* AquaSalGraphics::getBitmap( long  nX, long  nY, long  nDX, long  nDY )
1277cdf0e10cSrcweir {
1278cdf0e10cSrcweir 	DBG_ASSERT( mxLayer, "AquaSalGraphics::getBitmap() with no layer" );
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir 	ApplyXorContext();
1281cdf0e10cSrcweir 
1282cdf0e10cSrcweir 	AquaSalBitmap* pBitmap = new AquaSalBitmap;
1283cdf0e10cSrcweir 	if( !pBitmap->Create( mxLayer, mnBitmapDepth, nX, nY, nDX, nDY, !mbWindow ) )
1284cdf0e10cSrcweir 	{
1285cdf0e10cSrcweir 		delete pBitmap;
1286cdf0e10cSrcweir 		pBitmap = NULL;
1287cdf0e10cSrcweir 	}
1288cdf0e10cSrcweir 
1289cdf0e10cSrcweir 	return pBitmap;
1290cdf0e10cSrcweir }
1291cdf0e10cSrcweir 
1292cdf0e10cSrcweir // -----------------------------------------------------------------------
1293cdf0e10cSrcweir 
1294cdf0e10cSrcweir SalColor AquaSalGraphics::getPixel( long nX, long nY )
1295cdf0e10cSrcweir {
1296cdf0e10cSrcweir 	// return default value on printers or when out of bounds
1297cdf0e10cSrcweir 	if( !mxLayer
1298cdf0e10cSrcweir 	|| (nX < 0) || (nX >= mnWidth)
1299cdf0e10cSrcweir 	|| (nY < 0) || (nY >= mnHeight))
1300cdf0e10cSrcweir 		return COL_BLACK;
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir 	// prepare creation of matching a CGBitmapContext
1303cdf0e10cSrcweir 	CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
1304cdf0e10cSrcweir 	CGBitmapInfo aCGBmpInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big;
1305cdf0e10cSrcweir #if __BIG_ENDIAN__
1306cdf0e10cSrcweir 	struct{ unsigned char b, g, r, a; } aPixel;
1307cdf0e10cSrcweir #else
1308cdf0e10cSrcweir 	struct{ unsigned char a, r, g, b; } aPixel;
1309cdf0e10cSrcweir #endif
1310cdf0e10cSrcweir 
1311cdf0e10cSrcweir 	// create a one-pixel bitmap context
1312cdf0e10cSrcweir 	// TODO: is it worth to cache it?
1313cdf0e10cSrcweir 	CGContextRef xOnePixelContext = ::CGBitmapContextCreate( &aPixel,
1314cdf0e10cSrcweir 		1, 1, 8, sizeof(aPixel), aCGColorSpace, aCGBmpInfo );
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir 	// update this graphics layer
1317cdf0e10cSrcweir 	ApplyXorContext();
1318cdf0e10cSrcweir 
1319cdf0e10cSrcweir 	// copy the requested pixel into the bitmap context
1320cdf0e10cSrcweir 	if( IsFlipped() )
1321cdf0e10cSrcweir 		nY = mnHeight - nY;
1322cdf0e10cSrcweir 	const CGPoint aCGPoint = {-nX, -nY};
1323cdf0e10cSrcweir 	CGContextDrawLayerAtPoint( xOnePixelContext, aCGPoint, mxLayer );
1324cdf0e10cSrcweir 	CGContextRelease( xOnePixelContext );
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir 	SalColor nSalColor = MAKE_SALCOLOR( aPixel.r, aPixel.g, aPixel.b );
1327cdf0e10cSrcweir 	return nSalColor;
1328cdf0e10cSrcweir }
1329cdf0e10cSrcweir 
1330cdf0e10cSrcweir // -----------------------------------------------------------------------
1331cdf0e10cSrcweir 
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir static void DrawPattern50( void*, CGContextRef rContext )
1334cdf0e10cSrcweir {
1335cdf0e10cSrcweir     static const CGRect aRects[2] = { { {0,0}, { 2, 2 } }, { { 2, 2 }, { 2, 2 } } };
1336cdf0e10cSrcweir     CGContextAddRects( rContext, aRects, 2 );
1337cdf0e10cSrcweir     CGContextFillPath( rContext );
1338cdf0e10cSrcweir }
1339cdf0e10cSrcweir 
1340cdf0e10cSrcweir void AquaSalGraphics::Pattern50Fill()
1341cdf0e10cSrcweir {
1342cdf0e10cSrcweir     static const float aFillCol[4] = { 1,1,1,1 };
1343cdf0e10cSrcweir     static const CGPatternCallbacks aCallback = { 0, &DrawPattern50, NULL };
1344cdf0e10cSrcweir     if( ! GetSalData()->mxP50Space )
1345cdf0e10cSrcweir         GetSalData()->mxP50Space = CGColorSpaceCreatePattern( GetSalData()->mxRGBSpace );
1346cdf0e10cSrcweir     if( ! GetSalData()->mxP50Pattern )
1347cdf0e10cSrcweir         GetSalData()->mxP50Pattern = CGPatternCreate( NULL, CGRectMake( 0, 0, 4, 4 ),
1348cdf0e10cSrcweir                                                       CGAffineTransformIdentity, 4, 4,
1349cdf0e10cSrcweir                                                       kCGPatternTilingConstantSpacing,
1350cdf0e10cSrcweir                                                       false, &aCallback );
1351cdf0e10cSrcweir 
1352cdf0e10cSrcweir     CGContextSetFillColorSpace( mrContext, GetSalData()->mxP50Space );
1353cdf0e10cSrcweir     CGContextSetFillPattern( mrContext, GetSalData()->mxP50Pattern, aFillCol );
1354cdf0e10cSrcweir     CGContextFillPath( mrContext );
1355cdf0e10cSrcweir }
1356cdf0e10cSrcweir 
1357cdf0e10cSrcweir void AquaSalGraphics::invert( long nX, long nY, long nWidth, long nHeight, SalInvert nFlags )
1358cdf0e10cSrcweir {
1359cdf0e10cSrcweir     if ( CheckContext() )
1360cdf0e10cSrcweir     {
1361cdf0e10cSrcweir         CGRect aCGRect = CGRectMake( nX, nY, nWidth, nHeight);
1362cdf0e10cSrcweir         CGContextSaveGState(mrContext);
1363cdf0e10cSrcweir 
1364cdf0e10cSrcweir         if ( nFlags & SAL_INVERT_TRACKFRAME )
1365cdf0e10cSrcweir         {
1366cdf0e10cSrcweir             const float dashLengths[2]  = { 4.0, 4.0 };     // for drawing dashed line
1367cdf0e10cSrcweir             CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
1368cdf0e10cSrcweir             CGContextSetRGBStrokeColor ( mrContext, 1.0, 1.0, 1.0, 1.0 );
1369cdf0e10cSrcweir             CGContextSetLineDash ( mrContext, 0, dashLengths, 2 );
1370cdf0e10cSrcweir             CGContextSetLineWidth( mrContext, 2.0);
1371cdf0e10cSrcweir             CGContextStrokeRect ( mrContext, aCGRect );
1372cdf0e10cSrcweir         }
1373cdf0e10cSrcweir         else if ( nFlags & SAL_INVERT_50 )
1374cdf0e10cSrcweir         {
1375cdf0e10cSrcweir             //CGContextSetAllowsAntialiasing( mrContext, false );
1376cdf0e10cSrcweir             CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
1377cdf0e10cSrcweir             CGContextAddRect( mrContext, aCGRect );
1378cdf0e10cSrcweir             Pattern50Fill();
1379cdf0e10cSrcweir 		}
1380cdf0e10cSrcweir         else // just invert
1381cdf0e10cSrcweir         {
1382cdf0e10cSrcweir             CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
1383cdf0e10cSrcweir             CGContextSetRGBFillColor ( mrContext,1.0, 1.0, 1.0 , 1.0 );
1384cdf0e10cSrcweir             CGContextFillRect ( mrContext, aCGRect );
1385cdf0e10cSrcweir         }
1386cdf0e10cSrcweir         CGContextRestoreGState( mrContext);
1387cdf0e10cSrcweir         RefreshRect( aCGRect );
1388cdf0e10cSrcweir     }
1389cdf0e10cSrcweir }
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir // -----------------------------------------------------------------------
1392cdf0e10cSrcweir 
1393cdf0e10cSrcweir void AquaSalGraphics::invert( sal_uLong nPoints, const SalPoint*  pPtAry, SalInvert nSalFlags )
1394cdf0e10cSrcweir {
1395cdf0e10cSrcweir     CGPoint* CGpoints ;
1396cdf0e10cSrcweir     if ( CheckContext() )
1397cdf0e10cSrcweir     {
1398cdf0e10cSrcweir         CGContextSaveGState(mrContext);
1399cdf0e10cSrcweir         CGpoints = makeCGptArray(nPoints,pPtAry);
1400cdf0e10cSrcweir         CGContextAddLines ( mrContext, CGpoints, nPoints );
1401cdf0e10cSrcweir         if ( nSalFlags & SAL_INVERT_TRACKFRAME )
1402cdf0e10cSrcweir         {
1403cdf0e10cSrcweir             const float dashLengths[2]  = { 4.0, 4.0 };     // for drawing dashed line
1404cdf0e10cSrcweir             CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
1405cdf0e10cSrcweir             CGContextSetRGBStrokeColor ( mrContext, 1.0, 1.0, 1.0, 1.0 );
1406cdf0e10cSrcweir             CGContextSetLineDash ( mrContext, 0, dashLengths, 2 );
1407cdf0e10cSrcweir             CGContextSetLineWidth( mrContext, 2.0);
1408cdf0e10cSrcweir             CGContextStrokePath ( mrContext );
1409cdf0e10cSrcweir         }
1410cdf0e10cSrcweir         else if ( nSalFlags & SAL_INVERT_50 )
1411cdf0e10cSrcweir         {
1412cdf0e10cSrcweir             CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
1413cdf0e10cSrcweir             Pattern50Fill();
1414cdf0e10cSrcweir         }
1415cdf0e10cSrcweir         else // just invert
1416cdf0e10cSrcweir         {
1417cdf0e10cSrcweir             CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
1418cdf0e10cSrcweir             CGContextSetRGBFillColor( mrContext, 1.0, 1.0, 1.0, 1.0 );
1419cdf0e10cSrcweir             CGContextFillPath( mrContext );
1420cdf0e10cSrcweir         }
1421cdf0e10cSrcweir         const CGRect aRefreshRect = CGContextGetClipBoundingBox(mrContext);
1422cdf0e10cSrcweir         CGContextRestoreGState( mrContext);
1423cdf0e10cSrcweir         delete []  CGpoints;
1424cdf0e10cSrcweir         RefreshRect( aRefreshRect );
1425cdf0e10cSrcweir     }
1426cdf0e10cSrcweir }
1427cdf0e10cSrcweir 
1428cdf0e10cSrcweir // -----------------------------------------------------------------------
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir sal_Bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
1431cdf0e10cSrcweir 	void* pEpsData, sal_uLong nByteCount )
1432cdf0e10cSrcweir {
1433cdf0e10cSrcweir 	// convert the raw data to an NSImageRef
1434cdf0e10cSrcweir 	NSData* xNSData = [NSData dataWithBytes:(void*)pEpsData length:(int)nByteCount];
1435cdf0e10cSrcweir 	NSImageRep* xEpsImage = [NSEPSImageRep imageRepWithData: xNSData];
1436cdf0e10cSrcweir 	if( !xEpsImage )
1437cdf0e10cSrcweir 		return false;
1438cdf0e10cSrcweir 
1439cdf0e10cSrcweir 	// get the target context
1440cdf0e10cSrcweir 	if( !CheckContext() )
1441cdf0e10cSrcweir 		return false;
1442cdf0e10cSrcweir 
1443cdf0e10cSrcweir 	// NOTE: flip drawing, else the nsimage would be drawn upside down
1444cdf0e10cSrcweir 	CGContextSaveGState( mrContext );
1445cdf0e10cSrcweir //	CGContextTranslateCTM( mrContext, 0, +mnHeight );
1446cdf0e10cSrcweir 	CGContextScaleCTM( mrContext, +1, -1 );
1447cdf0e10cSrcweir 	nY = /*mnHeight*/ - (nY + nHeight);
1448cdf0e10cSrcweir 
1449cdf0e10cSrcweir 	// prepare the target context
1450cdf0e10cSrcweir 	NSGraphicsContext* pOrigNSCtx = [NSGraphicsContext currentContext];
1451cdf0e10cSrcweir 	[pOrigNSCtx retain];
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir 	// create new context
1454cdf0e10cSrcweir 	NSGraphicsContext* pDrawNSCtx = [NSGraphicsContext graphicsContextWithGraphicsPort: mrContext flipped: IsFlipped()];
1455cdf0e10cSrcweir 	// set it, setCurrentContext also releases the prviously set one
1456cdf0e10cSrcweir 	[NSGraphicsContext setCurrentContext: pDrawNSCtx];
1457cdf0e10cSrcweir 
1458cdf0e10cSrcweir 	// draw the EPS
1459cdf0e10cSrcweir 	const NSRect aDstRect = {{nX,nY},{nWidth,nHeight}};
1460cdf0e10cSrcweir 	const BOOL bOK = [xEpsImage drawInRect: aDstRect];
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir 	// restore the NSGraphicsContext
1463cdf0e10cSrcweir 	[NSGraphicsContext setCurrentContext: pOrigNSCtx];
1464cdf0e10cSrcweir 	[pOrigNSCtx release]; // restore the original retain count
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir 	CGContextRestoreGState( mrContext );
1467cdf0e10cSrcweir 	// mark the destination rectangle as updated
1468cdf0e10cSrcweir    	RefreshRect( aDstRect );
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir 	return bOK;
1471cdf0e10cSrcweir }
1472cdf0e10cSrcweir 
1473cdf0e10cSrcweir // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
1474cdf0e10cSrcweir bool AquaSalGraphics::drawAlphaBitmap( const SalTwoRect& rTR,
1475cdf0e10cSrcweir     const SalBitmap& rSrcBitmap, const SalBitmap& rAlphaBmp )
1476cdf0e10cSrcweir {
1477cdf0e10cSrcweir     // An image mask can't have a depth > 8 bits (should be 1 to 8 bits)
1478cdf0e10cSrcweir     if( rAlphaBmp.GetBitCount() > 8 )
1479cdf0e10cSrcweir         return false;
1480cdf0e10cSrcweir 
1481cdf0e10cSrcweir     // are these two tests really necessary? (see vcl/unx/source/gdi/salgdi2.cxx)
1482cdf0e10cSrcweir     // horizontal/vertical mirroring not implemented yet
1483cdf0e10cSrcweir     if( rTR.mnDestWidth < 0 || rTR.mnDestHeight < 0 )
1484cdf0e10cSrcweir         return false;
1485cdf0e10cSrcweir 
1486cdf0e10cSrcweir     const AquaSalBitmap& rSrcSalBmp = static_cast<const AquaSalBitmap&>(rSrcBitmap);
1487cdf0e10cSrcweir     const AquaSalBitmap& rMaskSalBmp = static_cast<const AquaSalBitmap&>(rAlphaBmp);
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir     CGImageRef xMaskedImage = rSrcSalBmp.CreateWithMask( rMaskSalBmp, rTR.mnSrcX, rTR.mnSrcY, rTR.mnSrcWidth, rTR.mnSrcHeight );
1490cdf0e10cSrcweir 	if( !xMaskedImage )
1491cdf0e10cSrcweir 		return false;
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir     if ( CheckContext() )
1494cdf0e10cSrcweir     {
1495cdf0e10cSrcweir 		const CGRect aDstRect = {{rTR.mnDestX, rTR.mnDestY}, {rTR.mnDestWidth, rTR.mnDestHeight}};
1496cdf0e10cSrcweir 		CGContextDrawImage( mrContext, aDstRect, xMaskedImage );
1497cdf0e10cSrcweir 		RefreshRect( aDstRect );
1498cdf0e10cSrcweir     }
1499cdf0e10cSrcweir 
1500cdf0e10cSrcweir     CGImageRelease(xMaskedImage);
1501cdf0e10cSrcweir     return true;
1502cdf0e10cSrcweir }
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
1505cdf0e10cSrcweir bool AquaSalGraphics::drawAlphaRect( long nX, long nY, long nWidth,
1506cdf0e10cSrcweir                                      long nHeight, sal_uInt8 nTransparency )
1507cdf0e10cSrcweir {
1508cdf0e10cSrcweir     if( !CheckContext() )
1509cdf0e10cSrcweir     	return true;
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir 	// save the current state
1512cdf0e10cSrcweir 	CGContextSaveGState( mrContext );
1513cdf0e10cSrcweir 	CGContextSetAlpha( mrContext, (100-nTransparency) * (1.0/100) );
1514cdf0e10cSrcweir 
1515cdf0e10cSrcweir 	CGRect aRect = {{nX,nY},{nWidth-1,nHeight-1}};
1516cdf0e10cSrcweir 	if( IsPenVisible() )
1517cdf0e10cSrcweir 	{
1518cdf0e10cSrcweir 		aRect.origin.x += 0.5;
1519cdf0e10cSrcweir 		aRect.origin.y += 0.5;
1520cdf0e10cSrcweir 	}
1521cdf0e10cSrcweir 
1522cdf0e10cSrcweir 	CGContextBeginPath( mrContext );
1523cdf0e10cSrcweir 	CGContextAddRect( mrContext, aRect );
1524cdf0e10cSrcweir 	CGContextDrawPath( mrContext, kCGPathFill );
1525cdf0e10cSrcweir 
1526cdf0e10cSrcweir 	// restore state
1527cdf0e10cSrcweir 	CGContextRestoreGState(mrContext);
1528cdf0e10cSrcweir 	RefreshRect( aRect );
1529cdf0e10cSrcweir     return true;
1530cdf0e10cSrcweir }
1531cdf0e10cSrcweir 
1532cdf0e10cSrcweir // -----------------------------------------------------------------------
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir void AquaSalGraphics::SetTextColor( SalColor nSalColor )
1535cdf0e10cSrcweir {
1536cdf0e10cSrcweir     RGBColor color;
1537cdf0e10cSrcweir     color.red     = (unsigned short) ( SALCOLOR_RED(nSalColor)   * 65535.0 / 255.0 );
1538cdf0e10cSrcweir     color.green   = (unsigned short) ( SALCOLOR_GREEN(nSalColor) * 65535.0 / 255.0 );
1539cdf0e10cSrcweir     color.blue    = (unsigned short) ( SALCOLOR_BLUE(nSalColor)  * 65535.0 / 255.0 );
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir     ATSUAttributeTag aTag = kATSUColorTag;
1542cdf0e10cSrcweir     ByteCount aValueSize = sizeof( color );
1543cdf0e10cSrcweir     ATSUAttributeValuePtr aValue = &color;
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir     OSStatus err = ATSUSetAttributes( maATSUStyle, 1, &aTag, &aValueSize, &aValue );
1546cdf0e10cSrcweir 	DBG_ASSERT( (err==noErr), "AquaSalGraphics::SetTextColor() : Could not set font attributes!\n");
1547cdf0e10cSrcweir 	if( err != noErr )
1548cdf0e10cSrcweir 		return;
1549cdf0e10cSrcweir }
1550cdf0e10cSrcweir 
1551cdf0e10cSrcweir // -----------------------------------------------------------------------
1552cdf0e10cSrcweir 
1553cdf0e10cSrcweir void AquaSalGraphics::GetFontMetric( ImplFontMetricData* pMetric, int nFallbackLevel )
1554cdf0e10cSrcweir {
1555cdf0e10cSrcweir 	(void)nFallbackLevel; // glyph-fallback on ATSU is done differently -> no fallback level
1556cdf0e10cSrcweir 
1557cdf0e10cSrcweir 	// get the ATSU font metrics (in point units)
1558cdf0e10cSrcweir 	// of the font that has eventually been size-limited
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir     ATSUFontID fontId;
1561cdf0e10cSrcweir     OSStatus err = ATSUGetAttribute( maATSUStyle, kATSUFontTag, sizeof(ATSUFontID), &fontId, 0 );
1562cdf0e10cSrcweir     DBG_ASSERT( (err==noErr), "AquaSalGraphics::GetFontMetric() : could not get font id\n");
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir     ATSFontMetrics aMetrics;
1565cdf0e10cSrcweir     ATSFontRef rFont = FMGetATSFontRefFromFont( fontId );
1566cdf0e10cSrcweir     err = ATSFontGetHorizontalMetrics ( rFont, kATSOptionFlagsDefault, &aMetrics );
1567cdf0e10cSrcweir 	DBG_ASSERT( (err==noErr), "AquaSalGraphics::GetFontMetric() : could not get font metrics\n");
1568cdf0e10cSrcweir     if( err != noErr )
1569cdf0e10cSrcweir 		return;
1570cdf0e10cSrcweir 
1571cdf0e10cSrcweir 	// all ATS fonts are scalable fonts
1572cdf0e10cSrcweir 	pMetric->mbScalableFont = true;
1573cdf0e10cSrcweir 	// TODO: check if any kerning is possible
1574cdf0e10cSrcweir 	pMetric->mbKernableFont = true;
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir 	// convert into VCL font metrics (in unscaled pixel units)
1577cdf0e10cSrcweir 
1578cdf0e10cSrcweir     Fixed ptSize;
1579cdf0e10cSrcweir     err = ATSUGetAttribute( maATSUStyle, kATSUSizeTag, sizeof(Fixed), &ptSize, 0);
1580cdf0e10cSrcweir     DBG_ASSERT( (err==noErr), "AquaSalGraphics::GetFontMetric() : could not get font size\n");
1581cdf0e10cSrcweir 	const double fPointSize = Fix2X( ptSize );
1582cdf0e10cSrcweir 
1583cdf0e10cSrcweir 	// convert quartz units to pixel units
1584cdf0e10cSrcweir 	// please see the comment in AquaSalGraphics::SetFont() for details
1585cdf0e10cSrcweir     const double fPixelSize = (mfFontScale * mfFakeDPIScale * fPointSize);
1586cdf0e10cSrcweir     pMetric->mnAscent       = static_cast<long>(+aMetrics.ascent  * fPixelSize + 0.5);
1587cdf0e10cSrcweir     pMetric->mnDescent      = static_cast<long>(-aMetrics.descent * fPixelSize + 0.5);
1588cdf0e10cSrcweir     const long nExtDescent  = static_cast<long>((-aMetrics.descent + aMetrics.leading) * fPixelSize + 0.5);
1589cdf0e10cSrcweir     pMetric->mnExtLeading   = nExtDescent - pMetric->mnDescent;
1590cdf0e10cSrcweir     pMetric->mnIntLeading   = 0;
1591cdf0e10cSrcweir     // ATSFontMetrics.avgAdvanceWidth is obsolete, so it is usually set to zero
1592cdf0e10cSrcweir     // since ImplFontMetricData::mnWidth is only used for stretching/squeezing fonts
1593cdf0e10cSrcweir     // setting this width to the pixel height of the fontsize is good enough
1594cdf0e10cSrcweir     // it also makes the calculation of the stretch factor simple
1595cdf0e10cSrcweir     pMetric->mnWidth        = static_cast<long>(mfFontStretch * fPixelSize + 0.5);
1596cdf0e10cSrcweir }
1597cdf0e10cSrcweir 
1598cdf0e10cSrcweir // -----------------------------------------------------------------------
1599cdf0e10cSrcweir 
1600cdf0e10cSrcweir sal_uLong AquaSalGraphics::GetKernPairs( sal_uLong, ImplKernPairData* )
1601cdf0e10cSrcweir {
1602cdf0e10cSrcweir 	return 0;
1603cdf0e10cSrcweir }
1604cdf0e10cSrcweir 
1605cdf0e10cSrcweir // -----------------------------------------------------------------------
1606cdf0e10cSrcweir 
1607cdf0e10cSrcweir static bool AddTempFontDir( const char* pDir )
1608cdf0e10cSrcweir {
1609cdf0e10cSrcweir     FSRef aPathFSRef;
1610cdf0e10cSrcweir     Boolean bIsDirectory = true;
1611cdf0e10cSrcweir     OSStatus eStatus = FSPathMakeRef( reinterpret_cast<const UInt8*>(pDir), &aPathFSRef, &bIsDirectory );
1612cdf0e10cSrcweir     DBG_ASSERTWARNING( (eStatus==noErr) && bIsDirectory, "vcl AddTempFontDir() with invalid directory name!" );
1613cdf0e10cSrcweir     if( eStatus != noErr )
1614cdf0e10cSrcweir         return false;
1615cdf0e10cSrcweir 
1616cdf0e10cSrcweir     // TODO: deactivate ATSFontContainerRef when closing app
1617cdf0e10cSrcweir     ATSFontContainerRef aATSFontContainer;
1618cdf0e10cSrcweir 
1619cdf0e10cSrcweir     const ATSFontContext eContext = kATSFontContextLocal; // TODO: *Global???
1620cdf0e10cSrcweir #if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
1621cdf0e10cSrcweir     eStatus = ::ATSFontActivateFromFileReference( &aPathFSRef,
1622cdf0e10cSrcweir         eContext, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault,
1623cdf0e10cSrcweir         &aATSFontContainer );
1624cdf0e10cSrcweir #else
1625cdf0e10cSrcweir     FSSpec aPathFSSpec;
1626cdf0e10cSrcweir     eStatus = ::FSGetCatalogInfo( &aPathFSRef, kFSCatInfoNone,
1627cdf0e10cSrcweir         NULL, NULL, &aPathFSSpec, NULL );
1628cdf0e10cSrcweir     if( eStatus != noErr )
1629cdf0e10cSrcweir         return false;
1630cdf0e10cSrcweir 
1631cdf0e10cSrcweir     eStatus = ::ATSFontActivateFromFileSpecification( &aPathFSSpec,
1632cdf0e10cSrcweir         eContext, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault,
1633cdf0e10cSrcweir         &aATSFontContainer );
1634cdf0e10cSrcweir #endif
1635cdf0e10cSrcweir     if( eStatus != noErr )
1636cdf0e10cSrcweir         return false;
1637cdf0e10cSrcweir 
1638cdf0e10cSrcweir     return true;
1639cdf0e10cSrcweir }
1640cdf0e10cSrcweir 
1641cdf0e10cSrcweir static bool AddLocalTempFontDirs( void )
1642cdf0e10cSrcweir {
1643cdf0e10cSrcweir     static bool bFirst = true;
1644cdf0e10cSrcweir     if( !bFirst )
1645cdf0e10cSrcweir         return false;
1646cdf0e10cSrcweir     bFirst = false;
1647cdf0e10cSrcweir 
1648cdf0e10cSrcweir     // add private font files found in brand and base layer
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir     rtl::OUString aBrandStr( RTL_CONSTASCII_USTRINGPARAM( "$BRAND_BASE_DIR" ) );
1651cdf0e10cSrcweir     rtl_bootstrap_expandMacros( &aBrandStr.pData );
1652cdf0e10cSrcweir     rtl::OUString aBrandSysPath;
1653cdf0e10cSrcweir     OSL_VERIFY( osl_getSystemPathFromFileURL( aBrandStr.pData, &aBrandSysPath.pData ) == osl_File_E_None );
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir     rtl::OStringBuffer aBrandFontDir( aBrandSysPath.getLength()*2 );
1656cdf0e10cSrcweir     aBrandFontDir.append( rtl::OUStringToOString( aBrandSysPath, RTL_TEXTENCODING_UTF8 ) );
1657cdf0e10cSrcweir     aBrandFontDir.append( "/share/fonts/truetype/" );
1658cdf0e10cSrcweir     bool bBrandSuccess = AddTempFontDir( aBrandFontDir.getStr() );
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir     rtl::OUString aBaseStr( RTL_CONSTASCII_USTRINGPARAM( "$OOO_BASE_DIR" ) );
1661cdf0e10cSrcweir     rtl_bootstrap_expandMacros( &aBaseStr.pData );
1662cdf0e10cSrcweir     rtl::OUString aBaseSysPath;
1663cdf0e10cSrcweir     OSL_VERIFY( osl_getSystemPathFromFileURL( aBaseStr.pData, &aBaseSysPath.pData ) == osl_File_E_None );
1664cdf0e10cSrcweir 
1665cdf0e10cSrcweir     rtl::OStringBuffer aBaseFontDir( aBaseSysPath.getLength()*2 );
1666cdf0e10cSrcweir     aBaseFontDir.append( rtl::OUStringToOString( aBaseSysPath, RTL_TEXTENCODING_UTF8 ) );
1667cdf0e10cSrcweir     aBaseFontDir.append( "/share/fonts/truetype/" );
1668cdf0e10cSrcweir     bool bBaseSuccess = AddTempFontDir( aBaseFontDir.getStr() );
1669cdf0e10cSrcweir 
1670cdf0e10cSrcweir     return bBrandSuccess && bBaseSuccess;
1671cdf0e10cSrcweir }
1672cdf0e10cSrcweir 
1673cdf0e10cSrcweir void AquaSalGraphics::GetDevFontList( ImplDevFontList* pFontList )
1674cdf0e10cSrcweir {
1675cdf0e10cSrcweir 	DBG_ASSERT( pFontList, "AquaSalGraphics::GetDevFontList(NULL) !");
1676cdf0e10cSrcweir 
1677cdf0e10cSrcweir     AddLocalTempFontDirs();
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir 	// The idea is to cache the list of system fonts once it has been generated.
1680cdf0e10cSrcweir 	// SalData seems to be a good place for this caching. However we have to
1681cdf0e10cSrcweir 	// carefully make the access to the font list thread-safe. If we register
1682cdf0e10cSrcweir 	// a font-change event handler to update the font list in case fonts have
1683cdf0e10cSrcweir 	// changed on the system we have to lock access to the list. The right
1684cdf0e10cSrcweir     // way to do that is the solar mutex since GetDevFontList is protected
1685cdf0e10cSrcweir     // through it as should be all event handlers
1686cdf0e10cSrcweir 
1687cdf0e10cSrcweir 	SalData* pSalData = GetSalData();
1688cdf0e10cSrcweir 	if (pSalData->mpFontList == NULL)
1689cdf0e10cSrcweir 		pSalData->mpFontList = new SystemFontList();
1690cdf0e10cSrcweir 
1691cdf0e10cSrcweir 	// Copy all ImplFontData objects contained in the SystemFontList
1692cdf0e10cSrcweir 	pSalData->mpFontList->AnnounceFonts( *pFontList );
1693cdf0e10cSrcweir }
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir // -----------------------------------------------------------------------
1696cdf0e10cSrcweir 
1697cdf0e10cSrcweir bool AquaSalGraphics::AddTempDevFont( ImplDevFontList*,
1698cdf0e10cSrcweir 	const String& rFontFileURL, const String& /*rFontName*/ )
1699cdf0e10cSrcweir {
1700cdf0e10cSrcweir 	::rtl::OUString aUSytemPath;
1701cdf0e10cSrcweir     OSL_VERIFY( !osl::FileBase::getSystemPathFromFileURL( rFontFileURL, aUSytemPath ) );
1702cdf0e10cSrcweir 
1703cdf0e10cSrcweir 	FSRef aNewRef;
1704cdf0e10cSrcweir 	Boolean bIsDirectory = true;
1705cdf0e10cSrcweir 	::rtl::OString aCFileName = rtl::OUStringToOString( aUSytemPath, RTL_TEXTENCODING_UTF8 );
1706cdf0e10cSrcweir 	OSStatus eStatus = FSPathMakeRef( (UInt8*)aCFileName.getStr(), &aNewRef, &bIsDirectory );
1707cdf0e10cSrcweir     DBG_ASSERT( (eStatus==noErr) && !bIsDirectory, "vcl AddTempDevFont() with invalid fontfile name!" );
1708cdf0e10cSrcweir 	if( eStatus != noErr )
1709cdf0e10cSrcweir 		return false;
1710cdf0e10cSrcweir 
1711cdf0e10cSrcweir 	ATSFontContainerRef oContainer;
1712cdf0e10cSrcweir 
1713cdf0e10cSrcweir 	const ATSFontContext eContext = kATSFontContextLocal; // TODO: *Global???
1714cdf0e10cSrcweir #if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
1715cdf0e10cSrcweir 	eStatus = ::ATSFontActivateFromFileReference( &aNewRef,
1716cdf0e10cSrcweir 		eContext, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault,
1717cdf0e10cSrcweir 		&oContainer );
1718cdf0e10cSrcweir #else
1719cdf0e10cSrcweir 	FSSpec aFontFSSpec;
1720cdf0e10cSrcweir 	eStatus = ::FSGetCatalogInfo( &aNewRef, kFSCatInfoNone,
1721cdf0e10cSrcweir 		NULL, NULL,	&aFontFSSpec, NULL );
1722cdf0e10cSrcweir 	if( eStatus != noErr )
1723cdf0e10cSrcweir 		return false;
1724cdf0e10cSrcweir 
1725cdf0e10cSrcweir 	eStatus = ::ATSFontActivateFromFileSpecification( &aFontFSSpec,
1726cdf0e10cSrcweir 		eContext, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault,
1727cdf0e10cSrcweir 		&oContainer );
1728cdf0e10cSrcweir #endif
1729cdf0e10cSrcweir 	if( eStatus != noErr )
1730cdf0e10cSrcweir 		return false;
1731cdf0e10cSrcweir 
1732cdf0e10cSrcweir 	// TODO: ATSFontDeactivate( oContainer ) when fonts are no longer needed
1733cdf0e10cSrcweir 	// TODO: register new ImplMacFontdata in pFontList
1734cdf0e10cSrcweir     return true;
1735cdf0e10cSrcweir }
1736cdf0e10cSrcweir 
1737cdf0e10cSrcweir // -----------------------------------------------------------------------
1738cdf0e10cSrcweir 
1739cdf0e10cSrcweir // callbacks from ATSUGlyphGetCubicPaths() fore GetGlyphOutline()
1740cdf0e10cSrcweir struct GgoData { basegfx::B2DPolygon maPolygon; basegfx::B2DPolyPolygon* mpPolyPoly; };
1741cdf0e10cSrcweir 
1742cdf0e10cSrcweir static OSStatus GgoLineToProc( const Float32Point* pPoint, void* pData )
1743cdf0e10cSrcweir {
1744cdf0e10cSrcweir 	basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
1745cdf0e10cSrcweir 	const basegfx::B2DPoint aB2DPoint( pPoint->x, pPoint->y );
1746cdf0e10cSrcweir 	rPolygon.append( aB2DPoint );
1747cdf0e10cSrcweir 	return noErr;
1748cdf0e10cSrcweir }
1749cdf0e10cSrcweir 
1750cdf0e10cSrcweir static OSStatus GgoCurveToProc( const Float32Point* pCP1, const Float32Point* pCP2,
1751cdf0e10cSrcweir 	const Float32Point* pPoint, void* pData )
1752cdf0e10cSrcweir {
1753cdf0e10cSrcweir 	basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
1754cdf0e10cSrcweir 	const sal_uInt32 nPointCount = rPolygon.count();
1755cdf0e10cSrcweir 	const basegfx::B2DPoint aB2DControlPoint1( pCP1->x, pCP1->y );
1756cdf0e10cSrcweir 	rPolygon.setNextControlPoint( nPointCount-1, aB2DControlPoint1 );
1757cdf0e10cSrcweir 	const basegfx::B2DPoint aB2DEndPoint( pPoint->x, pPoint->y );
1758cdf0e10cSrcweir 	rPolygon.append( aB2DEndPoint );
1759cdf0e10cSrcweir 	const basegfx::B2DPoint aB2DControlPoint2( pCP2->x, pCP2->y );
1760cdf0e10cSrcweir 	rPolygon.setPrevControlPoint( nPointCount, aB2DControlPoint2 );
1761cdf0e10cSrcweir 	return noErr;
1762cdf0e10cSrcweir }
1763cdf0e10cSrcweir 
1764cdf0e10cSrcweir static OSStatus GgoClosePathProc( void* pData )
1765cdf0e10cSrcweir {
1766cdf0e10cSrcweir 	GgoData* pGgoData = static_cast<GgoData*>(pData);
1767cdf0e10cSrcweir 	basegfx::B2DPolygon& rPolygon = pGgoData->maPolygon;
1768cdf0e10cSrcweir 	if( rPolygon.count() > 0 )
1769cdf0e10cSrcweir 		pGgoData->mpPolyPoly->append( rPolygon );
1770cdf0e10cSrcweir 	rPolygon.clear();
1771cdf0e10cSrcweir 	return noErr;
1772cdf0e10cSrcweir }
1773cdf0e10cSrcweir 
1774cdf0e10cSrcweir static OSStatus GgoMoveToProc( const Float32Point* pPoint, void* pData )
1775cdf0e10cSrcweir {
1776cdf0e10cSrcweir 	GgoClosePathProc( pData );
1777cdf0e10cSrcweir 	OSStatus eStatus = GgoLineToProc( pPoint, pData );
1778cdf0e10cSrcweir 	return eStatus;
1779cdf0e10cSrcweir }
1780cdf0e10cSrcweir 
1781cdf0e10cSrcweir sal_Bool AquaSalGraphics::GetGlyphOutline( long nGlyphId, basegfx::B2DPolyPolygon& rPolyPoly )
1782cdf0e10cSrcweir {
1783cdf0e10cSrcweir 	GgoData aGgoData;
1784cdf0e10cSrcweir 	aGgoData.mpPolyPoly = &rPolyPoly;
1785cdf0e10cSrcweir 	rPolyPoly.clear();
1786cdf0e10cSrcweir 
1787cdf0e10cSrcweir 	ATSUStyle rATSUStyle = maATSUStyle;	// TODO: handle glyph fallback when CWS pdffix02 is integrated
1788cdf0e10cSrcweir 	OSStatus eGgoStatus = noErr;
1789cdf0e10cSrcweir 	OSStatus eStatus = ATSUGlyphGetCubicPaths( rATSUStyle, nGlyphId,
1790cdf0e10cSrcweir 		GgoMoveToProc, GgoLineToProc, GgoCurveToProc, GgoClosePathProc,
1791cdf0e10cSrcweir 		&aGgoData, &eGgoStatus );
1792cdf0e10cSrcweir 	if( (eStatus != noErr) ) // TODO: why is (eGgoStatus!=noErr) when curves are involved?
1793cdf0e10cSrcweir 		return false;
1794cdf0e10cSrcweir 
1795cdf0e10cSrcweir 	GgoClosePathProc( &aGgoData );
1796cdf0e10cSrcweir 	if( mfFontScale != 1.0 ) {
1797cdf0e10cSrcweir 		rPolyPoly.transform(basegfx::tools::createScaleB2DHomMatrix(+mfFontScale, +mfFontScale));
1798cdf0e10cSrcweir 	}
1799cdf0e10cSrcweir 	return true;
1800cdf0e10cSrcweir }
1801cdf0e10cSrcweir 
1802cdf0e10cSrcweir // -----------------------------------------------------------------------
1803cdf0e10cSrcweir 
1804cdf0e10cSrcweir long AquaSalGraphics::GetGraphicsWidth() const
1805cdf0e10cSrcweir {
1806cdf0e10cSrcweir     long w = 0;
1807cdf0e10cSrcweir     if( mrContext && (mbWindow || mbVirDev) )
1808cdf0e10cSrcweir     {
1809cdf0e10cSrcweir         w = mnWidth;
1810cdf0e10cSrcweir     }
1811cdf0e10cSrcweir 
1812cdf0e10cSrcweir     if( w == 0 )
1813cdf0e10cSrcweir     {
1814cdf0e10cSrcweir         if( mbWindow && mpFrame )
1815cdf0e10cSrcweir             w = mpFrame->maGeometry.nWidth;
1816cdf0e10cSrcweir     }
1817cdf0e10cSrcweir 
1818cdf0e10cSrcweir     return w;
1819cdf0e10cSrcweir }
1820cdf0e10cSrcweir 
1821cdf0e10cSrcweir // -----------------------------------------------------------------------
1822cdf0e10cSrcweir 
1823cdf0e10cSrcweir sal_Bool AquaSalGraphics::GetGlyphBoundRect( long nGlyphId, Rectangle& rRect )
1824cdf0e10cSrcweir {
1825cdf0e10cSrcweir 	ATSUStyle rATSUStyle = maATSUStyle;	// TODO: handle glyph fallback
1826cdf0e10cSrcweir 	GlyphID aGlyphId = nGlyphId;
1827cdf0e10cSrcweir 	ATSGlyphScreenMetrics aGlyphMetrics;
1828cdf0e10cSrcweir 	OSStatus eStatus = ATSUGlyphGetScreenMetrics( rATSUStyle,
1829cdf0e10cSrcweir 		1, &aGlyphId, 0, FALSE, !mbNonAntialiasedText, &aGlyphMetrics );
1830cdf0e10cSrcweir 	if( eStatus != noErr )
1831cdf0e10cSrcweir 		return false;
1832cdf0e10cSrcweir 
1833cdf0e10cSrcweir 	const long nMinX = (long)(+aGlyphMetrics.topLeft.x * mfFontScale - 0.5);
1834cdf0e10cSrcweir 	const long nMaxX = (long)(aGlyphMetrics.width * mfFontScale + 0.5) + nMinX;
1835cdf0e10cSrcweir 	const long nMinY = (long)(-aGlyphMetrics.topLeft.y * mfFontScale - 0.5);
1836cdf0e10cSrcweir 	const long nMaxY = (long)(aGlyphMetrics.height * mfFontScale + 0.5) + nMinY;
1837cdf0e10cSrcweir 	rRect = Rectangle( nMinX, nMinY, nMaxX, nMaxY );
1838cdf0e10cSrcweir 	return true;
1839cdf0e10cSrcweir }
1840cdf0e10cSrcweir 
1841cdf0e10cSrcweir // -----------------------------------------------------------------------
1842cdf0e10cSrcweir 
1843cdf0e10cSrcweir void AquaSalGraphics::GetDevFontSubstList( OutputDevice* )
1844cdf0e10cSrcweir {
1845cdf0e10cSrcweir 	// nothing to do since there are no device-specific fonts on Aqua
1846cdf0e10cSrcweir }
1847cdf0e10cSrcweir 
1848cdf0e10cSrcweir // -----------------------------------------------------------------------
1849cdf0e10cSrcweir 
1850cdf0e10cSrcweir void AquaSalGraphics::DrawServerFontLayout( const ServerFontLayout& )
1851cdf0e10cSrcweir {
1852cdf0e10cSrcweir }
1853cdf0e10cSrcweir 
1854cdf0e10cSrcweir // -----------------------------------------------------------------------
1855cdf0e10cSrcweir 
1856cdf0e10cSrcweir sal_uInt16 AquaSalGraphics::SetFont( ImplFontSelectData* pReqFont, int /*nFallbackLevel*/ )
1857cdf0e10cSrcweir {
1858cdf0e10cSrcweir     if( !pReqFont )
1859cdf0e10cSrcweir     {
1860cdf0e10cSrcweir     	ATSUClearStyle( maATSUStyle );
1861cdf0e10cSrcweir 		mpMacFontData = NULL;
1862cdf0e10cSrcweir         return 0;
1863cdf0e10cSrcweir     }
1864cdf0e10cSrcweir 
1865cdf0e10cSrcweir     // store the requested device font entry
1866cdf0e10cSrcweir     const ImplMacFontData* pMacFont = static_cast<const ImplMacFontData*>( pReqFont->mpFontData );
1867cdf0e10cSrcweir     mpMacFontData = pMacFont;
1868cdf0e10cSrcweir 
1869cdf0e10cSrcweir     // convert pixel units (as seen by upper layers) to typographic point units
1870cdf0e10cSrcweir     double fScaledAtsHeight = pReqFont->mfExactHeight;
1871cdf0e10cSrcweir     // avoid Fixed16.16 overflows by limiting the ATS font size
1872cdf0e10cSrcweir     static const float fMaxAtsHeight = 144.0;
1873cdf0e10cSrcweir     if( fScaledAtsHeight <= fMaxAtsHeight )
1874cdf0e10cSrcweir         mfFontScale = 1.0;
1875cdf0e10cSrcweir     else
1876cdf0e10cSrcweir     {
1877cdf0e10cSrcweir         mfFontScale = fScaledAtsHeight / fMaxAtsHeight;
1878cdf0e10cSrcweir         fScaledAtsHeight = fMaxAtsHeight;
1879cdf0e10cSrcweir     }
1880cdf0e10cSrcweir     Fixed fFixedSize = FloatToFixed( fScaledAtsHeight );
1881cdf0e10cSrcweir     // enable bold-emulation if needed
1882cdf0e10cSrcweir     Boolean bFakeBold = FALSE;
1883cdf0e10cSrcweir     if( (pReqFont->GetWeight() >= WEIGHT_BOLD)
1884cdf0e10cSrcweir     &&  (pMacFont->GetWeight() < WEIGHT_SEMIBOLD) )
1885cdf0e10cSrcweir         bFakeBold = TRUE;
1886cdf0e10cSrcweir     // enable italic-emulation if needed
1887cdf0e10cSrcweir     Boolean bFakeItalic = FALSE;
1888cdf0e10cSrcweir     if( ((pReqFont->GetSlant() == ITALIC_NORMAL) || (pReqFont->GetSlant() == ITALIC_OBLIQUE))
1889cdf0e10cSrcweir     && !((pMacFont->GetSlant() == ITALIC_NORMAL) || (pMacFont->GetSlant() == ITALIC_OBLIQUE)) )
1890cdf0e10cSrcweir         bFakeItalic = TRUE;
1891cdf0e10cSrcweir 
1892cdf0e10cSrcweir     // enable/disable antialiased text
1893cdf0e10cSrcweir     mbNonAntialiasedText = pReqFont->mbNonAntialiased;
1894cdf0e10cSrcweir     UInt32 nStyleRenderingOptions = kATSStyleNoOptions;
1895cdf0e10cSrcweir     if( pReqFont->mbNonAntialiased )
1896cdf0e10cSrcweir         nStyleRenderingOptions |= kATSStyleNoAntiAliasing;
1897cdf0e10cSrcweir 
1898cdf0e10cSrcweir 	// set horizontal/vertical mode
1899cdf0e10cSrcweir 	ATSUVerticalCharacterType aVerticalCharacterType = kATSUStronglyHorizontal;
1900cdf0e10cSrcweir 	if( pReqFont->mbVertical )
1901cdf0e10cSrcweir 		aVerticalCharacterType = kATSUStronglyVertical;
1902cdf0e10cSrcweir 
1903cdf0e10cSrcweir 	// prepare ATS-fontid as type matching to the kATSUFontTag request
1904cdf0e10cSrcweir 	ATSUFontID nFontID = static_cast<ATSUFontID>(pMacFont->GetFontId());
1905cdf0e10cSrcweir 
1906cdf0e10cSrcweir     // update ATSU style attributes with requested font parameters
1907cdf0e10cSrcweir 	// TODO: no need to set styles which are already defaulted
1908cdf0e10cSrcweir 
1909cdf0e10cSrcweir     const ATSUAttributeTag aTag[] =
1910cdf0e10cSrcweir     {
1911cdf0e10cSrcweir         kATSUFontTag,
1912cdf0e10cSrcweir         kATSUSizeTag,
1913cdf0e10cSrcweir         kATSUQDBoldfaceTag,
1914cdf0e10cSrcweir         kATSUQDItalicTag,
1915cdf0e10cSrcweir         kATSUStyleRenderingOptionsTag,
1916cdf0e10cSrcweir 		kATSUVerticalCharacterTag
1917cdf0e10cSrcweir     };
1918cdf0e10cSrcweir 
1919cdf0e10cSrcweir     const ByteCount aValueSize[] =
1920cdf0e10cSrcweir     {
1921cdf0e10cSrcweir         sizeof(ATSUFontID),
1922cdf0e10cSrcweir         sizeof(fFixedSize),
1923cdf0e10cSrcweir         sizeof(bFakeBold),
1924cdf0e10cSrcweir         sizeof(bFakeItalic),
1925cdf0e10cSrcweir         sizeof(nStyleRenderingOptions),
1926cdf0e10cSrcweir         sizeof(aVerticalCharacterType)
1927cdf0e10cSrcweir     };
1928cdf0e10cSrcweir 
1929cdf0e10cSrcweir     const ATSUAttributeValuePtr aValue[] =
1930cdf0e10cSrcweir     {
1931cdf0e10cSrcweir         &nFontID,
1932cdf0e10cSrcweir         &fFixedSize,
1933cdf0e10cSrcweir         &bFakeBold,
1934cdf0e10cSrcweir         &bFakeItalic,
1935cdf0e10cSrcweir         &nStyleRenderingOptions,
1936cdf0e10cSrcweir         &aVerticalCharacterType
1937cdf0e10cSrcweir     };
1938cdf0e10cSrcweir 
1939cdf0e10cSrcweir 	static const int nTagCount = sizeof(aTag) / sizeof(*aTag);
1940cdf0e10cSrcweir     OSStatus eStatus = ATSUSetAttributes( maATSUStyle, nTagCount,
1941cdf0e10cSrcweir                              aTag, aValueSize, aValue );
1942cdf0e10cSrcweir     // reset ATSUstyle if there was an error
1943cdf0e10cSrcweir     if( eStatus != noErr )
1944cdf0e10cSrcweir     {
1945cdf0e10cSrcweir         DBG_WARNING( "AquaSalGraphics::SetFont() : Could not set font attributes!\n");
1946cdf0e10cSrcweir         ATSUClearStyle( maATSUStyle );
1947cdf0e10cSrcweir         mpMacFontData = NULL;
1948cdf0e10cSrcweir         return 0;
1949cdf0e10cSrcweir     }
1950cdf0e10cSrcweir 
1951cdf0e10cSrcweir     // prepare font stretching
1952cdf0e10cSrcweir     const ATSUAttributeTag aMatrixTag = kATSUFontMatrixTag;
1953cdf0e10cSrcweir     if( (pReqFont->mnWidth == 0) || (pReqFont->mnWidth == pReqFont->mnHeight) )
1954cdf0e10cSrcweir     {
1955cdf0e10cSrcweir         mfFontStretch = 1.0;
1956cdf0e10cSrcweir     	ATSUClearAttributes( maATSUStyle, 1, &aMatrixTag );
1957cdf0e10cSrcweir     }
1958cdf0e10cSrcweir     else
1959cdf0e10cSrcweir     {
1960cdf0e10cSrcweir         mfFontStretch = (float)pReqFont->mnWidth / pReqFont->mnHeight;
1961cdf0e10cSrcweir         CGAffineTransform aMatrix = CGAffineTransformMakeScale( mfFontStretch, 1.0F );
1962cdf0e10cSrcweir         const ATSUAttributeValuePtr aAttr = &aMatrix;
1963cdf0e10cSrcweir         const ByteCount aMatrixBytes = sizeof(aMatrix);
1964cdf0e10cSrcweir         eStatus = ATSUSetAttributes( maATSUStyle, 1, &aMatrixTag, &aMatrixBytes, &aAttr );
1965cdf0e10cSrcweir         DBG_ASSERT( (eStatus==noErr), "AquaSalGraphics::SetFont() : Could not set font matrix\n");
1966cdf0e10cSrcweir     }
1967cdf0e10cSrcweir 
1968cdf0e10cSrcweir     // prepare font rotation
1969cdf0e10cSrcweir     mnATSUIRotation = FloatToFixed( pReqFont->mnOrientation / 10.0 );
1970cdf0e10cSrcweir 
1971cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 3
1972cdf0e10cSrcweir     fprintf( stderr, "SetFont to (\"%s\", \"%s\", fontid=%d) for (\"%s\" \"%s\" weight=%d, slant=%d size=%dx%d orientation=%d)\n",
1973cdf0e10cSrcweir              ::rtl::OUStringToOString( pMacFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ).getStr(),
1974cdf0e10cSrcweir              ::rtl::OUStringToOString( pMacFont->GetStyleName(), RTL_TEXTENCODING_UTF8 ).getStr(),
1975cdf0e10cSrcweir              (int)nFontID,
1976cdf0e10cSrcweir              ::rtl::OUStringToOString( pReqFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ).getStr(),
1977cdf0e10cSrcweir              ::rtl::OUStringToOString( pReqFont->GetStyleName(), RTL_TEXTENCODING_UTF8 ).getStr(),
1978cdf0e10cSrcweir              pReqFont->GetWeight(),
1979cdf0e10cSrcweir              pReqFont->GetSlant(),
1980cdf0e10cSrcweir              pReqFont->mnHeight,
1981cdf0e10cSrcweir              pReqFont->mnWidth,
1982cdf0e10cSrcweir              pReqFont->mnOrientation);
1983cdf0e10cSrcweir #endif
1984cdf0e10cSrcweir 
1985cdf0e10cSrcweir     return 0;
1986cdf0e10cSrcweir }
1987cdf0e10cSrcweir 
1988cdf0e10cSrcweir // -----------------------------------------------------------------------
1989cdf0e10cSrcweir 
1990cdf0e10cSrcweir const ImplFontCharMap* AquaSalGraphics::GetImplFontCharMap() const
1991cdf0e10cSrcweir {
1992cdf0e10cSrcweir 	if( !mpMacFontData )
1993cdf0e10cSrcweir 		return ImplFontCharMap::GetDefaultMap();
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir 	return mpMacFontData->GetImplFontCharMap();
1996cdf0e10cSrcweir }
1997cdf0e10cSrcweir 
1998cdf0e10cSrcweir // -----------------------------------------------------------------------
1999cdf0e10cSrcweir 
2000cdf0e10cSrcweir // fake a SFNT font directory entry for a font table
2001cdf0e10cSrcweir // see http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6.html#Directory
2002cdf0e10cSrcweir static void FakeDirEntry( FourCharCode eFCC, ByteCount nOfs, ByteCount nLen,
2003cdf0e10cSrcweir 	const unsigned char* /*pData*/, unsigned char*& rpDest )
2004cdf0e10cSrcweir {
2005cdf0e10cSrcweir 	// write entry tag
2006cdf0e10cSrcweir 	rpDest[ 0] = (char)(eFCC >> 24);
2007cdf0e10cSrcweir 	rpDest[ 1] = (char)(eFCC >> 16);
2008cdf0e10cSrcweir 	rpDest[ 2] = (char)(eFCC >>  8);
2009cdf0e10cSrcweir 	rpDest[ 3] = (char)(eFCC >>  0);
2010cdf0e10cSrcweir 	// TODO: get entry checksum and write it
2011cdf0e10cSrcweir 	// 		not too important since the subsetter doesn't care currently
2012cdf0e10cSrcweir 	// 		for( pData+nOfs ... pData+nOfs+nLen )
2013cdf0e10cSrcweir 	// write entry offset
2014cdf0e10cSrcweir 	rpDest[ 8] = (char)(nOfs >> 24);
2015cdf0e10cSrcweir 	rpDest[ 9] = (char)(nOfs >> 16);
2016cdf0e10cSrcweir 	rpDest[10] = (char)(nOfs >>  8);
2017cdf0e10cSrcweir 	rpDest[11] = (char)(nOfs >>  0);
2018cdf0e10cSrcweir 	// write entry length
2019cdf0e10cSrcweir 	rpDest[12] = (char)(nLen >> 24);
2020cdf0e10cSrcweir 	rpDest[13] = (char)(nLen >> 16);
2021cdf0e10cSrcweir 	rpDest[14] = (char)(nLen >>  8);
2022cdf0e10cSrcweir 	rpDest[15] = (char)(nLen >>  0);
2023cdf0e10cSrcweir 	// advance to next entry
2024cdf0e10cSrcweir 	rpDest += 16;
2025cdf0e10cSrcweir }
2026cdf0e10cSrcweir 
2027cdf0e10cSrcweir static bool GetRawFontData( const ImplFontData* pFontData,
2028cdf0e10cSrcweir 	ByteVector& rBuffer, bool* pJustCFF )
2029cdf0e10cSrcweir {
2030cdf0e10cSrcweir 	const ImplMacFontData* pMacFont = static_cast<const ImplMacFontData*>(pFontData);
2031cdf0e10cSrcweir 	const ATSUFontID nFontId = static_cast<ATSUFontID>(pMacFont->GetFontId());
2032cdf0e10cSrcweir 	ATSFontRef rFont = FMGetATSFontRefFromFont( nFontId );
2033cdf0e10cSrcweir 
2034cdf0e10cSrcweir 	ByteCount nCffLen = 0;
2035cdf0e10cSrcweir 	OSStatus eStatus = ATSFontGetTable( rFont, GetTag("CFF "), 0, 0, NULL, &nCffLen);
2036cdf0e10cSrcweir 	if( pJustCFF != NULL )
2037cdf0e10cSrcweir 	{
2038cdf0e10cSrcweir 		*pJustCFF = (eStatus == noErr) && (nCffLen > 0);
2039cdf0e10cSrcweir 		if( *pJustCFF )
2040cdf0e10cSrcweir 		{
2041cdf0e10cSrcweir 			rBuffer.resize( nCffLen );
2042cdf0e10cSrcweir 			eStatus = ATSFontGetTable( rFont, GetTag("CFF "), 0, nCffLen, (void*)&rBuffer[0], &nCffLen);
2043cdf0e10cSrcweir 			if( (eStatus != noErr) || (nCffLen <= 0) )
2044cdf0e10cSrcweir 				return false;
2045cdf0e10cSrcweir 			return true;
2046cdf0e10cSrcweir 		}
2047cdf0e10cSrcweir 	}
2048cdf0e10cSrcweir 
2049cdf0e10cSrcweir 	// get font table availability and size in bytes
2050cdf0e10cSrcweir 	ByteCount nHeadLen	= 0;
2051cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("head"), 0, 0, NULL, &nHeadLen);
2052cdf0e10cSrcweir 	if( (eStatus != noErr) || (nHeadLen <= 0) )
2053cdf0e10cSrcweir 		return false;
2054cdf0e10cSrcweir 	ByteCount nMaxpLen	= 0;
2055cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("maxp"), 0, 0, NULL, &nMaxpLen);
2056cdf0e10cSrcweir 	if( (eStatus != noErr) || (nMaxpLen <= 0) )
2057cdf0e10cSrcweir 		return false;
2058cdf0e10cSrcweir 	ByteCount nCmapLen	= 0;
2059cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, 0, NULL, &nCmapLen);
2060cdf0e10cSrcweir 	if( (eStatus != noErr) || (nCmapLen <= 0) )
2061cdf0e10cSrcweir 		return false;
2062cdf0e10cSrcweir 	ByteCount nNameLen	= 0;
2063cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("name"), 0, 0, NULL, &nNameLen);
2064cdf0e10cSrcweir 	if( (eStatus != noErr) || (nNameLen <= 0) )
2065cdf0e10cSrcweir 		return false;
2066cdf0e10cSrcweir 	ByteCount nHheaLen	= 0;
2067cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("hhea"), 0, 0, NULL, &nHheaLen);
2068cdf0e10cSrcweir 	if( (eStatus != noErr) || (nHheaLen <= 0) )
2069cdf0e10cSrcweir 		return false;
2070cdf0e10cSrcweir 	ByteCount nHmtxLen	= 0;
2071cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("hmtx"), 0, 0, NULL, &nHmtxLen);
2072cdf0e10cSrcweir 	if( (eStatus != noErr) || (nHmtxLen <= 0) )
2073cdf0e10cSrcweir 		return false;
2074cdf0e10cSrcweir 
2075cdf0e10cSrcweir 	// get the glyph outline tables
2076cdf0e10cSrcweir 	ByteCount nLocaLen	= 0;
2077cdf0e10cSrcweir 	ByteCount nGlyfLen	= 0;
2078cdf0e10cSrcweir 	if( (eStatus != noErr) || (nCffLen <= 0) )
2079cdf0e10cSrcweir 	{
2080cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("loca"), 0, 0, NULL, &nLocaLen);
2081cdf0e10cSrcweir 		if( (eStatus != noErr) || (nLocaLen <= 0) )
2082cdf0e10cSrcweir 			return false;
2083cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("glyf"), 0, 0, NULL, &nGlyfLen);
2084cdf0e10cSrcweir 		if( (eStatus != noErr) || (nGlyfLen <= 0) )
2085cdf0e10cSrcweir 			return false;
2086cdf0e10cSrcweir 	}
2087cdf0e10cSrcweir 
2088cdf0e10cSrcweir 	ByteCount nPrepLen=0, nCvtLen=0, nFpgmLen=0;
2089cdf0e10cSrcweir 	if( nGlyfLen )	// TODO: reduce PDF size by making hint subsetting optional
2090cdf0e10cSrcweir 	{
2091cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("prep"), 0, 0, NULL, &nPrepLen);
2092cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("cvt "), 0, 0, NULL, &nCvtLen);
2093cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("fpgm"), 0, 0, NULL, &nFpgmLen);
2094cdf0e10cSrcweir 	}
2095cdf0e10cSrcweir 
2096cdf0e10cSrcweir 	// prepare a byte buffer for a fake font
2097cdf0e10cSrcweir 	int nTableCount = 7;
2098cdf0e10cSrcweir 	nTableCount += (nPrepLen>0) + (nCvtLen>0) + (nFpgmLen>0) + (nGlyfLen>0);
2099cdf0e10cSrcweir 	const ByteCount nFdirLen = 12 + 16*nTableCount;
2100cdf0e10cSrcweir 	ByteCount nTotalLen = nFdirLen;
2101cdf0e10cSrcweir 	nTotalLen += nHeadLen + nMaxpLen + nNameLen + nCmapLen;
2102cdf0e10cSrcweir 	if( nGlyfLen )
2103cdf0e10cSrcweir 		nTotalLen += nLocaLen + nGlyfLen;
2104cdf0e10cSrcweir 	else
2105cdf0e10cSrcweir 		nTotalLen += nCffLen;
2106cdf0e10cSrcweir 	nTotalLen += nHheaLen + nHmtxLen;
2107cdf0e10cSrcweir 	nTotalLen += nPrepLen + nCvtLen + nFpgmLen;
2108cdf0e10cSrcweir 	rBuffer.resize( nTotalLen );
2109cdf0e10cSrcweir 
2110cdf0e10cSrcweir 	// fake a SFNT font directory header
2111cdf0e10cSrcweir 	if( nTableCount < 16 )
2112cdf0e10cSrcweir 	{
2113cdf0e10cSrcweir 		int nLog2 = 0;
2114cdf0e10cSrcweir 		while( (nTableCount >> nLog2) > 1 ) ++nLog2;
2115cdf0e10cSrcweir 		rBuffer[ 1] = 1;						// Win-TTF style scaler
2116cdf0e10cSrcweir 		rBuffer[ 5] = nTableCount;				// table count
2117cdf0e10cSrcweir 		rBuffer[ 7] = nLog2*16;					// searchRange
2118cdf0e10cSrcweir 		rBuffer[ 9] = nLog2;					// entrySelector
2119cdf0e10cSrcweir 		rBuffer[11] = (nTableCount-nLog2)*16;	// rangeShift
2120cdf0e10cSrcweir 	}
2121cdf0e10cSrcweir 
2122cdf0e10cSrcweir 	// get font table raw data and update the fake directory entries
2123cdf0e10cSrcweir 	ByteCount nOfs = nFdirLen;
2124cdf0e10cSrcweir 	unsigned char* pFakeEntry = &rBuffer[12];
2125cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("cmap"), 0, nCmapLen, (void*)&rBuffer[nOfs], &nCmapLen);
2126cdf0e10cSrcweir 	FakeDirEntry( GetTag("cmap"), nOfs, nCmapLen, &rBuffer[0], pFakeEntry );
2127cdf0e10cSrcweir 	nOfs += nCmapLen;
2128cdf0e10cSrcweir 	if( nCvtLen ) {
2129cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("cvt "), 0, nCvtLen, (void*)&rBuffer[nOfs], &nCvtLen);
2130cdf0e10cSrcweir 		FakeDirEntry( GetTag("cvt "), nOfs, nCvtLen, &rBuffer[0], pFakeEntry );
2131cdf0e10cSrcweir 		nOfs += nCvtLen;
2132cdf0e10cSrcweir 	}
2133cdf0e10cSrcweir 	if( nFpgmLen ) {
2134cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("fpgm"), 0, nFpgmLen, (void*)&rBuffer[nOfs], &nFpgmLen);
2135cdf0e10cSrcweir 		FakeDirEntry( GetTag("fpgm"), nOfs, nFpgmLen, &rBuffer[0], pFakeEntry );
2136cdf0e10cSrcweir 		nOfs += nFpgmLen;
2137cdf0e10cSrcweir 	}
2138cdf0e10cSrcweir 	if( nCffLen ) {
2139cdf0e10cSrcweir 	    eStatus = ATSFontGetTable( rFont, GetTag("CFF "), 0, nCffLen, (void*)&rBuffer[nOfs], &nCffLen);
2140cdf0e10cSrcweir 		FakeDirEntry( GetTag("CFF "), nOfs, nCffLen, &rBuffer[0], pFakeEntry );
2141cdf0e10cSrcweir 		nOfs += nGlyfLen;
2142cdf0e10cSrcweir 	} else {
2143cdf0e10cSrcweir 	    eStatus = ATSFontGetTable( rFont, GetTag("glyf"), 0, nGlyfLen, (void*)&rBuffer[nOfs], &nGlyfLen);
2144cdf0e10cSrcweir 		FakeDirEntry( GetTag("glyf"), nOfs, nGlyfLen, &rBuffer[0], pFakeEntry );
2145cdf0e10cSrcweir 		nOfs += nGlyfLen;
2146cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("loca"), 0, nLocaLen, (void*)&rBuffer[nOfs], &nLocaLen);
2147cdf0e10cSrcweir 		FakeDirEntry( GetTag("loca"), nOfs, nLocaLen, &rBuffer[0], pFakeEntry );
2148cdf0e10cSrcweir 		nOfs += nLocaLen;
2149cdf0e10cSrcweir 	}
2150cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("head"), 0, nHeadLen, (void*)&rBuffer[nOfs], &nHeadLen);
2151cdf0e10cSrcweir 	FakeDirEntry( GetTag("head"), nOfs, nHeadLen, &rBuffer[0], pFakeEntry );
2152cdf0e10cSrcweir 	nOfs += nHeadLen;
2153cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("hhea"), 0, nHheaLen, (void*)&rBuffer[nOfs], &nHheaLen);
2154cdf0e10cSrcweir 	FakeDirEntry( GetTag("hhea"), nOfs, nHheaLen, &rBuffer[0], pFakeEntry );
2155cdf0e10cSrcweir 	nOfs += nHheaLen;
2156cdf0e10cSrcweir 	eStatus = ATSFontGetTable( rFont, GetTag("hmtx"), 0, nHmtxLen, (void*)&rBuffer[nOfs], &nHmtxLen);
2157cdf0e10cSrcweir 	FakeDirEntry( GetTag("hmtx"), nOfs, nHmtxLen, &rBuffer[0], pFakeEntry );
2158cdf0e10cSrcweir 	nOfs += nHmtxLen;
2159cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("maxp"), 0, nMaxpLen, (void*)&rBuffer[nOfs], &nMaxpLen);
2160cdf0e10cSrcweir 	FakeDirEntry( GetTag("maxp"), nOfs, nMaxpLen, &rBuffer[0], pFakeEntry );
2161cdf0e10cSrcweir 	nOfs += nMaxpLen;
2162cdf0e10cSrcweir     eStatus = ATSFontGetTable( rFont, GetTag("name"), 0, nNameLen, (void*)&rBuffer[nOfs], &nNameLen);
2163cdf0e10cSrcweir 	FakeDirEntry( GetTag("name"), nOfs, nNameLen, &rBuffer[0], pFakeEntry );
2164cdf0e10cSrcweir 	nOfs += nNameLen;
2165cdf0e10cSrcweir 	if( nPrepLen ) {
2166cdf0e10cSrcweir 		eStatus = ATSFontGetTable( rFont, GetTag("prep"), 0, nPrepLen, (void*)&rBuffer[nOfs], &nPrepLen);
2167cdf0e10cSrcweir 		FakeDirEntry( GetTag("prep"), nOfs, nPrepLen, &rBuffer[0], pFakeEntry );
2168cdf0e10cSrcweir 		nOfs += nPrepLen;
2169cdf0e10cSrcweir 	}
2170cdf0e10cSrcweir 
2171cdf0e10cSrcweir 	DBG_ASSERT( (nOfs==nTotalLen), "AquaSalGraphics::CreateFontSubset (nOfs!=nTotalLen)");
2172cdf0e10cSrcweir 
2173cdf0e10cSrcweir 	return sal_True;
2174cdf0e10cSrcweir }
2175cdf0e10cSrcweir 
2176cdf0e10cSrcweir sal_Bool AquaSalGraphics::CreateFontSubset( const rtl::OUString& rToFile,
2177cdf0e10cSrcweir 	const ImplFontData* pFontData, long* pGlyphIDs, sal_uInt8* pEncoding,
2178cdf0e10cSrcweir 	sal_Int32* pGlyphWidths, int nGlyphCount, FontSubsetInfo& rInfo )
2179cdf0e10cSrcweir {
2180cdf0e10cSrcweir 	// TODO: move more of the functionality here into the generic subsetter code
2181cdf0e10cSrcweir 
2182cdf0e10cSrcweir 	// prepare the requested file name for writing the font-subset file
2183cdf0e10cSrcweir     rtl::OUString aSysPath;
2184cdf0e10cSrcweir     if( osl_File_E_None != osl_getSystemPathFromFileURL( rToFile.pData, &aSysPath.pData ) )
2185cdf0e10cSrcweir         return sal_False;
2186cdf0e10cSrcweir     const rtl_TextEncoding aThreadEncoding = osl_getThreadTextEncoding();
2187cdf0e10cSrcweir     const ByteString aToFile( rtl::OUStringToOString( aSysPath, aThreadEncoding ) );
2188cdf0e10cSrcweir 
2189cdf0e10cSrcweir 	// get the raw-bytes from the font to be subset
2190cdf0e10cSrcweir 	ByteVector aBuffer;
2191cdf0e10cSrcweir 	bool bCffOnly = false;
2192cdf0e10cSrcweir 	if( !GetRawFontData( pFontData, aBuffer, &bCffOnly ) )
2193cdf0e10cSrcweir 		return sal_False;
2194cdf0e10cSrcweir 
2195cdf0e10cSrcweir 	// handle CFF-subsetting
2196cdf0e10cSrcweir 	if( bCffOnly )
2197cdf0e10cSrcweir 	{
2198cdf0e10cSrcweir 		// provide the raw-CFF data to the subsetter
2199cdf0e10cSrcweir 		ByteCount nCffLen = aBuffer.size();
2200cdf0e10cSrcweir 		rInfo.LoadFont( FontSubsetInfo::CFF_FONT, &aBuffer[0], nCffLen );
2201cdf0e10cSrcweir 
2202cdf0e10cSrcweir 		// NOTE: assuming that all glyphids requested on Aqua are fully translated
2203cdf0e10cSrcweir 
2204cdf0e10cSrcweir 		// make the subsetter provide the requested subset
2205cdf0e10cSrcweir 		FILE* pOutFile = fopen( aToFile.GetBuffer(), "wb" );
2206cdf0e10cSrcweir 		bool bRC = rInfo.CreateFontSubset( FontSubsetInfo::TYPE1_PFB, pOutFile, NULL,
2207cdf0e10cSrcweir 			pGlyphIDs, pEncoding, nGlyphCount, pGlyphWidths );
2208cdf0e10cSrcweir 		fclose( pOutFile );
2209cdf0e10cSrcweir 		return bRC;
2210cdf0e10cSrcweir 	}
2211cdf0e10cSrcweir 
2212cdf0e10cSrcweir 	// TODO: modernize psprint's horrible fontsubset C-API
2213cdf0e10cSrcweir 	// this probably only makes sense after the switch to another SCM
2214cdf0e10cSrcweir 	// that can preserve change history after file renames
2215cdf0e10cSrcweir 
2216cdf0e10cSrcweir 	// prepare data for psprint's font subsetter
2217cdf0e10cSrcweir 	TrueTypeFont* pSftFont = NULL;
2218cdf0e10cSrcweir 	int nRC = ::OpenTTFontBuffer( (void*)&aBuffer[0], aBuffer.size(), 0, &pSftFont);
2219cdf0e10cSrcweir 	if( nRC != SF_OK )
2220cdf0e10cSrcweir 		return sal_False;
2221cdf0e10cSrcweir 
2222cdf0e10cSrcweir 	// get details about the subsetted font
2223cdf0e10cSrcweir 	TTGlobalFontInfo aTTInfo;
2224cdf0e10cSrcweir 	::GetTTGlobalFontInfo( pSftFont, &aTTInfo );
2225cdf0e10cSrcweir 	rInfo.m_nFontType   = FontSubsetInfo::SFNT_TTF;
2226cdf0e10cSrcweir 	rInfo.m_aPSName     = String( aTTInfo.psname, RTL_TEXTENCODING_UTF8 );
2227cdf0e10cSrcweir 	rInfo.m_aFontBBox	= Rectangle( Point( aTTInfo.xMin, aTTInfo.yMin ),
2228cdf0e10cSrcweir                                     Point( aTTInfo.xMax, aTTInfo.yMax ) );
2229cdf0e10cSrcweir 	rInfo.m_nCapHeight	= aTTInfo.yMax; // Well ...
223059213536SEike Rathke 	rInfo.m_nAscent     = aTTInfo.winAscent;
223159213536SEike Rathke 	rInfo.m_nDescent    = aTTInfo.winDescent;
2232cdf0e10cSrcweir 	// mac fonts usually do not have an OS2-table
2233cdf0e10cSrcweir 	// => get valid ascent/descent values from other tables
2234cdf0e10cSrcweir 	if( !rInfo.m_nAscent )
2235cdf0e10cSrcweir 		rInfo.m_nAscent	= +aTTInfo.typoAscender;
2236cdf0e10cSrcweir 	if( !rInfo.m_nAscent )
2237cdf0e10cSrcweir 		rInfo.m_nAscent	= +aTTInfo.ascender;
2238cdf0e10cSrcweir 	if( !rInfo.m_nDescent )
2239cdf0e10cSrcweir 		rInfo.m_nDescent = +aTTInfo.typoDescender;
2240cdf0e10cSrcweir 	if( !rInfo.m_nDescent )
2241cdf0e10cSrcweir 		rInfo.m_nDescent = -aTTInfo.descender;
2242cdf0e10cSrcweir 
2243cdf0e10cSrcweir     // subset glyphs and get their properties
2244cdf0e10cSrcweir     // take care that subset fonts require the NotDef glyph in pos 0
2245cdf0e10cSrcweir     int nOrigCount = nGlyphCount;
2246cdf0e10cSrcweir     sal_uInt16    aShortIDs[ 256 ];
2247cdf0e10cSrcweir     sal_uInt8 aTempEncs[ 256 ];
2248cdf0e10cSrcweir 
2249cdf0e10cSrcweir     int nNotDef = -1;
2250cdf0e10cSrcweir     for( int i = 0; i < nGlyphCount; ++i )
2251cdf0e10cSrcweir     {
2252cdf0e10cSrcweir         aTempEncs[i] = pEncoding[i];
2253cdf0e10cSrcweir         sal_uInt32 nGlyphIdx = pGlyphIDs[i] & GF_IDXMASK;
2254cdf0e10cSrcweir         if( pGlyphIDs[i] & GF_ISCHAR )
2255cdf0e10cSrcweir         {
2256cdf0e10cSrcweir             bool bVertical = (pGlyphIDs[i] & GF_ROTMASK) != 0;
2257cdf0e10cSrcweir             nGlyphIdx = ::MapChar( pSftFont, static_cast<sal_uInt16>(nGlyphIdx), bVertical );
2258cdf0e10cSrcweir             if( nGlyphIdx == 0 && pFontData->IsSymbolFont() )
2259cdf0e10cSrcweir             {
2260cdf0e10cSrcweir                 // #i12824# emulate symbol aliasing U+FXXX <-> U+0XXX
2261cdf0e10cSrcweir                 nGlyphIdx = pGlyphIDs[i] & GF_IDXMASK;
2262cdf0e10cSrcweir                 nGlyphIdx = (nGlyphIdx & 0xF000) ? (nGlyphIdx & 0x00FF) : (nGlyphIdx | 0xF000 );
2263cdf0e10cSrcweir                 nGlyphIdx = ::MapChar( pSftFont, static_cast<sal_uInt16>(nGlyphIdx), bVertical );
2264cdf0e10cSrcweir             }
2265cdf0e10cSrcweir         }
2266cdf0e10cSrcweir         aShortIDs[i] = static_cast<sal_uInt16>( nGlyphIdx );
2267cdf0e10cSrcweir         if( !nGlyphIdx )
2268cdf0e10cSrcweir             if( nNotDef < 0 )
2269cdf0e10cSrcweir                 nNotDef = i; // first NotDef glyph found
2270cdf0e10cSrcweir     }
2271cdf0e10cSrcweir 
2272cdf0e10cSrcweir     if( nNotDef != 0 )
2273cdf0e10cSrcweir     {
2274cdf0e10cSrcweir         // add fake NotDef glyph if needed
2275cdf0e10cSrcweir         if( nNotDef < 0 )
2276cdf0e10cSrcweir             nNotDef = nGlyphCount++;
2277cdf0e10cSrcweir 
2278cdf0e10cSrcweir         // NotDef glyph must be in pos 0 => swap glyphids
2279cdf0e10cSrcweir         aShortIDs[ nNotDef ] = aShortIDs[0];
2280cdf0e10cSrcweir         aTempEncs[ nNotDef ] = aTempEncs[0];
2281cdf0e10cSrcweir         aShortIDs[0] = 0;
2282cdf0e10cSrcweir         aTempEncs[0] = 0;
2283cdf0e10cSrcweir     }
2284cdf0e10cSrcweir     DBG_ASSERT( nGlyphCount < 257, "too many glyphs for subsetting" );
2285cdf0e10cSrcweir 
2286cdf0e10cSrcweir 	// TODO: where to get bVertical?
2287cdf0e10cSrcweir 	const bool bVertical = false;
2288cdf0e10cSrcweir 
2289cdf0e10cSrcweir     // fill the pGlyphWidths array
2290cdf0e10cSrcweir 	// while making sure that the NotDef glyph is at index==0
2291cdf0e10cSrcweir     TTSimpleGlyphMetrics* pGlyphMetrics =
2292cdf0e10cSrcweir         ::GetTTSimpleGlyphMetrics( pSftFont, aShortIDs, nGlyphCount, bVertical );
2293cdf0e10cSrcweir     if( !pGlyphMetrics )
2294cdf0e10cSrcweir         return sal_False;
2295cdf0e10cSrcweir     sal_uInt16 nNotDefAdv   	= pGlyphMetrics[0].adv;
2296cdf0e10cSrcweir     pGlyphMetrics[0].adv    	= pGlyphMetrics[nNotDef].adv;
2297cdf0e10cSrcweir     pGlyphMetrics[nNotDef].adv	= nNotDefAdv;
2298cdf0e10cSrcweir     for( int i = 0; i < nOrigCount; ++i )
2299cdf0e10cSrcweir         pGlyphWidths[i] = pGlyphMetrics[i].adv;
2300cdf0e10cSrcweir     free( pGlyphMetrics );
2301cdf0e10cSrcweir 
2302cdf0e10cSrcweir     // write subset into destination file
2303cdf0e10cSrcweir     nRC = ::CreateTTFromTTGlyphs( pSftFont, aToFile.GetBuffer(), aShortIDs,
2304cdf0e10cSrcweir             aTempEncs, nGlyphCount, 0, NULL, 0 );
2305cdf0e10cSrcweir 	::CloseTTFont(pSftFont);
2306cdf0e10cSrcweir     return (nRC == SF_OK);
2307cdf0e10cSrcweir }
2308cdf0e10cSrcweir 
2309cdf0e10cSrcweir // -----------------------------------------------------------------------
2310cdf0e10cSrcweir 
2311cdf0e10cSrcweir void AquaSalGraphics::GetGlyphWidths( const ImplFontData* pFontData, bool bVertical,
2312cdf0e10cSrcweir 	Int32Vector& rGlyphWidths, Ucs2UIntMap& rUnicodeEnc )
2313cdf0e10cSrcweir {
2314cdf0e10cSrcweir     rGlyphWidths.clear();
2315cdf0e10cSrcweir     rUnicodeEnc.clear();
2316cdf0e10cSrcweir 
2317cdf0e10cSrcweir 	if( pFontData->IsSubsettable() )
2318cdf0e10cSrcweir     {
2319cdf0e10cSrcweir 		ByteVector aBuffer;
2320cdf0e10cSrcweir 		if( !GetRawFontData( pFontData, aBuffer, NULL ) )
2321cdf0e10cSrcweir 			return;
2322cdf0e10cSrcweir 
2323cdf0e10cSrcweir 		// TODO: modernize psprint's horrible fontsubset C-API
2324cdf0e10cSrcweir 		// this probably only makes sense after the switch to another SCM
2325cdf0e10cSrcweir 		// that can preserve change history after file renames
2326cdf0e10cSrcweir 
2327cdf0e10cSrcweir 		// use the font subsetter to get the widths
2328cdf0e10cSrcweir 		TrueTypeFont* pSftFont = NULL;
2329cdf0e10cSrcweir 		int nRC = ::OpenTTFontBuffer( (void*)&aBuffer[0], aBuffer.size(), 0, &pSftFont);
2330cdf0e10cSrcweir 		if( nRC != SF_OK )
2331cdf0e10cSrcweir 			return;
2332cdf0e10cSrcweir 
2333cdf0e10cSrcweir         const int nGlyphCount = ::GetTTGlyphCount( pSftFont );
2334cdf0e10cSrcweir         if( nGlyphCount > 0 )
2335cdf0e10cSrcweir         {
2336cdf0e10cSrcweir 			// get glyph metrics
2337cdf0e10cSrcweir             rGlyphWidths.resize(nGlyphCount);
2338cdf0e10cSrcweir             std::vector<sal_uInt16> aGlyphIds(nGlyphCount);
2339cdf0e10cSrcweir             for( int i = 0; i < nGlyphCount; i++ )
2340cdf0e10cSrcweir                 aGlyphIds[i] = static_cast<sal_uInt16>(i);
2341cdf0e10cSrcweir             const TTSimpleGlyphMetrics* pGlyphMetrics = ::GetTTSimpleGlyphMetrics(
2342cdf0e10cSrcweir 				pSftFont, &aGlyphIds[0], nGlyphCount, bVertical );
2343cdf0e10cSrcweir             if( pGlyphMetrics )
2344cdf0e10cSrcweir             {
2345cdf0e10cSrcweir                 for( int i = 0; i < nGlyphCount; ++i )
2346cdf0e10cSrcweir                     rGlyphWidths[i] = pGlyphMetrics[i].adv;
2347cdf0e10cSrcweir                 free( (void*)pGlyphMetrics );
2348cdf0e10cSrcweir             }
2349cdf0e10cSrcweir 
2350cdf0e10cSrcweir             const ImplFontCharMap* pMap = mpMacFontData->GetImplFontCharMap();
2351cdf0e10cSrcweir             DBG_ASSERT( pMap && pMap->GetCharCount(), "no charmap" );
2352cdf0e10cSrcweir             pMap->AddReference(); // TODO: add and use RAII object instead
2353cdf0e10cSrcweir 
2354cdf0e10cSrcweir 			// get unicode<->glyph encoding
2355cdf0e10cSrcweir 			// TODO? avoid sft mapping by using the pMap itself
2356cdf0e10cSrcweir 			int nCharCount = pMap->GetCharCount();
2357cdf0e10cSrcweir 			sal_uInt32 nChar = pMap->GetFirstChar();
2358cdf0e10cSrcweir 			for(; --nCharCount >= 0; nChar = pMap->GetNextChar( nChar ) )
2359cdf0e10cSrcweir             {
2360cdf0e10cSrcweir 				if( nChar > 0xFFFF ) // TODO: allow UTF-32 chars
2361cdf0e10cSrcweir 					break;
2362cdf0e10cSrcweir 				sal_Ucs nUcsChar = static_cast<sal_Ucs>(nChar);
2363cdf0e10cSrcweir                 sal_uInt32 nGlyph = ::MapChar( pSftFont, nUcsChar, bVertical );
2364cdf0e10cSrcweir                 if( nGlyph > 0 )
2365cdf0e10cSrcweir                     rUnicodeEnc[ nUcsChar ] = nGlyph;
2366cdf0e10cSrcweir             }
2367cdf0e10cSrcweir 
2368cdf0e10cSrcweir             pMap->DeReference(); // TODO: add and use RAII object instead
2369cdf0e10cSrcweir         }
2370cdf0e10cSrcweir 
2371cdf0e10cSrcweir 		::CloseTTFont( pSftFont );
2372cdf0e10cSrcweir     }
2373cdf0e10cSrcweir     else if( pFontData->IsEmbeddable() )
2374cdf0e10cSrcweir     {
2375cdf0e10cSrcweir         // get individual character widths
2376cdf0e10cSrcweir #if 0 // FIXME
2377cdf0e10cSrcweir         rWidths.reserve( 224 );
2378cdf0e10cSrcweir         for( sal_Unicode i = 32; i < 256; ++i )
2379cdf0e10cSrcweir         {
2380cdf0e10cSrcweir             int nCharWidth = 0;
2381cdf0e10cSrcweir             if( ::GetCharWidth32W( mhDC, i, i, &nCharWidth ) )
2382cdf0e10cSrcweir             {
2383cdf0e10cSrcweir                 rUnicodeEnc[ i ] = rWidths.size();
2384cdf0e10cSrcweir                 rWidths.push_back( nCharWidth );
2385cdf0e10cSrcweir             }
2386cdf0e10cSrcweir         }
2387cdf0e10cSrcweir #else
2388cdf0e10cSrcweir 		DBG_ERROR("not implemented for non-subsettable fonts!\n");
2389cdf0e10cSrcweir #endif
2390cdf0e10cSrcweir     }
2391cdf0e10cSrcweir }
2392cdf0e10cSrcweir 
2393cdf0e10cSrcweir // -----------------------------------------------------------------------
2394cdf0e10cSrcweir 
2395cdf0e10cSrcweir const Ucs2SIntMap* AquaSalGraphics::GetFontEncodingVector(
2396cdf0e10cSrcweir 	const ImplFontData*, const Ucs2OStrMap** /*ppNonEncoded*/ )
2397cdf0e10cSrcweir {
2398cdf0e10cSrcweir     return NULL;
2399cdf0e10cSrcweir }
2400cdf0e10cSrcweir 
2401cdf0e10cSrcweir // -----------------------------------------------------------------------
2402cdf0e10cSrcweir 
2403cdf0e10cSrcweir const void*	AquaSalGraphics::GetEmbedFontData( const ImplFontData*,
2404cdf0e10cSrcweir                               const sal_Ucs* /*pUnicodes*/,
2405cdf0e10cSrcweir                               sal_Int32* /*pWidths*/,
2406cdf0e10cSrcweir                               FontSubsetInfo&,
2407cdf0e10cSrcweir                               long* /*pDataLen*/ )
2408cdf0e10cSrcweir {
2409cdf0e10cSrcweir     return NULL;
2410cdf0e10cSrcweir }
2411cdf0e10cSrcweir 
2412cdf0e10cSrcweir // -----------------------------------------------------------------------
2413cdf0e10cSrcweir 
2414cdf0e10cSrcweir void AquaSalGraphics::FreeEmbedFontData( const void* pData, long /*nDataLen*/ )
2415cdf0e10cSrcweir {
2416cdf0e10cSrcweir     // TODO: implementing this only makes sense when the implementation of
2417cdf0e10cSrcweir 	//		AquaSalGraphics::GetEmbedFontData() returns non-NULL
2418cdf0e10cSrcweir 	(void)pData;
2419cdf0e10cSrcweir 	DBG_ASSERT( (pData!=NULL), "AquaSalGraphics::FreeEmbedFontData() is not implemented\n");
2420cdf0e10cSrcweir }
2421cdf0e10cSrcweir 
2422cdf0e10cSrcweir // -----------------------------------------------------------------------
2423cdf0e10cSrcweir 
2424cdf0e10cSrcweir SystemFontData AquaSalGraphics::GetSysFontData( int /* nFallbacklevel */ ) const
2425cdf0e10cSrcweir {
2426cdf0e10cSrcweir     SystemFontData aSysFontData;
2427cdf0e10cSrcweir     OSStatus err;
2428cdf0e10cSrcweir     aSysFontData.nSize = sizeof( SystemFontData );
2429cdf0e10cSrcweir 
2430cdf0e10cSrcweir     // NOTE: Native ATSU font fallbacks are used, not the VCL fallbacks.
2431cdf0e10cSrcweir     ATSUFontID fontId;
2432cdf0e10cSrcweir     err = ATSUGetAttribute( maATSUStyle, kATSUFontTag, sizeof(fontId), &fontId, 0 );
2433cdf0e10cSrcweir     if (err) fontId = 0;
2434cdf0e10cSrcweir     aSysFontData.aATSUFontID = (void *) fontId;
2435cdf0e10cSrcweir 
2436cdf0e10cSrcweir     Boolean bFbold;
2437cdf0e10cSrcweir     err = ATSUGetAttribute( maATSUStyle, kATSUQDBoldfaceTag, sizeof(bFbold), &bFbold, 0 );
2438cdf0e10cSrcweir     if (err) bFbold = FALSE;
2439cdf0e10cSrcweir     aSysFontData.bFakeBold = (bool) bFbold;
2440cdf0e10cSrcweir 
2441cdf0e10cSrcweir     Boolean bFItalic;
2442cdf0e10cSrcweir     err = ATSUGetAttribute( maATSUStyle, kATSUQDItalicTag, sizeof(bFItalic), &bFItalic, 0 );
2443cdf0e10cSrcweir     if (err) bFItalic = FALSE;
2444cdf0e10cSrcweir     aSysFontData.bFakeItalic = (bool) bFItalic;
2445cdf0e10cSrcweir 
2446cdf0e10cSrcweir     ATSUVerticalCharacterType aVerticalCharacterType;
2447cdf0e10cSrcweir     err = ATSUGetAttribute( maATSUStyle, kATSUVerticalCharacterTag, sizeof(aVerticalCharacterType), &aVerticalCharacterType, 0 );
2448cdf0e10cSrcweir     if (!err && aVerticalCharacterType == kATSUStronglyVertical) {
2449cdf0e10cSrcweir         aSysFontData.bVerticalCharacterType = true;
2450cdf0e10cSrcweir     } else {
2451cdf0e10cSrcweir         aSysFontData.bVerticalCharacterType = false;
2452cdf0e10cSrcweir     }
2453cdf0e10cSrcweir 
2454cdf0e10cSrcweir     aSysFontData.bAntialias = !mbNonAntialiasedText;
2455cdf0e10cSrcweir 
2456cdf0e10cSrcweir     return aSysFontData;
2457cdf0e10cSrcweir }
2458cdf0e10cSrcweir 
2459cdf0e10cSrcweir // -----------------------------------------------------------------------
2460cdf0e10cSrcweir 
2461cdf0e10cSrcweir SystemGraphicsData AquaSalGraphics::GetGraphicsData() const
2462cdf0e10cSrcweir {
2463cdf0e10cSrcweir     SystemGraphicsData aRes;
2464cdf0e10cSrcweir     aRes.nSize = sizeof(aRes);
2465cdf0e10cSrcweir     aRes.rCGContext = mrContext;
2466cdf0e10cSrcweir     return aRes;
2467cdf0e10cSrcweir }
2468cdf0e10cSrcweir 
2469cdf0e10cSrcweir // -----------------------------------------------------------------------
2470cdf0e10cSrcweir 
2471cdf0e10cSrcweir void AquaSalGraphics::SetXORMode( bool bSet, bool bInvertOnly )
2472cdf0e10cSrcweir {
2473cdf0e10cSrcweir 	// return early if XOR mode remains unchanged
2474cdf0e10cSrcweir     if( mbPrinter )
2475cdf0e10cSrcweir     	return;
2476cdf0e10cSrcweir 
2477cdf0e10cSrcweir     if( ! bSet && mnXorMode == 2 )
2478cdf0e10cSrcweir     {
2479cdf0e10cSrcweir         CGContextSetBlendMode( mrContext, kCGBlendModeNormal );
2480cdf0e10cSrcweir         mnXorMode = 0;
2481cdf0e10cSrcweir         return;
2482cdf0e10cSrcweir     }
2483cdf0e10cSrcweir     else if( bSet && bInvertOnly && mnXorMode == 0)
2484cdf0e10cSrcweir     {
2485cdf0e10cSrcweir         CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
2486cdf0e10cSrcweir         mnXorMode = 2;
2487cdf0e10cSrcweir         return;
2488cdf0e10cSrcweir     }
2489cdf0e10cSrcweir 
2490cdf0e10cSrcweir 	if( (mpXorEmulation == NULL) && !bSet )
2491cdf0e10cSrcweir 		return;
2492cdf0e10cSrcweir 	if( (mpXorEmulation != NULL) && (bSet == mpXorEmulation->IsEnabled()) )
2493cdf0e10cSrcweir 		return;
2494cdf0e10cSrcweir 	if( !CheckContext() )
2495cdf0e10cSrcweir 	 	return;
2496cdf0e10cSrcweir 
2497cdf0e10cSrcweir 	// prepare XOR emulation
2498cdf0e10cSrcweir 	if( !mpXorEmulation )
2499cdf0e10cSrcweir 	{
2500cdf0e10cSrcweir 		mpXorEmulation = new XorEmulation();
2501cdf0e10cSrcweir 		mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer );
2502cdf0e10cSrcweir 	}
2503cdf0e10cSrcweir 
2504cdf0e10cSrcweir 	// change the XOR mode
2505cdf0e10cSrcweir 	if( bSet )
2506cdf0e10cSrcweir 	{
2507cdf0e10cSrcweir 		mpXorEmulation->Enable();
2508cdf0e10cSrcweir 		mrContext = mpXorEmulation->GetMaskContext();
2509cdf0e10cSrcweir         mnXorMode = 1;
2510cdf0e10cSrcweir 	}
2511cdf0e10cSrcweir 	else
2512cdf0e10cSrcweir 	{
2513cdf0e10cSrcweir 		mpXorEmulation->UpdateTarget();
2514cdf0e10cSrcweir 		mpXorEmulation->Disable();
2515cdf0e10cSrcweir 		mrContext = mpXorEmulation->GetTargetContext();
2516cdf0e10cSrcweir         mnXorMode = 0;
2517cdf0e10cSrcweir 	}
2518cdf0e10cSrcweir }
2519cdf0e10cSrcweir 
2520cdf0e10cSrcweir // -----------------------------------------------------------------------
2521cdf0e10cSrcweir 
2522cdf0e10cSrcweir // apply the XOR mask to the target context if active and dirty
2523cdf0e10cSrcweir void AquaSalGraphics::ApplyXorContext()
2524cdf0e10cSrcweir {
2525cdf0e10cSrcweir 	if( !mpXorEmulation )
2526cdf0e10cSrcweir 		return;
2527cdf0e10cSrcweir 	if( mpXorEmulation->UpdateTarget() )
2528cdf0e10cSrcweir 		RefreshRect( 0, 0, mnWidth, mnHeight ); // TODO: refresh minimal changerect
2529cdf0e10cSrcweir }
2530cdf0e10cSrcweir 
2531cdf0e10cSrcweir // ======================================================================
2532cdf0e10cSrcweir 
2533cdf0e10cSrcweir XorEmulation::XorEmulation()
2534cdf0e10cSrcweir :	mxTargetLayer( NULL )
2535cdf0e10cSrcweir ,	mxTargetContext( NULL )
2536cdf0e10cSrcweir ,	mxMaskContext( NULL )
2537cdf0e10cSrcweir ,	mxTempContext( NULL )
2538cdf0e10cSrcweir ,	mpMaskBuffer( NULL )
2539cdf0e10cSrcweir ,	mpTempBuffer( NULL )
2540cdf0e10cSrcweir ,	mnBufferLongs( 0 )
2541cdf0e10cSrcweir ,	mbIsEnabled( false )
2542cdf0e10cSrcweir {}
2543cdf0e10cSrcweir 
2544cdf0e10cSrcweir // ----------------------------------------------------------------------
2545cdf0e10cSrcweir 
2546cdf0e10cSrcweir XorEmulation::~XorEmulation()
2547cdf0e10cSrcweir {
2548cdf0e10cSrcweir 	Disable();
2549cdf0e10cSrcweir 	SetTarget( 0, 0, 0, NULL, NULL );
2550cdf0e10cSrcweir }
2551cdf0e10cSrcweir 
2552cdf0e10cSrcweir // -----------------------------------------------------------------------
2553cdf0e10cSrcweir 
2554cdf0e10cSrcweir void XorEmulation::SetTarget( int nWidth, int nHeight, int nTargetDepth,
2555cdf0e10cSrcweir 	CGContextRef xTargetContext, CGLayerRef xTargetLayer )
2556cdf0e10cSrcweir {
2557cdf0e10cSrcweir 	// prepare to replace old mask+temp context
2558cdf0e10cSrcweir 	if( mxMaskContext )
2559cdf0e10cSrcweir 	{
2560cdf0e10cSrcweir 		// cleanup the mask context
2561cdf0e10cSrcweir 		CGContextRelease( mxMaskContext );
2562cdf0e10cSrcweir 		delete[] mpMaskBuffer;
2563cdf0e10cSrcweir 		mxMaskContext = NULL;
2564cdf0e10cSrcweir 		mpMaskBuffer = NULL;
2565cdf0e10cSrcweir 
2566cdf0e10cSrcweir 		// cleanup the temp context if needed
2567cdf0e10cSrcweir 		if( mxTempContext )
2568cdf0e10cSrcweir 		{
2569cdf0e10cSrcweir 			CGContextRelease( mxTempContext );
2570cdf0e10cSrcweir 			delete[] mpTempBuffer;
2571cdf0e10cSrcweir 			mxTempContext = NULL;
2572cdf0e10cSrcweir 			mpTempBuffer = NULL;
2573cdf0e10cSrcweir 		}
2574cdf0e10cSrcweir 	}
2575cdf0e10cSrcweir 
2576cdf0e10cSrcweir 	// return early if there is nothing more to do
2577cdf0e10cSrcweir 	if( !xTargetContext )
2578cdf0e10cSrcweir 		return;
2579cdf0e10cSrcweir 
2580cdf0e10cSrcweir 	// retarget drawing operations to the XOR mask
2581cdf0e10cSrcweir 	mxTargetLayer = xTargetLayer;
2582cdf0e10cSrcweir 	mxTargetContext = xTargetContext;
2583cdf0e10cSrcweir 
2584cdf0e10cSrcweir 	// prepare creation of matching CGBitmaps
2585cdf0e10cSrcweir 	CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
2586cdf0e10cSrcweir 	CGBitmapInfo aCGBmpInfo = kCGImageAlphaNoneSkipFirst;
2587cdf0e10cSrcweir 	int nBitDepth = nTargetDepth;
2588cdf0e10cSrcweir 	if( !nBitDepth )
2589cdf0e10cSrcweir 		nBitDepth = 32;
2590cdf0e10cSrcweir 	int nBytesPerRow = (nBitDepth == 16) ? 2 : 4;
2591cdf0e10cSrcweir 	const size_t nBitsPerComponent = (nBitDepth == 16) ? 5 : 8;
2592cdf0e10cSrcweir 	if( nBitDepth <= 8 )
2593cdf0e10cSrcweir 	{
2594cdf0e10cSrcweir 		aCGColorSpace = GetSalData()->mxGraySpace;
2595cdf0e10cSrcweir 		aCGBmpInfo = kCGImageAlphaNone;
2596cdf0e10cSrcweir 		nBytesPerRow = 1;
2597cdf0e10cSrcweir 	}
2598cdf0e10cSrcweir 	nBytesPerRow *= nWidth;
2599cdf0e10cSrcweir 	mnBufferLongs = (nHeight * nBytesPerRow + sizeof(sal_uLong)-1) / sizeof(sal_uLong);
2600cdf0e10cSrcweir 
2601cdf0e10cSrcweir 	// create a XorMask context
2602cdf0e10cSrcweir 	mpMaskBuffer = new sal_uLong[ mnBufferLongs ];
2603cdf0e10cSrcweir 	mxMaskContext = ::CGBitmapContextCreate( mpMaskBuffer,
2604cdf0e10cSrcweir 		nWidth, nHeight, nBitsPerComponent, nBytesPerRow,
2605cdf0e10cSrcweir 		aCGColorSpace, aCGBmpInfo );
2606cdf0e10cSrcweir 	// reset the XOR mask to black
2607cdf0e10cSrcweir 	memset( mpMaskBuffer, 0, mnBufferLongs * sizeof(sal_uLong) );
2608cdf0e10cSrcweir 
2609cdf0e10cSrcweir 	// a bitmap context will be needed for manual XORing
2610cdf0e10cSrcweir 	// create one unless the target context is a bitmap context
2611cdf0e10cSrcweir 	if( nTargetDepth )
2612cdf0e10cSrcweir 		mpTempBuffer = (sal_uLong*)CGBitmapContextGetData( mxTargetContext );
2613cdf0e10cSrcweir 	if( !mpTempBuffer )
2614cdf0e10cSrcweir 	{
2615cdf0e10cSrcweir 		// create a bitmap context matching to the target context
2616cdf0e10cSrcweir 		mpTempBuffer = new sal_uLong[ mnBufferLongs ];
2617cdf0e10cSrcweir 		mxTempContext = ::CGBitmapContextCreate( mpTempBuffer,
2618cdf0e10cSrcweir 			nWidth, nHeight, nBitsPerComponent, nBytesPerRow,
2619cdf0e10cSrcweir 			aCGColorSpace, aCGBmpInfo );
2620cdf0e10cSrcweir 	}
2621cdf0e10cSrcweir 
2622cdf0e10cSrcweir 	// initialize XOR mask context for drawing
2623cdf0e10cSrcweir 	CGContextSetFillColorSpace( mxMaskContext, aCGColorSpace );
2624cdf0e10cSrcweir 	CGContextSetStrokeColorSpace( mxMaskContext, aCGColorSpace );
2625cdf0e10cSrcweir 	CGContextSetShouldAntialias( mxMaskContext, false );
2626cdf0e10cSrcweir 
2627cdf0e10cSrcweir 	// improve the XorMask's XOR emulation a litte
2628cdf0e10cSrcweir 	// NOTE: currently only enabled for monochrome contexts
2629cdf0e10cSrcweir 	if( aCGColorSpace == GetSalData()->mxGraySpace )
2630cdf0e10cSrcweir 		CGContextSetBlendMode( mxMaskContext, kCGBlendModeDifference );
2631cdf0e10cSrcweir 
2632cdf0e10cSrcweir 	// intialize the transformation matrix to the drawing target
2633cdf0e10cSrcweir 	const CGAffineTransform aCTM = CGContextGetCTM( xTargetContext );
2634cdf0e10cSrcweir 	CGContextConcatCTM( mxMaskContext, aCTM );
2635cdf0e10cSrcweir 	if( mxTempContext )
2636cdf0e10cSrcweir 		CGContextConcatCTM( mxTempContext, aCTM );
2637cdf0e10cSrcweir 
2638cdf0e10cSrcweir 	// initialize the default XorMask graphics state
2639cdf0e10cSrcweir 	CGContextSaveGState( mxMaskContext );
2640cdf0e10cSrcweir }
2641cdf0e10cSrcweir 
2642cdf0e10cSrcweir // ----------------------------------------------------------------------
2643cdf0e10cSrcweir 
2644cdf0e10cSrcweir bool XorEmulation::UpdateTarget()
2645cdf0e10cSrcweir {
2646cdf0e10cSrcweir 	if( !IsEnabled() )
2647cdf0e10cSrcweir 		return false;
2648cdf0e10cSrcweir 
2649cdf0e10cSrcweir 	// update the temp bitmap buffer if needed
2650cdf0e10cSrcweir 	if( mxTempContext )
2651cdf0e10cSrcweir 		CGContextDrawLayerAtPoint( mxTempContext, CGPointZero, mxTargetLayer );
2652cdf0e10cSrcweir 
2653cdf0e10cSrcweir 	// do a manual XOR with the XorMask
2654cdf0e10cSrcweir 	// this approach suffices for simple color manipulations
2655cdf0e10cSrcweir 	// and also the complex-clipping-XOR-trick used in metafiles
2656cdf0e10cSrcweir 	const sal_uLong* pSrc = mpMaskBuffer;
2657cdf0e10cSrcweir 	sal_uLong* pDst = mpTempBuffer;
2658cdf0e10cSrcweir 	for( int i = mnBufferLongs; --i >= 0;)
2659cdf0e10cSrcweir 		*(pDst++) ^= *(pSrc++);
2660cdf0e10cSrcweir 
2661cdf0e10cSrcweir 	// write back the XOR results to the target context
2662cdf0e10cSrcweir 	if( mxTempContext )
2663cdf0e10cSrcweir 	{
2664cdf0e10cSrcweir 		CGImageRef xXorImage = CGBitmapContextCreateImage( mxTempContext );
2665cdf0e10cSrcweir 		const int nWidth  = (int)CGImageGetWidth( xXorImage );
2666cdf0e10cSrcweir 		const int nHeight = (int)CGImageGetHeight( xXorImage );
2667cdf0e10cSrcweir 		// TODO: update minimal changerect
2668cdf0e10cSrcweir 		const CGRect aFullRect = {{0,0},{nWidth,nHeight}};
2669cdf0e10cSrcweir 		CGContextDrawImage( mxTargetContext, aFullRect, xXorImage );
2670cdf0e10cSrcweir 		CGImageRelease( xXorImage );
2671cdf0e10cSrcweir 	}
2672cdf0e10cSrcweir 
2673cdf0e10cSrcweir 	// reset the XorMask to black again
2674cdf0e10cSrcweir 	// TODO: not needed for last update
2675cdf0e10cSrcweir 	memset( mpMaskBuffer, 0, mnBufferLongs * sizeof(sal_uLong) );
2676cdf0e10cSrcweir 
2677cdf0e10cSrcweir 	// TODO: return FALSE if target was not changed
2678cdf0e10cSrcweir 	return true;
2679cdf0e10cSrcweir }
2680cdf0e10cSrcweir 
2681cdf0e10cSrcweir // =======================================================================
2682cdf0e10cSrcweir 
2683