DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (66531295716a)

VCS Links

Line Code
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef WritingModes_h_
#define WritingModes_h_

#include <ostream>

#include "mozilla/ComputedStyle.h"
#include "mozilla/ComputedStyleInlines.h"

#include "nsRect.h"
#include "nsBidiUtils.h"

// It is the caller's responsibility to operate on logical-coordinate objects
// with matched writing modes. Failure to do so will be a runtime bug; the
// compiler can't catch it, but in debug mode, we'll throw an assertion.
// NOTE that in non-debug builds, a writing mode mismatch error will NOT be
// detected, yet the results will be nonsense (and may lead to further layout
// failures). Therefore, it is important to test (and fuzz-test) writing-mode
// support using debug builds.

// Methods in logical-coordinate classes that take another logical-coordinate
// object as a parameter should call CHECK_WRITING_MODE on it to verify that
// the writing modes match.
// (In some cases, there are internal (private) methods that don't do this;
// such methods should only be used by other methods that have already checked
// the writing modes.)
// The check ignores the StyleWritingMode_VERTICAL_SIDEWAYS and
// StyleWritingMode_TEXT_SIDEWAYS bit of writing mode, because
// this does not affect the interpretation of logical coordinates.

#define CHECK_WRITING_MODE(param)                                           \
  NS_ASSERTION(param.IgnoreSideways() == GetWritingMode().IgnoreSideways(), \
               "writing-mode mismatch")

namespace mozilla {

namespace widget {
struct IMENotification;
}  // namespace widget

// Physical axis constants.
enum PhysicalAxis { eAxisVertical = 0x0, eAxisHorizontal = 0x1 };

inline LogicalAxis GetOrthogonalAxis(LogicalAxis aAxis) {
  return aAxis == eLogicalAxisBlock ? eLogicalAxisInline : eLogicalAxisBlock;
}

inline bool IsInline(LogicalSide aSide) { return aSide & 0x2; }
inline bool IsBlock(LogicalSide aSide) { return !IsInline(aSide); }
inline bool IsEnd(LogicalSide aSide) { return aSide & 0x1; }
inline bool IsStart(LogicalSide aSide) { return !IsEnd(aSide); }

inline LogicalAxis GetAxis(LogicalSide aSide) {
  return IsInline(aSide) ? eLogicalAxisInline : eLogicalAxisBlock;
}

inline LogicalEdge GetEdge(LogicalSide aSide) {
  return IsEnd(aSide) ? eLogicalEdgeEnd : eLogicalEdgeStart;
}

inline LogicalEdge GetOppositeEdge(LogicalEdge aEdge) {
  // This relies on the only two LogicalEdge enum values being 0 and 1.
  return LogicalEdge(1 - aEdge);
}

inline LogicalSide MakeLogicalSide(LogicalAxis aAxis, LogicalEdge aEdge) {
  return LogicalSide((aAxis << 1) | aEdge);
}

inline LogicalSide GetOppositeSide(LogicalSide aSide) {
  return MakeLogicalSide(GetAxis(aSide), GetOppositeEdge(GetEdge(aSide)));
}

enum LogicalSideBits {
  eLogicalSideBitsNone = 0,
  eLogicalSideBitsBStart = 1 << eLogicalSideBStart,
  eLogicalSideBitsBEnd = 1 << eLogicalSideBEnd,
  eLogicalSideBitsIEnd = 1 << eLogicalSideIEnd,
  eLogicalSideBitsIStart = 1 << eLogicalSideIStart,
  eLogicalSideBitsBBoth = eLogicalSideBitsBStart | eLogicalSideBitsBEnd,
  eLogicalSideBitsIBoth = eLogicalSideBitsIStart | eLogicalSideBitsIEnd,
  eLogicalSideBitsAll = eLogicalSideBitsBBoth | eLogicalSideBitsIBoth
};

enum LineRelativeDir {
  eLineRelativeDirOver = eLogicalSideBStart,
  eLineRelativeDirUnder = eLogicalSideBEnd,
  eLineRelativeDirLeft = eLogicalSideIStart,
  eLineRelativeDirRight = eLogicalSideIEnd
};

/**
 * LogicalSides represents a set of logical sides.
 */
struct LogicalSides final {
  LogicalSides() : mBits(0) {}
  explicit LogicalSides(LogicalSideBits aSideBits) {
    MOZ_ASSERT((aSideBits & ~eLogicalSideBitsAll) == 0, "illegal side bits");
    mBits = aSideBits;
  }
  bool IsEmpty() const { return mBits == 0; }
  bool BStart() const { return mBits & eLogicalSideBitsBStart; }
  bool BEnd() const { return mBits & eLogicalSideBitsBEnd; }
  bool IStart() const { return mBits & eLogicalSideBitsIStart; }
  bool IEnd() const { return mBits & eLogicalSideBitsIEnd; }
  bool Contains(LogicalSideBits aSideBits) const {
    MOZ_ASSERT((aSideBits & ~eLogicalSideBitsAll) == 0, "illegal side bits");
    return (mBits & aSideBits) == aSideBits;
  }
  LogicalSides operator|(LogicalSides aOther) const {
    return LogicalSides(LogicalSideBits(mBits | aOther.mBits));
  }
  LogicalSides operator|(LogicalSideBits aSideBits) const {
    return *this | LogicalSides(aSideBits);
  }
  LogicalSides& operator|=(LogicalSides aOther) {
    mBits |= aOther.mBits;
    return *this;
  }
  LogicalSides& operator|=(LogicalSideBits aSideBits) {
    return *this |= LogicalSides(aSideBits);
  }
  bool operator==(LogicalSides aOther) const { return mBits == aOther.mBits; }
  bool operator!=(LogicalSides aOther) const { return !(*this == aOther); }

 private:
  uint8_t mBits;
};

/**
 * mozilla::WritingMode is an immutable class representing a
 * writing mode.
 *
 * It efficiently stores the writing mode and can rapidly compute
 * interesting things about it for use in layout.
 *
 * Writing modes are computed from the CSS 'direction',
 * 'writing-mode', and 'text-orientation' properties.
 * See CSS3 Writing Modes for more information
 *   http://www.w3.org/TR/css3-writing-modes/
 */
class WritingMode {
 public:
  /**
   * Absolute inline flow direction
   */
  enum InlineDir {
    eInlineLTR = 0x00,  // text flows horizontally left to right
    eInlineRTL = 0x02,  // text flows horizontally right to left
    eInlineTTB = 0x01,  // text flows vertically top to bottom
    eInlineBTT = 0x03,  // text flows vertically bottom to top
  };

  /**
   * Absolute block flow direction
   */
  enum BlockDir {
    eBlockTB = 0x00,  // horizontal lines stack top to bottom
    eBlockRL = 0x01,  // vertical lines stack right to left
    eBlockLR = 0x05,  // vertical lines stack left to right
  };

  /**
   * Line-relative (bidi-relative) inline flow direction
   */
  enum BidiDir {
    eBidiLTR = 0x00,  // inline flow matches bidi LTR text
    eBidiRTL = 0x10,  // inline flow matches bidi RTL text
  };

  /**
   * Unknown writing mode (should never actually be stored or used anywhere).
   */
  enum { eUnknownWritingMode = 0xff };

  /**
   * Return the absolute inline flow direction as an InlineDir
   */
  InlineDir GetInlineDir() const {
    return InlineDir(mWritingMode.bits & eInlineMask);
  }

  /**
   * Return the absolute block flow direction as a BlockDir
   */
  BlockDir GetBlockDir() const {
    return BlockDir(mWritingMode.bits & eBlockMask);
  }

  /**
   * Return the line-relative inline flow direction as a BidiDir
   */
  BidiDir GetBidiDir() const {
    return BidiDir((mWritingMode & StyleWritingMode_RTL).bits);
  }

  /**
   * Return true if the inline flow direction is against physical direction
   * (i.e. right-to-left or bottom-to-top).
   * This occurs when writing-mode is sideways-lr OR direction is rtl (but not
   * if both of those are true).
   */
  bool IsInlineReversed() const {
    return !!(mWritingMode & StyleWritingMode_INLINE_REVERSED);
  }

  /**
   * Return true if bidi direction is LTR. (Convenience method)
   */
  bool IsBidiLTR() const { return eBidiLTR == GetBidiDir(); }

  /**
   * Return true if bidi direction is RTL. (Convenience method)
   */
  bool IsBidiRTL() const { return eBidiRTL == GetBidiDir(); }

  /**
   * True if it is vertical and vertical-lr, or is horizontal and bidi LTR.
   */
  bool IsPhysicalLTR() const {
    return IsVertical() ? IsVerticalLR() : IsBidiLTR();
  }

  /**
   * True if it is vertical and vertical-rl, or is horizontal and bidi RTL.
   */
  bool IsPhysicalRTL() const {
    return IsVertical() ? IsVerticalRL() : IsBidiRTL();
  }

  /**
   * True if vertical-mode block direction is LR (convenience method).
   */
  bool IsVerticalLR() const { return eBlockLR == GetBlockDir(); }

  /**
   * True if vertical-mode block direction is RL (convenience method).
   */
  bool IsVerticalRL() const { return eBlockRL == GetBlockDir(); }

  /**
   * True if vertical writing mode, i.e. when
   * writing-mode: vertical-lr | vertical-rl.
   */
  bool IsVertical() const {
    return !!(mWritingMode & StyleWritingMode_VERTICAL);
  }

  /**
   * True if line-over/line-under are inverted from block-start/block-end.
   * This is true only when writing-mode is vertical-lr.
   */
  bool IsLineInverted() const {
    return !!(mWritingMode & StyleWritingMode_LINE_INVERTED);
  }

  /**
   * Block-axis flow-relative to line-relative factor.
   * May be used as a multiplication factor for block-axis coordinates
   * to convert between flow- and line-relative coordinate systems (e.g.
   * positioning an over- or under-line decoration).
   */
  int FlowRelativeToLineRelativeFactor() const {
    return IsLineInverted() ? -1 : 1;
  }

  /**
   * True if vertical sideways writing mode, i.e. when
   * writing-mode: sideways-lr | sideways-rl.
   */
  bool IsVerticalSideways() const {
    return !!(mWritingMode & StyleWritingMode_VERTICAL_SIDEWAYS);
  }

  /**
   * True if this is writing-mode: sideways-rl (convenience method).
   */
  bool IsSidewaysRL() const { return IsVerticalRL() && IsVerticalSideways(); }

  /**
   * True if this is writing-mode: sideways-lr (convenience method).
   */
  bool IsSidewaysLR() const { return IsVerticalLR() && IsVerticalSideways(); }

  /**
   * True if either text-orientation or writing-mode will force all text to be
   * rendered sideways in vertical lines, in which case we should prefer an
   * alphabetic baseline; otherwise, the default is centered.
   *
   * Note that some glyph runs may be rendered sideways even if this is false,
   * due to text-orientation:mixed resolution, but in that case the dominant
   * baseline remains centered.
   */
  bool IsSideways() const {
    return !!(mWritingMode & (StyleWritingMode_VERTICAL_SIDEWAYS |
                              StyleWritingMode_TEXT_SIDEWAYS));
  }

#ifdef DEBUG
  // Used by CHECK_WRITING_MODE to compare modes without regard for the
  // StyleWritingMode_VERTICAL_SIDEWAYS or StyleWritingMode_TEXT_SIDEWAYS flags.
  WritingMode IgnoreSideways() const {
    return WritingMode(
        mWritingMode.bits &
        ~(StyleWritingMode_VERTICAL_SIDEWAYS | StyleWritingMode_TEXT_SIDEWAYS)
             .bits);
  }
#endif

  /**
   * Return true if boxes with this writing mode should use central baselines.
   */
  bool IsCentralBaseline() const { return IsVertical() && !IsSideways(); }

  /**
   * Return true if boxes with this writing mode should use alphabetical
   * baselines.
   */
  bool IsAlphabeticalBaseline() const { return !IsCentralBaseline(); }

  static mozilla::PhysicalAxis PhysicalAxisForLogicalAxis(
      uint8_t aWritingModeValue, LogicalAxis aAxis) {
    // This relies on bit 0 of a writing-value mode indicating vertical
    // orientation and bit 0 of a LogicalAxis value indicating the inline axis,
    // so that it can correctly form mozilla::PhysicalAxis values using bit
    // manipulation.
    static_assert(NS_STYLE_WRITING_MODE_HORIZONTAL_TB == 0 &&
                      NS_STYLE_WRITING_MODE_VERTICAL_RL == 1 &&
                      NS_STYLE_WRITING_MODE_VERTICAL_LR == 3 &&
                      eLogicalAxisBlock == 0 && eLogicalAxisInline == 1 &&
                      eAxisVertical == 0 && eAxisHorizontal == 1,
                  "unexpected writing-mode, logical axis or physical axis "
                  "constant values");
    return mozilla::PhysicalAxis((aWritingModeValue ^ aAxis) & 0x1);
  }

  mozilla::PhysicalAxis PhysicalAxis(LogicalAxis aAxis) const {
    // This will set wm to either NS_STYLE_WRITING_MODE_HORIZONTAL_TB or
    // NS_STYLE_WRITING_MODE_VERTICAL_RL, and not the other two (real
    // and hypothetical) values.  But this is fine; we only need to
    // distinguish between vertical and horizontal in
    // PhysicalAxisForLogicalAxis.
    const auto wm = (mWritingMode & StyleWritingMode_VERTICAL).bits;
    return PhysicalAxisForLogicalAxis(wm, aAxis);
  }

  static mozilla::Side PhysicalSideForBlockAxis(uint8_t aWritingModeValue,
                                                LogicalEdge aEdge) {
    // indexes are NS_STYLE_WRITING_MODE_* values, which are the same as these
    // two-bit values:
    //   bit 0 = the StyleWritingMode_VERTICAL value
    //   bit 1 = the StyleWritingMode_VERTICAL_LR value
    static const mozilla::Side kLogicalBlockSides[][2] = {
        {eSideTop, eSideBottom},  // horizontal-tb
        {eSideRight, eSideLeft},  // vertical-rl
        {eSideBottom, eSideTop},  // (horizontal-bt)
        {eSideLeft, eSideRight},  // vertical-lr
    };

    // Ignore the SIDEWAYS_MASK bit of the writing-mode value, as this has no
    // effect on the side mappings.
    aWritingModeValue &= ~NS_STYLE_WRITING_MODE_SIDEWAYS_MASK;

    // What's left of the writing-mode should be in the range 0-3:
    NS_ASSERTION(aWritingModeValue < 4, "invalid aWritingModeValue value");

    return kLogicalBlockSides[aWritingModeValue][aEdge];
  }

  mozilla::Side PhysicalSideForInlineAxis(LogicalEdge aEdge) const {
    // indexes are four-bit values:
    //   bit 0 = the StyleWritingMode_VERTICAL value
    //   bit 1 = the StyleWritingMode_INLINE_REVERSED value
    //   bit 2 = the StyleWritingMode_VERTICAL_LR value
    //   bit 3 = the StyleWritingMode_LINE_INVERTED value
    // Not all of these combinations can actually be specified via CSS: there
    // is no horizontal-bt writing-mode, and no text-orientation value that
    // produces "inverted" text. (The former 'sideways-left' value, no longer
    // in the spec, would have produced this in vertical-rl mode.)
    static const mozilla::Side kLogicalInlineSides[][2] = {
        {eSideLeft, eSideRight},  // horizontal-tb               ltr
        {eSideTop, eSideBottom},  // vertical-rl                 ltr
        {eSideRight, eSideLeft},  // horizontal-tb               rtl
        {eSideBottom, eSideTop},  // vertical-rl                 rtl
        {eSideRight, eSideLeft},  // (horizontal-bt)  (inverted) ltr
        {eSideTop, eSideBottom},  // sideways-lr                 rtl
        {eSideLeft, eSideRight},  // (horizontal-bt)  (inverted) rtl
        {eSideBottom, eSideTop},  // sideways-lr                 ltr
        {eSideLeft, eSideRight},  // horizontal-tb    (inverted) rtl
        {eSideTop, eSideBottom},  // vertical-rl      (inverted) rtl
        {eSideRight, eSideLeft},  // horizontal-tb    (inverted) ltr
        {eSideBottom, eSideTop},  // vertical-rl      (inverted) ltr
        {eSideLeft, eSideRight},  // (horizontal-bt)             ltr
        {eSideTop, eSideBottom},  // vertical-lr                 ltr
        {eSideRight, eSideLeft},  // (horizontal-bt)             rtl
        {eSideBottom, eSideTop},  // vertical-lr                 rtl
    };

    // Inline axis sides depend on all three of writing-mode, text-orientation
    // and direction, which are encoded in the StyleWritingMode_VERTICAL,
    // StyleWritingMode_INLINE_REVERSED, StyleWritingMode_VERTICAL_LR and
    // StyleWritingMode_LINE_INVERTED bits.  Use these four bits to index into
    // kLogicalInlineSides.
    MOZ_ASSERT(StyleWritingMode_VERTICAL.bits == 0x01 &&
                   StyleWritingMode_INLINE_REVERSED.bits == 0x02 &&
                   StyleWritingMode_VERTICAL_LR.bits == 0x04 &&
                   StyleWritingMode_LINE_INVERTED.bits == 0x08,
               "unexpected mask values");
    int index = mWritingMode.bits & 0x0F;
    return kLogicalInlineSides[index][aEdge];
  }

  /**
   * Returns the physical side corresponding to the specified logical side,
   * given the current writing mode.
   */
  mozilla::Side PhysicalSide(LogicalSide aSide) const {
    if (IsBlock(aSide)) {
      MOZ_ASSERT(StyleWritingMode_VERTICAL.bits == 0x01 &&
                     StyleWritingMode_VERTICAL_LR.bits == 0x04,
                 "unexpected mask values");
      const auto wm = static_cast<uint8_t>(
          ((mWritingMode & StyleWritingMode_VERTICAL_LR).bits >> 1) |
          (mWritingMode & StyleWritingMode_VERTICAL).bits);
      return PhysicalSideForBlockAxis(wm, GetEdge(aSide));
    }

    return PhysicalSideForInlineAxis(GetEdge(aSide));
  }

  /**
   * Returns the logical side corresponding to the specified physical side,
   * given the current writing mode.
   * (This is the inverse of the PhysicalSide() method above.)
   */
  LogicalSide LogicalSideForPhysicalSide(mozilla::Side aSide) const {
    // clang-format off
    // indexes are four-bit values:
    //   bit 0 = the StyleWritingMode_VERTICAL value
    //   bit 1 = the StyleWritingMode_INLINE_REVERSED value
    //   bit 2 = the StyleWritingMode_VERTICAL_LR value
    //   bit 3 = the StyleWritingMode_LINE_INVERTED value
    static const LogicalSide kPhysicalToLogicalSides[][4] = {
      // top                right
      // bottom             left
      { eLogicalSideBStart, eLogicalSideIEnd,
        eLogicalSideBEnd,   eLogicalSideIStart },  // horizontal-tb         ltr
      { eLogicalSideIStart, eLogicalSideBStart,
        eLogicalSideIEnd,   eLogicalSideBEnd   },  // vertical-rl           ltr
      { eLogicalSideBStart, eLogicalSideIStart,
        eLogicalSideBEnd,   eLogicalSideIEnd   },  // horizontal-tb         rtl
      { eLogicalSideIEnd,   eLogicalSideBStart,
        eLogicalSideIStart, eLogicalSideBEnd   },  // vertical-rl           rtl
      { eLogicalSideBEnd,   eLogicalSideIStart,
        eLogicalSideBStart, eLogicalSideIEnd   },  // (horizontal-bt) (inv) ltr
      { eLogicalSideIStart, eLogicalSideBEnd,
        eLogicalSideIEnd,   eLogicalSideBStart },  // vertical-lr   sw-left rtl
      { eLogicalSideBEnd,   eLogicalSideIEnd,
        eLogicalSideBStart, eLogicalSideIStart },  // (horizontal-bt) (inv) rtl
      { eLogicalSideIEnd,   eLogicalSideBEnd,
        eLogicalSideIStart, eLogicalSideBStart },  // vertical-lr   sw-left ltr
      { eLogicalSideBStart, eLogicalSideIEnd,
        eLogicalSideBEnd,   eLogicalSideIStart },  // horizontal-tb   (inv) rtl
      { eLogicalSideIStart, eLogicalSideBStart,
        eLogicalSideIEnd,   eLogicalSideBEnd   },  // vertical-rl   sw-left rtl
      { eLogicalSideBStart, eLogicalSideIStart,
        eLogicalSideBEnd,   eLogicalSideIEnd   },  // horizontal-tb   (inv) ltr
      { eLogicalSideIEnd,   eLogicalSideBStart,
        eLogicalSideIStart, eLogicalSideBEnd   },  // vertical-rl   sw-left ltr
      { eLogicalSideBEnd,   eLogicalSideIEnd,
        eLogicalSideBStart, eLogicalSideIStart },  // (horizontal-bt)       ltr
      { eLogicalSideIStart, eLogicalSideBEnd,
        eLogicalSideIEnd,   eLogicalSideBStart },  // vertical-lr           ltr
      { eLogicalSideBEnd,   eLogicalSideIStart,
        eLogicalSideBStart, eLogicalSideIEnd   },  // (horizontal-bt)       rtl
      { eLogicalSideIEnd,   eLogicalSideBEnd,
        eLogicalSideIStart, eLogicalSideBStart },  // vertical-lr           rtl
    };
    // clang-format on

    MOZ_ASSERT(StyleWritingMode_VERTICAL.bits == 0x01 &&
                   StyleWritingMode_INLINE_REVERSED.bits == 0x02 &&
                   StyleWritingMode_VERTICAL_LR.bits == 0x04 &&
                   StyleWritingMode_LINE_INVERTED.bits == 0x08,
               "unexpected mask values");
    int index = mWritingMode.bits & 0x0F;
    return kPhysicalToLogicalSides[index][aSide];
  }

  /**
   * Returns the logical side corresponding to the specified
   * line-relative direction, given the current writing mode.
   */
  LogicalSide LogicalSideForLineRelativeDir(LineRelativeDir aDir) const {
    auto side = static_cast<LogicalSide>(aDir);
    if (IsInline(side)) {
      return IsBidiLTR() ? side : GetOppositeSide(side);
    }
    return !IsLineInverted() ? side : GetOppositeSide(side);
  }

  /**
   * Default constructor gives us a horizontal, LTR writing mode.
   * XXX We will probably eliminate this and require explicit initialization
   *     in all cases once transition is complete.
   */
  WritingMode() : mWritingMode{0} {}

  /**
   * Construct writing mode based on a ComputedStyle.
   */
  explicit WritingMode(const ComputedStyle* aComputedStyle) {
    NS_ASSERTION(aComputedStyle, "we need an ComputedStyle here");
    mWritingMode = aComputedStyle->WritingMode();
  }

  /**
   * This function performs fixup for elements with 'unicode-bidi: plaintext',
   * where inline directionality is derived from the Unicode bidi categories
   * of the element's content, and not the CSS 'direction' property.
   *
   * The WritingMode constructor will have already incorporated the 'direction'
   * property into our flag bits, so such elements need to use this method
   * (after resolving the bidi level of their content) to update the direction
   * bits as needed.
   *
   * If it turns out that our bidi direction already matches what plaintext
   * resolution determined, there's nothing to do here. If it didn't (i.e. if
   * the rtl-ness doesn't match), then we correct the direction by flipping the
   * same bits that get flipped in the constructor's CSS 'direction'-based
   * chunk.
   *
   * XXX change uint8_t to UBiDiLevel after bug 924851
   */
  void SetDirectionFromBidiLevel(uint8_t level) {
    if (IS_LEVEL_RTL(level) == IsBidiLTR()) {
      mWritingMode ^= StyleWritingMode_RTL | StyleWritingMode_INLINE_REVERSED;
    }
  }

  /**
   * Compare two WritingModes for equality.
   */
  bool operator==(const WritingMode& aOther) const {
    return mWritingMode == aOther.mWritingMode;
  }

  bool operator!=(const WritingMode& aOther) const {
    return mWritingMode != aOther.mWritingMode;
  }

  /**
   * Check whether two modes are orthogonal to each other.
   */
  bool IsOrthogonalTo(const WritingMode& aOther) const {
    return IsVertical() != aOther.IsVertical();
  }

  /**
   * Returns true if this WritingMode's aLogicalAxis has the same physical
   * start side as the parallel axis of WritingMode |aOther|.
   *
   * @param aLogicalAxis The axis to compare from this WritingMode.
   * @param aOther The other WritingMode (from which we'll choose the axis
   *               that's parallel to this WritingMode's aLogicalAxis, for
   *               comparison).
   */
  bool ParallelAxisStartsOnSameSide(LogicalAxis aLogicalAxis,
                                    const WritingMode& aOther) const {
    mozilla::Side myStartSide =
        this->PhysicalSide(MakeLogicalSide(aLogicalAxis, eLogicalEdgeStart));

    // Figure out which of aOther's axes is parallel to |this| WritingMode's
    // aLogicalAxis, and get its physical start side as well.
    LogicalAxis otherWMAxis = aOther.IsOrthogonalTo(*this)
                                  ? GetOrthogonalAxis(aLogicalAxis)
                                  : aLogicalAxis;
    mozilla::Side otherWMStartSide =
        aOther.PhysicalSide(MakeLogicalSide(otherWMAxis, eLogicalEdgeStart));

    NS_ASSERTION(myStartSide % 2 == otherWMStartSide % 2,
                 "Should end up with sides in the same physical axis");
    return myStartSide == otherWMStartSide;
  }

  uint8_t GetBits() const { return mWritingMode.bits; }

 private:
  friend class LogicalPoint;
  friend class LogicalSize;
  friend class LogicalMargin;
  friend class LogicalRect;

  friend struct IPC::ParamTraits<WritingMode>;
  // IMENotification cannot store this class directly since this has some
  // constructors.  Therefore, it stores mWritingMode and recreate the
  // instance from it.
  friend struct widget::IMENotification;

  /**
   * Return a WritingMode representing an unknown value.
   */
  static inline WritingMode Unknown() {
    return WritingMode(eUnknownWritingMode);
  }

  /**
   * Constructing a WritingMode with an arbitrary value is a private operation
   * currently only used by the Unknown() and IgnoreSideways() methods.
   */
  explicit WritingMode(uint8_t aValue) : mWritingMode{aValue} {}

  StyleWritingMode mWritingMode;

  enum Masks {
    // Masks for output enums
    eInlineMask = 0x03,  // VERTICAL | INLINE_REVERSED
    eBlockMask = 0x05,   // VERTICAL | VERTICAL_LR
  };
};

inline std::ostream& operator<<(std::ostream& aStream, const WritingMode& aWM) {
  return aStream
         << (aWM.IsVertical()
                 ? aWM.IsVerticalLR()
                       ? aWM.IsBidiLTR()
                             ? aWM.IsSideways() ? "sw-lr-ltr" : "v-lr-ltr"
                             : aWM.IsSideways() ? "sw-lr-rtl" : "v-lr-rtl"
                       : aWM.IsBidiLTR()
                             ? aWM.IsSideways() ? "sw-rl-ltr" : "v-rl-ltr"
                             : aWM.IsSideways() ? "sw-rl-rtl" : "v-rl-rtl"
                 : aWM.IsBidiLTR() ? "h-ltr" : "h-rtl");
}

/**
 * Logical-coordinate classes:
 *
 * There are three sets of coordinate space:
 *   - physical (top, left, bottom, right)
 *       relative to graphics coord system
 *   - flow-relative (block-start, inline-start, block-end, inline-end)
 *       relative to block/inline flow directions
 *   - line-relative (line-over, line-left, line-under, line-right)
 *       relative to glyph orientation / inline bidi directions
 * See CSS3 Writing Modes for more information
 *   http://www.w3.org/TR/css3-writing-modes/#abstract-box
 *
 * For shorthand, B represents the block-axis
 *                I represents the inline-axis
 *
 * The flow-relative geometric classes store coords in flow-relative space.
 * They use a private ns{Point,Size,Rect,Margin} member to store the actual
 * coordinate values, but reinterpret them as logical instead of physical.
 * This allows us to easily perform calculations in logical space (provided
 * writing modes of the operands match), by simply mapping to nsPoint (etc)
 * methods.
 *
 * Physical-coordinate accessors/setters are responsible to translate these
 * internal logical values as necessary.
 *
 * In DEBUG builds, the logical types store their WritingMode and check
 * that the same WritingMode is passed whenever callers ask them to do a
 * writing-mode-dependent operation. Non-DEBUG builds do NOT check this,
 * to avoid the overhead of storing WritingMode fields.
 *
 * Open question: do we need a different set optimized for line-relative
 * math, for use in nsLineLayout and the like? Or is multiplying values
 * by FlowRelativeToLineRelativeFactor() enough?
 */

/**
 * Flow-relative point
 */
class LogicalPoint {
 public:
  explicit LogicalPoint(WritingMode aWritingMode)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mPoint(0, 0) {
  }

  // Construct from a writing mode and individual coordinates (which MUST be
  // values in that writing mode, NOT physical coordinates!)
  LogicalPoint(WritingMode aWritingMode, nscoord aI, nscoord aB)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mPoint(aI, aB) {
  }

  // Construct from a writing mode and a physical point, within a given
  // containing rectangle's size (defining the conversion between LTR
  // and RTL coordinates, and between TTB and BTT coordinates).
  LogicalPoint(WritingMode aWritingMode, const nsPoint& aPoint,
               const nsSize& aContainerSize)
#ifdef DEBUG
      : mWritingMode(aWritingMode)
#endif
  {
    if (aWritingMode.IsVertical()) {
      I() = aWritingMode.IsInlineReversed() ? aContainerSize.height - aPoint.y
                                            : aPoint.y;
      B() = aWritingMode.IsVerticalLR() ? aPoint.x
                                        : aContainerSize.width - aPoint.x;
    } else {
      I() = aWritingMode.IsInlineReversed() ? aContainerSize.width - aPoint.x
                                            : aPoint.x;
      B() = aPoint.y;
    }
  }

  /**
   * Read-only (const) access to the logical coordinates.
   */
  nscoord I(WritingMode aWritingMode) const  // inline-axis
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mPoint.x;
  }
  nscoord B(WritingMode aWritingMode) const  // block-axis
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mPoint.y;
  }
  nscoord LineRelative(WritingMode aWritingMode,
                       const nsSize& aContainerSize) const  // line-axis
  {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsBidiLTR()) {
      return I();
    }
    return (aWritingMode.IsVertical() ? aContainerSize.height
                                      : aContainerSize.width) -
           I();
  }

  /**
   * These non-const accessors return a reference (lvalue) that can be
   * assigned to by callers.
   */
  nscoord& I(WritingMode aWritingMode)  // inline-axis
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mPoint.x;
  }
  nscoord& B(WritingMode aWritingMode)  // block-axis
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mPoint.y;
  }

  /**
   * Return a physical point corresponding to our logical coordinates,
   * converted according to our writing mode.
   */
  nsPoint GetPhysicalPoint(WritingMode aWritingMode,
                           const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return nsPoint(
          aWritingMode.IsVerticalLR() ? B() : aContainerSize.width - B(),
          aWritingMode.IsInlineReversed() ? aContainerSize.height - I() : I());
    } else {
      return nsPoint(
          aWritingMode.IsInlineReversed() ? aContainerSize.width - I() : I(),
          B());
    }
  }

  /**
   * Return the equivalent point in a different writing mode.
   */
  LogicalPoint ConvertTo(WritingMode aToMode, WritingMode aFromMode,
                         const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aFromMode);
    return aToMode == aFromMode
               ? *this
               : LogicalPoint(aToMode,
                              GetPhysicalPoint(aFromMode, aContainerSize),
                              aContainerSize);
  }

  bool operator==(const LogicalPoint& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return mPoint == aOther.mPoint;
  }

  bool operator!=(const LogicalPoint& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return mPoint != aOther.mPoint;
  }

  LogicalPoint operator+(const LogicalPoint& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    // In non-debug builds, LogicalPoint does not store the WritingMode,
    // so the first parameter here (which will always be eUnknownWritingMode)
    // is ignored.
    return LogicalPoint(GetWritingMode(), mPoint.x + aOther.mPoint.x,
                        mPoint.y + aOther.mPoint.y);
  }

  LogicalPoint& operator+=(const LogicalPoint& aOther) {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    I() += aOther.I();
    B() += aOther.B();
    return *this;
  }

  LogicalPoint operator-(const LogicalPoint& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    // In non-debug builds, LogicalPoint does not store the WritingMode,
    // so the first parameter here (which will always be eUnknownWritingMode)
    // is ignored.
    return LogicalPoint(GetWritingMode(), mPoint.x - aOther.mPoint.x,
                        mPoint.y - aOther.mPoint.y);
  }

  LogicalPoint& operator-=(const LogicalPoint& aOther) {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    I() -= aOther.I();
    B() -= aOther.B();
    return *this;
  }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const LogicalPoint& aPoint) {
    return aStream << aPoint.mPoint;
  }

 private:
  friend class LogicalRect;

  /**
   * NOTE that in non-DEBUG builds, GetWritingMode() always returns
   * eUnknownWritingMode, as the current mode is not stored in the logical-
   * geometry classes. Therefore, this method is private; it is used ONLY
   * by the DEBUG-mode checking macros in this class and its friends;
   * other code is not allowed to ask a logical point for its writing mode,
   * as this info will simply not be available in non-DEBUG builds.
   *
   * Also, in non-DEBUG builds, CHECK_WRITING_MODE does nothing, and the
   * WritingMode parameter to logical methods will generally be optimized
   * away altogether.
   */
#ifdef DEBUG
  WritingMode GetWritingMode() const { return mWritingMode; }
#else
  WritingMode GetWritingMode() const { return WritingMode::Unknown(); }
#endif

  // We don't allow construction of a LogicalPoint with no writing mode.
  LogicalPoint() = delete;

  // Accessors that don't take or check a WritingMode value.
  // These are for internal use only; they are called by methods that have
  // themselves already checked the WritingMode passed by the caller.
  nscoord I() const  // inline-axis
  {
    return mPoint.x;
  }
  nscoord B() const  // block-axis
  {
    return mPoint.y;
  }

  nscoord& I()  // inline-axis
  {
    return mPoint.x;
  }
  nscoord& B()  // block-axis
  {
    return mPoint.y;
  }

#ifdef DEBUG
  WritingMode mWritingMode;
#endif

  // We use an nsPoint to hold the coordinates, but reinterpret its .x and .y
  // fields as the inline and block directions. Hence, this is not exposed
  // directly, but only through accessors that will map them according to the
  // writing mode.
  nsPoint mPoint;
};

/**
 * Flow-relative size
 */
class LogicalSize {
 public:
  explicit LogicalSize(WritingMode aWritingMode)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mSize(0, 0) {
  }

  LogicalSize(WritingMode aWritingMode, nscoord aISize, nscoord aBSize)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mSize(aISize, aBSize) {
  }

  LogicalSize(WritingMode aWritingMode, const nsSize& aPhysicalSize)
#ifdef DEBUG
      : mWritingMode(aWritingMode)
#endif
  {
    if (aWritingMode.IsVertical()) {
      ISize() = aPhysicalSize.height;
      BSize() = aPhysicalSize.width;
    } else {
      ISize() = aPhysicalSize.width;
      BSize() = aPhysicalSize.height;
    }
  }

  void SizeTo(WritingMode aWritingMode, nscoord aISize, nscoord aBSize) {
    CHECK_WRITING_MODE(aWritingMode);
    mSize.SizeTo(aISize, aBSize);
  }

  /**
   * Dimensions in logical and physical terms
   */
  nscoord ISize(WritingMode aWritingMode) const  // inline-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mSize.width;
  }
  nscoord BSize(WritingMode aWritingMode) const  // block-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mSize.height;
  }
  nscoord Size(LogicalAxis aAxis, WritingMode aWM) const {
    return aAxis == eLogicalAxisInline ? ISize(aWM) : BSize(aWM);
  }

  nscoord Width(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? BSize() : ISize();
  }
  nscoord Height(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? ISize() : BSize();
  }

  /**
   * Writable references to the logical dimensions
   */
  nscoord& ISize(WritingMode aWritingMode)  // inline-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mSize.width;
  }
  nscoord& BSize(WritingMode aWritingMode)  // block-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mSize.height;
  }
  nscoord& Size(LogicalAxis aAxis, WritingMode aWM) {
    return aAxis == eLogicalAxisInline ? ISize(aWM) : BSize(aWM);
  }

  /**
   * Return an nsSize containing our physical dimensions
   */
  nsSize GetPhysicalSize(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? nsSize(BSize(), ISize())
                                     : nsSize(ISize(), BSize());
  }

  /**
   * Return a LogicalSize representing this size in a different writing mode
   */
  LogicalSize ConvertTo(WritingMode aToMode, WritingMode aFromMode) const {
#ifdef DEBUG
    // In DEBUG builds make sure to return a LogicalSize with the
    // expected writing mode
    CHECK_WRITING_MODE(aFromMode);
    return aToMode == aFromMode
               ? *this
               : LogicalSize(aToMode, GetPhysicalSize(aFromMode));
#else
    // optimization for non-DEBUG builds where LogicalSize doesn't store
    // the writing mode
    return (aToMode == aFromMode || !aToMode.IsOrthogonalTo(aFromMode))
               ? *this
               : LogicalSize(aToMode, BSize(), ISize());
#endif
  }

  /**
   * Test if a size is (0, 0).
   */
  bool IsAllZero() const { return ISize() == 0 && BSize() == 0; }

  /**
   * Various binary operators on LogicalSize. These are valid ONLY for operands
   * that share the same writing mode.
   */
  bool operator==(const LogicalSize& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return mSize == aOther.mSize;
  }

  bool operator!=(const LogicalSize& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return mSize != aOther.mSize;
  }

  LogicalSize operator+(const LogicalSize& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return LogicalSize(GetWritingMode(), ISize() + aOther.ISize(),
                       BSize() + aOther.BSize());
  }
  LogicalSize& operator+=(const LogicalSize& aOther) {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    ISize() += aOther.ISize();
    BSize() += aOther.BSize();
    return *this;
  }

  LogicalSize operator-(const LogicalSize& aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    return LogicalSize(GetWritingMode(), ISize() - aOther.ISize(),
                       BSize() - aOther.BSize());
  }
  LogicalSize& operator-=(const LogicalSize& aOther) {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    ISize() -= aOther.ISize();
    BSize() -= aOther.BSize();
    return *this;
  }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const LogicalSize& aSize) {
    return aStream << aSize.mSize;
  }

 private:
  friend class LogicalRect;

  LogicalSize() = delete;

#ifdef DEBUG
  WritingMode GetWritingMode() const { return mWritingMode; }
#else
  WritingMode GetWritingMode() const { return WritingMode::Unknown(); }
#endif

  nscoord ISize() const  // inline-size
  {
    return mSize.width;
  }
  nscoord BSize() const  // block-size
  {
    return mSize.height;
  }

  nscoord& ISize()  // inline-size
  {
    return mSize.width;
  }
  nscoord& BSize()  // block-size
  {
    return mSize.height;
  }

#ifdef DEBUG
  WritingMode mWritingMode;
#endif
  nsSize mSize;
};

/**
 * Flow-relative margin
 */
class LogicalMargin {
 public:
  explicit LogicalMargin(WritingMode aWritingMode)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mMargin(0, 0, 0, 0) {
  }

  LogicalMargin(WritingMode aWritingMode, nscoord aBStart, nscoord aIEnd,
                nscoord aBEnd, nscoord aIStart)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mMargin(aBStart, aIEnd, aBEnd, aIStart) {
  }

  LogicalMargin(WritingMode aWritingMode, const nsMargin& aPhysicalMargin)
#ifdef DEBUG
      : mWritingMode(aWritingMode)
#endif
  {
    if (aWritingMode.IsVertical()) {
      if (aWritingMode.IsVerticalLR()) {
        mMargin.top = aPhysicalMargin.left;
        mMargin.bottom = aPhysicalMargin.right;
      } else {
        mMargin.top = aPhysicalMargin.right;
        mMargin.bottom = aPhysicalMargin.left;
      }
      if (aWritingMode.IsInlineReversed()) {
        mMargin.left = aPhysicalMargin.bottom;
        mMargin.right = aPhysicalMargin.top;
      } else {
        mMargin.left = aPhysicalMargin.top;
        mMargin.right = aPhysicalMargin.bottom;
      }
    } else {
      mMargin.top = aPhysicalMargin.top;
      mMargin.bottom = aPhysicalMargin.bottom;
      if (aWritingMode.IsInlineReversed()) {
        mMargin.left = aPhysicalMargin.right;
        mMargin.right = aPhysicalMargin.left;
      } else {
        mMargin.left = aPhysicalMargin.left;
        mMargin.right = aPhysicalMargin.right;
      }
    }
  }

  nscoord IStart(WritingMode aWritingMode) const  // inline-start margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.left;
  }
  nscoord IEnd(WritingMode aWritingMode) const  // inline-end margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.right;
  }
  nscoord BStart(WritingMode aWritingMode) const  // block-start margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.top;
  }
  nscoord BEnd(WritingMode aWritingMode) const  // block-end margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.bottom;
  }
  nscoord Start(LogicalAxis aAxis, WritingMode aWM) const {
    return aAxis == eLogicalAxisInline ? IStart(aWM) : BStart(aWM);
  }
  nscoord End(LogicalAxis aAxis, WritingMode aWM) const {
    return aAxis == eLogicalAxisInline ? IEnd(aWM) : BEnd(aWM);
  }

  nscoord& IStart(WritingMode aWritingMode)  // inline-start margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.left;
  }
  nscoord& IEnd(WritingMode aWritingMode)  // inline-end margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.right;
  }
  nscoord& BStart(WritingMode aWritingMode)  // block-start margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.top;
  }
  nscoord& BEnd(WritingMode aWritingMode)  // block-end margin
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.bottom;
  }
  nscoord& Start(LogicalAxis aAxis, WritingMode aWM) {
    return aAxis == eLogicalAxisInline ? IStart(aWM) : BStart(aWM);
  }
  nscoord& End(LogicalAxis aAxis, WritingMode aWM) {
    return aAxis == eLogicalAxisInline ? IEnd(aWM) : BEnd(aWM);
  }

  nscoord IStartEnd(WritingMode aWritingMode) const  // inline margins
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.LeftRight();
  }
  nscoord BStartEnd(WritingMode aWritingMode) const  // block margins
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mMargin.TopBottom();
  }
  nscoord StartEnd(LogicalAxis aAxis, WritingMode aWM) const {
    return aAxis == eLogicalAxisInline ? IStartEnd(aWM) : BStartEnd(aWM);
  }

  /*
   * Return margin values for line-relative sides, as defined in
   * http://www.w3.org/TR/css-writing-modes-3/#line-directions:
   *
   * line-left
   *     Nominally the side from which LTR text would start.
   * line-right
   *     Nominally the side from which RTL text would start. (Opposite of
   *     line-left.)
   */
  nscoord LineLeft(WritingMode aWritingMode) const {
    // We don't need to CHECK_WRITING_MODE here because the IStart or IEnd
    // accessor that we call will do it.
    return aWritingMode.IsBidiLTR() ? IStart(aWritingMode) : IEnd(aWritingMode);
  }
  nscoord LineRight(WritingMode aWritingMode) const {
    return aWritingMode.IsBidiLTR() ? IEnd(aWritingMode) : IStart(aWritingMode);
  }

  /**
   * Return a LogicalSize representing the total size of the inline-
   * and block-dimension margins.
   */
  LogicalSize Size(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return LogicalSize(aWritingMode, IStartEnd(), BStartEnd());
  }

  /**
   * Accessors for physical margins, using our writing mode to convert from
   * logical values.
   */
  nscoord Top(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical()
               ? (aWritingMode.IsInlineReversed() ? IEnd() : IStart())
               : BStart();
  }

  nscoord Bottom(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical()
               ? (aWritingMode.IsInlineReversed() ? IStart() : IEnd())
               : BEnd();
  }

  nscoord Left(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical()
               ? (aWritingMode.IsVerticalLR() ? BStart() : BEnd())
               : (aWritingMode.IsInlineReversed() ? IEnd() : IStart());
  }

  nscoord Right(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical()
               ? (aWritingMode.IsVerticalLR() ? BEnd() : BStart())
               : (aWritingMode.IsInlineReversed() ? IStart() : IEnd());
  }

  nscoord LeftRight(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? BStartEnd() : IStartEnd();
  }

  nscoord TopBottom(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? IStartEnd() : BStartEnd();
  }

  void SizeTo(WritingMode aWritingMode, nscoord aBStart, nscoord aIEnd,
              nscoord aBEnd, nscoord aIStart) {
    CHECK_WRITING_MODE(aWritingMode);
    mMargin.SizeTo(aBStart, aIEnd, aBEnd, aIStart);
  }

  /**
   * Return an nsMargin containing our physical coordinates
   */
  nsMargin GetPhysicalMargin(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical()
               ? (aWritingMode.IsVerticalLR()
                      ? (aWritingMode.IsInlineReversed()
                             ? nsMargin(IEnd(), BEnd(), IStart(), BStart())
                             : nsMargin(IStart(), BEnd(), IEnd(), BStart()))
                      : (aWritingMode.IsInlineReversed()
                             ? nsMargin(IEnd(), BStart(), IStart(), BEnd())
                             : nsMargin(IStart(), BStart(), IEnd(), BEnd())))
               : (aWritingMode.IsInlineReversed()
                      ? nsMargin(BStart(), IStart(), BEnd(), IEnd())
                      : nsMargin(BStart(), IEnd(), BEnd(), IStart()));
  }

  /**
   * Return a LogicalMargin representing this margin in a different
   * writing mode
   */
  LogicalMargin ConvertTo(WritingMode aToMode, WritingMode aFromMode) const {
    CHECK_WRITING_MODE(aFromMode);
    return aToMode == aFromMode
               ? *this
               : LogicalMargin(aToMode, GetPhysicalMargin(aFromMode));
  }

  void ApplySkipSides(LogicalSides aSkipSides) {
    if (aSkipSides.BStart()) {
      BStart() = 0;
    }
    if (aSkipSides.BEnd()) {
      BEnd() = 0;
    }
    if (aSkipSides.IStart()) {
      IStart() = 0;
    }
    if (aSkipSides.IEnd()) {
      IEnd() = 0;
    }
  }

  bool IsAllZero() const {
    return (mMargin.left == 0 && mMargin.top == 0 && mMargin.right == 0 &&
            mMargin.bottom == 0);
  }

  LogicalMargin operator+(const LogicalMargin& aMargin) const {
    CHECK_WRITING_MODE(aMargin.GetWritingMode());
    return LogicalMargin(GetWritingMode(), BStart() + aMargin.BStart(),
                         IEnd() + aMargin.IEnd(), BEnd() + aMargin.BEnd(),
                         IStart() + aMargin.IStart());
  }

  LogicalMargin operator+=(const LogicalMargin& aMargin) {
    CHECK_WRITING_MODE(aMargin.GetWritingMode());
    mMargin += aMargin.mMargin;
    return *this;
  }

  LogicalMargin operator-(const LogicalMargin& aMargin) const {
    CHECK_WRITING_MODE(aMargin.GetWritingMode());
    return LogicalMargin(GetWritingMode(), BStart() - aMargin.BStart(),
                         IEnd() - aMargin.IEnd(), BEnd() - aMargin.BEnd(),
                         IStart() - aMargin.IStart());
  }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const LogicalMargin& aMargin) {
    return aStream << aMargin.mMargin;
  }

 private:
  friend class LogicalRect;

  LogicalMargin() = delete;

#ifdef DEBUG
  WritingMode GetWritingMode() const { return mWritingMode; }
#else
  WritingMode GetWritingMode() const { return WritingMode::Unknown(); }
#endif

  nscoord IStart() const  // inline-start margin
  {
    return mMargin.left;
  }
  nscoord IEnd() const  // inline-end margin
  {
    return mMargin.right;
  }
  nscoord BStart() const  // block-start margin
  {
    return mMargin.top;
  }
  nscoord BEnd() const  // block-end margin
  {
    return mMargin.bottom;
  }

  nscoord& IStart()  // inline-start margin
  {
    return mMargin.left;
  }
  nscoord& IEnd()  // inline-end margin
  {
    return mMargin.right;
  }
  nscoord& BStart()  // block-start margin
  {
    return mMargin.top;
  }
  nscoord& BEnd()  // block-end margin
  {
    return mMargin.bottom;
  }

  nscoord IStartEnd() const  // inline margins
  {
    return mMargin.LeftRight();
  }
  nscoord BStartEnd() const  // block margins
  {
    return mMargin.TopBottom();
  }

#ifdef DEBUG
  WritingMode mWritingMode;
#endif
  nsMargin mMargin;
};

/**
 * Flow-relative rectangle
 */
class LogicalRect {
 public:
  explicit LogicalRect(WritingMode aWritingMode)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mIStart(0),
        mBStart(0),
        mISize(0),
        mBSize(0) {
  }

  LogicalRect(WritingMode aWritingMode, nscoord aIStart, nscoord aBStart,
              nscoord aISize, nscoord aBSize)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mIStart(aIStart),
        mBStart(aBStart),
        mISize(aISize),
        mBSize(aBSize) {
  }

  LogicalRect(WritingMode aWritingMode, const LogicalPoint& aOrigin,
              const LogicalSize& aSize)
      :
#ifdef DEBUG
        mWritingMode(aWritingMode),
#endif
        mIStart(aOrigin.mPoint.x),
        mBStart(aOrigin.mPoint.y),
        mISize(aSize.mSize.width),
        mBSize(aSize.mSize.height) {
    CHECK_WRITING_MODE(aOrigin.GetWritingMode());
    CHECK_WRITING_MODE(aSize.GetWritingMode());
  }

  LogicalRect(WritingMode aWritingMode, const nsRect& aRect,
              const nsSize& aContainerSize)
#ifdef DEBUG
      : mWritingMode(aWritingMode)
#endif
  {
    if (aWritingMode.IsVertical()) {
      mBStart = aWritingMode.IsVerticalLR()
                    ? aRect.X()
                    : aContainerSize.width - aRect.XMost();
      mIStart = aWritingMode.IsInlineReversed()
                    ? aContainerSize.height - aRect.YMost()
                    : aRect.Y();
      mBSize = aRect.Width();
      mISize = aRect.Height();
    } else {
      mIStart = aWritingMode.IsInlineReversed()
                    ? aContainerSize.width - aRect.XMost()
                    : aRect.X();
      mBStart = aRect.Y();
      mISize = aRect.Width();
      mBSize = aRect.Height();
    }
  }

  /**
   * Inline- and block-dimension geometry.
   */
  nscoord IStart(WritingMode aWritingMode) const  // inline-start edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mIStart;
  }
  nscoord IEnd(WritingMode aWritingMode) const  // inline-end edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mIStart + mISize;
  }
  nscoord ISize(WritingMode aWritingMode) const  // inline-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mISize;
  }

  nscoord BStart(WritingMode aWritingMode) const  // block-start edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mBStart;
  }
  nscoord BEnd(WritingMode aWritingMode) const  // block-end edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mBStart + mBSize;
  }
  nscoord BSize(WritingMode aWritingMode) const  // block-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mBSize;
  }

  /**
   * Writable (reference) accessors are only available for the basic logical
   * fields (Start and Size), not derivatives like End.
   */
  nscoord& IStart(WritingMode aWritingMode)  // inline-start edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mIStart;
  }
  nscoord& ISize(WritingMode aWritingMode)  // inline-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mISize;
  }
  nscoord& BStart(WritingMode aWritingMode)  // block-start edge
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mBStart;
  }
  nscoord& BSize(WritingMode aWritingMode)  // block-size
  {
    CHECK_WRITING_MODE(aWritingMode);
    return mBSize;
  }

  /**
   * Accessors for line-relative coordinates
   */
  nscoord LineLeft(WritingMode aWritingMode,
                   const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsBidiLTR()) {
      return IStart();
    }
    nscoord containerISize = aWritingMode.IsVertical() ? aContainerSize.height
                                                       : aContainerSize.width;
    return containerISize - IEnd();
  }
  nscoord LineRight(WritingMode aWritingMode,
                    const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsBidiLTR()) {
      return IEnd();
    }
    nscoord containerISize = aWritingMode.IsVertical() ? aContainerSize.height
                                                       : aContainerSize.width;
    return containerISize - IStart();
  }

  /**
   * Physical coordinates of the rect.
   */
  nscoord X(WritingMode aWritingMode, nscoord aContainerWidth) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return aWritingMode.IsVerticalLR() ? mBStart : aContainerWidth - BEnd();
    }
    return aWritingMode.IsInlineReversed() ? aContainerWidth - IEnd() : mIStart;
  }

  nscoord Y(WritingMode aWritingMode, nscoord aContainerHeight) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return aWritingMode.IsInlineReversed() ? aContainerHeight - IEnd()
                                             : mIStart;
    }
    return mBStart;
  }

  nscoord Width(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? mBSize : mISize;
  }

  nscoord Height(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return aWritingMode.IsVertical() ? mISize : mBSize;
  }

  nscoord XMost(WritingMode aWritingMode, nscoord aContainerWidth) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return aWritingMode.IsVerticalLR() ? BEnd() : aContainerWidth - mBStart;
    }
    return aWritingMode.IsInlineReversed() ? aContainerWidth - mIStart : IEnd();
  }

  nscoord YMost(WritingMode aWritingMode, nscoord aContainerHeight) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return aWritingMode.IsInlineReversed() ? aContainerHeight - mIStart
                                             : IEnd();
    }
    return BEnd();
  }

  bool IsEmpty() const { return mISize <= 0 || mBSize <= 0; }

  bool IsAllZero() const {
    return (mIStart == 0 && mBStart == 0 && mISize == 0 && mBSize == 0);
  }

  bool IsZeroSize() const { return (mISize == 0 && mBSize == 0); }

  void SetEmpty() { mISize = mBSize = 0; }

  bool IsEqualEdges(const LogicalRect aOther) const {
    CHECK_WRITING_MODE(aOther.GetWritingMode());
    bool result = mIStart == aOther.mIStart && mBStart == aOther.mBStart &&
                  mISize == aOther.mISize && mBSize == aOther.mBSize;

    // We want the same result as nsRect, so assert we get it.
    MOZ_ASSERT(result ==
               nsRect(mIStart, mBStart, mISize, mBSize)
                   .IsEqualEdges(nsRect(aOther.mIStart, aOther.mBStart,
                                        aOther.mISize, aOther.mBSize)));
    return result;
  }

  LogicalPoint Origin(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return LogicalPoint(aWritingMode, IStart(), BStart());
  }
  void SetOrigin(WritingMode aWritingMode, const LogicalPoint& aPoint) {
    IStart(aWritingMode) = aPoint.I(aWritingMode);
    BStart(aWritingMode) = aPoint.B(aWritingMode);
  }

  LogicalSize Size(WritingMode aWritingMode) const {
    CHECK_WRITING_MODE(aWritingMode);
    return LogicalSize(aWritingMode, ISize(), BSize());
  }

  LogicalRect operator+(const LogicalPoint& aPoint) const {
    CHECK_WRITING_MODE(aPoint.GetWritingMode());
    return LogicalRect(GetWritingMode(), IStart() + aPoint.I(),
                       BStart() + aPoint.B(), ISize(), BSize());
  }

  LogicalRect& operator+=(const LogicalPoint& aPoint) {
    CHECK_WRITING_MODE(aPoint.GetWritingMode());
    mIStart += aPoint.mPoint.x;
    mBStart += aPoint.mPoint.y;
    return *this;
  }

  LogicalRect operator-(const LogicalPoint& aPoint) const {
    CHECK_WRITING_MODE(aPoint.GetWritingMode());
    return LogicalRect(GetWritingMode(), IStart() - aPoint.I(),
                       BStart() - aPoint.B(), ISize(), BSize());
  }

  LogicalRect& operator-=(const LogicalPoint& aPoint) {
    CHECK_WRITING_MODE(aPoint.GetWritingMode());
    mIStart -= aPoint.mPoint.x;
    mBStart -= aPoint.mPoint.y;
    return *this;
  }

  void MoveBy(WritingMode aWritingMode, const LogicalPoint& aDelta) {
    CHECK_WRITING_MODE(aWritingMode);
    CHECK_WRITING_MODE(aDelta.GetWritingMode());
    IStart() += aDelta.I();
    BStart() += aDelta.B();
  }

  void Inflate(nscoord aD) {
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Inflate(aD);
#endif
    mIStart -= aD;
    mBStart -= aD;
    mISize += 2 * aD;
    mBSize += 2 * aD;
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }
  void Inflate(nscoord aDI, nscoord aDB) {
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Inflate(aDI, aDB);
#endif
    mIStart -= aDI;
    mBStart -= aDB;
    mISize += 2 * aDI;
    mBSize += 2 * aDB;
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }
  void Inflate(WritingMode aWritingMode, const LogicalMargin& aMargin) {
    CHECK_WRITING_MODE(aWritingMode);
    CHECK_WRITING_MODE(aMargin.GetWritingMode());
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Inflate(aMargin.mMargin);
#endif
    mIStart -= aMargin.mMargin.left;
    mBStart -= aMargin.mMargin.top;
    mISize += aMargin.mMargin.LeftRight();
    mBSize += aMargin.mMargin.TopBottom();
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }

  void Deflate(nscoord aD) {
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Deflate(aD);
#endif
    mIStart += aD;
    mBStart += aD;
    mISize = std::max(0, mISize - 2 * aD);
    mBSize = std::max(0, mBSize - 2 * aD);
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }
  void Deflate(nscoord aDI, nscoord aDB) {
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Deflate(aDI, aDB);
#endif
    mIStart += aDI;
    mBStart += aDB;
    mISize = std::max(0, mISize - 2 * aDI);
    mBSize = std::max(0, mBSize - 2 * aDB);
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }
  void Deflate(WritingMode aWritingMode, const LogicalMargin& aMargin) {
    CHECK_WRITING_MODE(aWritingMode);
    CHECK_WRITING_MODE(aMargin.GetWritingMode());
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug(mIStart, mBStart, mISize, mBSize);
    rectDebug.Deflate(aMargin.mMargin);
#endif
    mIStart += aMargin.mMargin.left;
    mBStart += aMargin.mMargin.top;
    mISize = std::max(0, mISize - aMargin.mMargin.LeftRight());
    mBSize = std::max(0, mBSize - aMargin.mMargin.TopBottom());
    MOZ_ASSERT(
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
  }

  /**
   * Return an nsRect containing our physical coordinates within the given
   * container size.
   */
  nsRect GetPhysicalRect(WritingMode aWritingMode,
                         const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aWritingMode);
    if (aWritingMode.IsVertical()) {
      return nsRect(aWritingMode.IsVerticalLR() ? BStart()
                                                : aContainerSize.width - BEnd(),
                    aWritingMode.IsInlineReversed()
                        ? aContainerSize.height - IEnd()
                        : IStart(),
                    BSize(), ISize());
    } else {
      return nsRect(aWritingMode.IsInlineReversed()
                        ? aContainerSize.width - IEnd()
                        : IStart(),
                    BStart(), ISize(), BSize());
    }
  }

  /**
   * Return a LogicalRect representing this rect in a different writing mode
   */
  LogicalRect ConvertTo(WritingMode aToMode, WritingMode aFromMode,
                        const nsSize& aContainerSize) const {
    CHECK_WRITING_MODE(aFromMode);
    return aToMode == aFromMode
               ? *this
               : LogicalRect(aToMode,
                             GetPhysicalRect(aFromMode, aContainerSize),
                             aContainerSize);
  }

  /**
   * Set *this to be the rectangle containing the intersection of aRect1
   * and aRect2, return whether the intersection is non-empty.
   */
  bool IntersectRect(const LogicalRect& aRect1, const LogicalRect& aRect2) {
    CHECK_WRITING_MODE(aRect1.mWritingMode);
    CHECK_WRITING_MODE(aRect2.mWritingMode);
#ifdef DEBUG
    // Compute using nsRect and assert the results match
    nsRect rectDebug;
    rectDebug.IntersectRect(
        nsRect(aRect1.mIStart, aRect1.mBStart, aRect1.mISize, aRect1.mBSize),
        nsRect(aRect2.mIStart, aRect2.mBStart, aRect2.mISize, aRect2.mBSize));
#endif

    nscoord iEnd = std::min(aRect1.IEnd(), aRect2.IEnd());
    mIStart = std::max(aRect1.mIStart, aRect2.mIStart);
    mISize = iEnd - mIStart;

    nscoord bEnd = std::min(aRect1.BEnd(), aRect2.BEnd());
    mBStart = std::max(aRect1.mBStart, aRect2.mBStart);
    mBSize = bEnd - mBStart;

    if (mISize < 0 || mBSize < 0) {
      mISize = 0;
      mBSize = 0;
    }

    MOZ_ASSERT(
        (rectDebug.IsEmpty() && (mISize == 0 || mBSize == 0)) ||
        rectDebug.IsEqualEdges(nsRect(mIStart, mBStart, mISize, mBSize)));
    return mISize > 0 && mBSize > 0;
  }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const LogicalRect& aRect) {
    return aStream << '(' << aRect.IStart() << ',' << aRect.BStart() << ','
                   << aRect.ISize() << ',' << aRect.BSize() << ')';
  }

 private:
  LogicalRect() = delete;

#ifdef DEBUG
  WritingMode GetWritingMode() const { return mWritingMode; }
#else
  WritingMode GetWritingMode() const { return WritingMode::Unknown(); }
#endif

  nscoord IStart() const  // inline-start edge
  {
    return mIStart;
  }
  nscoord IEnd() const  // inline-end edge
  {
    return mIStart + mISize;
  }
  nscoord ISize() const  // inline-size
  {
    return mISize;
  }

  nscoord BStart() const  // block-start edge
  {
    return mBStart;
  }
  nscoord BEnd() const  // block-end edge
  {
    return mBStart + mBSize;
  }
  nscoord BSize() const  // block-size
  {
    return mBSize;
  }

  nscoord& IStart()  // inline-start edge
  {
    return mIStart;
  }
  nscoord& ISize()  // inline-size
  {
    return mISize;
  }
  nscoord& BStart()  // block-start edge
  {
    return mBStart;
  }
  nscoord& BSize()  // block-size
  {
    return mBSize;
  }

#ifdef DEBUG
  WritingMode mWritingMode;
#endif
  // Inline- and block-geometry dimension
  nscoord mIStart;  // inline-start edge
  nscoord mBStart;  // block-start edge
  nscoord mISize;   // inline-size
  nscoord mBSize;   // block-size
};

template <typename T>
const T& StyleRect<T>::Get(mozilla::WritingMode aWM,
                           mozilla::LogicalSide aSide) const {
  return Get(aWM.PhysicalSide(aSide));
}

template <typename T>
const T& StyleRect<T>::GetIStart(mozilla::WritingMode aWM) const {
  return Get(aWM, mozilla::eLogicalSideIStart);
}

template <typename T>
const T& StyleRect<T>::GetBStart(mozilla::WritingMode aWM) const {
  return Get(aWM, mozilla::eLogicalSideBStart);
}

template <typename T>
const T& StyleRect<T>::GetIEnd(mozilla::WritingMode aWM) const {
  return Get(aWM, mozilla::eLogicalSideIEnd);
}

template <typename T>
const T& StyleRect<T>::GetBEnd(mozilla::WritingMode aWM) const {
  return Get(aWM, mozilla::eLogicalSideBEnd);
}

}  // namespace mozilla

// Definitions of inline methods for nsStylePosition, declared in
// nsStyleStruct.h but not defined there because they need WritingMode.
inline const mozilla::StyleSize& nsStylePosition::ISize(WritingMode aWM) const {
  return aWM.IsVertical() ? mHeight : mWidth;
}
inline const mozilla::StyleSize& nsStylePosition::MinISize(
    WritingMode aWM) const {
  return aWM.IsVertical() ? mMinHeight : mMinWidth;
}
inline const mozilla::StyleMaxSize& nsStylePosition::MaxISize(
    WritingMode aWM) const {
  return aWM.IsVertical() ? mMaxHeight : mMaxWidth;
}
inline const mozilla::StyleSize& nsStylePosition::BSize(WritingMode aWM) const {
  return aWM.IsVertical() ? mWidth : mHeight;
}
inline const mozilla::StyleSize& nsStylePosition::MinBSize(
    WritingMode aWM) const {
  return aWM.IsVertical() ? mMinWidth : mMinHeight;
}
inline const mozilla::StyleMaxSize& nsStylePosition::MaxBSize(
    WritingMode aWM) const {
  return aWM.IsVertical() ? mMaxWidth : mMaxHeight;
}

inline bool nsStylePosition::ISizeDependsOnContainer(WritingMode aWM) const {
  const auto& iSize = ISize(aWM);
  return iSize.IsAuto() || ISizeCoordDependsOnContainer(iSize);
}
inline bool nsStylePosition::MinISizeDependsOnContainer(WritingMode aWM) const {
  // NOTE: For a flex item, "min-inline-size:auto" is supposed to behave like
  // "min-content", which does depend on the container, so you might think we'd
  // need a special case for "flex item && min-inline-size:auto" here. However,
  // we don't actually need that special-case code, because flex items are
  // explicitly supposed to *ignore* their min-inline-size (i.e. behave like
  // it's 0) until the flex container explicitly considers it. So -- since the
  // flex container doesn't rely on this method, we don't need to worry about
  // special behavior for flex items' "min-inline-size:auto" values here.
  return ISizeCoordDependsOnContainer(MinISize(aWM));
}
inline bool nsStylePosition::MaxISizeDependsOnContainer(WritingMode aWM) const {
  // NOTE: The comment above MinISizeDependsOnContainer about flex items
  // applies here, too.
  return ISizeCoordDependsOnContainer(MaxISize(aWM));
}
// Note that these functions count `auto` as depending on the container
// since that's the case for absolutely positioned elements.
// However, some callers do not care about this case and should check
// for it, since it is the most common case.
// FIXME: We should probably change the assumption to be the other way
// around.
inline bool nsStylePosition::BSizeDependsOnContainer(WritingMode aWM) const {
  const auto& bSize = BSize(aWM);
  return bSize.BehavesLikeInitialValueOnBlockAxis() ||
         BSizeCoordDependsOnContainer(bSize);
}
inline bool nsStylePosition::MinBSizeDependsOnContainer(WritingMode aWM) const {
  return BSizeCoordDependsOnContainer(MinBSize(aWM));
}
inline bool nsStylePosition::MaxBSizeDependsOnContainer(WritingMode aWM) const {
  return BSizeCoordDependsOnContainer(MaxBSize(aWM));
}

inline bool nsStyleMargin::HasBlockAxisAuto(mozilla::WritingMode aWM) const {
  return mMargin.GetBStart(aWM).IsAuto() || mMargin.GetBEnd(aWM).IsAuto();
}

inline bool nsStyleMargin::HasInlineAxisAuto(mozilla::WritingMode aWM) const {
  return mMargin.GetIStart(aWM).IsAuto() || mMargin.GetIEnd(aWM).IsAuto();
}

#endif  // WritingModes_h_