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