1 /*
2 * Copyright (C) 2007, 2008, 2009 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include "config.h"
26 #include "ComplexTextController.h"
27 #include "TextRun.h"
28 #include "WebCoreSystemInterface.h"
29
30 #if USE(CORE_TEXT)
31
32 #include "Font.h"
33
34 #if defined(BUILDING_ON_LEOPARD)
35 // The following symbols are SPI in 10.5.
36 extern "C" {
37 void CTRunGetAdvances(CTRunRef run, CFRange range, CGSize buffer[]);
38 const CGSize* CTRunGetAdvancesPtr(CTRunRef run);
39 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
40 }
41 #endif
42
43 namespace WebCore {
44
ComplexTextRun(CTRunRef ctRun,const SimpleFontData * fontData,const UChar * characters,unsigned stringLocation,size_t stringLength,CFRange runRange)45 ComplexTextController::ComplexTextRun::ComplexTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
46 : m_fontData(fontData)
47 , m_characters(characters)
48 , m_stringLocation(stringLocation)
49 , m_stringLength(stringLength)
50 , m_indexEnd(runRange.location + runRange.length)
51 , m_isMonotonic(true)
52 {
53 m_glyphCount = CTRunGetGlyphCount(ctRun);
54 m_coreTextIndices = CTRunGetStringIndicesPtr(ctRun);
55 if (!m_coreTextIndices) {
56 m_coreTextIndicesVector.grow(m_glyphCount);
57 CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), m_coreTextIndicesVector.data());
58 m_coreTextIndices = m_coreTextIndicesVector.data();
59 }
60
61 m_glyphs = CTRunGetGlyphsPtr(ctRun);
62 if (!m_glyphs) {
63 m_glyphsVector.grow(m_glyphCount);
64 CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), m_glyphsVector.data());
65 m_glyphs = m_glyphsVector.data();
66 }
67
68 m_advances = CTRunGetAdvancesPtr(ctRun);
69 if (!m_advances) {
70 m_advancesVector.grow(m_glyphCount);
71 CTRunGetAdvances(ctRun, CFRangeMake(0, 0), m_advancesVector.data());
72 m_advances = m_advancesVector.data();
73 }
74 }
75
76 // Missing glyphs run constructor. Core Text will not generate a run of missing glyphs, instead falling back on
77 // glyphs from LastResort. We want to use the primary font's missing glyph in order to match the fast text code path.
createTextRunFromFontDataCoreText(bool ltr)78 void ComplexTextController::ComplexTextRun::createTextRunFromFontDataCoreText(bool ltr)
79 {
80 m_coreTextIndicesVector.reserveInitialCapacity(m_stringLength);
81 unsigned r = 0;
82 while (r < m_stringLength) {
83 m_coreTextIndicesVector.uncheckedAppend(r);
84 if (U_IS_SURROGATE(m_characters[r])) {
85 ASSERT(r + 1 < m_stringLength);
86 ASSERT(U_IS_SURROGATE_LEAD(m_characters[r]));
87 ASSERT(U_IS_TRAIL(m_characters[r + 1]));
88 r += 2;
89 } else
90 r++;
91 }
92 m_glyphCount = m_coreTextIndicesVector.size();
93 if (!ltr) {
94 for (unsigned r = 0, end = m_glyphCount - 1; r < m_glyphCount / 2; ++r, --end)
95 std::swap(m_coreTextIndicesVector[r], m_coreTextIndicesVector[end]);
96 }
97 m_coreTextIndices = m_coreTextIndicesVector.data();
98
99 // Synthesize a run of missing glyphs.
100 m_glyphsVector.fill(0, m_glyphCount);
101 m_glyphs = m_glyphsVector.data();
102 m_advancesVector.fill(CGSizeMake(m_fontData->widthForGlyph(0), 0), m_glyphCount);
103 m_advances = m_advancesVector.data();
104 }
105
106 struct ProviderInfo {
107 const UChar* cp;
108 unsigned length;
109 CFDictionaryRef attributes;
110 };
111
provideStringAndAttributes(CFIndex stringIndex,CFIndex * charCount,CFDictionaryRef * attributes,void * refCon)112 static const UniChar* provideStringAndAttributes(CFIndex stringIndex, CFIndex* charCount, CFDictionaryRef* attributes, void* refCon)
113 {
114 ProviderInfo* info = static_cast<struct ProviderInfo*>(refCon);
115 if (stringIndex < 0 || static_cast<unsigned>(stringIndex) >= info->length)
116 return 0;
117
118 *charCount = info->length - stringIndex;
119 *attributes = info->attributes;
120 return info->cp + stringIndex;
121 }
122
collectComplexTextRunsForCharactersCoreText(const UChar * cp,unsigned length,unsigned stringLocation,const SimpleFontData * fontData)123 void ComplexTextController::collectComplexTextRunsForCharactersCoreText(const UChar* cp, unsigned length, unsigned stringLocation, const SimpleFontData* fontData)
124 {
125 if (!fontData) {
126 // Create a run of missing glyphs from the primary font.
127 m_complexTextRuns.append(ComplexTextRun::create(m_font.primaryFont(), cp, stringLocation, length, m_run.ltr()));
128 return;
129 }
130
131 if (m_fallbackFonts && fontData != m_font.primaryFont())
132 m_fallbackFonts->add(fontData);
133
134 RetainPtr<CTLineRef> line;
135
136 if (!m_mayUseNaturalWritingDirection || m_run.directionalOverride()) {
137 static const void* optionKeys[] = { kCTTypesetterOptionForcedEmbeddingLevel };
138 const short ltrForcedEmbeddingLevelValue = 0;
139 const short rtlForcedEmbeddingLevelValue = 1;
140 static const void* ltrOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, <rForcedEmbeddingLevelValue) };
141 static const void* rtlOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &rtlForcedEmbeddingLevelValue) };
142 static CFDictionaryRef ltrTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, ltrOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
143 static CFDictionaryRef rtlTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, rtlOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
144
145 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
146 ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation()) };
147 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, wkCreateCTTypesetterWithUniCharProviderAndOptions(&provideStringAndAttributes, 0, &info, m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
148 #else
149 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, cp, length, kCFAllocatorNull));
150 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation())));
151 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, CTTypesetterCreateWithAttributedStringAndOptions(attributedString.get(), m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
152 #endif
153
154 line.adoptCF(CTTypesetterCreateLine(typesetter.get(), CFRangeMake(0, 0)));
155 } else {
156 ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation()) };
157
158 line.adoptCF(wkCreateCTLineWithUniCharProvider(&provideStringAndAttributes, 0, &info));
159 }
160
161 m_coreTextLines.append(line.get());
162
163 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
164
165 CFIndex runCount = CFArrayGetCount(runArray);
166
167 for (CFIndex r = 0; r < runCount; r++) {
168 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
169 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
170 CFRange runRange = CTRunGetStringRange(ctRun);
171 m_complexTextRuns.append(ComplexTextRun::create(ctRun, fontData, cp, stringLocation, length, runRange));
172 }
173 }
174
175 } // namespace WebCore
176
177 #endif // USE(CORE_TEXT)
178