2 * Copyright (C) 2005, 2006, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
28 #include "platform/fonts/SimpleFontData.h"
30 #include "platform/fonts/Character.h"
31 #include "platform/fonts/Font.h"
32 #include "platform/fonts/GlyphPage.h"
33 #include <ApplicationServices/ApplicationServices.h>
35 // Forward declare Mac SPIs.
36 // Request for public API: rdar://13787589
38 void CGFontGetGlyphsForUnichars(CGFontRef, const UniChar chars[], CGGlyph glyphs[], size_t length);
43 CFDictionaryRef SimpleFontData::getCFStringAttributes(TypesettingFeatures typesettingFeatures, FontOrientation orientation) const
45 unsigned key = typesettingFeatures + 1;
46 HashMap<unsigned, RetainPtr<CFDictionaryRef> >::AddResult addResult = m_CFStringAttributes.add(key, RetainPtr<CFDictionaryRef>());
47 RetainPtr<CFDictionaryRef>& attributesDictionary = addResult.storedValue->value;
48 if (!addResult.isNewEntry)
49 return attributesDictionary.get();
51 attributesDictionary.adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 4, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
52 CFMutableDictionaryRef mutableAttributes = (CFMutableDictionaryRef)attributesDictionary.get();
54 CFDictionarySetValue(mutableAttributes, kCTFontAttributeName, platformData().ctFont());
56 if (!(typesettingFeatures & Kerning)) {
58 static CFNumberRef zeroKerningValue = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &zero);
59 CFDictionarySetValue(mutableAttributes, kCTKernAttributeName, zeroKerningValue);
62 bool allowLigatures = (orientation == Horizontal && platformData().allowsLigatures()) || (typesettingFeatures & Ligatures);
63 if (!allowLigatures) {
65 static CFNumberRef essentialLigaturesValue = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &zero);
66 CFDictionarySetValue(mutableAttributes, kCTLigatureAttributeName, essentialLigaturesValue);
69 if (orientation == Vertical)
70 CFDictionarySetValue(mutableAttributes, kCTVerticalFormsAttributeName, kCFBooleanTrue);
72 return attributesDictionary.get();
75 static bool shouldUseCoreText(UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
77 if (fontData->platformData().isCompositeFontReference())
80 // CoreText doesn't have vertical glyphs of surrogate pair characters.
81 // Therefore, we should not use CoreText, but this always returns horizontal glyphs.
82 // FIXME: We should use vertical glyphs. https://code.google.com/p/chromium/issues/detail?id=340173
83 if (bufferLength >= 2 && U_IS_SURROGATE(buffer[0]) && fontData->hasVerticalGlyphs()) {
84 ASSERT(U_IS_SURROGATE_LEAD(buffer[0]));
85 ASSERT(U_IS_TRAIL(buffer[1]));
89 if (fontData->platformData().widthVariant() != RegularWidth || fontData->hasVerticalGlyphs()) {
90 // Ideographs don't have a vertical variant or width variants.
91 for (unsigned i = 0; i < bufferLength; ++i) {
92 if (!Character::isCJKIdeograph(buffer[i]))
100 bool SimpleFontData::fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const
102 bool haveGlyphs = false;
104 Vector<CGGlyph, 512> glyphs(bufferLength);
105 if (!shouldUseCoreText(buffer, bufferLength, this)) {
106 CGFontGetGlyphsForUnichars(platformData().cgFont(), buffer, glyphs.data(), bufferLength);
107 for (unsigned i = 0; i < length; ++i) {
109 pageToFill->setGlyphDataForIndex(offset + i, glyphs[i], this);
113 } else if (!platformData().isCompositeFontReference() && platformData().widthVariant() != RegularWidth
114 && CTFontGetGlyphsForCharacters(platformData().ctFont(), buffer, glyphs.data(), bufferLength)) {
115 // When buffer consists of surrogate pairs, CTFontGetGlyphsForCharacters
116 // places the glyphs at indices corresponding to the first character of each pair.
117 unsigned glyphStep = bufferLength / length;
118 for (unsigned i = 0; i < length; ++i) {
119 if (glyphs[i * glyphStep]) {
120 pageToFill->setGlyphDataForIndex(offset + i, glyphs[i * glyphStep], this);
125 // We ask CoreText for possible vertical variant glyphs
126 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buffer, bufferLength, kCFAllocatorNull));
127 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), getCFStringAttributes(0, hasVerticalGlyphs() ? Vertical : Horizontal)));
128 RetainPtr<CTLineRef> line(AdoptCF, CTLineCreateWithAttributedString(attributedString.get()));
130 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
131 CFIndex runCount = CFArrayGetCount(runArray);
133 Vector<CGGlyph, 512> glyphVector;
134 Vector<CFIndex, 512> indexVector;
137 // For the CGFont comparison in the loop, use the CGFont that Core Text assigns to the CTFont. This may
138 // be non-CFEqual to platformData().cgFont().
139 RetainPtr<CGFontRef> cgFont(AdoptCF, CTFontCopyGraphicsFont(platformData().ctFont(), 0));
141 for (CFIndex r = 0; r < runCount && !done ; ++r) {
142 // CTLine could map characters over multiple fonts using its own font fallback list.
143 // We need to pick runs that use the exact font we need, i.e., platformData().ctFont().
144 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
145 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
147 CFDictionaryRef attributes = CTRunGetAttributes(ctRun);
148 CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(attributes, kCTFontAttributeName));
149 RetainPtr<CGFontRef> runCGFont(AdoptCF, CTFontCopyGraphicsFont(runFont, 0));
150 // Use CGFont here as CFEqual for CTFont counts all attributes for font.
151 bool gotBaseFont = CFEqual(cgFont.get(), runCGFont.get());
152 if (gotBaseFont || platformData().isCompositeFontReference()) {
153 // This run uses the font we want. Extract glyphs.
154 CFIndex glyphCount = CTRunGetGlyphCount(ctRun);
155 const CGGlyph* glyphs = CTRunGetGlyphsPtr(ctRun);
157 glyphVector.resize(glyphCount);
158 CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data());
159 glyphs = glyphVector.data();
161 const CFIndex* stringIndices = CTRunGetStringIndicesPtr(ctRun);
162 if (!stringIndices) {
163 indexVector.resize(glyphCount);
164 CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), indexVector.data());
165 stringIndices = indexVector.data();
169 for (CFIndex i = 0; i < glyphCount; ++i) {
170 if (stringIndices[i] >= static_cast<CFIndex>(length)) {
175 pageToFill->setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], this);
180 const SimpleFontData* runSimple = getCompositeFontReferenceFontData((NSFont *)runFont);
182 for (CFIndex i = 0; i < glyphCount; ++i) {
183 if (stringIndices[i] >= static_cast<CFIndex>(length)) {
188 pageToFill->setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], runSimple);
201 } // namespace WebCore