(fontDescriptor: FontDescriptor)
| 24 | } |
| 25 | |
| 26 | function getUIFontCached(fontDescriptor: FontDescriptor) { |
| 27 | const cacheKey = computeFontCacheKey(fontDescriptor); |
| 28 | |
| 29 | if (uiFontCache.has(cacheKey)) { |
| 30 | if (Trace.isEnabled()) { |
| 31 | Trace.write(`UIFont reuse from cache: ${JSON.stringify(fontDescriptor)}, cache size: ${uiFontCache.size}`, Trace.categories.Style, Trace.messageType.info); |
| 32 | } |
| 33 | return uiFontCache.get(cacheKey); |
| 34 | } |
| 35 | let uiFont = NativeScriptUtils.createUIFont(fontDescriptor as any); |
| 36 | if (fontDescriptor.fontVariationSettings?.length) { |
| 37 | let font = CGFontCreateWithFontName(uiFont.fontName); |
| 38 | const variationAxes: NSArray<NSDictionary<FontVariationAxisType, string | number>> = CGFontCopyVariationAxes(font); |
| 39 | // This can be null if font doesn't support axes |
| 40 | if (variationAxes?.count) { |
| 41 | const variationSettings = NSMutableDictionary.new(); |
| 42 | const variationAxesCount = variationAxes.count; |
| 43 | const variationAxesNames: string[] = []; |
| 44 | |
| 45 | for (let i = 0, length = variationAxes.count; i < length; i++) { |
| 46 | variationAxesNames.push(String(variationAxes[i].objectForKey('kCGFontVariationAxisName'))); |
| 47 | } |
| 48 | |
| 49 | for (const variationSetting of fontDescriptor.fontVariationSettings) { |
| 50 | const axisName = fuzzySearch(variationSetting.axis, variationAxesNames); |
| 51 | if (axisName?.length) { |
| 52 | variationSettings.setValueForKey(variationSetting.value, axisName[0]); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | font = CGFontCreateCopyWithVariations(font, variationSettings); |
| 57 | uiFont = CTFontCreateWithGraphicsFont(font, fontDescriptor.fontSize, null, null); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | uiFontCache.set(cacheKey, uiFont); |
| 62 | if (Trace.isEnabled()) { |
| 63 | Trace.write(`UIFont creation: ${JSON.stringify(fontDescriptor)}, cache size: ${uiFontCache.size}`, Trace.categories.Style, Trace.messageType.info); |
| 64 | } |
| 65 | |
| 66 | return uiFont; |
| 67 | } |
| 68 | |
| 69 | export class Font extends FontBase { |
| 70 | static default = new Font(undefined, undefined); |
no test coverage detected