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 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.UnsupportedAppUsage;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.text.LineBreaker;
import android.text.method.TextKeyListener;
import android.text.style.AlignmentSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
import android.text.style.LineBackgroundSpan;
import android.text.style.ParagraphStyle;
import android.text.style.ReplacementSpan;
import android.text.style.TabStopSpan;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
/**
* A base class that manages text layout in visual elements on
* the screen.
* <p>For text that will be edited, use a {@link DynamicLayout},
* which will be updated as the text changes.
* For text that will not change, use a {@link StaticLayout}.
*/
public abstract class Layout {
/** @hide */
@IntDef(prefix = { "BREAK_STRATEGY_" }, value = {
LineBreaker.BREAK_STRATEGY_SIMPLE,
LineBreaker.BREAK_STRATEGY_HIGH_QUALITY,
LineBreaker.BREAK_STRATEGY_BALANCED
})
@Retention(RetentionPolicy.SOURCE)
public @interface BreakStrategy {}
/**
* Value for break strategy indicating simple line breaking. Automatic hyphens are not added
* (though soft hyphens are respected), and modifying text generally doesn't affect the layout
* before it (which yields a more consistent user experience when editing), but layout may not
* be the highest quality.
*/
public static final int BREAK_STRATEGY_SIMPLE = LineBreaker.BREAK_STRATEGY_SIMPLE;
/**
* Value for break strategy indicating high quality line breaking, including automatic
* hyphenation and doing whole-paragraph optimization of line breaks.
*/
public static final int BREAK_STRATEGY_HIGH_QUALITY = LineBreaker.BREAK_STRATEGY_HIGH_QUALITY;
/**
* Value for break strategy indicating balanced line breaking. The breaks are chosen to
* make all lines as close to the same length as possible, including automatic hyphenation.
*/
public static final int BREAK_STRATEGY_BALANCED = LineBreaker.BREAK_STRATEGY_BALANCED;
/** @hide */
@IntDef(prefix = { "HYPHENATION_FREQUENCY_" }, value = {
HYPHENATION_FREQUENCY_NORMAL,
HYPHENATION_FREQUENCY_FULL,
HYPHENATION_FREQUENCY_NONE
})
@Retention(RetentionPolicy.SOURCE)
public @interface HyphenationFrequency {}
/**
* Value for hyphenation frequency indicating no automatic hyphenation. Useful
* for backward compatibility, and for cases where the automatic hyphenation algorithm results
* in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
* layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
* as suggestions for potential line breaks.
*/
public static final int HYPHENATION_FREQUENCY_NONE = LineBreaker.HYPHENATION_FREQUENCY_NONE;
/**
* Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
* is a conservative default. Useful for informal cases, such as short sentences or chat
* messages.
*/
public static final int HYPHENATION_FREQUENCY_NORMAL = LineBreaker.HYPHENATION_FREQUENCY_NORMAL;
/**
* Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
* in typography. Useful for running text and where it's important to put the maximum amount of
* text in a screen with limited space.
*/
public static final int HYPHENATION_FREQUENCY_FULL = LineBreaker.HYPHENATION_FREQUENCY_FULL;
private static final ParagraphStyle[] NO_PARA_SPANS =
ArrayUtils.emptyArray(ParagraphStyle.class);
/** @hide */
@IntDef(prefix = { "JUSTIFICATION_MODE_" }, value = {
LineBreaker.JUSTIFICATION_MODE_NONE,
LineBreaker.JUSTIFICATION_MODE_INTER_WORD
})
@Retention(RetentionPolicy.SOURCE)
public @interface JustificationMode {}
/**
* Value for justification mode indicating no justification.
*/
public static final int JUSTIFICATION_MODE_NONE = LineBreaker.JUSTIFICATION_MODE_NONE;
/**
* Value for justification mode indicating the text is justified by stretching word spacing.
*/
public static final int JUSTIFICATION_MODE_INTER_WORD =
LineBreaker.JUSTIFICATION_MODE_INTER_WORD;
/*
* Line spacing multiplier for default line spacing.
*/
public static final float DEFAULT_LINESPACING_MULTIPLIER = 1.0f;
/*
* Line spacing addition for default line spacing.
*/
public static final float DEFAULT_LINESPACING_ADDITION = 0.0f;
/**
* Return how wide a layout must be in order to display the specified text with one line per
* paragraph.
*
* <p>As of O, Uses
* {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
* the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
*/
public static float getDesiredWidth(CharSequence source,
TextPaint paint) {
return getDesiredWidth(source, 0, source.length(), paint);
}
/**
* Return how wide a layout must be in order to display the specified text slice with one
* line per paragraph.
*
* <p>As of O, Uses
* {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
* the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
*/
public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
}
/**
* Return how wide a layout must be in order to display the
* specified text slice with one line per paragraph.
*
* @hide
*/
public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
TextDirectionHeuristic textDir) {
return getDesiredWidthWithLimit(source, start, end, paint, textDir, Float.MAX_VALUE);
}
/**
* Return how wide a layout must be in order to display the
* specified text slice with one line per paragraph.
*
* If the measured width exceeds given limit, returns limit value instead.
* @hide
*/
public static float getDesiredWidthWithLimit(CharSequence source, int start, int end,
TextPaint paint, TextDirectionHeuristic textDir, float upperLimit) {
float need = 0;
int next;
for (int i = start; i <= end; i = next) {
next = TextUtils.indexOf(source, '\n', i, end);
if (next < 0)
next = end;
// note, omits trailing paragraph char
float w = measurePara(paint, source, i, next, textDir);
if (w > upperLimit) {
return upperLimit;
}
if (w > need)
need = w;
next++;
}
return need;
}
/**
* Subclasses of Layout use this constructor to set the display text,
* width, and other standard properties.
* @param text the text to render
* @param paint the default paint for the layout. Styles can override
* various attributes of the paint.
* @param width the wrapping width for the text.
* @param align whether to left, right, or center the text. Styles can
* override the alignment.
* @param spacingMult factor by which to scale the font size to get the
* default line spacing
* @param spacingAdd amount to add to the default line spacing
*/
protected Layout(CharSequence text, TextPaint paint,
int width, Alignment align,
float spacingMult, float spacingAdd) {
this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
spacingMult, spacingAdd);
}
/**
* Subclasses of Layout use this constructor to set the display text,
* width, and other standard properties.
* @param text the text to render
* @param paint the default paint for the layout. Styles can override
* various attributes of the paint.
* @param width the wrapping width for the text.
* @param align whether to left, right, or center the text. Styles can
* override the alignment.
* @param spacingMult factor by which to scale the font size to get the
* default line spacing
* @param spacingAdd amount to add to the default line spacing
*
* @hide
*/
protected Layout(CharSequence text, TextPaint paint,
int width, Alignment align, TextDirectionHeuristic textDir,
float spacingMult, float spacingAdd) {
if (width < 0)
throw new IllegalArgumentException("Layout: " + width + " < 0");
// Ensure paint doesn't have baselineShift set.
// While normally we don't modify the paint the user passed in,
// we were already doing this in Styled.drawUniformRun with both
// baselineShift and bgColor. We probably should reevaluate bgColor.
if (paint != null) {
paint.bgColor = 0;
paint.baselineShift = 0;
}
mText = text;
mPaint = paint;
mWidth = width;
mAlignment = align;
mSpacingMult = spacingMult;
mSpacingAdd = spacingAdd;
mSpannedText = text instanceof Spanned;
mTextDir = textDir;
}
/** @hide */
protected void setJustificationMode(@JustificationMode int justificationMode) {
mJustificationMode = justificationMode;
}
/**
* Replace constructor properties of this Layout with new ones. Be careful.
*/
/* package */ void replaceWith(CharSequence text, TextPaint paint,
int width, Alignment align,
float spacingmult, float spacingadd) {
if (width < 0) {
throw new IllegalArgumentException("Layout: " + width + " < 0");
}
mText = text;
mPaint = paint;
mWidth = width;
mAlignment = align;
mSpacingMult = spacingmult;
mSpacingAdd = spacingadd;
mSpannedText = text instanceof Spanned;
}
/**
* Draw this Layout on the specified Canvas.
*/
public void draw(Canvas c) {
draw(c, null, null, 0);
}
/**
* Draw this Layout on the specified canvas, with the highlight path drawn
* between the background and the text.
*
* @param canvas the canvas
* @param highlight the path of the highlight or cursor; can be null
* @param highlightPaint the paint for the highlight
* @param cursorOffsetVertical the amount to temporarily translate the
* canvas while rendering the highlight
*/
public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
int cursorOffsetVertical) {
final long lineRange = getLineRangeForDraw(canvas);
int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
if (lastLine < 0) return;
drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
firstLine, lastLine);
drawText(canvas, firstLine, lastLine);
}
private boolean isJustificationRequired(int lineNum) {
if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
final int lineEnd = getLineEnd(lineNum);
return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
}
private float getJustifyWidth(int lineNum) {
Alignment paraAlign = mAlignment;
int left = 0;
int right = mWidth;
final int dir = getParagraphDirection(lineNum);
ParagraphStyle[] spans = NO_PARA_SPANS;
if (mSpannedText) {
Spanned sp = (Spanned) mText;
final int start = getLineStart(lineNum);
final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
if (isFirstParaLine) {
final int spanEnd = sp.nextSpanTransition(start, mText.length(),
ParagraphStyle.class);
spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
for (int n = spans.length - 1; n >= 0; n--) {
if (spans[n] instanceof AlignmentSpan) {
paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
break;
}
}
}
final int length = spans.length;
boolean useFirstLineMargin = isFirstParaLine;
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan2) {
int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
if (lineNum < startLine + count) {
useFirstLineMargin = true;
break;
}
}
}
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan) {
LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
if (dir == DIR_RIGHT_TO_LEFT) {
right -= margin.getLeadingMargin(useFirstLineMargin);
} else {
left += margin.getLeadingMargin(useFirstLineMargin);
}
}
}
}
final Alignment align;
if (paraAlign == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (paraAlign == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
} else {
align = paraAlign;
}
final int indentWidth;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
} else {
indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
}
} else if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
} else {
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
}
} else { // Alignment.ALIGN_CENTER
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
}
return right - left - indentWidth;
}
/**
* @hide
*/
@UnsupportedAppUsage
public void drawText(Canvas canvas, int firstLine, int lastLine) {
int previousLineBottom = getLineTop(firstLine);
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spanEnd = 0;
final TextPaint paint = mWorkPaint;
paint.set(mPaint);
CharSequence buf = mText;
Alignment paraAlign = mAlignment;
TabStops tabStops = null;
boolean tabStopsIsInitialized = false;
TextLine tl = TextLine.obtain();
// Draw the lines, one at a time.
// The baseline is the top of the following line minus the current line's descent.
for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
int start = previousLineEnd;
previousLineEnd = getLineStart(lineNum + 1);
final boolean justify = isJustificationRequired(lineNum);
int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
paint.setStartHyphenEdit(getStartHyphenEdit(lineNum));
paint.setEndHyphenEdit(getEndHyphenEdit(lineNum));
int ltop = previousLineBottom;
int lbottom = getLineTop(lineNum + 1);
previousLineBottom = lbottom;
int lbaseline = lbottom - getLineDescent(lineNum);
int dir = getParagraphDirection(lineNum);
int left = 0;
int right = mWidth;
if (mSpannedText) {
Spanned sp = (Spanned) buf;
int textLength = buf.length();
boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
// New batch of paragraph styles, collect into spans array.
// Compute the alignment, last alignment style wins.
// Reset tabStops, we'll rebuild if we encounter a line with
// tabs.
// We expect paragraph spans to be relatively infrequent, use
// spanEnd so that we can check less frequently. Since
// paragraph styles ought to apply to entire paragraphs, we can
// just collect the ones present at the start of the paragraph.
// If spanEnd is before the end of the paragraph, that's not
// our problem.
if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
spanEnd = sp.nextSpanTransition(start, textLength,
ParagraphStyle.class);
spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
paraAlign = mAlignment;
for (int n = spans.length - 1; n >= 0; n--) {
if (spans[n] instanceof AlignmentSpan) {
paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
break;
}
}
tabStopsIsInitialized = false;
}
// Draw all leading margin spans. Adjust left or right according
// to the paragraph direction of the line.
final int length = spans.length;
boolean useFirstLineMargin = isFirstParaLine;
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan2) {
int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
// if there is more than one LeadingMarginSpan2, use
// the count that is greatest
if (lineNum < startLine + count) {
useFirstLineMargin = true;
break;
}
}
}
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan) {
LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
if (dir == DIR_RIGHT_TO_LEFT) {
margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
lbaseline, lbottom, buf,
start, end, isFirstParaLine, this);
right -= margin.getLeadingMargin(useFirstLineMargin);
} else {
margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
lbaseline, lbottom, buf,
start, end, isFirstParaLine, this);
left += margin.getLeadingMargin(useFirstLineMargin);
}
}
}
}
boolean hasTab = getLineContainsTab(lineNum);
// Can't tell if we have tabs for sure, currently
if (hasTab && !tabStopsIsInitialized) {
if (tabStops == null) {
tabStops = new TabStops(TAB_INCREMENT, spans);
} else {
tabStops.reset(TAB_INCREMENT, spans);
}
tabStopsIsInitialized = true;
}
// Determine whether the line aligns to normal, opposite, or center.
Alignment align = paraAlign;
if (align == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ?
Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (align == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ?
Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
}
int x;
final int indentWidth;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
x = left + indentWidth;
} else {
indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
x = right - indentWidth;
}
} else {
int max = (int)getLineExtent(lineNum, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
x = right - max - indentWidth;
} else {
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
x = left - max + indentWidth;
}
} else { // Alignment.ALIGN_CENTER
indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
max = max & ~1;
x = ((right + left - max) >> 1) + indentWidth;
}
}
Directions directions = getLineDirections(lineNum);
if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
// XXX: assumes there's nothing additional to be done
canvas.drawText(buf, start, end, x, lbaseline, paint);
} else {
tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops,
getEllipsisStart(lineNum),
getEllipsisStart(lineNum) + getEllipsisCount(lineNum));
if (justify) {
tl.justify(right - left - indentWidth);
}
tl.draw(canvas, x, ltop, lbaseline, lbottom);
}
}
TextLine.recycle(tl);
}
/**
* @hide
*/
@UnsupportedAppUsage
public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
int cursorOffsetVertical, int firstLine, int lastLine) {
// First, draw LineBackgroundSpans.
// LineBackgroundSpans know nothing about the alignment, margins, or
// direction of the layout or line. XXX: Should they?
// They are evaluated at each line.
if (mSpannedText) {
if (mLineBackgroundSpans == null) {
mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
}
Spanned buffer = (Spanned) mText;
int textLength = buffer.length();
mLineBackgroundSpans.init(buffer, 0, textLength);
if (mLineBackgroundSpans.numberOfSpans > 0) {
int previousLineBottom = getLineTop(firstLine);
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spansLength = 0;
TextPaint paint = mPaint;
int spanEnd = 0;
final int width = mWidth;
for (int i = firstLine; i <= lastLine; i++) {
int start = previousLineEnd;
int end = getLineStart(i + 1);
previousLineEnd = end;
int ltop = previousLineBottom;
int lbottom = getLineTop(i + 1);
previousLineBottom = lbottom;
int lbaseline = lbottom - getLineDescent(i);
if (end >= spanEnd) {
// These should be infrequent, so we'll use this so that
// we don't have to check as often.
spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
// All LineBackgroundSpans on a line contribute to its background.
spansLength = 0;
// Duplication of the logic of getParagraphSpans
if (start != end || start == 0) {
// Equivalent to a getSpans(start, end), but filling the 'spans' local
// array instead to reduce memory allocation
for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
// equal test is valid since both intervals are not empty by
// construction
if (mLineBackgroundSpans.spanStarts[j] >= end ||
mLineBackgroundSpans.spanEnds[j] <= start) continue;
spans = GrowingArrayUtils.append(
spans, spansLength, mLineBackgroundSpans.spans[j]);
spansLength++;
}
}
}
for (int n = 0; n < spansLength; n++) {
LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
ltop, lbaseline, lbottom,
buffer, start, end, i);
}
}
}
mLineBackgroundSpans.recycle();
}
// There can be a highlight even without spans if we are drawing
// a non-spanned transformation of a spanned editing buffer.
if (highlight != null) {
if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
canvas.drawPath(highlight, highlightPaint);
if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
}
}
/**
* @param canvas
* @return The range of lines that need to be drawn, possibly empty.
* @hide
*/
@UnsupportedAppUsage
public long getLineRangeForDraw(Canvas canvas) {
int dtop, dbottom;
synchronized (sTempRect) {
if (!canvas.getClipBounds(sTempRect)) {
// Negative range end used as a special flag
return TextUtils.packRangeInLong(0, -1);
}
dtop = sTempRect.top;
dbottom = sTempRect.bottom;
}
final int top = Math.max(dtop, 0);
final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
}
/**
* Return the start position of the line, given the left and right bounds
* of the margins.
*
* @param line the line index
* @param left the left bounds (0, or leading margin if ltr para)
* @param right the right bounds (width, minus leading margin if rtl para)
* @return the start position of the line (to right of line if rtl para)
*/
private int getLineStartPos(int line, int left, int right) {
// Adjust the point at which to start rendering depending on the
// alignment of the paragraph.
Alignment align = getParagraphAlignment(line);
int dir = getParagraphDirection(line);
if (align == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (align == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
}
int x;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
} else {
x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
}
} else {
TabStops tabStops = null;
if (mSpannedText && getLineContainsTab(line)) {
Spanned spanned = (Spanned) mText;
int start = getLineStart(line);
int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
TabStopSpan.class);
TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
TabStopSpan.class);
if (tabSpans.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabSpans);
}
}
int max = (int)getLineExtent(line, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
} else {
// max is negative here
x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
}
} else { // Alignment.ALIGN_CENTER
max = max & ~1;
x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
}
}
return x;
}
/**
* Return the text that is displayed by this Layout.
*/
public final CharSequence getText() {
return mText;
}
/**
* Return the base Paint properties for this layout.
* Do NOT change the paint, which may result in funny
* drawing for this layout.
*/
public final TextPaint getPaint() {
return mPaint;
}
/**
* Return the width of this layout.
*/
public final int getWidth() {
return mWidth;
}
/**
* Return the width to which this Layout is ellipsizing, or
* {@link #getWidth} if it is not doing anything special.
*/
public int getEllipsizedWidth() {
return mWidth;
}
/**
* Increase the width of this layout to the specified width.
* Be careful to use this only when you know it is appropriate—
* it does not cause the text to reflow to use the full new width.
*/
public final void increaseWidthTo(int wid) {
if (wid < mWidth) {
throw new RuntimeException("attempted to reduce Layout width");
}
mWidth = wid;
}
/**
* Return the total height of this layout.
*/
public int getHeight() {
return getLineTop(getLineCount());
}
/**
* Return the total height of this layout.
*
* @param cap if true and max lines is set, returns the height of the layout at the max lines.
*
* @hide
*/
public int getHeight(boolean cap) {
return getHeight();
}
/**
* Return the base alignment of this layout.
*/
public final Alignment getAlignment() {
return mAlignment;
}
/**
* Return what the text height is multiplied by to get the line height.
*/
public final float getSpacingMultiplier() {
return mSpacingMult;
}
/**
* Return the number of units of leading that are added to each line.
*/
public final float getSpacingAdd() {
return mSpacingAdd;
}
/**
* Return the heuristic used to determine paragraph text direction.
* @hide
*/
public final TextDirectionHeuristic getTextDirectionHeuristic() {
return mTextDir;
}
/**
* Return the number of lines of text in this layout.
*/
public abstract int getLineCount();
/**
* Return the baseline for the specified line (0…getLineCount() - 1)
* If bounds is not null, return the top, left, right, bottom extents
* of the specified line in it.
* @param line which line to examine (0..getLineCount() - 1)
* @param bounds Optional. If not null, it returns the extent of the line
* @return the Y-coordinate of the baseline
*/
public int getLineBounds(int line, Rect bounds) {
if (bounds != null) {
bounds.left = 0; // ???
bounds.top = getLineTop(line);
bounds.right = mWidth; // ???
bounds.bottom = getLineTop(line + 1);
}
return getLineBaseline(line);
}
/**
* Return the vertical position of the top of the specified line
* (0…getLineCount()).
* If the specified line is equal to the line count, returns the
* bottom of the last line.
*/
public abstract int getLineTop(int line);
/**
* Return the descent of the specified line(0…getLineCount() - 1).
*/
public abstract int getLineDescent(int line);
/**
* Return the text offset of the beginning of the specified line (
* 0…getLineCount()). If the specified line is equal to the line
* count, returns the length of the text.
*/
public abstract int getLineStart(int line);
/**
* Returns the primary directionality of the paragraph containing the
* specified line, either 1 for left-to-right lines, or -1 for right-to-left
* lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
*/
public abstract int getParagraphDirection(int line);
/**
* Returns whether the specified line contains one or more
* characters that need to be handled specially, like tabs.
*/
public abstract boolean getLineContainsTab(int line);
/**
* Returns the directional run information for the specified line.
* The array alternates counts of characters in left-to-right
* and right-to-left segments of the line.
*
* <p>NOTE: this is inadequate to support bidirectional text, and will change.
*/
public abstract Directions getLineDirections(int line);
/**
* Returns the (negative) number of extra pixels of ascent padding in the
* top line of the Layout.
*/
public abstract int getTopPadding();
/**
* Returns the number of extra pixels of descent padding in the
* bottom line of the Layout.
*/
public abstract int getBottomPadding();
/**
* Returns the start hyphen edit for a line.
*
* @hide
*/
public @Paint.StartHyphenEdit int getStartHyphenEdit(int line) {
return Paint.START_HYPHEN_EDIT_NO_EDIT;
}
/**
* Returns the end hyphen edit for a line.
*
* @hide
*/
public @Paint.EndHyphenEdit int getEndHyphenEdit(int line) {
return Paint.END_HYPHEN_EDIT_NO_EDIT;
}
/**
* Returns the left indent for a line.
*
* @hide
*/
public int getIndentAdjust(int line, Alignment alignment) {
return 0;
}
/**
* Returns true if the character at offset and the preceding character
* are at different run levels (and thus there's a split caret).
* @param offset the offset
* @return true if at a level boundary
* @hide
*/
@UnsupportedAppUsage
public boolean isLevelBoundary(int offset) {
int line = getLineForOffset(offset);
Directions dirs = getLineDirections(line);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
return false;
}
int[] runs = dirs.mDirections;
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
if (offset == lineStart || offset == lineEnd) {
int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
int runIndex = offset == lineStart ? 0 : runs.length - 2;
return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
}
offset -= lineStart;
for (int i = 0; i < runs.length; i += 2) {
if (offset == runs[i]) {
return true;
}
}
return false;
}
/**
* Returns true if the character at offset is right to left (RTL).
* @param offset the offset
* @return true if the character is RTL, false if it is LTR
*/
public boolean isRtlCharAt(int offset) {
int line = getLineForOffset(offset);
Directions dirs = getLineDirections(line);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
return false;
}
if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
return true;
}
int[] runs = dirs.mDirections;
int lineStart = getLineStart(line);
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (offset >= start && offset < limit) {
int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
return ((level & 1) != 0);
}
}
// Should happen only if the offset is "out of bounds"
return false;
}
/**
* Returns the range of the run that the character at offset belongs to.
* @param offset the offset
* @return The range of the run
* @hide
*/
public long getRunRange(int offset) {
int line = getLineForOffset(offset);
Directions dirs = getLineDirections(line);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
return TextUtils.packRangeInLong(0, getLineEnd(line));
}
int[] runs = dirs.mDirections;
int lineStart = getLineStart(line);
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (offset >= start && offset < limit) {
return TextUtils.packRangeInLong(start, limit);
}
}
// Should happen only if the offset is "out of bounds"
return TextUtils.packRangeInLong(0, getLineEnd(line));
}
/**
* Checks if the trailing BiDi level should be used for an offset
*
* This method is useful when the offset is at the BiDi level transition point and determine
* which run need to be used. For example, let's think about following input: (L* denotes
* Left-to-Right characters, R* denotes Right-to-Left characters.)
* Input (Logical Order): L1 L2 L3 R1 R2 R3 L4 L5 L6
* Input (Display Order): L1 L2 L3 R3 R2 R1 L4 L5 L6
*
* Then, think about selecting the range (3, 6). The offset=3 and offset=6 are ambiguous here
* since they are at the BiDi transition point. In Android, the offset is considered to be
* associated with the trailing run if the BiDi level of the trailing run is higher than of the
* previous run. In this case, the BiDi level of the input text is as follows:
*
* Input (Logical Order): L1 L2 L3 R1 R2 R3 L4 L5 L6
* BiDi Run: [ Run 0 ][ Run 1 ][ Run 2 ]
* BiDi Level: 0 0 0 1 1 1 0 0 0
*
* Thus, offset = 3 is part of Run 1 and this method returns true for offset = 3, since the BiDi
* level of Run 1 is higher than the level of Run 0. Similarly, the offset = 6 is a part of Run
* 1 and this method returns false for the offset = 6 since the BiDi level of Run 1 is higher
* than the level of Run 2.
*
* @returns true if offset is at the BiDi level transition point and trailing BiDi level is
* higher than previous BiDi level. See above for the detail.
* @hide
*/
@VisibleForTesting
public boolean primaryIsTrailingPrevious(int offset) {
int line = getLineForOffset(offset);
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int[] runs = getLineDirections(line).mDirections;
int levelAt = -1;
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (limit > lineEnd) {
limit = lineEnd;
}
if (offset >= start && offset < limit) {
if (offset > start) {
// Previous character is at same level, so don't use trailing.
return false;
}
levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
break;
}
}
if (levelAt == -1) {
// Offset was limit of line.
levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
}
// At level boundary, check previous level.
int levelBefore = -1;
if (offset == lineStart) {
levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
} else {
offset -= 1;
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
if (limit > lineEnd) {
limit = lineEnd;
}
if (offset >= start && offset < limit) {
levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
break;
}
}
}
return levelBefore < levelAt;
}
/**
* Computes in linear time the results of calling
* #primaryIsTrailingPrevious for all offsets on a line.
* @param line The line giving the offsets we compute the information for
* @return The array of results, indexed from 0, where 0 corresponds to the line start offset
* @hide
*/
@VisibleForTesting
public boolean[] primaryIsTrailingPreviousAllLineOffsets(int line) {
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int[] runs = getLineDirections(line).mDirections;
boolean[] trailing = new boolean[lineEnd - lineStart + 1];
byte[] level = new byte[lineEnd - lineStart + 1];
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
int limit = start + (runs[i + 1] & RUN_LENGTH_MASK);
if (limit > lineEnd) {
limit = lineEnd;
}
if (limit == start) {
continue;
}
level[limit - lineStart - 1] =
(byte) ((runs[i + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK);
}
for (int i = 0; i < runs.length; i += 2) {
int start = lineStart + runs[i];
byte currentLevel = (byte) ((runs[i + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK);
trailing[start - lineStart] = currentLevel > (start == lineStart
? (getParagraphDirection(line) == 1 ? 0 : 1)
: level[start - lineStart - 1]);
}
return trailing;
}
/**
* Get the primary horizontal position for the specified text offset.
* This is the location where a new character would be inserted in
* the paragraph's primary direction.
*/
public float getPrimaryHorizontal(int offset) {
return getPrimaryHorizontal(offset, false /* not clamped */);
}
/**
* Get the primary horizontal position for the specified text offset, but
* optionally clamp it so that it doesn't exceed the width of the layout.
* @hide
*/
@UnsupportedAppUsage
public float getPrimaryHorizontal(int offset, boolean clamped) {
boolean trailing = primaryIsTrailingPrevious(offset);
return getHorizontal(offset, trailing, clamped);
}
/**
* Get the secondary horizontal position for the specified text offset.
* This is the location where a new character would be inserted in
* the direction other than the paragraph's primary direction.
*/
public float getSecondaryHorizontal(int offset) {
return getSecondaryHorizontal(offset, false /* not clamped */);
}
/**
* Get the secondary horizontal position for the specified text offset, but
* optionally clamp it so that it doesn't exceed the width of the layout.
* @hide
*/
@UnsupportedAppUsage
public float getSecondaryHorizontal(int offset, boolean clamped) {
boolean trailing = primaryIsTrailingPrevious(offset);
return getHorizontal(offset, !trailing, clamped);
}
private float getHorizontal(int offset, boolean primary) {
return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
}
private float getHorizontal(int offset, boolean trailing, boolean clamped) {
int line = getLineForOffset(offset);
return getHorizontal(offset, trailing, line, clamped);
}
private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
int start = getLineStart(line);
int end = getLineEnd(line);
int dir = getParagraphDirection(line);
boolean hasTab = getLineContainsTab(line);
Directions directions = getLineDirections(line);
TabStops tabStops = null;
if (hasTab && mText instanceof Spanned) {
// Just checking this line should be good enough, tabs should be
// consistent across all lines in a paragraph.
TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
if (tabs.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
}
}
TextLine tl = TextLine.obtain();
tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
float wid = tl.measure(offset - start, trailing, null);
TextLine.recycle(tl);
if (clamped && wid > mWidth) {
wid = mWidth;
}
int left = getParagraphLeft(line);
int right = getParagraphRight(line);
return getLineStartPos(line, left, right) + wid;
}
/**
* Computes in linear time the results of calling #getHorizontal for all offsets on a line.
*
* @param line The line giving the offsets we compute information for
* @param clamped Whether to clamp the results to the width of the layout
* @param primary Whether the results should be the primary or the secondary horizontal
* @return The array of results, indexed from 0, where 0 corresponds to the line start offset
*/
private float[] getLineHorizontals(int line, boolean clamped, boolean primary) {
int start = getLineStart(line);
int end = getLineEnd(line);
int dir = getParagraphDirection(line);
boolean hasTab = getLineContainsTab(line);
Directions directions = getLineDirections(line);
TabStops tabStops = null;
if (hasTab && mText instanceof Spanned) {
// Just checking this line should be good enough, tabs should be
// consistent across all lines in a paragraph.
TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
if (tabs.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
}
}
TextLine tl = TextLine.obtain();
tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
boolean[] trailings = primaryIsTrailingPreviousAllLineOffsets(line);
if (!primary) {
for (int offset = 0; offset < trailings.length; ++offset) {
trailings[offset] = !trailings[offset];
}
}
float[] wid = tl.measureAllOffsets(trailings, null);
TextLine.recycle(tl);
if (clamped) {
for (int offset = 0; offset < wid.length; ++offset) {
if (wid[offset] > mWidth) {
wid[offset] = mWidth;
}
}
}
int left = getParagraphLeft(line);
int right = getParagraphRight(line);
int lineStartPos = getLineStartPos(line, left, right);
float[] horizontal = new float[end - start + 1];
for (int offset = 0; offset < horizontal.length; ++offset) {
horizontal[offset] = lineStartPos + wid[offset];
}
return horizontal;
}
/**
* Get the leftmost position that should be exposed for horizontal
* scrolling on the specified line.
*/
public float getLineLeft(int line) {
final int dir = getParagraphDirection(line);
Alignment align = getParagraphAlignment(line);
// Before Q, StaticLayout.Builder.setAlignment didn't check whether the input alignment
// is null. And when it is null, the old behavior is the same as ALIGN_CENTER.
// To keep consistency, we convert a null alignment to ALIGN_CENTER.
if (align == null) {
align = Alignment.ALIGN_CENTER;
}
// First convert combinations of alignment and direction settings to
// three basic cases: ALIGN_LEFT, ALIGN_RIGHT and ALIGN_CENTER.
// For unexpected cases, it will fallback to ALIGN_LEFT.
final Alignment resultAlign;
switch(align) {
case ALIGN_NORMAL:
resultAlign =
dir == DIR_RIGHT_TO_LEFT ? Alignment.ALIGN_RIGHT : Alignment.ALIGN_LEFT;
break;
case ALIGN_OPPOSITE:
resultAlign =
dir == DIR_RIGHT_TO_LEFT ? Alignment.ALIGN_LEFT : Alignment.ALIGN_RIGHT;
break;
case ALIGN_CENTER:
resultAlign = Alignment.ALIGN_CENTER;
break;
case ALIGN_RIGHT:
resultAlign = Alignment.ALIGN_RIGHT;
break;
default: /* align == Alignment.ALIGN_LEFT */
resultAlign = Alignment.ALIGN_LEFT;
}
// Here we must use getLineMax() to do the computation, because it maybe overridden by
// derived class. And also note that line max equals the width of the text in that line
// plus the leading margin.
switch (resultAlign) {
case ALIGN_CENTER:
final int left = getParagraphLeft(line);
final float max = getLineMax(line);
// This computation only works when mWidth equals leadingMargin plus
// the width of text in this line. If this condition doesn't meet anymore,
// please change here too.
return (float) Math.floor(left + (mWidth - max) / 2);
case ALIGN_RIGHT:
return mWidth - getLineMax(line);
default: /* resultAlign == Alignment.ALIGN_LEFT */
return 0;
}
}
/**
* Get the rightmost position that should be exposed for horizontal
* scrolling on the specified line.
*/
public float getLineRight(int line) {
final int dir = getParagraphDirection(line);
Alignment align = getParagraphAlignment(line);
// Before Q, StaticLayout.Builder.setAlignment didn't check whether the input alignment
// is null. And when it is null, the old behavior is the same as ALIGN_CENTER.
// To keep consistency, we convert a null alignment to ALIGN_CENTER.
if (align == null) {
align = Alignment.ALIGN_CENTER;
}
final Alignment resultAlign;
switch(align) {
case ALIGN_NORMAL:
resultAlign =
dir == DIR_RIGHT_TO_LEFT ? Alignment.ALIGN_RIGHT : Alignment.ALIGN_LEFT;
break;
case ALIGN_OPPOSITE:
resultAlign =
dir == DIR_RIGHT_TO_LEFT ? Alignment.ALIGN_LEFT : Alignment.ALIGN_RIGHT;
break;
case ALIGN_CENTER:
resultAlign = Alignment.ALIGN_CENTER;
break;
case ALIGN_RIGHT:
resultAlign = Alignment.ALIGN_RIGHT;
break;
default: /* align == Alignment.ALIGN_LEFT */
resultAlign = Alignment.ALIGN_LEFT;
}
switch (resultAlign) {
case ALIGN_CENTER:
final int right = getParagraphRight(line);
final float max = getLineMax(line);
// This computation only works when mWidth equals leadingMargin plus width of the
// text in this line. If this condition doesn't meet anymore, please change here.
return (float) Math.ceil(right - (mWidth - max) / 2);
case ALIGN_RIGHT:
return mWidth;
default: /* resultAlign == Alignment.ALIGN_LEFT */
return getLineMax(line);
}
}
/**
* Gets the unsigned horizontal extent of the specified line, including
* leading margin indent, but excluding trailing whitespace.
*/
public float getLineMax(int line) {
float margin = getParagraphLeadingMargin(line);
float signedExtent = getLineExtent(line, false);
return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
}
/**
* Gets the unsigned horizontal extent of the specified line, including
* leading margin indent and trailing whitespace.
*/
public float getLineWidth(int line) {
float margin = getParagraphLeadingMargin(line);
float signedExtent = getLineExtent(line, true);
return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
}
/**
* Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
* tab stops instead of using the ones passed in.
* @param line the index of the line
* @param full whether to include trailing whitespace
* @return the extent of the line
*/
private float getLineExtent(int line, boolean full) {
final int start = getLineStart(line);
final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
final boolean hasTabs = getLineContainsTab(line);
TabStops tabStops = null;
if (hasTabs && mText instanceof Spanned) {
// Just checking this line should be good enough, tabs should be
// consistent across all lines in a paragraph.
TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
if (tabs.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
}
}
final Directions directions = getLineDirections(line);
// Returned directions can actually be null
if (directions == null) {
return 0f;
}
final int dir = getParagraphDirection(line);
final TextLine tl = TextLine.obtain();
final TextPaint paint = mWorkPaint;
paint.set(mPaint);
paint.setStartHyphenEdit(getStartHyphenEdit(line));
paint.setEndHyphenEdit(getEndHyphenEdit(line));
tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
if (isJustificationRequired(line)) {
tl.justify(getJustifyWidth(line));
}
final float width = tl.metrics(null);
TextLine.recycle(tl);
return width;
}
/**
* Returns the signed horizontal extent of the specified line, excluding
* leading margin. If full is false, excludes trailing whitespace.
* @param line the index of the line
* @param tabStops the tab stops, can be null if we know they're not used.
* @param full whether to include trailing whitespace
* @return the extent of the text on this line
*/
private float getLineExtent(int line, TabStops tabStops, boolean full) {
final int start = getLineStart(line);
final int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
final boolean hasTabs = getLineContainsTab(line);
final Directions directions = getLineDirections(line);
final int dir = getParagraphDirection(line);
final TextLine tl = TextLine.obtain();
final TextPaint paint = mWorkPaint;
paint.set(mPaint);
paint.setStartHyphenEdit(getStartHyphenEdit(line));
paint.setEndHyphenEdit(getEndHyphenEdit(line));
tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
if (isJustificationRequired(line)) {
tl.justify(getJustifyWidth(line));
}
final float width = tl.metrics(null);
TextLine.recycle(tl);
return width;
}
/**
* Get the line number corresponding to the specified vertical position.
* If you ask for a position above 0, you get 0; if you ask for a position
* below the bottom of the text, you get the last line.
*/
// FIXME: It may be faster to do a linear search for layouts without many lines.
public int getLineForVertical(int vertical) {
int high = getLineCount(), low = -1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (getLineTop(guess) > vertical)
high = guess;
else
low = guess;
}
if (low < 0)
return 0;
else
return low;
}
/**
* Get the line number on which the specified text offset appears.
* If you ask for a position before 0, you get 0; if you ask for a position
* beyond the end of the text, you get the last line.
*/
public int getLineForOffset(int offset) {
int high = getLineCount(), low = -1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
if (getLineStart(guess) > offset)
high = guess;
else
low = guess;
}
if (low < 0) {
return 0;
} else {
return low;
}
}
/**
* Get the character offset on the specified line whose position is
* closest to the specified horizontal position.
*/
public int getOffsetForHorizontal(int line, float horiz) {
return getOffsetForHorizontal(line, horiz, true);
}
/**
* Get the character offset on the specified line whose position is
* closest to the specified horizontal position.
*
* @param line the line used to find the closest offset
* @param horiz the horizontal position used to find the closest offset
* @param primary whether to use the primary position or secondary position to find the offset
*
* @hide
*/
public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
// TODO: use Paint.getOffsetForAdvance to avoid binary search
final int lineEndOffset = getLineEnd(line);
final int lineStartOffset = getLineStart(line);
Directions dirs = getLineDirections(line);
TextLine tl = TextLine.obtain();
// XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
false, null,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
final HorizontalMeasurementProvider horizontal =
new HorizontalMeasurementProvider(line, primary);
final int max;
if (line == getLineCount() - 1) {
max = lineEndOffset;
} else {
max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
!isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
}
int best = lineStartOffset;
float bestdist = Math.abs(horizontal.get(lineStartOffset) - horiz);
for (int i = 0; i < dirs.mDirections.length; i += 2) {
int here = lineStartOffset + dirs.mDirections[i];
int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
int swap = isRtl ? -1 : 1;
if (there > max)
there = max;
int high = there - 1 + 1, low = here + 1 - 1, guess;
while (high - low > 1) {
guess = (high + low) / 2;
int adguess = getOffsetAtStartOf(guess);
if (horizontal.get(adguess) * swap >= horiz * swap) {
high = guess;
} else {
low = guess;
}
}
if (low < here + 1)
low = here + 1;
if (low < there) {
int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
if (low >= here && low < there) {
float dist = Math.abs(horizontal.get(low) - horiz);
if (aft < there) {
float other = Math.abs(horizontal.get(aft) - horiz);
if (other < dist) {
dist = other;
low = aft;
}
}
if (dist < bestdist) {
bestdist = dist;
best = low;
}
}
}
float dist = Math.abs(horizontal.get(here) - horiz);
if (dist < bestdist) {
bestdist = dist;
best = here;
}
}
float dist = Math.abs(horizontal.get(max) - horiz);
if (dist <= bestdist) {
best = max;
}
TextLine.recycle(tl);
return best;
}
/**
* Responds to #getHorizontal queries, by selecting the better strategy between:
* - calling #getHorizontal explicitly for each query
* - precomputing all #getHorizontal measurements, and responding to any query in constant time
* The first strategy is used for LTR-only text, while the second is used for all other cases.
* The class is currently only used in #getOffsetForHorizontal, so reuse with care in other
* contexts.
*/
private class HorizontalMeasurementProvider {
private final int mLine;
private final boolean mPrimary;
private float[] mHorizontals;
private int mLineStartOffset;
HorizontalMeasurementProvider(final int line, final boolean primary) {
mLine = line;
mPrimary = primary;
init();
}
private void init() {
final Directions dirs = getLineDirections(mLine);
if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
return;
}
mHorizontals = getLineHorizontals(mLine, false, mPrimary);
mLineStartOffset = getLineStart(mLine);
}
float get(final int offset) {
final int index = offset - mLineStartOffset;
if (mHorizontals == null || index < 0 || index >= mHorizontals.length) {
return getHorizontal(offset, mPrimary);
} else {
return mHorizontals[index];
}
}
}
/**
* Return the text offset after the last character on the specified line.
*/
public final int getLineEnd(int line) {
return getLineStart(line + 1);
}
/**
* Return the text offset after the last visible character (so whitespace
* is not counted) on the specified line.
*/
public int getLineVisibleEnd(int line) {
return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
}
private int getLineVisibleEnd(int line, int start, int end) {
CharSequence text = mText;
char ch;
if (line == getLineCount() - 1) {
return end;
}
for (; end > start; end--) {
ch = text.charAt(end - 1);
if (ch == '\n') {
return end - 1;
}
if (!TextLine.isLineEndSpace(ch)) {
break;
}
}
return end;
}
/**
* Return the vertical position of the bottom of the specified line.
*/
public final int getLineBottom(int line) {
return getLineTop(line + 1);
}
/**
* Return the vertical position of the bottom of the specified line without the line spacing
* added.
*
* @hide
*/
public final int getLineBottomWithoutSpacing(int line) {
return getLineTop(line + 1) - getLineExtra(line);
}
/**
* Return the vertical position of the baseline of the specified line.
*/
public final int getLineBaseline(int line) {
// getLineTop(line+1) == getLineBottom(line)
return getLineTop(line+1) - getLineDescent(line);
}
/**
* Get the ascent of the text on the specified line.
* The return value is negative to match the Paint.ascent() convention.
*/
public final int getLineAscent(int line) {
// getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
}
/**
* Return the extra space added as a result of line spacing attributes
* {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
*
* @param line the index of the line, the value should be equal or greater than {@code zero}
* @hide
*/
public int getLineExtra(@IntRange(from = 0) int line) {
return 0;
}
public int getOffsetToLeftOf(int offset) {
return getOffsetToLeftRightOf(offset, true);
}
public int getOffsetToRightOf(int offset) {
return getOffsetToLeftRightOf(offset, false);
}
private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
int line = getLineForOffset(caret);
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int lineDir = getParagraphDirection(line);
boolean lineChanged = false;
boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
// if walking off line, look at the line we're headed to
if (advance) {
if (caret == lineEnd) {
if (line < getLineCount() - 1) {
lineChanged = true;
++line;
} else {
return caret; // at very end, don't move
}
}
} else {
if (caret == lineStart) {
if (line > 0) {
lineChanged = true;
--line;
} else {
return caret; // at very start, don't move
}
}
}
if (lineChanged) {
lineStart = getLineStart(line);
lineEnd = getLineEnd(line);
int newDir = getParagraphDirection(line);
if (newDir != lineDir) {
// unusual case. we want to walk onto the line, but it runs
// in a different direction than this one, so we fake movement
// in the opposite direction.
toLeft = !toLeft;
lineDir = newDir;
}
}
Directions directions = getLineDirections(line);
TextLine tl = TextLine.obtain();
// XXX: we don't care about tabs
tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null,
getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line));
caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
TextLine.recycle(tl);
return caret;
}
private int getOffsetAtStartOf(int offset) {
// XXX this probably should skip local reorderings and
// zero-width characters, look at callers
if (offset == 0)
return 0;
CharSequence text = mText;
char c = text.charAt(offset);
if (c >= '\uDC00' && c <= '\uDFFF') {
char c1 = text.charAt(offset - 1);
if (c1 >= '\uD800' && c1 <= '\uDBFF')
offset -= 1;
}
if (mSpannedText) {
ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
ReplacementSpan.class);
for (int i = 0; i < spans.length; i++) {
int start = ((Spanned) text).getSpanStart(spans[i]);
int end = ((Spanned) text).getSpanEnd(spans[i]);
if (start < offset && end > offset)
offset = start;
}
}
return offset;
}
/**
* Determine whether we should clamp cursor position. Currently it's
* only robust for left-aligned displays.
* @hide
*/
@UnsupportedAppUsage
public boolean shouldClampCursor(int line) {
// Only clamp cursor position in left-aligned displays.
switch (getParagraphAlignment(line)) {
case ALIGN_LEFT:
return true;
case ALIGN_NORMAL:
return getParagraphDirection(line) > 0;
default:
return false;
}
}
/**
* Fills in the specified Path with a representation of a cursor
* at the specified offset. This will often be a vertical line
* but can be multiple discontinuous lines in text with multiple
* directionalities.
*/
public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) {
dest.reset();
int line = getLineForOffset(point);
int top = getLineTop(line);
int bottom = getLineBottomWithoutSpacing(line);
boolean clamped = shouldClampCursor(line);
float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
int dist = 0;
if (caps != 0 || fn != 0) {
dist = (bottom - top) >> 2;
if (fn != 0)
top += dist;
if (caps != 0)
bottom -= dist;
}
if (h1 < 0.5f)
h1 = 0.5f;
dest.moveTo(h1, top);
dest.lineTo(h1, bottom);
if (caps == 2) {
dest.moveTo(h1, bottom);
dest.lineTo(h1 - dist, bottom + dist);
dest.lineTo(h1, bottom);
dest.lineTo(h1 + dist, bottom + dist);
} else if (caps == 1) {
dest.moveTo(h1, bottom);
dest.lineTo(h1 - dist, bottom + dist);
dest.moveTo(h1 - dist, bottom + dist - 0.5f);
dest.lineTo(h1 + dist, bottom + dist - 0.5f);
dest.moveTo(h1 + dist, bottom + dist);
dest.lineTo(h1, bottom);
}
if (fn == 2) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.lineTo(h1, top);
dest.lineTo(h1 + dist, top - dist);
} else if (fn == 1) {
dest.moveTo(h1, top);
dest.lineTo(h1 - dist, top - dist);
dest.moveTo(h1 - dist, top - dist + 0.5f);
dest.lineTo(h1 + dist, top - dist + 0.5f);
dest.moveTo(h1 + dist, top - dist);
dest.lineTo(h1, top);
}
}
private void addSelection(int line, int start, int end,
int top, int bottom, SelectionRectangleConsumer consumer) {
int linestart = getLineStart(line);
int lineend = getLineEnd(line);
Directions dirs = getLineDirections(line);
if (lineend > linestart && mText.charAt(lineend - 1) == '\n') {
lineend--;
}
for (int i = 0; i < dirs.mDirections.length; i += 2) {
int here = linestart + dirs.mDirections[i];
int there = here + (dirs.mDirections[i + 1] & RUN_LENGTH_MASK);
if (there > lineend) {
there = lineend;
}
if (start <= there && end >= here) {
int st = Math.max(start, here);
int en = Math.min(end, there);
if (st != en) {
float h1 = getHorizontal(st, false, line, false /* not clamped */);
float h2 = getHorizontal(en, true, line, false /* not clamped */);
float left = Math.min(h1, h2);
float right = Math.max(h1, h2);
final @TextSelectionLayout int layout =
((dirs.mDirections[i + 1] & RUN_RTL_FLAG) != 0)
? TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT
: TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT;
consumer.accept(left, top, right, bottom, layout);
}
}
}
}
/**
* Fills in the specified Path with a representation of a highlight
* between the specified offsets. This will often be a rectangle
* or a potentially discontinuous set of rectangles. If the start
* and end are the same, the returned path is empty.
*/
public void getSelectionPath(int start, int end, Path dest) {
dest.reset();
getSelection(start, end, (left, top, right, bottom, textSelectionLayout) ->
dest.addRect(left, top, right, bottom, Path.Direction.CW));
}
/**
* Calculates the rectangles which should be highlighted to indicate a selection between start
* and end and feeds them into the given {@link SelectionRectangleConsumer}.
*
* @param start the starting index of the selection
* @param end the ending index of the selection
* @param consumer the {@link SelectionRectangleConsumer} which will receive the generated
* rectangles. It will be called every time a rectangle is generated.
* @hide
* @see #getSelectionPath(int, int, Path)
*/
public final void getSelection(int start, int end, final SelectionRectangleConsumer consumer) {
if (start == end) {
return;
}
if (end < start) {
int temp = end;
end = start;
start = temp;
}
final int startline = getLineForOffset(start);
final int endline = getLineForOffset(end);
int top = getLineTop(startline);
int bottom = getLineBottomWithoutSpacing(endline);
if (startline == endline) {
addSelection(startline, start, end, top, bottom, consumer);
} else {
final float width = mWidth;
addSelection(startline, start, getLineEnd(startline),
top, getLineBottom(startline), consumer);
if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) {
consumer.accept(getLineLeft(startline), top, 0, getLineBottom(startline),
TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
} else {
consumer.accept(getLineRight(startline), top, width, getLineBottom(startline),
TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
}
for (int i = startline + 1; i < endline; i++) {
top = getLineTop(i);
bottom = getLineBottom(i);
if (getParagraphDirection(i) == DIR_RIGHT_TO_LEFT) {
consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
} else {
consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
}
}
top = getLineTop(endline);
bottom = getLineBottomWithoutSpacing(endline);
addSelection(endline, getLineStart(endline), end, top, bottom, consumer);
if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT) {
consumer.accept(width, top, getLineRight(endline), bottom,
TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT);
} else {
consumer.accept(0, top, getLineLeft(endline), bottom,
TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT);
}
}
}
/**
* Get the alignment of the specified paragraph, taking into account
* markup attached to it.
*/
public final Alignment getParagraphAlignment(int line) {
Alignment align = mAlignment;
if (mSpannedText) {
Spanned sp = (Spanned) mText;
AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
getLineEnd(line),
AlignmentSpan.class);
int spanLength = spans.length;
if (spanLength > 0) {
align = spans[spanLength-1].getAlignment();
}
}
return align;
}
/**
* Get the left edge of the specified paragraph, inset by left margins.
*/
public final int getParagraphLeft(int line) {
int left = 0;
int dir = getParagraphDirection(line);
if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
return left; // leading margin has no impact, or no styles
}
return getParagraphLeadingMargin(line);
}
/**
* Get the right edge of the specified paragraph, inset by right margins.
*/
public final int getParagraphRight(int line) {
int right = mWidth;
int dir = getParagraphDirection(line);
if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
return right; // leading margin has no impact, or no styles
}
return right - getParagraphLeadingMargin(line);
}
/**
* Returns the effective leading margin (unsigned) for this line,
* taking into account LeadingMarginSpan and LeadingMarginSpan2.
* @param line the line index
* @return the leading margin of this line
*/
private int getParagraphLeadingMargin(int line) {
if (!mSpannedText) {
return 0;
}
Spanned spanned = (Spanned) mText;
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
LeadingMarginSpan.class);
LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
LeadingMarginSpan.class);
if (spans.length == 0) {
return 0; // no leading margin span;
}
int margin = 0;
boolean useFirstLineMargin = lineStart == 0 || spanned.charAt(lineStart - 1) == '\n';
for (int i = 0; i < spans.length; i++) {
if (spans[i] instanceof LeadingMarginSpan2) {
int spStart = spanned.getSpanStart(spans[i]);
int spanLine = getLineForOffset(spStart);
int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
// if there is more than one LeadingMarginSpan2, use the count that is greatest
useFirstLineMargin |= line < spanLine + count;
}
}
for (int i = 0; i < spans.length; i++) {
LeadingMarginSpan span = spans[i];
margin += span.getLeadingMargin(useFirstLineMargin);
}
return margin;
}
private static float measurePara(TextPaint paint, CharSequence text, int start, int end,
TextDirectionHeuristic textDir) {
MeasuredParagraph mt = null;
TextLine tl = TextLine.obtain();
try {
mt = MeasuredParagraph.buildForBidi(text, start, end, textDir, mt);
final char[] chars = mt.getChars();
final int len = chars.length;
final Directions directions = mt.getDirections(0, len);
final int dir = mt.getParagraphDir();
boolean hasTabs = false;
TabStops tabStops = null;
// leading margins should be taken into account when measuring a paragraph
int margin = 0;
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
LeadingMarginSpan.class);
for (LeadingMarginSpan lms : spans) {
margin += lms.getLeadingMargin(true);
}
}
for (int i = 0; i < len; ++i) {
if (chars[i] == '\t') {
hasTabs = true;
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
int spanEnd = spanned.nextSpanTransition(start, end,
TabStopSpan.class);
TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
TabStopSpan.class);
if (spans.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, spans);
}
}
break;
}
}
tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops,
0 /* ellipsisStart */, 0 /* ellipsisEnd */);
return margin + Math.abs(tl.metrics(null));
} finally {
TextLine.recycle(tl);
if (mt != null) {
mt.recycle();
}
}
}
/**
* @hide
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public static class TabStops {
private float[] mStops;
private int mNumStops;
private float mIncrement;
public TabStops(float increment, Object[] spans) {
reset(increment, spans);
}
void reset(float increment, Object[] spans) {
this.mIncrement = increment;
int ns = 0;
if (spans != null) {
float[] stops = this.mStops;
for (Object o : spans) {
if (o instanceof TabStopSpan) {
if (stops == null) {
stops = new float[10];
} else if (ns == stops.length) {
float[] nstops = new float[ns * 2];
for (int i = 0; i < ns; ++i) {
nstops[i] = stops[i];
}
stops = nstops;
}
stops[ns++] = ((TabStopSpan) o).getTabStop();
}
}
if (ns > 1) {
Arrays.sort(stops, 0, ns);
}
if (stops != this.mStops) {
this.mStops = stops;
}
}
this.mNumStops = ns;
}
float nextTab(float h) {
int ns = this.mNumStops;
if (ns > 0) {
float[] stops = this.mStops;
for (int i = 0; i < ns; ++i) {
float stop = stops[i];
if (stop > h) {
return stop;
}
}
}
return nextDefaultStop(h, mIncrement);
}
/**
* Returns the position of next tab stop.
*/
public static float nextDefaultStop(float h, float inc) {
return ((int) ((h + inc) / inc)) * inc;
}
}
/**
* Returns the position of the next tab stop after h on the line.
*
* @param text the text
* @param start start of the line
* @param end limit of the line
* @param h the current horizontal offset
* @param tabs the tabs, can be null. If it is null, any tabs in effect
* on the line will be used. If there are no tabs, a default offset
* will be used to compute the tab stop.
* @return the offset of the next tab stop.
*/
/* package */ static float nextTab(CharSequence text, int start, int end,
float h, Object[] tabs) {
float nh = Float.MAX_VALUE;
boolean alltabs = false;
if (text instanceof Spanned) {
if (tabs == null) {
tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
alltabs = true;
}
for (int i = 0; i < tabs.length; i++) {
if (!alltabs) {
if (!(tabs[i] instanceof TabStopSpan))
continue;
}
int where = ((TabStopSpan) tabs[i]).getTabStop();
if (where < nh && where > h)
nh = where;
}
if (nh != Float.MAX_VALUE)
return nh;
}
return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
}
protected final boolean isSpanned() {
return mSpannedText;
}
/**
* Returns the same as <code>text.getSpans()</code>, except where
* <code>start</code> and <code>end</code> are the same and are not
* at the very beginning of the text, in which case an empty array
* is returned instead.
* <p>
* This is needed because of the special case that <code>getSpans()</code>
* on an empty range returns the spans adjacent to that range, which is
* primarily for the sake of <code>TextWatchers</code> so they will get
* notifications when text goes from empty to non-empty. But it also
* has the unfortunate side effect that if the text ends with an empty
* paragraph, that paragraph accidentally picks up the styles of the
* preceding paragraph (even though those styles will not be picked up
* by new text that is inserted into the empty paragraph).
* <p>
* The reason it just checks whether <code>start</code> and <code>end</code>
* is the same is that the only time a line can contain 0 characters
* is if it is the final paragraph of the Layout; otherwise any line will
* contain at least one printing or newline character. The reason for the
* additional check if <code>start</code> is greater than 0 is that
* if the empty paragraph is the entire content of the buffer, paragraph
* styles that are already applied to the buffer will apply to text that
* is inserted into it.
*/
/* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
if (start == end && start > 0) {
return ArrayUtils.emptyArray(type);
}
if(text instanceof SpannableStringBuilder) {
return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
} else {
return text.getSpans(start, end, type);
}
}
private void ellipsize(int start, int end, int line,
char[] dest, int destoff, TextUtils.TruncateAt method) {
final int ellipsisCount = getEllipsisCount(line);
if (ellipsisCount == 0) {
return;
}
final int ellipsisStart = getEllipsisStart(line);
final int lineStart = getLineStart(line);
final String ellipsisString = TextUtils.getEllipsisString(method);
final int ellipsisStringLen = ellipsisString.length();
// Use the ellipsis string only if there are that at least as many characters to replace.
final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
for (int i = 0; i < ellipsisCount; i++) {
final char c;
if (useEllipsisString && i < ellipsisStringLen) {
c = ellipsisString.charAt(i);
} else {
c = TextUtils.ELLIPSIS_FILLER;
}
final int a = i + ellipsisStart + lineStart;
if (start <= a && a < end) {
dest[destoff + a - start] = c;
}
}
}
/**
* Stores information about bidirectional (left-to-right or right-to-left)
* text within the layout of a line.
*/
public static class Directions {
/**
* Directions represents directional runs within a line of text. Runs are pairs of ints
* listed in visual order, starting from the leading margin. The first int of each pair is
* the offset from the first character of the line to the start of the run. The second int
* represents both the length and level of the run. The length is in the lower bits,
* accessed by masking with RUN_LENGTH_MASK. The level is in the higher bits, accessed by
* shifting by RUN_LEVEL_SHIFT and masking by RUN_LEVEL_MASK. To simply test for an RTL
* direction, test the bit using RUN_RTL_FLAG, if set then the direction is rtl.
* @hide
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public int[] mDirections;
/**
* @hide
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public Directions(int[] dirs) {
mDirections = dirs;
}
/**
* Returns number of BiDi runs.
*
* @hide
*/
public @IntRange(from = 0) int getRunCount() {
return mDirections.length / 2;
}
/**
* Returns the start offset of the BiDi run.
*
* @param runIndex the index of the BiDi run
* @return the start offset of the BiDi run.
* @hide
*/
public @IntRange(from = 0) int getRunStart(@IntRange(from = 0) int runIndex) {
return mDirections[runIndex * 2];
}
/**
* Returns the length of the BiDi run.
*
* Note that this method may return too large number due to reducing the number of object
* allocations. The too large number means the remaining part is assigned to this run. The
* caller must clamp the returned value.
*
* @param runIndex the index of the BiDi run
* @return the length of the BiDi run.
* @hide
*/
public @IntRange(from = 0) int getRunLength(@IntRange(from = 0) int runIndex) {
return mDirections[runIndex * 2 + 1] & RUN_LENGTH_MASK;
}
/**
* Returns true if the BiDi run is RTL.
*
* @param runIndex the index of the BiDi run
* @return true if the BiDi run is RTL.
* @hide
*/
public boolean isRunRtl(int runIndex) {
return (mDirections[runIndex * 2 + 1] & RUN_RTL_FLAG) != 0;
}
}
/**
* Return the offset of the first character to be ellipsized away,
* relative to the start of the line. (So 0 if the beginning of the
* line is ellipsized, not getLineStart().)
*/
public abstract int getEllipsisStart(int line);
/**
* Returns the number of characters to be ellipsized away, or 0 if
* no ellipsis is to take place.
*/
public abstract int getEllipsisCount(int line);
/* package */ static class Ellipsizer implements CharSequence, GetChars {
/* package */ CharSequence mText;
/* package */ Layout mLayout;
/* package */ int mWidth;
/* package */ TextUtils.TruncateAt mMethod;
public Ellipsizer(CharSequence s) {
mText = s;
}
public char charAt(int off) {
char[] buf = TextUtils.obtain(1);
getChars(off, off + 1, buf, 0);
char ret = buf[0];
TextUtils.recycle(buf);
return ret;
}
public void getChars(int start, int end, char[] dest, int destoff) {
int line1 = mLayout.getLineForOffset(start);
int line2 = mLayout.getLineForOffset(end);
TextUtils.getChars(mText, start, end, dest, destoff);
for (int i = line1; i <= line2; i++) {
mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
}
}
public int length() {
return mText.length();
}
public CharSequence subSequence(int start, int end) {
char[] s = new char[end - start];
getChars(start, end, s, 0);
return new String(s);
}
@Override
public String toString() {
char[] s = new char[length()];
getChars(0, length(), s, 0);
return new String(s);
}
}
/* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
private Spanned mSpanned;
public SpannedEllipsizer(CharSequence display) {
super(display);
mSpanned = (Spanned) display;
}
public <T> T[] getSpans(int start, int end, Class<T> type) {
return mSpanned.getSpans(start, end, type);
}
public int getSpanStart(Object tag) {
return mSpanned.getSpanStart(tag);
}
public int getSpanEnd(Object tag) {
return mSpanned.getSpanEnd(tag);
}
public int getSpanFlags(Object tag) {
return mSpanned.getSpanFlags(tag);
}
@SuppressWarnings("rawtypes")
public int nextSpanTransition(int start, int limit, Class type) {
return mSpanned.nextSpanTransition(start, limit, type);
}
@Override
public CharSequence subSequence(int start, int end) {
char[] s = new char[end - start];
getChars(start, end, s, 0);
SpannableString ss = new SpannableString(new String(s));
TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
return ss;
}
}
private CharSequence mText;
@UnsupportedAppUsage
private TextPaint mPaint;
private TextPaint mWorkPaint = new TextPaint();
private int mWidth;
private Alignment mAlignment = Alignment.ALIGN_NORMAL;
private float mSpacingMult;
private float mSpacingAdd;
private static final Rect sTempRect = new Rect();
private boolean mSpannedText;
private TextDirectionHeuristic mTextDir;
private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
private int mJustificationMode;
/** @hide */
@IntDef(prefix = { "DIR_" }, value = {
DIR_LEFT_TO_RIGHT,
DIR_RIGHT_TO_LEFT
})
@Retention(RetentionPolicy.SOURCE)
public @interface Direction {}
public static final int DIR_LEFT_TO_RIGHT = 1;
public static final int DIR_RIGHT_TO_LEFT = -1;
/* package */ static final int DIR_REQUEST_LTR = 1;
/* package */ static final int DIR_REQUEST_RTL = -1;
@UnsupportedAppUsage
/* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
/* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
/* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
/* package */ static final int RUN_LEVEL_SHIFT = 26;
/* package */ static final int RUN_LEVEL_MASK = 0x3f;
/* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
public enum Alignment {
ALIGN_NORMAL,
ALIGN_OPPOSITE,
ALIGN_CENTER,
/** @hide */
@UnsupportedAppUsage
ALIGN_LEFT,
/** @hide */
@UnsupportedAppUsage
ALIGN_RIGHT,
}
private static final float TAB_INCREMENT = 20;
/** @hide */
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
@UnsupportedAppUsage
public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
new Directions(new int[] { 0, RUN_LENGTH_MASK });
/** @hide */
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
@UnsupportedAppUsage
public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = { "TEXT_SELECTION_LAYOUT_" }, value = {
TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT,
TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT
})
public @interface TextSelectionLayout {}
/** @hide */
public static final int TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT = 0;
/** @hide */
public static final int TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT = 1;
/** @hide */
@FunctionalInterface
public interface SelectionRectangleConsumer {
/**
* Performs this operation on the given rectangle.
*
* @param left the left edge of the rectangle
* @param top the top edge of the rectangle
* @param right the right edge of the rectangle
* @param bottom the bottom edge of the rectangle
* @param textSelectionLayout the layout (RTL or LTR) of the text covered by this
* selection rectangle
*/
void accept(float left, float top, float right, float bottom,
@TextSelectionLayout int textSelectionLayout);
}
}
|