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 "GlyphPageTreeNode.h"
31
32 #include "Font.h"
33 #include "SimpleFontData.h"
34 #include "WebCoreSystemInterface.h"
35 #include <ApplicationServices/ApplicationServices.h>
36
37 namespace WebCore {
38
shouldUseCoreText(UChar * buffer,unsigned bufferLength,const SimpleFontData * fontData)39 static bool shouldUseCoreText(UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
40 {
41 if (fontData->platformData().widthVariant() != RegularWidth || fontData->hasVerticalGlyphs()) {
42 // Ideographs don't have a vertical variant or width variants.
43 for (unsigned i = 0; i < bufferLength; ++i) {
44 if (!Font::isCJKIdeograph(buffer[i]))
45 return true;
46 }
47 }
48
49 return false;
50 }
51
fill(unsigned offset,unsigned length,UChar * buffer,unsigned bufferLength,const SimpleFontData * fontData)52 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
53 {
54 bool haveGlyphs = false;
55
56 if (!shouldUseCoreText(buffer, bufferLength, fontData)) {
57 Vector<CGGlyph, 512> glyphs(bufferLength);
58 wkGetGlyphsForCharacters(fontData->platformData().cgFont(), buffer, glyphs.data(), bufferLength);
59 for (unsigned i = 0; i < length; ++i) {
60 if (!glyphs[i])
61 setGlyphDataForIndex(offset + i, 0, 0);
62 else {
63 setGlyphDataForIndex(offset + i, glyphs[i], fontData);
64 haveGlyphs = true;
65 }
66 }
67 } else {
68 // We ask CoreText for possible vertical variant glyphs
69 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buffer, bufferLength, kCFAllocatorNull));
70 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(0, fontData->hasVerticalGlyphs() ? Vertical : Horizontal)));
71 RetainPtr<CTLineRef> line(AdoptCF, CTLineCreateWithAttributedString(attributedString.get()));
72
73 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
74 CFIndex runCount = CFArrayGetCount(runArray);
75
76 // Initialize glyph entries
77 for (unsigned index = 0; index < length; ++index)
78 setGlyphDataForIndex(offset + index, 0, 0);
79
80 Vector<CGGlyph, 512> glyphVector;
81 Vector<CFIndex, 512> indexVector;
82 bool done = false;
83
84 // For the CGFont comparison in the loop, use the CGFont that Core Text assigns to the CTFont. This may
85 // be non-CFEqual to fontData->platformData().cgFont().
86 RetainPtr<CGFontRef> cgFont(AdoptCF, CTFontCopyGraphicsFont(fontData->platformData().ctFont(), 0));
87
88 for (CFIndex r = 0; r < runCount && !done ; ++r) {
89 // CTLine could map characters over multiple fonts using its own font fallback list.
90 // We need to pick runs that use the exact font we need, i.e., fontData->platformData().ctFont().
91 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
92 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
93
94 CFDictionaryRef attributes = CTRunGetAttributes(ctRun);
95 CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(attributes, kCTFontAttributeName));
96 RetainPtr<CGFontRef> runCGFont(AdoptCF, CTFontCopyGraphicsFont(runFont, 0));
97 // Use CGFont here as CFEqual for CTFont counts all attributes for font.
98 if (CFEqual(cgFont.get(), runCGFont.get())) {
99 // This run uses the font we want. Extract glyphs.
100 CFIndex glyphCount = CTRunGetGlyphCount(ctRun);
101 const CGGlyph* glyphs = CTRunGetGlyphsPtr(ctRun);
102 if (!glyphs) {
103 glyphVector.resize(glyphCount);
104 CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data());
105 glyphs = glyphVector.data();
106 }
107 const CFIndex* stringIndices = CTRunGetStringIndicesPtr(ctRun);
108 if (!stringIndices) {
109 indexVector.resize(glyphCount);
110 CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), indexVector.data());
111 stringIndices = indexVector.data();
112 }
113
114 for (CFIndex i = 0; i < glyphCount; ++i) {
115 if (stringIndices[i] >= static_cast<CFIndex>(length)) {
116 done = true;
117 break;
118 }
119 if (glyphs[i]) {
120 setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], fontData);
121 haveGlyphs = true;
122 }
123 }
124 }
125 }
126 }
127
128 return haveGlyphs;
129 }
130
131 } // namespace WebCore
132