GlyphPageTreeNodeMac.cpp [plain text]
#include "config.h"
#include "GlyphPageTreeNode.h"
#include "Font.h"
#include "SimpleFontData.h"
#include "WebCoreSystemInterface.h"
#if !PLATFORM(IOS)
#include <ApplicationServices/ApplicationServices.h>
#else
#include <CoreGraphics/CGFontUnicodeSupport.h>
#include <CoreText/CTFontPriv.h>
#include <CoreText/CoreText.h>
#endif
namespace WebCore {
static bool shouldUseCoreText(const UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
{
if (fontData->platformData().isCompositeFontReference())
return true;
if (fontData->platformData().widthVariant() != RegularWidth || fontData->hasVerticalGlyphs()) {
for (unsigned i = 0; i < bufferLength; ++i) {
if (!Font::isCJKIdeograph(buffer[i]))
return true;
}
}
return false;
}
bool GlyphPage::mayUseMixedFontDataWhenFilling(const UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
{
return shouldUseCoreText(buffer, bufferLength, fontData);
}
bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
{
bool haveGlyphs = false;
Vector<CGGlyph, 512> glyphs(bufferLength);
if (!shouldUseCoreText(buffer, bufferLength, fontData)) {
#if !PLATFORM(IOS)
wkGetGlyphsForCharacters(fontData->platformData().cgFont(), buffer, glyphs.data(), bufferLength);
#else
CGFontGetGlyphsForUnichars(fontData->platformData().cgFont(), buffer, glyphs.data(), bufferLength);
#endif
for (unsigned i = 0; i < length; ++i) {
if (!glyphs[i])
setGlyphDataForIndex(offset + i, 0, 0);
else {
setGlyphDataForIndex(offset + i, glyphs[i], fontData);
haveGlyphs = true;
}
}
} else if (!fontData->platformData().isCompositeFontReference() && ((fontData->platformData().widthVariant() == RegularWidth) ? wkGetVerticalGlyphsForCharacters(fontData->platformData().ctFont(), buffer, glyphs.data(), bufferLength)
: CTFontGetGlyphsForCharacters(fontData->platformData().ctFont(), buffer, glyphs.data(), bufferLength))) {
unsigned glyphStep = bufferLength / length;
for (unsigned i = 0; i < length; ++i) {
if (!glyphs[i * glyphStep])
setGlyphDataForIndex(offset + i, 0, 0);
else {
setGlyphDataForIndex(offset + i, glyphs[i * glyphStep], fontData);
haveGlyphs = true;
}
}
} else {
RetainPtr<CFStringRef> string = adoptCF(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buffer, bufferLength, kCFAllocatorNull));
RetainPtr<CFAttributedStringRef> attributedString = adoptCF(CFAttributedStringCreate(kCFAllocatorDefault, string.get(), fontData->getCFStringAttributes(0, fontData->hasVerticalGlyphs() ? Vertical : Horizontal)));
RetainPtr<CTLineRef> line = adoptCF(CTLineCreateWithAttributedString(attributedString.get()));
CFArrayRef runArray = CTLineGetGlyphRuns(line.get());
CFIndex runCount = CFArrayGetCount(runArray);
for (unsigned index = 0; index < length; ++index)
setGlyphDataForIndex(offset + index, 0, 0);
Vector<CGGlyph, 512> glyphVector;
Vector<CFIndex, 512> indexVector;
bool done = false;
RetainPtr<CGFontRef> cgFont = adoptCF(CTFontCopyGraphicsFont(fontData->platformData().ctFont(), 0));
for (CFIndex r = 0; r < runCount && !done ; ++r) {
CTRunRef ctRun = static_cast<CTRunRef>(CFArrayGetValueAtIndex(runArray, r));
ASSERT(CFGetTypeID(ctRun) == CTRunGetTypeID());
CFDictionaryRef attributes = CTRunGetAttributes(ctRun);
CTFontRef runFont = static_cast<CTFontRef>(CFDictionaryGetValue(attributes, kCTFontAttributeName));
RetainPtr<CGFontRef> runCGFont = adoptCF(CTFontCopyGraphicsFont(runFont, 0));
bool gotBaseFont = CFEqual(cgFont.get(), runCGFont.get());
if (gotBaseFont || fontData->platformData().isCompositeFontReference()) {
CFIndex glyphCount = CTRunGetGlyphCount(ctRun);
const CGGlyph* glyphs = CTRunGetGlyphsPtr(ctRun);
if (!glyphs) {
glyphVector.resize(glyphCount);
CTRunGetGlyphs(ctRun, CFRangeMake(0, 0), glyphVector.data());
glyphs = glyphVector.data();
}
const CFIndex* stringIndices = CTRunGetStringIndicesPtr(ctRun);
if (!stringIndices) {
indexVector.resize(glyphCount);
CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), indexVector.data());
stringIndices = indexVector.data();
}
if (gotBaseFont) {
for (CFIndex i = 0; i < glyphCount; ++i) {
if (stringIndices[i] >= static_cast<CFIndex>(length)) {
done = true;
break;
}
if (glyphs[i]) {
setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], fontData);
haveGlyphs = true;
}
}
#if !PLATFORM(IOS)
} else {
const SimpleFontData* runSimple = fontData->getCompositeFontReferenceFontData((NSFont *)runFont);
if (runSimple) {
for (CFIndex i = 0; i < glyphCount; ++i) {
if (stringIndices[i] >= static_cast<CFIndex>(length)) {
done = true;
break;
}
if (glyphs[i]) {
setGlyphDataForIndex(offset + stringIndices[i], glyphs[i], runSimple);
haveGlyphs = true;
}
}
}
#endif // !PLATFORM(IOS)
}
}
}
}
return haveGlyphs;
}
}