2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * 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.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "platform/fonts/GlyphPageTreeNode.h"
32 #include <ApplicationServices/ApplicationServices.h>
33 #include "platform/fonts/Character.h"
34 #include "platform/fonts/Font.h"
35 #include "platform/fonts/SimpleFontData.h"
37 // Forward declare Mac SPIs.
38 // Request for public API: rdar://13787589
40 void CGFontGetGlyphsForUnichars(CGFontRef font, const UniChar chars[], CGGlyph glyphs[], size_t length);
45 static bool shouldUseCoreText(UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
47 if (fontData->platformData().isCompositeFontReference())
50 // CoreText doesn't have vertical glyphs of surrogate pair characters.
51 // Therefore, we should not use CoreText, but this always returns horizontal glyphs.
52 // FIXME: We should use vertical glyphs. https://code.google.com/p/chromium/issues/detail?id=340173
53 if (bufferLength >= 2 && U_IS_SURROGATE(buffer[0]) && fontData->hasVerticalGlyphs()) {
54 ASSERT(U_IS_SURROGATE_LEAD(buffer[0]));
55 ASSERT(U_IS_TRAIL(buffer[1]));
59 if (fontData->platformData().widthVariant() != RegularWidth || fontData->hasVerticalGlyphs()) {
60 // Ideographs don't have a vertical variant or width variants.
61 for (unsigned i = 0; i < bufferLength; ++i) {
62 if (!Character::isCJKIdeograph(buffer[i]))
70 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
72 bool haveGlyphs = false;
74 Vector<CGGlyph, 512> glyphs(bufferLength);
75 if (!shouldUseCoreText(buffer, bufferLength, fontData)) {
76 CGFontGetGlyphsForUnichars(fontData->platformData().cgFont(), buffer, glyphs.data(), bufferLength);
77 for (unsigned i = 0; i < length; ++i) {
79 setGlyphDataForIndex(offset + i, 0, 0);
81 setGlyphDataForIndex(offset + i, glyphs[i], fontData);
85 } else if (!fontData->platformData().isCompositeFontReference() && fontData->platformData().widthVariant() != RegularWidth
86 && CTFontGetGlyphsForCharacters(fontData->platformData().ctFont(), buffer, glyphs.data(), bufferLength)) {
87 // When buffer consists of surrogate pairs, CTFontGetGlyphsForCharacters
88 // places the glyphs at indices corresponding to the first character of each pair.
89 unsigned glyphStep = bufferLength / length;
90 for (unsigned i = 0; i < length; ++i) {
91 if (!glyphs[i * glyphStep])
92 setGlyphDataForIndex(offset + i, 0, 0);
94 setGlyphDataForIndex(offset + i, glyphs[i * glyphStep], fontData);
99 // We ask CoreText for possible vertical variant glyphs
100 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buffer, bufferLength, kCFAllocatorNull));
101 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(0, fontData->hasVerticalGlyphs() ? Vertical : Horizontal)));
102 RetainPtr<CTLineRef> line(AdoptCF, CTLineCreateWithAttributedString(attributedString.get()));
104 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
105 CFIndex runCount = CFArrayGetCount(runArray);
107 // Initialize glyph entries
108 for (unsigned index = 0; index < length; ++index)
109 setGlyphDataForIndex(offset + index, 0, 0);
111 Vector<CGGlyph, 512> glyphVector;
112 Vector<CFIndex, 512> indexVector;
115 // For the CGFont comparison in the loop, use the CGFont that Core Text assigns to the CTFont. This may
116 // be non-CFEqual to fontData->platformData().cgFont().
117 RetainPtr<CGFontRef> cgFont(AdoptCF, CTFontCopyGraphicsFont(fontData->platformData().ctFont(), 0));
119 for (CFIndex r = 0; r < runCount && !done ; ++r) {
120 // CTLine could map characters over multiple fonts using its own font fallback list.
121 // We need to pick runs that use the exact font we need, i.e., fontData->platformData().ctFont().
122 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
123 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
125 CFDictionaryRef attributes = CTRunGetAttributes(ctRun);
126 CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(attributes, kCTFontAttributeName));
127 RetainPtr<CGFontRef> runCGFont(AdoptCF, CTFontCopyGraphicsFont(runFont, 0));
128 // Use CGFont here as CFEqual for CTFont counts all attributes for font.
129 bool gotBaseFont = CFEqual(cgFont.get(), runCGFont.get());
130 if (gotBaseFont || fontData->platformData().isCompositeFontReference()) {
131 // This run uses the font we want. Extract glyphs.
132 CFIndex glyphCount = CTRunGetGlyphCount(ctRun);
133 const CGGlyph* glyphs = CTRunGetGlyphsPtr(ctRun);
135 glyphVector.resize(glyphCount);
136 CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data());
137 glyphs = glyphVector.data();
139 const CFIndex* stringIndices = CTRunGetStringIndicesPtr(ctRun);
140 if (!stringIndices) {
141 indexVector.resize(glyphCount);
142 CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), indexVector.data());
143 stringIndices = indexVector.data();
147 for (CFIndex i = 0; i < glyphCount; ++i) {
148 if (stringIndices[i] >= static_cast<CFIndex>(length)) {
153 setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], fontData);
158 const SimpleFontData* runSimple = fontData->getCompositeFontReferenceFontData((NSFont *)runFont);
160 for (CFIndex i = 0; i < glyphCount; ++i) {
161 if (stringIndices[i] >= static_cast<CFIndex>(length)) {
166 setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], runSimple);
179 } // namespace WebCore