[go: up one dir, main page]

Menu

[23fa7c]: / src / lib / WPS8.cpp  Maximize  Restore  History

Download this file

2123 lines (1911 with data), 56.2 kB

   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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* libwps
* Version: MPL 2.0 / LGPLv2.1+
*
* 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/.
*
* Major Contributor(s):
* Copyright (C) 2006, 2007 Andrew Ziem
* Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU Lesser General Public License Version 2.1 or later
* (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
* applicable instead of those above.
*/
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <librevenge-stream/librevenge-stream.h>
#include "libwps_internal.h"
#include "libwps_tools_win.h"
#include "WPSContentListener.h"
#include "WPSEntry.h"
#include "WPSHeader.h"
#include "WPSOLEParser.h"
#include "WPSPageSpan.h"
#include "WPSPosition.h"
#include "WPSTextSubDocument.h"
#include "WPS8Graph.h"
#include "WPS8Struct.h"
#include "WPS8Table.h"
#include "WPS8Text.h"
#include "WPS8.h"
namespace WPS8ParserInternal
{
//! Internal: the subdocument of a WPS8Parser
class SubDocument final : public WPSTextSubDocument
{
public:
//! type of an entry
enum Type { Unknown, TEXT };
//! constructor for a text entry
SubDocument(RVNGInputStreamPtr const &input, WPS8Parser &pars, WPSEntry const &entry)
: WPSTextSubDocument(input, &pars)
, m_entry(entry) {}
//! destructor
~SubDocument() final {}
//! operator==
bool operator==(std::shared_ptr<WPSSubDocument> const &doc) const final
{
if (!doc || !WPSTextSubDocument::operator==(doc))
return false;
auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get());
if (!sDoc) return false;
return m_entry == sDoc->m_entry;
}
//! the parser function
void parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType) final;
//! the entry
WPSEntry m_entry;
};
void SubDocument::parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType)
{
if (!listener.get())
{
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: no listener\n"));
return;
}
if (!dynamic_cast<WPSContentListener *>(listener.get()))
{
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: bad listener\n"));
return;
}
auto &listen = static_cast<WPSContentListenerPtr &>(listener);
if (!m_parser)
{
listen->insertCharacter(' ');
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: bad parser\n"));
return;
}
if (m_entry.isParsed() && subDocumentType != libwps::DOC_HEADER_FOOTER)
{
listen->insertCharacter(' ');
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: this zone is already parsed\n"));
return;
}
m_entry.setParsed(true);
if (m_entry.type() != "Text")
{
listen->insertCharacter(' ');
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: send not Text entry is not implemented\n"));
return;
}
if (!m_entry.valid())
{
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: empty document found...\n"));
listen->insertCharacter(' ');
return;
}
auto *mnParser = dynamic_cast<WPS8Parser *>(m_parser);
if (!mnParser)
{
WPS_DEBUG_MSG(("WPS8ParserInternal::SubDocument::parse: can not find a WPS8Parser...\n"));
listen->insertCharacter(' ');
return;
}
mnParser->send(m_entry);
}
/** Internal: a frame, a zone which can contain text, picture, ... and have some borders */
struct Frame
{
//! constructor
Frame() : m_parsed(false), m_type(UNKNOWN), m_pos(),
m_idStrs(-1), m_idObject(-1), m_idTable(-1), m_idOle(-1), m_columns(1), m_idBorder(),
m_backgroundColor(WPSColor::white()), m_error("")
{
m_pos.setRelativePosition(WPSPosition::Page);
m_pos.setPage(1);
}
//! operator <<
friend std::ostream &operator<<(std::ostream &o, Frame const &ft);
/** The frame type
*
* - Header/Footer: the header and footer frame
* - Table : a table
* - Object : a picture, ...
* - Text : a text zone */
enum { UNKNOWN = 0, DbField, Header, Footer, Table, Object, Text };
//! a flag to know if the frame is already sent to the listener
mutable bool m_parsed;
//! the frame type
int m_type;
//! the frame position
WPSPosition m_pos;
int m_idStrs /** identifier corresponding to a text zone (STRS)*/,
m_idObject /** identifier corresponding to an object zone (EOBJ)*/,
m_idTable /** identifier corresponding to a table (MCLD)*/,
m_idOle /** identifier corresponding to an ole*/;
//! the number of columns for a textbox, ...
int m_columns;
//! the border: an entry to some complex border (if sets)
WPSEntry m_idBorder;
//! the border's color
WPSColor m_backgroundColor;
//! a string used to store the parsing errors
std::string m_error;
};
//! operator<< for a Frame
std::ostream &operator<<(std::ostream &o, Frame const &ft)
{
switch (ft.m_type)
{
case Frame::DbField:
o<< "db[field]";
break;
case Frame::Header:
o<< "header";
break;
case Frame::Footer:
o<< "footer";
break;
case Frame::Table:
o<< "table";
break;
case Frame::Text:
o<< "textbox";
break;
case Frame::Object:
o<< "object";
break;
default:
o << "###type=unknown";
break;
}
o << "(";
if (ft.m_idStrs >= 0) o << "STRS" << ft.m_idStrs << ",";
if (ft.m_idObject >= 0) o << "EOBJ" << ft.m_idObject << ",";
if (ft.m_idTable >= 0) o << "MCLD/Table" << ft.m_idTable << ",";
if (ft.m_idOle >= 0) o << "oleId=" << ft.m_idOle << ",";
o << "),";
o << ft.m_pos << ",";
switch (ft.m_pos.page())
{
case -1:
o << "allpages,";
break;
case -2:
break; // undef
default:
if (ft.m_pos.page() < 0) o << "###page=" << ft.m_pos.page() << ",";
}
if (ft.m_columns != 1) o << ft.m_columns << "columns,";
if (ft.m_idBorder.valid())
o << "border='" << ft.m_idBorder.name() << "':" << ft.m_idBorder.id() << ",";
if (!ft.m_backgroundColor.isWhite())
o << "backColor=" << ft.m_backgroundColor << ",";
if (!ft.m_error.empty()) o << "errors=(" << ft.m_error << ")";
return o;
}
//! the state of WPS8
struct State
{
State()
: m_eof(-1)
, m_pageSpan()
, m_localeLanguage("")
, m_background()
, m_frameList()
, m_object2FrameMap()
, m_table2FrameMap()
, m_docPropertyTypes()
, m_frameTypes()
, m_numColumns(1)
, m_actPage(0)
, m_numPages(0)
{
initTypeMaps();
}
//! initializes the type map
void initTypeMaps();
//! the end of file
long m_eof;
//! the actual document size
WPSPageSpan m_pageSpan;
//! the language
std::string m_localeLanguage;
//! an identificator to design a background picture
WPSEntry m_background;
//! the frame's list
std::vector<Frame> m_frameList;
//! a map m_idObject -> frame
std::map<int, int> m_object2FrameMap;
//! a map m_idTable -> frame
std::map<int, int> m_table2FrameMap;
//! the document property type
std::map<int,int> m_docPropertyTypes;
//! the frame type
std::map<int,int> m_frameTypes;
int m_numColumns /** the number of columns */;
int m_actPage /** the actual page*/, m_numPages /* the number of pages */;
};
void State::initTypeMaps()
{
static int const docTypes[] =
{
0, 0x22, 1, 0x22, 2, 0x22, 3, 0x22, 4, 0x22, 5, 0x22, 6, 0x22, 7, 0x22,
8, 0x1a, 0xa, 0x2,
0x13, 0x2a, 0x15, 0x2a,
0x18, 0x12, 0x19, 0x2, 0x1b, 0x12, 0x1c, 0x22, 0x1e, 0x22,
0x26, 0x22, 0x27, 0x22,
0x28, 0x22, 0x29, 0x22, 0x2a, 0x22, 0x2b, 0x22, 0x2c, 0x82, 0x2d, 0x82, 0x2e, 0x82
};
for (int i = 0; i+1 < int(WPS_N_ELEMENTS(docTypes)); i+=2)
m_docPropertyTypes[docTypes[i]] = docTypes[i+1];
static int const frameTypes[] =
{
0, 0x1a, 1, 0x12, 2, 0x12, 3, 0x2, 4, 0x22, 5, 0x22, 6, 0x22, 7, 0x22,
8, 0x22, 9, 0x22, 0xa, 0x22,
0x10, 0x2a, 0x11, 0x82, 0x13, 0x12, 0x14, 0x12, 0x16, 0x2a, 0x17, 0x2,
0x18, 0x22, 0x19, 0x22, 0x1a, 0x12, 0x1b, 0x22, 0x1d, 0x22, 0x1e, 0x22, 0x1f, 0x22,
0x20, 0x22, 0x26, 0x22,
0x2a, 0x22, 0x2c, 0x1a, 0x2d, 0x1a, 0x2e, 0x22, 0x2f, 0x2,
0x30, 0x22
};
for (int i = 0; i+1 < int(WPS_N_ELEMENTS(frameTypes)); i+=2)
m_frameTypes[frameTypes[i]] = frameTypes[i+1];
}
class TableRecursionGuard
{
// disable copying
TableRecursionGuard(const TableRecursionGuard &) = delete;
TableRecursionGuard &operator=(const TableRecursionGuard &) = delete;
public:
TableRecursionGuard(std::deque<int> &stack, const int id)
: m_stack(stack)
{
m_stack.push_front(id);
}
~TableRecursionGuard()
{
m_stack.pop_front();
}
private:
std::deque<int> &m_stack;
};
}
// constructor, destructor
WPS8Parser::WPS8Parser(RVNGInputStreamPtr &input, WPSHeaderPtr &header)
: WPSParser(input, header)
, m_listener()
, m_graphParser()
, m_tableParser()
, m_textParser()
, m_state()
, m_sendingTables()
{
if (version()<5) setVersion(5);
m_state.reset(new WPS8ParserInternal::State);
m_graphParser.reset(new WPS8Graph(*this));
m_tableParser.reset(new WPS8Table(*this));
m_textParser.reset(new WPS8Text(*this));
}
WPS8Parser::~WPS8Parser()
{
}
// small funtion ( dimension, ...)
float WPS8Parser::pageHeight() const
{
return float(m_state->m_pageSpan.getFormLength()-m_state->m_pageSpan.getMarginTop()-m_state->m_pageSpan.getMarginBottom());
}
float WPS8Parser::pageWidth() const
{
return float(m_state->m_pageSpan.getFormWidth()-m_state->m_pageSpan.getMarginLeft()-m_state->m_pageSpan.getMarginRight());
}
int WPS8Parser::numColumns() const
{
return m_state->m_numColumns;
}
bool WPS8Parser::checkInFile(long pos)
{
if (pos <= m_state->m_eof)
return true;
RVNGInputStreamPtr input = getInput();
long actPos = input->tell();
input->seek(pos, librevenge::RVNG_SEEK_SET);
bool ok = input->tell() == pos;
if (ok) m_state->m_eof = pos;
input->seek(actPos, librevenge::RVNG_SEEK_SET);
return ok;
}
// listener, new page
void WPS8Parser::setListener(std::shared_ptr<WPSContentListener> const &listener)
{
m_listener = listener;
m_graphParser->setListener(m_listener);
m_tableParser->setListener(m_listener);
m_textParser->setListener(m_listener);
}
std::shared_ptr<WPSContentListener> WPS8Parser::createListener(librevenge::RVNGTextInterface *interface)
{
std::vector<WPSPageSpan> pageList;
WPSPageSpan ps(m_state->m_pageSpan);
int numPages = 1;
for (auto const &frame : m_state->m_frameList)
{
if (frame.m_pos.page() > numPages)
numPages = frame.m_pos.page();
}
int textPages = m_textParser->numPages();
if (textPages > numPages) numPages = textPages;
int tablePages = m_tableParser->numPages();
if (tablePages > numPages) numPages = tablePages;
int graphPages = m_graphParser->numPages();
if (graphPages>=numPages) numPages = graphPages;
WPSEntry entry = m_textParser->getHeaderEntry();
if (entry.valid())
{
WPSSubDocumentPtr subdoc(new WPS8ParserInternal::SubDocument
(getInput(), *this, entry));
ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::ALL, subdoc);
}
entry = m_textParser->getFooterEntry();
if (entry.valid())
{
WPSSubDocumentPtr subdoc(new WPS8ParserInternal::SubDocument
(getInput(), *this, entry));
ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::ALL, subdoc);
}
#ifdef DEBUG
// create all the pages + an empty page, if we have some remaining data...
numPages++;
#endif
pageList.push_back(ps);
for (int i = 1; i < numPages; i++) pageList.push_back(ps);
m_state->m_numPages=numPages;
return std::shared_ptr<WPSContentListener>
(new WPSContentListener(pageList, interface));
}
void WPS8Parser::newPage(int number)
{
if (number <= m_state->m_actPage || number > m_state->m_numPages)
return;
while (m_state->m_actPage < number)
{
m_state->m_actPage++;
if (m_listener.get() == nullptr || m_state->m_actPage == 1)
continue;
m_listener->insertBreak(WPS_PAGE_BREAK);
m_graphParser->sendObjects(m_state->m_actPage);
}
}
////////////////////////////////////////////////////////////
// interface with the graph/text parser
void WPS8Parser::send(WPSEntry const &entry)
{
RVNGInputStreamPtr input = getInput();
long actPos = input->tell();
m_textParser->readText(entry);
input->seek(actPos, librevenge::RVNG_SEEK_SET);
}
void WPS8Parser::send(int strsId)
{
send(m_textParser->getEntry(strsId));
}
void WPS8Parser::sendTextInCell(int strsId, int cellId)
{
RVNGInputStreamPtr input = getInput();
long actPos = input->tell();
m_textParser->readTextInCell(strsId,cellId);
input->seek(actPos, librevenge::RVNG_SEEK_SET);
}
void WPS8Parser::sendTextBox(WPSPosition const &pos, int strsid, librevenge::RVNGPropertyList frameExtras)
{
if (!m_listener)
{
WPS_DEBUG_MSG(("WPS8Parser::sendTextBox can not find the listener\n"));
return;
}
WPSEntry entry = m_textParser->getEntry(strsid);
WPSSubDocumentPtr subdoc(new WPS8ParserInternal::SubDocument
(getInput(), *this, entry));
m_listener->insertTextBox(pos, subdoc, frameExtras);
}
bool WPS8Parser::sendObject(Vec2f const &size, int objectId, bool ole)
{
WPSPosition posi(Vec2f(),size);
posi.setRelativePosition(WPSPosition::CharBaseLine);
posi.m_wrapping = WPSPosition::WDynamic;
m_graphParser->sendObject(posi,objectId,ole);
return true;
}
bool WPS8Parser::sendTable(Vec2f const &size, int objectId)
{
if (find(m_sendingTables.begin(), m_sendingTables.end(), objectId) != m_sendingTables.end())
{
WPS_DEBUG_MSG(("WPS8Parser::sendTable table %d is contained in itself\n", objectId));
return false;
}
auto pos = m_state->m_object2FrameMap.find(objectId);
if (pos == m_state->m_object2FrameMap.end())
{
WPS_DEBUG_MSG(("WPS8Parser::sendTable can not find the table %d \n", objectId));
return false;
}
auto const &frame = m_state->m_frameList[(size_t)pos->second];
if (frame.m_idStrs < 0)
{
WPS_DEBUG_MSG(("WPS8Parser:sendTable can not find the text zone \n"));
return false;
}
if (frame.m_idTable < 0)
{
WPSPosition position(Vec2f(), size);
position.m_anchorTo = WPSPosition::CharBaseLine; // CHECKME
position.m_wrapping = WPSPosition::WDynamic;
WPS_DEBUG_MSG(("WPS8Parser:sendTable can not find the table zone \n"));
sendTextBox(position, frame.m_idStrs);
return true;
}
frame.m_parsed = true;
WPS8ParserInternal::TableRecursionGuard guard(m_sendingTables, objectId);
return m_tableParser->sendTable(size, frame.m_idTable, frame.m_idStrs);
}
int WPS8Parser::getTableSTRSId(int tableId) const
{
auto pos = m_state->m_table2FrameMap.find(tableId);
// probably ok: checkme
if (pos == m_state->m_table2FrameMap.end())
return -1;
auto const &frame = m_state->m_frameList[(size_t)pos->second];
if (frame.m_idStrs < 0)
{
WPS_DEBUG_MSG(("WPS8Parser:getTableSTRSId can not find the text zone \n"));
}
return frame.m_idStrs;
}
////////////////////////////////////////////////////////////
// main function to parse the document
void WPS8Parser::parse(librevenge::RVNGTextInterface *documentInterface)
{
RVNGInputStreamPtr input=getInput();
if (!input)
{
WPS_DEBUG_MSG(("WPS8Parser::parse: does not find main ole\n"));
throw (libwps::ParseException());
}
try
{
createOLEStructures();
}
catch (...)
{
WPS_DEBUG_MSG(("WPS8Parser::parse: exception catched when parsing secondary OLEs\n"));
}
ascii().setStream(input);
ascii().open("CONTENTS");
try
{
if (!createStructures()) throw(libwps::ParseException());
}
catch (...)
{
WPS_DEBUG_MSG(("WPS8Parser::parse: exception catched when parsing MN0\n"));
throw (libwps::ParseException());
}
setListener(createListener(documentInterface));
if (!m_listener)
{
WPS_DEBUG_MSG(("WPS8Parser::parse: can not create the listener\n"));
throw (libwps::ParseException());
}
m_listener->startDocument();
sendPageFrames();
WPSEntry ent = m_textParser->getTextEntry();
if (ent.valid())
m_textParser->readText(ent);
else
{
WPS_DEBUG_MSG(("WPS8Parser::parse: can not find main text entry\n"));
throw (libwps::ParseException());
}
#ifdef DEBUG
m_tableParser->flushExtra();
#endif
m_textParser->flushExtra();
#ifdef DEBUG
m_graphParser->sendObjects(-1);
#endif
m_listener->endDocument();
m_listener.reset();
ascii().reset();
}
// find and create all the zones ( normal/ole )
bool WPS8Parser::createStructures()
{
if (!getInput()) return false;
RVNGInputStreamPtr input = getInput();
parseHeaderIndex();
// initialize the text, table, ..
if (!m_textParser->readStructures())
return false;
m_graphParser->readStructures(input);
m_tableParser->readStructures(input);
// read DOP zone (document properties)
auto pos = getNameEntryMap().lower_bound("DOP ");
while (getNameEntryMap().end() != pos)
{
WPSEntry const &entry = pos++->second;
if (!entry.hasName("DOP ")) break;
if (!entry.hasType("DOP ")) continue;
WPSPageSpan page;
if (readDocProperties(entry, page)) m_state->m_pageSpan = page;
}
// printer data
pos = getNameEntryMap().lower_bound("PRNT");
while (getNameEntryMap().end() != pos)
{
WPSEntry const &entry = pos++->second;
if (!entry.hasName("PRNT")) break;
if (!entry.hasType("WNPR")) continue;
readWNPR(entry);
}
// SYID
pos = getNameEntryMap().lower_bound("SYID");
while (getNameEntryMap().end() != pos)
{
WPSEntry const &entry = pos++->second;
if (!entry.hasName("SYID")) break;
if (!entry.hasType("SYID")) continue;
std::vector<int> listId;
readSYID(entry, listId);
}
// document title
pos = getNameEntryMap().find("TITL");
librevenge::RVNGString title;
if (getNameEntryMap().end() != pos && pos->second.hasType("TITL"))
{
pos->second.setParsed(true);
input->seek(pos->second.begin(), librevenge::RVNG_SEEK_SET);
m_textParser->readString(input, pos->second.length(), title);
ascii().addPos(pos->second.begin());
ascii().addNote(title.cstr());
}
// ok, we can now read the frame
pos = getNameEntryMap().lower_bound("FRAM");
while (getNameEntryMap().end() != pos)
{
WPSEntry const &entry = pos++->second;
if (!entry.hasName("FRAM")) break;
if (!entry.hasType("FRAM")) continue;
readFRAM(entry);
}
// FRCD
pos = getNameEntryMap().lower_bound("FRCD");
while (getNameEntryMap().end() != pos)
{
WPSEntry const &entry = pos++->second;
if (!entry.hasName("FRCD")) break;
if (!entry.hasType("FRCD")) continue;
readFRCD(entry);
}
// creates the correspondance between the eobj and the frame
size_t numFrames = m_state->m_frameList.size();
for (size_t i = 0; i < numFrames; i++)
{
WPS8ParserInternal::Frame const &frame = m_state->m_frameList[i];
if (frame.m_idObject < 0) continue;
m_state->m_object2FrameMap[frame.m_idObject] = (int) i;
m_state->m_table2FrameMap[frame.m_idTable] = (int) i;
}
m_graphParser->computePositions();
return true;
}
bool WPS8Parser::createOLEStructures()
{
RVNGInputStreamPtr input=getFileInput();
if (!input) return false;
if (!input->isStructured()) return true;
WPSOLEParser oleParser("CONTENTS", libwps_tools_win::Font::WIN3_WEUROPE);
if (!oleParser.parse(input)) return false;
m_graphParser->storeObjects(oleParser.getObjectsMap());
#ifdef DEBUG
// there can remain some embedded Works subdocument ( WKS, ... )
// with name MN0 and some unknown picture ole
auto unparsed = oleParser.getNotParse();
for (std::string const &name : unparsed)
{
if (name == "CONTENTS")
continue;
if (name== "SPELLING")
{
RVNGInputStreamPtr ole(input->getSubStreamByName(name.c_str()));
if (ole && readSPELLING(ole, name))
continue;
}
WPS_DEBUG_MSG(("WPS8Parser::createOLEStructures:: Find unparsed ole: %s\n", name.c_str()));
#ifdef DEBUG_WITH_FILES
RVNGInputStreamPtr ole(input->getSubStreamByName(name.c_str()));
if (!ole.get())
{
WPS_DEBUG_MSG(("WPS8Parser::createOLEStructures: error: can find OLE part: \"%s\"\n", name.c_str()));
continue;
}
librevenge::RVNGBinaryData data;
if (libwps::readDataToEnd(ole,data))
libwps::Debug::dumpFile(data, name.c_str());
#endif
}
#endif
return true;
}
////////////////////////////////////////////////////////////
// send the frames which correspond to a page
////////////////////////////////////////////////////////////
void WPS8Parser::sendPageFrames()
{
if (!m_listener) return;
RVNGInputStreamPtr input = getInput();
long actPos = input->tell();
for (auto const &frame : m_state->m_frameList)
{
if (frame.m_parsed) continue;
if (frame.m_pos.page() < 0 || frame.m_idObject != -1) continue;
if (frame.m_pos.size()[0] <= 0 || frame.m_pos.size()[1] <= 0) continue;
WPSPosition pos(frame.m_pos);
if (pos.origin().y() < 0 || pos.origin().x() < 0)
{
if (frame.m_type == WPS8ParserInternal::Frame::DbField) continue;
// FIXME: the
WPS_DEBUG_MSG(("WPS8Parser::insertPageFrames: origin is too small\n"));
Vec2f orig = pos.origin();
orig += Vec2f(float(m_state->m_pageSpan.getMarginLeft()),
float(m_state->m_pageSpan.getMarginTop()));
pos.setOrigin(orig);
}
pos.m_anchorTo = WPSPosition::Page;
pos.m_wrapping = WPSPosition::WDynamic;
frame.m_parsed = true;
if (frame.m_type == WPS8ParserInternal::Frame::Object)
m_graphParser->sendObject(pos, frame.m_idOle, true);
else if (frame.m_type == WPS8ParserInternal::Frame::Text)
{
librevenge::RVNGPropertyList frameExtras;
if (!frame.m_backgroundColor.isWhite())
frameExtras.insert("fo:background-color", frame.m_backgroundColor.str().c_str());
sendTextBox(pos, frame.m_idStrs, frameExtras);
}
else if (frame.m_type != WPS8ParserInternal::Frame::Header && frame.m_type != WPS8ParserInternal::Frame::Footer)
{
WPS_DEBUG_MSG(("WPS8Parser::insertPageFrames: not implemented type%d\n", int(frame.m_type)));
}
}
input->seek(actPos,librevenge::RVNG_SEEK_SET);
}
////////////////////////////////////////////////////////////
// read the index
bool WPS8Parser::parseHeaderIndexEntry()
{
RVNGInputStreamPtr input = getInput();
long pos = input->tell();
ascii().addPos(pos);
libwps::DebugStream f;
uint16_t cch = libwps::readU16(input);
// check if the entry can be read
input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
if (input->tell() != pos+cch)
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndexEntry: error: incomplete entry\n"));
ascii().addNote("###IndexEntry incomplete (ignored)");
return false;
}
input->seek(pos + 2, librevenge::RVNG_SEEK_SET);
if (0x18 != cch)
{
if (cch < 0x18)
{
input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
ascii().addNote("###IndexEntry too short(ignored)");
if (cch < 10) throw libwps::ParseException();
return true;
}
}
std::string name;
// sanity check
int i;
for (i =0; i < 4; i++)
{
uint8_t c = libwps::readU8(input);
name.append(1, char(c));
if (c != 0 && c != 0x20 && (41 > c || c > 90))
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndexEntry: error: bad character=%u (0x%02x) in name in header index\n", (unsigned)c, (unsigned)c));
ascii().addNote("###IndexEntry bad name(ignored)");
input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
return true;
}
}
f << "Entries("<<name<<")";
if (cch != 24) f << ", #size=" << int(cch);
auto id = (int) libwps::readU16(input);
f << ", id=" << id << ", (";
for (i = 0; i < 2; i ++)
{
auto val = (int) libwps::read16(input);
f << val << ",";
}
std::string name2;
for (i =0; i < 4; i++)
name2.append(1, (char) libwps::readU8(input));
f << "), " << name2;
WPSEntry hie;
hie.setName(name);
hie.setType(name2);
hie.setId(id);
hie.setBegin((long) libwps::readU32(input));
hie.setLength((long) libwps::readU32(input));
f << ", offset=" << std::hex << hie.begin() << ", length=" << hie.length();
std::string mess;
if (cch != 0x18 && parseHeaderIndexEntryEnd(pos+cch, hie, mess))
f << "," << mess;
input->seek(hie.end(), librevenge::RVNG_SEEK_SET);
if (input->tell() != hie.end())
{
f << ", ###ignored";
ascii().addNote(f.str().c_str());
input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
return true;
}
getNameEntryMap().insert(NameMultiMap::value_type(name, hie));
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
ascii().addPos(hie.begin());
f.str("");
f << name;
if (name != name2) f << "/" << name2;
f << ":" << std::dec << id;
ascii().addNote(f.str().c_str());
ascii().addPos(hie.end());
ascii().addNote("_");
input->seek(pos + cch, librevenge::RVNG_SEEK_SET);
return true;
}
//
// read the end the entry index: normally a string name
//
bool WPS8Parser::parseHeaderIndexEntryEnd(long endPos, WPSEntry &hie, std::string &mess)
{
RVNGInputStreamPtr input = getInput();
long pos = input->tell();
long len = endPos-pos;
libwps::DebugStream f;
auto size = (int) libwps::read16(input);
librevenge::RVNGString str;
if (2*(size+1) != len || !m_textParser->readString(input, 2*size, str))
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndexEntryEnd: unknown data\n"));
ascii().addPos(pos);
ascii().addNote("Entry(end): ###ignored");
}
else
{
hie.setExtra(str.cstr());
f << "'" << str.cstr() << "'";
mess = f.str();
}
return true;
}
/**
* In the header, parse the index to the different sections of
* the CONTENTS stream.
*
*/
bool WPS8Parser::parseHeaderIndex()
{
RVNGInputStreamPtr input = getInput();
getNameEntryMap().clear();
input->seek(0x08, librevenge::RVNG_SEEK_SET);
long pos = input->tell();
auto i0 = (int) libwps::read16(input);
auto i1 = (int) libwps::read16(input);
uint16_t n_entries = libwps::readU16(input);
// fixme: sanity check n_entries
libwps::DebugStream f;
f << "Entries(HeaderZ): N=" << n_entries << ", " << i0 << ", " << i1 << "(";
for (int i = 0; i < 4; i++)
f << std::hex << libwps::read16(input) << std::dec << ",";
f << "), ";
f << "unk=" << std::hex <<libwps::read16(input) << std::dec << ",";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
input->seek(0x18, librevenge::RVNG_SEEK_SET);
bool readSome = false;
do
{
if (input->isEnd()) return readSome;
pos = input->tell();
f.str("");
uint16_t unknown1 = libwps::readU16(input); // function of the size of the entries ?
uint16_t n_entries_local = libwps::readU16(input);
f << "HeaderZ("<<std::hex << unknown1<<"): N=" << std::dec << n_entries_local;
if (n_entries_local > 0x20)
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndex: error: n_entries_local=%i\n", n_entries_local));
return readSome;
}
uint32_t next_index_table = libwps::readU32(input);
f << std::hex << ", nextHeader=" << next_index_table;
if (next_index_table != 0xFFFFFFFF && long(next_index_table) < pos)
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndex: error: next_index_table=%x decreasing !!!\n", next_index_table));
return readSome;
}
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
do
{
if (!parseHeaderIndexEntry()) return readSome;
readSome=true;
n_entries--;
n_entries_local--;
}
while (n_entries > 0 && n_entries_local);
if (0xFFFFFFFF == next_index_table && n_entries > 0)
{
WPS_DEBUG_MSG(("WPS8Parser::parseHeaderIndex: error: expected more header index entries\n"));
return n_entries > 0;
}
if (0xFFFFFFFF == next_index_table) break;
if (input->seek((long) next_index_table, librevenge::RVNG_SEEK_SET) != 0) return readSome;
}
while (n_entries > 0);
return true;
}
////////////////////////////////////////////////////////////
// low level
////////////////////////////////////////////////////////////
// read DOP zone: the document properties
bool WPS8Parser::readDocProperties(WPSEntry const &entry, WPSPageSpan &page)
{
if (!entry.hasType(entry.name()))
{
WPS_DEBUG_MSG(("WPS8Parser::readDocProperties: warning: DOP name=%s, type=%s\n",
entry.name().c_str(), entry.type().c_str()));
return false;
}
RVNGInputStreamPtr input = getInput();
page=WPSPageSpan();
long page_offset = entry.begin();
long length = entry.length();
long endPage = entry.end();
if (length < 2)
{
WPS_DEBUG_MSG(("WPS8Parser::readDocProperties: warning: DOP length=0x%lx\n", (unsigned long) length));
return false;
}
entry.setParsed();
input->seek(page_offset, librevenge::RVNG_SEEK_SET);
libwps::DebugStream f, f2;
if (libwps::read16(input) != length)
{
WPS_DEBUG_MSG(("WPS8Parser::readDocProperties: invalid length=%ld\n", length));
return false;
}
WPS8Struct::FileData mainData;
std::string error;
bool readOk = readBlockData(input, endPage,mainData, error);
double dim[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool setVal[8] = { false, false, false, false, false, false, false, false};
for (auto const &dt : mainData.m_recursData)
{
if (dt.isBad()) continue;
if (m_state->m_docPropertyTypes.find(dt.id())==m_state->m_docPropertyTypes.end())
{
f << "##" << dt << ",";
continue;
}
if (m_state->m_docPropertyTypes.find(dt.id())->second != dt.type())
{
WPS_DEBUG_MSG(("WPS8Parser::readDocProperties: unexpected type for %d=%d\n", dt.id(), dt.type()));
f << "###" << dt << ",";
continue;
}
// is dt.id()==6 another dimension ?
if (dt.id() >= 0 && (dt.id() < 6 || dt.id()==7))
{
dim[dt.id()] = float(dt.m_value)/914400.f;
setVal[dt.id()] = true;
continue;
}
bool ok = true;
switch (dt.id())
{
case 0x8:
if (!dt.m_value) break;
if (dt.m_value >= 1 && dt.m_value <= 13)
m_state->m_numColumns = int(1+dt.m_value);
else
f2 << "#";
f2 << "numCols=" << dt.m_value+1 << ",";
break;
case 0x18:
switch (dt.m_value)
{
case 1:
break; // portrait
case 2:
page.setFormOrientation(WPSPageSpan::LANDSCAPE);
f2 << "landscape,";
break;
default:
f2 << "#pageOrientation=" << dt.m_value << ",";
break;
}
break;
case 0x1b: // [-1,0,1,3,4]
f2 << "f" << dt.id() << "=" << (int) int8_t(dt.m_value) << ",";
break;
case 0x13: // an entries index, always an IBGF ?
m_state->m_background.setName(dt.m_text);
m_state->m_background.setId(int(dt.m_value));
f2 << "background(entries)='" << dt.m_text << "':" << dt.m_value << ",";
break;
case 0x15: // another entries index, always an 'BDR '?
WPS_DEBUG_MSG(("WPS8Parser::readDocProperties: find a BDR entry, not implemented\n"));
f2 << "pageBorder(entries)='" << dt.m_text << "':" << dt.m_value << ",";
break;
case 0xa:
if (dt.isTrue()) f2 << "colSep(line),";
break;
case 0x19: // almost always set
if (dt.isFalse())
f2 << "f" << dt.id() << "=false,";
else
f2 << "f" << dt.id() << ",";
break;
case 0x1c: // 0.1
f2 << "colSep=" << float(dt.m_value)/914400.f << "(inch),";
break;
case 0x28: // main language ?
f2 << "lang?=" << libwps_tools_win::Language::name(dt.m_value) << ",";
m_state->m_localeLanguage = libwps_tools_win::Language::localeName(dt.m_value);
break;
case 0x1e: // -3, 47 ou 85
case 0x29: // 1 ou 2
f2 << "f" << dt.id() << "=" << dt.m_value << ",";
break;
// 0x2a: an integer = 4|6|b
// 0x2b: an integer = 0|8|c
case 0x2c:
{
if (dt.isRead() || !dt.isArray())
{
ok = false;
break;
}
auto size = int(dt.end()-dt.begin());
if (size < 2 || size%2 != 0)
{
ok = false;
break;
}
long actPos = input->tell();
int numElt = (size-2)/2;
input->seek(dt.begin()+2, librevenge::RVNG_SEEK_SET);
std::string str;
int elt=0;
while (elt < numElt)
{
long val = libwps::read16(input);
if (val < 30 && val != '\t' && val != '\n')
{
input->seek(-2, librevenge::RVNG_SEEK_CUR);
break;
}
elt++;
str += (char) val;
}
if (!str.length())
{
ok = false;
input->seek(actPos, librevenge::RVNG_SEEK_SET);
break;
}
f2 << "filename?=["; // wkthmLET.fmt and wkthmSCH.fmt
if (str.length()) f2 << "\"" << str << "\",";
numElt -= elt;
elt=0;
while (elt++ < numElt)
{
auto val = (long) libwps::readU16(input);
if (val) f2 << "f" << elt << "=" << std::hex << val << std::dec << ",";
}
f2 << "],";
input->seek(actPos, librevenge::RVNG_SEEK_SET);
break;
}
case 0x2d:
case 0x2e:
{
// In one file with unk2d=[f1=20,], unk2e=[f2=20,]
if (dt.isRead() || !dt.isArray())
{
ok = false;
break;
}
auto size = int(dt.end()-dt.begin());
if (size != 0x100)
{
ok = false;
break;
}
long actPos = input->tell();
input->seek(dt.begin(), librevenge::RVNG_SEEK_SET);
f2 << "unk" << std::hex << dt.id() << "=" << "[";
for (int i = 0; i < 128; i++)
{
auto val = (long) libwps::readU16(input);
if (val) f2 << "f" << i << "=" << std::hex << val << std::dec << ",";
}
f2 << "]" << std::dec << ",";
input->seek(actPos, librevenge::RVNG_SEEK_SET);
break;
}
default:
ok = false;
break;
}
if (ok) continue;
f2 << dt << ",";
}
if (setVal[0])
{
if (dim[0] < 0.5 || dim[0] > 40) f << "###";
else page.setFormWidth(dim[0]);
f << "width=" << dim[0] << ",";
}
if (setVal[1])
{
if (dim[1] < 0.5 || dim[1] > 40) f << "###";
else page.setFormLength(dim[1]);
f << "height=" << dim[1] << ",";
}
f << "margin=[";
for (int i = 2; i < 6; i++)
{
if (!setVal[i])
{
f << "_,";
continue;
}
int w = i%2;
double dd = w ? page.getFormWidth() : page.getFormLength();
bool ok = (dim[i] >= 0) && (2*dim[i] < dd);
switch (i)
{
case 2:
if (ok) page.setMarginTop(dim[i]);
break;
case 3:
if (ok) page.setMarginLeft(dim[i]);
break;
case 4:
if (ok) page.setMarginBottom(dim[i]);
break;
case 5:
if (ok) page.setMarginRight(dim[i]);
break;
default:
break;
}
if (!ok) f << "###";
f << dim[i] << ",";
}
f << "],";
if (setVal[7]) f << "bordDim?=" << dim[7] << ",";
f << f2.str();
if (!readOk) f << "###or [" << mainData << "]";
ascii().addPos(page_offset);
ascii().addNote(f.str().c_str());
return true;
}
/** Frame zone: ie a zone which can contains text, picture, ...
and have some border */
bool WPS8Parser::readFRAM(WPSEntry const &entry)
{
typedef WPS8ParserInternal::Frame Frame;
libwps::DebugStream f;
if (!entry.hasType(entry.name()))
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM warning: name=%s, type=%s\n",
entry.name().c_str(), entry.type().c_str()));
return false;
}
RVNGInputStreamPtr input = getInput();
long page_offset = entry.begin();
long length = entry.length();
long endPage = entry.end();
if (length < 2)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM warning: length=0x%lx\n", (unsigned long) length));
return false;
}
entry.setParsed();
input->seek(page_offset, librevenge::RVNG_SEEK_SET);
int numFram = libwps::read16(input);
if (numFram < 0 || numFram*2 > length)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM warning: length=0x%lx, num=%d\n", (unsigned long) length, numFram));
return false;
}
f << "N=" << numFram;
ascii().addPos(page_offset);
ascii().addNote(f.str().c_str());
bool parsedAll = true, color = false;
long lastPos=input->tell(); // uneeded initialization but makes clang analyser happier
for (int i = 0; i < numFram; i++)
{
lastPos = input->tell();
if (lastPos+2 > endPage)
{
parsedAll = false;
break;
}
int sz = libwps::read16(input);
if (sz < 2 || lastPos + sz > endPage)
{
parsedAll = false;
break;
}
WPS8Struct::FileData mainData;
std::string error;
bool readOk = readBlockData(input, lastPos + sz, mainData, error);
double dim[3] = {0, 0, 0};
bool setVal[3] = { false, false, false};
double bDim[4] = {0, 0, 0, 0};
bool bset = false, bsetVal[4] = { false, false, false, false};
libwps::DebugStream f2;
Frame frame;
int lastPage = 0;
Vec2f minP, sizeP;
for (auto const &dt : mainData.m_recursData)
{
if (dt.isBad()) continue;
if (m_state->m_frameTypes.find(dt.id())==m_state->m_frameTypes.end())
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM: unexpected id for %d\n", dt.id()));
f2 << "##" << dt << ",";
continue;
}
if (m_state->m_frameTypes.find(dt.id())->second != dt.type())
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM: unexpected type for %d=%d\n", dt.id(), dt.type()));
f2 << "###" << dt << ",";
continue;
}
if (dt.id() >= 4 && dt.id() < 11)
{
switch (dt.id())
{
case 4:
minP.setX(float(dt.m_value)/914400.f);
break;
case 5:
minP.setY(float(dt.m_value)/914400.f);
break;
case 6:
sizeP.setX(float(dt.m_value)/914400.f);
break;
case 7:
sizeP.setY(float(dt.m_value)/914400.f);
break;
default:
dim[dt.id()-8] = float(dt.m_value)/914400.f;
setVal[dt.id()-8] = true;
break;
}
continue;
}
bool ok = true;
switch (dt.id())
{
case 0:
{
int page = (int16_t) dt.m_value;
// try to avoid creating to many page
if (page > lastPage+100)
{
ok = false;
break;
}
frame.m_pos.setPage(page > 0 ? page+1 : page);
if (page > lastPage) lastPage = page;
break;
}
case 1:
switch (dt.m_value)
{
// associated with mailing database with 0x16
case 4:
frame.m_type = Frame::DbField;
break;
// associated with 0x13=22:[0|96], 0x18,1b=0x41,0x2e, rarely with 0x1a
case 6:
frame.m_type = Frame::Header;
break;
// associated with 0x13=22:[0|96],0x18,1b=0x41,0x2e, rarely with 0x1a
case 7:
frame.m_type = Frame::Footer;
break;
// does the following correspond to page, column, char position ?
// associated with 0x11=(m_idOle,0,1), 0x2e=?
case 8:
frame.m_type = Frame::Object;
break;
// associated with 0x18=?, 0x46=? and often with a BDR entry
// CHECKME: seems also associated to a MCLD, how ?
case 9:
frame.m_type = Frame::Text;
break;
// associated with f3 and 0x11=(table_id,0,0) and 0x2a=?
case 12:
frame.m_type = Frame::Table;
break;
default:
f2 << "###type=" << dt.m_value << ",";
break;
}
break;
case 0x2:
if (dt.m_value&1) f2 << "noText[right],";
if (dt.m_value&2) f2 << "noText[left],";
if (dt.m_value&0xFC) f2 << "#f2=" << std::hex << (dt.m_value&0xFC) << ",";
break;
case 0x3: // seem to exist iff type=12
if ((frame.m_type == Frame::Table) != dt.isTrue())
f2 << "isTable?["
<< (dt.isTrue() ? "true" : "false") << "],";
break;
case 0x10: // an entry index always a BDR ?
frame.m_idBorder.setName(dt.m_text);
frame.m_idBorder.setId(int(dt.m_value));
break;
case 0x11: // a list of 3 int32 ? Only the first is signifiant ?
{
if (dt.isRead() || !dt.isArray())
{
ok = false;
break;
}
auto size = int(dt.end()-dt.begin());
if (size < 2 || (size+2)%4 != 0)
{
ok = false;
break;
}
int numElt = (size-2)/4;
long actPos = input->tell();
input->seek(dt.begin()+2, librevenge::RVNG_SEEK_SET);
std::vector<long> values;
for (int elt = 0; elt < numElt; elt++)
{
long val = libwps::read32(input);
values.push_back(val);
}
bool canHaveId = frame.m_type==Frame::Table || frame.m_type==Frame::Object;
if (canHaveId && numElt == 3 && values[1] == 0 && values[2] ==0)
frame.m_idObject = int(values[0]);
else if (canHaveId && numElt == 3 && values[1] == 0 && values[2] == 1)
frame.m_idOle = int(values[0]);
else
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM unknown field 0x11\n"));
f2 << "###f17=(";
for (int j = 0; j < numElt; j++) f2 << values[size_t(j)] << ",";
f2 << "),";
}
input->seek(actPos, librevenge::RVNG_SEEK_SET);
break;
}
case 0x16: // associated with field and database
f2 << "database=" << dt.m_text << ":" << dt.m_value << ",";
break;
// associated with a tex box and field 0x19
case 0x17:
color = dt.isTrue();
break;
case 0x18:
{
if (dt.m_value < 0 || dt.m_value >= m_textParser->getNumTextZones())
{
ok = false;
break;
}
int type = m_textParser->getTextZoneType((int) dt.m_value);
if ((type == 6 && frame.m_type == Frame::Header) ||
(type == 7 && frame.m_type == Frame::Footer) ||
(type == 5))
frame.m_idStrs = (int) dt.m_value;
else
{
WPS_DEBUG_MSG(("WPS8Parser::readFRAM odd id for field 0x18\n"));
ok = false;
}
break;
}
case 0x19: // CHECKME: can we also have a front color and a pattern ?
if (!color)
f2 << "#f23=false,";
frame.m_backgroundColor = dt.getRGBColor();
break;
case 0x1b: // 41
if (frame.m_type!=Frame::Footer || dt.m_value != 0x41)
f2 << "##f" << dt.id() << "=" << (int16_t) dt.m_value << ",";
break;
case 0x26: // rotation in degree from center ( CCW)
f2 << "rot=" << float(dt.m_value)/10. << "deg,";
break;
//
// Table specific field ?
//
case 0x2a: // link to the m_idTable(ids) number for a table
{
int id = (int16_t) dt.m_value;
if (frame.m_type==Frame::Table)
frame.m_idTable = id;
else
f2 << "f" << dt.id() << "=" << id << ",";
break;
}
case 0x2c: // CHEKME
if (frame.m_type==Frame::Table)
frame.m_columns = int(dt.m_value);
else ok = false;
break;
case 0x14: // in relation with 0x30 ? find a small number between 1 and 18
if (frame.m_type==Frame::Table)
f2 << "grpId=" << (int16_t) dt.m_value << ",";
else ok = false;
break;
case 0x30: // seems to link the table together
if (frame.m_type==Frame::Table)
f2 << "tableGrp=" << (int16_t) dt.m_value << ",";
else ok = false;
break;
case 0x2d: // appear 2 time in a table with value 2
f2 << "f" << dt.id() << "=" << (int16_t) dt.m_value << ",";
break;
// UNKNOWN
case 0x1a: // appear 2 time (in a header and in a footer) with value 16
f2 << "f" << dt.id() << "=" << (int)(int8_t) dt.m_value << ",";
break;
case 0x2e: // in a table: objectId+1, in textbox: m_idTable ?, other = i ?
f2 << "id" << dt.id() << "=" << (int16_t)(dt.m_value-1) << ",";
break;
case 0x13:
// table 0x6-??,
// header/footer 0x16-??,
// textbox 0x10(means automatic resize)
// object : no set
f2 << "f" << dt.id();
if (dt.m_value)
f2 << "=" << std::hex << dt.m_value << std::dec;
f2 << ",";
break;
case 0x2f:
if (dt.isTrue())
f2 << "f" << dt.id() << ",";
else
f2 << "f" << dt.id() << "=false,";
break;
case 0x1D:
case 0x1E:
case 0x1F:
case 0x20: // in inches small value 0/0.05/0.1 -> a border size ?
bset = true;
bsetVal[dt.id()-0x1D] = true;
bDim[dt.id()-0x1D] = float(dt.m_value)/914400.f;
break;
default:
ok = false;
}
if (ok) continue;
f2 << "#" << dt << ",";
}
frame.m_pos.setOrigin(minP);
frame.m_pos.setSize(sizeP);
m_state->m_frameList.push_back(frame);
f.str("");
f << entry.name() << "(" << i << "):" << frame << ",";
if (setVal[0] || setVal[1])
{
f << "f8/9=[";
for (int j = 0; j < 2; j++)
{
if (!setVal[j])
{
f << "_,";
continue;
}
double diff = 25/18.-dim[j];
if (diff >= -1e-3 && diff <= 1e-3) f << "*,";
else f << dim[j] << ",";
}
f << "],";
}
if (setVal[2]) f << "border[w]=" << dim[2] << ",";
if (bset)
{
f << "borderMod[w]=["; // L, T, R, B
for (int j = 0; j < 4; j++)
{
if (!bsetVal[j])
{
f << "_,";
continue;
}
f << bDim[j] << ",";
}
f << "],";
}
f << f2.str();
if (!readOk) f << ", ###or" << mainData;
ascii().addPos(lastPos);
ascii().addNote(f.str().c_str());
}
if (parsedAll) return true;
ascii().addPos(lastPos);
f.str("");
f << "###" << entry.name();
ascii().addNote(f.str().c_str());
return false;
}
/** try to read a frame XXX database */
bool WPS8Parser::readFRCD(WPSEntry const &entry)
{
libwps::DebugStream f;
if (!entry.hasType(entry.name()))
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: name=%s, type=%s\n",
entry.name().c_str(), entry.type().c_str()));
return false;
}
RVNGInputStreamPtr input = getInput();
long page_offset = entry.begin();
long length = entry.length();
long endPage = entry.end();
if (length < 4+33+5*8)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: length=0x%lx\n", (unsigned long) length));
return false;
}
entry.setParsed();
input->seek(page_offset, librevenge::RVNG_SEEK_SET);
auto val=int(libwps::readU16(input)); // 0x30
if (val!=0x30) f << "f0=" << val << ",";
auto sSz=int(libwps::readU16(input));
librevenge::RVNGString str;
if (4+2*sSz+33+5*8>length || !m_textParser->readString(input, sSz, str))
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: can not read main string\n"));
return false;
}
f << str.cstr() << ",";
for (int i=0; i<16; ++i) // f1=10,f2=22,f4=4,f7=256,f11=-1,f12=-256,f13=255,f14=-1
{
val=int(libwps::read16(input));
if (val) f << "f" << i+1 << "=" << val << ",";
}
val=int(libwps::read8(input));
if (val) f << "g0=" << val << ",";
ascii().addPos(page_offset);
ascii().addNote(f.str().c_str());
for (int i=0; i<5; ++i)
{
long pos=input->tell();
f.str("");
f << "FRCD-A" << i << ":";
if (pos+8>endPage)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the zone seems too short\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
for (int s=0; s<2; ++s)
{
if (pos+4>endPage || libwps::read8(input)!=-1)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the zone seems bad\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
val=int(libwps::read8(input));
if (val==-1)
{
val=int(libwps::read16(input));
if (val!=0x204)
f << "f" << s << "=" << val << ",";
}
else if (val==-2)
{
val=int(libwps::read8(input));
if (val!=-1) f << "g" << s << "=" << val << ",";
sSz=int(libwps::readU8(input));
str.clear();
if (pos+2*sSz>endPage || (sSz && !m_textParser->readString(input, 2*sSz, str)))
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the zone seems too short\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
if (!str.empty())
f << str.cstr() << ",";
}
else
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: find unexpected value\n"));
f << "###val=" << val << ",";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
}
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
}
long pos=input->tell();
f.str("");
f << "FRCD-B:";
if (pos+4>endPage)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: can not read the second zone\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
auto N=int(libwps::readU16(input));
if (pos+4+10*N>endPage)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: can not read the second zone\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
val=int(libwps::read16(input)); // 0
if (val) f << "f0=" << val << ",";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
for (int n=0; n<N; ++n)
{
pos=input->tell();
f.str("");
f << "FRCD-B" << n << ":";
if (pos+10>endPage)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the zone seems too short\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
val=int(libwps::read16(input)); // 0
if (val) f << "f0=" << val << ",";
for (int s=0; s<2; ++s)
{
if (pos+4>endPage || libwps::read8(input)!=-1)
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the second zone seems bad\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
val=int(libwps::read8(input));
if (val==-2)
{
val=int(libwps::read8(input));
if (val!=-1) f << "g" << s << "=" << val << ",";
sSz=int(libwps::readU8(input));
str.clear();
if (pos+2*sSz>endPage || (sSz && !m_textParser->readString(input, 2*sSz, str)))
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: the second zone seems too short\n"));
f << "###";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
if (!str.empty())
f << str.cstr() << ",";
}
else
{
WPS_DEBUG_MSG(("WPS8Parser::readFRCD warning: find unexpected value\n"));
f << "###val=" << val << ",";
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
return true;
}
}
ascii().addPos(pos);
ascii().addNote(f.str().c_str());
}
return true;
}
// reads SYID zone: an unknown zone which seems to have the
// same number of entries than the text zone
// also related to the MCLD ?
bool WPS8Parser::readSYID(WPSEntry const &entry, std::vector<int> &listId)
{
RVNGInputStreamPtr input = getInput();
listId.resize(0);
libwps::DebugStream f;
if (!entry.hasType(entry.name()))
{
WPS_DEBUG_MSG(("WPS8Parser::readSYID: warning: SYID name=%s, type=%s\n",
entry.name().c_str(), entry.type().c_str()));
return false;
}
long page_offset = entry.begin();
long length = entry.length();
if (length < 4)
{
WPS_DEBUG_MSG(("WPS8Parser::readSYID: warning: SYID length=0x%lx\n", (unsigned long) length));
return false;
}
input->seek(page_offset, librevenge::RVNG_SEEK_SET);
int unk = libwps::read32(input);
int numID = libwps::read32(input);
if (numID < 0 || 4*(long(numID)+2) != length)
{
WPS_DEBUG_MSG(("WPS8Parser::readSYID: invalid length=%ld, num=%d\n", length, numID));
return false;
}
f << "N=" << numID << ", unkn=" << unk << ", (";
for (int i = 0; i < numID; i++)
{
int val = libwps::read32(input);
listId.push_back(val);
f << val << ",";
}
f << ")";
ascii().addPos(page_offset);
ascii().addNote(f.str().c_str());
entry.setParsed();
return true;
}
// reads the WNPR zone : a zone which seems to contain the printer preferences
// Warning: read data are not used
bool WPS8Parser::readWNPR(WPSEntry const &entry)
{
if (!entry.hasType("WNPR"))
{
WPS_DEBUG_MSG(("WPS8Parser::readWNPR: warning: WNPR name=%s, type=%s\n",
entry.name().c_str(), entry.type().c_str()));
return false;
}
RVNGInputStreamPtr input = getInput();
long page_offset = entry.begin();
long length = entry.length();
long endPage = entry.end();
if (length < 40)
{
WPS_DEBUG_MSG(("WPS8Parser::readWNPR: warning: WNPR length=0x%lx\n", (unsigned long) length));
return false;
}
entry.setParsed();
input->seek(page_offset, librevenge::RVNG_SEEK_SET);
libwps::DebugStream f;
f << std::hex << libwps::readU32(input) << std::dec << ",";
long dim[4];
for (long &i : dim)
{
i = libwps::read32(input);
if (i <= 0) return false;
}
if (dim[2] && dim[3])
{
f << "width=" << float(dim[0])/float(dim[2]) << ",";
f << "height=" << float(dim[1])/float(dim[3]) << ",";
}
else
{
f << "###width=" << dim[0] << ":" << dim[3] << ",";
f << "###height=" << dim[1] << ":" << dim[3] << ",";
}
f << "printmargins?=(";
for (int i = 0; i < 4; i++)
{
auto val = (long) libwps::readU32(input);
long sz = dim[2+(i%2)];
if (sz)
f << float(val)/float(sz) << ",";
else
f << "###" << val << ",";
}
f << "),";
for (int i = 0; i < 2; i++)
f << std::hex << libwps::readU16(input) << std::dec <<",";
ascii().addPos(page_offset);
ascii().addNote(f.str().c_str());
long actPos = input->tell();
if (actPos + 32*2+38+22 > endPage)
{
WPS_DEBUG_MSG(("WPS8Parser::readWNPR: length=0x%lx seems too short\n", (unsigned long) length));
return false;
}
// DEVMODEA
std::string st;
for (int i = 0; i < 32; i++)
{
unsigned char c = libwps::readU8(input);
if (!c) continue; // normally end by c, check me
st+= char(c);
}
f.str("");
f << "PRNT(DevMode):";
f << "devName='" << st << "'," << std::hex;
f << "specVersion=" << libwps::readU16(input) << ",";
f << "driverVersion=" << libwps::readU16(input) << ",";
int dmSize = libwps::readU16(input);
if (actPos + dmSize > endPage || dmSize < 124) return false;
f << "dmSize=" << dmSize << ",";
f << "driverExtras=" << libwps::readU16(input) << ",";
f << "dmFields=" << libwps::readU32(input) << ",";
f << "orientation=" << libwps::readU16(input) << ","; // 1: portrait, 2: landscape
f << "paperSize=" << libwps::readU16(input) << ","; // 1: letter, 2:letter small, ..
long dim2[3];
for (long &i : dim2)
i = libwps::read16(input);
f << std::dec;
if (dim2[2] > 0)
{
f << "paperLength=" << std::dec << float(dim2[0])/float(dim2[2]) << ",";
f << "paperWidth=" << float(dim2[1])/float(dim2[2]) << ",";
}
else
{
f << "paperLength=" << dim2[0] << ",";
f << "paperWidth=" << dim2[1] << ",";
f << "dmScale=" << dim2[2] << ",";
}
f << "dmCopies=" << libwps::readU16(input) << ",";
f << "dmDefaultSource=" << libwps::readU16(input) << ",";
f << "dmPrintQuality=" << libwps::read16(input) << ",";
f << std::hex;
f << "dmColor=" << libwps::readU16(input) << ",";
f << "dmDuplex=" << libwps::readU16(input) << ",";
f << "dmYResolution=" << libwps::readU16(input) << ",";
f << "dmTTOption=" << libwps::readU16(input) << ",";
f << "dmCollate=" << libwps::readU16(input) << ",";
st="";
for (int i = 0; i < 32; i++)
{
unsigned char c = libwps::readU8(input);
if (!c) continue; // normally end by c, check me
st+= char(c);
}
f << "formName='" << st << "',";
f << "dmLogPixels=" << libwps::readU16(input) << ",";
f << "dmBitsPerPel=" << libwps::readU32(input) << ",";
f << "dmPelsWidth=" << libwps::readU32(input) << ",";
f << "dmPelsHeight=" << libwps::readU32(input) << ",";
f << "dmDisplayFlags=" << libwps::readU32(input) << ",";
f << "dmDisplayFrequency=" << libwps::readU32(input) << ",";
ascii().addPos(actPos);
ascii().addNote(f.str().c_str());
long devEndPos = actPos+dmSize;
actPos = input->tell();
if (actPos != devEndPos)
{
ascii().addPos(actPos);
ascii().addNote("PRNT(DevMode-End)");
}
ascii().addPos(devEndPos);
ascii().addNote("PRNT(Unknown)");
return true;
}
////////////////////////////////////////////////////////////////
//
// ? Syllabes seperator to read the text
// it is really related to spell check or to speech
// Can surely be safety ignored in normal version.
//
////////////////////////////////////////////////////////////////
#ifndef DEBUG
bool WPS8Parser::readSPELLING(RVNGInputStreamPtr, std::string const &)
{
return true;
}
#else
bool WPS8Parser::readSPELLING(RVNGInputStreamPtr input, std::string const &oleName)
{
// SPELLING
input->seek(0, librevenge::RVNG_SEEK_SET);
auto vers = (int) libwps::read32(input); // always 6 ?
if (vers < 0 || input->isEnd()) return false;
std::string fName = libwps::Debug::flattenFileName(oleName);
libwps::DebugFile asciiFile(input);
asciiFile.open(fName);
int actId = 0;
std::map<uint32_t, int> listIds;
libwps::DebugStream f;
int num = 0;
while (!input->isEnd())
{
long pos = input->tell();
auto numVal = (int) libwps::read32(input);
if (numVal < 0 || input->seek(8*numVal, librevenge::RVNG_SEEK_CUR) || input->tell() != pos+4+8*numVal)
{
input->seek(pos, librevenge::RVNG_SEEK_SET);
break;
}
f.str("");
f << "SPELLING" << num++ << ": "; // linked to STRSj by num = numSTRS-j?
input->seek(pos+4, librevenge::RVNG_SEEK_SET);
for (int j = 0; j < numVal; j++)
{
uint32_t ptr = libwps::readU32(input);
int id;
if (listIds.find(ptr) == listIds.end())
{
id = actId++;
listIds[ptr] = id;
}
else
id = listIds[ptr];
auto sPos = (long) libwps::read32(input);
f << std::hex << 2*sPos << ":SP" << std::dec << id << ",";
}
asciiFile.addPos(pos);
asciiFile.addNote(f.str().c_str());
}
f.str("");
f << "SPELLING:";
if (vers != 6) f << "version = " << vers << ", ";
f << "list=[";
for (auto it : listIds)
{
uint32_t val = it.first;
f << "SP" << std::dec << it.second << "(" << (val&0xFF) << ":"
<< std::hex << (val>>8) << std::dec << "),";
// (val&0xFF) :121-123, 129, 133, 139, 145
// (val>>8) : 0, e6, d9a3b, 3b75f4, 474555, 548bbb, 609daa,748cce, ba32b9, c068cd, d39bea
// seems constant in a SPELLING Ole
}
f << "]";
asciiFile.addPos(0);
asciiFile.addNote(f.str().c_str());
if (!input->isEnd())
{
asciiFile.addPos(input->tell());
asciiFile.addNote("###SPELLING");
}
return true;
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */