Skip to content

Commit

Permalink
patch 5 - Add testcases for emHeights and fontBoundingBox where the f…
Browse files Browse the repository at this point in the history
…ont's typographic ascent+descent does not equal the em size.

Depends on D184608

Differential Revision: https://phabricator.services.mozilla.com/D184609

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1841692
gecko-commit: 90b19e0d07e5d2cd137f135b860f0357df90e338
gecko-reviewers: gfx-reviewers, lsalzman
  • Loading branch information
moz-wptsync-bot committed Aug 7, 2023
1 parent bb87427 commit 29bc632
Show file tree
Hide file tree
Showing 18 changed files with 516 additions and 24 deletions.
Binary file added fonts/CanvasTest-ascent256.ttf
Binary file not shown.
Binary file added fonts/CanvasTest-descent0.ttf
Binary file not shown.
45 changes: 45 additions & 0 deletions html/canvas/element/text/2d.text.measure.emHeights-low-ascent.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>Canvas test: 2d.text.measure.emHeights-low-ascent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<style>
@font-face {
font-family: CanvasTest-ascent256;
src: url("/fonts/CanvasTest-ascent256.ttf");
}
</style>
<body class="show_output">

<h1>2d.text.measure.emHeights-low-ascent</h1>
<p class="desc">Testing emHeights with reduced ascent metric</p>


<span style="font-family: CanvasTest-ascent256; position: absolute; visibility: hidden">A</span>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
promise_test(async t => {

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

await document.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 20, "ctx.measureText('A').emHeightAscent", "20");
_assertSame(ctx.measureText('A').emHeightDescent, 20, "ctx.measureText('A').emHeightDescent", "20");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 20, "ctx.measureText('ABCD').emHeightAscent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 20, "ctx.measureText('ABCD').emHeightDescent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");

}, "Testing emHeights with reduced ascent metric");
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>Canvas test: 2d.text.measure.emHeights-zero-descent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<style>
@font-face {
font-family: CanvasTest-descent0;
src: url("/fonts/CanvasTest-descent0.ttf");
}
</style>
<body class="show_output">

<h1>2d.text.measure.emHeights-zero-descent</h1>
<p class="desc">Testing emHeights with zero descent metric</p>


<span style="font-family: CanvasTest-descent0; position: absolute; visibility: hidden">A</span>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
promise_test(async t => {

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

await document.fonts.ready;
ctx.font = '40px CanvasTest-descent0';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightAscent", "40");
_assertSame(ctx.measureText('A').emHeightDescent, 0, "ctx.measureText('A').emHeightDescent", "0");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightAscent", "40");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 0, "ctx.measureText('ABCD').emHeightDescent", "0");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");

}, "Testing emHeights with zero descent metric");
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>Canvas test: 2d.text.measure.fontBoundingBox-reduced-ascent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<style>
@font-face {
font-family: CanvasTest-ascent256;
src: url("/fonts/CanvasTest-ascent256.ttf");
}
</style>
<body class="show_output">

<h1>2d.text.measure.fontBoundingBox-reduced-ascent</h1>
<p class="desc">Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric</p>


<span style="font-family: CanvasTest-ascent256; position: absolute; visibility: hidden">A</span>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
promise_test(async t => {

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

await document.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').fontBoundingBoxAscent, 10, "ctx.measureText('A').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('A').fontBoundingBoxDescent, 10, "ctx.measureText('A').fontBoundingBoxDescent", "10");

_assertSame(ctx.measureText('ABCD').fontBoundingBoxAscent, 10, "ctx.measureText('ABCD').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxDescent, 10, "ctx.measureText('ABCD').fontBoundingBoxDescent", "10");

}, "Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric");
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>Canvas test: 2d.text.measure.fontBoundingBox-zero-descent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<style>
@font-face {
font-family: CanvasTest-descent0;
src: url("/fonts/CanvasTest-descent0.ttf");
}
</style>
<body class="show_output">

<h1>2d.text.measure.fontBoundingBox-zero-descent</h1>
<p class="desc">Testing fontBoundingBox for OffscreenCanvas with zero descent metric</p>


<span style="font-family: CanvasTest-descent0; position: absolute; visibility: hidden">A</span>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
promise_test(async t => {

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

await document.fonts.ready;
ctx.font = '40px CanvasTest-descent0';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').fontBoundingBoxAscent, 30, "ctx.measureText('A').fontBoundingBoxAscent", "30");
_assertSame(ctx.measureText('A').fontBoundingBoxDescent, 0, "ctx.measureText('A').fontBoundingBoxDescent", "0");

_assertSame(ctx.measureText('ABCD').fontBoundingBoxAscent, 30, "ctx.measureText('ABCD').fontBoundingBoxAscent", "30");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxDescent, 0, "ctx.measureText('ABCD').fontBoundingBoxDescent", "0");

}, "Testing fontBoundingBox for OffscreenCanvas with zero descent metric");
</script>

11 changes: 5 additions & 6 deletions html/canvas/element/text/2d.text.measure.fontBoundingBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ <h1>2d.text.measure.fontBoundingBox</h1>
var ctx = canvas.getContext('2d');

await document.fonts.ready;
ctx.font = '50px CanvasTest';
ctx.font = '40px CanvasTest';
ctx.direction = 'ltr';
ctx.align = 'left'
// approx_equals because font metrics may be rounded slightly differently by different platforms/browsers.
assert_approx_equals(ctx.measureText('A').fontBoundingBoxAscent, 50 * 768 / 1024, 1, "unexpected fontBoundingBoxAscent");
assert_approx_equals(ctx.measureText('A').fontBoundingBoxDescent, 50 * 256 / 1024, 1, "unexpected fontBoundingBoxDescent");
_assertSame(ctx.measureText('A').fontBoundingBoxAscent, 30, "ctx.measureText('A').fontBoundingBoxAscent", "30");
_assertSame(ctx.measureText('A').fontBoundingBoxDescent, 10, "ctx.measureText('A').fontBoundingBoxDescent", "10");

assert_approx_equals(ctx.measureText('ABCD').fontBoundingBoxAscent, 50 * 768 / 1024, 1, "unexpected fontBoundingBoxAscent");
assert_approx_equals(ctx.measureText('ABCD').fontBoundingBoxDescent, 50 * 256 / 1024, 1, "unexpected fontBoundingBoxDescent");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxAscent, 30, "ctx.measureText('ABCD').fontBoundingBoxAscent", "30");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxDescent, 10, "ctx.measureText('ABCD').fontBoundingBoxDescent", "10");

}, "Testing fontBoundingBox measurements");
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>OffscreenCanvas test: 2d.text.measure.emHeights-low-ascent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>

<h1>2d.text.measure.emHeights-low-ascent</h1>
<p class="desc">Testing emHeights with reduced ascent metric</p>


<script>
promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-ascent256", "url('/fonts/CanvasTest-ascent256.ttf')");
f.load();
document.fonts.add(f);
await document.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 20, "ctx.measureText('A').emHeightAscent", "20");
_assertSame(ctx.measureText('A').emHeightDescent, 20, "ctx.measureText('A').emHeightDescent", "20");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 20, "ctx.measureText('ABCD').emHeightAscent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 20, "ctx.measureText('ABCD').emHeightDescent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");

}, "Testing emHeights with reduced ascent metric");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py.
// OffscreenCanvas test in a worker:2d.text.measure.emHeights-low-ascent
// Description:Testing emHeights with reduced ascent metric
// Note:

importScripts("/resources/testharness.js");
importScripts("/html/canvas/resources/canvas-tests.js");

promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-ascent256", "url('/fonts/CanvasTest-ascent256.ttf')");
f.load();
self.fonts.add(f);
await self.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 20, "ctx.measureText('A').emHeightAscent", "20");
_assertSame(ctx.measureText('A').emHeightDescent, 20, "ctx.measureText('A').emHeightDescent", "20");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 20, "ctx.measureText('ABCD').emHeightAscent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 20, "ctx.measureText('ABCD').emHeightDescent", "20");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");
t.done();
}, "Testing emHeights with reduced ascent metric");
done();
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>OffscreenCanvas test: 2d.text.measure.emHeights-zero-descent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>

<h1>2d.text.measure.emHeights-zero-descent</h1>
<p class="desc">Testing emHeights with zero descent metric</p>


<script>
promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-descent0", "url('/fonts/CanvasTest-descent0.ttf')");
f.load();
document.fonts.add(f);
await document.fonts.ready;
ctx.font = '40px CanvasTest-descent0';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightAscent", "40");
_assertSame(ctx.measureText('A').emHeightDescent, 0, "ctx.measureText('A').emHeightDescent", "0");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightAscent", "40");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 0, "ctx.measureText('ABCD').emHeightDescent", "0");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");

}, "Testing emHeights with zero descent metric");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py.
// OffscreenCanvas test in a worker:2d.text.measure.emHeights-zero-descent
// Description:Testing emHeights with zero descent metric
// Note:

importScripts("/resources/testharness.js");
importScripts("/html/canvas/resources/canvas-tests.js");

promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-descent0", "url('/fonts/CanvasTest-descent0.ttf')");
f.load();
self.fonts.add(f);
await self.fonts.ready;
ctx.font = '40px CanvasTest-descent0';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightAscent", "40");
_assertSame(ctx.measureText('A').emHeightDescent, 0, "ctx.measureText('A').emHeightDescent", "0");
_assertSame(ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent, 40, "ctx.measureText('A').emHeightDescent + ctx.measureText('A').emHeightAscent", "40");

_assertSame(ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightAscent", "40");
_assertSame(ctx.measureText('ABCD').emHeightDescent, 0, "ctx.measureText('ABCD').emHeightDescent", "0");
_assertSame(ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent, 40, "ctx.measureText('ABCD').emHeightDescent + ctx.measureText('ABCD').emHeightAscent", "40");
t.done();
}, "Testing emHeights with zero descent metric");
done();
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>OffscreenCanvas test: 2d.text.measure.fontBoundingBox-reduced-ascent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>

<h1>2d.text.measure.fontBoundingBox-reduced-ascent</h1>
<p class="desc">Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric</p>


<script>
promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-ascent256", "url('/fonts/CanvasTest-ascent256.ttf')");
f.load();
document.fonts.add(f);
await document.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').fontBoundingBoxAscent, 10, "ctx.measureText('A').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('A').fontBoundingBoxDescent, 10, "ctx.measureText('A').fontBoundingBoxDescent", "10");

_assertSame(ctx.measureText('ABCD').fontBoundingBoxAscent, 10, "ctx.measureText('ABCD').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxDescent, 10, "ctx.measureText('ABCD').fontBoundingBoxDescent", "10");

}, "Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py.
// OffscreenCanvas test in a worker:2d.text.measure.fontBoundingBox-reduced-ascent
// Description:Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric
// Note:

importScripts("/resources/testharness.js");
importScripts("/html/canvas/resources/canvas-tests.js");

promise_test(async t => {

var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');

var f = new FontFace("CanvasTest-ascent256", "url('/fonts/CanvasTest-ascent256.ttf')");
f.load();
self.fonts.add(f);
await self.fonts.ready;
ctx.font = '40px CanvasTest-ascent256';
ctx.direction = 'ltr';
ctx.align = 'left'
_assertSame(ctx.measureText('A').fontBoundingBoxAscent, 10, "ctx.measureText('A').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('A').fontBoundingBoxDescent, 10, "ctx.measureText('A').fontBoundingBoxDescent", "10");

_assertSame(ctx.measureText('ABCD').fontBoundingBoxAscent, 10, "ctx.measureText('ABCD').fontBoundingBoxAscent", "10");
_assertSame(ctx.measureText('ABCD').fontBoundingBoxDescent, 10, "ctx.measureText('ABCD').fontBoundingBoxDescent", "10");
t.done();
}, "Testing fontBoundingBox for OffscreenCanvas with reduced ascent metric");
done();
Loading

0 comments on commit 29bc632

Please sign in to comment.