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