#include "config.h"
#include "WidthIterator.h"
#include "Font.h"
#include "GlyphBuffer.h"
#include "SimpleFontData.h"
#include "TextRun.h"
#include <wtf/MathExtras.h>
#if USE(ICU_UNICODE)
#include <unicode/unorm.h>
#endif
using namespace WTF;
using namespace Unicode;
using namespace std;
namespace WebCore {
static const uint8_t hiraganaKatakanaVoicingMarksCombiningClass = 8;
WidthIterator::WidthIterator(const Font* font, const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts, bool accountForGlyphBounds, bool forTextEmphasis)
: m_font(font)
, m_run(run)
, m_end(run.length())
, m_currentCharacter(0)
, m_runWidthSoFar(0)
, m_isAfterExpansion(!run.allowsLeadingExpansion())
, m_finalRoundingWidth(0)
, m_fallbackFonts(fallbackFonts)
, m_accountForGlyphBounds(accountForGlyphBounds)
, m_maxGlyphBoundingBoxY(numeric_limits<float>::min())
, m_minGlyphBoundingBoxY(numeric_limits<float>::max())
, m_firstGlyphOverflow(0)
, m_lastGlyphOverflow(0)
, m_forTextEmphasis(forTextEmphasis)
{
m_expansion = m_run.expansion();
if (!m_expansion)
m_expansionPerOpportunity = 0;
else {
bool isAfterExpansion = m_isAfterExpansion;
unsigned expansionOpportunityCount = Font::expansionOpportunityCount(m_run.characters(), m_end, m_run.ltr() ? LTR : RTL, isAfterExpansion);
if (isAfterExpansion && !m_run.allowsTrailingExpansion())
expansionOpportunityCount--;
if (!expansionOpportunityCount)
m_expansionPerOpportunity = 0;
else
m_expansionPerOpportunity = m_expansion / expansionOpportunityCount;
}
}
void WidthIterator::advance(int offset, GlyphBuffer* glyphBuffer)
{
if (offset > m_end)
offset = m_end;
int currentCharacter = m_currentCharacter;
if (currentCharacter >= offset)
return;
const UChar* cp = m_run.data(currentCharacter);
bool rtl = m_run.rtl();
bool hasExtraSpacing = (m_font->letterSpacing() || m_font->wordSpacing() || m_expansion) && !m_run.spacingDisabled();
float widthSinceLastRounding = m_runWidthSoFar;
m_runWidthSoFar = floorf(m_runWidthSoFar);
widthSinceLastRounding -= m_runWidthSoFar;
float lastRoundingWidth = m_finalRoundingWidth;
FloatRect bounds;
const SimpleFontData* primaryFont = m_font->primaryFont();
const SimpleFontData* lastFontData = primaryFont;
while (currentCharacter < offset) {
UChar32 c = *cp;
unsigned clusterLength = 1;
if (c >= 0x3041) {
if (c <= 0x30FE) {
UChar32 normalized = normalizeVoicingMarks(currentCharacter);
if (normalized) {
c = normalized;
clusterLength = 2;
}
} else if (U16_IS_SURROGATE(c)) {
if (!U16_IS_SURROGATE_LEAD(c))
break;
if (currentCharacter + 1 >= m_run.length())
break;
UChar low = cp[1];
if (!U16_IS_TRAIL(low))
break;
c = U16_GET_SUPPLEMENTARY(c, low);
clusterLength = 2;
}
}
const GlyphData& glyphData = m_font->glyphDataForCharacter(c, rtl);
Glyph glyph = glyphData.glyph;
const SimpleFontData* fontData = glyphData.fontData;
ASSERT(fontData);
float width;
if (c == '\t' && m_run.allowTabs()) {
float tabWidth = m_font->tabWidth(*fontData);
width = tabWidth - fmodf(m_run.xPos() + m_runWidthSoFar + widthSinceLastRounding, tabWidth);
} else {
width = fontData->widthForGlyph(glyph);
#if ENABLE(SVG)
width *= m_run.horizontalGlyphStretch();
#endif
if (m_run.applyWordRounding() && width == fontData->spaceWidth() && (fontData->pitch() == FixedPitch || glyph == fontData->spaceGlyph()))
width = fontData->adjustedSpaceWidth();
}
if (fontData != lastFontData && width) {
lastFontData = fontData;
if (m_fallbackFonts && fontData != primaryFont) {
if (!m_font->isSmallCaps() || c == toUpper(c))
m_fallbackFonts->add(fontData);
else {
const GlyphData& uppercaseGlyphData = m_font->glyphDataForCharacter(toUpper(c), rtl);
if (uppercaseGlyphData.fontData != primaryFont)
m_fallbackFonts->add(uppercaseGlyphData.fontData);
}
}
}
if (hasExtraSpacing) {
if (width && m_font->letterSpacing())
width += m_font->letterSpacing();
static bool expandAroundIdeographs = Font::canExpandAroundIdeographsInComplexText();
bool treatAsSpace = Font::treatAsSpace(c);
if (treatAsSpace || (expandAroundIdeographs && Font::isCJKIdeographOrSymbol(c))) {
if (m_expansion) {
float previousExpansion = m_expansion;
if (!treatAsSpace && !m_isAfterExpansion) {
m_expansion -= m_expansionPerOpportunity;
float expansionAtThisOpportunity = !m_run.applyWordRounding() ? m_expansionPerOpportunity : roundf(previousExpansion) - roundf(m_expansion);
m_runWidthSoFar += expansionAtThisOpportunity;
if (glyphBuffer) {
if (glyphBuffer->isEmpty())
glyphBuffer->add(fontData->spaceGlyph(), fontData, expansionAtThisOpportunity);
else
glyphBuffer->expandLastAdvance(expansionAtThisOpportunity);
}
previousExpansion = m_expansion;
}
if (m_run.allowsTrailingExpansion() || (m_run.ltr() && currentCharacter + clusterLength < static_cast<size_t>(m_run.length()))
|| (m_run.rtl() && currentCharacter)) {
m_expansion -= m_expansionPerOpportunity;
width += !m_run.applyWordRounding() ? m_expansionPerOpportunity : roundf(previousExpansion) - roundf(m_expansion);
m_isAfterExpansion = true;
}
} else
m_isAfterExpansion = false;
if (treatAsSpace && currentCharacter && !Font::treatAsSpace(cp[-1]) && m_font->wordSpacing())
width += m_font->wordSpacing();
} else
m_isAfterExpansion = false;
}
if (m_accountForGlyphBounds) {
bounds = fontData->boundsForGlyph(glyph);
if (!currentCharacter)
m_firstGlyphOverflow = max<float>(0, -bounds.x());
}
if (m_forTextEmphasis && !Font::canReceiveTextEmphasis(c))
glyph = 0;
cp += clusterLength;
currentCharacter += clusterLength;
float oldWidth = width;
if (m_run.applyWordRounding() && Font::isRoundingHackCharacter(c)) {
width = ceilf(width);
m_runWidthSoFar += width;
ASSERT(!widthSinceLastRounding);
} else {
if ((m_run.applyWordRounding() && currentCharacter < m_run.length() && Font::isRoundingHackCharacter(*cp))
|| (m_run.applyRunRounding() && currentCharacter >= m_end)) {
float totalWidth = widthSinceLastRounding + width;
widthSinceLastRounding = ceilf(totalWidth);
width += widthSinceLastRounding - totalWidth;
m_runWidthSoFar += widthSinceLastRounding;
widthSinceLastRounding = 0;
} else
widthSinceLastRounding += width;
}
if (glyphBuffer)
glyphBuffer->add(glyph, fontData, (rtl ? oldWidth + lastRoundingWidth : width));
lastRoundingWidth = width - oldWidth;
if (m_accountForGlyphBounds) {
m_maxGlyphBoundingBoxY = max(m_maxGlyphBoundingBoxY, bounds.maxY());
m_minGlyphBoundingBoxY = min(m_minGlyphBoundingBoxY, bounds.y());
m_lastGlyphOverflow = max<float>(0, bounds.maxX() - width);
}
}
m_currentCharacter = currentCharacter;
m_runWidthSoFar += widthSinceLastRounding;
m_finalRoundingWidth = lastRoundingWidth;
}
bool WidthIterator::advanceOneCharacter(float& width, GlyphBuffer* glyphBuffer)
{
int oldSize = glyphBuffer->size();
advance(m_currentCharacter + 1, glyphBuffer);
float w = 0;
for (int i = oldSize; i < glyphBuffer->size(); ++i)
w += glyphBuffer->advanceAt(i);
width = w;
return glyphBuffer->size() > oldSize;
}
UChar32 WidthIterator::normalizeVoicingMarks(int currentCharacter)
{
if (currentCharacter + 1 < m_end) {
if (combiningClass(m_run[currentCharacter + 1]) == hiraganaKatakanaVoicingMarksCombiningClass) {
#if USE(ICU_UNICODE)
UChar normalizedCharacters[2] = { 0, 0 };
UErrorCode uStatus = U_ZERO_ERROR;
int32_t resultLength = unorm_normalize(m_run.data(currentCharacter), 2,
UNORM_NFC, UNORM_UNICODE_3_2, &normalizedCharacters[0], 2, &uStatus);
if (resultLength == 1 && uStatus == 0)
return normalizedCharacters[0];
#elif USE(QT4_UNICODE)
QString tmp(reinterpret_cast<const QChar*>(m_run.data(currentCharacter)), 2);
QString res = tmp.normalized(QString::NormalizationForm_C, QChar::Unicode_3_2);
if (res.length() == 1)
return res.at(0).unicode();
#endif
}
}
return 0;
}
}