OLD | NEW |
| (Empty) |
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 "core/platform/graphics/GlyphPageTreeNode.h" | |
31 | |
32 #include <ApplicationServices/ApplicationServices.h> | |
33 #include "core/platform/graphics/Font.h" | |
34 #include "core/platform/graphics/SimpleFontData.h" | |
35 | |
36 // Forward declare Mac SPIs. | |
37 // Request for public API: rdar://13787589 | |
38 extern "C" { | |
39 void CGFontGetGlyphsForUnichars(CGFontRef font, const UniChar chars[], CGGlyph g
lyphs[], size_t length); | |
40 } | |
41 | |
42 namespace WebCore { | |
43 | |
44 static bool shouldUseCoreText(UChar* buffer, unsigned bufferLength, const Simple
FontData* fontData) | |
45 { | |
46 if (fontData->platformData().isCompositeFontReference()) | |
47 return true; | |
48 if (fontData->platformData().widthVariant() != RegularWidth || fontData->has
VerticalGlyphs()) { | |
49 // Ideographs don't have a vertical variant or width variants. | |
50 for (unsigned i = 0; i < bufferLength; ++i) { | |
51 if (!Font::isCJKIdeograph(buffer[i])) | |
52 return true; | |
53 } | |
54 } | |
55 | |
56 return false; | |
57 } | |
58 | |
59 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned b
ufferLength, const SimpleFontData* fontData) | |
60 { | |
61 bool haveGlyphs = false; | |
62 | |
63 Vector<CGGlyph, 512> glyphs(bufferLength); | |
64 if (!shouldUseCoreText(buffer, bufferLength, fontData)) { | |
65 CGFontGetGlyphsForUnichars(fontData->platformData().cgFont(), buffer, gl
yphs.data(), bufferLength); | |
66 for (unsigned i = 0; i < length; ++i) { | |
67 if (!glyphs[i]) | |
68 setGlyphDataForIndex(offset + i, 0, 0); | |
69 else { | |
70 setGlyphDataForIndex(offset + i, glyphs[i], fontData); | |
71 haveGlyphs = true; | |
72 } | |
73 } | |
74 } else if (!fontData->platformData().isCompositeFontReference() && fontData-
>platformData().widthVariant() != RegularWidth | |
75 && CTFontGetGlyphsForCharacters(fontData->platformData().ctFont()
, buffer, glyphs.data(), bufferLength)) { | |
76 // When buffer consists of surrogate pairs, CTFontGetGlyphsForCharacters | |
77 // places the glyphs at indices corresponding to the first character of
each pair. | |
78 unsigned glyphStep = bufferLength / length; | |
79 for (unsigned i = 0; i < length; ++i) { | |
80 if (!glyphs[i * glyphStep]) | |
81 setGlyphDataForIndex(offset + i, 0, 0); | |
82 else { | |
83 setGlyphDataForIndex(offset + i, glyphs[i * glyphStep], fontData
); | |
84 haveGlyphs = true; | |
85 } | |
86 } | |
87 } else { | |
88 // We ask CoreText for possible vertical variant glyphs | |
89 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCop
y(kCFAllocatorDefault, buffer, bufferLength, kCFAllocatorNull)); | |
90 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedS
tringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(0
, fontData->hasVerticalGlyphs() ? Vertical : Horizontal))); | |
91 RetainPtr<CTLineRef> line(AdoptCF, CTLineCreateWithAttributedString(attr
ibutedString.get())); | |
92 | |
93 CFArrayRef runArray = CTLineGetGlyphRuns(line.get()); | |
94 CFIndex runCount = CFArrayGetCount(runArray); | |
95 | |
96 // Initialize glyph entries | |
97 for (unsigned index = 0; index < length; ++index) | |
98 setGlyphDataForIndex(offset + index, 0, 0); | |
99 | |
100 Vector<CGGlyph, 512> glyphVector; | |
101 Vector<CFIndex, 512> indexVector; | |
102 bool done = false; | |
103 | |
104 // For the CGFont comparison in the loop, use the CGFont that Core Text
assigns to the CTFont. This may | |
105 // be non-CFEqual to fontData->platformData().cgFont(). | |
106 RetainPtr<CGFontRef> cgFont(AdoptCF, CTFontCopyGraphicsFont(fontData->pl
atformData().ctFont(), 0)); | |
107 | |
108 for (CFIndex r = 0; r < runCount && !done ; ++r) { | |
109 // CTLine could map characters over multiple fonts using its own fon
t fallback list. | |
110 // We need to pick runs that use the exact font we need, i.e., fontD
ata->platformData().ctFont(). | |
111 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArr
ay, r)); | |
112 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID()); | |
113 | |
114 CFDictionaryRef attributes = CTRunGetAttributes(ctRun); | |
115 CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(attr
ibutes, kCTFontAttributeName)); | |
116 RetainPtr<CGFontRef> runCGFont(AdoptCF, CTFontCopyGraphicsFont(runFo
nt, 0)); | |
117 // Use CGFont here as CFEqual for CTFont counts all attributes for f
ont. | |
118 bool gotBaseFont = CFEqual(cgFont.get(), runCGFont.get()); | |
119 if (gotBaseFont || fontData->platformData().isCompositeFontReference
()) { | |
120 // This run uses the font we want. Extract glyphs. | |
121 CFIndex glyphCount = CTRunGetGlyphCount(ctRun); | |
122 const CGGlyph* glyphs = CTRunGetGlyphsPtr(ctRun); | |
123 if (!glyphs) { | |
124 glyphVector.resize(glyphCount); | |
125 CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data())
; | |
126 glyphs = glyphVector.data(); | |
127 } | |
128 const CFIndex* stringIndices = CTRunGetStringIndicesPtr(ctRun); | |
129 if (!stringIndices) { | |
130 indexVector.resize(glyphCount); | |
131 CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), indexVector.
data()); | |
132 stringIndices = indexVector.data(); | |
133 } | |
134 | |
135 if (gotBaseFont) { | |
136 for (CFIndex i = 0; i < glyphCount; ++i) { | |
137 if (stringIndices[i] >= static_cast<CFIndex>(length)) { | |
138 done = true; | |
139 break; | |
140 } | |
141 if (glyphs[i]) { | |
142 setGlyphDataForIndex(offset + stringIndices[i], glyp
hs[i], fontData); | |
143 haveGlyphs = true; | |
144 } | |
145 } | |
146 } else { | |
147 const SimpleFontData* runSimple = fontData->getCompositeFont
ReferenceFontData((NSFont *)runFont); | |
148 if (runSimple) { | |
149 for (CFIndex i = 0; i < glyphCount; ++i) { | |
150 if (stringIndices[i] >= static_cast<CFIndex>(length)
) { | |
151 done = true; | |
152 break; | |
153 } | |
154 if (glyphs[i]) { | |
155 setGlyphDataForIndex(offset + stringIndices[i],
glyphs[i], runSimple); | |
156 haveGlyphs = true; | |
157 } | |
158 } | |
159 } | |
160 } | |
161 } | |
162 } | |
163 } | |
164 | |
165 return haveGlyphs; | |
166 } | |
167 | |
168 } // namespace WebCore | |
OLD | NEW |