Home | History | Annotate | Download | only in mac
      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 
     28 #if USE(CORE_TEXT)
     29 
     30 #include "Font.h"
     31 
     32 #if defined(BUILDING_ON_LEOPARD)
     33 // The following symbols are SPI in 10.5.
     34 extern "C" {
     35 void CTRunGetAdvances(CTRunRef run, CFRange range, CGSize buffer[]);
     36 const CGSize* CTRunGetAdvancesPtr(CTRunRef run);
     37 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
     38 }
     39 #endif
     40 
     41 namespace WebCore {
     42 
     43 ComplexTextController::ComplexTextRun::ComplexTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength)
     44     : m_coreTextRun(ctRun)
     45     , m_fontData(fontData)
     46     , m_characters(characters)
     47     , m_stringLocation(stringLocation)
     48     , m_stringLength(stringLength)
     49     , m_isMonotonic(true)
     50 {
     51     m_glyphCount = CTRunGetGlyphCount(m_coreTextRun.get());
     52     m_coreTextIndices = CTRunGetStringIndicesPtr(m_coreTextRun.get());
     53     if (!m_coreTextIndices) {
     54         m_coreTextIndicesData.adoptCF(CFDataCreateMutable(kCFAllocatorDefault, m_glyphCount * sizeof(CFIndex)));
     55         CFDataIncreaseLength(m_coreTextIndicesData.get(), m_glyphCount * sizeof(CFIndex));
     56         m_coreTextIndices = reinterpret_cast<const CFIndex*>(CFDataGetMutableBytePtr(m_coreTextIndicesData.get()));
     57         CTRunGetStringIndices(m_coreTextRun.get(), CFRangeMake(0, 0), const_cast<CFIndex*>(m_coreTextIndices));
     58     }
     59 
     60     m_glyphs = CTRunGetGlyphsPtr(m_coreTextRun.get());
     61     if (!m_glyphs) {
     62         m_glyphsVector.grow(m_glyphCount);
     63         CTRunGetGlyphs(m_coreTextRun.get(), CFRangeMake(0, 0), m_glyphsVector.data());
     64         m_glyphs = m_glyphsVector.data();
     65     }
     66 
     67     m_advances = CTRunGetAdvancesPtr(m_coreTextRun.get());
     68     if (!m_advances) {
     69         m_advancesVector.grow(m_glyphCount);
     70         CTRunGetAdvances(m_coreTextRun.get(), CFRangeMake(0, 0), m_advancesVector.data());
     71         m_advances = m_advancesVector.data();
     72     }
     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.
     78 void ComplexTextController::ComplexTextRun::createTextRunFromFontDataCoreText(bool ltr)
     79 {
     80     Vector<CFIndex, 16> indices;
     81     unsigned r = 0;
     82     while (r < m_stringLength) {
     83         indices.append(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 = indices.size();
     93     if (!ltr) {
     94         for (unsigned r = 0, end = m_glyphCount - 1; r < m_glyphCount / 2; ++r, --end)
     95             std::swap(indices[r], indices[end]);
     96     }
     97     m_coreTextIndicesData.adoptCF(CFDataCreateMutable(kCFAllocatorDefault, m_glyphCount * sizeof(CFIndex)));
     98     CFDataAppendBytes(m_coreTextIndicesData.get(), reinterpret_cast<const UInt8*>(indices.data()), m_glyphCount * sizeof(CFIndex));
     99     m_coreTextIndices = reinterpret_cast<const CFIndex*>(CFDataGetBytePtr(m_coreTextIndicesData.get()));
    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 void ComplexTextController::collectComplexTextRunsForCharactersCoreText(const UChar* cp, unsigned length, unsigned stringLocation, const SimpleFontData* fontData)
    109 {
    110     if (!fontData) {
    111         // Create a run of missing glyphs from the primary font.
    112         m_complexTextRuns.append(ComplexTextRun::create(m_font.primaryFont(), cp, stringLocation, length, m_run.ltr()));
    113         return;
    114     }
    115 
    116     if (m_fallbackFonts && fontData != m_font.primaryFont())
    117         m_fallbackFonts->add(fontData);
    118 
    119     RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(NULL, cp, length, kCFAllocatorNull));
    120 
    121     RetainPtr<CFAttributedStringRef> attributedString(AdoptCF, CFAttributedStringCreate(NULL, string.get(), fontData->getCFStringAttributes(m_font.typesettingFeatures())));
    122 
    123     RetainPtr<CTTypesetterRef> typesetter;
    124 
    125     if (!m_mayUseNaturalWritingDirection || m_run.directionalOverride()) {
    126         static const void* optionKeys[] = { kCTTypesetterOptionForcedEmbeddingLevel };
    127         static const void* ltrOptionValues[] = { kCFBooleanFalse };
    128         static const void* rtlOptionValues[] = { kCFBooleanTrue };
    129         static CFDictionaryRef ltrTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, ltrOptionValues, sizeof(optionKeys) / sizeof(*optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    130         static CFDictionaryRef rtlTypesetterOptions = CFDictionaryCreate(kCFAllocatorDefault, optionKeys, rtlOptionValues, sizeof(optionKeys) / sizeof(*optionKeys), &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    131         typesetter.adoptCF(CTTypesetterCreateWithAttributedStringAndOptions(attributedString.get(), m_run.ltr() ? ltrTypesetterOptions : rtlTypesetterOptions));
    132     } else
    133         typesetter.adoptCF(CTTypesetterCreateWithAttributedString(attributedString.get()));
    134 
    135     RetainPtr<CTLineRef> line(AdoptCF, CTTypesetterCreateLine(typesetter.get(), CFRangeMake(0, 0)));
    136 
    137     CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
    138 
    139     CFIndex runCount = CFArrayGetCount(runArray);
    140 
    141     for (CFIndex r = 0; r < runCount; r++) {
    142         CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
    143         ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
    144         m_complexTextRuns.append(ComplexTextRun::create(ctRun, fontData, cp, stringLocation, length));
    145     }
    146 }
    147 
    148 } // namespace WebCore
    149 
    150 #endif // USE(CORE_TEXT)
    151