#include "config.h"
#include "FontCascade.h"
#include "Font.h"
#include "GlyphBuffer.h"
#include "GraphicsContext.h"
#include "IntRect.h"
#include "LayoutRect.h"
#include "Logging.h"
#include "TextRun.h"
#include "UniscribeController.h"
#include <wtf/MathExtras.h>
using namespace std;
namespace WebCore {
bool FontCascade::canReturnFallbackFontsForComplexText()
{
return true;
}
bool FontCascade::canExpandAroundIdeographsInComplexText()
{
return false;
}
void FontCascade::adjustSelectionRectForComplexText(const TextRun& run, LayoutRect& selectionRect, unsigned from, unsigned to) const
{
UniscribeController it(this, run);
it.advance(from);
float beforeWidth = it.runWidthSoFar();
it.advance(to);
float afterWidth = it.runWidthSoFar();
if (run.rtl()) {
it.advance(run.length());
selectionRect.move(it.runWidthSoFar() - afterWidth, 0);
} else
selectionRect.move(beforeWidth, 0);
selectionRect.setWidth(afterWidth - beforeWidth);
}
float FontCascade::getGlyphsAndAdvancesForComplexText(const TextRun& run, unsigned from, unsigned to, GlyphBuffer& glyphBuffer, ForTextEmphasisOrNot forTextEmphasis) const
{
if (forTextEmphasis) {
LOG_ERROR("Not implemented for text emphasis.");
return 0;
}
UniscribeController controller(this, run);
controller.advance(from);
float beforeWidth = controller.runWidthSoFar();
controller.advance(to, &glyphBuffer);
if (glyphBuffer.isEmpty())
return 0;
float afterWidth = controller.runWidthSoFar();
if (run.rtl()) {
controller.advance(run.length());
return controller.runWidthSoFar() - afterWidth;
}
return beforeWidth;
}
void FontCascade::drawEmphasisMarksForComplexText(GraphicsContext& context, const TextRun& run, const AtomicString& mark, const FloatPoint& point, unsigned from, unsigned to) const
{
GlyphBuffer glyphBuffer;
float initialAdvance = getGlyphsAndAdvancesForComplexText(run, from, to, glyphBuffer, ForTextEmphasis);
if (glyphBuffer.isEmpty())
return;
drawEmphasisMarks(context, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
}
float FontCascade::floatWidthForComplexText(const TextRun& run, HashSet<const Font*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
UniscribeController controller(this, run, fallbackFonts);
controller.advance(run.length());
if (glyphOverflow) {
glyphOverflow->top = max<int>(glyphOverflow->top, ceilf(-controller.minGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().ascent()));
glyphOverflow->bottom = max<int>(glyphOverflow->bottom, ceilf(controller.maxGlyphBoundingBoxY()) - (glyphOverflow->computeBounds ? 0 : fontMetrics().descent()));
glyphOverflow->left = max<int>(0, ceilf(-controller.minGlyphBoundingBoxX()));
glyphOverflow->right = max<int>(0, ceilf(controller.maxGlyphBoundingBoxX() - controller.runWidthSoFar()));
}
return controller.runWidthSoFar();
}
int FontCascade::offsetForPositionForComplexText(const TextRun& run, float xFloat, bool includePartialGlyphs) const
{
int x = static_cast<int>(xFloat);
UniscribeController controller(this, run);
return controller.offsetForPosition(x, includePartialGlyphs);
}
}