haikuwebkit/LayoutTests/fast/canvas/canvas-measureText-2.html

69 lines
3.1 KiB
HTML
Raw Permalink Normal View History

Implement new TextMetrics, returned by canvas measureText() https://bugs.webkit.org/show_bug.cgi?id=82798 Source/WebCore: <rdar://problem/11159332> Patch by Arnaud Renevier <a.renevier@sisa.samsung.com> and Fujii Hironori <Hironori.Fujii@sony.com> on 2017-07-26 Reviewed by Dean Jackson. The specification: https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext Add new attributes to TextMetrics. Add a new method textOffset() of CanvasRenderingContext2D by extracting from drawTextInternal() to use the same horizontal and vertical offsets of a text in both drawTextInternal() and measureText(). Test: fast/canvas/canvas-measureText-2.html * html/TextMetrics.h: (WebCore::TextMetrics::actualBoundingBoxLeft): (WebCore::TextMetrics::setActualBoundingBoxLeft): (WebCore::TextMetrics::actualBoundingBoxRight): (WebCore::TextMetrics::setActualBoundingBoxRight): (WebCore::TextMetrics::fontBoundingBoxAscent): (WebCore::TextMetrics::setFontBoundingBoxAscent): (WebCore::TextMetrics::fontBoundingBoxDescent): (WebCore::TextMetrics::setFontBoundingBoxDescent): (WebCore::TextMetrics::actualBoundingBoxAscent): (WebCore::TextMetrics::setActualBoundingBoxAscent): (WebCore::TextMetrics::actualBoundingBoxDescent): (WebCore::TextMetrics::setActualBoundingBoxDescent): (WebCore::TextMetrics::emHeightAscent): (WebCore::TextMetrics::setEmHeightAscent): (WebCore::TextMetrics::emHeightDescent): (WebCore::TextMetrics::setEmHeightDescent): (WebCore::TextMetrics::hangingBaseline): (WebCore::TextMetrics::setHangingBaseline): (WebCore::TextMetrics::alphabeticBaseline): (WebCore::TextMetrics::setAlphabeticBaseline): (WebCore::TextMetrics::ideographicBaseline): (WebCore::TextMetrics::setIdeographicBaseline): Added getters and setters. (WebCore::TextMetrics::TextMetrics): Deleted. * html/TextMetrics.idl: Added new attributes. * html/canvas/CanvasRenderingContext2D.cpp: (WebCore::CanvasRenderingContext2D::FontProxy::fontMetrics): Changed the return value type to a const reference of FontMetrics not to copy it. (WebCore::CanvasRenderingContext2D::FontProxy::width): Added the second arguemnt of GlyphOverflow type. (WebCore::CanvasRenderingContext2D::measureText): Calculate and set the new attributes of TextMetrics. (WebCore::CanvasRenderingContext2D::textOffset): Extracted from drawTextInternal. (WebCore::CanvasRenderingContext2D::drawTextInternal): Removed the offset calculation code and call textOffset. * html/canvas/CanvasRenderingContext2D.h: Added the method declaration of textOffset. Change types of fontMetrics and width methods. * platform/graphics/cairo/FontCairoHarfbuzzNG.cpp: (WebCore::FontCascade::floatWidthForComplexText): Added a dummy implementation of calculating GlyphOverflow. LayoutTests: Patch by Arnaud Renevier <a.renevier@sisa.samsung.com> and Fujii Hironori <Hironori.Fujii@sony.com> on 2017-07-26 Reviewed by Dean Jackson. Create a test that checks that: - ascent + descent is greater than zero - actualBoundingBoxLeft + actualBoundingBoxRight is somewhere quite close to width - when baseline is top, emHeightAscent is 0 (respectively bottom/emHeightDescent) - when baseline is hanging, hangingBaseline in 0 (respectively alphabetic and ideographic) - order of different vertical measures (for example, emHeightAscent is always higher that alphabeticBaseline) * fast/canvas/canvas-measureText-2-expected.txt: Added. * fast/canvas/canvas-measureText-2.html: Added. Canonical link: https://commits.webkit.org/191701@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219970 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-07-26 23:53:26 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<script src="../../resources/js-test-pre.js"></script>
<script>
var texts = ['Some simple text', 'དབུ་མེད་']; // tibetan script triggers complex path
var baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'];
var aligns = ['start', 'end', 'left', 'right', 'center'];
function tests() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.font = "14px sans-serif";
for (var i = 0; i < texts.length; i++) {
for (var j = 0; j < baselines.length; j++) {
for (var k = 0; k < aligns.length; k++) {
var text = texts[i];
var align = aligns[k];
var baseline = baselines[j];
debug('baseline=' + baseline + ' align=' + align + ' text="' + text + '"');
ctx.textBaseline = baseline;
ctx.textAlign = align;
metrics = ctx.measureText(text);
shouldBeCloseTo("metrics.actualBoundingBoxLeft + metrics.actualBoundingBoxRight - metrics.width", 0, 1);
shouldBeGreaterThanOrEqual("metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent", "0");
shouldBeGreaterThanOrEqual("metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent", "0");
shouldBeGreaterThanOrEqual("metrics.emHeightAscent + metrics.emHeightDescent", "0");
if (baseline === 'top')
shouldBeZero("Math.abs(metrics.emHeightAscent)");
if (baseline === 'bottom')
shouldBeZero("Math.abs(metrics.emHeightDescent)");
if (baseline === 'hanging')
shouldBeZero("Math.abs(metrics.hangingBaseline)");
if (baseline === 'alphabetic')
shouldBeZero("Math.abs(metrics.alphabeticBaseline)");
if (baseline === 'ideographic')
shouldBeZero("Math.abs(metrics.ideographicBaseline)");
shouldBeGreaterThanOrEqual("metrics.emHeightAscent", "metrics.hangingBaseline");
shouldBeGreaterThanOrEqual("metrics.hangingBaseline", "metrics.alphabeticBaseline");
shouldBeGreaterThanOrEqual("metrics.alphabeticBaseline", "metrics.ideographicBaseline");
shouldBeGreaterThanOrEqual("metrics.ideographicBaseline", "-metrics.emHeightDescent");
}
}
}
}
window.addEventListener('load', tests, true);
</script>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>