InlineTextBoxStyle.cpp [plain text]
#include "config.h"
#include "InlineTextBoxStyle.h"
#include "FontCascade.h"
#include "InlineTextBox.h"
#include "RootInlineBox.h"
namespace WebCore {
int computeUnderlineOffset(TextUnderlinePosition underlinePosition, const FontMetrics& fontMetrics, const InlineTextBox* inlineTextBox, int textDecorationThickness)
{
int gap = std::max<int>(1, ceilf(textDecorationThickness / 2.0));
TextUnderlinePosition resolvedUnderlinePosition = underlinePosition;
if (resolvedUnderlinePosition == TextUnderlinePositionAuto) {
if (inlineTextBox)
resolvedUnderlinePosition = inlineTextBox->root().baselineType() == IdeographicBaseline ? TextUnderlinePositionUnder : TextUnderlinePositionAlphabetic;
else
resolvedUnderlinePosition = TextUnderlinePositionAlphabetic;
}
switch (resolvedUnderlinePosition) {
case TextUnderlinePositionAlphabetic:
return fontMetrics.ascent() + gap;
case TextUnderlinePositionUnder: {
ASSERT(inlineTextBox);
const RootInlineBox& rootBox = inlineTextBox->root();
const RenderElement* decorationRenderer = inlineTextBox->parent()->renderer().enclosingRendererWithTextDecoration(TextDecorationUnderline, inlineTextBox->isFirstLine());
float offset;
if (inlineTextBox->renderer().style().isFlippedLinesWritingMode()) {
offset = inlineTextBox->logicalTop();
rootBox.minLogicalTopForTextDecorationLine(offset, decorationRenderer, TextDecorationUnderline);
offset = inlineTextBox->logicalTop() - offset;
} else {
offset = inlineTextBox->logicalBottom();
rootBox.maxLogicalBottomForTextDecorationLine(offset, decorationRenderer, TextDecorationUnderline);
offset -= inlineTextBox->logicalBottom();
}
return inlineTextBox->logicalHeight() + gap + std::max<float>(offset, 0);
}
case TextUnderlinePositionAuto:
ASSERT_NOT_REACHED();
}
ASSERT_NOT_REACHED();
return fontMetrics.ascent() + gap;
}
void getWavyStrokeParameters(float strokeThickness, float& controlPointDistance, float& step)
{
controlPointDistance = 3 * std::max<float>(2, strokeThickness);
step = 2 * std::max<float>(2, strokeThickness);
}
static inline void extendIntToFloat(int& extendMe, float extendTo)
{
extendMe = std::max(extendMe, static_cast<int>(ceilf(extendTo)));
}
GlyphOverflow visualOverflowForDecorations(const RenderStyle& lineStyle, const InlineTextBox* inlineTextBox)
{
ASSERT(!inlineTextBox || inlineTextBox->lineStyle() == lineStyle);
TextDecoration decoration = lineStyle.textDecorationsInEffect();
if (decoration == TextDecorationNone)
return GlyphOverflow();
float strokeThickness = textDecorationStrokeThickness(lineStyle.fontSize());
float controlPointDistance = 0;
float step;
float wavyOffset = 0;
TextDecorationStyle decorationStyle = lineStyle.textDecorationStyle();
float height = lineStyle.fontCascade().fontMetrics().floatHeight();
GlyphOverflow overflowResult;
if (decorationStyle == TextDecorationStyleWavy) {
getWavyStrokeParameters(strokeThickness, controlPointDistance, step);
wavyOffset = wavyOffsetFromDecoration();
overflowResult.left = strokeThickness;
overflowResult.right = strokeThickness;
}
if (decoration & TextDecorationUnderline) {
int underlineOffset = 1;
underlineOffset += computeUnderlineOffset(lineStyle.textUnderlinePosition(), lineStyle.fontMetrics(), inlineTextBox, strokeThickness);
if (decorationStyle == TextDecorationStyleWavy) {
extendIntToFloat(overflowResult.bottom, underlineOffset + wavyOffset + controlPointDistance + strokeThickness - height);
extendIntToFloat(overflowResult.top, -(underlineOffset + wavyOffset - controlPointDistance - strokeThickness));
} else {
extendIntToFloat(overflowResult.bottom, underlineOffset + strokeThickness - height);
extendIntToFloat(overflowResult.top, -underlineOffset);
}
}
if (decoration & TextDecorationOverline) {
if (decorationStyle == TextDecorationStyleWavy) {
extendIntToFloat(overflowResult.bottom, -wavyOffset + controlPointDistance + strokeThickness - height);
extendIntToFloat(overflowResult.top, wavyOffset + controlPointDistance + strokeThickness);
} else {
extendIntToFloat(overflowResult.bottom, strokeThickness - height);
}
}
if (decoration & TextDecorationLineThrough) {
float baseline = lineStyle.fontMetrics().floatAscent();
if (decorationStyle == TextDecorationStyleWavy) {
extendIntToFloat(overflowResult.bottom, 2 * baseline / 3 + controlPointDistance + strokeThickness - height);
extendIntToFloat(overflowResult.top, -(2 * baseline / 3 - controlPointDistance - strokeThickness));
} else {
extendIntToFloat(overflowResult.bottom, 2 * baseline / 3 + strokeThickness - height);
extendIntToFloat(overflowResult.top, -(2 * baseline / 3));
}
}
return overflowResult;
}
}