usvg/text/layout.rs
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
// 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/.
use std::collections::HashMap;
use std::num::NonZeroU16;
use std::sync::Arc;
use fontdb::{Database, ID};
use kurbo::{ParamCurve, ParamCurveArclen, ParamCurveDeriv};
use rustybuzz::ttf_parser;
use rustybuzz::ttf_parser::{GlyphId, Tag};
use strict_num::NonZeroPositiveF32;
use tiny_skia_path::{NonZeroRect, Transform};
use unicode_script::UnicodeScript;
use crate::tree::{BBox, IsValidLength};
use crate::{
AlignmentBaseline, ApproxZeroUlps, BaselineShift, DominantBaseline, Fill, FillRule, Font,
FontResolver, LengthAdjust, PaintOrder, Path, ShapeRendering, Stroke, Text, TextAnchor,
TextChunk, TextDecorationStyle, TextFlow, TextPath, TextSpan, WritingMode,
};
/// A glyph that has already been positioned correctly.
///
/// Note that the transform already takes the font size into consideration, so applying the
/// transform to the outline of the glyphs is all that is necessary to display it correctly.
#[derive(Clone, Debug)]
pub struct PositionedGlyph {
/// Returns the transform of the glyph itself within the cluster. For example,
/// for zalgo text, it contains the transform to position the glyphs above/below
/// the main glyph.
glyph_ts: Transform,
/// Returns the transform of the whole cluster that the glyph is part of.
cluster_ts: Transform,
/// Returns the transform of the span that the glyph is a part of.
span_ts: Transform,
/// The units per em of the font the glyph belongs to.
units_per_em: u16,
/// The font size the glyph should be scaled to.
font_size: f32,
/// The ID of the glyph.
pub id: GlyphId,
/// The text from the original string that corresponds to that glyph.
pub text: String,
/// The ID of the font the glyph should be taken from. Can be used with the
/// [font database of the tree](crate::Tree::fontdb) this glyph is part of.
pub font: ID,
}
impl PositionedGlyph {
/// Returns the transform of glyph, assuming that an outline
/// glyph is being used (i.e. from the `glyf` or `CFF/CFF2` table).
pub fn outline_transform(&self) -> Transform {
let mut ts = Transform::identity();
// Outlines are mirrored by default.
ts = ts.pre_scale(1.0, -1.0);
let sx = self.font_size / self.units_per_em as f32;
ts = ts.pre_scale(sx, sx);
ts = ts
.pre_concat(self.glyph_ts)
.post_concat(self.cluster_ts)
.post_concat(self.span_ts);
ts
}
/// Returns the transform for the glyph, assuming that a CBTD-based raster glyph
/// is being used.
pub fn cbdt_transform(&self, x: f32, y: f32, pixels_per_em: f32, height: f32) -> Transform {
self.span_ts
.pre_concat(self.cluster_ts)
.pre_concat(self.glyph_ts)
.pre_concat(Transform::from_scale(
self.font_size / pixels_per_em,
self.font_size / pixels_per_em,
))
// Right now, the top-left corner of the image would be placed in
// on the "text cursor", but we want the bottom-left corner to be there,
// so we need to shift it up and also apply the x/y offset.
.pre_translate(x, -height - y)
}
/// Returns the transform for the glyph, assuming that a sbix-based raster glyph
/// is being used.
pub fn sbix_transform(
&self,
x: f32,
y: f32,
x_min: f32,
y_min: f32,
pixels_per_em: f32,
height: f32,
) -> Transform {
// In contrast to CBDT, we also need to look at the outline bbox of the glyph and add a shift if necessary.
let bbox_x_shift = self.font_size * (-x_min / self.units_per_em as f32);
let bbox_y_shift = if y_min.approx_zero_ulps(4) {
// For unknown reasons, using Apple Color Emoji will lead to a vertical shift on MacOS, but this shift
// doesn't seem to be coming from the font and most likely is somehow hardcoded. On Windows,
// this shift will not be applied. However, if this shift is not applied the emojis are a bit
// too high up when being together with other text, so we try to imitate this.
// See also https://github.com/harfbuzz/harfbuzz/issues/2679#issuecomment-1345595425
// So whenever the y-shift is 0, we approximate this vertical shift that seems to be produced by it.
// This value seems to be pretty close to what is happening on MacOS.
// We can still remove this if it turns out to be a problem, but Apple Color Emoji is pretty
// much the only `sbix` font out there and they all seem to have a y-shift of 0, so it
// makes sense to keep it.
0.128 * self.font_size
} else {
self.font_size * (-y_min / self.units_per_em as f32)
};
self.span_ts
.pre_concat(self.cluster_ts)
.pre_concat(self.glyph_ts)
.pre_concat(Transform::from_translate(bbox_x_shift, bbox_y_shift))
.pre_concat(Transform::from_scale(
self.font_size / pixels_per_em,
self.font_size / pixels_per_em,
))
// Right now, the top-left corner of the image would be placed in
// on the "text cursor", but we want the bottom-left corner to be there,
// so we need to shift it up and also apply the x/y offset.
.pre_translate(x, -height - y)
}
/// Returns the transform for the glyph, assuming that an SVG glyph is
/// being used.
pub fn svg_transform(&self) -> Transform {
let mut ts = Transform::identity();
let sx = self.font_size / self.units_per_em as f32;
ts = ts.pre_scale(sx, sx);
ts = ts
.pre_concat(self.glyph_ts)
.post_concat(self.cluster_ts)
.post_concat(self.span_ts);
ts
}
/// Returns the transform for the glyph, assuming that a COLR glyph is
/// being used.
pub fn colr_transform(&self) -> Transform {
self.outline_transform()
}
}
/// A span contains a number of layouted glyphs that share the same fill, stroke, paint order and
/// visibility.
#[derive(Clone, Debug)]
pub struct Span {
/// The fill of the span.
pub fill: Option<Fill>,
/// The stroke of the span.
pub stroke: Option<Stroke>,
/// The paint order of the span.
pub paint_order: PaintOrder,
/// The font size of the span.
pub font_size: NonZeroPositiveF32,
/// The visibility of the span.
pub visible: bool,
/// The glyphs that make up the span.
pub positioned_glyphs: Vec<PositionedGlyph>,
/// An underline text decoration of the span.
/// Needs to be rendered before all glyphs.
pub underline: Option<Path>,
/// An overline text decoration of the span.
/// Needs to be rendered before all glyphs.
pub overline: Option<Path>,
/// A line-through text decoration of the span.
/// Needs to be rendered after all glyphs.
pub line_through: Option<Path>,
}
#[derive(Clone, Debug)]
struct GlyphCluster {
byte_idx: ByteIndex,
codepoint: char,
width: f32,
advance: f32,
ascent: f32,
descent: f32,
has_relative_shift: bool,
glyphs: Vec<PositionedGlyph>,
transform: Transform,
path_transform: Transform,
visible: bool,
}
impl GlyphCluster {
pub(crate) fn height(&self) -> f32 {
self.ascent - self.descent
}
pub(crate) fn transform(&self) -> Transform {
self.path_transform.post_concat(self.transform)
}
}
pub(crate) fn layout_text(
text_node: &Text,
resolver: &FontResolver,
fontdb: &mut Arc<fontdb::Database>,
) -> Option<(Vec<Span>, NonZeroRect)> {
let mut fonts_cache: FontsCache = HashMap::new();
for chunk in &text_node.chunks {
for span in &chunk.spans {
if !fonts_cache.contains_key(&span.font) {
if let Some(font) =
(resolver.select_font)(&span.font, fontdb).and_then(|id| fontdb.load_font(id))
{
fonts_cache.insert(span.font.clone(), Arc::new(font));
}
}
}
}
let mut spans = vec![];
let mut char_offset = 0;
let mut last_x = 0.0;
let mut last_y = 0.0;
let mut bbox = BBox::default();
for chunk in &text_node.chunks {
let (x, y) = match chunk.text_flow {
TextFlow::Linear => (chunk.x.unwrap_or(last_x), chunk.y.unwrap_or(last_y)),
TextFlow::Path(_) => (0.0, 0.0),
};
let mut clusters = process_chunk(chunk, &fonts_cache, resolver, fontdb);
if clusters.is_empty() {
char_offset += chunk.text.chars().count();
continue;
}
apply_writing_mode(text_node.writing_mode, &mut clusters);
apply_letter_spacing(chunk, &mut clusters);
apply_word_spacing(chunk, &mut clusters);
apply_length_adjust(chunk, &mut clusters);
let mut curr_pos = resolve_clusters_positions(
text_node,
chunk,
char_offset,
text_node.writing_mode,
&fonts_cache,
&mut clusters,
);
let mut text_ts = Transform::default();
if text_node.writing_mode == WritingMode::TopToBottom {
if let TextFlow::Linear = chunk.text_flow {
text_ts = text_ts.pre_rotate_at(90.0, x, y);
}
}
for span in &chunk.spans {
let font = match fonts_cache.get(&span.font) {
Some(v) => v,
None => continue,
};
let decoration_spans = collect_decoration_spans(span, &clusters);
let mut span_ts = text_ts;
span_ts = span_ts.pre_translate(x, y);
if let TextFlow::Linear = chunk.text_flow {
let shift = resolve_baseline(span, font, text_node.writing_mode);
// In case of a horizontal flow, shift transform and not clusters,
// because clusters can be rotated and an additional shift will lead
// to invalid results.
span_ts = span_ts.pre_translate(0.0, shift);
}
let mut underline = None;
let mut overline = None;
let mut line_through = None;
if let Some(decoration) = span.decoration.underline.clone() {
// TODO: No idea what offset should be used for top-to-bottom layout.
// There is
// https://www.w3.org/TR/css-text-decor-3/#text-underline-position-property
// but it doesn't go into details.
let offset = match text_node.writing_mode {
WritingMode::LeftToRight => -font.underline_position(span.font_size.get()),
WritingMode::TopToBottom => font.height(span.font_size.get()) / 2.0,
};
if let Some(path) =
convert_decoration(offset, span, font, decoration, &decoration_spans, span_ts)
{
bbox = bbox.expand(path.data.bounds());
underline = Some(path);
}
}
if let Some(decoration) = span.decoration.overline.clone() {
let offset = match text_node.writing_mode {
WritingMode::LeftToRight => -font.ascent(span.font_size.get()),
WritingMode::TopToBottom => -font.height(span.font_size.get()) / 2.0,
};
if let Some(path) =
convert_decoration(offset, span, font, decoration, &decoration_spans, span_ts)
{
bbox = bbox.expand(path.data.bounds());
overline = Some(path);
}
}
if let Some(decoration) = span.decoration.line_through.clone() {
let offset = match text_node.writing_mode {
WritingMode::LeftToRight => -font.line_through_position(span.font_size.get()),
WritingMode::TopToBottom => 0.0,
};
if let Some(path) =
convert_decoration(offset, span, font, decoration, &decoration_spans, span_ts)
{
bbox = bbox.expand(path.data.bounds());
line_through = Some(path);
}
}
let mut fill = span.fill.clone();
if let Some(ref mut fill) = fill {
// The `fill-rule` should be ignored.
// https://www.w3.org/TR/SVG2/text.html#TextRenderingOrder
//
// 'Since the fill-rule property does not apply to SVG text elements,
// the specific order of the subpaths within the equivalent path does not matter.'
fill.rule = FillRule::NonZero;
}
if let Some((span_fragments, span_bbox)) = convert_span(span, &clusters, span_ts) {
bbox = bbox.expand(span_bbox);
let positioned_glyphs = span_fragments
.into_iter()
.flat_map(|mut gc| {
let cluster_ts = gc.transform();
gc.glyphs.iter_mut().for_each(|pg| {
pg.cluster_ts = cluster_ts;
pg.span_ts = span_ts;
});
gc.glyphs
})
.collect();
spans.push(Span {
fill,
stroke: span.stroke.clone(),
paint_order: span.paint_order,
font_size: span.font_size,
visible: span.visible,
positioned_glyphs,
underline,
overline,
line_through,
});
}
}
char_offset += chunk.text.chars().count();
if text_node.writing_mode == WritingMode::TopToBottom {
if let TextFlow::Linear = chunk.text_flow {
std::mem::swap(&mut curr_pos.0, &mut curr_pos.1);
}
}
last_x = x + curr_pos.0;
last_y = y + curr_pos.1;
}
let bbox = bbox.to_non_zero_rect()?;
Some((spans, bbox))
}
fn convert_span(
span: &TextSpan,
clusters: &[GlyphCluster],
text_ts: Transform,
) -> Option<(Vec<GlyphCluster>, NonZeroRect)> {
let mut span_clusters = vec![];
let mut bboxes_builder = tiny_skia_path::PathBuilder::new();
for cluster in clusters {
if !cluster.visible {
continue;
}
if span_contains(span, cluster.byte_idx) {
span_clusters.push(cluster.clone());
}
let mut advance = cluster.advance;
if advance <= 0.0 {
advance = 1.0;
}
// We have to calculate text bbox using font metrics and not glyph shape.
if let Some(r) = NonZeroRect::from_xywh(0.0, -cluster.ascent, advance, cluster.height()) {
if let Some(r) = r.transform(cluster.transform()) {
bboxes_builder.push_rect(r.to_rect());
}
}
}
let mut bboxes = bboxes_builder.finish()?;
bboxes = bboxes.transform(text_ts)?;
let bbox = bboxes.compute_tight_bounds()?.to_non_zero_rect()?;
Some((span_clusters, bbox))
}
fn collect_decoration_spans(span: &TextSpan, clusters: &[GlyphCluster]) -> Vec<DecorationSpan> {
let mut spans = Vec::new();
let mut started = false;
let mut width = 0.0;
let mut transform = Transform::default();
for cluster in clusters {
if span_contains(span, cluster.byte_idx) {
if started && cluster.has_relative_shift {
started = false;
spans.push(DecorationSpan { width, transform });
}
if !started {
width = cluster.advance;
started = true;
transform = cluster.transform;
} else {
width += cluster.advance;
}
} else if started {
spans.push(DecorationSpan { width, transform });
started = false;
}
}
if started {
spans.push(DecorationSpan { width, transform });
}
spans
}
pub(crate) fn convert_decoration(
dy: f32,
span: &TextSpan,
font: &ResolvedFont,
mut decoration: TextDecorationStyle,
decoration_spans: &[DecorationSpan],
transform: Transform,
) -> Option<Path> {
debug_assert!(!decoration_spans.is_empty());
let thickness = font.underline_thickness(span.font_size.get());
let mut builder = tiny_skia_path::PathBuilder::new();
for dec_span in decoration_spans {
let rect = match NonZeroRect::from_xywh(0.0, -thickness / 2.0, dec_span.width, thickness) {
Some(v) => v,
None => {
log::warn!("a decoration span has a malformed bbox");
continue;
}
};
let ts = dec_span.transform.pre_translate(0.0, dy);
let mut path = tiny_skia_path::PathBuilder::from_rect(rect.to_rect());
path = match path.transform(ts) {
Some(v) => v,
None => continue,
};
builder.push_path(&path);
}
let mut path_data = builder.finish()?;
path_data = path_data.transform(transform)?;
Path::new(
String::new(),
span.visible,
decoration.fill.take(),
decoration.stroke.take(),
PaintOrder::default(),
ShapeRendering::default(),
Arc::new(path_data),
Transform::default(),
)
}
/// A text decoration span.
///
/// Basically a horizontal line, that will be used for underline, overline and line-through.
/// It doesn't have a height, since it depends on the Font metrics.
#[derive(Clone, Copy)]
pub(crate) struct DecorationSpan {
pub(crate) width: f32,
pub(crate) transform: Transform,
}
/// Resolves clusters positions.
///
/// Mainly sets the `transform` property.
///
/// Returns the last text position. The next text chunk should start from that position.
fn resolve_clusters_positions(
text: &Text,
chunk: &TextChunk,
char_offset: usize,
writing_mode: WritingMode,
fonts_cache: &FontsCache,
clusters: &mut [GlyphCluster],
) -> (f32, f32) {
match chunk.text_flow {
TextFlow::Linear => {
resolve_clusters_positions_horizontal(text, chunk, char_offset, writing_mode, clusters)
}
TextFlow::Path(ref path) => resolve_clusters_positions_path(
text,
chunk,
char_offset,
path,
writing_mode,
fonts_cache,
clusters,
),
}
}
fn clusters_length(clusters: &[GlyphCluster]) -> f32 {
clusters.iter().fold(0.0, |w, cluster| w + cluster.advance)
}
fn resolve_clusters_positions_horizontal(
text: &Text,
chunk: &TextChunk,
offset: usize,
writing_mode: WritingMode,
clusters: &mut [GlyphCluster],
) -> (f32, f32) {
let mut x = process_anchor(chunk.anchor, clusters_length(clusters));
let mut y = 0.0;
for cluster in clusters {
let cp = offset + cluster.byte_idx.code_point_at(&chunk.text);
if let (Some(dx), Some(dy)) = (text.dx.get(cp), text.dy.get(cp)) {
if writing_mode == WritingMode::LeftToRight {
x += dx;
y += dy;
} else {
y -= dx;
x += dy;
}
cluster.has_relative_shift = !dx.approx_zero_ulps(4) || !dy.approx_zero_ulps(4);
}
cluster.transform = cluster.transform.pre_translate(x, y);
if let Some(angle) = text.rotate.get(cp).cloned() {
if !angle.approx_zero_ulps(4) {
cluster.transform = cluster.transform.pre_rotate(angle);
cluster.has_relative_shift = true;
}
}
x += cluster.advance;
}
(x, y)
}
// Baseline resolving in SVG is a mess.
// Not only it's poorly documented, but as soon as you start mixing
// `dominant-baseline` and `alignment-baseline` each application/browser will produce
// different results.
//
// For now, resvg simply tries to match Chrome's output and not the mythical SVG spec output.
//
// See `alignment_baseline_shift` method comment for more details.
pub(crate) fn resolve_baseline(
span: &TextSpan,
font: &ResolvedFont,
writing_mode: WritingMode,
) -> f32 {
let mut shift = -resolve_baseline_shift(&span.baseline_shift, font, span.font_size.get());
// TODO: support vertical layout as well
if writing_mode == WritingMode::LeftToRight {
if span.alignment_baseline == AlignmentBaseline::Auto
|| span.alignment_baseline == AlignmentBaseline::Baseline
{
shift += font.dominant_baseline_shift(span.dominant_baseline, span.font_size.get());
} else {
shift += font.alignment_baseline_shift(span.alignment_baseline, span.font_size.get());
}
}
shift
}
fn resolve_baseline_shift(baselines: &[BaselineShift], font: &ResolvedFont, font_size: f32) -> f32 {
let mut shift = 0.0;
for baseline in baselines.iter().rev() {
match baseline {
BaselineShift::Baseline => {}
BaselineShift::Subscript => shift -= font.subscript_offset(font_size),
BaselineShift::Superscript => shift += font.superscript_offset(font_size),
BaselineShift::Number(n) => shift += n,
}
}
shift
}
fn resolve_clusters_positions_path(
text: &Text,
chunk: &TextChunk,
char_offset: usize,
path: &TextPath,
writing_mode: WritingMode,
fonts_cache: &FontsCache,
clusters: &mut [GlyphCluster],
) -> (f32, f32) {
let mut last_x = 0.0;
let mut last_y = 0.0;
let mut dy = 0.0;
// In the text path mode, chunk's x/y coordinates provide an additional offset along the path.
// The X coordinate is used in a horizontal mode, and Y in vertical.
let chunk_offset = match writing_mode {
WritingMode::LeftToRight => chunk.x.unwrap_or(0.0),
WritingMode::TopToBottom => chunk.y.unwrap_or(0.0),
};
let start_offset =
chunk_offset + path.start_offset + process_anchor(chunk.anchor, clusters_length(clusters));
let normals = collect_normals(text, chunk, clusters, &path.path, char_offset, start_offset);
for (cluster, normal) in clusters.iter_mut().zip(normals) {
let (x, y, angle) = match normal {
Some(normal) => (normal.x, normal.y, normal.angle),
None => {
// Hide clusters that are outside the text path.
cluster.visible = false;
continue;
}
};
// We have to break a decoration line for each cluster during text-on-path.
cluster.has_relative_shift = true;
let orig_ts = cluster.transform;
// Clusters should be rotated by the x-midpoint x baseline position.
let half_width = cluster.width / 2.0;
cluster.transform = Transform::default();
cluster.transform = cluster.transform.pre_translate(x - half_width, y);
cluster.transform = cluster.transform.pre_rotate_at(angle, half_width, 0.0);
let cp = char_offset + cluster.byte_idx.code_point_at(&chunk.text);
dy += text.dy.get(cp).cloned().unwrap_or(0.0);
let baseline_shift = chunk_span_at(chunk, cluster.byte_idx)
.map(|span| {
let font = match fonts_cache.get(&span.font) {
Some(v) => v,
None => return 0.0,
};
-resolve_baseline(span, font, writing_mode)
})
.unwrap_or(0.0);
// Shift only by `dy` since we already applied `dx`
// during offset along the path calculation.
if !dy.approx_zero_ulps(4) || !baseline_shift.approx_zero_ulps(4) {
let shift = kurbo::Vec2::new(0.0, (dy - baseline_shift) as f64);
cluster.transform = cluster
.transform
.pre_translate(shift.x as f32, shift.y as f32);
}
if let Some(angle) = text.rotate.get(cp).cloned() {
if !angle.approx_zero_ulps(4) {
cluster.transform = cluster.transform.pre_rotate(angle);
}
}
// The possible `lengthAdjust` transform should be applied after text-on-path positioning.
cluster.transform = cluster.transform.pre_concat(orig_ts);
last_x = x + cluster.advance;
last_y = y;
}
(last_x, last_y)
}
pub(crate) fn process_anchor(a: TextAnchor, text_width: f32) -> f32 {
match a {
TextAnchor::Start => 0.0, // Nothing.
TextAnchor::Middle => -text_width / 2.0,
TextAnchor::End => -text_width,
}
}
pub(crate) struct PathNormal {
pub(crate) x: f32,
pub(crate) y: f32,
pub(crate) angle: f32,
}
fn collect_normals(
text: &Text,
chunk: &TextChunk,
clusters: &[GlyphCluster],
path: &tiny_skia_path::Path,
char_offset: usize,
offset: f32,
) -> Vec<Option<PathNormal>> {
let mut offsets = Vec::with_capacity(clusters.len());
let mut normals = Vec::with_capacity(clusters.len());
{
let mut advance = offset;
for cluster in clusters {
// Clusters should be rotated by the x-midpoint x baseline position.
let half_width = cluster.width / 2.0;
// Include relative position.
let cp = char_offset + cluster.byte_idx.code_point_at(&chunk.text);
advance += text.dx.get(cp).cloned().unwrap_or(0.0);
let offset = advance + half_width;
// Clusters outside the path have no normals.
if offset < 0.0 {
normals.push(None);
}
offsets.push(offset as f64);
advance += cluster.advance;
}
}
let mut prev_mx = path.points()[0].x;
let mut prev_my = path.points()[0].y;
let mut prev_x = prev_mx;
let mut prev_y = prev_my;
fn create_curve_from_line(px: f32, py: f32, x: f32, y: f32) -> kurbo::CubicBez {
let line = kurbo::Line::new(
kurbo::Point::new(px as f64, py as f64),
kurbo::Point::new(x as f64, y as f64),
);
let p1 = line.eval(0.33);
let p2 = line.eval(0.66);
kurbo::CubicBez {
p0: line.p0,
p1,
p2,
p3: line.p1,
}
}
let mut length: f64 = 0.0;
for seg in path.segments() {
let curve = match seg {
tiny_skia_path::PathSegment::MoveTo(p) => {
prev_mx = p.x;
prev_my = p.y;
prev_x = p.x;
prev_y = p.y;
continue;
}
tiny_skia_path::PathSegment::LineTo(p) => {
create_curve_from_line(prev_x, prev_y, p.x, p.y)
}
tiny_skia_path::PathSegment::QuadTo(p1, p) => kurbo::QuadBez {
p0: kurbo::Point::new(prev_x as f64, prev_y as f64),
p1: kurbo::Point::new(p1.x as f64, p1.y as f64),
p2: kurbo::Point::new(p.x as f64, p.y as f64),
}
.raise(),
tiny_skia_path::PathSegment::CubicTo(p1, p2, p) => kurbo::CubicBez {
p0: kurbo::Point::new(prev_x as f64, prev_y as f64),
p1: kurbo::Point::new(p1.x as f64, p1.y as f64),
p2: kurbo::Point::new(p2.x as f64, p2.y as f64),
p3: kurbo::Point::new(p.x as f64, p.y as f64),
},
tiny_skia_path::PathSegment::Close => {
create_curve_from_line(prev_x, prev_y, prev_mx, prev_my)
}
};
let arclen_accuracy = {
let base_arclen_accuracy = 0.5;
// Accuracy depends on a current scale.
// When we have a tiny path scaled by a large value,
// we have to increase out accuracy accordingly.
let (sx, sy) = text.abs_transform.get_scale();
// 1.0 acts as a threshold to prevent division by 0 and/or low accuracy.
base_arclen_accuracy / (sx * sy).sqrt().max(1.0)
};
let curve_len = curve.arclen(arclen_accuracy as f64);
for offset in &offsets[normals.len()..] {
if *offset >= length && *offset <= length + curve_len {
let mut offset = curve.inv_arclen(offset - length, arclen_accuracy as f64);
// some rounding error may occur, so we give offset a little tolerance
debug_assert!((-1.0e-3..=1.0 + 1.0e-3).contains(&offset));
offset = offset.min(1.0).max(0.0);
let pos = curve.eval(offset);
let d = curve.deriv().eval(offset);
let d = kurbo::Vec2::new(-d.y, d.x); // tangent
let angle = d.atan2().to_degrees() - 90.0;
normals.push(Some(PathNormal {
x: pos.x as f32,
y: pos.y as f32,
angle: angle as f32,
}));
if normals.len() == offsets.len() {
break;
}
}
}
length += curve_len;
prev_x = curve.p3.x as f32;
prev_y = curve.p3.y as f32;
}
// If path ended and we still have unresolved normals - set them to `None`.
for _ in 0..(offsets.len() - normals.len()) {
normals.push(None);
}
normals
}
/// Converts a text chunk into a list of outlined clusters.
///
/// This function will do the BIDI reordering, text shaping and glyphs outlining,
/// but not the text layouting. So all clusters are in the 0x0 position.
fn process_chunk(
chunk: &TextChunk,
fonts_cache: &FontsCache,
resolver: &FontResolver,
fontdb: &mut Arc<fontdb::Database>,
) -> Vec<GlyphCluster> {
// The way this function works is a bit tricky.
//
// The first problem is BIDI reordering.
// We cannot shape text span-by-span, because glyph clusters are not guarantee to be continuous.
//
// For example:
// <text>Hel<tspan fill="url(#lg1)">lo של</tspan>ום.</text>
//
// Would be shaped as:
// H e l l o ש ל ו ם . (characters)
// 0 1 2 3 4 5 12 10 8 6 14 (cluster indices in UTF-8)
// --- --- (green span)
//
// As you can see, our continuous `lo של` span was split into two separated one.
// So our 3 spans: black - green - black, become 5 spans: black - green - black - green - black.
// If we shape `Hel`, then `lo של` an then `ום` separately - we would get an incorrect output.
// To properly handle this we simply shape the whole chunk.
//
// But this introduces another issue - what to do when we have multiple fonts?
// The easy solution would be to simply shape text with each font,
// where the first font output is used as a base one and all others overwrite it.
// This way in case of:
// <text font-family="Arial">Hello <tspan font-family="Helvetica">world</tspan></text>
// we would replace Arial glyphs for `world` with Helvetica one. Pretty simple.
//
// Well, it would work most of the time, but not always.
// This is because different fonts can produce different amount of glyphs for the same text.
// The most common example are ligatures. Some fonts can shape `fi` as two glyphs `f` and `i`,
// but some can use `fi` (U+FB01) instead.
// Meaning that during merging we have to overwrite not individual glyphs, but clusters.
let mut glyphs = Vec::new();
for span in &chunk.spans {
let font = match fonts_cache.get(&span.font) {
Some(v) => v.clone(),
None => continue,
};
let tmp_glyphs = shape_text(
&chunk.text,
font,
span.small_caps,
span.apply_kerning,
resolver,
fontdb,
);
// Do nothing with the first run.
if glyphs.is_empty() {
glyphs = tmp_glyphs;
continue;
}
// Overwrite span's glyphs.
let mut iter = tmp_glyphs.into_iter();
while let Some(new_glyph) = iter.next() {
if !span_contains(span, new_glyph.byte_idx) {
continue;
}
let Some(idx) = glyphs.iter().position(|g| g.byte_idx == new_glyph.byte_idx) else {
continue;
};
let prev_cluster_len = glyphs[idx].cluster_len;
if prev_cluster_len < new_glyph.cluster_len {
// If the new font represents the same cluster with fewer glyphs
// then remove remaining glyphs.
for _ in 1..new_glyph.cluster_len {
glyphs.remove(idx + 1);
}
} else if prev_cluster_len > new_glyph.cluster_len {
// If the new font represents the same cluster with more glyphs
// then insert them after the current one.
for j in 1..prev_cluster_len {
if let Some(g) = iter.next() {
glyphs.insert(idx + j, g);
}
}
}
glyphs[idx] = new_glyph;
}
}
// Convert glyphs to clusters.
let mut clusters = Vec::new();
for (range, byte_idx) in GlyphClusters::new(&glyphs) {
if let Some(span) = chunk_span_at(chunk, byte_idx) {
clusters.push(form_glyph_clusters(
&glyphs[range],
&chunk.text,
span.font_size.get(),
));
}
}
clusters
}
fn apply_length_adjust(chunk: &TextChunk, clusters: &mut [GlyphCluster]) {
let is_horizontal = matches!(chunk.text_flow, TextFlow::Linear);
for span in &chunk.spans {
let target_width = match span.text_length {
Some(v) => v,
None => continue,
};
let mut width = 0.0;
let mut cluster_indexes = Vec::new();
for i in span.start..span.end {
if let Some(index) = clusters.iter().position(|c| c.byte_idx.value() == i) {
cluster_indexes.push(index);
}
}
// Complex scripts can have mutli-codepoint clusters therefore we have to remove duplicates.
cluster_indexes.sort();
cluster_indexes.dedup();
for i in &cluster_indexes {
// Use the original cluster `width` and not `advance`.
// This method essentially discards any `word-spacing` and `letter-spacing`.
width += clusters[*i].width;
}
if cluster_indexes.is_empty() {
continue;
}
if span.length_adjust == LengthAdjust::Spacing {
let factor = if cluster_indexes.len() > 1 {
(target_width - width) / (cluster_indexes.len() - 1) as f32
} else {
0.0
};
for i in cluster_indexes {
clusters[i].advance = clusters[i].width + factor;
}
} else {
let factor = target_width / width;
// Prevent multiplying by zero.
if factor < 0.001 {
continue;
}
for i in cluster_indexes {
clusters[i].transform = clusters[i].transform.pre_scale(factor, 1.0);
// Technically just a hack to support the current text-on-path algorithm.
if !is_horizontal {
clusters[i].advance *= factor;
clusters[i].width *= factor;
}
}
}
}
}
/// Rotates clusters according to
/// [Unicode Vertical_Orientation Property](https://www.unicode.org/reports/tr50/tr50-19.html).
fn apply_writing_mode(writing_mode: WritingMode, clusters: &mut [GlyphCluster]) {
if writing_mode != WritingMode::TopToBottom {
return;
}
for cluster in clusters {
let orientation = unicode_vo::char_orientation(cluster.codepoint);
if orientation == unicode_vo::Orientation::Upright {
let mut ts = Transform::default();
// Position glyph in the center of vertical axis.
ts = ts.pre_translate(0.0, (cluster.ascent + cluster.descent) / 2.0);
// Rotate by 90 degrees in the center.
ts = ts.pre_rotate_at(
-90.0,
cluster.width / 2.0,
-(cluster.ascent + cluster.descent) / 2.0,
);
cluster.path_transform = ts;
// Move "baseline" to the middle and make height equal to width.
cluster.ascent = cluster.width / 2.0;
cluster.descent = -cluster.width / 2.0;
} else {
// Could not find a spec that explains this,
// but this is how other applications are shifting the "rotated" characters
// in the top-to-bottom mode.
cluster.transform = cluster
.transform
.pre_translate(0.0, (cluster.ascent + cluster.descent) / 2.0);
}
}
}
/// Applies the `letter-spacing` property to a text chunk clusters.
///
/// [In the CSS spec](https://www.w3.org/TR/css-text-3/#letter-spacing-property).
fn apply_letter_spacing(chunk: &TextChunk, clusters: &mut [GlyphCluster]) {
// At least one span should have a non-zero spacing.
if !chunk
.spans
.iter()
.any(|span| !span.letter_spacing.approx_zero_ulps(4))
{
return;
}
let num_clusters = clusters.len();
for (i, cluster) in clusters.iter_mut().enumerate() {
// Spacing must be applied only to characters that belongs to the script
// that supports spacing.
// We are checking only the first code point, since it should be enough.
// https://www.w3.org/TR/css-text-3/#cursive-tracking
let script = cluster.codepoint.script();
if script_supports_letter_spacing(script) {
if let Some(span) = chunk_span_at(chunk, cluster.byte_idx) {
// A space after the last cluster should be ignored,
// since it affects the bbox and text alignment.
if i != num_clusters - 1 {
cluster.advance += span.letter_spacing;
}
// If the cluster advance became negative - clear it.
// This is an UB so we can do whatever we want, and we mimic Chrome's behavior.
if !cluster.advance.is_valid_length() {
cluster.width = 0.0;
cluster.advance = 0.0;
cluster.glyphs = vec![];
}
}
}
}
}
/// Applies the `word-spacing` property to a text chunk clusters.
///
/// [In the CSS spec](https://www.w3.org/TR/css-text-3/#propdef-word-spacing).
fn apply_word_spacing(chunk: &TextChunk, clusters: &mut [GlyphCluster]) {
// At least one span should have a non-zero spacing.
if !chunk
.spans
.iter()
.any(|span| !span.word_spacing.approx_zero_ulps(4))
{
return;
}
for cluster in clusters {
if is_word_separator_characters(cluster.codepoint) {
if let Some(span) = chunk_span_at(chunk, cluster.byte_idx) {
// Technically, word spacing 'should be applied half on each
// side of the character', but it doesn't affect us in any way,
// so we are ignoring this.
cluster.advance += span.word_spacing;
// After word spacing, `advance` can be negative.
}
}
}
}
fn form_glyph_clusters(glyphs: &[Glyph], text: &str, font_size: f32) -> GlyphCluster {
debug_assert!(!glyphs.is_empty());
let mut width = 0.0;
let mut x: f32 = 0.0;
let mut positioned_glyphs = vec![];
for glyph in glyphs {
let sx = glyph.font.scale(font_size);
// Apply offset.
//
// The first glyph in the cluster will have an offset from 0x0,
// but the later one will have an offset from the "current position".
// So we have to keep an advance.
// TODO: should be done only inside a single text span
let ts = Transform::from_translate(x + glyph.dx as f32, glyph.dy as f32);
positioned_glyphs.push(PositionedGlyph {
glyph_ts: ts,
// Will be set later.
cluster_ts: Transform::default(),
// Will be set later.
span_ts: Transform::default(),
units_per_em: glyph.font.units_per_em.get(),
font_size,
font: glyph.font.id,
text: glyph.text.clone(),
id: glyph.id,
});
x += glyph.width as f32;
let glyph_width = glyph.width as f32 * sx;
if glyph_width > width {
width = glyph_width;
}
}
let byte_idx = glyphs[0].byte_idx;
let font = glyphs[0].font.clone();
GlyphCluster {
byte_idx,
codepoint: byte_idx.char_from(text),
width,
advance: width,
ascent: font.ascent(font_size),
descent: font.descent(font_size),
has_relative_shift: false,
transform: Transform::default(),
path_transform: Transform::default(),
glyphs: positioned_glyphs,
visible: true,
}
}
pub(crate) trait DatabaseExt {
fn load_font(&self, id: ID) -> Option<ResolvedFont>;
fn has_char(&self, id: ID, c: char) -> bool;
}
impl DatabaseExt for Database {
#[inline(never)]
fn load_font(&self, id: ID) -> Option<ResolvedFont> {
self.with_face_data(id, |data, face_index| -> Option<ResolvedFont> {
let font = ttf_parser::Face::parse(data, face_index).ok()?;
let units_per_em = NonZeroU16::new(font.units_per_em())?;
let ascent = font.ascender();
let descent = font.descender();
let x_height = font
.x_height()
.and_then(|x| u16::try_from(x).ok())
.and_then(NonZeroU16::new);
let x_height = match x_height {
Some(height) => height,
None => {
// If not set - fallback to height * 45%.
// 45% is what Firefox uses.
u16::try_from((f32::from(ascent - descent) * 0.45) as i32)
.ok()
.and_then(NonZeroU16::new)?
}
};
let line_through = font.strikeout_metrics();
let line_through_position = match line_through {
Some(metrics) => metrics.position,
None => x_height.get() as i16 / 2,
};
let (underline_position, underline_thickness) = match font.underline_metrics() {
Some(metrics) => {
let thickness = u16::try_from(metrics.thickness)
.ok()
.and_then(NonZeroU16::new)
// `ttf_parser` guarantees that units_per_em is >= 16
.unwrap_or_else(|| NonZeroU16::new(units_per_em.get() / 12).unwrap());
(metrics.position, thickness)
}
None => (
-(units_per_em.get() as i16) / 9,
NonZeroU16::new(units_per_em.get() / 12).unwrap(),
),
};
// 0.2 and 0.4 are generic offsets used by some applications (Inkscape/librsvg).
let mut subscript_offset = (units_per_em.get() as f32 / 0.2).round() as i16;
let mut superscript_offset = (units_per_em.get() as f32 / 0.4).round() as i16;
if let Some(metrics) = font.subscript_metrics() {
subscript_offset = metrics.y_offset;
}
if let Some(metrics) = font.superscript_metrics() {
superscript_offset = metrics.y_offset;
}
Some(ResolvedFont {
id,
units_per_em,
ascent,
descent,
x_height,
underline_position,
underline_thickness,
line_through_position,
subscript_offset,
superscript_offset,
})
})?
}
#[inline(never)]
fn has_char(&self, id: ID, c: char) -> bool {
let res = self.with_face_data(id, |font_data, face_index| -> Option<bool> {
let font = ttf_parser::Face::parse(font_data, face_index).ok()?;
font.glyph_index(c)?;
Some(true)
});
res == Some(Some(true))
}
}
/// Text shaping with font fallback.
pub(crate) fn shape_text(
text: &str,
font: Arc<ResolvedFont>,
small_caps: bool,
apply_kerning: bool,
resolver: &FontResolver,
fontdb: &mut Arc<fontdb::Database>,
) -> Vec<Glyph> {
let mut glyphs = shape_text_with_font(text, font.clone(), small_caps, apply_kerning, fontdb)
.unwrap_or_default();
// Remember all fonts used for shaping.
let mut used_fonts = vec![font.id];
// Loop until all glyphs become resolved or until no more fonts are left.
'outer: loop {
let mut missing = None;
for glyph in &glyphs {
if glyph.is_missing() {
missing = Some(glyph.byte_idx.char_from(text));
break;
}
}
if let Some(c) = missing {
let fallback_font = match (resolver.select_fallback)(c, &used_fonts, fontdb)
.and_then(|id| fontdb.load_font(id))
{
Some(v) => Arc::new(v),
None => break 'outer,
};
// Shape again, using a new font.
let fallback_glyphs = shape_text_with_font(
text,
fallback_font.clone(),
small_caps,
apply_kerning,
fontdb,
)
.unwrap_or_default();
let all_matched = fallback_glyphs.iter().all(|g| !g.is_missing());
if all_matched {
// Replace all glyphs when all of them were matched.
glyphs = fallback_glyphs;
break 'outer;
}
// We assume, that shaping with an any font will produce the same amount of glyphs.
// This is incorrect, but good enough for now.
if glyphs.len() != fallback_glyphs.len() {
break 'outer;
}
// TODO: Replace clusters and not glyphs. This should be more accurate.
// Copy new glyphs.
for i in 0..glyphs.len() {
if glyphs[i].is_missing() && !fallback_glyphs[i].is_missing() {
glyphs[i] = fallback_glyphs[i].clone();
}
}
// Remember this font.
used_fonts.push(fallback_font.id);
} else {
break 'outer;
}
}
// Warn about missing glyphs.
for glyph in &glyphs {
if glyph.is_missing() {
let c = glyph.byte_idx.char_from(text);
// TODO: print a full grapheme
log::warn!(
"No fonts with a {}/U+{:X} character were found.",
c,
c as u32
);
}
}
glyphs
}
/// Converts a text into a list of glyph IDs.
///
/// This function will do the BIDI reordering and text shaping.
fn shape_text_with_font(
text: &str,
font: Arc<ResolvedFont>,
small_caps: bool,
apply_kerning: bool,
fontdb: &fontdb::Database,
) -> Option<Vec<Glyph>> {
fontdb.with_face_data(font.id, |font_data, face_index| -> Option<Vec<Glyph>> {
let rb_font = rustybuzz::Face::from_slice(font_data, face_index)?;
let bidi_info = unicode_bidi::BidiInfo::new(text, Some(unicode_bidi::Level::ltr()));
let paragraph = &bidi_info.paragraphs[0];
let line = paragraph.range.clone();
let mut glyphs = Vec::new();
let (levels, runs) = bidi_info.visual_runs(paragraph, line);
for run in runs.iter() {
let sub_text = &text[run.clone()];
if sub_text.is_empty() {
continue;
}
let ltr = levels[run.start].is_ltr();
let hb_direction = if ltr {
rustybuzz::Direction::LeftToRight
} else {
rustybuzz::Direction::RightToLeft
};
let mut buffer = rustybuzz::UnicodeBuffer::new();
buffer.push_str(sub_text);
buffer.set_direction(hb_direction);
let mut features = Vec::new();
if small_caps {
features.push(rustybuzz::Feature::new(Tag::from_bytes(b"smcp"), 1, ..));
}
if !apply_kerning {
features.push(rustybuzz::Feature::new(Tag::from_bytes(b"kern"), 0, ..));
}
let output = rustybuzz::shape(&rb_font, &features, buffer);
let positions = output.glyph_positions();
let infos = output.glyph_infos();
for i in 0..output.len() {
let pos = positions[i];
let info = infos[i];
let idx = run.start + info.cluster as usize;
let start = info.cluster as usize;
let end = if ltr {
i.checked_add(1)
} else {
i.checked_sub(1)
}
.and_then(|last| infos.get(last))
.map_or(sub_text.len(), |info| info.cluster as usize);
glyphs.push(Glyph {
byte_idx: ByteIndex::new(idx),
cluster_len: end.checked_sub(start).unwrap_or(0), // TODO: can fail?
text: sub_text[start..end].to_string(),
id: GlyphId(info.glyph_id as u16),
dx: pos.x_offset,
dy: pos.y_offset,
width: pos.x_advance,
font: font.clone(),
});
}
}
Some(glyphs)
})?
}
/// An iterator over glyph clusters.
///
/// Input: 0 2 2 2 3 4 4 5 5
/// Result: 0 1 4 5 7
pub(crate) struct GlyphClusters<'a> {
data: &'a [Glyph],
idx: usize,
}
impl<'a> GlyphClusters<'a> {
pub(crate) fn new(data: &'a [Glyph]) -> Self {
GlyphClusters { data, idx: 0 }
}
}
impl<'a> Iterator for GlyphClusters<'a> {
type Item = (std::ops::Range<usize>, ByteIndex);
fn next(&mut self) -> Option<Self::Item> {
if self.idx == self.data.len() {
return None;
}
let start = self.idx;
let cluster = self.data[self.idx].byte_idx;
for g in &self.data[self.idx..] {
if g.byte_idx != cluster {
break;
}
self.idx += 1;
}
Some((start..self.idx, cluster))
}
}
/// Checks that selected script supports letter spacing.
///
/// [In the CSS spec](https://www.w3.org/TR/css-text-3/#cursive-tracking).
///
/// The list itself is from: https://github.com/harfbuzz/harfbuzz/issues/64
pub(crate) fn script_supports_letter_spacing(script: unicode_script::Script) -> bool {
use unicode_script::Script;
!matches!(
script,
Script::Arabic
| Script::Syriac
| Script::Nko
| Script::Manichaean
| Script::Psalter_Pahlavi
| Script::Mandaic
| Script::Mongolian
| Script::Phags_Pa
| Script::Devanagari
| Script::Bengali
| Script::Gurmukhi
| Script::Modi
| Script::Sharada
| Script::Syloti_Nagri
| Script::Tirhuta
| Script::Ogham
)
}
/// A glyph.
///
/// Basically, a glyph ID and it's metrics.
#[derive(Clone)]
pub(crate) struct Glyph {
/// The glyph ID in the font.
pub(crate) id: GlyphId,
/// Position in bytes in the original string.
///
/// We use it to match a glyph with a character in the text chunk and therefore with the style.
pub(crate) byte_idx: ByteIndex,
// The length of the cluster in bytes.
pub(crate) cluster_len: usize,
/// The text from the original string that corresponds to that glyph.
pub(crate) text: String,
/// The glyph offset in font units.
pub(crate) dx: i32,
/// The glyph offset in font units.
pub(crate) dy: i32,
/// The glyph width / X-advance in font units.
pub(crate) width: i32,
/// Reference to the source font.
///
/// Each glyph can have it's own source font.
pub(crate) font: Arc<ResolvedFont>,
}
impl Glyph {
fn is_missing(&self) -> bool {
self.id.0 == 0
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct ResolvedFont {
pub(crate) id: ID,
units_per_em: NonZeroU16,
// All values below are in font units.
ascent: i16,
descent: i16,
x_height: NonZeroU16,
underline_position: i16,
underline_thickness: NonZeroU16,
// line-through thickness should be the the same as underline thickness
// according to the TrueType spec:
// https://docs.microsoft.com/en-us/typography/opentype/spec/os2#ystrikeoutsize
line_through_position: i16,
subscript_offset: i16,
superscript_offset: i16,
}
pub(crate) fn chunk_span_at(chunk: &TextChunk, byte_offset: ByteIndex) -> Option<&TextSpan> {
chunk
.spans
.iter()
.find(|&span| span_contains(span, byte_offset))
}
pub(crate) fn span_contains(span: &TextSpan, byte_offset: ByteIndex) -> bool {
byte_offset.value() >= span.start && byte_offset.value() < span.end
}
/// Checks that the selected character is a word separator.
///
/// According to: https://www.w3.org/TR/css-text-3/#word-separator
pub(crate) fn is_word_separator_characters(c: char) -> bool {
matches!(
c as u32,
0x0020 | 0x00A0 | 0x1361 | 0x010100 | 0x010101 | 0x01039F | 0x01091F
)
}
impl ResolvedFont {
#[inline]
pub(crate) fn scale(&self, font_size: f32) -> f32 {
font_size / self.units_per_em.get() as f32
}
#[inline]
pub(crate) fn ascent(&self, font_size: f32) -> f32 {
self.ascent as f32 * self.scale(font_size)
}
#[inline]
pub(crate) fn descent(&self, font_size: f32) -> f32 {
self.descent as f32 * self.scale(font_size)
}
#[inline]
pub(crate) fn height(&self, font_size: f32) -> f32 {
self.ascent(font_size) - self.descent(font_size)
}
#[inline]
pub(crate) fn x_height(&self, font_size: f32) -> f32 {
self.x_height.get() as f32 * self.scale(font_size)
}
#[inline]
pub(crate) fn underline_position(&self, font_size: f32) -> f32 {
self.underline_position as f32 * self.scale(font_size)
}
#[inline]
fn underline_thickness(&self, font_size: f32) -> f32 {
self.underline_thickness.get() as f32 * self.scale(font_size)
}
#[inline]
pub(crate) fn line_through_position(&self, font_size: f32) -> f32 {
self.line_through_position as f32 * self.scale(font_size)
}
#[inline]
fn subscript_offset(&self, font_size: f32) -> f32 {
self.subscript_offset as f32 * self.scale(font_size)
}
#[inline]
fn superscript_offset(&self, font_size: f32) -> f32 {
self.superscript_offset as f32 * self.scale(font_size)
}
fn dominant_baseline_shift(&self, baseline: DominantBaseline, font_size: f32) -> f32 {
let alignment = match baseline {
DominantBaseline::Auto => AlignmentBaseline::Auto,
DominantBaseline::UseScript => AlignmentBaseline::Auto, // unsupported
DominantBaseline::NoChange => AlignmentBaseline::Auto, // already resolved
DominantBaseline::ResetSize => AlignmentBaseline::Auto, // unsupported
DominantBaseline::Ideographic => AlignmentBaseline::Ideographic,
DominantBaseline::Alphabetic => AlignmentBaseline::Alphabetic,
DominantBaseline::Hanging => AlignmentBaseline::Hanging,
DominantBaseline::Mathematical => AlignmentBaseline::Mathematical,
DominantBaseline::Central => AlignmentBaseline::Central,
DominantBaseline::Middle => AlignmentBaseline::Middle,
DominantBaseline::TextAfterEdge => AlignmentBaseline::TextAfterEdge,
DominantBaseline::TextBeforeEdge => AlignmentBaseline::TextBeforeEdge,
};
self.alignment_baseline_shift(alignment, font_size)
}
// The `alignment-baseline` property is a mess.
//
// The SVG 1.1 spec (https://www.w3.org/TR/SVG11/text.html#BaselineAlignmentProperties)
// goes on and on about what this property suppose to do, but doesn't actually explain
// how it should be implemented. It's just a very verbose overview.
//
// As of Nov 2022, only Chrome and Safari support `alignment-baseline`. Firefox isn't.
// Same goes for basically every SVG library in existence.
// Meaning we have no idea how exactly it should be implemented.
//
// And even Chrome and Safari cannot agree on how to handle `baseline`, `after-edge`,
// `text-after-edge` and `ideographic` variants. Producing vastly different output.
//
// As per spec, a proper implementation should get baseline values from the font itself,
// using `BASE` and `bsln` TrueType tables. If those tables are not present,
// we have to synthesize them (https://drafts.csswg.org/css-inline/#baseline-synthesis-fonts).
// And in the worst case scenario simply fallback to hardcoded values.
//
// Also, most fonts do not provide `BASE` and `bsln` tables to begin with.
//
// Again, as of Nov 2022, Chrome does only the latter:
// https://github.com/chromium/chromium/blob/main/third_party/blink/renderer/platform/fonts/font_metrics.cc#L153
//
// Since baseline TrueType tables parsing and baseline synthesis are pretty hard,
// we do what Chrome does - use hardcoded values. And it seems like Safari does the same.
//
//
// But that's not all! SVG 2 and CSS Inline Layout 3 did a baseline handling overhaul,
// and it's far more complex now. Not sure if anyone actually supports it.
fn alignment_baseline_shift(&self, alignment: AlignmentBaseline, font_size: f32) -> f32 {
match alignment {
AlignmentBaseline::Auto => 0.0,
AlignmentBaseline::Baseline => 0.0,
AlignmentBaseline::BeforeEdge | AlignmentBaseline::TextBeforeEdge => {
self.ascent(font_size)
}
AlignmentBaseline::Middle => self.x_height(font_size) * 0.5,
AlignmentBaseline::Central => self.ascent(font_size) - self.height(font_size) * 0.5,
AlignmentBaseline::AfterEdge | AlignmentBaseline::TextAfterEdge => {
self.descent(font_size)
}
AlignmentBaseline::Ideographic => self.descent(font_size),
AlignmentBaseline::Alphabetic => 0.0,
AlignmentBaseline::Hanging => self.ascent(font_size) * 0.8,
AlignmentBaseline::Mathematical => self.ascent(font_size) * 0.5,
}
}
}
pub(crate) type FontsCache = HashMap<Font, Arc<ResolvedFont>>;
/// A read-only text index in bytes.
///
/// Guarantee to be on a char boundary and in text bounds.
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) struct ByteIndex(usize);
impl ByteIndex {
fn new(i: usize) -> Self {
ByteIndex(i)
}
pub(crate) fn value(&self) -> usize {
self.0
}
/// Converts byte position into a code point position.
pub(crate) fn code_point_at(&self, text: &str) -> usize {
text.char_indices()
.take_while(|(i, _)| *i != self.0)
.count()
}
/// Converts byte position into a character.
pub(crate) fn char_from(&self, text: &str) -> char {
text[self.0..].chars().next().unwrap()
}
}