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 
27 #if USE(CORE_TEXT)
28 
29 #include "ComplexTextController.h"
30 
31 #include "Font.h"
32 #include "TextRun.h"
33 #include "WebCoreSystemInterface.h"
34 #include <CoreText/CoreText.h>
35 
36 #if defined(BUILDING_ON_LEOPARD)
37 // The following symbols are SPI in 10.5.
38 extern "C" {
39 void CTRunGetAdvances(CTRunRef run, CFRange range, CGSize buffer[]);
40 const CGSize* CTRunGetAdvancesPtr(CTRunRef run);
41 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
42 }
43 #endif
44 
45 namespace WebCore {
46 
ComplexTextRun(CTRunRef ctRun,const SimpleFontData * fontData,const UChar * characters,unsigned stringLocation,size_t stringLength,CFRange runRange)47 ComplexTextController::ComplexTextRun::ComplexTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
48     : m_fontData(fontData)
49     , m_characters(characters)
50     , m_stringLocation(stringLocation)
51     , m_stringLength(stringLength)
52     , m_indexEnd(runRange.location + runRange.length)
53     , m_isMonotonic(true)
54 {
55     m_glyphCount = CTRunGetGlyphCount(ctRun);
56     m_coreTextIndices = CTRunGetStringIndicesPtr(ctRun);
57     if (!m_coreTextIndices) {
58         m_coreTextIndicesVector.grow(m_glyphCount);
59         CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), m_coreTextIndicesVector.data());
60         m_coreTextIndices = m_coreTextIndicesVector.data();
61     }
62 
63     m_glyphs = CTRunGetGlyphsPtr(ctRun);
64     if (!m_glyphs) {
65         m_glyphsVector.grow(m_glyphCount);
66         CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), m_glyphsVector.data());
67         m_glyphs = m_glyphsVector.data();
68     }
69 
70     m_advances = CTRunGetAdvancesPtr(ctRun);
71     if (!m_advances) {
72         m_advancesVector.grow(m_glyphCount);
73         CTRunGetAdvances(ctRun, CFRangeMake(0, 0), m_advancesVector.data());
74         m_advances = m_advancesVector.data();
75     }
76 }
77 
78 // Missing glyphs run constructor. Core Text will not generate a run of missing glyphs, instead falling back on
79 // 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)80 void ComplexTextController::ComplexTextRun::createTextRunFromFontDataCoreText(bool ltr)
81 {
82     m_coreTextIndicesVector.reserveInitialCapacity(m_stringLength);
83     unsigned r = 0;
84     while (r < m_stringLength) {
85         m_coreTextIndicesVector.uncheckedAppend(r);
86         if (U_IS_SURROGATE(m_characters[r])) {
87             ASSERT(r + 1 < m_stringLength);
88             ASSERT(U_IS_SURROGATE_LEAD(m_characters[r]));
89             ASSERT(U_IS_TRAIL(m_characters[r + 1]));
90             r += 2;
91         } else
92             r++;
93     }
94     m_glyphCount = m_coreTextIndicesVector.size();
95     if (!ltr) {
96         for (unsigned r = 0, end = m_glyphCount - 1; r < m_glyphCount / 2; ++r, --end)
97             std::swap(m_coreTextIndicesVector[r], m_coreTextIndicesVector[end]);
98     }
99     m_coreTextIndices = m_coreTextIndicesVector.data();
100 
101     // Synthesize a run of missing glyphs.
102     m_glyphsVector.fill(0, m_glyphCount);
103     m_glyphs = m_glyphsVector.data();
104     m_advancesVector.fill(CGSizeMake(m_fontData->widthForGlyph(0), 0), m_glyphCount);
105     m_advances = m_advancesVector.data();
106 }
107 
108 struct ProviderInfo {
109     const UChar* cp;
110     unsigned length;
111     CFDictionaryRef attributes;
112 };
113 
provideStringAndAttributes(CFIndex stringIndex,CFIndex * charCount,CFDictionaryRef * attributes,void * refCon)114 static const UniChar* provideStringAndAttributes(CFIndex stringIndex, CFIndex* charCount, CFDictionaryRef* attributes, void* refCon)
115 {
116     ProviderInfo* info = static_cast<struct ProviderInfo*>(refCon);
117     if (stringIndex < 0 || static_cast<unsigned>(stringIndex) >= info->length)
118         return 0;
119 
120     *charCount = info->length - stringIndex;
121     *attributes = info->attributes;
122     return info->cp + stringIndex;
123 }
124 
collectComplexTextRunsForCharactersCoreText(const UChar * cp,unsigned length,unsigned stringLocation,const SimpleFontData * fontData)125 void ComplexTextController::collectComplexTextRunsForCharactersCoreText(const UChar* cp, unsigned length, unsigned stringLocation, const SimpleFontData* fontData)
126 {
127     if (!fontData) {
128         // Create a run of missing glyphs from the primary font.
129         m_complexTextRuns.append(ComplexTextRun::create(m_font.primaryFont(), cp, stringLocation, length, m_run.ltr()));
130         return;
131     }
132 
133     if (m_fallbackFonts && fontData != m_font.primaryFont())
134         m_fallbackFonts->add(fontData);
135 
136     RetainPtr<CTLineRef> line;
137 
138     if (!m_mayUseNaturalWritingDirection || m_run.directionalOverride()) {
139         static const void* optionKeys[] = { kCTTypesetterOptionForcedEmbeddingLevel };
140         const short ltrForcedEmbeddingLevelValue = 0;
141         const short rtlForcedEmbeddingLevelValue = 1;
142         static const void* ltrOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &ltrForcedEmbeddingLevelValue) };
143         static const void* rtlOptionValues[] = { CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &rtlForcedEmbeddingLevelValue) };
144         static CFDictionaryRef ltrTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, ltrOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
145         static CFDictionaryRef rtlTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, rtlOptionValues, WTF_ARRAY_LENGTH(optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
146 
147 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
148         ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation()) };
149         RetainPtr<CTTypesetterRef> typesetter(AdoptCF, wkCreateCTTypesetterWithUniCharProviderAndOptions(&provideStringAndAttributes, 0, &info, m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
150 #else
151         RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, cp, length, kCFAllocatorNull));
152         RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation())));
153         RetainPtr<CTTypesetterRef> typesetter(AdoptCF, CTTypesetterCreateWithAttributedStringAndOptions(attributedString.get(), m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
154 #endif
155 
156         line.adoptCF(CTTypesetterCreateLine(typesetter.get(), CFRangeMake(0, 0)));
157     } else {
158         ProviderInfo info = { cp, length, fontData->getCFStringAttributes(m_font.typesettingFeatures(), fontData->platformData().orientation()) };
159 
160         line.adoptCF(wkCreateCTLineWithUniCharProvider(&provideStringAndAttributes, 0, &info));
161     }
162 
163     m_coreTextLines.append(line.get());
164 
165     CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
166 
167     CFIndex runCount = CFArrayGetCount(runArray);
168 
169     for (CFIndex r = 0; r < runCount; r++) {
170         CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
171         ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
172         CFRange runRange = CTRunGetStringRange(ctRun);
173         m_complexTextRuns.append(ComplexTextRun::create(ctRun, fontData, cp, stringLocation, length, runRange));
174     }
175 }
176 
177 } // namespace WebCore
178 
179 #endif // USE(CORE_TEXT)
180