WebCoreTextRenderer.cpp [plain text]
#include "config.h"
#include "WebCoreTextRenderer.h"
#include "Font.h"
#include "FontCache.h"
#include "FontDescription.h"
#include "GraphicsContext.h"
#include "StringTruncator.h"
#include "TextRun.h"
namespace WebCore {
static bool shouldUseFontSmoothing = true;
static bool isOneLeftToRightRun(const TextRun& run)
{
for (int i = 0; i < run.length(); i++) {
UCharDirection direction = u_charDirection(run[i]);
if (direction == U_RIGHT_TO_LEFT || direction > U_OTHER_NEUTRAL)
return false;
}
return true;
}
static void doDrawTextAtPoint(GraphicsContext& context, const String& text, const IntPoint& point, const Font& font, const Color& color, int underlinedIndex)
{
FontCachePurgePreventer fontCachePurgePreventer;
TextRun run(text);
context.setFillColor(color, ColorSpaceDeviceRGB);
if (isOneLeftToRightRun(run))
font.drawText(&context, run, point);
else
context.drawBidiText(font, run, point);
if (underlinedIndex >= 0) {
ASSERT_WITH_SECURITY_IMPLICATION(underlinedIndex < static_cast<int>(text.length()));
int beforeWidth;
if (underlinedIndex > 0) {
TextRun beforeRun(StringView(text).substring(0, underlinedIndex));
beforeWidth = font.width(beforeRun);
} else
beforeWidth = 0;
TextRun underlinedRun(StringView(text).substring(underlinedIndex, 1));
int underlinedWidth = font.width(underlinedRun);
IntPoint underlinePoint(point);
underlinePoint.move(beforeWidth, 1);
context.setStrokeColor(color, ColorSpaceDeviceRGB);
context.drawLineForText(underlinePoint, underlinedWidth, false);
}
}
void WebCoreDrawDoubledTextAtPoint(GraphicsContext& context, const String& text, const IntPoint& point, const Font& font, const Color& topColor, const Color& bottomColor, int underlinedIndex)
{
context.save();
IntPoint textPos = point;
doDrawTextAtPoint(context, text, textPos, font, bottomColor, underlinedIndex);
textPos.move(0, -1);
doDrawTextAtPoint(context, text, textPos, font, topColor, underlinedIndex);
context.restore();
}
float WebCoreTextFloatWidth(const String& text, const Font& font)
{
FontCachePurgePreventer fontCachePurgePreventer;
return StringTruncator::width(text, font, StringTruncator::EnableRoundingHacks);
}
void WebCoreSetShouldUseFontSmoothing(bool smooth)
{
shouldUseFontSmoothing = smooth;
}
bool WebCoreShouldUseFontSmoothing()
{
return shouldUseFontSmoothing;
}
void WebCoreSetAlwaysUsesComplexTextCodePath(bool complex)
{
Font::setCodePath(complex ? Font::Complex : Font::Auto);
}
bool WebCoreAlwaysUsesComplexTextCodePath()
{
return Font::codePath() == Font::Complex;
}
}