1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
* Copyright (C) 2003, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) 2008 Holger Hans Peter Freyther
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "WidthIterator.h"
#include "Font.h"
#include "FontCascade.h"
#include "GlyphBuffer.h"
#include "Latin1TextIterator.h"
#include "SurrogatePairAwareTextIterator.h"
#include <wtf/MathExtras.h>
using namespace WTF;
using namespace Unicode;
namespace WebCore {
WidthIterator::WidthIterator(const FontCascade* font, const TextRun& run, HashSet<const Font*>* fallbackFonts, bool accountForGlyphBounds, bool forTextEmphasis)
: m_font(font)
, m_run(run)
, m_currentCharacter(0)
, m_runWidthSoFar(0)
, m_isAfterExpansion((run.expansionBehavior() & LeadingExpansionMask) == ForbidLeadingExpansion)
, m_finalRoundingWidth(0)
, m_fallbackFonts(fallbackFonts)
, m_accountForGlyphBounds(accountForGlyphBounds)
, m_enableKerning(font->enableKerning())
, m_requiresShaping(font->requiresShaping())
, m_forTextEmphasis(forTextEmphasis)
{
// If the padding is non-zero, count the number of spaces in the run
// and divide that by the padding for per space addition.
m_expansion = m_run.expansion();
if (!m_expansion)
m_expansionPerOpportunity = 0;
else {
unsigned expansionOpportunityCount = FontCascade::expansionOpportunityCount(m_run.text(), m_run.ltr() ? LTR : RTL, run.expansionBehavior()).first;
if (!expansionOpportunityCount)
m_expansionPerOpportunity = 0;
else
m_expansionPerOpportunity = m_expansion / expansionOpportunityCount;
}
}
GlyphData WidthIterator::glyphDataForCharacter(UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength, String& normalizedSpacesStringCache)
{
ASSERT(m_font);
#if ENABLE(SVG_FONTS)
if (TextRun::RenderingContext* renderingContext = m_run.renderingContext())
return renderingContext->glyphDataForCharacter(*m_font, *this, character, mirror, currentCharacter, advanceLength, normalizedSpacesStringCache);
#else
UNUSED_PARAM(currentCharacter);
UNUSED_PARAM(advanceLength);
UNUSED_PARAM(normalizedSpacesStringCache);
#endif
return m_font->glyphDataForCharacter(character, mirror);
}
struct OriginalAdvancesForCharacterTreatedAsSpace {
public:
OriginalAdvancesForCharacterTreatedAsSpace(bool isSpace, float advanceBefore, float advanceAt)
: characterIsSpace(isSpace)
, advanceBeforeCharacter(advanceBefore)
, advanceAtCharacter(advanceAt)
{
}
bool characterIsSpace;
float advanceBeforeCharacter;
float advanceAtCharacter;
};
static inline bool isSoftBankEmoji(UChar32 codepoint)
{
return codepoint >= 0xE001 && codepoint <= 0xE537;
}
inline auto WidthIterator::shouldApplyFontTransforms(const GlyphBuffer* glyphBuffer, int lastGlyphCount, UChar32 previousCharacter) const -> TransformsType
{
if (glyphBuffer && glyphBuffer->size() == (lastGlyphCount + 1) && isSoftBankEmoji(previousCharacter))
return TransformsType::Forced;
if (m_run.length() <= 1 || !(m_enableKerning || m_requiresShaping))
return TransformsType::None;
return TransformsType::NotForced;
}
inline float WidthIterator::applyFontTransforms(GlyphBuffer* glyphBuffer, bool ltr, int& lastGlyphCount, const Font* font, UChar32 previousCharacter, bool force, CharactersTreatedAsSpace& charactersTreatedAsSpace)
{
ASSERT_UNUSED(previousCharacter, shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, previousCharacter) != WidthIterator::TransformsType::None);
if (!glyphBuffer)
return 0;
int glyphBufferSize = glyphBuffer->size();
if (!force && glyphBufferSize <= lastGlyphCount + 1) {
lastGlyphCount = glyphBufferSize;
return 0;
}
GlyphBufferAdvance* advances = glyphBuffer->advances(0);
float widthDifference = 0;
for (int i = lastGlyphCount; i < glyphBufferSize; ++i)
widthDifference -= advances[i].width();
if (!ltr)
glyphBuffer->reverse(lastGlyphCount, glyphBufferSize - lastGlyphCount);
#if ENABLE(SVG_FONTS)
// We need to handle transforms on SVG fonts internally, since they are rendered internally.
if (font->isSVGFont()) {
// SVG font ligatures are handled during glyph selection, only kerning remaining.
if (run().renderingContext() && m_enableKerning) {
// FIXME: We could pass the necessary context down to this level so we can lazily create rendering contexts at this point.
// However, a larger refactoring of SVG fonts might necessary to sidestep this problem completely.
run().renderingContext()->applySVGKerning(font, *this, glyphBuffer, lastGlyphCount);
}
} else
#endif
font->applyTransforms(glyphBuffer->glyphs(lastGlyphCount), advances + lastGlyphCount, glyphBufferSize - lastGlyphCount, m_enableKerning, m_requiresShaping);
if (!ltr)
glyphBuffer->reverse(lastGlyphCount, glyphBufferSize - lastGlyphCount);
for (size_t i = 0; i < charactersTreatedAsSpace.size(); ++i) {
int spaceOffset = charactersTreatedAsSpace[i].first;
const OriginalAdvancesForCharacterTreatedAsSpace& originalAdvances = charactersTreatedAsSpace[i].second;
if (spaceOffset && !originalAdvances.characterIsSpace)
glyphBuffer->advances(spaceOffset - 1)->setWidth(originalAdvances.advanceBeforeCharacter);
glyphBuffer->advances(spaceOffset)->setWidth(originalAdvances.advanceAtCharacter);
}
charactersTreatedAsSpace.clear();
for (int i = lastGlyphCount; i < glyphBufferSize; ++i)
widthDifference += advances[i].width();
lastGlyphCount = glyphBufferSize;
return widthDifference;
}
static inline std::pair<bool, bool> expansionLocation(bool ideograph, bool treatAsSpace, bool ltr, bool isAfterExpansion, bool forbidLeadingExpansion, bool forbidTrailingExpansion, bool forceLeadingExpansion, bool forceTrailingExpansion)
{
bool expandLeft = ideograph;
bool expandRight = ideograph;
if (treatAsSpace) {
if (ltr)
expandRight = true;
else
expandLeft = true;
}
if (isAfterExpansion) {
if (ltr)
expandLeft = false;
else
expandRight = false;
}
ASSERT(!forbidLeadingExpansion || !forceLeadingExpansion);
ASSERT(!forbidTrailingExpansion || !forceTrailingExpansion);
if (forbidLeadingExpansion)
expandLeft = false;
if (forbidTrailingExpansion)
expandRight = false;
if (forceLeadingExpansion)
expandLeft = true;
if (forceTrailingExpansion)
expandRight = true;
return std::make_pair(expandLeft, expandRight);
}
template <typename TextIterator>
inline unsigned WidthIterator::advanceInternal(TextIterator& textIterator, GlyphBuffer* glyphBuffer)
{
bool rtl = m_run.rtl();
bool hasExtraSpacing = (m_font->letterSpacing() || m_font->wordSpacing() || m_expansion) && !m_run.spacingDisabled();
bool runForcesLeadingExpansion = (m_run.expansionBehavior() & LeadingExpansionMask) == ForceLeadingExpansion;
bool runForcesTrailingExpansion = (m_run.expansionBehavior() & TrailingExpansionMask) == ForceTrailingExpansion;
bool runForbidsLeadingExpansion = (m_run.expansionBehavior() & LeadingExpansionMask) == ForbidLeadingExpansion;
bool runForbidsTrailingExpansion = (m_run.expansionBehavior() & TrailingExpansionMask) == ForbidTrailingExpansion;
float widthSinceLastRounding = m_runWidthSoFar;
float leftoverJustificationWidth = 0;
m_runWidthSoFar = floorf(m_runWidthSoFar);
widthSinceLastRounding -= m_runWidthSoFar;
float lastRoundingWidth = m_finalRoundingWidth;
FloatRect bounds;
const Font& primaryFont = m_font->primaryFont();
const Font* lastFontData = &primaryFont;
int lastGlyphCount = glyphBuffer ? glyphBuffer->size() : 0;
UChar32 character = 0;
UChar32 previousCharacter = 0;
unsigned clusterLength = 0;
CharactersTreatedAsSpace charactersTreatedAsSpace;
String normalizedSpacesStringCache;
// We are iterating in string order, not glyph order. Compare this to ComplexTextController::adjustGlyphsAndAdvances()
while (textIterator.consume(character, clusterLength)) {
unsigned advanceLength = clusterLength;
int currentCharacter = textIterator.currentCharacter();
const GlyphData& glyphData = glyphDataForCharacter(character, rtl, currentCharacter, advanceLength, normalizedSpacesStringCache);
Glyph glyph = glyphData.glyph;
if (!glyph) {
textIterator.advance(advanceLength);
continue;
}
const Font* font = glyphData.font;
ASSERT(font);
// Now that we have a glyph and font data, get its width.
float width;
if (character == '\t' && m_run.allowTabs())
width = m_font->tabWidth(*font, m_run.tabSize(), m_run.xPos() + m_runWidthSoFar + widthSinceLastRounding);
else {
width = font->widthForGlyph(glyph);
// SVG uses horizontalGlyphStretch(), when textLength is used to stretch/squeeze text.
width *= m_run.horizontalGlyphStretch();
}
if (font != lastFontData && width) {
auto transformsType = shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, previousCharacter);
if (transformsType != TransformsType::None) {
m_runWidthSoFar += applyFontTransforms(glyphBuffer, m_run.ltr(), lastGlyphCount, lastFontData, previousCharacter, transformsType == TransformsType::Forced, charactersTreatedAsSpace);
if (glyphBuffer)
glyphBuffer->shrink(lastGlyphCount);
}
lastFontData = font;
if (m_fallbackFonts && font != &primaryFont) {
// FIXME: This does a little extra work that could be avoided if
// glyphDataForCharacter() returned whether it chose to use a small caps font.
if (!m_font->isSmallCaps() || character == u_toupper(character))
m_fallbackFonts->add(font);
else {
const GlyphData& uppercaseGlyphData = m_font->glyphDataForCharacter(u_toupper(character), rtl);
if (uppercaseGlyphData.font != &primaryFont)
m_fallbackFonts->add(uppercaseGlyphData.font);
}
}
}
if (hasExtraSpacing) {
// Account for letter-spacing.
if (width) {
width += m_font->letterSpacing();
width += leftoverJustificationWidth;
leftoverJustificationWidth = 0;
}
static bool expandAroundIdeographs = FontCascade::canExpandAroundIdeographsInComplexText();
bool treatAsSpace = FontCascade::treatAsSpace(character);
bool currentIsLastCharacter = currentCharacter + advanceLength == static_cast<size_t>(m_run.length());
bool forceLeadingExpansion = false; // On the left, regardless of m_run.ltr()
bool forceTrailingExpansion = false; // On the right, regardless of m_run.ltr()
bool forbidLeadingExpansion = false;
bool forbidTrailingExpansion = false;
if (runForcesLeadingExpansion)
forceLeadingExpansion = m_run.ltr() ? !currentCharacter : currentIsLastCharacter;
if (runForcesTrailingExpansion)
forceTrailingExpansion = m_run.ltr() ? currentIsLastCharacter : !currentCharacter;
if (runForbidsLeadingExpansion)
forbidLeadingExpansion = m_run.ltr() ? !currentCharacter : currentIsLastCharacter;
if (runForbidsTrailingExpansion)
forbidTrailingExpansion = m_run.ltr() ? currentIsLastCharacter : !currentCharacter;
bool ideograph = (expandAroundIdeographs && FontCascade::isCJKIdeographOrSymbol(character));
if (treatAsSpace || ideograph || forceLeadingExpansion || forceTrailingExpansion) {
// Distribute the run's total expansion evenly over all expansion opportunities in the run.
if (m_expansion) {
bool expandLeft, expandRight;
std::tie(expandLeft, expandRight) = expansionLocation(ideograph, treatAsSpace, m_run.ltr(), m_isAfterExpansion, forbidLeadingExpansion, forbidTrailingExpansion, forceLeadingExpansion, forceTrailingExpansion);
if (expandLeft) {
if (m_run.ltr()) {
// Increase previous width
m_expansion -= m_expansionPerOpportunity;
m_runWidthSoFar += m_expansionPerOpportunity;
if (glyphBuffer) {
if (glyphBuffer->isEmpty()) {
if (m_forTextEmphasis)
glyphBuffer->add(font->zeroWidthSpaceGlyph(), font, m_expansionPerOpportunity, currentCharacter);
else
glyphBuffer->add(font->spaceGlyph(), font, m_expansionPerOpportunity, currentCharacter);
} else
glyphBuffer->expandLastAdvance(m_expansionPerOpportunity);
}
} else {
// Increase next width
leftoverJustificationWidth += m_expansionPerOpportunity;
m_isAfterExpansion = true;
}
}
if (expandRight) {
m_expansion -= m_expansionPerOpportunity;
width += m_expansionPerOpportunity;
if (m_run.ltr())
m_isAfterExpansion = true;
}
} else
m_isAfterExpansion = false;
// Account for word spacing.
// We apply additional space between "words" by adding width to the space character.
if (treatAsSpace && (character != '\t' || !m_run.allowTabs()) && (currentCharacter || character == noBreakSpace) && m_font->wordSpacing())
width += m_font->wordSpacing();
} else
m_isAfterExpansion = false;
}
auto transformsType = shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, previousCharacter);
if (transformsType != TransformsType::None && glyphBuffer && FontCascade::treatAsSpace(character)) {
charactersTreatedAsSpace.append(std::make_pair(glyphBuffer->size(),
OriginalAdvancesForCharacterTreatedAsSpace(character == ' ', glyphBuffer->size() ? glyphBuffer->advanceAt(glyphBuffer->size() - 1).width() : 0, width)));
}
if (m_accountForGlyphBounds) {
bounds = font->boundsForGlyph(glyph);
if (!currentCharacter)
m_firstGlyphOverflow = std::max<float>(0, -bounds.x());
}
if (m_forTextEmphasis && !FontCascade::canReceiveTextEmphasis(character))
glyph = 0;
// Advance past the character we just dealt with.
textIterator.advance(advanceLength);
float oldWidth = width;
widthSinceLastRounding += width;
if (glyphBuffer)
glyphBuffer->add(glyph, font, (rtl ? oldWidth + lastRoundingWidth : width), currentCharacter);
lastRoundingWidth = width - oldWidth;
if (m_accountForGlyphBounds) {
m_maxGlyphBoundingBoxY = std::max(m_maxGlyphBoundingBoxY, bounds.maxY());
m_minGlyphBoundingBoxY = std::min(m_minGlyphBoundingBoxY, bounds.y());
m_lastGlyphOverflow = std::max<float>(0, bounds.maxX() - width);
}
previousCharacter = character;
}
if (leftoverJustificationWidth) {
if (m_forTextEmphasis)
glyphBuffer->add(lastFontData->zeroWidthSpaceGlyph(), lastFontData, leftoverJustificationWidth, m_run.length() - 1);
else
glyphBuffer->add(lastFontData->spaceGlyph(), lastFontData, leftoverJustificationWidth, m_run.length() - 1);
}
auto transformsType = shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, previousCharacter);
if (transformsType != TransformsType::None) {
m_runWidthSoFar += applyFontTransforms(glyphBuffer, m_run.ltr(), lastGlyphCount, lastFontData, previousCharacter, transformsType == TransformsType::Forced, charactersTreatedAsSpace);
if (glyphBuffer)
glyphBuffer->shrink(lastGlyphCount);
}
unsigned consumedCharacters = textIterator.currentCharacter() - m_currentCharacter;
m_currentCharacter = textIterator.currentCharacter();
m_runWidthSoFar += widthSinceLastRounding;
m_finalRoundingWidth = lastRoundingWidth;
return consumedCharacters;
}
unsigned WidthIterator::advance(int offset, GlyphBuffer* glyphBuffer)
{
int length = m_run.length();
if (offset > length)
offset = length;
if (m_currentCharacter >= static_cast<unsigned>(offset))
return 0;
if (m_run.is8Bit()) {
Latin1TextIterator textIterator(m_run.data8(m_currentCharacter), m_currentCharacter, offset, length);
return advanceInternal(textIterator, glyphBuffer);
}
SurrogatePairAwareTextIterator textIterator(m_run.data16(m_currentCharacter), m_currentCharacter, offset, length);
return advanceInternal(textIterator, glyphBuffer);
}
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();
width = w;
return glyphBuffer.size() > oldSize;
}
}
|