#include "config.h"
#include "Font.h"
#include "FloatRect.h"
#include "FontCache.h"
#include "FontFallbackList.h"
#include "GlyphBuffer.h"
#include "GlyphPageTreeNode.h"
#include "SimpleFontData.h"
#include "TextRun.h"
#include "WidthIterator.h"
#include <wtf/MainThread.h>
#include <wtf/MathExtras.h>
#include <wtf/unicode/CharacterNames.h>
#include <wtf/unicode/Unicode.h>
using namespace WTF;
using namespace Unicode;
using namespace std;
namespace WebCore {
GlyphData Font::glyphDataForCharacter(UChar32 c, bool mirror, FontDataVariant variant) const
{
return glyphDataAndPageForCharacter(c, mirror, variant).first;
}
std::pair<GlyphData, GlyphPage*> Font::glyphDataAndPageForCharacter(UChar32 c, bool mirror, FontDataVariant variant) const
{
ASSERT(isMainThread() || pthread_main_np());
if (variant == AutoVariant) {
if (m_fontDescription.smallCaps() && !primaryFont()->isSVGFont()) {
UChar32 upperC = toUpper(c);
if (upperC != c) {
c = upperC;
variant = SmallCapsVariant;
} else
variant = NormalVariant;
} else
variant = NormalVariant;
}
if (mirror)
c = mirroredChar(c);
bool forceFallback = false;
if (c >= 0x0600 && c <= 0x06FF)
forceFallback = true;
unsigned pageNumber = (c / GlyphPage::size);
GlyphPageTreeNode* node = pageNumber ? m_fontList->m_pages.get(pageNumber) : m_fontList->m_pageZero;
if (!node) {
node = GlyphPageTreeNode::getRootChild(fontDataAt(0), pageNumber);
if (pageNumber)
m_fontList->m_pages.set(pageNumber, node);
else
m_fontList->m_pageZero = node;
}
GlyphPage* page = 0;
if (variant == NormalVariant) {
while (true) {
page = node->page();
if (page) {
GlyphData data = page->glyphDataForCharacter(c);
if (forceFallback && !node->isSystemFallback())
data.fontData = 0;
if (data.fontData && (data.fontData->platformData().orientation() == Horizontal || data.fontData->isTextOrientationFallback()))
return make_pair(data, page);
if (data.fontData) {
if (isCJKIdeographOrSymbol(c)) {
if (!data.fontData->hasVerticalGlyphs()) {
variant = BrokenIdeographVariant;
break;
}
} else {
if (m_fontDescription.textOrientation() == TextOrientationVerticalRight) {
const SimpleFontData* verticalRightFontData = data.fontData->verticalRightOrientationFontData();
GlyphPageTreeNode* verticalRightNode = GlyphPageTreeNode::getRootChild(verticalRightFontData, pageNumber);
GlyphPage* verticalRightPage = verticalRightNode->page();
if (verticalRightPage) {
GlyphData verticalRightData = verticalRightPage->glyphDataForCharacter(c);
if (data.glyph != verticalRightData.glyph)
return make_pair(data, page);
if (verticalRightData.fontData)
return make_pair(verticalRightData, verticalRightPage);
}
} else if (m_fontDescription.textOrientation() == TextOrientationUpright) {
const SimpleFontData* uprightFontData = data.fontData->uprightOrientationFontData();
GlyphPageTreeNode* uprightNode = GlyphPageTreeNode::getRootChild(uprightFontData, pageNumber);
GlyphPage* uprightPage = uprightNode->page();
if (uprightPage) {
GlyphData uprightData = uprightPage->glyphDataForCharacter(c);
if (data.glyph == uprightData.glyph)
return make_pair(data, page);
if (uprightData.fontData)
return make_pair(uprightData, uprightPage);
}
}
ASSERT_NOT_REACHED();
}
return make_pair(data, page);
}
if (node->isSystemFallback())
break;
}
node = node->getChild(fontDataAt(node->level()), pageNumber);
if (pageNumber)
m_fontList->m_pages.set(pageNumber, node);
else
m_fontList->m_pageZero = node;
}
}
if (variant != NormalVariant) {
while (true) {
page = node->page();
if (page) {
GlyphData data = page->glyphDataForCharacter(c);
if (data.fontData) {
const SimpleFontData* variantFontData = data.fontData->variantFontData(m_fontDescription, variant);
if (!variantFontData)
return make_pair(data, page);
GlyphPageTreeNode* variantNode = GlyphPageTreeNode::getRootChild(variantFontData, pageNumber);
GlyphPage* variantPage = variantNode->page();
if (variantPage) {
GlyphData data = variantPage->glyphDataForCharacter(c);
if (data.fontData)
return make_pair(data, variantPage);
}
return make_pair(variantFontData->missingGlyphData(), page);
}
if (node->isSystemFallback())
break;
}
node = node->getChild(fontDataAt(node->level()), pageNumber);
if (pageNumber)
m_fontList->m_pages.set(pageNumber, node);
else
m_fontList->m_pageZero = node;
}
}
ASSERT(page);
ASSERT(node->isSystemFallback());
UChar codeUnits[2];
int codeUnitsLength;
if (c <= 0xFFFF) {
codeUnits[0] = Font::normalizeSpaces(c);
codeUnitsLength = 1;
} else {
codeUnits[0] = U16_LEAD(c);
codeUnits[1] = U16_TRAIL(c);
codeUnitsLength = 2;
}
const SimpleFontData* characterFontData = fontCache()->getFontDataForCharacters(*this, codeUnits, codeUnitsLength);
if (characterFontData) {
if (characterFontData->platformData().orientation() == Vertical && !characterFontData->hasVerticalGlyphs() && isCJKIdeographOrSymbol(c))
variant = BrokenIdeographVariant;
if (variant != NormalVariant)
characterFontData = characterFontData->variantFontData(m_fontDescription, variant);
}
if (characterFontData) {
GlyphPage* fallbackPage = GlyphPageTreeNode::getRootChild(characterFontData, pageNumber)->page();
GlyphData data = fallbackPage && fallbackPage->fontDataForCharacter(c) ? fallbackPage->glyphDataForCharacter(c) : characterFontData->missingGlyphData();
if (variant == NormalVariant) {
#if OS(WINCE)
page->setGlyphDataForCharacter(c, c, characterFontData);
characterFontData->setMaxGlyphPageTreeLevel(max(characterFontData->maxGlyphPageTreeLevel(), node->level()));
return make_pair(page->glyphDataForCharacter(c), page);
#else
page->setGlyphDataForCharacter(c, data.glyph, data.fontData);
data.fontData->setMaxGlyphPageTreeLevel(max(data.fontData->maxGlyphPageTreeLevel(), node->level()));
#endif
}
return make_pair(data, page);
}
GlyphData data = primaryFont()->missingGlyphData();
if (variant == NormalVariant) {
#if OS(WINCE)
page->setGlyphDataForCharacter(c, c, data.fontData);
data.fontData->setMaxGlyphPageTreeLevel(max(data.fontData->maxGlyphPageTreeLevel(), node->level()));
return make_pair(page->glyphDataForCharacter(c), page);
#else
page->setGlyphDataForCharacter(c, data.glyph, data.fontData);
data.fontData->setMaxGlyphPageTreeLevel(max(data.fontData->maxGlyphPageTreeLevel(), node->level()));
#endif
}
return make_pair(data, page);
}
bool Font::primaryFontHasGlyphForCharacter(UChar32 character) const
{
unsigned pageNumber = (character / GlyphPage::size);
GlyphPageTreeNode* node = GlyphPageTreeNode::getRootChild(primaryFont(), pageNumber);
GlyphPage* page = node->page();
return page && page->fontDataForCharacter(character);
}
bool Font::getEmphasisMarkGlyphData(const AtomicString& mark, GlyphData& glyphData) const
{
if (mark.isEmpty())
return false;
UChar32 character = mark[0];
if (U16_IS_SURROGATE(character)) {
if (!U16_IS_SURROGATE_LEAD(character))
return false;
if (mark.length() < 2)
return false;
UChar low = mark[1];
if (!U16_IS_TRAIL(low))
return false;
character = U16_GET_SUPPLEMENTARY(character, low);
}
glyphData = glyphDataForCharacter(character, false, EmphasisMarkVariant);
return true;
}
int Font::emphasisMarkAscent(const AtomicString& mark) const
{
GlyphData markGlyphData;
if (!getEmphasisMarkGlyphData(mark, markGlyphData))
return 0;
const SimpleFontData* markFontData = markGlyphData.fontData;
ASSERT(markFontData);
if (!markFontData)
return 0;
return markFontData->fontMetrics().ascent();
}
int Font::emphasisMarkDescent(const AtomicString& mark) const
{
GlyphData markGlyphData;
if (!getEmphasisMarkGlyphData(mark, markGlyphData))
return 0;
const SimpleFontData* markFontData = markGlyphData.fontData;
ASSERT(markFontData);
if (!markFontData)
return 0;
return markFontData->fontMetrics().descent();
}
int Font::emphasisMarkHeight(const AtomicString& mark) const
{
GlyphData markGlyphData;
if (!getEmphasisMarkGlyphData(mark, markGlyphData))
return 0;
const SimpleFontData* markFontData = markGlyphData.fontData;
ASSERT(markFontData);
if (!markFontData)
return 0;
return markFontData->fontMetrics().height();
}
float Font::getGlyphsAndAdvancesForSimpleText(const TextRun& run, int from, int to, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const
{
float initialAdvance;
WidthIterator it(this, run, 0, false, forTextEmphasis);
it.advance(from);
float beforeWidth = it.m_runWidthSoFar;
it.advance(to, &glyphBuffer);
if (glyphBuffer.isEmpty())
return 0;
float afterWidth = it.m_runWidthSoFar;
if (run.rtl()) {
float finalRoundingWidth = it.m_finalRoundingWidth;
it.advance(run.length());
initialAdvance = finalRoundingWidth + it.m_runWidthSoFar - afterWidth;
} else
initialAdvance = beforeWidth;
if (run.rtl()) {
for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
glyphBuffer.swap(i, end);
}
return initialAdvance;
}
float Font::drawSimpleText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
{
GlyphBuffer glyphBuffer;
float startX = point.x() + getGlyphsAndAdvancesForSimpleText(run, from, to, glyphBuffer);
if (glyphBuffer.isEmpty())
return 0.0f;
FloatPoint startPoint(startX, point.y());
drawGlyphBuffer(context, run, glyphBuffer, startPoint);
return startPoint.x() - startX;
}
void Font::drawEmphasisMarksForSimpleText(GraphicsContext* context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to) const
{
GlyphBuffer glyphBuffer;
float initialAdvance = getGlyphsAndAdvancesForSimpleText(run, from, to, glyphBuffer, ForTextEmphasis);
if (glyphBuffer.isEmpty())
return;
drawEmphasisMarks(context, run, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
}
void Font::drawGlyphBuffer(GraphicsContext* context, const TextRun& run, const GlyphBuffer& glyphBuffer, FloatPoint& point) const
{
#if !ENABLE(SVG_FONTS)
UNUSED_PARAM(run);
#endif
const SimpleFontData* fontData = glyphBuffer.fontDataAt(0);
FloatSize offset = glyphBuffer.offsetAt(0);
FloatPoint startPoint(point);
float nextX = startPoint.x() + glyphBuffer.advanceAt(0);
int lastFrom = 0;
int nextGlyph = 1;
#if ENABLE(SVG_FONTS)
TextRun::RenderingContext* renderingContext = run.renderingContext();
#endif
while (nextGlyph < glyphBuffer.size()) {
const SimpleFontData* nextFontData = glyphBuffer.fontDataAt(nextGlyph);
FloatSize nextOffset = glyphBuffer.offsetAt(nextGlyph);
if (nextFontData != fontData || nextOffset != offset) {
#if ENABLE(SVG_FONTS)
if (renderingContext && fontData->isSVGFont())
renderingContext->drawSVGGlyphs(context, run, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
else
#endif
drawGlyphs(context, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
lastFrom = nextGlyph;
fontData = nextFontData;
offset = nextOffset;
startPoint.setX(nextX);
}
nextX += glyphBuffer.advanceAt(nextGlyph);
nextGlyph++;
}
#if ENABLE(SVG_FONTS)
if (renderingContext && fontData->isSVGFont())
renderingContext->drawSVGGlyphs(context, run, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
else {
#endif
drawGlyphs(context, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
point.setX(nextX);
}
}
inline static float offsetToMiddleOfGlyph(const SimpleFontData* fontData, Glyph glyph)
{
if (fontData->platformData().orientation() == Horizontal) {
FloatRect bounds = fontData->boundsForGlyph(glyph);
return bounds.x() + bounds.width() / 2;
}
return fontData->widthForGlyph(glyph) / 2;
}
inline static float offsetToMiddleOfGlyphAtIndex(const GlyphBuffer& glyphBuffer, size_t i)
{
return offsetToMiddleOfGlyph(glyphBuffer.fontDataAt(i), glyphBuffer.glyphAt(i));
}
void Font::drawEmphasisMarks(GraphicsContext* context, const TextRun& run, const GlyphBuffer& glyphBuffer, const AtomicString& mark, const FloatPoint& point) const
{
GlyphData markGlyphData;
if (!getEmphasisMarkGlyphData(mark, markGlyphData))
return;
const SimpleFontData* markFontData = markGlyphData.fontData;
ASSERT(markFontData);
if (!markFontData)
return;
Glyph markGlyph = markGlyphData.glyph;
Glyph spaceGlyph = markFontData->spaceGlyph();
float middleOfLastGlyph = offsetToMiddleOfGlyphAtIndex(glyphBuffer, 0);
FloatPoint startPoint(point.x() + middleOfLastGlyph - offsetToMiddleOfGlyph(markFontData, markGlyph), point.y());
GlyphBuffer markBuffer;
for (int i = 0; i + 1 < glyphBuffer.size(); ++i) {
float middleOfNextGlyph = offsetToMiddleOfGlyphAtIndex(glyphBuffer, i + 1);
float advance = glyphBuffer.advanceAt(i) - middleOfLastGlyph + middleOfNextGlyph;
markBuffer.add(glyphBuffer.glyphAt(i) ? markGlyph : spaceGlyph, markFontData, advance);
middleOfLastGlyph = middleOfNextGlyph;
}
markBuffer.add(glyphBuffer.glyphAt(glyphBuffer.size() - 1) ? markGlyph : spaceGlyph, markFontData, 0);
drawGlyphBuffer(context, run, markBuffer, startPoint);
}
float Font::floatWidthForSimpleText(const TextRun& run, GlyphBuffer* glyphBuffer, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
WidthIterator it(this, run, fallbackFonts, glyphOverflow);
it.advance(run.length(), glyphBuffer);
if (glyphOverflow) {
glyphOverflow->top = max<int>(glyphOverflow->top, ceilf(-it.minGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().ascent()));
glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(it.maxGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().descent()));
glyphOverflow->left = ceilf(it.firstGlyphOverflow());
glyphOverflow->right = ceilf(it.lastGlyphOverflow());
}
return it.m_runWidthSoFar;
}
FloatRect Font::selectionRectForSimpleText(const TextRun& run, const FloatPoint& point, int h, int from, int to) const
{
WidthIterator it(this, run);
it.advance(from);
float beforeWidth = it.m_runWidthSoFar;
it.advance(to);
float afterWidth = it.m_runWidthSoFar;
if (run.rtl()) {
it.advance(run.length());
float totalWidth = it.m_runWidthSoFar;
return FloatRect(floorf(point.x() + totalWidth - afterWidth), point.y(), roundf(point.x() + totalWidth - beforeWidth) - floorf(point.x() + totalWidth - afterWidth), h);
}
return FloatRect(floorf(point.x() + beforeWidth), point.y(), roundf(point.x() + afterWidth) - floorf(point.x() + beforeWidth), h);
}
int Font::offsetForPositionForSimpleText(const TextRun& run, float x, bool includePartialGlyphs) const
{
float delta = x;
WidthIterator it(this, run);
GlyphBuffer localGlyphBuffer;
unsigned offset;
if (run.rtl()) {
delta -= floatWidthForSimpleText(run, 0);
while (1) {
offset = it.m_currentCharacter;
float w;
if (!it.advanceOneCharacter(w, &localGlyphBuffer))
break;
delta += w;
if (includePartialGlyphs) {
if (delta - w / 2 >= 0)
break;
} else {
if (delta >= 0)
break;
}
}
} else {
while (1) {
offset = it.m_currentCharacter;
float w;
if (!it.advanceOneCharacter(w, &localGlyphBuffer))
break;
delta -= w;
if (includePartialGlyphs) {
if (delta + w / 2 <= 0)
break;
} else {
if (delta <= 0)
break;
}
}
}
return offset;
}
}