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