#include "config.h"
#include "Font.h"
#include "CharacterNames.h"
#include "FloatRect.h"
#include "FontCache.h"
#include "FontFallbackList.h"
#include "GlyphBuffer.h"
#include "GlyphPageTreeNode.h"
#include "IntPoint.h"
#include "SimpleFontData.h"
#include "WidthIterator.h"
#include <wtf/MathExtras.h>
#include <wtf/unicode/Unicode.h>
using namespace WTF;
using namespace Unicode;
namespace WebCore {
GlyphData Font::glyphDataForCharacter(UChar32 c, bool mirror, bool forceSmallCaps) const
{
ASSERT(isMainThread() || pthread_main_np());
bool useSmallCapsFont = forceSmallCaps;
if (m_fontDescription.smallCaps()) {
UChar32 upperC = toUpper(c);
if (upperC != c) {
c = upperC;
useSmallCapsFont = true;
}
}
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;
if (!useSmallCapsFont) {
while (true) {
page = node->page();
if (page) {
GlyphData data = page->glyphDataForCharacter(c);
if (data.fontData && (!forceFallback || node->isSystemFallback()))
return data;
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;
}
} else {
while (true) {
page = node->page();
if (page) {
GlyphData data = page->glyphDataForCharacter(c);
if (data.fontData) {
const SimpleFontData* smallCapsFontData = data.fontData->smallCapsFontData(m_fontDescription);
if (!smallCapsFontData)
return data;
GlyphPageTreeNode* smallCapsNode = GlyphPageTreeNode::getRootChild(smallCapsFontData, pageNumber);
const GlyphPage* smallCapsPage = smallCapsNode->page();
if (smallCapsPage) {
GlyphData data = smallCapsPage->glyphDataForCharacter(c);
if (data.fontData)
return data;
}
return smallCapsFontData->missingGlyphData();
}
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 (useSmallCapsFont && characterFontData)
characterFontData = characterFontData->smallCapsFontData(m_fontDescription);
if (characterFontData) {
GlyphPage* fallbackPage = GlyphPageTreeNode::getRootChild(characterFontData, pageNumber)->page();
GlyphData data = fallbackPage && fallbackPage->fontDataForCharacter(c) ? fallbackPage->glyphDataForCharacter(c) : characterFontData->missingGlyphData();
if (!useSmallCapsFont) {
#if OS(WINCE)
page->setGlyphDataForCharacter(c, c, characterFontData);
return page->glyphDataForCharacter(c);
#else
page->setGlyphDataForCharacter(c, data.glyph, data.fontData);
#endif
}
return data;
}
GlyphData data = primaryFont()->missingGlyphData();
if (!useSmallCapsFont) {
#if OS(WINCE)
page->setGlyphDataForCharacter(c, c, data.fontData);
return page->glyphDataForCharacter(c);
#else
page->setGlyphDataForCharacter(c, data.glyph, data.fontData);
#endif
}
return data;
}
void Font::setCodePath(CodePath p)
{
s_codePath = p;
}
Font::CodePath Font::codePath()
{
return s_codePath;
}
Font::CodePath Font::codePath(const TextRun& run) const
{
if (s_codePath != Auto)
return s_codePath;
CodePath result = Simple;
for (int i = 0; i < run.length(); i++) {
const UChar c = run[i];
if (c < 0x300) continue;
if (c <= 0x36F)
return Complex;
if (c < 0x0591 || c == 0x05BE) continue;
if (c <= 0x05CF)
return Complex;
if (c < 0x0600) continue;
if (c <= 0x1059)
return Complex;
if (c < 0x1100) continue;
if (c <= 0x11FF)
return Complex;
if (c < 0x1780) continue;
if (c <= 0x18AF)
return Complex;
if (c < 0x1900) continue;
if (c <= 0x194F)
return Complex;
if (c < 0x1E00) continue;
if (c <= 0x2000) {
result = SimpleWithGlyphOverflow;
continue;
}
if (c < 0x20D0) continue;
if (c <= 0x20FF)
return Complex;
if (c < 0xFE20) continue;
if (c <= 0xFE2F)
return Complex;
}
if (typesettingFeatures())
return Complex;
return result;
}
float Font::drawSimpleText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
{
GlyphBuffer glyphBuffer;
float startX = point.x();
WidthIterator it(this, run);
it.advance(from);
float beforeWidth = it.m_runWidthSoFar;
it.advance(to, &glyphBuffer);
if (glyphBuffer.isEmpty())
return 0.0f;
float afterWidth = it.m_runWidthSoFar;
if (run.rtl()) {
float finalRoundingWidth = it.m_finalRoundingWidth;
it.advance(run.length());
startX += finalRoundingWidth + it.m_runWidthSoFar - afterWidth;
} else
startX += beforeWidth;
if (run.rtl())
for (int i = 0, end = glyphBuffer.size() - 1; i < glyphBuffer.size() / 2; ++i, --end)
glyphBuffer.swap(i, end);
FloatPoint startPoint(startX, point.y());
drawGlyphBuffer(context, glyphBuffer, run, startPoint);
return it.m_runWidthSoFar;
}
void Font::drawGlyphBuffer(GraphicsContext* context, const GlyphBuffer& glyphBuffer, const TextRun&, FloatPoint& point) const
{
const SimpleFontData* fontData = glyphBuffer.fontDataAt(0);
FloatSize offset = glyphBuffer.offsetAt(0);
FloatPoint startPoint(point);
float nextX = startPoint.x();
int lastFrom = 0;
int nextGlyph = 0;
while (nextGlyph < glyphBuffer.size()) {
const SimpleFontData* nextFontData = glyphBuffer.fontDataAt(nextGlyph);
FloatSize nextOffset = glyphBuffer.offsetAt(nextGlyph);
if (nextFontData != fontData || nextOffset != offset) {
drawGlyphs(context, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
lastFrom = nextGlyph;
fontData = nextFontData;
offset = nextOffset;
startPoint.setX(nextX);
}
nextX += glyphBuffer.advanceAt(nextGlyph);
nextGlyph++;
}
drawGlyphs(context, fontData, glyphBuffer, lastFrom, nextGlyph - lastFrom, startPoint);
point.setX(nextX);
}
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()) - ascent());
glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(it.maxGlyphBoundingBoxY()) - descent());
glyphOverflow->left = ceilf(it.firstGlyphOverflow());
glyphOverflow->right = ceilf(it.lastGlyphOverflow());
}
return it.m_runWidthSoFar;
}
FloatRect Font::selectionRectForSimpleText(const TextRun& run, const IntPoint& 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(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
} else {
return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
}
}
int Font::offsetForPositionForSimpleText(const TextRun& run, int x, bool includePartialGlyphs) const
{
float delta = (float)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;
}
}