File: Layout.pm

package info (click to toggle)
libtext-layout-perl 0.045-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 548 kB
  • sloc: perl: 4,117; makefile: 2
file content (2144 lines) | stat: -rw-r--r-- 41,821 bytes parent folder | download
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
#! perl

use strict;
use warnings;
use utf8;

package Text::Layout;

use Carp;

 our $VERSION = "0.045";

=head1 NAME

Text::Layout - Pango style markup formatting

This module will cooperate with PDF::API2, PDF::Builder, Cairo, and Pango.

=head1 SYNOPSIS

Text::Layout provides methods for Pango style text formatting. Where
possible the methods have identical names and (near) identical
behaviour as their Pango counterparts.

See L<https://developer.gnome.org/pango/stable/pango-Layout-Objects.html>.

The package uses Text::Layout::FontConfig (included) to organize fonts
by description.

If module HarfBuzz::Shaper is installed, Text::Layout can use it for
text shaping.

Example, using PDF::API2 integration:

    use PDF::API2;		# or PDF::Builder
    use Text::Layout;

    # Create a PDF document.
    my $pdf = PDF::API2->new;	# or PDF::Builder->new
    $pdf->default_page_size("a4")	# ISO A4

    # Set up page and get the text context.
    my $page = $pdf->page;
    my $ctx  = $page->text;

    # Create a markup instance.
    my $layout = Text::Layout->new($pdf);

    # This example uses PDF corefonts only.
    Text::Layout::FontConfig->register_corefonts;

    $layout->set_font_description(Text::Layout::FontConfig->from_string("times 40"));
    $layout->set_markup( q{The <i><span foreground="red">quick</span> <span size="20"><b>brown</b></span></i> fox} );

    # Center text.
    $layout->set_width(595);	# width of A4 page
    $layout->set_alignment("center");

    # Render it.
    $layout->show( 0, 600, $ctx );
    $pdf->save("out.pdf");

All PDF::API2 graphic and text methods can still be used, they won't
interfere with the layout methods.

=head1 NOTES FOR PDF::API2/Builder USERS

=head2 Baselines

PDF::API2 and PDF::Builder render texts using the font baseline as origin.

This module typesets text in an area of possibly limited width and
height. The origin is the top left of this area. Currently this area
contains only a single line of text. This will change in the future
when line breaking and paragraph formatting is implemented.

PDF::API2 and PDF::Builder coordinates have origin bottom left. This
module produces information with respect to top left coordinates.

=head1 IMPORTANT NOTES FOR PANGO USERS

=head2 Coordinate system

Pango, layered upon Cairo, uses a coordinate system that starts off
top left. So for western text the direction is increasing I<x> and
increasing I<y>.

PDF::API2 uses the coordinate system as defined in the PDF
specification. It starts off bottom left. For western text the
direction is increasing I<x> and B<de>creasing I<y>.

=head1 Pango Conformance Mode

Text::Layout can operate in one of two modes: I<convenience mode>
(enabled by default), and I<Pango conformance mode>. The desired mode
can be selected by calling the method set_pango_scaling().

=head2 Pango coordinates

Pango uses two device coordinates units: Pango units and device units.
Pango units are 1024 (C<PANGO_SCALE>) times the device units.

Several methods have two variants, e.g. get_size() and
get_pixel_size(). The pixel-variant uses device units while the other
variant uses Pango units.

In I<convenience mode>, this module assumes no scaling. All units are
PDF device units (1/72 inch).

=head2 Pango device units

Device units are used for font rendering. Pango device units are 96dpi
while PDF uses 72dpi.

In I<convenience mode> this is ignored. E.g. a C<Times 20> font 
will be of equal size in the two systems,

In I<Pango conformance mode> you would need to specify a font size of
C<15360> to get a 20pt font.

=head1 SUPPORTED MARKUP

Text::Layout recognizes most of the Pango markup as provided by the
Pango library version 1.50 or newer. However, not everything is supported.

=head2 Span attributes

=over 8

=item font="I<DESC>"   font_desc="I<DESC>"

Specifies a font to be used, e.g. C<Serif 20>.

=item font_face="I<FAM>"   face="I<FAM>"

Specifies a font family to be used.

=item font_family="I<FAM>"

Same as font_face="I<FAM>".

=item size=I<FNUM>   size=I<FNUM>pt   size=I<FNUM>%

Font size in 1024ths of a point (conformance mode), or in points (e.g.
'12.5pt'), or a percentage (e.g. '200%'), or one of the relative sizes
'smaller' or 'larger'.

Note that in Pango conformance mode, the actual font size is 96/72
larger. So C<"45pt"> gives a 60pt font.

=item style="I<STYLE>"   font_style="I<STYLE>"

Specifes the font style, e.g. C<italic>.

=item weight="I<WEIGHT>"   font_weight="I<WEIGHT>"

Specifies the font weight, e.g. C<bold>.

=item foreground="I<COLOR>"   fgcolor="I<COLOR>"   color="I<COLOR>"

Specifies the foreground colour, e.g. C<black>.

=item background="I<COLOR>"   bgcolor="I<COLOR>"

Specifies the background colour, e.g. C<white>.

=item underline="I<TYPE>"

Enables underlining.
I<TYPE> must be C<none>, C<single>, or C<double>.

=item underline_color="I<COLOR>"

The colour to be used for underlining, if enabled.

=item overline="I<TYPE>"

Enables overlining.
I<TYPE> must be C<none>, C<single>, or C<double>.

=item overline_color="I<COLOR>"

The colour to be used for ovderlining, if enabled.

=item strikethrough="I<ARG>"

Enables or disables overlining. I<ARG> must be C<true> or C<1> to
enable, and C<false> or C<0> to disable.

=item strikethrough_color="I<COLOR>"

The colour to be used for striking, if enabled.

=item rise=C<NUM>

In convenience mode, lowers the text by I<NUM>/1024 of the font size.
May be negative to rise the text.

In Pango conformance mode, rises the text by I<NUM> units from the baseline.
May be negative to lower the text.

Note: In Pango conformance mode, C<rise> does B<not> accumulate.
 Use C<baseline_shift> instead.

=item rise=C<NUM>pt   rise=C<NUM>%   rise=C<NUM>em   rise=C<NUM>ex

Rises the text from the baseline. May be negative to lower the text.

Units are points if postfixed by B<pt>, and a percentage of the current font size if postfixed by B<%>.

B<em> units are equal to the current font size, B<ex> half the font size.

Note: This is not (yet?) part of the Pango markup standard.

=item baseline_shift=C<NUM>   beseline_shift=C<NUM>pt   baseline_shift=C<NUM>%

Like C<rise>, but accumulates.

=back

Also supported but not part of the official Pango Markup specification.

=over 8

=item href="C<TARGET>"

Creates a clickable target that activates the I<TARGET>.

The I<TARGET> can be:

=over 4

=item *

The name of an external PDF document, e.g. C<"that.pdf">.

=item *

A named destination in the document, e.g. C<"#there">.
See L<Strut attributes>.

=item *

a named destination in an external document, e.g. C<"that.pdf#there">.

=back

=back

=head2 Img (image) attributes

I<This is an extension to Pango markup.>

Note that image markup elements may only occur as closed elements,
i.e., C<< <img/> >>.

=over 8

=item id="I<ID>"

Implementation dependent.

=item src="I<URI>"

Source filename or url for the image.

=item width="I<WIDTH>" (short: w="I<WIDTH>")

The width the image should be considered to occupy, regardless its
actual dimensions.

=item height="I<HEIGHT>" (short: h="I<HEIGHT>")

The height the image should be considered to occupy, regardless its
actual dimensions.

=item x="I<XDISP>"

Horizontal displacement of the image relative to the C<< <img/> >> element.

=item y="I<YDISP>"

Vertical displacement of the image relative to the C<< <img/> >> element.

=item border="I<THICK>"

Provide a border around the element. I<THICK> denotes its thickness.

=back

=head2 Strut attributes

I<This is an extension to Pango markup.>

A C<strut> is a markup element that has bounding box dimensions but no
ink dimensions.

Note that strut markup elements may only occur as closed elements,
i.e., C<< <strut/> >>.

=over 8

=item label="I<LABEL>"

An optional identifying label.

When a label is used, this will create a I<named destination>, a
symbolic name for a location in the document.
See the C<span> C<href> attribute,

=item width="I<WIDTH>" (short w="I<WIDTH>")

The width of the strut. Default value is zero.

Width may be expressed in points, C<em> (font size) or C<ex> (half of
font size).

=item ascender="I<ASC>" (short: a="I<ASC>")

The ascender of the strut. Optional.

May be expressed in points, C<em> (font size) or C<ex> (half of
font size).

=item descender="I<DESC>" (short: d="I<DESC>")

The descender of the strut. Optional.

May be expressed in points, C<em> (font size) or C<ex> (half of
font size).

=back

=head2 Shortcuts

Equivalent C<span> attributes for shortcuts.

=over 8

=item b

weight=bold

=item big

larger

=item emp

style=italic

=item i

style=italic

=item s

strikethrough=true

=item small

size=smaller

=item strong

weight=bold

=item sub

size=smaller rise=-30%

=item sup

size=smaller rise=30%

=item tt

face=monospace

=item u

underline=single

=back

=cut

use constant {
    PANGO_SCALE		=> 1024,
    PANGO_DEVICE_UNITS	=>   96,
    PDF_DEVICE_UNITS	=>   72,
};

# Default is no Pango scaling.
sub px2pu { $_[0] };
sub pu2px { $_[0] };

use Text::Layout::FontConfig;
use Text::Layout::Utils qw(parse_kv);

# Global (persistent) shortcodes.
# These map <XX> -> <span YY>.
my %shortcodes =
  ( b	    => "weight=bold",
    big	    => "size=larger",
    emp	    => "style=italic",
    i	    => "style=italic",
    s	    => "strikethrough=true",
    small   => "size=smaller",
    strong  => "weight=bold",
    sub	    => "size=smaller rise=-30%",
    sup	    => "size=smaller rise=30%",
    tt	    => "face=monospace",
    u	    => "underline=single",
);

=head1 METHODS

=over

=item new( $pdf )

Creates a new layout instance for PDF::API2. This is for convenience.
It is equivalent to

    use Text::Layout::PDFAPI2;
    $layout = Text::Layout::PDFAPI2->new($pdf);

For other implementations only the above method can be used.

The argument is the I<context> for text formatting. In the case of
PDF::API2 this will be a PDF::API2 object.

=back

=cut

sub new {
    my ( $pkg, @data ) = @_;

    $pkg =~ s/::$//;
    if ( $pkg eq __PACKAGE__ ) {
	# For convenience (and backward compatibility)...
	unless ( @data >= 1 && ref($data[0]) =~ /^PDF::(API2|Builder)\b/ ) {
	    croak("Please instantiate a backend, e.g. ".__PACKAGE__."::PDFAPI2");
	}
	require Text::Layout::PDFAPI2;
	return Text::Layout::PDFAPI2->new(@data); # CUL8R
    }
    bless { _context => undef,
	    _fonts   => {},
	    _content => [],
	    _px2pu   => \&px2pu,
	    _pu2px   => \&pu2px,
	    _pango   => 0,
	    _sc	     => {},
	  } => $pkg;
}

=over

=item copy

Copies (clones) a layout instance.

The content is copied deeply, the context and fonts are copied by
reference.

=back

=cut

sub copy {
    my ( $self ) = @_;

    my $copy = bless { %$self } => ref($self);
    $copy->{_content} = [];
    for ( @{ $self->{_content} } ) {
	my %h;
	@h{ keys %$_ } = values %$_;
	push( @{ $copy->{_content} }, { %h } );
    }
    $copy->{_sc} = { %{ $self->{_sc} } };
    return $copy;
}

=over

=item get_context

Gets the context of this layout.

=back

=cut

sub get_context {
    my ( $self ) = ( $_ );
    $self->{_context};
}

=over

=item context_changed

Not supported.

=back

=cut

sub context_changed { nyi() }

=over

=item get_serial

Not supported.

=back

=cut

sub get_serial { nyi() }

=over

=item set_text( $text )

Puts a string in this layout instance. No markup is processed.

Note that if you have used set_markup() on this layout before, you may
want to call set_attributes() to clear the attributes set on the
layout from the markup as this function does not clear all attributes.

=back

=cut

sub set_text {
    my ( $self, $string ) = @_;
    $self->{_content} =
      [ { type  => "text",
	  text  => $string,
	  font  => $self->{_currentfont},
	  size  => $self->{_currentsize},
	  color => $self->{_currentcolor},
	  base  => 0,
	} ];
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_text

Gets the content of this layout instance as a single string.
Markup is ignored.

Returns undef if no text has been set.

=back

=cut

sub get_text {
    my ( $self ) = @_;
    return unless $self->{_content};
    join( "", map { $_->{text} } @{ $self->{_content} } );
}

=over

=item get_character_count

Returns the number of characters in the text of this layout.

Basically the same as length of get_text().

Returns undef if no text has been set.

=back

=cut

sub get_character_count {
    my ( $self ) = @_;
    return unless $self->{_content};
    my $len = 0;
    $len += length($_->{text}) for @{ $self->{_content} };
    return $len;
}

=over

=item set_markup( $string )

Puts a string in this layout instance.

The string can contain Pango-compatible markup. See
L<https://developer.gnome.org/pygtk/stable/pango-markup-language.html>.

Implementation note: Although all markup is parsed, not all is implemented.

=back

=cut

use Text::ParseWords;
use constant STEP => 0.83333125;	# Cf. Pango

my %magstep =
  ( "xx-small"	=> 1.0*STEP*STEP*STEP,
    "x-small"	=> 1.0*STEP*STEP,
    "small"	=> 1.0*STEP,
    "smaller"	=> 1.0*STEP,
    "medium"	=> 1.0,
    "large"	=> 1.0/STEP,
    "larger"	=> 1.0/STEP,
    "x-large"	=> 1.0/(STEP*STEP),
    "xx-large"  => 1.0/(STEP*STEP*STEP),
  );

sub set_markup {
    my ( $self, $string ) = @_;
    confess("set_markup with UNDEF") unless defined $string;
    my @stack;
    my @content;
    my $fcur = $self->{_currentfont};
    my $fcol = $self->{_currentcolor};
    my $fsiz = $self->{_currentsize};
    my $href;
    my $undl;
    my $uncl = $fcol;
    my $ovrl;
    my $ovcl = $fcol;
    my $strk;
    my $stcl = $fcol;
    my $bcol;
    my $base = 0;

    my $try_size = sub {
	my ( $k ) = @_;
	if ( exists $magstep{$k} ) {
	    $fsiz *= $magstep{$k};
	}
	elsif ( $k =~ /^(smaller)$/ ) {
	    $fsiz *= STEP;
	}
	elsif ( $k =~ /^(larger)$/ ) {
	    $fsiz /= STEP;
	}
	else {
	    return;		# fail
	}
	return 1;		# success
    };

    my $span;
    $span = sub {
	my ( $v ) = @_;

	# Split the span attributes. Note that shellwords is so kind to
	# split 'font="xx yy"' as a single word 'font=xx yy'.
	# NOTE: can't use parse_kv -- order is important
	foreach my $k ( shellwords($v) ) {

	    # key=value
	    if ( $k =~ /^([-\w]+)=(.+)$/ ) {
		my ( $k, $v ) = ( $1, $2 );

		# Ignore case unless required.
		$v = lc $v unless $k =~ /^(link|href|a)$/;

		# <span font_desc="Sans 20">
		if ( $k =~ /^(font|font_desc)$/ ) {
		    $fcur = Text::Layout::FontConfig->from_string($v);
		    $fsiz = $fcur->get_size if $fcur->get_size;
		}

		# <span face="Sans">
		elsif ( $k =~ /^(face|font_face|font_family)$/ ) {
		    $fcur = Text::Layout::FontConfig->find_font( $v,
								 $fcur->{style},
								 $fcur->{weight});
		}

		# <span size=20>
		elsif ( $k =~ /^(size|font_size)$/ ) {
		    if ( $try_size->($v) ) {
			#ok
		    }
		    elsif ( $v =~ /^(\d+(?:\.\d+)?)pt$/ ) {
			$fsiz = $self->{_pango}
			  ? $1 * PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS
			  : $1;
			# warn("fsiz \"$v\" -> $fsiz\n");
		    }
		    elsif ( $v =~ /^\d+(?:\.\d+)?$/ ) {
			$fsiz = $self->{_pango}
			  ? $v * PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS / PANGO_SCALE
			  : $v;
			# warn("fsiz \"$v\" -> $fsiz\n");
		    }
		    elsif ( $v =~ /^(\d+(?:\.\d+)?)\%$/ ) {
			$fsiz *= $1 / 100;
			# warn("fsiz \"$v\" -> $fsiz\n");
		    }
		    else {
			carp("Invalid size: \"$v\"\n");
		    }
		}

		# <span style="Italic">
		elsif ( $k =~ /^(style|font_style)$/ ) {
		    $v = Text::Layout::FontConfig::_norm_style($v);
		    $fcur = Text::Layout::FontConfig->find_font( $fcur->{family},
					      $v, $fcur->{weight} );
		}

		# <span weight="bold">
		elsif ( $k =~ /^(weight|font_weight)$/ ) {
		    $v = Text::Layout::FontConfig::_norm_weight($v);
		    $fcur = Text::Layout::FontConfig->find_font( $fcur->{family},
					      $fcur->{style}, $v );
		}

		# <span variant="...">
		# <span stretch="...">
		elsif ( $k =~ /^(?:font_)?(variant|stretch)$/ ) {
		    # ignore for now
		}
		elsif ( $k =~ /^(features|background_alpha|alpha)$/ ) {
		    # ignore for now
		}

		# <span foreground="red">
		elsif ( $k =~ /^(foreground|fgcolor|color)$/ ) {
		    $fcol = $v;
		}

		# <span background="red">
		elsif ( $k =~ /^(background|bgcolor)$/ ) {
		    $bcol = $v;
		}

		# <span underline="double">
		elsif ( $k eq "underline" && $v =~ /^(none|single|double)$/i ) {
		    $undl = lc $v;
		}
		elsif ( $k eq "underline_color" ) {
		    $uncl = lc $v;
		}

		# <span overline="double">
		elsif ( $k eq "overline" && $v =~ /^(none|single|double)$/i ) {
		    $ovrl = lc $v;
		}
		elsif ( $k eq "overline_color" ) {
		    $ovcl = lc $v;
		}

		# <span rise=324>
		elsif ( $k eq "rise" ) {
		    if ( $v =~ /^(-?\d+(?:\.\d*)?)pt$/ ) {
			$base = -$1;
		    }
		    elsif ( !$self->{_pango} && $v =~ /^(-?\d+(?:\.\d*)?)\%$/ ) {
			$base = -$1 * $fsiz / 100;
		    }
		    elsif ( !$self->{_pango} && $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/ ) {
			$base = $2 eq 'x' ? -$1 * $fsiz / 2 : -$1 * $fsiz;
		    }
		    else {
			$v /= PANGO_SCALE;
			$base = $self->{_pango} ? -$v : $base + $v * $fsiz;
		    }
		}

		# <span baseline_shift=324>
		# Line rise, but accumulate.
		elsif ( $k eq "baseline_shift" ) {
		    if ( $v =~ /^(-?\d+(?:\.\d*)?)pt$/ ) {
			$base -= $1;
		    }
		    elsif ( $v =~ /^(-?\d+(?:\.\d*)?)\%$/ ) {
			$base -= $1 * $fsiz / 100;
		    }
		    elsif ( $v =~ /^(-?\d+(?:\.\d*)?)e([mx])$/ ) {
			$base = $2 eq 'x' ? $1 * $fsiz / 2 : $1 * $fsiz;
		    }
		    else {
			$base -= $self->{_pango} ? $v : -$v * $fsiz;
		    }
		}

		# <span strikethrough=false>
		elsif ( $k eq "strikethrough" && $v =~ /^(true|1)$/i ) {
		    $strk = 1;
		}
		elsif ( $k eq "strikethrough" && $v =~ /^(false|0)$/i ) {
		    $strk = 0;
		}
		elsif ( $k eq "strikethrough_color" ) {
		    $stcl = $v;
		}

		# <span fallback=false>
		elsif ( $k eq "fallback" ) {
		    # Not supported.
		}

		# <span lang="en">
		elsif ( $k eq "lang" ) {
		    # Not supported.
		}

		# <span href="...">
		elsif ( $k eq "href" ) {
		    $href = $v;
		}
	    }

	    # <span strikethrough>
	    elsif ( $k eq "strikethrough" ) {
		$span->("strikethrough=true");
	    }

	    # <span fallback>
	    elsif ( $k eq "fallback" ) {
		$span->("fallback=true");
	    }

	    else {
		carp("Invalid span markup: \"$k\"\n");
	    }
	}
    };

    my $image = sub {
	my ( $v ) = @_;

	my %img;
	# Split the attributes.
	while ( my ( $k, $v ) = each %{parse_kv($v)} ) {

	    # Ignore case unless required.
	    $v = lc $v unless $k =~ /^(src|id)$/;

	    if ( $k eq "src" ) {
		$img{src} = $v;
	    }
	    elsif ( $k eq "id" ) {
		$img{id} = $v;
	    }
	    elsif ( $k =~ /^(width|height|w|h)$/ ) {
		$img{$k} = $v;
	    }
	    elsif ( $k =~ /^(x|y)$/ ) {
		$img{$k} = $v;
	    }
	    elsif ( $k eq "border" ) {
		$img{border} = $v;
	    }
	    else {
		carp("Invalid image attribute: \"$k\"\n");
	    }
	}
	return \%img;
    };


    # Split the string on markup instructions.
 L: foreach my $a ( split( /(<.*?>)/, $string ) ) {

	# Closing markup, e.g. </b> or </span>.
	if ( $a =~ m;^<\s*/\s*(\w+)(.*)>$; ) {
	    my $k = lc $1;
	    if ( @stack ) {
		# Check if it is closing the currently pending markup.
		if ( $stack[-1][0] =~ /^<\s*$k\b/ ) {
		    # Restore.
		    ( undef,
		      $fcur, $fsiz, $fcol, $undl, $uncl, $ovrl, $ovcl,
		      $bcol, $strk, $stcl, $base, $href ) = @{$stack[-1]};
		    pop(@stack);
		}
		else {
		    carp("Markup error: \"$string\"\n",
			 "  Closing <$k> but $stack[-1][0] is open\n");
		    next;
		}
	    }
	    else {
		carp("Markup error: \"$string\"\n",
		     "  Closing <$k> but no markup is open\n");
		next;
	    }
	}

	# Opening markup, e.g. <b> or <span ...>.
	elsif ( $a =~ m;^<\s*([-\w]+)(.*)?>$; ) {
	    my $k = lc $1;
	    my $v = $2;
	    my $closed = $v =~ s;\s*/\s*$;;;

	    # Save.
	    push( @stack, [ "<$k".lc($v).">",
			    $fcur, $fsiz, $fcol, $undl, $uncl, $ovrl, $ovcl,
			    $bcol, $strk, $stcl, $base, $href ] );

	    # Find existing shortcode.
	    if ( my $sc = $self->{_sc}->{$k} // $shortcodes{$k} ) {
		$span->($sc);
	    }

	    # <strut width=".."/>
	    elsif ( $k eq "strut" && $closed ) {
		my $args = { w => 0, %{parse_kv($v)} };
		# Split the attributes.
		while ( my ( $k, $v ) = each(%$args) ) {
		    if ( $k =~ /^(w(?:idth)?|a(?:scend)?|d(?:escend)?)$/i ) {
			if ( $v =~ /^([.\d]+)em$/ ) {
			    $args->{$k} = $1 * $fsiz;
			}
			elsif ( $v =~ /^([.\d]+)ex$/ ) {
			    $args->{$k} = 0.5 * $1 * $fsiz;
			}
			else {
			    $args->{$k} = 0+$v;
			}
		    }
		    elsif ( $k =~ /^(label)$/i ) {
			$args->{$k} = $v;
		    }
		    else {
			carp("Unknown strut attribute: \"$k\"\n");
		    }
		}
		push( @content, { type	=> $k,
				  width	=> $args->{width}   // $args->{w},
				  desc	=> $args->{descend} // $args->{d},
				  asc	=> $args->{ascend}  // $args->{a},
				  $args->{label} ? ( label => $args->{label} ) : (),
				} );
	    }

	    # <span ...>.
	    elsif ( $k =~ /^(span)$/ ) {
		$span->($v);
	    }

	    # <.../>.
	    elsif ( ( my $p = $self->get_element_handler($k) ) && $closed ) {
		push( @content, { type => $k,
				  %{ $p->parse
				       ( { type			      => $k,
					   font			      => $fcur,
					   size			      => $fsiz,
					   color		      => $fcol,
					   bgcolor		      => $bcol,
					   underline		      => $undl,
					   underline_color	      => $uncl,
					   overline		      => $ovrl,
					   overline_color	      => $ovcl,
					   strikethrough	      => $strk,
					   strikethrough_color	      => $stcl,
					   base			      => $base,
					   href			      => $href,
					 }, $k, $v ) } } );
	    }

	    else {
		carp("Invalid markup: \"$k\"\n");
	    }
	    if ( $closed ) {
		$a = "</$k>";
		redo L;
	    }
	}

	# Text.
	else {
	    push( @content,
		  { type		     => "text",
		    text		     => $a,
		    font		     => $fcur,
		    size		     => $fsiz,
		    color		     => $fcol,
		    bgcolor		     => $bcol,
		    underline		     => $undl,
		    underline_color	     => $uncl,
		    overline		     => $ovrl,
		    overline_color	     => $ovcl,
		    strikethrough	     => $strk,
		    strikethrough_color	     => $stcl,
		    base		     => $base,
		    href		     => $href,
		  } ) if defined $a && $a ne '';
	}
    }

    # Verify that the markup is decently closed.
    if ( @stack ) {
	carp("Markup error: \"$string\"\n",
	     "  Unclosed markup: ",
	     join( " ", map { $_->[0] } @{[ reverse @stack ]} ), "\n" );
    }

    # Store content.
    $self->{_content} = \@content;
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item set_markup_with_accel

Not supported.

=back

=cut

sub set_markup_with_accel { nyi() }

=over

=item set_attributes

=item get_attributes

Not yet implemented.

=back

=cut

sub set_attributes { nyi() }

sub get_attributes { nyi() }

=over

=item set_font_description( $description )

Sets the default font description for the layout. If no font
description is set on the layout, the font description from the
layout's context is used.

$description is a Text::Layout::FontConfig object.

=back

=cut

sub set_font_description {
    my ( $self, $description ) = @_;
    my $o = "Text::Layout::FontDescriptor";
    croak("set_font_description requires a $o object (not $description)")
      unless UNIVERSAL::isa( $description, $o );

    $self->{_currentfont}  = $description;

    if ( my $sz = $description->{size} ) {
	# Font sizes can be in either Pango units, or device units.
	# Use heuristics.
	if ( $sz > 2 * PANGO_SCALE ) { # Pango units
	    $sz /= PANGO_SCALE;
	}
	if ( $self->{_pango} ) {
	    # Pango uses 96dpi while PDF is 72dpi.
	    $sz *= PANGO_DEVICE_UNITS / PDF_DEVICE_UNITS;
	}
	$self->{_currentsize} = $sz;
    }

    $self->{_currentcolor} = $description->{color} || "black";

    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_font_description

Gets the font description for the layout.

Returns undef if no font has been set yet.

=back

=cut

sub get_font_description {
    my ( $self ) = ( @_ );
    my $res = $self->{_currentfont};
    $res->{size} = $self->get_font_size;
    return $res;
}

=over

=item set_width( $width )

Sets the width to which the lines of the layout should align,
wrap or ellipsized. A value of zero or less means unlimited width.
The width is in Pango units.

Implementation note: Only alignment is implemented.

=back

=cut

sub set_width {
    my ( $self, $width ) = @_;
    $self->{_width} = $self->{_pu2px}->($width);
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_width

Gets the width in Pango units for for this instance, or zero if
unlimited.

=back

=cut

sub get_width {
    my ( $self ) = @_;
    $self->{_px2pu}->($self->{_width}) || 0;
}

=over

=item set_height( $height )

Sets the height in Pango units for this instance.

Implementation note: Height restrictions are not yet implemented.

=back

=cut

sub set_height {
    my ( $self, $height ) = @_;
    $self->{_height} = $self->{_pu2px}->($height);
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_height

Gets the height in Pango units for this instance, or zero if no height
restrictions apply.

=back

=cut

sub get_height {
    my ( $self ) = @_;
    $self->{_px2pu}->($self->{_height}) || 0;
}

=over

=item set_wrap( $mode )

Sets the wrap mode; the wrap mode only has effect if a width is set on
the layout with set_width(). To turn off wrapping, set the width to
zero or less.

Not yet implemented.

=back

=cut

sub set_wrap {
    my ( $self, $mode ) = @_;
    $self->{_wrap} = $mode;
    nyi();
}

=over

=item get_wrap

Returns the current wrap mode.

Not yet implemented.

=back

=cut

sub get_wrap {
    my ( $self ) = @_;
    $self->{_wrap};
}

=over

=item is_wrapped

Queries whether the layout had to wrap any paragraphs.

=back

=cut

sub is_wrapped {
    my ( $self ) = @_;
    nyi();
}

=over

=item set_ellipsize( $mode )

Sets the type of ellipsization being performed for the layout.

Not yet implemented.

=back

=cut

sub set_ellipsize {
    my ( $self ) = @_;
    nyi();
}

=over

=item get_ellipsize

Gets the type of ellipsization being performed for the layout.

=back

=cut

sub get_ellipsize {
    my ( $self ) = @_;
    return;
}

=over

=item is_ellipsized

Queries whether the layout had to ellipsize any paragraphs.

Not yet implemented.

=back

=cut

sub is_ellipsized {
    my ( $self ) = @_;
    nyi();
}

=over

=item set_indent( $value )

Sets the width in Pango units to indent for each paragraph.

A negative value of indent will produce a hanging indentation. That
is, the first line will have the full width, and subsequent lines will
be indented by the absolute value of indent.

The indent setting is ignored if layout alignment is set to C<center>.

Not yet implemented.

=back

=cut

sub set_indent {
    my ( $self, $size ) = @_;
    $self->{_currentindent} = $self->{_pu2px}->($size);
    nyi();
}

=over

=item get_indent

Gets the current indent value in Pango units.

=back

=cut

sub get_indent {
    my ( $self ) = @_;
    $self->{_px2pu}->($self->{_currentindent}) || 0;
}

=over

=item set_spacing( $value )

Sets the amount of spacing, in Pango units, between lines of the
layout.

When placing lines with spacing, things are arranged so that

    line2.top = line1.bottom + spacing

Note: By default the line height (as determined by the font) for
placing lines is used. The spacing set with this function is only
taken into account when the line-height factor is set to zero with
set_line_spacing().

Not yet implemented.

=back

=cut

sub set_spacing {
    my ( $self, $spacing ) = @_;
    $self->{_currentspacing} = $self->{_pu2px}->($spacing);
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_spacing

Gets the current amount of spacing, in Pango units.

=back

=cut

sub get_spacing {
    my ( $self ) = @_;
    $self->{_px2pu}->($self->{_currentspacing});
}

=over

=item set_line_spacing( $factor )

Sets a factor for line spacing. Typical values are: 0, 1, 1.5, 2. The
default value is 0.

If factor is non-zero, lines are placed so that

    baseline2 = baseline1 + factor * height2

where height2 is the line height of the second line (as determined by
the font(s)). In this case, the spacing set with set_spacing() is ignored.

If factor is zero, spacing is applied as before.

Not yet implemented.

=back

=cut

sub set_line_spacing {
    my ( $self, $factor ) = @_;
    $self->{_currentlinespacing} = $factor;
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_line_spacing

Gets the current line spacing factor.

=back

=cut

sub get_line_spacing {
    my ( $self ) = @_;
    $self->{_currentlinespacing} || 0
;
}

=over

=item set_justify( $state )

Sets whether each complete line should be stretched to fill the entire
width of the layout. This stretching is typically done by adding
whitespace.

Not yet implemented.

=back

=cut

sub set_justify {
    my ( $self, $state ) = @_;
    $self->{_currentjustify} = !!$state;
    nyi();
}

=over

=item get_justify

Gets whether each complete line should be stretched to fill the entire
width of the layout.

=back

=cut

sub get_justify {
    my ( $self ) = @_;
    $self->{_currentjustify};
}

=over

=item set_auto_dir( $state )

=item get_auto_dir

Not supported.

=back

=cut

sub set_auto_dir { nyi() }

sub get_auto_dir { nyi() }

=over

=item set_alignment( $align )

Sets the alignment for the layout: how partial lines are positioned
within the horizontal space available.

$align must be one of C<left>, C<center>, or C<right>,

=back

=cut

sub set_alignment {
    my ( $self, $align ) = @_;
    if ( $align =~ /^(left|right|center)$/i ) {
	$self->{_alignment} = lc $align;
    }
    else {
	croak("Invalid alignment: \"$align\"");
    }
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_alignment

Gets the alignment for this instance.

=back

=cut

sub get_align {
    my ( $self ) = @_;
    $self->{_alignment};
}

=over

=item set_tabs( $stops )

=item get_tabs

Not yet implemented.

=back

=cut

sub set_tabs { nyi() }

sub get_tabs { nyi() }

=over

=item set_single_paragraph_mode( $state )

=item get_single_paragraph_mode

Not yet implemented.

=back

=cut

sub set_single_paragraph_mode { nyi() }

sub get_single_paragraph_mode { nyi() }

=over

=item get_unknown_glyphs_count

Counts the number unknown glyphs in the layout.

Not yet implemented.

=back

=cut

sub get_unknown_glyphs_count { nyi() }

=over

=item get_log_attrs

=item get_log_attrs_readonly

Not implemented.

=back

=cut

sub get_log_attrs { nyi() }

sub get_log_attrs_readonly { nyi() }

=over

=item index_to_pos( $index )

Converts from a character index within the layout to the onscreen position
corresponding to the grapheme at that index, which is represented as
rectangle.

Not yet implemented.

=back

=cut

sub index_to_pos { nyi() }

=over

=item index_to_line_x ( $index )

Converts from a character index within the layout to line and X position.

Not yet implemented.

=back

=cut

sub index_to_line_x { nyi() }

=over

=item xy_to_index ( $x, $y )

Converts from $x,$y position to a character index within the layout.

Not yet implemented.

=back

=cut

sub xy_to_index { nyi() }

=over

=item get_extents

Computes the logical and ink extents of the layout.

Logical extents are usually what you want for positioning things.

Return value in scalar context is a hash ref with 4 values:
 C<x>, C<y>, C<width>, and C<height>
describing the logical extents of the layout.
In list context an array of two hashrefs is returned.
The first reflects the ink extents, the second the logical extents.

In the extents, C<x> will reflect the offset when text is centered or
right aligned. It will be zero for left aligned text. For right
aligned text, it will be the width of the layout.

C<y> will reflect the offset when text is centered vertically or
bottom aligned. It will be zero for top aligned text.

See also get_pixel_extents below.

Implementation note: If the PDF::API support layer cannot calculate ink,
this function returns two identical extents.

=back

=cut

sub get_extents {
    my ( $self ) = @_;
    my $need_ink = wantarray;

    my @bb = $self->get_bbox($need_ink);
    my $res = { bl    => $bb[0],
		x     => $bb[1], y      => $bb[2],
		width => $bb[3], height => $bb[4] };
    my $ink = $res;
    if ( @bb > 5 ) {
	$ink = { bl    => $bb[0],
		 x     => $bb[5], y      => $bb[6],
		 width => $bb[7], height => $bb[8] };
    }
    return $need_ink ? ( $ink, $res ) : $res;
}

=over

=item get_pixel_extents

Same as get_extents, but using device units.

The returned values are suitable for (assuming $pdf_text and
$pdf_gfx are the PDF text and graphics contexts):

    $layout->render( $x, $y, $pdf_text );
    $box = $layout->get_pixel_extents;
    $pdf_gfx->translate( $x, $y );
    $pdf_gfx->rect( @$box{ qw( x y width height ) } );
    $pdf_gfx->stroke;

=back

=cut

sub get_pixel_extents {
    my ( $self ) = @_;

    my $need_ink = wantarray;

    my @bb = $self->get_pixel_bbox($need_ink);
    my $res = { bl    => $bb[0],
		x     => $bb[1], y      => $bb[2],
		width => $bb[3], height => $bb[4] };
    my $ink = $res;
    if ( @bb > 5 ) {
	$ink = { bl    => $bb[0],
		 x     => $bb[5], y      => $bb[6],
		 width => $bb[7], height => $bb[8] };
    }
    return $need_ink ? ( $ink, $res ) : $res;
}

=over

=cut

=item get_size

Returns the width and height of this layout.

In list context, width and height are returned as an two-element list.
In scalar context a hashref with keys C<width> and C<height> is
returned.

=back

=cut

sub get_size {
    my ( $self ) = @_;
    my $e = $self->get_extents;
    wantarray
      ? return ( $e->{width}, $e->{height} )
      : return { width => $e->{width}, height => $e->{height} };
}

=over

=item get_pixel_size

Same as get_size().

=back

=cut

sub get_pixel_size {
    my ( $self ) = @_;
    my $e = $self->get_pixel_extents;
    wantarray
      ? return ( $e->{width}, $e->{height} )
      : return { width => $e->{width}, height => $e->{height} };
}

=over

=item get_iter

Returns the layout for the first line.

Implementation note: This is a dummy, it returns the layout.
It is provided so you can write $layout->get_iter()->get_baseline()
to be compatible with the official Pango API.

=back

=cut

sub get_iter { $_[0] }

=over

=item get_baseline

Gets the Y position of the baseline of the first line in this layout.

Implementation note: Position is relative to top left, so due to the
PDF coordinate system this is a negative number.

Note: The Python API only supports this method on iteration objects.
See get_iter().

=back

=cut

sub get_baseline {
    my ( $self ) = @_;
    return -$self->get_bbox->[0];
}

=head1 METHODS NOT IMPLEMENTED

=over

=item get_line_count

=item get_line( $index )

=item get_line_readonly( $index )

=item get_lines

=item get_lines_readonly

=item line_get_extents

=item line_get_pixel_entents

=item line_index_to_x

=item line_x_to_index

=item line_get_x_ranges

=item line_get_height

=item get_cursor_pos

=item move_cursor_visually

=back

=head2 ADDITIONAL METHODS

The following methods are not part of the Pango API.

=over

=item set_font_size( $size )

Sets the size for the current font.

=back

=cut

sub set_font_size {
    my ( $self, $size ) = @_;
    $self->{_currentsize} = $size;
    delete( $self->{_bbcache} );
    delete( $self->{_struts} );
}

=over

=item get_font_size

Returns the size of the current font.

=back

=cut

sub get_font_size {
    my ( $self ) = @_;
    $self->{_currentsize};
}

=over

=item get_bbox

Returns the bounding box of the text, w.r.t. the (top-left) origin.

bb = ( bl, x, y, width, height )

bb[0] = baseline distance from the top.

bb[1] = displacement from the left, nonzero for centered and right aligned text

bb[2] = displacement from the top, usually zero

bb[3] = advancewidth

bb[4] = height

Note that the bounding box will in general be equal to the font
bounding box except for the advancewidth.

NOTE: Some fonts do not include accents on capital letters in the ascend.

If an argument is supplied and true, get_bbox() will attempt to
calculate the ink extents as well, and add these as another set of 4
elements,

In list context returns the array of values, in scalar context an
array ref.

=back

=cut

sub get_pixel_bbox {
    my ( $self, $all ) = @_;
    my $res;
    if ( $self->{_bbcache}
	 && @{ $self->{_bbcache} } == ($all ? 9 : 5) ) {
	$res = $self->{_bbcache};
    }
    else {
	$res = $self->{_bbcache} = $self->bbox($all);
    }
    wantarray ? @$res : $res;
}

sub get_bbox {
    my ( $self, $all ) = @_;
    my @res = map { $self->{_px2pu}->($_) } ( $self->get_pixel_bbox($all) );
    wantarray ? @res : \@res;
}

=over

=item get_struts

Returns the list of the struts in the layout, if any.

Each element of the list is a hash, with key/value pairs for all the
attributes of the corresponding C<< <strut/> >> markup item.

Additionally, in each element there's a key C<_x> that contains the
horizontal displacement of the strut relative to the star of the layout.

In list context returns the array of values, in scalar context an
array ref.

=back

=cut

sub get_struts {
    my ( $self ) = @_;
    $self->bbox unless $self->{_struts};
    my $res = $self->{_struts};
    wantarray ? @$res : $res;
}

=over

=item align_struts( $other )

Aligns the fragments in both layouts to each other, based on the struts.

This will adjust the widths of the struts of both participants.

=back

=cut

sub align_struts {
    my ( $self, $other ) = @_;
    my @s1 = $self->get_struts;
    my @s2 = $other->get_struts;
    # warn("Struts mismatch\n") unless @s1 == @s2;

    # Accumulated displacements.
    my $dx1 = 0;
    my $dx2 = 0;

    # Process struts.
    for my $s1 ( @s1 ) {
	last unless @s2;

	my $s2 = shift(@s2);

	# Displacement.
	my $d = ( $s1->{_x} + $dx1 ) - ( $s2->{_x} + $dx2 );

	# Adjust the smaller to the larger.
	if ( $d < 0 ) {
	    $s1->{_strut}->{width} -= $d;
	    $dx1 -= $d;
	}
	elsif ( $d > 0 ) {
	    $s2->{_strut}->{width} += $d;
	    $dx2 += $d;
	}
    }
}

=over

=item spread_struts

Evenly distributes the available space over the struts, if any.

=back

=cut

sub spread_struts {
    my ( $self, $width ) = @_;
    my $w = $width || $self->get_width;
    return unless $w;
    my @s1 = $self->get_struts;
    return unless @s1;
    my $dx1 = ( $w - $self->get_pixel_size->{width} ) / @s1;

    # Process struts.
    for my $s1 ( @s1 ) {
	$s1->{_strut}->{width} += $dx1;
    }
}

=over

=item show( $x, $y, $text )

Transfers the content of this layout instance to the designated
graphics environment.

Use this instead of Pango::Cairo::show_layout().

For PDF::API2, $text must be an object created by the $page->text method.

=back

=cut

sub show {
    my ( $self, @data ) = @_;
    $self->render( @data );
}

=over

=item set_pango_mode( $enable )

Enable/disable Pango conformance mode.
See L<Pango Conformance Mode>.

Returns the internal Pango scaling factor if enabled.

=back

=cut

sub set_pango_mode {
    my ( $self, $conformant ) = @_;

    if ( $conformant ) {
	delete( $self->{_bbcache} ), delete( $self->{_struts} )
	  unless $self->{_pango};
	$self->{_px2pu} = sub { $_[0] * PANGO_SCALE };
	$self->{_pu2px} = sub { $_[0] / PANGO_SCALE };
	return $self->{_pango} = PANGO_SCALE;
    }

    delete( $self->{_bbcache} ), delete( $self->{_struts} )
      if $self->{_pango};
    $self->{_px2pu} = \&px2pu;
    $self->{_pu2px} = \&pu2px;
    return $self->{_pango} = 0;
}

# Legacy.
*set_pango_scale = \&set_pango_mode;

=over

=item get_pango

See L<Pango Conformance Mode>.

Returns the internal Pango scaling factor if conformance mode is
enabled, otherwise it returns 1 (one).

=back

=cut

sub get_pango_scale {
    my ( $self ) = @_;
    $self->{_pango} ? PANGO_SCALE : 1;
}

sub nyi {
    croak("Method \"" . (caller(1))[3] . "\" not implemented");
}

my $aliens;
sub register_element {
    my ( $self, $hd, @tags ) = @_;
    croak("Element handler for \"$tags[0]\" does not play nicely")
      unless $hd->DOES(qw(Text::Layout::ElementRole));
    for ( @tags ) {
	$aliens->{$_} = $hd;
    }
}

sub get_element_handler {
    my ( $self, $tag ) = @_;
    return unless $tag;
    $aliens->{$tag};
}

=over

=item register_shortcode( $code, $span, %flags )

Add user-defined shortcodes.
This is just a replacement of <$code> -> <span $span>.

When used as a class method, the shortcodes are accessible to all
layout instances,

Flags:

remove => 1 -- remove this shortcode

=back

=cut

sub register_shortcode {
    my ( $self, $key, $value, %flags ) = @_;

    my $ctl = ref($self) ? $self->{_sc} : \%shortcodes;
    $key = lc($key);

    if ( $flags{remove} ) {
	delete($ctl->{$key}) // croak("No such shortcode: \"$key\"");
    }
    else {
	$ctl->{$key} = $value;
    }
}

=head1 SEE ALSO

Description of the Pango Markup Language:
L<https://docs.gtk.org/Pango/pango_markup.html#pango-markup>.

Documentation of the Pango Layout class:
L<https://docs.gtk.org/Pango/class.Layout.html>.

L<PDF::API2>, L<PDF::Builder>, L<HarfBuzz::Shaper>, L<Font::TTF>.

=head1 AUTHOR

Johan Vromans, C<< <JV at CPAN dot org> >>

=head1 SUPPORT

Development of this module takes place on GitHub:
L<https://github.com/sciurius/perl-Text-Layout>.

You can find documentation for this module with the perldoc command.

  perldoc Text::Layout

Please report any bugs or feature requests using the issue tracker on
GitHub.

=head1 LICENSE

Copyright (C) 2019,2024 Johan Vromans

This module is free software. You can redistribute it and/or
modify it under the terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

1;