ComplexTextControllerLinux.cpp [plain text]
#include "config.h"
#include "ComplexTextControllerLinux.h"
#include "Font.h"
#include "TextRun.h"
#include <unicode/normlzr.h>
namespace WebCore {
static int truncateFixedPointToInteger(HB_Fixed value)
{
return value >> 6;
}
ComplexTextController::ComplexTextController(const TextRun& run, unsigned startingX, unsigned startingY, const Font* font)
: m_font(font)
, m_run(getNormalizedTextRun(run, m_normalizedRun, m_normalizedBuffer))
, m_wordSpacingAdjustment(0)
, m_padding(0)
, m_padPerWordBreak(0)
, m_padError(0)
, m_letterSpacing(0)
{
memset(&m_item, 0, sizeof(m_item));
createGlyphArrays((m_run.length() + 2) * 2);
m_item.log_clusters = new unsigned short[m_run.length()];
m_item.face = 0;
m_item.font = allocHarfbuzzFont();
m_item.item.bidiLevel = m_run.rtl();
m_item.string = m_run.characters();
m_item.stringLength = m_run.length();
reset(startingX);
m_startingY = startingY;
}
ComplexTextController::~ComplexTextController()
{
fastFree(m_item.font);
deleteGlyphArrays();
delete[] m_item.log_clusters;
}
bool ComplexTextController::isWordBreak(unsigned index)
{
return index && isCodepointSpace(m_item.string[index]) && !isCodepointSpace(m_item.string[index - 1]);
}
int ComplexTextController::determineWordBreakSpacing(unsigned logClustersIndex)
{
int wordBreakSpacing = 0;
if (logClustersIndex < m_item.item.length
&& isWordBreak(m_item.item.pos + logClustersIndex)) {
wordBreakSpacing = m_wordSpacingAdjustment;
if (m_padding > 0) {
int toPad = roundf(m_padPerWordBreak + m_padError);
m_padError += m_padPerWordBreak - toPad;
if (m_padding < toPad)
toPad = m_padding;
m_padding -= toPad;
wordBreakSpacing += toPad;
}
}
return wordBreakSpacing;
}
void ComplexTextController::setPadding(int padding)
{
m_padding = padding;
if (!m_padding)
return;
unsigned numWordBreaks = 0;
for (unsigned i = 0; i < m_item.stringLength; i++) {
if (isWordBreak(i))
numWordBreaks++;
}
if (numWordBreaks)
m_padPerWordBreak = m_padding / numWordBreaks;
else
m_padPerWordBreak = 0;
}
void ComplexTextController::reset(unsigned offset)
{
m_indexOfNextScriptRun = 0;
m_offsetX = offset;
}
bool ComplexTextController::nextScriptRun()
{
m_item.string = m_run.characters();
if (!hb_utf16_script_run_next(0, &m_item.item, m_run.characters(), m_run.length(), &m_indexOfNextScriptRun))
return false;
m_currentFontData = m_font->glyphDataForCharacter(m_item.string[m_item.item.pos], false).fontData;
unsigned endOfRun;
for (endOfRun = 1; endOfRun < m_item.item.length; ++endOfRun) {
const SimpleFontData* nextFontData = m_font->glyphDataForCharacter(m_item.string[m_item.item.pos + endOfRun], false).fontData;
if (nextFontData != m_currentFontData)
break;
}
m_item.item.length = endOfRun;
m_indexOfNextScriptRun = m_item.item.pos + endOfRun;
setupFontForScriptRun();
shapeGlyphs();
setGlyphPositions(rtl());
return true;
}
float ComplexTextController::widthOfFullRun()
{
float widthSum = 0;
while (nextScriptRun())
widthSum += width();
return widthSum;
}
void ComplexTextController::setupFontForScriptRun()
{
FontDataVariant fontDataVariant = AutoVariant;
if (m_font->isSmallCaps() && u_islower(m_item.string[m_item.item.pos])) {
m_smallCapsString = String(m_run.data(m_item.item.pos), m_item.item.length);
m_smallCapsString.makeUpper();
m_item.string = m_smallCapsString.characters();
m_item.item.pos = 0;
fontDataVariant = SmallCapsVariant;
}
const FontData* fontData = m_font->glyphDataForCharacter(m_item.string[m_item.item.pos], false, fontDataVariant).fontData;
const FontPlatformData& platformData = fontData->fontDataForCharacter(' ')->platformData();
m_item.face = platformData.harfbuzzFace();
void* opaquePlatformData = const_cast<FontPlatformData*>(&platformData);
m_item.font->userData = opaquePlatformData;
int size = platformData.size();
m_item.font->x_ppem = size;
m_item.font->y_ppem = size;
const int devicePixelFraction = 64;
const int multiplyFor16Dot16 = 1 << 16;
int scale = devicePixelFraction * size * multiplyFor16Dot16 / platformData.emSizeInFontUnits();
m_item.font->x_scale = scale;
m_item.font->y_scale = scale;
}
HB_FontRec* ComplexTextController::allocHarfbuzzFont()
{
HB_FontRec* font = reinterpret_cast<HB_FontRec*>(fastMalloc(sizeof(HB_FontRec)));
memset(font, 0, sizeof(HB_FontRec));
font->klass = &harfbuzzSkiaClass;
font->userData = 0;
return font;
}
void ComplexTextController::deleteGlyphArrays()
{
delete[] m_item.glyphs;
delete[] m_item.attributes;
delete[] m_item.advances;
delete[] m_item.offsets;
delete[] m_glyphs16;
delete[] m_positions;
}
void ComplexTextController::createGlyphArrays(int size)
{
m_item.glyphs = new HB_Glyph[size];
m_item.attributes = new HB_GlyphAttributes[size];
m_item.advances = new HB_Fixed[size];
m_item.offsets = new HB_FixedPoint[size];
m_glyphs16 = new uint16_t[size];
m_positions = new SkPoint[size];
m_item.num_glyphs = size;
m_glyphsArrayCapacity = size; resetGlyphArrays();
}
void ComplexTextController::resetGlyphArrays()
{
int size = m_item.num_glyphs;
memset(m_item.glyphs, 0, size * sizeof(HB_Glyph));
memset(m_item.attributes, 0, size * sizeof(HB_GlyphAttributes));
memset(m_item.advances, 0, size * sizeof(HB_Fixed));
memset(m_item.offsets, 0, size * sizeof(HB_FixedPoint));
memset(m_glyphs16, 0, size * sizeof(uint16_t));
memset(m_positions, 0, size * sizeof(SkPoint));
}
void ComplexTextController::shapeGlyphs()
{
m_item.num_glyphs = m_glyphsArrayCapacity;
resetGlyphArrays();
while (!HB_ShapeItem(&m_item)) {
deleteGlyphArrays();
createGlyphArrays(m_item.num_glyphs + 1);
}
}
void ComplexTextController::setGlyphPositions(bool isRTL)
{
const double rtlFlip = isRTL ? -1 : 1;
double position = 0;
int logClustersIndex = 0;
for (size_t i = 0; i < m_item.num_glyphs; ++i) {
while (static_cast<unsigned>(logClustersIndex) < m_item.item.length && logClusters()[logClustersIndex] < i)
logClustersIndex++;
position += determineWordBreakSpacing(logClustersIndex);
m_glyphs16[i] = m_item.glyphs[i];
double offsetX = truncateFixedPointToInteger(m_item.offsets[i].x);
double offsetY = truncateFixedPointToInteger(m_item.offsets[i].y);
double advance = truncateFixedPointToInteger(m_item.advances[i]);
if (isRTL)
offsetX -= advance;
m_positions[i].set(m_offsetX + (position * rtlFlip) + offsetX,
m_startingY + offsetY);
if (m_currentFontData->isZeroWidthSpaceGlyph(m_glyphs16[i]))
continue;
if (i + 1 == m_item.num_glyphs || m_item.attributes[i + 1].clusterStart)
position += m_letterSpacing;
position += advance;
}
m_pixelWidth = std::max(position, 0.0);
m_offsetX += m_pixelWidth * rtlFlip;
}
void ComplexTextController::normalizeSpacesAndMirrorChars(const UChar* source, bool rtl, UChar* destination, int length)
{
int position = 0;
bool error = false;
while (position < length) {
UChar32 character;
int nextPosition = position;
U16_NEXT(source, nextPosition, length, character);
if (Font::treatAsSpace(character))
character = ' ';
else if (Font::treatAsZeroWidthSpace(character))
character = zeroWidthSpace;
else if (rtl)
character = u_charMirror(character);
U16_APPEND(destination, position, length, character, error);
ASSERT(!error);
position = nextPosition;
}
}
const TextRun& ComplexTextController::getNormalizedTextRun(const TextRun& originalRun, OwnPtr<TextRun>& normalizedRun, OwnArrayPtr<UChar>& normalizedBuffer)
{
icu::UnicodeString normalizedString;
UErrorCode error = U_ZERO_ERROR;
for (int i = 0; i < originalRun.length(); ++i) {
UChar ch = originalRun[i];
if (::ublock_getCode(ch) == UBLOCK_COMBINING_DIACRITICAL_MARKS) {
icu::Normalizer::normalize(icu::UnicodeString(originalRun.characters(),
originalRun.length()), UNORM_NFC, 0 ,
normalizedString, error);
if (U_FAILURE(error))
return originalRun;
break;
}
}
int normalizedBufferLength;
const UChar* sourceText;
if (normalizedString.isEmpty()) {
normalizedBufferLength = originalRun.length();
sourceText = originalRun.characters();
} else {
normalizedBufferLength = normalizedString.length();
sourceText = normalizedString.getBuffer();
}
normalizedBuffer = adoptArrayPtr(new UChar[normalizedBufferLength + 1]);
normalizeSpacesAndMirrorChars(sourceText, originalRun.rtl(), normalizedBuffer.get(), normalizedBufferLength);
normalizedRun = adoptPtr(new TextRun(originalRun));
normalizedRun->setText(normalizedBuffer.get(), normalizedBufferLength);
return *normalizedRun;
}
}