2 * Copyright (C) 2007, 2008, 2009 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
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.
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.
26 #include "ComplexTextController.h"
28 #include "WebCoreSystemInterface.h"
34 #if defined(BUILDING_ON_LEOPARD)
35 // The following symbols are SPI in 10.5.
37 void CTRunGetAdvances(CTRunRef run, CFRange range, CGSize buffer[]);
38 const CGSize* CTRunGetAdvancesPtr(CTRunRef run);
39 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
45 ComplexTextController::ComplexTextRun::ComplexTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength)
46 : m_coreTextRun(ctRun)
47 , m_fontData(fontData)
48 , m_characters(characters)
49 , m_stringLocation(stringLocation)
50 , m_stringLength(stringLength)
53 m_glyphCount = CTRunGetGlyphCount(m_coreTextRun.get());
54 m_coreTextIndices = CTRunGetStringIndicesPtr(m_coreTextRun.get());
55 if (!m_coreTextIndices) {
56 m_coreTextIndicesVector.grow(m_glyphCount);
57 CTRunGetStringIndices(m_coreTextRun.get(), CFRangeMake(0, 0), m_coreTextIndicesVector.data());
58 m_coreTextIndices = m_coreTextIndicesVector.data();
61 m_glyphs = CTRunGetGlyphsPtr(m_coreTextRun.get());
63 m_glyphsVector.grow(m_glyphCount);
64 CTRunGetGlyphs(m_coreTextRun.get(), CFRangeMake(0, 0), m_glyphsVector.data());
65 m_glyphs = m_glyphsVector.data();
68 m_advances = CTRunGetAdvancesPtr(m_coreTextRun.get());
70 m_advancesVector.grow(m_glyphCount);
71 CTRunGetAdvances(m_coreTextRun.get(), CFRangeMake(0, 0), m_advancesVector.data());
72 m_advances = m_advancesVector.data();
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.
78 void ComplexTextController::ComplexTextRun::createTextRunFromFontDataCoreText(bool ltr)
80 m_coreTextIndicesVector.reserveInitialCapacity(m_stringLength);
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]));
92 m_glyphCount = m_coreTextIndicesVector.size();
94 for (unsigned r = 0, end = m_glyphCount - 1; r < m_glyphCount / 2; ++r, --end)
95 std::swap(m_coreTextIndicesVector[r], m_coreTextIndicesVector[end]);
97 m_coreTextIndices = m_coreTextIndicesVector.data();
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();
106 struct ProviderInfo {
109 CFDictionaryRef attributes;
112 static const UniChar* provideStringAndAttributes(CFIndex stringIndex, CFIndex* charCount, CFDictionaryRef* attributes, void* refCon)
114 ProviderInfo* info = static_cast<struct ProviderInfo*>(refCon);
115 if (stringIndex < 0 || static_cast<unsigned>(stringIndex) >= info->length)
118 *charCount = info->length - stringIndex;
119 *attributes = info->attributes;
120 return info->cp + stringIndex;
123 void ComplexTextController::collectComplexTextRunsForCharactersCoreText(const UChar* cp, unsigned length, unsigned stringLocation, const SimpleFontData* 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()));
131 if (m_fallbackFonts && fontData != m_font.primaryFont())
132 m_fallbackFonts->add(fontData);
134 RetainPtr<CTLineRef> line;
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);
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()) };
147 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, wkCreateCTTypesetterWithUniCharProviderAndOptions(&provideStringAndAttributes, 0, &info, m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
149 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, cp, length, kCFAllocatorNull));
150 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(m_font.typesettingFeatures())));
151 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, CTTypesetterCreateWithAttributedStringAndOptions(attributedString.get(), m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
154 line.adoptCF(CTTypesetterCreateLine(typesetter.get(), CFRangeMake(0, 0)));
156 ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures()) };
158 line.adoptCF(wkCreateCTLineWithUniCharProvider(&provideStringAndAttributes, 0, &info));
161 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
163 CFIndex runCount = CFArrayGetCount(runArray);
165 for (CFIndex r = 0; r < runCount; r++) {
166 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
167 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
168 m_complexTextRuns.append(ComplexTextRun::create(ctRun, fontData, cp, stringLocation, length));
172 } // namespace WebCore
174 #endif // USE(CORE_TEXT)