<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Subscripts and Superscripts metrics</title>
<link rel="help" href="https://w3c.github.io/mathml-core/#subscripts-and-superscripts-msub-msup-msubsup">
<meta name="assert" content="Basic metrics for the mmultiscript element with many scripts.">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/mathml/support/feature-detection.js"></script>
<script src="/mathml/support/fonts.js"></script>
<link rel="stylesheet" type="text/css" href="/fonts/ahem.css" />
<style>
  math, mspace {
    font: 25px/1 Ahem;
  }
</style>
<script>
  /* This test does not use a font with a MATH table and does not verify layout
     rules in a very strict way. */

  function getBox(aId) {
    var box = document.getElementById(aId).getBoundingClientRect();
    box.middle = (box.bottom + box.top) / 2;
    return box;
  }

  setup({ explicit_done: true });
  window.addEventListener("load", () => { loadAllFonts().then(runTests); });

  function runTests() {
    test(function() {
      assert_true(MathMLFeatureDetection.has_mspace());

      var e = 1;
      for (var i = 0; i < 5; i++)
        assert_approx_equals(getBox("multi" + i + "base").middle, getBox("baseline").bottom, e, "base " + i + "is placed on the baseline");
    }, "Alignment of the base on the baseline");

    test(function() {
      assert_true(MathMLFeatureDetection.has_mspace());

      var e = 5;
      assert_approx_equals(getBox("multi0").width, 30, e, "width of multi0");
      assert_approx_equals(getBox("multi0").height, 30, e, "height of multi0");
      assert_approx_equals(getBox("multi1").width, 30, e, "width of multi1");
      assert_approx_equals(getBox("multi1").height, 30, e, "height of multi1");
      for (i = 2; i <= 4; i++) {
        var scriptedBox = getBox("multi" + i);
        var lastPostScript = getBox("multi" + i + "postsup" + (i - 1));
        var firstPreScript = getBox("multi" + i + "presub1");
        assert_approx_equals(scriptedBox.height, firstPreScript.bottom - lastPostScript.top, e, "height of multiscript" + i);
        assert_approx_equals(scriptedBox.width, lastPostScript.right - firstPreScript.left, e, "width of multiscript" + i);
      }
    }, "Dimensions of the scripted elements");

    test(function() {
      assert_true(MathMLFeatureDetection.has_mspace());

      var e = 3;
      for (var i = 2; i <= 4; i++) {
        var base = getBox("multi" + i + "base");
        for (var j = 1; j < i; j++) {
          var presup = getBox("multi" + i + "presup" + j);
          var postsup = getBox("multi" + i + "postsup" + j);
          var presub = getBox("multi" + i + "presub" + j);
          var postsub = getBox("multi" + i + "postsub" + j);
          assert_approx_equals(base.top, presup.middle, e, "multi" + i + " " + j + " presup script");
          assert_approx_equals(base.top, postsup.middle, e, "multi" + i + " " + j + " postsup script");
          assert_approx_equals(base.bottom, presub.middle, e, "multi" + i + " " + j + " presub script");
          assert_approx_equals(base.bottom, postsub.middle, e, "multi" + i + " " + j + " postsub script");
        }
      }
    }, "Vertical positions of scripts");

    test(function() {
      assert_true(MathMLFeatureDetection.has_mspace());

      var e = 1;
      for (var i = 2; i <= 4; i++) {
        var base = getBox("multi" + i + "base");
        for (var j = 1; j < i; j++) {
          var presup = getBox("multi" + i + "presup" + j);
          var postsup = getBox("multi" + i + "postsup" + j);
          var presub = getBox("multi" + i + "presub" + j);
          var postsub = getBox("multi" + i + "postsub" + j);
          assert_approx_equals(presup.right, presub.right, e, "multi" + i + "pre");
          assert_approx_equals(postsup.left, postsub.left, e, "multi" + i + "post");
        }
      }
    }, "Horizontal alignment of scripts");

    test(function() {
      assert_true(MathMLFeatureDetection.has_mspace());

      for (var i = 2; i <= 4; i++) {
        var base = getBox("multi" + i + "base");
        var firstPostScript = getBox("multi" + i + "postsub1");
        var lastPreScript = getBox("multi" + i + "presup" + (i - 1));
        assert_less_than_equal(base.right, firstPostScript.left, 1, "postcripts are after base");
        assert_less_than_equal(lastPreScript.right, base.left, 1, "prescripts are before base");
        assert_approx_equals(base.right, firstPostScript.left, 5, "spacing after base is not too large");
        assert_approx_equals(lastPreScript.right, base.left, 5, "spacing before base is not too large");
        for (var j = 1; j < i - 1; j++) {
          var post = getBox("multi" + i + "postsub" + j);
          var postNext = getBox("multi" + i + "postsub" + (j + 1));
          var pre = getBox("multi" + i + "presup" + j);
          var preNext = getBox("multi" + i + "presup" + (j + 1));
          assert_less_than_equal(post.right, postNext.left, 1, "multi" + i + "order post" + j + " is before its successor");
          assert_less_than_equal(pre.right, preNext.left, 1, "multi" + i + "order pre" + j + " is before its successor");
          assert_approx_equals(post.right, postNext.left, 5, "multi" + i + "space after post" + j + " is not too large");
          assert_approx_equals(pre.right, preNext.left, 5, "multi" + i + "space after pre" + j + " is not too large");
        }
      }
    }, "Horizontal positions of scripts");

    done();
  }
</script>
</head>
<body>
  <div id="log"></div>
  <p>
    <math>
      <mspace id="baseline" width="30px" height="2px" depth="0px" style="background: blue"/>
      <mmultiscripts id="multi0" style="background: green">
        <mspace id="multi0base" width="30px" height="15px" depth="15px" style="background: black"/>
      </mmultiscripts>
      <mmultiscripts id="multi1" style="background: green">
        <mspace id="multi1base" width="30px" height="15px" depth="15px" style="background: black"/>
        <mprescripts/>
      </mmultiscripts>
      <mmultiscripts id="multi2" style="background: green">
        <mspace id="multi2base" width="30px" height="15px" depth="15px" style="background: black"/>
        <mspace id="multi2postsub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi2postsup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mprescripts/>
        <mspace id="multi2presub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi2presup1" width="10px" height="5px" depth="5px" style="background: black"/>
      </mmultiscripts>
      <mmultiscripts id="multi3" style="background: green">
        <mspace id="multi3base" width="30px" height="15px" depth="15px" style="background: black"/>
        <mspace id="multi3postsub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3postsup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3postsub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3postsup2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mprescripts/>
        <mspace id="multi3presub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3presup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3presub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi3presup2" width="10px" height="5px" depth="5px" style="background: black"/>
      </mmultiscripts>
      <mmultiscripts id="multi4" style="background: green">
        <mspace id="multi4base" width="30px" height="15px" depth="15px" style="background: black"/>
        <mspace id="multi4postsub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4postsup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4postsub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4postsup2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4postsub3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4postsup3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mprescripts/>
        <mspace id="multi4presub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4presup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4presub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4presup2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4presub3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi4presup3" width="10px" height="5px" depth="5px" style="background: black"/>
      </mmultiscripts>
      <mmultiscripts id="multi5" style="background: green">
        <mspace id="multi5base" width="30px" height="15px" depth="15px" style="background: black"/>
        <mspace id="multi5postsub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsup2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsub3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsup3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsub4" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5postsup4" width="10px" height="5px" depth="5px" style="background: black"/>
        <mprescripts/>
        <mspace id="multi5presub1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presup1" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presub2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presup2" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presub3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presup3" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presub4" width="10px" height="5px" depth="5px" style="background: black"/>
        <mspace id="multi5presup4" width="10px" height="5px" depth="5px" style="background: black"/>
      </mmultiscripts>
    </math>
  </p>
</body>
</html>
