Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / mac / GlyphPageTreeNodeMac.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
16  *
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.
27  */
28
29 #include "config.h"
30 #include "platform/fonts/GlyphPageTreeNode.h"
31
32 #include <ApplicationServices/ApplicationServices.h>
33 #include "platform/fonts/Character.h"
34 #include "platform/fonts/Font.h"
35 #include "platform/fonts/SimpleFontData.h"
36
37 // Forward declare Mac SPIs.
38 // Request for public API: rdar://13787589
39 extern "C" {
40 void CGFontGetGlyphsForUnichars(CGFontRef font, const UniChar chars[], CGGlyph glyphs[], size_t length);
41 }
42
43 namespace WebCore {
44
45 static bool shouldUseCoreText(UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
46 {
47     if (fontData->platformData().isCompositeFontReference())
48         return true;
49
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]));
56         return false;
57     }
58
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]))
63                 return true;
64         }
65     }
66
67     return false;
68 }
69
70 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
71 {
72     bool haveGlyphs = false;
73
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) {
78             if (!glyphs[i])
79                 setGlyphDataForIndex(offset + i, 0, 0);
80             else {
81                 setGlyphDataForIndex(offset + i, glyphs[i], fontData);
82                 haveGlyphs = true;
83             }
84         }
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);
93             else {
94                 setGlyphDataForIndex(offset + i, glyphs[i * glyphStep], fontData);
95                 haveGlyphs = true;
96             }
97         }
98     } else {
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()));
103
104         CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
105         CFIndex runCount = CFArrayGetCount(runArray);
106
107         // Initialize glyph entries
108         for (unsigned index = 0; index < length; ++index)
109             setGlyphDataForIndex(offset + index, 0, 0);
110
111         Vector<CGGlyph, 512> glyphVector;
112         Vector<CFIndex, 512> indexVector;
113         bool done = false;
114
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));
118
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());
124
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);
134                 if (!glyphs) {
135                     glyphVector.resize(glyphCount);
136                     CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data());
137                     glyphs = glyphVector.data();
138                 }
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();
144                 }
145
146                 if (gotBaseFont) {
147                     for (CFIndex i = 0; i < glyphCount; ++i) {
148                         if (stringIndices[i] >= static_cast<CFIndex>(length)) {
149                             done = true;
150                             break;
151                         }
152                         if (glyphs[i]) {
153                             setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], fontData);
154                             haveGlyphs = true;
155                         }
156                     }
157                 } else {
158                     const SimpleFontData* runSimple = fontData->getCompositeFontReferenceFontData((NSFont *)runFont);
159                     if (runSimple) {
160                         for (CFIndex i = 0; i < glyphCount; ++i) {
161                             if (stringIndices[i] >= static_cast<CFIndex>(length)) {
162                                 done = true;
163                                 break;
164                             }
165                             if (glyphs[i]) {
166                                 setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], runSimple);
167                                 haveGlyphs = true;
168                             }
169                         }
170                     }
171                 }
172             }
173         }
174     }
175
176     return haveGlyphs;
177 }
178
179 } // namespace WebCore