1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include <basegfx/polygon/b2dpolygon.hxx>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <tools/long.hxx>
26 #include <vcl/settings.hxx>
27 
28 
29 #include <quartz/ctfonts.hxx>
30 #include <impfont.hxx>
31 #ifdef MACOSX
32 #include <osx/saldata.hxx>
33 #include <osx/salinst.h>
34 #endif
35 #include <fontinstance.hxx>
36 #include <fontattributes.hxx>
37 #include <impglyphitem.hxx>
38 #include <PhysicalFontCollection.hxx>
39 #include <quartz/salgdi.h>
40 #include <quartz/utils.h>
41 #include <sallayout.hxx>
42 #include <hb-coretext.h>
43 
toRadian(int nDegree)44 static double toRadian(int nDegree)
45 {
46     return nDegree * (M_PI / 1800.0);
47 }
48 
CoreTextStyle(const PhysicalFontFace & rPFF,const FontSelectPattern & rFSP)49 CoreTextStyle::CoreTextStyle(const PhysicalFontFace& rPFF, const FontSelectPattern& rFSP)
50     : LogicalFontInstance(rPFF, rFSP)
51     , mfFontStretch( 1.0 )
52     , mfFontRotation( 0.0 )
53     , mbFauxBold(false)
54     , mpStyleDict( nullptr )
55 {
56     double fScaledFontHeight = rFSP.mfExactHeight;
57 
58     // convert font rotation to radian
59     mfFontRotation = toRadian(rFSP.mnOrientation.get());
60 
61     // dummy matrix so we can use CGAffineTransformConcat() below
62     CGAffineTransform aMatrix = CGAffineTransformMakeTranslation(0, 0);
63 
64     // handle font stretching if any
65     if( (rFSP.mnWidth != 0) && (rFSP.mnWidth != rFSP.mnHeight) )
66     {
67         mfFontStretch = float(rFSP.mnWidth) / rFSP.mnHeight;
68         aMatrix = CGAffineTransformConcat(aMatrix, CGAffineTransformMakeScale(mfFontStretch, 1.0F));
69     }
70 
71     // create the style object for CoreText font attributes
72     static const CFIndex nMaxDictSize = 16; // TODO: does this really suffice?
73     mpStyleDict = CFDictionaryCreateMutable( nullptr, nMaxDictSize,
74                                              &kCFTypeDictionaryKeyCallBacks,
75                                              &kCFTypeDictionaryValueCallBacks );
76 
77     CFBooleanRef pCFVertBool = rFSP.mbVertical ? kCFBooleanTrue : kCFBooleanFalse;
78     CFDictionarySetValue( mpStyleDict, kCTVerticalFormsAttributeName, pCFVertBool );
79 
80     // fake bold
81     if ( (rFSP.GetWeight() >= WEIGHT_BOLD) &&
82          ((rPFF.GetWeight() < WEIGHT_SEMIBOLD) &&
83           (rPFF.GetWeight() != WEIGHT_DONTKNOW)) )
84     {
85         mbFauxBold = true;
86     }
87 
88     // fake italic
89     if (((rFSP.GetItalic() == ITALIC_NORMAL) ||
90          (rFSP.GetItalic() == ITALIC_OBLIQUE)) &&
91         (rPFF.GetItalic() == ITALIC_NONE))
92     {
93         aMatrix = CGAffineTransformConcat(aMatrix, CGAffineTransformMake(1, 0, toRadian(120), 1, 0, 0));
94     }
95 
96     CTFontDescriptorRef pFontDesc = reinterpret_cast<CTFontDescriptorRef>(rPFF.GetFontId());
97     CTFontRef pNewCTFont = CTFontCreateWithFontDescriptor( pFontDesc, fScaledFontHeight, &aMatrix );
98     CFDictionarySetValue( mpStyleDict, kCTFontAttributeName, pNewCTFont );
99     CFRelease( pNewCTFont);
100 }
101 
~CoreTextStyle()102 CoreTextStyle::~CoreTextStyle()
103 {
104     if( mpStyleDict )
105         CFRelease( mpStyleDict );
106 }
107 
GetFontMetric(ImplFontMetricDataRef const & rxFontMetric)108 void CoreTextStyle::GetFontMetric( ImplFontMetricDataRef const & rxFontMetric )
109 {
110     // get the matching CoreText font handle
111     // TODO: is it worth it to cache the CTFontRef in SetFont() and reuse it here?
112     CTFontRef aCTFontRef = static_cast<CTFontRef>(CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName ));
113 
114     rxFontMetric->ImplCalcLineSpacing(this);
115 
116     // since ImplFontMetricData::mnWidth is only used for stretching/squeezing fonts
117     // setting this width to the pixel height of the fontsize is good enough
118     // it also makes the calculation of the stretch factor simple
119     rxFontMetric->SetWidth( lrint( CTFontGetSize( aCTFontRef ) * mfFontStretch) );
120 
121     rxFontMetric->SetMinKashida(GetKashidaWidth());
122 }
123 
ImplGetGlyphBoundRect(sal_GlyphId nId,tools::Rectangle & rRect,bool bVertical) const124 bool CoreTextStyle::ImplGetGlyphBoundRect(sal_GlyphId nId, tools::Rectangle& rRect, bool bVertical) const
125 {
126     CGGlyph nCGGlyph = nId;
127     CTFontRef aCTFontRef = static_cast<CTFontRef>(CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName ));
128 
129     SAL_WNODEPRECATED_DECLARATIONS_PUSH //TODO: 10.11 kCTFontDefaultOrientation
130     const CTFontOrientation aFontOrientation = kCTFontDefaultOrientation; // TODO: horz/vert
131     SAL_WNODEPRECATED_DECLARATIONS_POP
132     CGRect aCGRect = CTFontGetBoundingRectsForGlyphs(aCTFontRef, aFontOrientation, &nCGGlyph, nullptr, 1);
133 
134     // Apply font rotation to non-vertical glyphs.
135     if (mfFontRotation && !bVertical)
136         aCGRect = CGRectApplyAffineTransform(aCGRect, CGAffineTransformMakeRotation(mfFontRotation));
137 
138     tools::Long xMin = floor(aCGRect.origin.x);
139     tools::Long yMin = floor(aCGRect.origin.y);
140     tools::Long xMax = ceil(aCGRect.origin.x + aCGRect.size.width);
141     tools::Long yMax = ceil(aCGRect.origin.y + aCGRect.size.height);
142     rRect = tools::Rectangle(xMin, -yMax, xMax, -yMin);
143     return true;
144 }
145 
146 namespace {
147 
148 // callbacks from CTFontCreatePathForGlyph+CGPathApply for GetGlyphOutline()
149 struct GgoData { basegfx::B2DPolygon maPolygon; basegfx::B2DPolyPolygon* mpPolyPoly; };
150 
151 }
152 
MyCGPathApplierFunc(void * pData,const CGPathElement * pElement)153 static void MyCGPathApplierFunc( void* pData, const CGPathElement* pElement )
154 {
155     basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
156     const int nPointCount = rPolygon.count();
157 
158     switch( pElement->type )
159     {
160     case kCGPathElementCloseSubpath:
161     case kCGPathElementMoveToPoint:
162         if( nPointCount > 0 )
163         {
164             static_cast<GgoData*>(pData)->mpPolyPoly->append( rPolygon );
165             rPolygon.clear();
166         }
167         // fall through for kCGPathElementMoveToPoint:
168         if( pElement->type != kCGPathElementMoveToPoint )
169         {
170             break;
171         }
172         [[fallthrough]];
173     case kCGPathElementAddLineToPoint:
174         rPolygon.append( basegfx::B2DPoint( +pElement->points[0].x, -pElement->points[0].y ) );
175         break;
176 
177     case kCGPathElementAddCurveToPoint:
178         rPolygon.append( basegfx::B2DPoint( +pElement->points[2].x, -pElement->points[2].y ) );
179         rPolygon.setNextControlPoint( nPointCount - 1,
180                                       basegfx::B2DPoint( pElement->points[0].x,
181                                                          -pElement->points[0].y ) );
182         rPolygon.setPrevControlPoint( nPointCount + 0,
183                                       basegfx::B2DPoint( pElement->points[1].x,
184                                                          -pElement->points[1].y ) );
185         break;
186 
187     case kCGPathElementAddQuadCurveToPoint:
188         {
189             const basegfx::B2DPoint aStartPt = rPolygon.getB2DPoint( nPointCount-1 );
190             const basegfx::B2DPoint aCtrPt1( (aStartPt.getX() + 2 * pElement->points[0].x) / 3.0,
191                                              (aStartPt.getY() - 2 * pElement->points[0].y) / 3.0 );
192             const basegfx::B2DPoint aCtrPt2( (+2 * pElement->points[0].x + pElement->points[1].x) / 3.0,
193                                              (-2 * pElement->points[0].y - pElement->points[1].y) / 3.0 );
194             rPolygon.append( basegfx::B2DPoint( +pElement->points[1].x, -pElement->points[1].y ) );
195             rPolygon.setNextControlPoint( nPointCount-1, aCtrPt1 );
196             rPolygon.setPrevControlPoint( nPointCount+0, aCtrPt2 );
197         }
198         break;
199     }
200 }
201 
GetGlyphOutline(sal_GlyphId nId,basegfx::B2DPolyPolygon & rResult,bool) const202 bool CoreTextStyle::GetGlyphOutline(sal_GlyphId nId, basegfx::B2DPolyPolygon& rResult, bool) const
203 {
204     rResult.clear();
205 
206     CGGlyph nCGGlyph = nId;
207     CTFontRef pCTFont = static_cast<CTFontRef>(CFDictionaryGetValue( mpStyleDict, kCTFontAttributeName ));
208 
209     SAL_WNODEPRECATED_DECLARATIONS_PUSH
210     const CTFontOrientation aFontOrientation = kCTFontDefaultOrientation;
211     SAL_WNODEPRECATED_DECLARATIONS_POP
212     CGRect aCGRect = CTFontGetBoundingRectsForGlyphs(pCTFont, aFontOrientation, &nCGGlyph, nullptr, 1);
213 
214     if (!CGRectIsNull(aCGRect) && CGRectIsEmpty(aCGRect))
215     {
216         // CTFontCreatePathForGlyph returns NULL for blank glyphs, but we want
217         // to return true for them.
218         return true;
219     }
220 
221     CGPathRef xPath = CTFontCreatePathForGlyph( pCTFont, nCGGlyph, nullptr );
222     if (!xPath)
223     {
224         return false;
225     }
226 
227     GgoData aGgoData;
228     aGgoData.mpPolyPoly = &rResult;
229     CGPathApply( xPath, static_cast<void*>(&aGgoData), MyCGPathApplierFunc );
230 #if 0 // TODO: does OSX ensure that the last polygon is always closed?
231     const CGPathElement aClosingElement = { kCGPathElementCloseSubpath, NULL };
232     MyCGPathApplierFunc( (void*)&aGgoData, &aClosingElement );
233 #endif
234     CFRelease( xPath );
235 
236     return true;
237 }
238 
getFontTable(hb_face_t *,hb_tag_t nTableTag,void * pUserData)239 static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pUserData)
240 {
241     sal_uLong nLength = 0;
242     unsigned char* pBuffer = nullptr;
243     CoreTextFontFace* pFont = static_cast<CoreTextFontFace*>(pUserData);
244     nLength = pFont->GetFontTable(nTableTag, nullptr);
245     if (nLength > 0)
246     {
247         pBuffer = new unsigned char[nLength];
248         pFont->GetFontTable(nTableTag, pBuffer);
249     }
250 
251     hb_blob_t* pBlob = nullptr;
252     if (pBuffer != nullptr)
253         pBlob = hb_blob_create(reinterpret_cast<const char*>(pBuffer), nLength, HB_MEMORY_MODE_READONLY,
254                                pBuffer, [](void* data){ delete[] static_cast<unsigned char*>(data); });
255     return pBlob;
256 }
257 
ImplInitHbFont()258 hb_font_t* CoreTextStyle::ImplInitHbFont()
259 {
260     hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, GetFontFace(), nullptr);
261 
262     return InitHbFont(pHbFace);
263 }
264 
CreateFontInstance(const FontSelectPattern & rFSD) const265 rtl::Reference<LogicalFontInstance> CoreTextFontFace::CreateFontInstance(const FontSelectPattern& rFSD) const
266 {
267     return new CoreTextStyle(*this, rFSD);
268 }
269 
GetFontTable(const char pTagName[5],unsigned char * pResultBuf) const270 int CoreTextFontFace::GetFontTable( const char pTagName[5], unsigned char* pResultBuf ) const
271 {
272     SAL_WARN_IF( pTagName[4]!='\0', "vcl", "CoreTextFontFace::GetFontTable with invalid tagname!" );
273 
274     const CTFontTableTag nTagCode = (pTagName[0]<<24) + (pTagName[1]<<16) + (pTagName[2]<<8) + (pTagName[3]<<0);
275 
276     return GetFontTable(nTagCode, pResultBuf);
277 }
278 
GetFontTable(uint32_t nTagCode,unsigned char * pResultBuf) const279 int CoreTextFontFace::GetFontTable(uint32_t nTagCode, unsigned char* pResultBuf ) const
280 {
281     // get the raw table length
282     CTFontDescriptorRef pFontDesc = reinterpret_cast<CTFontDescriptorRef>( GetFontId());
283     CTFontRef rCTFont = CTFontCreateWithFontDescriptor( pFontDesc, 0.0, nullptr);
284     const uint32_t opts( kCTFontTableOptionNoOptions );
285     CFDataRef pDataRef = CTFontCopyTable( rCTFont, nTagCode, opts);
286     CFRelease( rCTFont);
287     if( !pDataRef)
288         return 0;
289 
290     const CFIndex nByteLength = CFDataGetLength( pDataRef);
291 
292     // get the raw table data if requested
293     if( pResultBuf && (nByteLength > 0))
294     {
295         const CFRange aFullRange = CFRangeMake( 0, nByteLength);
296         CFDataGetBytes( pDataRef, aFullRange, reinterpret_cast<UInt8*>(pResultBuf));
297     }
298 
299     CFRelease( pDataRef);
300 
301     return static_cast<int>(nByteLength);
302 }
303 
DevFontFromCTFontDescriptor(CTFontDescriptorRef pFD,bool * bFontEnabled)304 FontAttributes DevFontFromCTFontDescriptor( CTFontDescriptorRef pFD, bool* bFontEnabled )
305 {
306     // all CoreText fonts are device fonts that can rotate just fine
307     FontAttributes rDFA;
308     rDFA.SetQuality( 0 );
309 
310     // reset the font attributes
311     rDFA.SetFamilyType( FAMILY_DONTKNOW );
312     rDFA.SetPitch( PITCH_VARIABLE );
313     rDFA.SetWidthType( WIDTH_NORMAL );
314     rDFA.SetWeight( WEIGHT_NORMAL );
315     rDFA.SetItalic( ITALIC_NONE );
316     rDFA.SetSymbolFlag( false );
317 
318     // get font name
319 #ifdef MACOSX
320     CFStringRef pLang = nullptr;
321     CFStringRef pFamilyName = static_cast<CFStringRef>(
322             CTFontDescriptorCopyLocalizedAttribute( pFD, kCTFontFamilyNameAttribute, &pLang ));
323 
324     if ( !pLang )
325     {
326         if( pFamilyName )
327         {
328             CFRelease( pFamilyName );
329         }
330         pFamilyName = static_cast<CFStringRef>(CTFontDescriptorCopyAttribute( pFD, kCTFontFamilyNameAttribute ));
331     }
332 #else
333     // No "Application" on iOS. And it is unclear whether this code
334     // snippet will actually ever get invoked on iOS anyway. So just
335     // use the old code that uses a non-localized font name.
336     CFStringRef pFamilyName = (CFStringRef)CTFontDescriptorCopyAttribute( pFD, kCTFontFamilyNameAttribute );
337 #endif
338 
339     rDFA.SetFamilyName( GetOUString( pFamilyName ) );
340 
341     // get font style
342     CFStringRef pStyleName = static_cast<CFStringRef>(CTFontDescriptorCopyAttribute( pFD, kCTFontStyleNameAttribute ));
343     rDFA.SetStyleName( GetOUString( pStyleName ) );
344 
345     // get font-enabled status
346     if( bFontEnabled )
347     {
348         int bEnabled = TRUE; // by default (and when we're on macOS < 10.6) it's "enabled"
349         CFNumberRef pEnabled = static_cast<CFNumberRef>(CTFontDescriptorCopyAttribute( pFD, kCTFontEnabledAttribute ));
350         CFNumberGetValue( pEnabled, kCFNumberIntType, &bEnabled );
351         *bFontEnabled = bEnabled;
352     }
353 
354     // get font attributes
355     CFDictionaryRef pAttrDict = static_cast<CFDictionaryRef>(CTFontDescriptorCopyAttribute( pFD, kCTFontTraitsAttribute ));
356 
357     if (bFontEnabled && *bFontEnabled)
358     {
359         // Ignore font formats not supported.
360         int nFormat;
361         CFNumberRef pFormat = static_cast<CFNumberRef>(CTFontDescriptorCopyAttribute(pFD, kCTFontFormatAttribute));
362         CFNumberGetValue(pFormat, kCFNumberIntType, &nFormat);
363         if (nFormat == kCTFontFormatUnrecognized || nFormat == kCTFontFormatPostScript || nFormat == kCTFontFormatBitmap)
364         {
365             SAL_INFO("vcl.fonts", "Ignoring font with unsupported format: " << rDFA.GetFamilyName());
366             *bFontEnabled = false;
367         }
368         CFRelease(pFormat);
369     }
370 
371     // get symbolic trait
372     // TODO: use other traits such as MonoSpace/Condensed/Expanded or Vertical too
373     SInt64 nSymbolTrait = 0;
374     CFNumberRef pSymbolNum = nullptr;
375     if( CFDictionaryGetValueIfPresent( pAttrDict, kCTFontSymbolicTrait, reinterpret_cast<const void**>(&pSymbolNum) ) )
376     {
377         CFNumberGetValue( pSymbolNum, kCFNumberSInt64Type, &nSymbolTrait );
378         rDFA.SetSymbolFlag( (nSymbolTrait & kCTFontClassMaskTrait) == kCTFontSymbolicClass );
379 
380         if (nSymbolTrait & kCTFontMonoSpaceTrait)
381             rDFA.SetPitch(PITCH_FIXED);
382     }
383 
384     // get the font weight
385     double fWeight = 0;
386     CFNumberRef pWeightNum = static_cast<CFNumberRef>(CFDictionaryGetValue( pAttrDict, kCTFontWeightTrait ));
387     CFNumberGetValue( pWeightNum, kCFNumberDoubleType, &fWeight );
388     int nInt = WEIGHT_NORMAL;
389 
390     // Special case fixes
391 
392     // tdf#67744: Courier Std Medium is always bold. We get a kCTFontWeightTrait of 0.23 which
393     // surely must be wrong.
394     if (rDFA.GetFamilyName() == "Courier Std" &&
395         (rDFA.GetStyleName() == "Medium" || rDFA.GetStyleName() == "Medium Oblique") &&
396         fWeight > 0.2)
397     {
398         fWeight = 0;
399     }
400 
401     // tdf#68889: Ditto for Gill Sans MT Pro. Here I can kinda understand it, maybe the
402     // kCTFontWeightTrait is intended to give a subjective "optical" impression of how the font
403     // looks, and Gill Sans MT Pro Medium is kinda heavy. But with the way LibreOffice uses fonts,
404     // we still should think of it as being "medium" weight.
405     if (rDFA.GetFamilyName() == "Gill Sans MT Pro" &&
406         (rDFA.GetStyleName() == "Medium" || rDFA.GetStyleName() == "Medium Italic") &&
407         fWeight > 0.2)
408     {
409         fWeight = 0;
410     }
411 
412     if( fWeight > 0 )
413     {
414         nInt = rint(int(WEIGHT_NORMAL) + fWeight * ((WEIGHT_BLACK - WEIGHT_NORMAL)/0.68));
415         if( nInt > WEIGHT_BLACK )
416         {
417             nInt = WEIGHT_BLACK;
418         }
419     }
420     else if( fWeight < 0 )
421     {
422         nInt = rint(int(WEIGHT_NORMAL) + fWeight * ((WEIGHT_NORMAL - WEIGHT_THIN)/0.8));
423         if( nInt < WEIGHT_THIN )
424         {
425             nInt = WEIGHT_THIN;
426         }
427     }
428     rDFA.SetWeight( static_cast<FontWeight>(nInt) );
429 
430     // get the font slant
431     double fSlant = 0;
432     CFNumberRef pSlantNum = static_cast<CFNumberRef>(CFDictionaryGetValue( pAttrDict, kCTFontSlantTrait ));
433     CFNumberGetValue( pSlantNum, kCFNumberDoubleType, &fSlant );
434     if( fSlant >= 0.035 )
435     {
436         rDFA.SetItalic( ITALIC_NORMAL );
437     }
438     // get width trait
439     double fWidth = 0;
440     CFNumberRef pWidthNum = static_cast<CFNumberRef>(CFDictionaryGetValue( pAttrDict, kCTFontWidthTrait ));
441     CFNumberGetValue( pWidthNum, kCFNumberDoubleType, &fWidth );
442     nInt = WIDTH_NORMAL;
443 
444     if( fWidth > 0 )
445     {
446         nInt = rint( int(WIDTH_NORMAL) + fWidth * ((WIDTH_ULTRA_EXPANDED - WIDTH_NORMAL)/0.4));
447         if( nInt > WIDTH_ULTRA_EXPANDED )
448         {
449             nInt = WIDTH_ULTRA_EXPANDED;
450         }
451     }
452     else if( fWidth < 0 )
453     {
454         nInt = rint( int(WIDTH_NORMAL) + fWidth * ((WIDTH_NORMAL - WIDTH_ULTRA_CONDENSED)/0.5));
455         if( nInt < WIDTH_ULTRA_CONDENSED )
456         {
457             nInt = WIDTH_ULTRA_CONDENSED;
458         }
459     }
460     rDFA.SetWidthType( static_cast<FontWidth>(nInt) );
461 
462     // release the attribute dict that we had copied
463     CFRelease( pAttrDict );
464 
465     // TODO? also use the HEAD table if available to get more attributes
466 //  CFDataRef CTFontCopyTable( CTFontRef, kCTFontTableHead, /*kCTFontTableOptionNoOptions*/kCTFontTableOptionExcludeSynthetic );
467 
468     return rDFA;
469 }
470 
fontEnumCallBack(const void * pValue,void * pContext)471 static void fontEnumCallBack( const void* pValue, void* pContext )
472 {
473     CTFontDescriptorRef pFD = static_cast<CTFontDescriptorRef>(pValue);
474 
475     bool bFontEnabled;
476     FontAttributes rDFA = DevFontFromCTFontDescriptor( pFD, &bFontEnabled );
477 
478     if( bFontEnabled)
479     {
480         const sal_IntPtr nFontId = reinterpret_cast<sal_IntPtr>(pValue);
481         rtl::Reference<CoreTextFontFace> pFontData = new CoreTextFontFace( rDFA, nFontId );
482         SystemFontList* pFontList = static_cast<SystemFontList*>(pContext);
483         pFontList->AddFont( pFontData.get() );
484     }
485 }
486 
SystemFontList()487 SystemFontList::SystemFontList()
488   : mpCTFontCollection( nullptr )
489   , mpCTFontArray( nullptr )
490 {}
491 
~SystemFontList()492 SystemFontList::~SystemFontList()
493 {
494     maFontContainer.clear();
495 
496     if( mpCTFontArray )
497     {
498         CFRelease( mpCTFontArray );
499     }
500     if( mpCTFontCollection )
501     {
502         CFRelease( mpCTFontCollection );
503     }
504 }
505 
AddFont(CoreTextFontFace * pFontData)506 void SystemFontList::AddFont( CoreTextFontFace* pFontData )
507 {
508     sal_IntPtr nFontId = pFontData->GetFontId();
509     maFontContainer[ nFontId ] = pFontData;
510 }
511 
AnnounceFonts(PhysicalFontCollection & rFontCollection) const512 void SystemFontList::AnnounceFonts( PhysicalFontCollection& rFontCollection ) const
513 {
514     for(const auto& rEntry : maFontContainer )
515     {
516         rFontCollection.Add( rEntry.second.get() );
517     }
518 }
519 
GetFontDataFromId(sal_IntPtr nFontId) const520 CoreTextFontFace* SystemFontList::GetFontDataFromId( sal_IntPtr nFontId ) const
521 {
522     auto it = maFontContainer.find( nFontId );
523     if( it == maFontContainer.end() )
524     {
525         return nullptr;
526     }
527     return (*it).second.get();
528 }
529 
Init()530 bool SystemFontList::Init()
531 {
532     // enumerate available system fonts
533     static const int nMaxDictEntries = 8;
534     CFMutableDictionaryRef pCFDict = CFDictionaryCreateMutable( nullptr,
535                                                                 nMaxDictEntries,
536                                                                 &kCFTypeDictionaryKeyCallBacks,
537                                                                 &kCFTypeDictionaryValueCallBacks );
538 
539     CFDictionaryAddValue( pCFDict, kCTFontCollectionRemoveDuplicatesOption, kCFBooleanTrue );
540     mpCTFontCollection = CTFontCollectionCreateFromAvailableFonts( pCFDict );
541     CFRelease( pCFDict );
542     mpCTFontArray = CTFontCollectionCreateMatchingFontDescriptors( mpCTFontCollection );
543 
544     const int nFontCount = CFArrayGetCount( mpCTFontArray );
545     const CFRange aFullRange = CFRangeMake( 0, nFontCount );
546     CFArrayApplyFunction( mpCTFontArray, aFullRange, fontEnumCallBack, this );
547 
548     return true;
549 }
550 
GetCoretextFontList()551 SystemFontList* GetCoretextFontList()
552 {
553     SystemFontList* pList = new SystemFontList();
554     if( !pList->Init() )
555     {
556         delete pList;
557         return nullptr;
558     }
559 
560     return pList;
561 }
562 
563 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
564