Bug 1728955: part 6) Log result of Windows' `OleSetClipboardResult`. r=masayuki
[gecko.git] / gfx / thebes / gfxCoreTextShaper.cpp
blob0e24337bf47ed043ee35173aa744cf0093e8b3dc
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/ArrayUtils.h"
7 #include "gfxCoreTextShaper.h"
8 #include "gfxMacFont.h"
9 #include "gfxFontUtils.h"
10 #include "gfxTextRun.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/UniquePtrExtensions.h"
14 #include <algorithm>
16 #include <dlfcn.h>
18 using namespace mozilla;
20 // standard font descriptors that we construct the first time they're needed
21 CTFontDescriptorRef gfxCoreTextShaper::sFeaturesDescriptor[kMaxFontInstances];
23 // Helper to create a CFDictionary with the right attributes for shaping our
24 // text, including imposing the given directionality.
25 CFDictionaryRef gfxCoreTextShaper::CreateAttrDict(bool aRightToLeft) {
26 // Because we always shape unidirectional runs, and may have applied
27 // directional overrides, we want to force a direction rather than
28 // allowing CoreText to do its own unicode-based bidi processing.
29 SInt16 dirOverride = kCTWritingDirectionOverride |
30 (aRightToLeft ? kCTWritingDirectionRightToLeft
31 : kCTWritingDirectionLeftToRight);
32 CFNumberRef dirNumber =
33 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &dirOverride);
34 CFArrayRef dirArray = ::CFArrayCreate(
35 kCFAllocatorDefault, (const void**)&dirNumber, 1, &kCFTypeArrayCallBacks);
36 ::CFRelease(dirNumber);
37 CFTypeRef attrs[] = {kCTFontAttributeName, kCTWritingDirectionAttributeName};
38 CFTypeRef values[] = {mCTFont[0], dirArray};
39 CFDictionaryRef attrDict = ::CFDictionaryCreate(
40 kCFAllocatorDefault, attrs, values, ArrayLength(attrs),
41 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
42 ::CFRelease(dirArray);
43 return attrDict;
46 gfxCoreTextShaper::gfxCoreTextShaper(gfxMacFont* aFont)
47 : gfxFontShaper(aFont),
48 mAttributesDictLTR(nullptr),
49 mAttributesDictRTL(nullptr) {
50 for (size_t i = 0; i < kMaxFontInstances; i++) {
51 mCTFont[i] = nullptr;
53 // Create our default CTFontRef
54 mCTFont[0] = CreateCTFontWithFeatures(
55 aFont->GetAdjustedSize(), GetFeaturesDescriptor(kDefaultFeatures));
58 gfxCoreTextShaper::~gfxCoreTextShaper() {
59 if (mAttributesDictLTR) {
60 ::CFRelease(mAttributesDictLTR);
62 if (mAttributesDictRTL) {
63 ::CFRelease(mAttributesDictRTL);
65 for (size_t i = 0; i < kMaxFontInstances; i++) {
66 if (mCTFont[i]) {
67 ::CFRelease(mCTFont[i]);
72 static bool IsBuggyIndicScript(unicode::Script aScript) {
73 return aScript == unicode::Script::BENGALI ||
74 aScript == unicode::Script::KANNADA ||
75 aScript == unicode::Script::ORIYA || aScript == unicode::Script::KHMER;
78 bool gfxCoreTextShaper::ShapeText(DrawTarget* aDrawTarget,
79 const char16_t* aText, uint32_t aOffset,
80 uint32_t aLength, Script aScript,
81 nsAtom* aLanguage, bool aVertical,
82 RoundingFlags aRounding,
83 gfxShapedText* aShapedText) {
84 // Create a CFAttributedString with text and style info, so we can use
85 // CoreText to lay it out.
86 bool isRightToLeft = aShapedText->IsRightToLeft();
87 const UniChar* text = reinterpret_cast<const UniChar*>(aText);
89 CFStringRef stringObj = ::CFStringCreateWithCharactersNoCopy(
90 kCFAllocatorDefault, text, aLength, kCFAllocatorNull);
92 // Figure out whether we should try to set the AAT small-caps feature:
93 // examine OpenType tags for the requested style, and see if 'smcp' is
94 // among them.
95 const gfxFontStyle* style = mFont->GetStyle();
96 gfxFontEntry* entry = mFont->GetFontEntry();
97 auto handleFeatureTag = [](const uint32_t& aTag, uint32_t& aValue,
98 void* aUserArg) -> void {
99 if (aTag == HB_TAG('s', 'm', 'c', 'p') && aValue) {
100 *static_cast<bool*>(aUserArg) = true;
103 bool addSmallCaps = false;
104 MergeFontFeatures(style, entry->mFeatureSettings, false, entry->FamilyName(),
105 false, handleFeatureTag, &addSmallCaps);
107 // Get an attributes dictionary suitable for shaping text in the
108 // current direction, creating it if necessary.
109 CFDictionaryRef attrObj =
110 isRightToLeft ? mAttributesDictRTL : mAttributesDictLTR;
111 if (!attrObj) {
112 attrObj = CreateAttrDict(isRightToLeft);
113 (isRightToLeft ? mAttributesDictRTL : mAttributesDictLTR) = attrObj;
116 FeatureFlags featureFlags = kDefaultFeatures;
117 if (IsBuggyIndicScript(aScript)) {
118 // To work around buggy Indic AAT fonts shipped with OS X,
119 // we re-enable the Line Initial Smart Swashes feature that is needed
120 // for "split vowels" to work in at least Bengali and Kannada fonts.
121 // Affected fonts include Bangla MN, Bangla Sangam MN, Kannada MN,
122 // Kannada Sangam MN. See bugs 686225, 728557, 953231, 1145515.
123 // Also applies to Oriya and Khmer, see bug 1370927 and bug 1403166.
124 featureFlags |= kIndicFeatures;
126 if (aShapedText->DisableLigatures()) {
127 // For letterspacing (or maybe other situations) we need to make
128 // a copy of the CTFont with the ligature feature disabled.
129 featureFlags |= kDisableLigatures;
131 if (addSmallCaps) {
132 featureFlags |= kAddSmallCaps;
135 // For the disabled-ligature, buggy-indic-font or small-caps case, replace
136 // the default CTFont in the attribute dictionary with a tweaked version.
137 CFMutableDictionaryRef mutableAttr = nullptr;
138 if (featureFlags != 0) {
139 if (!mCTFont[featureFlags]) {
140 mCTFont[featureFlags] = CreateCTFontWithFeatures(
141 mFont->GetAdjustedSize(), GetFeaturesDescriptor(featureFlags));
143 mutableAttr =
144 ::CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attrObj);
145 ::CFDictionaryReplaceValue(mutableAttr, kCTFontAttributeName,
146 mCTFont[featureFlags]);
147 attrObj = mutableAttr;
150 // Now we can create an attributed string
151 CFAttributedStringRef attrStringObj =
152 ::CFAttributedStringCreate(kCFAllocatorDefault, stringObj, attrObj);
153 ::CFRelease(stringObj);
155 // Create the CoreText line from our string, then we're done with it
156 CTLineRef line = ::CTLineCreateWithAttributedString(attrStringObj);
157 ::CFRelease(attrStringObj);
159 // and finally retrieve the glyph data and store into the gfxTextRun
160 CFArrayRef glyphRuns = ::CTLineGetGlyphRuns(line);
161 uint32_t numRuns = ::CFArrayGetCount(glyphRuns);
163 // Iterate through the glyph runs.
164 bool success = true;
165 for (uint32_t runIndex = 0; runIndex < numRuns; runIndex++) {
166 CTRunRef aCTRun = (CTRunRef)::CFArrayGetValueAtIndex(glyphRuns, runIndex);
167 CFRange range = ::CTRunGetStringRange(aCTRun);
168 CFDictionaryRef runAttr = ::CTRunGetAttributes(aCTRun);
169 if (runAttr != attrObj) {
170 // If Core Text manufactured a new dictionary, this may indicate
171 // unexpected font substitution. In that case, we fail (and fall
172 // back to harfbuzz shaping)...
173 const void* font1 = ::CFDictionaryGetValue(attrObj, kCTFontAttributeName);
174 const void* font2 = ::CFDictionaryGetValue(runAttr, kCTFontAttributeName);
175 if (font1 != font2) {
176 // ...except that if the fallback was only for a variation
177 // selector or join control that is otherwise unsupported,
178 // we just ignore it.
179 if (range.length == 1) {
180 char16_t ch = aText[range.location];
181 if (gfxFontUtils::IsJoinControl(ch) ||
182 gfxFontUtils::IsVarSelector(ch)) {
183 continue;
186 NS_WARNING("unexpected font fallback in Core Text");
187 success = false;
188 break;
191 if (SetGlyphsFromRun(aShapedText, aOffset, aLength, aCTRun) != NS_OK) {
192 success = false;
193 break;
197 if (mutableAttr) {
198 ::CFRelease(mutableAttr);
200 ::CFRelease(line);
202 return success;
205 #define SMALL_GLYPH_RUN \
206 128 // preallocated size of our auto arrays for per-glyph data;
207 // some testing indicates that 90%+ of glyph runs will fit
208 // without requiring a separate allocation
210 nsresult gfxCoreTextShaper::SetGlyphsFromRun(gfxShapedText* aShapedText,
211 uint32_t aOffset, uint32_t aLength,
212 CTRunRef aCTRun) {
213 typedef gfxShapedText::CompressedGlyph CompressedGlyph;
215 int32_t direction = aShapedText->IsRightToLeft() ? -1 : 1;
217 int32_t numGlyphs = ::CTRunGetGlyphCount(aCTRun);
218 if (numGlyphs == 0) {
219 return NS_OK;
222 int32_t wordLength = aLength;
224 // character offsets get really confusing here, as we have to keep track of
225 // (a) the text in the actual textRun we're constructing
226 // (c) the string that was handed to CoreText, which contains the text of
227 // the font run
228 // (d) the CTRun currently being processed, which may be a sub-run of the
229 // CoreText line
231 // get the source string range within the CTLine's text
232 CFRange stringRange = ::CTRunGetStringRange(aCTRun);
233 // skip the run if it is entirely outside the actual range of the font run
234 if (stringRange.location + stringRange.length <= 0 ||
235 stringRange.location >= wordLength) {
236 return NS_OK;
239 // retrieve the laid-out glyph data from the CTRun
240 UniquePtr<CGGlyph[]> glyphsArray;
241 UniquePtr<CGPoint[]> positionsArray;
242 UniquePtr<CFIndex[]> glyphToCharArray;
243 const CGGlyph* glyphs = nullptr;
244 const CGPoint* positions = nullptr;
245 const CFIndex* glyphToChar = nullptr;
247 // Testing indicates that CTRunGetGlyphsPtr (almost?) always succeeds,
248 // and so allocating a new array and copying data with CTRunGetGlyphs
249 // will be extremely rare.
250 // If this were not the case, we could use an AutoTArray<> to
251 // try and avoid the heap allocation for small runs.
252 // It's possible that some future change to CoreText will mean that
253 // CTRunGetGlyphsPtr fails more often; if this happens, AutoTArray<>
254 // may become an attractive option.
255 glyphs = ::CTRunGetGlyphsPtr(aCTRun);
256 if (!glyphs) {
257 glyphsArray = MakeUniqueFallible<CGGlyph[]>(numGlyphs);
258 if (!glyphsArray) {
259 return NS_ERROR_OUT_OF_MEMORY;
261 ::CTRunGetGlyphs(aCTRun, ::CFRangeMake(0, 0), glyphsArray.get());
262 glyphs = glyphsArray.get();
265 positions = ::CTRunGetPositionsPtr(aCTRun);
266 if (!positions) {
267 positionsArray = MakeUniqueFallible<CGPoint[]>(numGlyphs);
268 if (!positionsArray) {
269 return NS_ERROR_OUT_OF_MEMORY;
271 ::CTRunGetPositions(aCTRun, ::CFRangeMake(0, 0), positionsArray.get());
272 positions = positionsArray.get();
275 // Remember that the glyphToChar indices relate to the CoreText line,
276 // not to the beginning of the textRun, the font run,
277 // or the stringRange of the glyph run
278 glyphToChar = ::CTRunGetStringIndicesPtr(aCTRun);
279 if (!glyphToChar) {
280 glyphToCharArray = MakeUniqueFallible<CFIndex[]>(numGlyphs);
281 if (!glyphToCharArray) {
282 return NS_ERROR_OUT_OF_MEMORY;
284 ::CTRunGetStringIndices(aCTRun, ::CFRangeMake(0, 0),
285 glyphToCharArray.get());
286 glyphToChar = glyphToCharArray.get();
289 double runWidth = ::CTRunGetTypographicBounds(aCTRun, ::CFRangeMake(0, 0),
290 nullptr, nullptr, nullptr);
292 AutoTArray<gfxShapedText::DetailedGlyph, 1> detailedGlyphs;
293 CompressedGlyph* charGlyphs = aShapedText->GetCharacterGlyphs() + aOffset;
295 // CoreText gives us the glyphindex-to-charindex mapping, which relates each
296 // glyph to a source text character; we also need the charindex-to-glyphindex
297 // mapping to find the glyph for a given char. Note that some chars may not
298 // map to any glyph (ligature continuations), and some may map to several
299 // glyphs (eg Indic split vowels). We set the glyph index to NO_GLYPH for
300 // chars that have no associated glyph, and we record the last glyph index for
301 // cases where the char maps to several glyphs, so that our clumping will
302 // include all the glyph fragments for the character.
304 // The charToGlyph array is indexed by char position within the stringRange of
305 // the glyph run.
307 static const int32_t NO_GLYPH = -1;
308 AutoTArray<int32_t, SMALL_GLYPH_RUN> charToGlyphArray;
309 if (!charToGlyphArray.SetLength(stringRange.length, fallible)) {
310 return NS_ERROR_OUT_OF_MEMORY;
312 int32_t* charToGlyph = charToGlyphArray.Elements();
313 for (int32_t offset = 0; offset < stringRange.length; ++offset) {
314 charToGlyph[offset] = NO_GLYPH;
316 for (int32_t i = 0; i < numGlyphs; ++i) {
317 int32_t loc = glyphToChar[i] - stringRange.location;
318 if (loc >= 0 && loc < stringRange.length) {
319 charToGlyph[loc] = i;
323 // Find character and glyph clumps that correspond, allowing for ligatures,
324 // indic reordering, split glyphs, etc.
326 // The idea is that we'll find a character sequence starting at the first char
327 // of stringRange, and extend it until it includes the character associated
328 // with the first glyph; we also extend it as long as there are "holes" in the
329 // range of glyphs. So we will eventually have a contiguous sequence of
330 // characters, starting at the beginning of the range, that map to a
331 // contiguous sequence of glyphs, starting at the beginning of the glyph
332 // array. That's a clump; then we update the starting positions and repeat.
334 // NB: In the case of RTL layouts, we iterate over the stringRange in reverse.
337 // This may find characters that fall outside the range 0:wordLength,
338 // so we won't necessarily use everything we find here.
340 bool isRightToLeft = aShapedText->IsRightToLeft();
341 int32_t glyphStart =
342 0; // looking for a clump that starts at this glyph index
343 int32_t charStart =
344 isRightToLeft
345 ? stringRange.length - 1
346 : 0; // and this char index (in the stringRange of the glyph run)
348 while (glyphStart <
349 numGlyphs) { // keep finding groups until all glyphs are accounted for
350 bool inOrder = true;
351 int32_t charEnd = glyphToChar[glyphStart] - stringRange.location;
352 NS_WARNING_ASSERTION(charEnd >= 0 && charEnd < stringRange.length,
353 "glyph-to-char mapping points outside string range");
354 // clamp charEnd to the valid range of the string
355 charEnd = std::max(charEnd, 0);
356 charEnd = std::min(charEnd, int32_t(stringRange.length));
358 int32_t glyphEnd = glyphStart;
359 int32_t charLimit = isRightToLeft ? -1 : stringRange.length;
360 do {
361 // This is normally executed once for each iteration of the outer loop,
362 // but in unusual cases where the character/glyph association is complex,
363 // the initial character range might correspond to a non-contiguous
364 // glyph range with "holes" in it. If so, we will repeat this loop to
365 // extend the character range until we have a contiguous glyph sequence.
366 NS_ASSERTION((direction > 0 && charEnd < charLimit) ||
367 (direction < 0 && charEnd > charLimit),
368 "no characters left in range?");
369 charEnd += direction;
370 while (charEnd != charLimit && charToGlyph[charEnd] == NO_GLYPH) {
371 charEnd += direction;
374 // find the maximum glyph index covered by the clump so far
375 if (isRightToLeft) {
376 for (int32_t i = charStart; i > charEnd; --i) {
377 if (charToGlyph[i] != NO_GLYPH) {
378 // update extent of glyph range
379 glyphEnd = std::max(glyphEnd, charToGlyph[i] + 1);
382 } else {
383 for (int32_t i = charStart; i < charEnd; ++i) {
384 if (charToGlyph[i] != NO_GLYPH) {
385 // update extent of glyph range
386 glyphEnd = std::max(glyphEnd, charToGlyph[i] + 1);
391 if (glyphEnd == glyphStart + 1) {
392 // for the common case of a single-glyph clump, we can skip the
393 // following checks
394 break;
397 if (glyphEnd == glyphStart) {
398 // no glyphs, try to extend the clump
399 continue;
402 // check whether all glyphs in the range are associated with the
403 // characters in our clump; if not, we have a discontinuous range, and
404 // should extend it unless we've reached the end of the text
405 bool allGlyphsAreWithinCluster = true;
406 int32_t prevGlyphCharIndex = charStart;
407 for (int32_t i = glyphStart; i < glyphEnd; ++i) {
408 int32_t glyphCharIndex = glyphToChar[i] - stringRange.location;
409 if (isRightToLeft) {
410 if (glyphCharIndex > charStart || glyphCharIndex <= charEnd) {
411 allGlyphsAreWithinCluster = false;
412 break;
414 if (glyphCharIndex > prevGlyphCharIndex) {
415 inOrder = false;
417 prevGlyphCharIndex = glyphCharIndex;
418 } else {
419 if (glyphCharIndex < charStart || glyphCharIndex >= charEnd) {
420 allGlyphsAreWithinCluster = false;
421 break;
423 if (glyphCharIndex < prevGlyphCharIndex) {
424 inOrder = false;
426 prevGlyphCharIndex = glyphCharIndex;
429 if (allGlyphsAreWithinCluster) {
430 break;
432 } while (charEnd != charLimit);
434 NS_WARNING_ASSERTION(glyphStart < glyphEnd,
435 "character/glyph clump contains no glyphs!");
436 if (glyphStart == glyphEnd) {
437 ++glyphStart; // make progress - avoid potential infinite loop
438 charStart = charEnd;
439 continue;
442 NS_WARNING_ASSERTION(charStart != charEnd,
443 "character/glyph clump contains no characters!");
444 if (charStart == charEnd) {
445 glyphStart = glyphEnd; // this is bad - we'll discard the glyph(s),
446 // as there's nowhere to attach them
447 continue;
450 // Now charStart..charEnd is a ligature clump, corresponding to
451 // glyphStart..glyphEnd; Set baseCharIndex to the char we'll actually attach
452 // the glyphs to (1st of ligature), and endCharIndex to the limit (position
453 // beyond the last char), adjusting for the offset of the stringRange
454 // relative to the textRun.
455 int32_t baseCharIndex, endCharIndex;
456 if (isRightToLeft) {
457 while (charEnd >= 0 && charToGlyph[charEnd] == NO_GLYPH) {
458 charEnd--;
460 baseCharIndex = charEnd + stringRange.location + 1;
461 endCharIndex = charStart + stringRange.location + 1;
462 } else {
463 while (charEnd < stringRange.length && charToGlyph[charEnd] == NO_GLYPH) {
464 charEnd++;
466 baseCharIndex = charStart + stringRange.location;
467 endCharIndex = charEnd + stringRange.location;
470 // Then we check if the clump falls outside our actual string range; if so,
471 // just go to the next.
472 if (endCharIndex <= 0 || baseCharIndex >= wordLength) {
473 glyphStart = glyphEnd;
474 charStart = charEnd;
475 continue;
477 // Ensure we won't try to go beyond the valid length of the word's text
478 baseCharIndex = std::max(baseCharIndex, 0);
479 endCharIndex = std::min(endCharIndex, wordLength);
481 // Now we're ready to set the glyph info in the textRun; measure the glyph
482 // width of the first (perhaps only) glyph, to see if it is "Simple"
483 int32_t appUnitsPerDevUnit = aShapedText->GetAppUnitsPerDevUnit();
484 double toNextGlyph;
485 if (glyphStart < numGlyphs - 1) {
486 toNextGlyph = positions[glyphStart + 1].x - positions[glyphStart].x;
487 } else {
488 toNextGlyph = positions[0].x + runWidth - positions[glyphStart].x;
490 int32_t advance = int32_t(toNextGlyph * appUnitsPerDevUnit);
492 // Check if it's a simple one-to-one mapping
493 int32_t glyphsInClump = glyphEnd - glyphStart;
494 if (glyphsInClump == 1 &&
495 gfxTextRun::CompressedGlyph::IsSimpleGlyphID(glyphs[glyphStart]) &&
496 gfxTextRun::CompressedGlyph::IsSimpleAdvance(advance) &&
497 charGlyphs[baseCharIndex].IsClusterStart() &&
498 positions[glyphStart].y == 0.0) {
499 charGlyphs[baseCharIndex].SetSimpleGlyph(advance, glyphs[glyphStart]);
500 } else {
501 // collect all glyphs in a list to be assigned to the first char;
502 // there must be at least one in the clump, and we already measured its
503 // advance, hence the placement of the loop-exit test and the measurement
504 // of the next glyph
505 while (true) {
506 gfxTextRun::DetailedGlyph* details = detailedGlyphs.AppendElement();
507 details->mGlyphID = glyphs[glyphStart];
508 details->mOffset.y = -positions[glyphStart].y * appUnitsPerDevUnit;
509 details->mAdvance = advance;
510 if (++glyphStart >= glyphEnd) {
511 break;
513 if (glyphStart < numGlyphs - 1) {
514 toNextGlyph = positions[glyphStart + 1].x - positions[glyphStart].x;
515 } else {
516 toNextGlyph = positions[0].x + runWidth - positions[glyphStart].x;
518 advance = int32_t(toNextGlyph * appUnitsPerDevUnit);
521 aShapedText->SetDetailedGlyphs(aOffset + baseCharIndex,
522 detailedGlyphs.Length(),
523 detailedGlyphs.Elements());
525 detailedGlyphs.Clear();
528 // the rest of the chars in the group are ligature continuations, no
529 // associated glyphs
530 while (++baseCharIndex != endCharIndex && baseCharIndex < wordLength) {
531 CompressedGlyph& shapedTextGlyph = charGlyphs[baseCharIndex];
532 NS_ASSERTION(!shapedTextGlyph.IsSimpleGlyph(),
533 "overwriting a simple glyph");
534 shapedTextGlyph.SetComplex(inOrder && shapedTextGlyph.IsClusterStart(),
535 false);
538 glyphStart = glyphEnd;
539 charStart = charEnd;
542 return NS_OK;
545 #undef SMALL_GLYPH_RUN
547 // Construct the font attribute descriptor that we'll apply by default when
548 // creating a CTFontRef. This will turn off line-edge swashes by default,
549 // because we don't know the actual line breaks when doing glyph shaping.
551 // We also cache feature descriptors for shaping with disabled ligatures, and
552 // for buggy Indic AAT font workarounds, created on an as-needed basis.
554 #define MAX_FEATURES 5 // max used by any of our Get*Descriptor functions
556 CTFontDescriptorRef gfxCoreTextShaper::CreateFontFeaturesDescriptor(
557 const std::pair<SInt16, SInt16>* aFeatures, size_t aCount) {
558 MOZ_ASSERT(aCount <= MAX_FEATURES);
560 CFDictionaryRef featureSettings[MAX_FEATURES];
562 for (size_t i = 0; i < aCount; i++) {
563 CFNumberRef type = ::CFNumberCreate(
564 kCFAllocatorDefault, kCFNumberSInt16Type, &aFeatures[i].first);
565 CFNumberRef selector = ::CFNumberCreate(
566 kCFAllocatorDefault, kCFNumberSInt16Type, &aFeatures[i].second);
568 CFTypeRef keys[] = {kCTFontFeatureTypeIdentifierKey,
569 kCTFontFeatureSelectorIdentifierKey};
570 CFTypeRef values[] = {type, selector};
571 featureSettings[i] = ::CFDictionaryCreate(
572 kCFAllocatorDefault, (const void**)keys, (const void**)values,
573 ArrayLength(keys), &kCFTypeDictionaryKeyCallBacks,
574 &kCFTypeDictionaryValueCallBacks);
576 ::CFRelease(selector);
577 ::CFRelease(type);
580 CFArrayRef featuresArray =
581 ::CFArrayCreate(kCFAllocatorDefault, (const void**)featureSettings,
582 aCount, // not ArrayLength(featureSettings), as we
583 // may not have used all the allocated slots
584 &kCFTypeArrayCallBacks);
586 for (size_t i = 0; i < aCount; i++) {
587 ::CFRelease(featureSettings[i]);
590 const CFTypeRef attrKeys[] = {kCTFontFeatureSettingsAttribute};
591 const CFTypeRef attrValues[] = {featuresArray};
592 CFDictionaryRef attributesDict = ::CFDictionaryCreate(
593 kCFAllocatorDefault, (const void**)attrKeys, (const void**)attrValues,
594 ArrayLength(attrKeys), &kCFTypeDictionaryKeyCallBacks,
595 &kCFTypeDictionaryValueCallBacks);
596 ::CFRelease(featuresArray);
598 CTFontDescriptorRef descriptor =
599 ::CTFontDescriptorCreateWithAttributes(attributesDict);
600 ::CFRelease(attributesDict);
602 return descriptor;
605 CTFontDescriptorRef gfxCoreTextShaper::GetFeaturesDescriptor(
606 FeatureFlags aFeatureFlags) {
607 MOZ_ASSERT(aFeatureFlags < kMaxFontInstances);
608 if (!sFeaturesDescriptor[aFeatureFlags]) {
609 typedef std::pair<SInt16, SInt16> FeatT;
610 AutoTArray<FeatT, MAX_FEATURES> features;
611 features.AppendElement(
612 FeatT(kSmartSwashType, kLineFinalSwashesOffSelector));
613 if ((aFeatureFlags & kIndicFeatures) == 0) {
614 features.AppendElement(
615 FeatT(kSmartSwashType, kLineInitialSwashesOffSelector));
617 if (aFeatureFlags & kAddSmallCaps) {
618 features.AppendElement(FeatT(kLetterCaseType, kSmallCapsSelector));
619 features.AppendElement(
620 FeatT(kLowerCaseType, kLowerCaseSmallCapsSelector));
622 if (aFeatureFlags & kDisableLigatures) {
623 features.AppendElement(
624 FeatT(kLigaturesType, kCommonLigaturesOffSelector));
626 MOZ_ASSERT(features.Length() <= MAX_FEATURES);
627 sFeaturesDescriptor[aFeatureFlags] =
628 CreateFontFeaturesDescriptor(features.Elements(), features.Length());
630 return sFeaturesDescriptor[aFeatureFlags];
633 CTFontRef gfxCoreTextShaper::CreateCTFontWithFeatures(
634 CGFloat aSize, CTFontDescriptorRef aDescriptor) {
635 const gfxFontEntry* fe = mFont->GetFontEntry();
636 bool isInstalledFont = !fe->IsUserFont() || fe->IsLocalUserFont();
637 CGFontRef cgFont = static_cast<gfxMacFont*>(mFont)->GetCGFontRef();
638 return gfxMacFont::CreateCTFontFromCGFontWithVariations(
639 cgFont, aSize, isInstalledFont, aDescriptor);
642 void gfxCoreTextShaper::Shutdown() // [static]
644 for (size_t i = 0; i < kMaxFontInstances; i++) {
645 if (sFeaturesDescriptor[i] != nullptr) {
646 ::CFRelease(sFeaturesDescriptor[i]);
647 sFeaturesDescriptor[i] = nullptr;