summaryrefslogtreecommitdiffstats
path: root/gfx/2d/ScaledFontMac.cpp
blob: 042c0a19ebcd3340b0a0405a496badbe5f9f0842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ScaledFontMac.h"
#include "UnscaledFontMac.h"
#include "mozilla/webrender/WebRenderTypes.h"
#include "nsCocoaFeatures.h"
#include "PathSkia.h"
#include "skia/include/core/SkPaint.h"
#include "skia/include/core/SkPath.h"
#include "skia/include/ports/SkTypeface_mac.h"
#include <vector>
#include <dlfcn.h>
#ifdef MOZ_WIDGET_UIKIT
#  include <CoreFoundation/CoreFoundation.h>
#endif
#include "nsCocoaFeatures.h"
#include "mozilla/gfx/Logging.h"

#ifdef MOZ_WIDGET_COCOA
// prototype for private API
extern "C" {
CGPathRef CGFontGetGlyphPath(CGFontRef fontRef,
                             CGAffineTransform* textTransform, int unknown,
                             CGGlyph glyph);
};
#endif

#include "cairo-quartz.h"

namespace mozilla {
namespace gfx {

// Simple helper class to automatically release a CFObject when it goes out
// of scope.
template <class T>
class AutoRelease final {
 public:
  explicit AutoRelease(T aObject) : mObject(aObject) {}

  ~AutoRelease() {
    if (mObject) {
      CFRelease(mObject);
    }
  }

  AutoRelease<T>& operator=(const T& aObject) {
    if (aObject != mObject) {
      if (mObject) {
        CFRelease(mObject);
      }
      mObject = aObject;
    }
    return *this;
  }

  operator T() { return mObject; }

  T forget() {
    T obj = mObject;
    mObject = nullptr;
    return obj;
  }

 private:
  T mObject;
};

// Helper to create a CTFont from a CGFont, copying any variations that were
// set on the CGFont, and applying attributes from (optional) aFontDesc.
CTFontRef CreateCTFontFromCGFontWithVariations(CGFontRef aCGFont, CGFloat aSize,
                                               bool aInstalledFont,
                                               CTFontDescriptorRef aFontDesc) {
  // New implementation (see bug 1856035) for macOS 13+.
  if (nsCocoaFeatures::OnVenturaOrLater()) {
    // Create CTFont, applying any descriptor that was passed (used by
    // gfxCoreTextShaper to set features).
    AutoRelease<CTFontRef> ctFont(
        CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, aFontDesc));
    AutoRelease<CFDictionaryRef> vars(CGFontCopyVariations(aCGFont));
    if (vars) {
      // Create an attribute dictionary containing the variations.
      AutoRelease<CFDictionaryRef> attrs(CFDictionaryCreate(
          nullptr, (const void**)&kCTFontVariationAttribute,
          (const void**)&vars, 1, &kCFTypeDictionaryKeyCallBacks,
          &kCFTypeDictionaryValueCallBacks));
      // Get the original descriptor from the CTFont, then add the variations
      // attribute to it.
      AutoRelease<CTFontDescriptorRef> desc(CTFontCopyFontDescriptor(ctFont));
      desc = CTFontDescriptorCreateCopyWithAttributes(desc, attrs);
      // Return a copy of the font that has the variations added.
      return CTFontCreateCopyWithAttributes(ctFont, 0.0, nullptr, desc);
    }
    // No variations to set, just return the default CTFont.
    return ctFont.forget();
  }

  // Older implementation used up to macOS 12.
  CTFontRef ctFont;
  if (aInstalledFont) {
    AutoRelease<CFDictionaryRef> vars(CGFontCopyVariations(aCGFont));
    if (vars) {
      AutoRelease<CFDictionaryRef> varAttr(CFDictionaryCreate(
          nullptr, (const void**)&kCTFontVariationAttribute,
          (const void**)&vars, 1, &kCFTypeDictionaryKeyCallBacks,
          &kCFTypeDictionaryValueCallBacks));

      AutoRelease<CTFontDescriptorRef> varDesc(
          aFontDesc
              ? ::CTFontDescriptorCreateCopyWithAttributes(aFontDesc, varAttr)
              : ::CTFontDescriptorCreateWithAttributes(varAttr));

      ctFont = CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, varDesc);
    } else {
      ctFont = CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, nullptr);
    }
  } else {
    ctFont = CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, nullptr);
  }
  return ctFont;
}

ScaledFontMac::ScaledFontMac(CGFontRef aFont,
                             const RefPtr<UnscaledFont>& aUnscaledFont,
                             Float aSize, bool aOwnsFont,
                             const DeviceColor& aFontSmoothingBackgroundColor,
                             bool aUseFontSmoothing, bool aApplySyntheticBold,
                             bool aHasColorGlyphs)
    : ScaledFontBase(aUnscaledFont, aSize),
      mFont(aFont),
      mFontSmoothingBackgroundColor(aFontSmoothingBackgroundColor),
      mUseFontSmoothing(aUseFontSmoothing),
      mApplySyntheticBold(aApplySyntheticBold),
      mHasColorGlyphs(aHasColorGlyphs) {
  if (!aOwnsFont) {
    // XXX: should we be taking a reference
    CGFontRetain(aFont);
  }

  auto unscaledMac = static_cast<UnscaledFontMac*>(aUnscaledFont.get());
  bool dataFont = unscaledMac->IsDataFont();
  mCTFont = CreateCTFontFromCGFontWithVariations(aFont, aSize, !dataFont);
}

ScaledFontMac::ScaledFontMac(CTFontRef aFont,
                             const RefPtr<UnscaledFont>& aUnscaledFont,
                             const DeviceColor& aFontSmoothingBackgroundColor,
                             bool aUseFontSmoothing, bool aApplySyntheticBold,
                             bool aHasColorGlyphs)
    : ScaledFontBase(aUnscaledFont, CTFontGetSize(aFont)),
      mCTFont(aFont),
      mFontSmoothingBackgroundColor(aFontSmoothingBackgroundColor),
      mUseFontSmoothing(aUseFontSmoothing),
      mApplySyntheticBold(aApplySyntheticBold),
      mHasColorGlyphs(aHasColorGlyphs) {
  mFont = CTFontCopyGraphicsFont(aFont, nullptr);

  CFRetain(mCTFont);
}

ScaledFontMac::~ScaledFontMac() {
  CFRelease(mCTFont);
  CGFontRelease(mFont);
}

SkTypeface* ScaledFontMac::CreateSkTypeface() {
  return SkMakeTypefaceFromCTFont(mCTFont).release();
}

void ScaledFontMac::SetupSkFontDrawOptions(SkFont& aFont) {
  aFont.setSubpixel(true);

  // Normally, Skia enables LCD FontSmoothing which creates thicker fonts
  // and also enables subpixel AA. CoreGraphics without font smoothing
  // explicitly creates thinner fonts and grayscale AA.
  // CoreGraphics doesn't support a configuration that produces thicker
  // fonts with grayscale AA as LCD Font Smoothing enables or disables
  // both. However, Skia supports it by enabling font smoothing (producing
  // subpixel AA) and converts it to grayscale AA. Since Skia doesn't
  // support subpixel AA on transparent backgrounds, we still want font
  // smoothing for the thicker fonts, even if it is grayscale AA.
  //
  // With explicit Grayscale AA (from -moz-osx-font-smoothing:grayscale),
  // we want to have grayscale AA with no smoothing at all. This means
  // disabling the LCD font smoothing behaviour.
  // To accomplish this we have to explicitly disable hinting,
  // and disable LCDRenderText.
  if (aFont.getEdging() == SkFont::Edging::kAntiAlias && !mUseFontSmoothing) {
    aFont.setHinting(SkFontHinting::kNone);
  }
}

// private API here are the public options on OS X
// CTFontCreatePathForGlyph
// ATSUGlyphGetCubicPaths
// we've used this in cairo sucessfully for some time.
// Note: cairo dlsyms it. We could do that but maybe it's
// safe just to use?

already_AddRefed<Path> ScaledFontMac::GetPathForGlyphs(
    const GlyphBuffer& aBuffer, const DrawTarget* aTarget) {
  return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
}

static uint32_t CalcTableChecksum(const uint32_t* tableStart, uint32_t length,
                                  bool skipChecksumAdjust = false) {
  uint32_t sum = 0L;
  const uint32_t* table = tableStart;
  const uint32_t* end = table + length / sizeof(uint32_t);
  while (table < end) {
    if (skipChecksumAdjust && (table - tableStart) == 2) {
      table++;
    } else {
      sum += CFSwapInt32BigToHost(*table++);
    }
  }

  // The length is not 4-byte aligned, but we still must process the remaining
  // bytes.
  if (length & 3) {
    // Pad with zero before adding to the checksum.
    uint32_t last = 0;
    memcpy(&last, end, length & 3);
    sum += CFSwapInt32BigToHost(last);
  }

  return sum;
}

struct TableRecord {
  uint32_t tag;
  uint32_t checkSum;
  uint32_t offset;
  uint32_t length;
  CFDataRef data;
};

static int maxPow2LessThanEqual(int a) {
  int x = 1;
  int shift = 0;
  while ((x << (shift + 1)) <= a) {
    shift++;
  }
  return shift;
}

struct writeBuf final {
  explicit writeBuf(int size) {
    this->data = new unsigned char[size];
    this->offset = 0;
  }
  ~writeBuf() { delete[] this->data; }

  template <class T>
  void writeElement(T a) {
    *reinterpret_cast<T*>(&this->data[this->offset]) = a;
    this->offset += sizeof(T);
  }

  void writeMem(const void* data, unsigned long length) {
    memcpy(&this->data[this->offset], data, length);
    this->offset += length;
  }

  void align() {
    while (this->offset & 3) {
      this->data[this->offset] = 0;
      this->offset++;
    }
  }

  unsigned char* data;
  int offset;
};

bool UnscaledFontMac::GetFontFileData(FontFileDataOutput aDataCallback,
                                      void* aBaton) {
  // We'll reconstruct a TTF font from the tables we can get from the CGFont
  CFArrayRef tags = CGFontCopyTableTags(mFont);
  CFIndex count = CFArrayGetCount(tags);

  TableRecord* records = new TableRecord[count];
  uint32_t offset = 0;
  offset += sizeof(uint32_t) * 3;
  offset += sizeof(uint32_t) * 4 * count;
  bool CFF = false;
  for (CFIndex i = 0; i < count; i++) {
    uint32_t tag = (uint32_t)(uintptr_t)CFArrayGetValueAtIndex(tags, i);
    if (tag == 0x43464620) {  // 'CFF '
      CFF = true;
    }
    CFDataRef data = CGFontCopyTableForTag(mFont, tag);
    // Bug 1602391 suggests CGFontCopyTableForTag can fail, even though we just
    // got the tag from the font via CGFontCopyTableTags above. If we can catch
    // this (e.g. in fuzz-testing) it'd be good to understand when it happens,
    // but in any case we'll handle it safely below by treating the table as
    // zero-length.
    MOZ_ASSERT(data, "failed to get font table data");
    records[i].tag = tag;
    records[i].offset = offset;
    records[i].data = data;
    if (data) {
      records[i].length = CFDataGetLength(data);
      bool skipChecksumAdjust = (tag == 0x68656164);  // 'head'
      records[i].checkSum = CalcTableChecksum(
          reinterpret_cast<const uint32_t*>(CFDataGetBytePtr(data)),
          records[i].length, skipChecksumAdjust);
      offset += records[i].length;
      // 32 bit align the tables
      offset = (offset + 3) & ~3;
    } else {
      records[i].length = 0;
      records[i].checkSum = 0;
    }
  }
  CFRelease(tags);

  struct writeBuf buf(offset);
  // write header/offset table
  if (CFF) {
    buf.writeElement(CFSwapInt32HostToBig(0x4f54544f));
  } else {
    buf.writeElement(CFSwapInt32HostToBig(0x00010000));
  }
  buf.writeElement(CFSwapInt16HostToBig(count));
  int maxPow2Count = maxPow2LessThanEqual(count);
  buf.writeElement(CFSwapInt16HostToBig((1 << maxPow2Count) * 16));
  buf.writeElement(CFSwapInt16HostToBig(maxPow2Count));
  buf.writeElement(CFSwapInt16HostToBig((count - (1 << maxPow2Count)) * 16));

  // write table record entries
  for (CFIndex i = 0; i < count; i++) {
    buf.writeElement(CFSwapInt32HostToBig(records[i].tag));
    buf.writeElement(CFSwapInt32HostToBig(records[i].checkSum));
    buf.writeElement(CFSwapInt32HostToBig(records[i].offset));
    buf.writeElement(CFSwapInt32HostToBig(records[i].length));
  }

  // write tables
  int checkSumAdjustmentOffset = 0;
  for (CFIndex i = 0; i < count; i++) {
    if (records[i].tag == 0x68656164) {
      checkSumAdjustmentOffset = buf.offset + 2 * 4;
    }
    if (records[i].data) {
      buf.writeMem(CFDataGetBytePtr(records[i].data), records[i].length);
      buf.align();
      CFRelease(records[i].data);
    }
  }
  delete[] records;

  // clear the checksumAdjust field before checksumming the whole font
  memset(&buf.data[checkSumAdjustmentOffset], 0, sizeof(uint32_t));
  uint32_t fontChecksum = CFSwapInt32HostToBig(
      0xb1b0afba -
      CalcTableChecksum(reinterpret_cast<const uint32_t*>(buf.data), offset));
  // set checkSumAdjust to the computed checksum
  memcpy(&buf.data[checkSumAdjustmentOffset], &fontChecksum,
         sizeof(fontChecksum));

  // we always use an index of 0
  aDataCallback(buf.data, buf.offset, 0, aBaton);

  return true;
}

bool UnscaledFontMac::GetFontDescriptor(FontDescriptorOutput aCb,
                                        void* aBaton) {
  if (mIsDataFont) {
    return false;
  }

  AutoRelease<CFStringRef> psname(CGFontCopyPostScriptName(mFont));
  if (!psname) {
    return false;
  }

  char buf[256];
  const char* cstr = CFStringGetCStringPtr(psname, kCFStringEncodingUTF8);
  if (!cstr) {
    if (!CFStringGetCString(psname, buf, sizeof(buf), kCFStringEncodingUTF8)) {
      return false;
    }
    cstr = buf;
  }

  aCb(reinterpret_cast<const uint8_t*>(cstr), strlen(cstr), 0, aBaton);
  return true;
}

static void CollectVariationsFromDictionary(const void* aKey,
                                            const void* aValue,
                                            void* aContext) {
  auto keyPtr = static_cast<const CFTypeRef>(aKey);
  auto valuePtr = static_cast<const CFTypeRef>(aValue);
  auto outVariations = static_cast<std::vector<FontVariation>*>(aContext);
  if (CFGetTypeID(keyPtr) == CFNumberGetTypeID() &&
      CFGetTypeID(valuePtr) == CFNumberGetTypeID()) {
    uint64_t t;
    double v;
    if (CFNumberGetValue(static_cast<CFNumberRef>(keyPtr), kCFNumberSInt64Type,
                         &t) &&
        CFNumberGetValue(static_cast<CFNumberRef>(valuePtr),
                         kCFNumberDoubleType, &v)) {
      outVariations->push_back(FontVariation{uint32_t(t), float(v)});
    }
  }
}

static bool GetVariationsForCTFont(CTFontRef aCTFont,
                                   std::vector<FontVariation>* aOutVariations) {
  if (!aCTFont) {
    return true;
  }
  AutoRelease<CFDictionaryRef> dict(CTFontCopyVariation(aCTFont));
  CFIndex count = dict ? CFDictionaryGetCount(dict) : 0;
  if (count > 0) {
    aOutVariations->reserve(count);
    CFDictionaryApplyFunction(dict, CollectVariationsFromDictionary,
                              aOutVariations);
  }
  return true;
}

bool ScaledFontMac::GetFontInstanceData(FontInstanceDataOutput aCb,
                                        void* aBaton) {
  // Collect any variation settings that were incorporated into the CTFont.
  std::vector<FontVariation> variations;
  if (!GetVariationsForCTFont(mCTFont, &variations)) {
    return false;
  }

  InstanceData instance(this);
  aCb(reinterpret_cast<uint8_t*>(&instance), sizeof(instance),
      variations.data(), variations.size(), aBaton);
  return true;
}

bool ScaledFontMac::GetWRFontInstanceOptions(
    Maybe<wr::FontInstanceOptions>* aOutOptions,
    Maybe<wr::FontInstancePlatformOptions>* aOutPlatformOptions,
    std::vector<FontVariation>* aOutVariations) {
  GetVariationsForCTFont(mCTFont, aOutVariations);

  wr::FontInstanceOptions options;
  options.render_mode = wr::FontRenderMode::Subpixel;
  options.flags = wr::FontInstanceFlags::SUBPIXEL_POSITION;
  if (mUseFontSmoothing) {
    options.flags |= wr::FontInstanceFlags::FONT_SMOOTHING;
  }
  if (mApplySyntheticBold) {
    options.flags |= wr::FontInstanceFlags::SYNTHETIC_BOLD;
  }
  if (mHasColorGlyphs) {
    options.flags |= wr::FontInstanceFlags::EMBEDDED_BITMAPS;
  }
  options.bg_color = wr::ToColorU(mFontSmoothingBackgroundColor);
  options.synthetic_italics =
      wr::DegreesToSyntheticItalics(GetSyntheticObliqueAngle());
  *aOutOptions = Some(options);
  return true;
}

ScaledFontMac::InstanceData::InstanceData(
    const wr::FontInstanceOptions* aOptions,
    const wr::FontInstancePlatformOptions* aPlatformOptions)
    : mUseFontSmoothing(true),
      mApplySyntheticBold(false),
      mHasColorGlyphs(false) {
  if (aOptions) {
    if (!(aOptions->flags & wr::FontInstanceFlags::FONT_SMOOTHING)) {
      mUseFontSmoothing = false;
    }
    if (aOptions->flags & wr::FontInstanceFlags::SYNTHETIC_BOLD) {
      mApplySyntheticBold = true;
    }
    if (aOptions->flags & wr::FontInstanceFlags::EMBEDDED_BITMAPS) {
      mHasColorGlyphs = true;
    }
    if (aOptions->bg_color.a != 0) {
      mFontSmoothingBackgroundColor =
          DeviceColor::FromU8(aOptions->bg_color.r, aOptions->bg_color.g,
                              aOptions->bg_color.b, aOptions->bg_color.a);
    }
  }
}

static CFDictionaryRef CreateVariationDictionaryOrNull(
    CGFontRef aCGFont, CFArrayRef& aCGAxesCache, CFArrayRef& aCTAxesCache,
    uint32_t aVariationCount, const FontVariation* aVariations) {
  if (!aCGAxesCache) {
    aCGAxesCache = CGFontCopyVariationAxes(aCGFont);
    if (!aCGAxesCache) {
      return nullptr;
    }
  }
  if (!aCTAxesCache) {
    AutoRelease<CTFontRef> ctFont(
        CTFontCreateWithGraphicsFont(aCGFont, 0, nullptr, nullptr));
    aCTAxesCache = CTFontCopyVariationAxes(ctFont);
    if (!aCTAxesCache) {
      return nullptr;
    }
  }

  CFIndex axisCount = CFArrayGetCount(aCTAxesCache);
  if (CFArrayGetCount(aCGAxesCache) != axisCount) {
    return nullptr;
  }

  AutoRelease<CFMutableDictionaryRef> dict(CFDictionaryCreateMutable(
      kCFAllocatorDefault, axisCount, &kCFTypeDictionaryKeyCallBacks,
      &kCFTypeDictionaryValueCallBacks));

  // Number of variation settings passed in the aVariations parameter.
  // This will typically be a very low value, so we just linear-search them.
  bool allDefaultValues = true;

  for (CFIndex i = 0; i < axisCount; ++i) {
    // We sanity-check the axis info found in the CTFont, and bail out
    // (returning null) if it doesn't have the expected types.
    CFTypeRef axisInfo = CFArrayGetValueAtIndex(aCTAxesCache, i);
    if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) {
      return nullptr;
    }
    CFDictionaryRef axis = static_cast<CFDictionaryRef>(axisInfo);

    CFTypeRef axisTag =
        CFDictionaryGetValue(axis, kCTFontVariationAxisIdentifierKey);
    if (!axisTag || CFGetTypeID(axisTag) != CFNumberGetTypeID()) {
      return nullptr;
    }
    int64_t tagLong;
    if (!CFNumberGetValue(static_cast<CFNumberRef>(axisTag),
                          kCFNumberSInt64Type, &tagLong)) {
      return nullptr;
    }

    axisInfo = CFArrayGetValueAtIndex(aCGAxesCache, i);
    if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) {
      return nullptr;
    }
    CFTypeRef axisName = CFDictionaryGetValue(
        static_cast<CFDictionaryRef>(axisInfo), kCGFontVariationAxisName);
    if (!axisName || CFGetTypeID(axisName) != CFStringGetTypeID()) {
      return nullptr;
    }

    // Clamp axis values to the supported range.
    CFTypeRef min =
        CFDictionaryGetValue(axis, kCTFontVariationAxisMinimumValueKey);
    CFTypeRef max =
        CFDictionaryGetValue(axis, kCTFontVariationAxisMaximumValueKey);
    CFTypeRef def =
        CFDictionaryGetValue(axis, kCTFontVariationAxisDefaultValueKey);
    if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || !max ||
        CFGetTypeID(max) != CFNumberGetTypeID() || !def ||
        CFGetTypeID(def) != CFNumberGetTypeID()) {
      return nullptr;
    }
    double minDouble;
    double maxDouble;
    double defDouble;
    if (!CFNumberGetValue(static_cast<CFNumberRef>(min), kCFNumberDoubleType,
                          &minDouble) ||
        !CFNumberGetValue(static_cast<CFNumberRef>(max), kCFNumberDoubleType,
                          &maxDouble) ||
        !CFNumberGetValue(static_cast<CFNumberRef>(def), kCFNumberDoubleType,
                          &defDouble)) {
      return nullptr;
    }

    double value = defDouble;
    for (uint32_t j = 0; j < aVariationCount; ++j) {
      if (aVariations[j].mTag == tagLong) {
        value = std::min(std::max<double>(aVariations[j].mValue, minDouble),
                         maxDouble);
        if (value != defDouble) {
          allDefaultValues = false;
        }
        break;
      }
    }
    AutoRelease<CFNumberRef> valueNumber(
        CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value));
    CFDictionaryAddValue(dict, axisName, valueNumber);
  }

  if (allDefaultValues) {
    // We didn't actually set any non-default values, so throw away the
    // variations dictionary and just use the default rendering.
    return nullptr;
  }

  return dict.forget();
}

static CFDictionaryRef CreateVariationTagDictionaryOrNull(
    CTFontRef aCTFont, uint32_t aVariationCount,
    const FontVariation* aVariations) {
  AutoRelease<CFArrayRef> axes(CTFontCopyVariationAxes(aCTFont));
  CFIndex axisCount = CFArrayGetCount(axes);

  AutoRelease<CFMutableDictionaryRef> dict(CFDictionaryCreateMutable(
      kCFAllocatorDefault, axisCount, &kCFTypeDictionaryKeyCallBacks,
      &kCFTypeDictionaryValueCallBacks));

  // Number of variation settings passed in the aVariations parameter.
  // This will typically be a very low value, so we just linear-search them.
  bool allDefaultValues = true;

  for (CFIndex i = 0; i < axisCount; ++i) {
    // We sanity-check the axis info found in the CTFont, and bail out
    // (returning null) if it doesn't have the expected types.
    CFTypeRef axisInfo = CFArrayGetValueAtIndex(axes, i);
    if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) {
      return nullptr;
    }
    CFDictionaryRef axis = static_cast<CFDictionaryRef>(axisInfo);

    CFTypeRef axisTag =
        CFDictionaryGetValue(axis, kCTFontVariationAxisIdentifierKey);
    if (!axisTag || CFGetTypeID(axisTag) != CFNumberGetTypeID()) {
      return nullptr;
    }
    int64_t tagLong;
    if (!CFNumberGetValue(static_cast<CFNumberRef>(axisTag),
                          kCFNumberSInt64Type, &tagLong)) {
      return nullptr;
    }

    // Clamp axis values to the supported range.
    CFTypeRef min =
        CFDictionaryGetValue(axis, kCTFontVariationAxisMinimumValueKey);
    CFTypeRef max =
        CFDictionaryGetValue(axis, kCTFontVariationAxisMaximumValueKey);
    CFTypeRef def =
        CFDictionaryGetValue(axis, kCTFontVariationAxisDefaultValueKey);
    if (!min || CFGetTypeID(min) != CFNumberGetTypeID() || !max ||
        CFGetTypeID(max) != CFNumberGetTypeID() || !def ||
        CFGetTypeID(def) != CFNumberGetTypeID()) {
      return nullptr;
    }
    double minDouble;
    double maxDouble;
    double defDouble;
    if (!CFNumberGetValue(static_cast<CFNumberRef>(min), kCFNumberDoubleType,
                          &minDouble) ||
        !CFNumberGetValue(static_cast<CFNumberRef>(max), kCFNumberDoubleType,
                          &maxDouble) ||
        !CFNumberGetValue(static_cast<CFNumberRef>(def), kCFNumberDoubleType,
                          &defDouble)) {
      return nullptr;
    }

    double value = defDouble;
    for (uint32_t j = 0; j < aVariationCount; ++j) {
      if (aVariations[j].mTag == tagLong) {
        value = std::min(std::max<double>(aVariations[j].mValue, minDouble),
                         maxDouble);
        if (value != defDouble) {
          allDefaultValues = false;
        }
        break;
      }
    }
    AutoRelease<CFNumberRef> valueNumber(
        CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value));
    CFDictionaryAddValue(dict, axisTag, valueNumber);
  }

  if (allDefaultValues) {
    // We didn't actually set any non-default values, so throw away the
    // variations dictionary and just use the default rendering.
    return nullptr;
  }

  return dict.forget();
}

/* static */
CGFontRef UnscaledFontMac::CreateCGFontWithVariations(
    CGFontRef aFont, CFArrayRef& aCGAxesCache, CFArrayRef& aCTAxesCache,
    uint32_t aVariationCount, const FontVariation* aVariations) {
  if (!aVariationCount) {
    return nullptr;
  }
  MOZ_ASSERT(aVariations);

  AutoRelease<CFDictionaryRef> varDict(CreateVariationDictionaryOrNull(
      aFont, aCGAxesCache, aCTAxesCache, aVariationCount, aVariations));
  if (!varDict) {
    return nullptr;
  }

  return CGFontCreateCopyWithVariations(aFont, varDict);
}

already_AddRefed<ScaledFont> UnscaledFontMac::CreateScaledFont(
    Float aGlyphSize, const uint8_t* aInstanceData,
    uint32_t aInstanceDataLength, const FontVariation* aVariations,
    uint32_t aNumVariations)

{
  if (aInstanceDataLength < sizeof(ScaledFontMac::InstanceData)) {
    gfxWarning() << "Mac scaled font instance data is truncated.";
    return nullptr;
  }
  const ScaledFontMac::InstanceData& instanceData =
      *reinterpret_cast<const ScaledFontMac::InstanceData*>(aInstanceData);
  RefPtr<ScaledFontMac> scaledFont;
  if (mFontDesc) {
    AutoRelease<CTFontRef> font(
        CTFontCreateWithFontDescriptor(mFontDesc, aGlyphSize, nullptr));
    if (aNumVariations > 0) {
      AutoRelease<CFDictionaryRef> varDict(CreateVariationTagDictionaryOrNull(
          font, aNumVariations, aVariations));
      if (varDict) {
        CFDictionaryRef varAttr = CFDictionaryCreate(
            nullptr, (const void**)&kCTFontVariationAttribute,
            (const void**)&varDict, 1, &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks);
        AutoRelease<CTFontDescriptorRef> fontDesc(
            CTFontDescriptorCreateCopyWithAttributes(mFontDesc, varAttr));
        if (!fontDesc) {
          return nullptr;
        }
        font = CTFontCreateWithFontDescriptor(fontDesc, aGlyphSize, nullptr);
      }
    }
    scaledFont = new ScaledFontMac(
        font, this, instanceData.mFontSmoothingBackgroundColor,
        instanceData.mUseFontSmoothing, instanceData.mApplySyntheticBold,
        instanceData.mHasColorGlyphs);
  } else {
    CGFontRef fontRef = mFont;
    if (aNumVariations > 0) {
      CGFontRef varFont = CreateCGFontWithVariations(
          mFont, mCGAxesCache, mCTAxesCache, aNumVariations, aVariations);
      if (varFont) {
        fontRef = varFont;
      }
    }

    scaledFont = new ScaledFontMac(fontRef, this, aGlyphSize, fontRef != mFont,
                                   instanceData.mFontSmoothingBackgroundColor,
                                   instanceData.mUseFontSmoothing,
                                   instanceData.mApplySyntheticBold,
                                   instanceData.mHasColorGlyphs);
  }
  return scaledFont.forget();
}

already_AddRefed<ScaledFont> UnscaledFontMac::CreateScaledFontFromWRFont(
    Float aGlyphSize, const wr::FontInstanceOptions* aOptions,
    const wr::FontInstancePlatformOptions* aPlatformOptions,
    const FontVariation* aVariations, uint32_t aNumVariations) {
  ScaledFontMac::InstanceData instanceData(aOptions, aPlatformOptions);
  return CreateScaledFont(aGlyphSize, reinterpret_cast<uint8_t*>(&instanceData),
                          sizeof(instanceData), aVariations, aNumVariations);
}

cairo_font_face_t* ScaledFontMac::CreateCairoFontFace(
    cairo_font_options_t* aFontOptions) {
  MOZ_ASSERT(mFont);
  return cairo_quartz_font_face_create_for_cgfont(mFont);
}

already_AddRefed<UnscaledFont> UnscaledFontMac::CreateFromFontDescriptor(
    const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) {
  if (aDataLength == 0) {
    gfxWarning() << "Mac font descriptor is truncated.";
    return nullptr;
  }
  CFStringRef name =
      CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)aData,
                              aDataLength, kCFStringEncodingUTF8, false);
  if (!name) {
    return nullptr;
  }
  CGFontRef font = CGFontCreateWithFontName(name);
  CFRelease(name);
  if (!font) {
    return nullptr;
  }
  RefPtr<UnscaledFont> unscaledFont = new UnscaledFontMac(font);
  CFRelease(font);
  return unscaledFont.forget();
}

}  // namespace gfx
}  // namespace mozilla