xref: /AOO41X/main/vcl/aqua/source/gdi/ctfonts.cxx (revision a29d375c28feefee5f66a5e3ed841463d3388e9b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_vcl.hxx"
24 
25 #include "impfont.hxx"
26 #include "outfont.hxx"
27 #include "sallayout.hxx"
28 
29 #include "aqua/salinst.h"
30 #include "aqua/saldata.hxx"
31 #include "aqua/salgdi.h"
32 #include "ctfonts.hxx"
33 
34 #include "basegfx/polygon/b2dpolygon.hxx"
35 #include "basegfx/matrix/b2dhommatrix.hxx"
36 
37 #ifndef DISABLE_CORETEXT_DYNLOAD
38 #include <dlfcn.h>
39 #endif
40 
41 // =======================================================================
42 
43 // CoreText specific physically available font face
44 class CTFontData
45 :   public ImplMacFontData
46 {
47 public:
48     explicit                CTFontData( const ImplDevFontAttributes&, sal_IntPtr nFontId );
49     virtual                 ~CTFontData( void );
50     virtual ImplFontData*   Clone( void ) const;
51 
52     virtual ImplMacTextStyle*   CreateMacTextStyle( const ImplFontSelectData& ) const;
53     virtual ImplFontEntry*      CreateFontInstance( /*const*/ ImplFontSelectData& ) const;
54     virtual int                 GetFontTable( const char pTagName[5], unsigned char* ) const;
55 };
56 
57 // =======================================================================
58 
59 class CTFontList
60 :   public SystemFontList
61 {
62 public:
63     explicit    CTFontList( void );
64     virtual     ~CTFontList( void );
65 
66     bool        Init( void );
67     void        AddFont( CTFontData* );
68 
69     virtual void    AnnounceFonts( ImplDevFontList& ) const;
70     virtual ImplMacFontData* GetFontDataFromId( sal_IntPtr ) const;
71 
72 private:
73     CTFontCollectionRef mpCTFontCollection;
74     CFArrayRef mpCTFontArray;
75 
76     typedef std::hash_map<sal_IntPtr,CTFontData*> CTFontContainer;
77     CTFontContainer maFontContainer;
78 };
79 
80 // =======================================================================
81 
82 CTTextStyle::CTTextStyle( const ImplFontSelectData& rFSD )
83 :   ImplMacTextStyle( rFSD )
84 ,   mpStyleDict( NULL )
85 {
86     mpFontData = (CTFontData*)rFSD.mpFontData;
87     const ImplFontSelectData* const pReqFont = &rFSD;
88 
89     double fScaledFontHeight = pReqFont->mfExactHeight;
90 #if 0 // TODO: does CoreText need font size limiting???
91     static const float fMaxFontHeight = 144.0; // TODO: is there a limit for CoreText?
92     if( fScaledFontHeight > fMaxFontHeight )
93     {
94         mfFontScale = fScaledFontHeight / fMaxFontHeight;
95         fScaledFontHeight = fMaxFontHeight;
96     }
97 #endif
98 
99     // convert font rotation to radian
100     mfFontRotation = pReqFont->mnOrientation * (M_PI / 1800.0);
101 
102     // handle font stretching if any
103     const CGAffineTransform* pMatrix = NULL;
104     CGAffineTransform aMatrix;
105     if( (pReqFont->mnWidth != 0) && (pReqFont->mnWidth != pReqFont->mnHeight) )
106     {
107         mfFontStretch = (float)pReqFont->mnWidth / pReqFont->mnHeight;
108         aMatrix = CGAffineTransformMakeScale( mfFontStretch, 1.0F );
109         pMatrix = &aMatrix;
110     }
111 
112     // handle emulation of italic/oblique styles if requested and the font doesn't provide them
113     if( ((pReqFont->meItalic == ITALIC_NORMAL) || (pReqFont->meItalic == ITALIC_OBLIQUE))
114     && (mpFontData->meItalic == ITALIC_NONE))
115     {
116         if( !pMatrix)
117             pMatrix = &(aMatrix = CGAffineTransformIdentity);
118         aMatrix = CGAffineTransformConcat(aMatrix, CGAffineTransformMake( 1, 0, 0.375F, 1, 0, 0));
119     }
120 
121     // create the style object for CoreText font attributes
122     static const CFIndex nMaxDictSize = 16; // TODO: does this really suffice?
123     mpStyleDict = CFDictionaryCreateMutable( NULL, nMaxDictSize,
124         &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
125 
126     // set some default styles: no kerning, regular ligatures
127     static const CGFloat fValZero = 0.0;
128     CFNumberRef pCFFloatNumZero = CFNumberCreate( NULL, kCFNumberFloatType, &fValZero );
129     CFDictionarySetValue( mpStyleDict, kCTKernAttributeName, pCFFloatNumZero );
130     CFRelease( pCFFloatNumZero);
131     static const int nValOne = 1;
132     CFNumberRef pCFIntNumOne = CFNumberCreate( NULL, kCFNumberIntType, &nValOne );
133     CFDictionarySetValue( mpStyleDict, kCTLigatureAttributeName, pCFIntNumOne );
134     CFRelease( pCFIntNumOne);
135     CFBooleanRef pCFVertBool = pReqFont->mbVertical ? kCFBooleanTrue : kCFBooleanFalse;
136     CFDictionarySetValue( mpStyleDict, kCTVerticalFormsAttributeName, pCFVertBool );
137 
138     CTFontDescriptorRef pFontDesc = (CTFontDescriptorRef)mpFontData->GetFontId();
139     CTFontRef pNewCTFont = CTFontCreateWithFontDescriptor( pFontDesc, fScaledFontHeight, pMatrix );
140     CFDictionarySetValue( mpStyleDict, kCTFontAttributeName, pNewCTFont );
141     CFRelease( pNewCTFont);
142 
143     // handle emulation of bold styles if requested and the font that doesn't provide them
144     if( (pReqFont->meWeight > WEIGHT_MEDIUM)
145     &&  (mpFontData->meWeight <= WEIGHT_MEDIUM)
146     &&  (mpFontData->meWeight != WEIGHT_DONTKNOW))
147     {
148         const int nBoldFactor = -lrint( (3.5F * pReqFont->meWeight) / mpFontData->meWeight);
149         CFNumberRef pCFIntBold = CFNumberCreate( NULL, kCFNumberIntType, &nBoldFactor);
150         CFDictionarySetValue( mpStyleDict, kCTStrokeWidthAttributeName, pCFIntBold);
151     }
152 
153 #if 0 // LastResort is implicit in CoreText's font cascading
154     const void* aGFBDescriptors[] = { CTFontDescriptorCreateWithNameAndSize( CFSTR("LastResort"), 0) }; // TODO: use the full GFB list
155     const int nGfbCount = sizeof(aGFBDescriptors) / sizeof(*aGFBDescriptors);
156     CFArrayRef pGfbList = CFArrayCreate( NULL, aGFBDescriptors, nGfbCount, &kCFTypeArrayCallBacks);
157     CFDictionaryAddValue( mpStyleDict, kCTFontCascadeListAttribute, pGfbList);
158     CFRelease( pGfbList);
159 #endif
160 }
161 
162 // -----------------------------------------------------------------------
163 
164 CTTextStyle::~CTTextStyle( void )
165 {
166     if( mpStyleDict )
167         CFRelease( mpStyleDict );
168 }
169 
170 // -----------------------------------------------------------------------
171 
172 void CTTextStyle::GetFontMetric( float fDPIY, ImplFontMetricData& rMetric ) const
173 {
174     // get the matching CoreText font handle
175     // TODO: is it worth it to cache the CTFontRef in SetFont() and reuse it here?
176     CTFontRef aCTFontRef = (CTFontRef)CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName );
177 
178     const double fPixelSize = (mfFontScale * fDPIY);
179     rMetric.mnAscent       = lrint( CTFontGetAscent( aCTFontRef ) * fPixelSize);
180     rMetric.mnDescent      = lrint( CTFontGetDescent( aCTFontRef ) * fPixelSize);
181     rMetric.mnIntLeading   = lrint( CTFontGetLeading( aCTFontRef ) * fPixelSize);
182     rMetric.mnExtLeading   = 0;
183     // since ImplFontMetricData::mnWidth is only used for stretching/squeezing fonts
184     // setting this width to the pixel height of the fontsize is good enough
185     // it also makes the calculation of the stretch factor simple
186     rMetric.mnWidth        = lrint( CTFontGetSize( aCTFontRef ) * fPixelSize * mfFontStretch);
187 
188     // all CoreText fonts are scalable
189     rMetric.mbScalableFont = true;
190     // TODO: check if any kerning is supported
191     rMetric.mbKernableFont = true;
192 }
193 
194 // -----------------------------------------------------------------------
195 
196 bool CTTextStyle::GetGlyphBoundRect( sal_GlyphId aGlyphId, Rectangle& rRect ) const
197 {
198     const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
199     CGGlyph nCGGlyph = aGlyphId & GF_IDXMASK; // NOTE: CoreText handles glyph fallback itself
200     CTFontRef aCTFontRef = (CTFontRef)CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName );
201 
202     const CTFontOrientation aFontOrientation = kCTFontDefaultOrientation; // TODO: horz/vert
203     const CGRect aCGRect = rCT.FontGetBoundingRectsForGlyphs( aCTFontRef, aFontOrientation, &nCGGlyph, NULL, 1 );
204 
205     rRect.Left()   = lrint( mfFontScale * aCGRect.origin.x );
206     rRect.Top()    = lrint( mfFontScale * aCGRect.origin.y );
207     rRect.Right()  = lrint( mfFontScale * (aCGRect.origin.x + aCGRect.size.width) );
208     rRect.Bottom() = lrint( mfFontScale * (aCGRect.origin.y + aCGRect.size.height) );
209     return true;
210 }
211 
212 // -----------------------------------------------------------------------
213 
214 // callbacks from CTFontCreatePathForGlyph+CGPathApply for GetGlyphOutline()
215 struct GgoData { basegfx::B2DPolygon maPolygon; basegfx::B2DPolyPolygon* mpPolyPoly; };
216 
217 static void MyCGPathApplierFunc( void* pData, const CGPathElement* pElement )
218 {
219     basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
220     const int nPointCount = rPolygon.count();
221 
222     switch( pElement->type )
223     {
224     case kCGPathElementCloseSubpath:
225     case kCGPathElementMoveToPoint:
226         if( nPointCount > 0 ) {
227             static_cast<GgoData*>(pData)->mpPolyPoly->append( rPolygon );
228             rPolygon.clear();
229         }
230         // fall through for kCGPathElementMoveToPoint:
231         if( pElement->type != kCGPathElementMoveToPoint )
232             break;
233     case kCGPathElementAddLineToPoint:
234         rPolygon.append( basegfx::B2DPoint( +pElement->points[0].x, -pElement->points[0].y ) );
235         break;
236     case kCGPathElementAddCurveToPoint:
237         rPolygon.append( basegfx::B2DPoint( +pElement->points[2].x, -pElement->points[2].y ) );
238         rPolygon.setNextControlPoint( nPointCount-1, basegfx::B2DPoint( pElement->points[0].x, -pElement->points[0].y ) );
239         rPolygon.setPrevControlPoint( nPointCount+0, basegfx::B2DPoint( pElement->points[1].x, -pElement->points[1].y ) );
240         break;
241     case kCGPathElementAddQuadCurveToPoint: {
242         const basegfx::B2DPoint aStartPt = rPolygon.getB2DPoint( nPointCount-1 );
243         const basegfx::B2DPoint aCtrPt1( (aStartPt.getX() + 2* pElement->points[0].x) / 3.0,
244                     (aStartPt.getY() - 2 * pElement->points[0].y) / 3.0 );
245         const basegfx::B2DPoint aCtrPt2( (+2 * +pElement->points[0].x + pElement->points[1].x) / 3.0,
246                 (-2 * pElement->points[0].y - pElement->points[1].y) / 3.0 );
247         rPolygon.append( basegfx::B2DPoint( +pElement->points[1].x, -pElement->points[1].y ) );
248         rPolygon.setNextControlPoint( nPointCount-1, aCtrPt1 );
249         rPolygon.setPrevControlPoint( nPointCount+0, aCtrPt2 );
250         } break;
251     }
252 }
253 
254 bool CTTextStyle::GetGlyphOutline( sal_GlyphId aGlyphId, basegfx::B2DPolyPolygon& rResult ) const
255 {
256     rResult.clear();
257 
258     const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
259     // TODO: GF_FONTMASK if using non-native glyph fallback
260     CGGlyph nCGGlyph = aGlyphId & GF_IDXMASK;
261     CTFontRef pCTFont = (CTFontRef)CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName );
262     CGPathRef xPath = rCT.FontCreatePathForGlyph( pCTFont, nCGGlyph, NULL );
263 
264     GgoData aGgoData;
265     aGgoData.mpPolyPoly = &rResult;
266     CGPathApply( xPath, (void*)&aGgoData, MyCGPathApplierFunc );
267 #if 0 // TODO: does OSX ensure that the last polygon is always closed?
268     const CGPathElement aClosingElement = { kCGPathElementCloseSubpath, NULL };
269     MyCGPathApplierFunc( (void*)&aGgoData, &aClosingElement );
270 #endif
271 
272     // apply the font scale
273     if( mfFontScale != 1.0 ) {
274         basegfx::B2DHomMatrix aScale;
275         aScale.scale( +mfFontScale, +mfFontScale );
276         rResult.transform( aScale );
277     }
278 
279     return true;
280 }
281 
282 // -----------------------------------------------------------------------
283 
284 void CTTextStyle::SetTextColor( const RGBAColor& rColor )
285 {
286 #if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
287     CGColorRef pCGColor = CGColorCreateGenericRGB( rColor.GetRed(),
288         rColor.GetGreen(), rColor.GetBlue(), rColor.GetAlpha() );
289 #else // for builds on OSX 10.4 SDK
290     const CGColorSpaceRef pCGColorSpace = GetSalData()->mxRGBSpace;
291     CGColorRef pCGColor = CGColorCreate( pCGColorSpace, rColor.AsArray() );
292 #endif
293     CFDictionarySetValue( mpStyleDict, kCTForegroundColorAttributeName, pCGColor );
294     CFRelease( pCGColor);
295 }
296 
297 // =======================================================================
298 
299 CTFontData::CTFontData( const ImplDevFontAttributes& rDFA, sal_IntPtr nFontId )
300 :   ImplMacFontData( rDFA, nFontId )
301 {}
302 
303 // -----------------------------------------------------------------------
304 
305 CTFontData::~CTFontData( void )
306 {
307     // TODO: any resources to release?
308 }
309 
310 // -----------------------------------------------------------------------
311 
312 ImplFontData* CTFontData::Clone( void ) const
313 {
314     return new CTFontData( *this);
315 }
316 
317 // -----------------------------------------------------------------------
318 
319 ImplMacTextStyle* CTFontData::CreateMacTextStyle( const ImplFontSelectData& rFSD ) const
320 {
321     return new CTTextStyle( rFSD);
322 }
323 
324 // -----------------------------------------------------------------------
325 
326 ImplFontEntry* CTFontData::CreateFontInstance( /*const*/ ImplFontSelectData& rFSD ) const
327 {
328     return new ImplFontEntry( rFSD);
329 }
330 
331 // -----------------------------------------------------------------------
332 
333 int CTFontData::GetFontTable( const char pTagName[5], unsigned char* pResultBuf ) const
334 {
335     DBG_ASSERT( pTagName[4]=='\0', "CTFontData::GetFontTable with invalid tagname!\n" );
336 
337     const CTFontTableTag nTagCode = (pTagName[0]<<24) + (pTagName[1]<<16) + (pTagName[2]<<8) + (pTagName[3]<<0);
338 
339     // get the raw table length
340     CTFontDescriptorRef pFontDesc = reinterpret_cast<CTFontDescriptorRef>( GetFontId());
341     CTFontRef rCTFont = CTFontCreateWithFontDescriptor( pFontDesc, 0.0, NULL);
342     CFDataRef pDataRef = CTFontCopyTable( rCTFont, nTagCode, kCTFontTableOptionExcludeSynthetic);
343     CFRelease( rCTFont);
344     if( !pDataRef)
345         return 0;
346 
347     const CFIndex nByteLength = CFDataGetLength( pDataRef);
348 
349     // get the raw table data if requested
350     if( pResultBuf && (nByteLength > 0))
351     {
352         const CFRange aFullRange = CFRangeMake( 0, nByteLength);
353         CFDataGetBytes( pDataRef, aFullRange, (UInt8*)pResultBuf);
354     }
355 
356     CFRelease( pDataRef);
357 
358     return (int)nByteLength;
359 }
360 
361 // =======================================================================
362 
363 static void CTFontEnumCallBack( const void* pValue, void* pContext )
364 {
365     CTFontDescriptorRef pFD = static_cast<CTFontDescriptorRef>(pValue);
366 
367     // all CoreText fonts are device fonts that can rotate just fine
368     ImplDevFontAttributes rDFA;
369     rDFA.mbOrientation = true;
370     rDFA.mbDevice      = true;
371     rDFA.mnQuality     = 0;
372 
373     // reset the font attributes
374     rDFA.meFamily     = FAMILY_DONTKNOW;
375     rDFA.mePitch      = PITCH_VARIABLE;
376     rDFA.meWidthType  = WIDTH_NORMAL;
377     rDFA.meWeight     = WEIGHT_NORMAL;
378     rDFA.meItalic     = ITALIC_NONE;
379     rDFA.mbSymbolFlag = false;
380 
381     // all scalable fonts on this platform are subsettable
382     rDFA.mbEmbeddable = false;
383     rDFA.mbSubsettable = true;
384 
385     // get font name
386     // TODO: use kCTFontDisplayNameAttribute instead???
387     CFStringRef pFamilyName = (CFStringRef)CTFontDescriptorCopyAttribute( pFD, kCTFontFamilyNameAttribute );
388     rDFA.maName = GetOUString( pFamilyName );
389     // get font style
390     CFStringRef pStyleName = (CFStringRef)CTFontDescriptorCopyAttribute( pFD, kCTFontStyleNameAttribute );
391     rDFA.maStyleName = GetOUString( pStyleName );
392 
393     // get font-enabled status
394     int bFontEnabled = FALSE;
395     CFNumberRef pFontEnabled = (CFNumberRef)CTFontDescriptorCopyAttribute( pFD, kCTFontEnabledAttribute );
396     CFNumberGetValue( pFontEnabled, kCFNumberIntType, &bFontEnabled );
397 
398     // get font attributes
399     CFDictionaryRef pAttrDict = (CFDictionaryRef)CTFontDescriptorCopyAttribute( pFD, kCTFontTraitsAttribute );
400 
401     // get symbolic trait
402     // TODO: use other traits such as MonoSpace/Condensed/Expanded or Vertical too
403     SInt64 nSymbolTrait = 0;
404     CFNumberRef pSymbolNum = NULL;
405     if( CFDictionaryGetValueIfPresent( pAttrDict, kCTFontSymbolicTrait, (const void**)&pSymbolNum ) ) {
406         CFNumberGetValue( pSymbolNum, kCFNumberSInt64Type, &nSymbolTrait );
407         rDFA.mbSymbolFlag = ((nSymbolTrait & kCTFontClassMaskTrait) == kCTFontSymbolicClass);
408     }
409 
410     // get the font weight
411     double fWeight = 0;
412     CFNumberRef pWeightNum = (CFNumberRef)CFDictionaryGetValue( pAttrDict, kCTFontWeightTrait );
413     CFNumberGetValue( pWeightNum, kCFNumberDoubleType, &fWeight );
414     int nInt = WEIGHT_NORMAL;
415     if( fWeight > 0 ) {
416         nInt = rint(WEIGHT_NORMAL + fWeight * ((WEIGHT_BLACK - WEIGHT_NORMAL)/0.68));
417         if( nInt > WEIGHT_BLACK )
418             nInt = WEIGHT_BLACK;
419     } else if( fWeight < 0 ) {
420         nInt = rint(WEIGHT_NORMAL + fWeight * ((WEIGHT_NORMAL - WEIGHT_THIN)/0.9));
421         if( nInt < WEIGHT_THIN )
422             nInt = WEIGHT_THIN;
423     }
424     rDFA.meWeight = (FontWeight)nInt;
425 
426     // get the font slant
427     double fSlant = 0;
428     CFNumberRef pSlantNum = (CFNumberRef)CFDictionaryGetValue( pAttrDict, kCTFontSlantTrait );
429     CFNumberGetValue( pSlantNum, kCFNumberDoubleType, &fSlant );
430     if( fSlant >= 0.035 )
431         rDFA.meItalic = ITALIC_NORMAL;
432 
433     // get width trait
434     double fWidth = 0;
435     CFNumberRef pWidthNum = (CFNumberRef)CFDictionaryGetValue( pAttrDict, kCTFontWidthTrait );
436     CFNumberGetValue( pWidthNum, kCFNumberDoubleType, &fWidth );
437     nInt = WIDTH_NORMAL;
438     if( fWidth > 0 ) {
439         nInt = rint( WIDTH_NORMAL + fWidth * ((WIDTH_ULTRA_EXPANDED - WIDTH_NORMAL)/0.4));
440         if( nInt > WIDTH_ULTRA_EXPANDED )
441             nInt = WIDTH_ULTRA_EXPANDED;
442     } else if( fWidth < 0 ) {
443         nInt = rint( WIDTH_NORMAL + fWidth * ((WIDTH_NORMAL - WIDTH_ULTRA_CONDENSED)/0.5));
444         if( nInt < WIDTH_ULTRA_CONDENSED )
445             nInt = WIDTH_ULTRA_CONDENSED;
446     }
447     rDFA.meWidthType = (FontWidth)nInt;
448 
449     // release the attribute dict that we had copied
450     CFRelease( pAttrDict );
451 
452     // TODO? also use the HEAD table if available to get more attributes
453 //  CFDataRef CTFontCopyTable( CTFontRef, kCTFontTableHead, /*kCTFontTableOptionNoOptions*/kCTFontTableOptionExcludeSynthetic );
454 
455 #if (OSL_DEBUG_LEVEL >= 1)
456     // update font attributes using the font's postscript name
457     ImplDevFontAttributes rDFA2;
458     CTFontRef pFont = CTFontCreateWithFontDescriptor( pFD, 0.0, NULL );
459     CFStringRef pPSName = CTFontCopyPostScriptName( pFont );
460     const String aPSName = GetOUString( pPSName );
461 
462     rDFA2.mbSymbolFlag = false;
463     rDFA2.mePitch      = PITCH_VARIABLE;
464     rDFA2.meWidthType  = WIDTH_NORMAL;
465     rDFA2.meWeight     = WEIGHT_NORMAL;
466     rDFA2.meItalic     = ITALIC_NONE;
467 
468     UpdateAttributesFromPSName( aPSName, rDFA2 );
469     CFRelease( pPSName );
470     CFRelease( pFont );
471 
472     // show the font details and compare the CTFontDescriptor vs. PSName traits
473     char cMatch = (rDFA.mbSymbolFlag==rDFA2.mbSymbolFlag);
474     cMatch &= (rDFA.meWeight==rDFA2.meWeight);
475     cMatch &= ((rDFA.meItalic==ITALIC_NONE) == (rDFA2.meItalic==ITALIC_NONE));
476     cMatch &= (rDFA.meWidthType==rDFA2.meWidthType);
477     cMatch = cMatch ? '.' : '#';
478 
479     char aFN[256], aSN[256];
480     CFStringGetCString( pFamilyName, aFN, sizeof(aFN), kCFStringEncodingUTF8 );
481     CFStringGetCString( pStyleName, aSN, sizeof(aSN), kCFStringEncodingUTF8 );
482 
483     const ByteString aPSCName( aPSName, RTL_TEXTENCODING_UTF8 );
484     const char* aPN = aPSCName.GetBuffer();
485     printf("\tCTFont_%d%x%d%d_%c_%d%x%d%d ena=%d s=%02d b=%+.2f i=%+.2f w=%+.2f (\"%s\", \"%s\", \"%s\")\n",
486         (int)rDFA.mbSymbolFlag,(int)rDFA.meWeight,(int)rDFA.meItalic,(int)rDFA.meWidthType,
487         cMatch,
488         (int)rDFA2.mbSymbolFlag,(int)rDFA2.meWeight,(int)rDFA2.meItalic,(int)rDFA2.meWidthType,
489         bFontEnabled,
490         (int)(nSymbolTrait>>kCTFontClassMaskShift),fWeight,fSlant,fWidth,aFN,aSN,aPN);
491 #endif // (OSL_DEBUG_LEVEL >= 1)
492 
493     if( bFontEnabled)
494     {
495         const sal_IntPtr nFontId = (sal_IntPtr)pValue;
496         CTFontData* pFontData = new CTFontData( rDFA, nFontId );
497         CTFontList* pFontList = (CTFontList*)pContext;
498         pFontList->AddFont( pFontData );
499     }
500 }
501 
502 // =======================================================================
503 
504 CTFontList::CTFontList()
505 :   mpCTFontCollection( NULL )
506 ,   mpCTFontArray( NULL )
507 {}
508 
509 // -----------------------------------------------------------------------
510 
511 CTFontList::~CTFontList()
512 {
513     CTFontContainer::const_iterator it = maFontContainer.begin();
514     for(; it != maFontContainer.end(); ++it )
515         delete (*it).second;
516     maFontContainer.clear();
517 
518     if( mpCTFontArray )
519         CFRelease( mpCTFontArray );
520     if( mpCTFontCollection )
521         CFRelease( mpCTFontCollection );
522 }
523 
524 // -----------------------------------------------------------------------
525 
526 void CTFontList::AddFont( CTFontData* pFontData )
527 {
528     sal_IntPtr nFontId = pFontData->GetFontId();
529     maFontContainer[ nFontId ] = pFontData;
530 }
531 
532 // -----------------------------------------------------------------------
533 
534 void CTFontList::AnnounceFonts( ImplDevFontList& rFontList ) const
535 {
536     CTFontContainer::const_iterator it = maFontContainer.begin();
537     for(; it != maFontContainer.end(); ++it )
538         rFontList.Add( (*it).second->Clone() );
539 }
540 
541 // -----------------------------------------------------------------------
542 
543 ImplMacFontData* CTFontList::GetFontDataFromId( sal_IntPtr nFontId ) const
544 {
545     CTFontContainer::const_iterator it = maFontContainer.find( nFontId );
546     if( it == maFontContainer.end() )
547         return NULL;
548     return (*it).second;
549 }
550 
551 // -----------------------------------------------------------------------
552 
553 bool CTFontList::Init( void )
554 {
555 #ifndef DISABLE_CORETEXT_DYNLOAD
556     // check availability of the CoreText API
557     const DynCoreTextSyms& rCT = DynCoreTextSyms::get();
558     if( !rCT.IsActive() )
559         return false;
560 #endif // DISABLE_CORETEXT_DYNLOAD
561 
562     // enumerate available system fonts
563     static const int nMaxDictEntries = 8;
564     CFMutableDictionaryRef pCFDict = CFDictionaryCreateMutable( NULL,
565         nMaxDictEntries, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
566     CFDictionaryAddValue( pCFDict, kCTFontCollectionRemoveDuplicatesOption, kCFBooleanTrue );
567     mpCTFontCollection = rCT.FontCollectionCreateFromAvailableFonts( pCFDict );
568     CFRelease( pCFDict );
569 
570     mpCTFontArray = rCT.FontCollectionCreateMatchingFontDescriptors( mpCTFontCollection );
571     const int nFontCount = CFArrayGetCount( mpCTFontArray );
572     const CFRange aFullRange = CFRangeMake( 0, nFontCount );
573     CFArrayApplyFunction( mpCTFontArray, aFullRange, CTFontEnumCallBack, this );
574 
575     return true;
576 }
577 
578 // =======================================================================
579 
580 #ifndef DISABLE_CORETEXT_DYNLOAD
581 
582 DynCoreTextSyms::DynCoreTextSyms( void )
583 {
584     mbIsActive = false;
585 
586     // check if CoreText has been explicitely disabled
587     const char* pEnvStr = getenv( "SAL_DISABLE_CORETEXT");
588     if( pEnvStr && (pEnvStr[0] != '0') )
589         return;
590 
591     // check CoreText version
592     GetCoreTextVersion = (uint32_t(*)(void))dlsym( RTLD_DEFAULT, "CTGetCoreTextVersion");
593     if( !GetCoreTextVersion) return;
594 
595     const uint32_t nCTVersion = GetCoreTextVersion();
596     static const uint32_t mykCTVersionNumber10_5 = 0x00020000;
597     if( nCTVersion < mykCTVersionNumber10_5)
598         return;
599 
600     // load CoreText symbols dynamically
601     LineGetTrailingWhitespaceWidth = (double(*)(CTLineRef))dlsym( RTLD_DEFAULT, "CTLineGetTrailingWhitespaceWidth");
602     if( !LineGetTrailingWhitespaceWidth) return;
603 
604     LineCreateJustifiedLine = (CTLineRef(*)(CTLineRef,CGFloat,double))dlsym( RTLD_DEFAULT, "CTLineCreateJustifiedLine");
605     if( !LineCreateJustifiedLine) return;
606 
607     LineGetOffsetForStringIndex = (CGFloat(*)(CTLineRef,CFIndex,CGFloat*))dlsym( RTLD_DEFAULT, "CTLineGetOffsetForStringIndex");
608     if( !LineGetOffsetForStringIndex) return;
609 
610     LineGetGlyphRuns = (CFArrayRef(*)(CTLineRef))dlsym( RTLD_DEFAULT, "CTLineGetGlyphRuns");
611     if( !LineGetGlyphRuns) return;
612 
613     RunGetGlyphCount = (CFIndex(*)(CTRunRef))dlsym( RTLD_DEFAULT, "CTRunGetGlyphCount");
614     if( !RunGetGlyphCount) return;
615 
616     RunGetGlyphsPtr = (const CGGlyph*(*)(CTRunRef))dlsym( RTLD_DEFAULT, "CTRunGetGlyphsPtr");
617     if( !RunGetGlyphsPtr) return;
618 
619     RunGetPositionsPtr = (const CGPoint*(*)(CTRunRef))dlsym( RTLD_DEFAULT, "CTRunGetPositionsPtr");
620     if( !RunGetPositionsPtr) return;
621 
622     RunGetAdvancesPtr = (const CGSize*(*)(CTRunRef))dlsym( RTLD_DEFAULT, "CTRunGetAdvancesPtr");
623     if( !RunGetAdvancesPtr) return;
624 
625     RunGetStringIndicesPtr = (const CFIndex*(*)(CTRunRef))dlsym( RTLD_DEFAULT, "CTRunGetStringIndicesPtr");
626     if( !RunGetStringIndicesPtr) return;
627 
628     FontCollectionCreateFromAvailableFonts = (CTFontCollectionRef(*)(CFDictionaryRef))dlsym( RTLD_DEFAULT, "CTFontCollectionCreateFromAvailableFonts");
629     if( !FontCollectionCreateFromAvailableFonts) return;
630 
631     FontCollectionCreateMatchingFontDescriptors = (CFArrayRef(*)(CTFontCollectionRef))dlsym( RTLD_DEFAULT, "CTFontCollectionCreateMatchingFontDescriptors");
632     if( !FontCollectionCreateMatchingFontDescriptors) return;
633 
634     FontCreatePathForGlyph = (CGPathRef(*)(CTFontRef,CGGlyph,const CGAffineTransform*))dlsym( RTLD_DEFAULT, "CTFontCreatePathForGlyph");
635     if( !FontCreatePathForGlyph) return;
636 
637     FontGetBoundingRectsForGlyphs = (CGRect(*)(CTFontRef,CTFontOrientation,CGGlyph*,CGRect*,CFIndex))dlsym( RTLD_DEFAULT, "CTFontGetBoundingRectsForGlyphs");
638     if( !FontGetBoundingRectsForGlyphs) return;
639 
640     mbIsActive = true;
641 }
642 
643 // -----------------------------------------------------------------------
644 
645 const DynCoreTextSyms& DynCoreTextSyms::get( void )
646 {
647     static DynCoreTextSyms aCT;
648     return aCT;
649 }
650 
651 #endif // DISABLE_CORETEXT_DYNLOAD
652 
653 // =======================================================================
654 
655 SystemFontList* GetCoretextFontList( void )
656 {
657     CTFontList* pList = new CTFontList();
658     if( !pList->Init() ) {
659         delete pList;
660         return NULL;
661     }
662 
663     return pList;
664 }
665 
666 // =======================================================================
667 
668