summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_text_lengthAdjust.html
blob: 9d1e007d00433d6a0938b56cb6ad1aadb62aeed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=569722
-->
<head>
  <meta charset=UTF-8>
  <title>Test for Bug 569722</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=569722">Mozilla Bug 569722</a>
<p id="display"></p>
<div id="content">
  <svg width="400" height="200">
    <text x="0" y="100" style="font: 16px sans-serif">aaa</text>
  </svg>
</div>

<pre id="test">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();

function close(x, y, message) {
  ok(Math.abs(x - y) < 1e-4, message);
}

function runTest() {
  var text = document.querySelector("text");

  // get the original length
  var length = text.getComputedTextLength();

  // get the original glyph positions
  var startPositions = [],
      endPositions = [],
      extents = [];
  for (var i = 0; i < 3; i++) {
    startPositions.push(text.getStartPositionOfChar(i));
    endPositions.push(text.getEndPositionOfChar(i));
    extents.push(text.getExtentOfChar(i));
  }

  // widths should all be the same
  is(extents[0].width, extents[1].width);
  is(extents[0].width, extents[2].width);

  var checkCharNumAtPosition = function(x, y, i) {
    var p = document.querySelector("svg").createSVGPoint();
    p.x = x;
    p.y = y;
    is(text.getCharNumAtPosition(p), i, "getCharNumAtPosition(" + i + ")");
  };

  var checkPositions = function(start, end, width) {
    for (var i = 0; i < 3; i++) {
      // check their positions
      close(text.getStartPositionOfChar(i).x, start[i], "start position of glyph " + i);
      close(text.getEndPositionOfChar(i).x, end[i], "end position of glyph " + i);
      close(text.getExtentOfChar(i).x, start[i], "left edge of extent of glyph " + i);
      close(text.getExtentOfChar(i).width, width, "width of glyph " + i);
      checkCharNumAtPosition((start[i] + end[i]) / 2, 100, i);
    }
  }

  var w = extents[0].width;

  var doLengthAdjustSpacingTest = function() {
    // getComputedTextLength should return the sum of the advances, and since
    // we are just changing the positions of the glyphs, it should be the same
    // as without a textLength="" attribute
    close(text.getComputedTextLength(), length, "getComputedTextLength when lengthAdjust=\"spacing\"");

    // expected start and end positions of the glyphs
    var start = [0, 50 - w / 2, 100 - w];
    var end = [w, 50 + w / 2, 100];
    checkPositions(start, end, w);
  };

  // switch to adjust glyph positions, using the default value of lengthAdjust=""
  text.setAttribute("textLength", "100");
  doLengthAdjustSpacingTest();
  // then with an explicit lengthAdjust="spacing"
  text.setAttribute("lengthAdjust", "spacing");
  doLengthAdjustSpacingTest();

  // now test with lengthAdjust="spacingAndGlyphs"
  text.setAttribute("lengthAdjust", "spacingAndGlyphs");

  // now that each glyph is stretched, the total advance should be the textLength
  close(text.getComputedTextLength(), 100, "getComputedTextLength when lengthAdjust=\"spacingAndGlyphs\"");

  // expected start and end positions of the glyphs
  var start = [0, 33.3333, 66.6666];
  var end = [33.3333, 66.6666, 100];
  checkPositions(start, end, 33.3333);

  SimpleTest.finish();
}

window.addEventListener("load", runTest, false);
</script>
</pre>
</body>
</html>