#include "config.h"
#include "Font.h"
#include "ComplexTextControllerHarfBuzz.h"
#include "FloatRect.h"
#include "GlyphBuffer.h"
#include "GraphicsContext.h"
#include "HarfBuzzSkia.h"
#include "NotImplemented.h"
#include "PlatformContextSkia.h"
#include "SimpleFontData.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkTemplates.h"
#include "SkTypeface.h"
#include "SkUtils.h"
#include <wtf/unicode/Unicode.h>
namespace WebCore {
bool Font::canReturnFallbackFontsForComplexText()
{
return false;
}
bool Font::canExpandAroundIdeographsInComplexText()
{
return false;
}
void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font,
const GlyphBuffer& glyphBuffer, int from, int numGlyphs,
const FloatPoint& point) const {
SkASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t));
const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from);
SkScalar x = SkFloatToScalar(point.x());
SkScalar y = SkFloatToScalar(point.y());
const GlyphBufferAdvance* adv = glyphBuffer.advances(from);
SkAutoSTMalloc<32, SkPoint> storage(numGlyphs), storage2(numGlyphs), storage3(numGlyphs);
SkPoint* pos = storage.get();
SkPoint* vPosBegin = storage2.get();
SkPoint* vPosEnd = storage3.get();
bool isVertical = font->platformData().orientation() == Vertical;
for (int i = 0; i < numGlyphs; i++) {
SkScalar myWidth = SkFloatToScalar(adv[i].width());
pos[i].set(x, y);
if (isVertical) {
vPosBegin[i].set(x + myWidth, y);
vPosEnd[i].set(x + myWidth, y - myWidth);
}
x += myWidth;
y += SkFloatToScalar(adv[i].height());
}
SkCanvas* canvas = gc->platformContext()->canvas();
TextDrawingModeFlags textMode = gc->platformContext()->getTextDrawingMode();
if (textMode & TextModeFill) {
SkPaint paint;
gc->platformContext()->setupPaintForFilling(&paint);
font->platformData().setupPaint(&paint);
gc->platformContext()->adjustTextRenderMode(&paint);
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
if (isVertical) {
SkPath path;
for (int i = 0; i < numGlyphs; ++i) {
path.reset();
path.moveTo(vPosBegin[i]);
path.lineTo(vPosEnd[i]);
canvas->drawTextOnPath(glyphs + i, 2, path, 0, paint);
}
} else
canvas->drawPosText(glyphs, numGlyphs << 1, pos, paint);
}
if ((textMode & TextModeStroke)
&& gc->platformContext()->getStrokeStyle() != NoStroke
&& gc->platformContext()->getStrokeThickness() > 0) {
SkPaint paint;
gc->platformContext()->setupPaintForStroking(&paint, 0, 0);
font->platformData().setupPaint(&paint);
gc->platformContext()->adjustTextRenderMode(&paint);
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
if (textMode & TextModeFill) {
paint.setLooper(0);
}
if (isVertical) {
SkPath path;
for (int i = 0; i < numGlyphs; ++i) {
path.reset();
path.moveTo(vPosBegin[i]);
path.lineTo(vPosEnd[i]);
canvas->drawTextOnPath(glyphs + i, 2, path, 0, paint);
}
} else
canvas->drawPosText(glyphs, numGlyphs << 1, pos, paint);
}
}
static void setupForTextPainting(SkPaint* paint, SkColor color)
{
paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
paint->setColor(color);
}
void Font::drawComplexText(GraphicsContext* gc, const TextRun& run,
const FloatPoint& point, int from, int to) const
{
if (!run.length())
return;
SkCanvas* canvas = gc->platformContext()->canvas();
TextDrawingModeFlags textMode = gc->platformContext()->getTextDrawingMode();
bool fill = textMode & TextModeFill;
bool stroke = (textMode & TextModeStroke)
&& gc->platformContext()->getStrokeStyle() != NoStroke
&& gc->platformContext()->getStrokeThickness() > 0;
if (!fill && !stroke)
return;
SkPaint strokePaint, fillPaint;
if (fill) {
gc->platformContext()->setupPaintForFilling(&fillPaint);
setupForTextPainting(&fillPaint, gc->fillColor().rgb());
}
if (stroke) {
gc->platformContext()->setupPaintForStroking(&strokePaint, 0, 0);
setupForTextPainting(&strokePaint, gc->strokeColor().rgb());
}
ComplexTextController controller(this, run, point.x(), point.y());
if (run.rtl())
controller.setupForRTL();
while (controller.nextScriptRun()) {
int fromGlyph, glyphLength;
controller.glyphsForRange(from, to, fromGlyph, glyphLength);
if (fromGlyph < 0 || glyphLength <= 0)
continue;
if (fill) {
controller.fontPlatformDataForScriptRun()->setupPaint(&fillPaint);
gc->platformContext()->adjustTextRenderMode(&fillPaint);
canvas->drawPosText(controller.glyphs() + fromGlyph, glyphLength << 1, controller.positions() + fromGlyph, fillPaint);
}
if (stroke) {
controller.fontPlatformDataForScriptRun()->setupPaint(&strokePaint);
gc->platformContext()->adjustTextRenderMode(&strokePaint);
canvas->drawPosText(controller.glyphs() + fromGlyph, glyphLength << 1, controller.positions() + fromGlyph, strokePaint);
}
}
}
void Font::drawEmphasisMarksForComplexText(GraphicsContext* , const TextRun& , const AtomicString& , const FloatPoint& , int , int ) const
{
notImplemented();
}
float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* , GlyphOverflow* ) const
{
ComplexTextController controller(this, run, 0, 0);
return controller.widthOfFullRun();
}
int Font::offsetForPositionForComplexText(const TextRun& run, float xFloat,
bool includePartialGlyphs) const
{
int targetX = static_cast<int>(xFloat);
ComplexTextController controller(this, run, 0, 0);
if (run.rtl())
controller.setupForRTL();
return controller.offsetForPosition(targetX);
}
FloatRect Font::selectionRectForComplexText(const TextRun& run,
const FloatPoint& point, int height,
int from, int to) const
{
ComplexTextController controller(this, run, 0, 0);
if (run.rtl())
controller.setupForRTL();
return controller.selectionRect(point, height, from, to);
}
}