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"
27 #include "WebCoreSystemInterface.h"
33 #if defined(BUILDING_ON_LEOPARD)
34 // The following symbols are SPI in 10.5.
36 void CTRunGetAdvances(CTRunRef run, CFRange range, CGSize buffer[]);
37 const CGSize* CTRunGetAdvancesPtr(CTRunRef run);
38 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
44 ComplexTextController::ComplexTextRun::ComplexTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength)
45 : m_coreTextRun(ctRun)
46 , m_fontData(fontData)
47 , m_characters(characters)
48 , m_stringLocation(stringLocation)
49 , m_stringLength(stringLength)
52 m_glyphCount = CTRunGetGlyphCount(m_coreTextRun.get());
53 m_coreTextIndices = CTRunGetStringIndicesPtr(m_coreTextRun.get());
54 if (!m_coreTextIndices) {
55 m_coreTextIndicesVector.grow(m_glyphCount);
56 CTRunGetStringIndices(m_coreTextRun.get(), CFRangeMake(0, 0), m_coreTextIndicesVector.data());
57 m_coreTextIndices = m_coreTextIndicesVector.data();
60 m_glyphs = CTRunGetGlyphsPtr(m_coreTextRun.get());
62 m_glyphsVector.grow(m_glyphCount);
63 CTRunGetGlyphs(m_coreTextRun.get(), CFRangeMake(0, 0), m_glyphsVector.data());
64 m_glyphs = m_glyphsVector.data();
67 m_advances = CTRunGetAdvancesPtr(m_coreTextRun.get());
69 m_advancesVector.grow(m_glyphCount);
70 CTRunGetAdvances(m_coreTextRun.get(), CFRangeMake(0, 0), m_advancesVector.data());
71 m_advances = m_advancesVector.data();
75 // Missing glyphs run constructor. Core Text will not generate a run of missing glyphs, instead falling back on
76 // glyphs from LastResort. We want to use the primary font's missing glyph in order to match the fast text code path.
77 void ComplexTextController::ComplexTextRun::createTextRunFromFontDataCoreText(bool ltr)
79 m_coreTextIndicesVector.reserveInitialCapacity(m_stringLength);
81 while (r < m_stringLength) {
82 m_coreTextIndicesVector.uncheckedAppend(r);
83 if (U_IS_SURROGATE(m_characters[r])) {
84 ASSERT(r + 1 < m_stringLength);
85 ASSERT(U_IS_SURROGATE_LEAD(m_characters[r]));
86 ASSERT(U_IS_TRAIL(m_characters[r + 1]));
91 m_glyphCount = m_coreTextIndicesVector.size();
93 for (unsigned r = 0, end = m_glyphCount - 1; r < m_glyphCount / 2; ++r, --end)
94 std::swap(m_coreTextIndicesVector[r], m_coreTextIndicesVector[end]);
96 m_coreTextIndices = m_coreTextIndicesVector.data();
98 // Synthesize a run of missing glyphs.
99 m_glyphsVector.fill(0, m_glyphCount);
100 m_glyphs = m_glyphsVector.data();
101 m_advancesVector.fill(CGSizeMake(m_fontData->widthForGlyph(0), 0), m_glyphCount);
102 m_advances = m_advancesVector.data();
105 struct ProviderInfo {
108 CFDictionaryRef attributes;
111 static const UniChar* provideStringAndAttributes(CFIndex stringIndex, CFIndex* charCount, CFDictionaryRef* attributes, void* refCon)
113 ProviderInfo* info = static_cast<struct ProviderInfo*>(refCon);
114 if (stringIndex < 0 || static_cast<unsigned>(stringIndex) >= info->length)
117 *charCount = info->length - stringIndex;
118 *attributes = info->attributes;
119 return info->cp + stringIndex;
122 void ComplexTextController::collectComplexTextRunsForCharactersCoreText(const UChar* cp, unsigned length, unsigned stringLocation, const SimpleFontData* fontData)
125 // Create a run of missing glyphs from the primary font.
126 m_complexTextRuns.append(ComplexTextRun::create(m_font.primaryFont(), cp, stringLocation, length, m_run.ltr()));
130 if (m_fallbackFonts && fontData != m_font.primaryFont())
131 m_fallbackFonts->add(fontData);
133 RetainPtr<CTLineRef> line;
135 if (!m_mayUseNaturalWritingDirection || m_run.directionalOverride()) {
136 static const void* optionKeys[] = { kCTTypesetterOptionForcedEmbeddingLevel };
137 const short ltrForcedEmbeddingLevelValue = 0;
138 const short rtlForcedEmbeddingLevelValue = 1;
139 static const void* ltrOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, <rForcedEmbeddingLevelValue) };
140 static const void* rtlOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &rtlForcedEmbeddingLevelValue) };
141 static CFDictionaryRef ltrTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, ltrOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
142 static CFDictionaryRef rtlTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, rtlOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
144 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
145 ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures()) };
146 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, wkCreateCTTypesetterWithUniCharProviderAndOptions(&provideStringAndAttributes, 0, &info, m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
148 RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, cp, length, kCFAllocatorNull));
149 RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(m_font.typesettingFeatures())));
150 RetainPtr<CTTypesetterRef> typesetter(AdoptCF, CTTypesetterCreateWithAttributedStringAndOptions(attributedString.get(), m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
153 line.adoptCF(CTTypesetterCreateLine(typesetter.get(), CFRangeMake(0, 0)));
155 ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures()) };
157 line.adoptCF(wkCreateCTLineWithUniCharProvider(&provideStringAndAttributes, 0, &info));
160 CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
162 CFIndex runCount = CFArrayGetCount(runArray);
164 for (CFIndex r = 0; r < runCount; r++) {
165 CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
166 ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
167 m_complexTextRuns.append(ComplexTextRun::create(ctRun, fontData, cp, stringLocation, length));
171 } // namespace WebCore
173 #endif // USE(CORE_TEXT)