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
|
/* AbiWord
* Copyright (C) 2001 Sean Young <sean@mess.org>
* Copyright (C) 2001 Hubert Figuiere
* Copyright (C) 2001 Dom Lachowicz
* Copyright (C) 2010-2011 Ingo Brueckl
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* Documentation at http://www.msxnet.org/word2rtf/formats/write
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gsf/gsf-input.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _MSC_VER
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#else
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif
#include "ie_imp_MSWrite.h"
#include "ap_Args.h"
#include "fg_Graphic.h"
#include "ie_impGraphic.h"
#include "ut_assert.h"
#include "ut_debugmsg.h"
#include "ut_locale.h"
#include "ut_types.h"
#include "xap_Module.h"
#ifdef ABI_PLUGIN_BUILTIN
#define abi_plugin_register abipgn_mswrite_register
#define abi_plugin_unregister abipgn_mswrite_unregister
#define abi_plugin_supports_version abipgn_mswrite_supports_version
#define ABI_BUILTIN_FAR_CALL extern "C"
#else
#define ABI_BUILTIN_FAR_CALL ABI_FAR_CALL
ABI_PLUGIN_DECLARE("MSWrite")
#endif
// we use a reference-counted sniffer
static IE_Imp_MSWrite_Sniffer *m_sniffer = 0;
/**********************************************************************
* Plugin Registration *
**********************************************************************/
ABI_BUILTIN_FAR_CALL
int abi_plugin_register (XAP_ModuleInfo *mi)
{
if (!m_sniffer) m_sniffer = new IE_Imp_MSWrite_Sniffer();
mi->name = "MSWrite Importer";
mi->desc = "Import Microsoft Write Documents";
mi->version = ABI_VERSION_STRING;
mi->author = "Sean Young, Ingo Brückl";
mi->usage = "No Usage";
IE_Imp::registerImporter(m_sniffer);
return 1;
}
ABI_BUILTIN_FAR_CALL
int abi_plugin_unregister (XAP_ModuleInfo *mi)
{
mi->name = 0;
mi->desc = 0;
mi->version = 0;
mi->author = 0;
mi->usage = 0;
UT_ASSERT(m_sniffer);
IE_Imp::unregisterImporter(m_sniffer);
DELETEP(m_sniffer);
return 1;
}
ABI_BUILTIN_FAR_CALL
int abi_plugin_supports_version (UT_uint32 major, UT_uint32 minor, UT_uint32 release)
{
UT_UNUSED(major);
UT_UNUSED(minor);
UT_UNUSED(release);
return 1;
}
/**********************************************************************
* File Sniffer *
**********************************************************************/
// supported suffix
static const IE_SuffixConfidence IE_Imp_MSWrite_Sniffer__SuffixConfidence[] =
{
{"wri", UT_CONFIDENCE_PERFECT},
{"", UT_CONFIDENCE_ZILCH}
};
IE_Imp_MSWrite_Sniffer::IE_Imp_MSWrite_Sniffer ()
: IE_ImpSniffer("AbiMSWrite::MSWrite")
{
}
bool IE_Imp_MSWrite_Sniffer::getDlgLabels (const char **szDesc,
const char **szSuffixList,
IEFileType *ft)
{
*szDesc = "Microsoft Write (.wri)";
*szSuffixList = "*.wri";
*ft = getFileType();
return true;
}
const IE_SuffixConfidence *IE_Imp_MSWrite_Sniffer::getSuffixConfidence ()
{
return IE_Imp_MSWrite_Sniffer__SuffixConfidence;
}
UT_Confidence_t IE_Imp_MSWrite_Sniffer::recognizeContents (const char *szBuf,
UT_uint32 iNumbytes)
{
if (iNumbytes > 8)
{
if ((szBuf[0] == static_cast<char>(0x31) || szBuf[0] == static_cast<char>(0x32)) &&
szBuf[1] == static_cast<char>(0xbe) &&
szBuf[2] == static_cast<char>(0) && szBuf[3] == static_cast<char>(0) &&
szBuf[4] == static_cast<char>(0) && szBuf[5] == static_cast<char>(0xab))
return UT_CONFIDENCE_PERFECT;
}
return UT_CONFIDENCE_ZILCH;
}
UT_Error IE_Imp_MSWrite_Sniffer::constructImporter (PD_Document *pDocument,
IE_Imp **ppie)
{
IE_Imp_MSWrite *p = new IE_Imp_MSWrite(pDocument);
*ppie = p;
return UT_OK;
}
/**********************************************************************
* MSWrite File Structures *
**********************************************************************/
static const wri_struct WRI_FILE_HEADER[] =
{
/* value, data, size, type, name */ /* word no. */
{0, NULL, 2, CT_VALUE, "wIdent"}, // 0
{0, NULL, 2, CT_VALUE, "dty"}, // 1
{0, NULL, 2, CT_VALUE, "wTool"}, // 2
{0, NULL, 2, CT_VALUE, "reserved1"}, // 3
{0, NULL, 2, CT_VALUE, "reserved2"}, // 4
{0, NULL, 2, CT_VALUE, "reserved3"}, // 5
{0, NULL, 2, CT_VALUE, "reserved4"}, // 6
{0, NULL, 4, CT_VALUE, "fcMac"}, // 7-8
{0, NULL, 2, CT_VALUE, "pnPara"}, // 9
{0, NULL, 2, CT_VALUE, "pnFntb"}, // 10
{0, NULL, 2, CT_VALUE, "pnSep"}, // 11
{0, NULL, 2, CT_VALUE, "pnSetb"}, // 12
{0, NULL, 2, CT_VALUE, "pnPgtb"}, // 13
{0, NULL, 2, CT_VALUE, "pnFfntb"}, // 14
{0, NULL, 64, CT_IGNORE, "szSsht"}, // 15-47
{0, NULL, 2, CT_VALUE, "pnMac"}, // 48
{0, NULL, 0, CT_IGNORE, NULL} // EOF
};
static const wri_struct WRI_PICTURE_HEADER[] =
{
/* value, data, size, type, name */ /* word no. */
// METAFILEPICT structure
{0, NULL, 2, CT_VALUE, "mm"}, // 0
{0, NULL, 2, CT_VALUE, "xExt"}, // 1
{0, NULL, 2, CT_VALUE, "yExt"}, // 2
{0, NULL, 2, CT_IGNORE, "hMF"}, // 3
// end of METAFILEPICT structure
{0, NULL, 2, CT_VALUE, "dxaOffset"}, // 4
{0, NULL, 2, CT_VALUE, "dxaSize"}, // 5
{0, NULL, 2, CT_VALUE, "dyaSize"}, // 6
{0, NULL, 2, CT_VALUE, "cbOldSize"}, // 7
// BITMAP structure
{0, NULL, 2, CT_VALUE, "bmType"}, // 8
{0, NULL, 2, CT_VALUE, "bmWidth"}, // 9
{0, NULL, 2, CT_VALUE, "bmHeight"}, // 10
{0, NULL, 2, CT_VALUE, "bmWidthBytes"}, // 11
{0, NULL, 1, CT_VALUE, "bmPlanes"}, // 12
{0, NULL, 1, CT_VALUE, "bmBitsPixel"}, // 12.5
{0, NULL, 4, CT_VALUE, "bmBits"}, // 13-14
// end of BITMAP structure
{0, NULL, 2, CT_VALUE, "cbHeader"}, // 15
{0, NULL, 4, CT_VALUE, "cbSize"}, // 16-17
{0, NULL, 2, CT_VALUE, "mx"}, // 18
{0, NULL, 2, CT_VALUE, "my"}, // 19
{0, NULL, 0, CT_IGNORE, NULL} // EOF
};
static const wri_struct WRI_OLE_HEADER[] =
{
/* value, data, size, type, name */ /* word no. */
{0, NULL, 2, CT_VALUE, "mm"}, // 0
{0, NULL, 4, CT_IGNORE, "not_used"}, // 1-2
{0, NULL, 2, CT_VALUE, "objectType"}, // 3
{0, NULL, 2, CT_VALUE, "dxaOffset"}, // 4
{0, NULL, 2, CT_VALUE, "dxaSize"}, // 5
{0, NULL, 2, CT_VALUE, "dyaSize"}, // 6
{0, NULL, 2, CT_IGNORE, "not_used2"}, // 7
{0, NULL, 4, CT_VALUE, "dwDataSize"}, // 8-9
{0, NULL, 4, CT_IGNORE, "not_used3"}, // 10-11
{0, NULL, 4, CT_VALUE, "dwObjNum"}, // 12-13
{0, NULL, 2, CT_VALUE, "not_used4"}, // 14
{0, NULL, 2, CT_VALUE, "cbHeader"}, // 15
{0, NULL, 4, CT_IGNORE, "not_used5"}, // 16-17
{0, NULL, 2, CT_VALUE, "mx"}, // 18
{0, NULL, 2, CT_VALUE, "my"}, // 19
{0, NULL, 0, CT_IGNORE, NULL} // EOF
};
#define PIC_OR_OLE_HEADER_SIZE 40 // common size of both header types
// OLE1.0 structure offsets
#define OLE_Version 0
#define OLE_FormatID 4
#define OLE_ClassNameLength 8
#define OLE_ClassNameString 12 // variable length
// offsets relative to empty ClassNameString
#define OLE_TopicNameLength 12
#define OLE_TopicNameString 16
// offsets relative to empty ClassNameString and TopicNameString
#define OLE_ItemNameLength 16
#define OLE_ItemNameString 20
// offsets relative to empty ClassNameString and TopicNameString and ItemNameString
#define OLE_ObjDataSize 20
#define OLE_Object 24
#define OLE_MF_Reserved 24
#define OLE_MF_Object 32
// Bitmap16 structure offsets
#define BM16_Type 0
#define BM16_Width 2
#define BM16_Height 4
#define BM16_WidthBytes 6
#define BM16_Planes 8
#define BM16_BitsPixel 9
#define BM16_Bits 10
/**********************************************************************
* MSWrite Importer *
**********************************************************************/
IE_Imp_MSWrite::IE_Imp_MSWrite (PD_Document *pDocument)
: IE_Imp(pDocument),
mDefaultCodepage("CP1252"),
hasHeader(false),
hasFooter(false),
wri_fonts(0),
wri_fonts_count(0),
pic_nr(0),
lf(false)
{
setProps(AP_Args::m_impProps);
const std::string &propCP = getProperty("mswrite-codepage");
if (!propCP.empty()) mDefaultCodepage = propCP;
UT_DEBUGMSG(("Codepage: %s\n", mDefaultCodepage.c_str()));
wri_file_header = static_cast<wri_struct*>(malloc(sizeof(WRI_FILE_HEADER)));
memcpy(wri_file_header, WRI_FILE_HEADER, sizeof(WRI_FILE_HEADER));
wri_picture_header = static_cast<wri_struct*>(malloc(sizeof(WRI_PICTURE_HEADER)));
memcpy(wri_picture_header, WRI_PICTURE_HEADER, sizeof(WRI_PICTURE_HEADER));
wri_ole_header = static_cast<wri_struct*>(malloc(sizeof(WRI_OLE_HEADER)));
memcpy(wri_ole_header, WRI_OLE_HEADER, sizeof(WRI_OLE_HEADER));
}
IE_Imp_MSWrite::~IE_Imp_MSWrite()
{
free_wri_struct(wri_file_header);
free(wri_file_header);
free(wri_picture_header);
free(wri_ole_header);
}
/**********************************************************************
* MSWrite Importer (file loading) *
**********************************************************************/
UT_Error IE_Imp_MSWrite::_loadFile (GsfInput *input)
{
UT_Error result;
mFile = (GsfInput *) g_object_ref(G_OBJECT(input));
if (!mFile) return UT_ERROR;
result = parse_file();
g_object_unref(G_OBJECT(mFile));
return result;
}
UT_Error IE_Imp_MSWrite::parse_file ()
{
int id, size;
UT_Byte *thetext;
if (!read_wri_struct(wri_file_header, mFile)) return UT_ERROR;
UT_DEBUGMSG(("File Header:\n"));
DEBUG_WRI_STRUCT(wri_file_header);
id = wri_struct_value(wri_file_header, "wIdent");
if (id == 0137062)
{
// okay, but expect OLE objects
}
else if (id != 0137061)
{
UT_WARNINGMSG(("parse_file: Not a write file!\n"));
return UT_ERROR;
}
if (wri_struct_value(wri_file_header, "wTool") != 0125400)
{
UT_WARNINGMSG(("parse_file: Not a write file!\n"));
return UT_ERROR;
}
size = wri_struct_value(wri_file_header, "fcMac") - 0x80;
thetext = static_cast<UT_Byte *>(malloc(size));
if (!thetext)
{
UT_WARNINGMSG(("parse_file: Out of memory!\n"));
return UT_ERROR;
}
if (gsf_input_seek(mFile, 0x80, G_SEEK_SET))
{
UT_WARNINGMSG(("parse_file: Can't seek data!\n"));
return UT_ERROR;
}
gsf_input_read(mFile, size, thetext);
if (!read_ffntb())
{
free(thetext);
return UT_ERROR;
}
mData.truncate(0);
mData.append(thetext, size);
free(thetext);
read_sep();
read_pap(All);
UT_DEBUGMSG(("Header: %d, on first page: %d\n", hasHeader, page1Header));
if (hasHeader)
{
_append_hdrftr(header);
read_pap(Header);
if (!page1Header) _append_hdrftr(headerfirst); // an empty one
}
UT_DEBUGMSG(("Footer: %d, on first page: %d\n", hasFooter, page1Footer));
if (hasFooter)
{
_append_hdrftr(footer);
read_pap(Footer);
if (!page1Footer) _append_hdrftr(footerfirst); // an empty one
}
free_ffntb();
return UT_OK;
}
/**********************************************************************
* MSWrite Importer (font handling) *
**********************************************************************/
bool IE_Imp_MSWrite::read_ffntb ()
{
int pnFfntb, pnMac, fonts_count = 0, cbFfn, fflen;
unsigned char buf[2], ffid;
wri_font *fonts;
char *ffn;
UT_DEBUGMSG(("Fonts:\n"));
pnFfntb = wri_struct_value(wri_file_header, "pnFfntb");
pnMac = wri_struct_value(wri_file_header, "pnMac");
// if pnFfntb is the same as pnMac, there are no fonts
if (pnFfntb == pnMac)
{
UT_DEBUGMSG((" (none)\n"));
return true;
}
if (gsf_input_seek(mFile, pnFfntb++ * 0x80, G_SEEK_SET))
{
UT_WARNINGMSG(("read_ffntb: Can't seek FFNTB!\n"));
return false;
}
// the first two bytes are the number of fonts
if (!gsf_input_read(mFile, 2, buf))
{
UT_WARNINGMSG(("read_ffntb: Can't read FFNTB!\n"));
return false;
}
wri_fonts_count = READ_WORD(buf);
UT_DEBUGMSG((" reported: %d\n", wri_fonts_count));
while (true)
{
if (!gsf_input_read(mFile, 2, buf))
{
UT_WARNINGMSG(("read_ffntb: Can't read cbFfn!\n"));
wri_fonts_count = fonts_count;
free_ffntb();
return false;
}
cbFfn = READ_WORD(buf);
if (cbFfn == 0) break;
if (cbFfn == 0xffff)
{
if (gsf_input_seek(mFile, pnFfntb++ * 0x80, G_SEEK_SET))
{
UT_WARNINGMSG(("read_ffntb: Can't seek next FFNTB!\n"));
wri_fonts_count = fonts_count;
free_ffntb();
return false;
}
continue;
}
// add one more font
fonts = (wri_font *) realloc(wri_fonts, (fonts_count + 1) * sizeof(wri_font));
if (!fonts)
{
UT_WARNINGMSG(("read_ffntb: Out of memory!\n"));
wri_fonts_count = fonts_count;
free_ffntb();
return false;
}
wri_fonts = fonts;
// This is the font family identifier. It can either be FF_DONTCARE,
// FF_ROMAN, FF_SWISS, FF_MODERN, FF_SCRIPT or FF_DECORATIVE. These
// are defined in <windows.h>, but I don't know what to do with them.
if (!gsf_input_read(mFile, 1, &ffid))
{
UT_WARNINGMSG(("read_ffntb: Can't read ffid!\n"));
wri_fonts_count = fonts_count;
free_ffntb();
return false;
}
wri_fonts[fonts_count].ffid = ffid;
cbFfn--; // we've already read ffid
ffn = static_cast<char *>(malloc(cbFfn));
if (!ffn)
{
UT_WARNINGMSG(("read_ffntb: Out of memory!\n"));
wri_fonts_count = fonts_count;
free_ffntb();
return false;
}
if (!gsf_input_read(mFile, cbFfn, (guint8 *) ffn))
{
UT_WARNINGMSG(("read_ffntb: Can't read szFfn!\n"));
wri_fonts_count = fonts_count + 1;
free_ffntb();
return false;
}
wri_fonts[fonts_count].codepage = get_codepage(ffn, &fflen);
ffn[fflen] = 0;
wri_fonts[fonts_count].name = ffn;
UT_DEBUGMSG((" %2d: %s (%s)\n", fonts_count, wri_fonts[fonts_count].name, wri_fonts[fonts_count].codepage));
fonts_count++;
}
if (fonts_count != wri_fonts_count)
{
wri_fonts_count = fonts_count;
UT_WARNINGMSG(("read_ffntb: Wrong number of fonts.\n"));
}
return true;
}
void IE_Imp_MSWrite::free_ffntb ()
{
for (int i = 0; i < wri_fonts_count; i++)
{
free((void *) wri_fonts[i].name);
wri_fonts[i].name = NULL;
}
free(wri_fonts);
wri_fonts = NULL;
}
/**********************************************************************
* MSWrite Importer (section property) *
**********************************************************************/
bool IE_Imp_MSWrite::read_sep ()
{
int pnSep, pnSetb;
int yaMac, xaMac, yaTop, dyaText, dxaText, rStartPage, yaHeader, yaFooter;
int cch, yaBot;
unsigned char sep[0x80];
UT_DEBUGMSG(("SEP:\n"));
pnSep = wri_struct_value(wri_file_header, "pnSep");
pnSetb = wri_struct_value(wri_file_header, "pnSetb");
// default SEP values
yaMac = 15840;
xaMac = 12240;
yaTop = 1440;
dyaText = 12960;
dxaText = 8640;
xaLeft = 1800;
rStartPage = 0xFFFF;
yaHeader = 1080;
yaFooter = 15760;
// if the section properties differ from the defaults, get them
if (pnSep != pnSetb)
{
gsf_input_seek(mFile, pnSep * 0x80, G_SEEK_SET);
gsf_input_read(mFile, 0x80, sep);
cch = *sep;
UT_DEBUGMSG((" cch = %d\n", cch));
if (cch >= 4) yaMac = READ_WORD(sep + 3);
if (cch >= 6) xaMac = READ_WORD(sep + 5);
if (cch >= 8) rStartPage = READ_WORD(sep + 7);
if (cch >= 10) yaTop = READ_WORD(sep + 9);
if (cch >= 12) dyaText = READ_WORD(sep + 11);
if (cch >= 14) xaLeft = READ_WORD(sep + 13);
if (cch >= 16) dxaText = READ_WORD(sep + 15);
if (cch >= 20) yaHeader = READ_WORD(sep + 19);
if (cch >= 22) yaFooter = READ_WORD(sep + 21);
}
if (rStartPage & 0x8000) rStartPage = -0x10000 + rStartPage;
UT_DEBUGMSG((" yaMac = %d\n", yaMac));
UT_DEBUGMSG((" xaMac = %d\n", xaMac));
UT_DEBUGMSG((" rStartPage = %d\n", rStartPage));
UT_DEBUGMSG((" yaTop = %d\n", yaTop));
UT_DEBUGMSG((" dyaText = %d\n", dyaText));
UT_DEBUGMSG((" xaLeft = %d\n", xaLeft));
UT_DEBUGMSG((" dxaText = %d\n", dxaText));
UT_DEBUGMSG((" yaHeader = %d\n", yaHeader));
UT_DEBUGMSG((" yaFooter = %d\n", yaFooter));
yaBot = yaMac - yaTop - dyaText;
xaRight = xaMac - xaLeft - dxaText;
UT_String properties;
UT_LocaleTransactor lt(LC_NUMERIC, "C");
UT_String_sprintf(properties, "page-margin-header:%.4fin; "
"page-margin-right:%.4fin; "
"page-margin-left:%.4fin; "
"page-margin-top:%.4fin; "
"page-margin-bottom:%.4fin; "
"page-margin-footer:%.4fin",
static_cast<float>(yaHeader) / 1440.0,
static_cast<float>(xaRight) / 1440.0,
static_cast<float>(xaLeft) / 1440.0,
static_cast<float>(yaTop) / 1440.0,
static_cast<float>(yaBot) / 1440.0,
static_cast<float>(yaMac - yaFooter) / 1440.0);
if (rStartPage >= 0)
{
UT_String tmp;
UT_String_sprintf(tmp, "; section-restart:1"
"; section-restart-value:%d", rStartPage);
properties += tmp;
}
const gchar *attributes[11];
attributes[0] = PT_PROPS_ATTRIBUTE_NAME;
attributes[1] = properties.c_str();
attributes[2] = PT_HEADERFIRST_ATTRIBUTE_NAME;
attributes[3] = "0";
attributes[4] = PT_HEADER_ATTRIBUTE_NAME;
attributes[5] = "1";
attributes[6] = PT_FOOTERFIRST_ATTRIBUTE_NAME;
attributes[7] = "2";
attributes[8] = PT_FOOTER_ATTRIBUTE_NAME;
attributes[9] = "3";
attributes[10] = NULL;
appendStrux(PTX_Section, attributes);
return true;
}
/**********************************************************************
* MSWrite Importer (paragraph property) *
**********************************************************************/
bool IE_Imp_MSWrite::read_pap (pap_t process)
{
static const char *text_align[] = {"left", "center", "right", "justify"};
int fcMac, pnPara, fcFirst, cfod, fc, fcLim;
unsigned char page[0x80];
UT_String properties, tmp, lastprops;
if (process == All) { UT_DEBUGMSG(("PAP:\n")); }
fcMac = wri_struct_value(wri_file_header, "fcMac");
pnPara = wri_struct_value(wri_file_header, "pnPara");
fcFirst = 0x80;
while (true)
{
gsf_input_seek(mFile, pnPara++ * 0x80, G_SEEK_SET);
gsf_input_read(mFile, 0x80, page);
fc = READ_DWORD(page);
cfod = page[0x7f];
if (process == All)
{
UT_DEBUGMSG((" fcFirst = %d\n", fc));
UT_DEBUGMSG((" cfod = %d\n", cfod));
}
if (fc != fcFirst) UT_WARNINGMSG(("read_pap: fcFirst wrong.\n"));
// read all FODs (format descriptors)
for (int fod = 0; fod < cfod; fod++)
{
int bfprop, cch;
int jc, dxaRight, dxaLeft, dxaLeft1, dyaLine, fGraphics;
int rhcPage, rHeaderFooter, rhcFirst;
int tabs, dxaTab[14], jcTab[14];
if (process == All) { UT_DEBUGMSG((" PAP-FOD #%02d:\n", fod + 1)); }
// read a FOD (format descriptor)
fcLim = READ_DWORD(page + 4 + fod * 6);
bfprop = READ_WORD(page + 8 + fod * 6);
if (process == All)
{
UT_DEBUGMSG((" fcLim = %d\n", fcLim));
UT_DEBUGMSG((bfprop == 0xffff ? " bfprop = 0x%04X\n" : " bfprop = %d\n", bfprop));
}
// default PAP values
jc = 0;
dxaRight = dxaLeft = dxaLeft1 = 0;
dyaLine = 240;
rhcPage = rHeaderFooter = rhcFirst = 0;
fGraphics = 0;
tabs = 0;
// if the PAP FPROPs (formatting properties) differ from the defaults, get them
if (bfprop != 0xffff && bfprop + (cch = page[bfprop + 4]) < 0x80)
{
if (process == All) { UT_DEBUGMSG((" cch = %d\n", cch)); }
if (cch >= 2) jc = page[bfprop + 6] & 3;
if (cch >= 6) dxaRight = READ_WORD(page + bfprop + 9);
if (cch >= 8) dxaLeft = READ_WORD(page + bfprop + 11);
if (cch >= 10) dxaLeft1 = READ_WORD(page + bfprop + 13);
if (cch >= 12) dyaLine = READ_WORD(page + bfprop + 15);
if (cch >= 17)
{
rhcPage = page[bfprop + 21] & 1;
rHeaderFooter = page[bfprop + 21] & 6;
rhcFirst = page[bfprop + 21] & 8;
fGraphics = page[bfprop + 21] & 0x10;
}
for (int n = 0; n < 14; n++)
{
if (cch >= 4 * (n + 1) + 26)
{
dxaTab[tabs] = READ_WORD(page + bfprop + n * 4 + 27);
jcTab[tabs] = page[bfprop + n * 4 + 29] & 3;
tabs++;
}
}
if (dxaRight & 0x8000) dxaRight = -0x10000 + dxaRight;
if (dxaLeft & 0x8000) dxaLeft = -0x10000 + dxaLeft;
if (dxaLeft1 & 0x8000) dxaLeft1 = -0x10000 + dxaLeft1;
if (dyaLine < 240) dyaLine = 240;
if (process == All && rHeaderFooter)
{
if (rhcPage)
{
if (!hasFooter)
{
hasFooter = true;
page1Footer = rhcFirst;
}
}
else
{
if (!hasHeader)
{
hasHeader = true;
page1Header = rhcFirst;
}
}
}
}
if ((process == All && !rHeaderFooter) ||
(rHeaderFooter && ((process == Header && !rhcPage) ||
(process == Footer && rhcPage))))
{
UT_DEBUGMSG((" jc = %d\n", jc));
UT_DEBUGMSG((" dxaRight = %d\n", dxaRight));
UT_DEBUGMSG((" dxaLeft = %d\n", dxaLeft));
UT_DEBUGMSG((" dxaLeft1 = %d\n", dxaLeft1));
UT_DEBUGMSG((" dyaLine = %d\n", dyaLine));
UT_DEBUGMSG((" rhcPage = %d\n", rhcPage));
UT_DEBUGMSG((" rHeaderFooter = %d\n", rHeaderFooter));
UT_DEBUGMSG((" rhcFirst = %d\n", rhcFirst));
UT_DEBUGMSG((" fGraphics = %d\n", fGraphics));
UT_LocaleTransactor lt(LC_NUMERIC, "C");
UT_String_sprintf(properties, "text-align:%s; line-height:%.1f",
text_align[jc],
static_cast<float>(dyaLine) / 240.0);
if (tabs)
{
properties += "; tabstops:";
UT_DEBUGMSG((" Tabs:\n"));
for (int n = 0; n < tabs; n++)
{
UT_String_sprintf(tmp, "%.4fin/%c0",
static_cast<float>(dxaTab[n]) / 1440.0,
jcTab[n] ? 'D' : 'L');
properties += tmp;
UT_DEBUGMSG((" #%02d dxa = %d, jcTab = %d\n", n + 1, dxaTab[n], jcTab[n]));
if (n != tabs - 1) properties += ",";
}
}
if (process == Header || process == Footer)
{
// For reasons unknown, the left and right margins from the paper
// are included in the indents of the headers and footers.
dxaLeft -= xaLeft;
dxaRight -= xaRight;
}
if (dxaLeft1)
{
UT_String_sprintf(tmp, "; text-indent:%.4fin",
static_cast<float>(dxaLeft1) / 1440.0);
properties += tmp;
}
if (dxaLeft)
{
UT_String_sprintf(tmp, "; margin-left:%.4fin",
static_cast<float>(dxaLeft) / 1440.0);
properties += tmp;
}
if (dxaRight)
{
UT_String_sprintf(tmp, "; margin-right:%.4fin",
static_cast<float>(dxaRight) / 1440.0);
properties += tmp;
}
// new attributes, only if there was a line feed or FPROPs have changed
if (lf || strcmp(properties.c_str(), lastprops.c_str()) != 0)
{
const gchar *attributes[3];
attributes[0] = PT_PROPS_ATTRIBUTE_NAME;
attributes[1] = properties.c_str();
attributes[2] = NULL;
appendStrux(PTX_Block, attributes);
lastprops = properties;
}
if (fGraphics) read_pic(fcFirst, fcLim - fcFirst);
else read_txt(fcFirst, fcLim - 1);
}
fcFirst = fcLim;
if (fcLim >= fcMac)
{
UT_DEBUGMSG((" PAP-FODs end, fcLim (%d) >= fcMac (%d)\n", fcLim, fcMac));
return true;
}
}
}
}
/**********************************************************************
* MSWrite Importer (text handling) *
**********************************************************************/
bool IE_Imp_MSWrite::read_txt (int from, int to)
{
static const char *currcp;
int fcMac, pnChar, fcFirst, cfod, fc, fcLim;
unsigned char page[0x80];
UT_String properties, tmp;
int dataLen = static_cast<UT_sint32>(mData.getLength());
UT_DEBUGMSG((" TXT:\n"));
UT_DEBUGMSG((" from = %d\n", from));
UT_DEBUGMSG((" to = %d\n", to));
fcMac = wri_struct_value(wri_file_header, "fcMac");
pnChar = (fcMac + 127) / 128;
fcFirst = 0x80;
while (true)
{
gsf_input_seek(mFile, pnChar++ * 0x80, G_SEEK_SET);
gsf_input_read(mFile, 0x80, page);
fc = READ_DWORD(page);
cfod = page[0x7f];
UT_DEBUGMSG((" fcFirst = %d\n", fc));
UT_DEBUGMSG((" cfod = %d\n", cfod));
if (fc != fcFirst) UT_WARNINGMSG(("read_txt: fcFirst wrong.\n"));
// read all FODs (format descriptors)
for (int fod = 0; fod < cfod; fod++)
{
int bfprop, cch, ftc, hps, fBold, fItalic, fUline, hpsPos;
UT_DEBUGMSG((" CHP-FOD #%02d:\n", fod + 1));
// read a FOD (format descriptor)
fcLim = READ_DWORD(page + 4 + fod * 6);
bfprop = READ_WORD(page + 8 + fod * 6);
UT_DEBUGMSG((" fcLim = %d\n", fcLim));
UT_DEBUGMSG((bfprop == 0xffff ? " bfprop = 0x%04X\n" : " bfprop = %d\n", bfprop));
// default CHP values
ftc = 0;
hps = 24;
fBold = fItalic = fUline = hpsPos = 0;
// if the CHP FPROPs (formatting properties) differ from the defaults, get them
if (bfprop != 0xffff && bfprop + (cch = page[bfprop + 4]) < 0x80)
{
UT_DEBUGMSG((" cch = %d\n", cch));
if (cch >= 2) ftc = page[bfprop + 6] >> 2;
if (cch >= 5) ftc |= (page[bfprop + 9] & 3) << 6;
if (cch >= 3) hps = page[bfprop + 7];
if (cch >= 2) fBold = page[bfprop + 6] & 1;
if (cch >= 2) fItalic = page[bfprop + 6] & 2;
if (cch >= 4) fUline = page[bfprop + 8] & 1;
if (cch >= 6) hpsPos = page[bfprop + 10];
}
UT_DEBUGMSG((" ftc = %d\n", ftc));
UT_DEBUGMSG((" hps = %d\n", hps));
UT_DEBUGMSG((" fBold = %d\n", fBold));
UT_DEBUGMSG((" fItalic = %d\n", fItalic));
UT_DEBUGMSG((" fUline = %d\n", fUline));
UT_DEBUGMSG((" hpsPos = %d\n", hpsPos));
if (ftc >= wri_fonts_count)
{
UT_WARNINGMSG(("read_txt: Wrong font code.\n"));
ftc = wri_fonts_count - 1;
}
if (from < fcLim && to >= fcFirst)
{
UT_LocaleTransactor lt(LC_NUMERIC, "C");
UT_String_sprintf(properties, "font-weight:%s",
fBold ? "bold" : "normal");
if (hps != 24)
{
UT_String_sprintf(tmp, "; font-size:%dpt", hps / 2);
properties += tmp;
}
if (fItalic) properties += "; font-style:italic";
if (fUline) properties += "; text-decoration:underline";
if (hpsPos)
{
UT_String_sprintf(tmp, "; text-position:%s",
hpsPos < 128 ? "superscript" : "subscript");
properties += tmp;
}
if (wri_fonts_count)
{
UT_String_sprintf(tmp, "; font-family:%s", wri_fonts[ftc].name);
properties += tmp;
}
if (wri_fonts[ftc].codepage != currcp /*sic!*/)
{
set_codepage(wri_fonts[ftc].codepage);
currcp = wri_fonts[ftc].codepage;
}
mText.clear();
UT_DEBUGMSG((" Text: "));
while (fcFirst <= from && from < fcLim && from <= to && from - 0x80 < dataLen)
translate_char(*mData.getPointer(from++ - 0x80), mText);
UT_DEBUGMSG(("\n"));
// new attributes, only if there was text
if (mText.size() > 0)
{
const gchar *attributes[5];
const UT_UCS4Char *text = mText.ucs4_str(), *p = text;
size_t txtLen;
UT_DEBUGMSG((" Conv: %s\n", mText.utf8_str()));
attributes[0] = PT_PROPS_ATTRIBUTE_NAME;
attributes[1] = properties.c_str();
attributes[2] = NULL;
appendFmt(attributes);
// check for page number (should only be in header or footer)
while (*p && *p != (UT_UCS4Char) 0x01) p++;
if (*p)
{
if (p - text) appendSpan(text, p - text);
attributes[2] = PT_TYPE_ATTRIBUTE_NAME;
attributes[3] = "page_number";
attributes[4] = NULL;
appendObject(PTO_Field, attributes);
txtLen = mText.size() - (p - text) - 1;
p++;
}
else
{
txtLen = mText.size();
p = text;
}
if (txtLen) appendSpan(p, txtLen);
}
}
fcFirst = fcLim;
if (fcLim >= fcMac || fcFirst > to)
{
UT_DEBUGMSG((" CHP-FODs end, fcLim (%d) >= fcMac (%d) or fcFirst (%d) > to (%d)\n", fcLim, fcMac, fcFirst, to));
return true;
}
}
}
}
/**********************************************************************
* MSWrite Importer (character translation) *
**********************************************************************/
static struct ffn_suff_cp
{
const char *suffix;
const char *codepage;
}
const ffn_suff_cp_tbl[] =
{
{"\x03 CE", "CP1250"},
{"\x04 Cyr", "CP1251"},
{"\x06 Greek", "CP1253"},
{"\x04 Tur", "CP1254"},
{"\x09 (Hebrew)", "CP1255"},
{"\x09 (Arabic)", "CP1256"},
{"\x07 Baltic", "CP1257"},
{NULL, NULL}
};
const char *IE_Imp_MSWrite::get_codepage (const char *facename, int *facelen) const
{
const struct ffn_suff_cp *f = ffn_suff_cp_tbl;
int l = strlen(facename);
while (f->suffix)
{
if (*f->suffix < l)
{
if (g_ascii_strcasecmp(&f->suffix[1], &facename[l - *f->suffix]) == 0)
{
*facelen = l - *f->suffix;
return f->codepage;
}
}
f++;
}
*facelen = l;
return mDefaultCodepage.c_str();
}
void IE_Imp_MSWrite::set_codepage (const char *charset)
{
charconv.setInCharset(charset);
}
void IE_Imp_MSWrite::translate_char (const UT_Byte ch, UT_UCS4String &buf)
{
UT_UCS4Char uch = ch;
lf = false;
switch (ch)
{
case 9:
buf += UCS_TAB;
break;
case 12:
buf += UCS_FF;
break;
case 10: // line feed
lf = true;
case 13: // carriage return
case 31: // soft hyphen
break;
default:
if (ch & 0x80) charconv.mbtowc(uch, ch);
buf += uch;
break;
}
UT_DEBUGMSG((ch < ' ' ? "\\x%x" : "%c", ch));
}
/**********************************************************************
* MSWrite Importer (picture handling) *
**********************************************************************/
bool IE_Imp_MSWrite::read_pic (int from, int size)
{
// 8 bit colormap (plus one extra color only in the 4 bit colormap)
static const UT_Byte bgr_palette[257][4] =
{
{0x00, 0x00, 0x00, 0x00},
{0x40, 0x00, 0x00, 0x00},
{0x80, 0x00, 0x00, 0x00},
{0xff, 0x00, 0x00, 0x00},
{0x00, 0x20, 0x00, 0x00},
{0x40, 0x20, 0x00, 0x00},
{0x80, 0x20, 0x00, 0x00},
{0xff, 0x20, 0x00, 0x00},
{0x00, 0x40, 0x00, 0x00},
{0x40, 0x40, 0x00, 0x00},
{0x80, 0x40, 0x00, 0x00},
{0xff, 0x40, 0x00, 0x00},
{0x00, 0x60, 0x00, 0x00},
{0x40, 0x60, 0x00, 0x00},
{0x80, 0x60, 0x00, 0x00},
{0xff, 0x60, 0x00, 0x00},
{0x00, 0x80, 0x00, 0x00},
{0x40, 0x80, 0x00, 0x00},
{0x80, 0x80, 0x00, 0x00},
{0xff, 0x80, 0x00, 0x00},
{0x00, 0xa0, 0x00, 0x00},
{0x40, 0xa0, 0x00, 0x00},
{0x80, 0xa0, 0x00, 0x00},
{0xff, 0xa0, 0x00, 0x00},
{0x00, 0xc0, 0x00, 0x00},
{0x40, 0xc0, 0x00, 0x00},
{0x80, 0xc0, 0x00, 0x00},
{0xff, 0xc0, 0x00, 0x00},
{0x00, 0xff, 0x00, 0x00},
{0x40, 0xff, 0x00, 0x00},
{0x80, 0xff, 0x00, 0x00},
{0xff, 0xff, 0x00, 0x00},
{0x00, 0x00, 0x20, 0x00},
{0x40, 0x00, 0x20, 0x00},
{0x80, 0x00, 0x20, 0x00},
{0xff, 0x00, 0x20, 0x00},
{0x00, 0x20, 0x20, 0x00},
{0x40, 0x20, 0x20, 0x00},
{0x80, 0x20, 0x20, 0x00},
{0xff, 0x20, 0x20, 0x00},
{0x00, 0x40, 0x20, 0x00},
{0x40, 0x40, 0x20, 0x00},
{0x80, 0x40, 0x20, 0x00},
{0xff, 0x40, 0x20, 0x00},
{0x00, 0x60, 0x20, 0x00},
{0x40, 0x60, 0x20, 0x00},
{0x80, 0x60, 0x20, 0x00},
{0xff, 0x60, 0x20, 0x00},
{0x00, 0x80, 0x20, 0x00},
{0x40, 0x80, 0x20, 0x00},
{0x80, 0x80, 0x20, 0x00},
{0xff, 0x80, 0x20, 0x00},
{0x00, 0xa0, 0x20, 0x00},
{0x40, 0xa0, 0x20, 0x00},
{0x80, 0xa0, 0x20, 0x00},
{0xff, 0xa0, 0x20, 0x00},
{0x00, 0xc0, 0x20, 0x00},
{0x40, 0xc0, 0x20, 0x00},
{0x80, 0xc0, 0x20, 0x00},
{0xff, 0xc0, 0x20, 0x00},
{0x00, 0xff, 0x20, 0x00},
{0x40, 0xff, 0x20, 0x00},
{0x80, 0xff, 0x20, 0x00},
{0xff, 0xff, 0x20, 0x00},
{0x00, 0x00, 0x40, 0x00},
{0x40, 0x00, 0x40, 0x00},
{0x80, 0x00, 0x40, 0x00},
{0xff, 0x00, 0x40, 0x00},
{0x00, 0x20, 0x40, 0x00},
{0x40, 0x20, 0x40, 0x00},
{0x80, 0x20, 0x40, 0x00},
{0xff, 0x20, 0x40, 0x00},
{0x00, 0x40, 0x40, 0x00},
{0x40, 0x40, 0x40, 0x00},
{0x80, 0x40, 0x40, 0x00},
{0xff, 0x40, 0x40, 0x00},
{0x00, 0x60, 0x40, 0x00},
{0x40, 0x60, 0x40, 0x00},
{0x80, 0x60, 0x40, 0x00},
{0xff, 0x60, 0x40, 0x00},
{0x00, 0x80, 0x40, 0x00},
{0x40, 0x80, 0x40, 0x00},
{0x80, 0x80, 0x40, 0x00},
{0xff, 0x80, 0x40, 0x00},
{0x00, 0xa0, 0x40, 0x00},
{0x40, 0xa0, 0x40, 0x00},
{0x80, 0xa0, 0x40, 0x00},
{0xff, 0xa0, 0x40, 0x00},
{0x00, 0xc0, 0x40, 0x00},
{0x40, 0xc0, 0x40, 0x00},
{0x80, 0xc0, 0x40, 0x00},
{0xff, 0xc0, 0x40, 0x00},
{0x00, 0xff, 0x40, 0x00},
{0x40, 0xff, 0x40, 0x00},
{0x80, 0xff, 0x40, 0x00},
{0xff, 0xff, 0x40, 0x00},
{0x00, 0x00, 0x60, 0x00},
{0x40, 0x00, 0x60, 0x00},
{0x80, 0x00, 0x60, 0x00},
{0xff, 0x00, 0x60, 0x00},
{0x00, 0x20, 0x60, 0x00},
{0x40, 0x20, 0x60, 0x00},
{0x80, 0x20, 0x60, 0x00},
{0xff, 0x20, 0x60, 0x00},
{0x00, 0x40, 0x60, 0x00},
{0x40, 0x40, 0x60, 0x00},
{0x80, 0x40, 0x60, 0x00},
{0xff, 0x40, 0x60, 0x00},
{0x00, 0x60, 0x60, 0x00},
{0x40, 0x60, 0x60, 0x00},
{0x80, 0x60, 0x60, 0x00},
{0xff, 0x60, 0x60, 0x00},
{0x00, 0x80, 0x60, 0x00},
{0x40, 0x80, 0x60, 0x00},
{0x80, 0x80, 0x60, 0x00},
{0xff, 0x80, 0x60, 0x00},
{0x00, 0xa0, 0x60, 0x00},
{0x40, 0xa0, 0x60, 0x00},
{0x80, 0xa0, 0x60, 0x00},
{0xff, 0xa0, 0x60, 0x00},
{0x00, 0xc0, 0x60, 0x00},
{0x40, 0xc0, 0x60, 0x00},
{0x80, 0xc0, 0x60, 0x00},
{0xff, 0xc0, 0x60, 0x00},
{0x00, 0xff, 0x60, 0x00},
{0x40, 0xff, 0x60, 0x00},
{0x80, 0xff, 0x60, 0x00},
{0xff, 0xff, 0x60, 0x00},
{0x00, 0x00, 0x80, 0x00},
{0x40, 0x00, 0x80, 0x00},
{0x80, 0x00, 0x80, 0x00},
{0xff, 0x00, 0x80, 0x00},
{0x00, 0x20, 0x80, 0x00},
{0x40, 0x20, 0x80, 0x00},
{0x80, 0x20, 0x80, 0x00},
{0xff, 0x20, 0x80, 0x00},
{0x00, 0x40, 0x80, 0x00},
{0x40, 0x40, 0x80, 0x00},
{0x80, 0x40, 0x80, 0x00},
{0xff, 0x40, 0x80, 0x00},
{0x00, 0x60, 0x80, 0x00},
{0x40, 0x60, 0x80, 0x00},
{0x80, 0x60, 0x80, 0x00},
{0xff, 0x60, 0x80, 0x00},
{0x00, 0x80, 0x80, 0x00},
{0x40, 0x80, 0x80, 0x00},
{0x80, 0x80, 0x80, 0x00},
{0xff, 0x80, 0x80, 0x00},
{0x00, 0xa0, 0x80, 0x00},
{0x40, 0xa0, 0x80, 0x00},
{0x80, 0xa0, 0x80, 0x00},
{0xff, 0xa0, 0x80, 0x00},
{0x00, 0xc0, 0x80, 0x00},
{0x40, 0xc0, 0x80, 0x00},
{0x80, 0xc0, 0x80, 0x00},
{0xff, 0xc0, 0x80, 0x00},
{0x00, 0xff, 0x80, 0x00},
{0x40, 0xff, 0x80, 0x00},
{0x80, 0xff, 0x80, 0x00},
{0xff, 0xff, 0x80, 0x00},
{0x00, 0x00, 0xa0, 0x00},
{0x40, 0x00, 0xa0, 0x00},
{0x80, 0x00, 0xa0, 0x00},
{0xff, 0x00, 0xa0, 0x00},
{0x00, 0x20, 0xa0, 0x00},
{0x40, 0x20, 0xa0, 0x00},
{0x80, 0x20, 0xa0, 0x00},
{0xff, 0x20, 0xa0, 0x00},
{0x00, 0x40, 0xa0, 0x00},
{0x40, 0x40, 0xa0, 0x00},
{0x80, 0x40, 0xa0, 0x00},
{0xff, 0x40, 0xa0, 0x00},
{0x00, 0x60, 0xa0, 0x00},
{0x40, 0x60, 0xa0, 0x00},
{0x80, 0x60, 0xa0, 0x00},
{0xff, 0x60, 0xa0, 0x00},
{0x00, 0x80, 0xa0, 0x00},
{0x40, 0x80, 0xa0, 0x00},
{0x80, 0x80, 0xa0, 0x00},
{0xff, 0x80, 0xa0, 0x00},
{0x00, 0xa0, 0xa0, 0x00},
{0x40, 0xa0, 0xa0, 0x00},
{0x80, 0xa0, 0xa0, 0x00},
{0xff, 0xa0, 0xa0, 0x00},
{0x00, 0xc0, 0xa0, 0x00},
{0x40, 0xc0, 0xa0, 0x00},
{0x80, 0xc0, 0xa0, 0x00},
{0xff, 0xc0, 0xa0, 0x00},
{0x00, 0xff, 0xa0, 0x00},
{0x40, 0xff, 0xa0, 0x00},
{0x80, 0xff, 0xa0, 0x00},
{0xff, 0xff, 0xa0, 0x00},
{0x00, 0x00, 0xc0, 0x00},
{0x40, 0x00, 0xc0, 0x00},
{0x80, 0x00, 0xc0, 0x00},
{0xff, 0x00, 0xc0, 0x00},
{0x00, 0x20, 0xc0, 0x00},
{0x40, 0x20, 0xc0, 0x00},
{0x80, 0x20, 0xc0, 0x00},
{0xff, 0x20, 0xc0, 0x00},
{0x00, 0x40, 0xc0, 0x00},
{0x40, 0x40, 0xc0, 0x00},
{0x80, 0x40, 0xc0, 0x00},
{0xff, 0x40, 0xc0, 0x00},
{0x00, 0x60, 0xc0, 0x00},
{0x40, 0x60, 0xc0, 0x00},
{0x80, 0x60, 0xc0, 0x00},
{0xff, 0x60, 0xc0, 0x00},
{0x00, 0x80, 0xc0, 0x00},
{0x40, 0x80, 0xc0, 0x00},
{0x80, 0x80, 0xc0, 0x00},
{0xff, 0x80, 0xc0, 0x00},
{0x00, 0xa0, 0xc0, 0x00},
{0x40, 0xa0, 0xc0, 0x00},
{0x80, 0xa0, 0xc0, 0x00},
{0xff, 0xa0, 0xc0, 0x00},
{0x00, 0xc0, 0xc0, 0x00},
{0x40, 0xc0, 0xc0, 0x00},
{0x80, 0xc0, 0xc0, 0x00},
{0xff, 0xc0, 0xc0, 0x00},
{0x00, 0xff, 0xc0, 0x00},
{0x40, 0xff, 0xc0, 0x00},
{0x80, 0xff, 0xc0, 0x00},
{0xff, 0xff, 0xc0, 0x00},
{0x00, 0x00, 0xff, 0x00},
{0x40, 0x00, 0xff, 0x00},
{0x80, 0x00, 0xff, 0x00},
{0xff, 0x00, 0xff, 0x00},
{0x00, 0x20, 0xff, 0x00},
{0x40, 0x20, 0xff, 0x00},
{0x80, 0x20, 0xff, 0x00},
{0xff, 0x20, 0xff, 0x00},
{0x00, 0x40, 0xff, 0x00},
{0x40, 0x40, 0xff, 0x00},
{0x80, 0x40, 0xff, 0x00},
{0xff, 0x40, 0xff, 0x00},
{0x00, 0x60, 0xff, 0x00},
{0x40, 0x60, 0xff, 0x00},
{0x80, 0x60, 0xff, 0x00},
{0xff, 0x60, 0xff, 0x00},
{0x00, 0x80, 0xff, 0x00},
{0x40, 0x80, 0xff, 0x00},
{0x80, 0x80, 0xff, 0x00},
{0xff, 0x80, 0xff, 0x00},
{0x00, 0xa0, 0xff, 0x00},
{0x40, 0xa0, 0xff, 0x00},
{0x80, 0xa0, 0xff, 0x00},
{0xff, 0xa0, 0xff, 0x00},
{0x00, 0xc0, 0xff, 0x00},
{0x40, 0xc0, 0xff, 0x00},
{0x80, 0xc0, 0xff, 0x00},
{0xff, 0xc0, 0xff, 0x00},
{0x00, 0xff, 0xff, 0x00},
{0x40, 0xff, 0xff, 0x00},
{0x80, 0xff, 0xff, 0x00},
{0xff, 0xff, 0xff, 0x00},
{0xc0, 0xc0, 0xc0, 0x00} // a color only in the 4 bit colormap
};
// indices (of the 8 bit colormap above) for the 4 bit colormap
static const int c16idx[16] = { 0, 128, 16, 144, 2, 130, 18, 256,
146, 224, 28, 252, 3, 227, 31, 255 };
// structs without alignment
#pragma pack(push)
#pragma pack(1)
struct
{
char bfType[2];
uint32_t bfSize;
uint32_t bfReserved;
uint32_t bfOffBits;
} bmpheader;
struct
{
uint32_t header_sz;
uint32_t width;
uint32_t height;
uint16_t nplanes;
uint16_t bitspp;
uint32_t compress_type;
uint32_t bmp_bytesz;
uint32_t hres;
uint32_t vres;
uint32_t ncolors;
uint32_t nimpcolors;
} bmpinfo;
#pragma pack(pop)
unsigned char page[0x80];
int mm, cbHeader, cbSize, bmBitsPixel, objectType;
int colorPaletteLen = 0, bmW = 0, bmH = 0;
int chunk, filler, written, ole_offset, formatID, objLen, bhSize, bppOff;
wri_struct *write_pic = NULL;
UT_ByteBuf pic;
IEGraphicFileType iegft = IEGFT_Unknown;
const char *msg = NULL, *className = "";
FG_Graphic *graphic = NULL;
if (size < PIC_OR_OLE_HEADER_SIZE + OLE_ClassNameString)
{
UT_WARNINGMSG(("read_pic: Size error, type 1!\n"));
return false;
}
// prepare bmp file header
memset(&bmpheader, 0, sizeof(bmpheader));
bmpheader.bfType[0] = 'B';
bmpheader.bfType[1] = 'M';
memset(&bmpinfo, 0, sizeof(bmpinfo));
gsf_input_seek(mFile, from, G_SEEK_SET);
gsf_input_read(mFile, sizeof(page), page);
mm = READ_WORD(page);
switch (mm)
{
case 0x88: // wmf file
case 0xe3: // bitmap
write_pic = wri_picture_header;
read_wri_struct_mem(write_pic, page);
UT_DEBUGMSG((" PIC (wmf/bmp) Header:\n"));
DEBUG_WRI_STRUCT(write_pic, 5);
cbHeader = wri_struct_value(write_pic, "cbHeader");
cbSize = wri_struct_value(write_pic, "cbSize");
if (size < cbHeader + cbSize)
{
msg = "Size error, type 2";
break;
}
if (mm == 0xe3) // bitmap needs header
{
if ((bmBitsPixel = wri_struct_value(write_pic, "bmBitsPixel")) < 16)
colorPaletteLen = (1 << bmBitsPixel) << 2;
if (colorPaletteLen != 8)
{
msg = "Color palette error";
break;
}
// make a bitmap file header
WRITE_DWORD(bmpheader.bfOffBits, sizeof(bmpheader) + sizeof(bmpinfo) + colorPaletteLen);
pic.append(reinterpret_cast<UT_Byte *>(&bmpheader), sizeof(bmpheader));
bmW = wri_struct_value(write_pic, "bmWidth");
bmH = wri_struct_value(write_pic, "bmHeight");
if (bmH & 0x8000) bmH = -0x10000 + bmH;
WRITE_DWORD(bmpinfo.header_sz, sizeof(bmpinfo));
WRITE_DWORD(bmpinfo.width, bmW);
WRITE_DWORD(bmpinfo.height, -bmH);
WRITE_WORD(bmpinfo.nplanes, wri_struct_value(write_pic, "bmPlanes"));
WRITE_WORD(bmpinfo.bitspp, bmBitsPixel);
pic.append(reinterpret_cast<UT_Byte *>(&bmpinfo), sizeof(bmpinfo));
// add monochrome colormap
pic.append(bgr_palette[0], colorPaletteLen >> 1);
pic.append(bgr_palette[255], colorPaletteLen >> 1);
chunk = wri_struct_value(write_pic, "bmWidthBytes");
filler = (4 - (chunk & 3)) & 3;
}
else
{
iegft = IEGFT_WMF;
chunk = cbSize;
filler = 0;
}
written = 0;
while (written < cbSize)
{
// append data
pic.append(mData.getPointer(from - 0x80 + cbHeader + written), chunk);
for (int i = 0; i < filler; i++)
pic.append(reinterpret_cast<const UT_Byte *>("\x00"), 1);
written += chunk;
}
break;
case 0xe4: // ole object
write_pic = wri_ole_header;
read_wri_struct_mem(write_pic, page);
UT_DEBUGMSG((" PIC (OLE) Header:\n"));
DEBUG_WRI_STRUCT(write_pic, 5);
objectType = wri_struct_value(write_pic, "objectType");
cbHeader = wri_struct_value(write_pic, "cbHeader");
if ((ole_offset = READ_DWORD(page + cbHeader + OLE_ClassNameLength)) > 0)
className = reinterpret_cast<const char *>(page + cbHeader + OLE_ClassNameString);
if ((formatID = READ_DWORD(page + cbHeader + OLE_FormatID)) == 1 ||
formatID == 2)
{
if (size < cbHeader + OLE_TopicNameString + ole_offset)
{
msg = "Size error, type 3";
break;
}
ole_offset += READ_DWORD(page + cbHeader + OLE_TopicNameLength + ole_offset);
if (size < cbHeader + OLE_ItemNameString + ole_offset)
{
msg = "Size error, type 4";
break;
}
ole_offset += READ_DWORD(page + cbHeader + OLE_ItemNameLength + ole_offset);
}
objLen = READ_DWORD(page + cbHeader + OLE_ObjDataSize + ole_offset);
if (strcmp(className, "METAFILEPICT") == 0)
{
iegft = IEGFT_WMF;
ole_offset += OLE_MF_Object - OLE_Object;
objLen -= OLE_MF_Object - OLE_Object;
objectType = 2; // we can go embedded now
}
if (size <= cbHeader + OLE_Object + ole_offset ||
cbHeader + OLE_Object + ole_offset > 0x80 ||
size < cbHeader + OLE_Object + ole_offset + objLen)
{
msg = "Size error, type 5";
break;
}
// static
if (objectType == 1)
{
if (strcmp(className, "DIB") == 0)
{
bhSize = READ_DWORD(page + cbHeader + OLE_Object + ole_offset);
bppOff = (bhSize == 12 ? 10 : 14); // different header info types
if ((bmBitsPixel = READ_WORD(page + cbHeader + OLE_Object + ole_offset + bppOff)) < 16)
colorPaletteLen = (1 << bmBitsPixel) << 2;
// make a bitmap file header
WRITE_DWORD(bmpheader.bfOffBits, sizeof(bmpheader) + bhSize + colorPaletteLen);
pic.append(reinterpret_cast<UT_Byte *>(&bmpheader), sizeof(bmpheader));
objectType = 2; // we can go embedded now
}
if (strcmp(className, "BITMAP") == 0)
{
if ((bmBitsPixel = *(page + cbHeader + OLE_Object + ole_offset + BM16_BitsPixel)) < 16)
colorPaletteLen = (1 << bmBitsPixel) << 2;
// make a bitmap file header
WRITE_DWORD(bmpheader.bfOffBits, sizeof(bmpheader) + sizeof(bmpinfo) + colorPaletteLen);
pic.append(reinterpret_cast<UT_Byte *>(&bmpheader), sizeof(bmpheader));
bmH = READ_WORD(page + cbHeader + OLE_Object + ole_offset + BM16_Height);
if (bmH & 0x8000) bmH = -0x10000 + bmH;
WRITE_DWORD(bmpinfo.header_sz, sizeof(bmpinfo));
WRITE_DWORD(bmpinfo.width, READ_WORD(page + cbHeader + OLE_Object + ole_offset + BM16_Width));
WRITE_DWORD(bmpinfo.height, bmH);
WRITE_WORD(bmpinfo.nplanes, *(page + cbHeader + OLE_Object + ole_offset + BM16_Planes));
WRITE_WORD(bmpinfo.bitspp, bmBitsPixel);
pic.append(reinterpret_cast<UT_Byte *>(&bmpinfo), sizeof(bmpinfo));
// add corresponding colormap
switch (bmBitsPixel)
{
case 1:
pic.append(bgr_palette[0], colorPaletteLen >> 1);
pic.append(bgr_palette[255], colorPaletteLen >> 1);
break;
case 4:
for (int i = 0; i < 16; i++) pic.append(bgr_palette[c16idx[i]], 4);
break;
case 8:
pic.append(bgr_palette[0], colorPaletteLen);
break;
}
ole_offset += BM16_Bits;
objectType = 2; // we can go embedded now
}
}
// embedded
if (objectType == 2)
pic.append(mData.getPointer(from - 0x80 + cbHeader + OLE_Object + ole_offset), objLen);
break;
}
// let's see...
if (pic.getLength())
{
// ...whether it's a picture
if (IE_ImpGraphic::loadGraphic(pic, iegft, &graphic) != UT_OK || !graphic)
msg = "Picture load error or no picture";
else
{
int dxaOffset, dxaSize, dyaSize, mx, my;
const gchar *attributes[5];
UT_String properties, id;
UT_LocaleTransactor lt(LC_NUMERIC, "C");
dxaOffset = wri_struct_value(write_pic, "dxaOffset");
if (dxaOffset & 0x8000) dxaOffset = -0x10000 + dxaOffset;
if (dxaOffset)
{
UT_String_sprintf(properties, "margin-left:%.4fin",
static_cast<float>(dxaOffset) / 1440.0);
attributes[0] = PT_PROPS_ATTRIBUTE_NAME;
attributes[1] = properties.c_str();
attributes[2] = NULL;
appendStrux(PTX_Block, attributes);
}
dxaSize = wri_struct_value(write_pic, "dxaSize");
dyaSize = wri_struct_value(write_pic, "dyaSize");
if (dxaSize == 0) dxaSize = wri_struct_value(write_pic, "xExt");
if (dyaSize == 0) dyaSize = wri_struct_value(write_pic, "yExt");
if (dxaSize & 0x8000) dxaSize = -0x10000 + dxaSize;
if (dyaSize & 0x8000) dyaSize = -0x10000 + dyaSize;
mx = wri_struct_value(write_pic, "mx");
my = wri_struct_value(write_pic, "my");
// 0xe3 picture sizes aren't reliable, so we recalculate them
// (1 pixel = 15 twips)
if (mm == 0xe3)
{
dxaSize = bmW * 15;
dyaSize = (bmH < 0 ? -bmH : bmH) * 15;
}
UT_String_sprintf(properties, "width:%.4fin; height:%.4fin",
static_cast<float>(mx) / 1000.0 *
static_cast<float>(dxaSize) / 1440.0,
static_cast<float>(my) / 1000.0 *
static_cast<float>(dyaSize) / 1440.0);
UT_String_sprintf(id, "image%u", ++pic_nr);
UT_DEBUGMSG((" Image #%02d\n", pic_nr));
attributes[0] = PT_PROPS_ATTRIBUTE_NAME;
attributes[1] = properties.c_str();
attributes[2] = "dataid";
attributes[3] = id.c_str();
attributes[4] = NULL;
appendObject(PTO_Image, attributes);
getDoc()->createDataItem(id.c_str(), false, graphic->getBuffer(), graphic->getMimeType(), NULL);
}
}
DELETEP(graphic);
if (write_pic) free_wri_struct(write_pic);
if (msg) UT_WARNINGMSG(("read_pic: %s!\n", msg));
return (msg ? false : true);
}
/**********************************************************************
* MSWrite Importer (header and footer opening) *
**********************************************************************/
void IE_Imp_MSWrite::_append_hdrftr (hdrftr_t which)
{
const gchar *attributes[5];
attributes[0] = PT_ID_ATTRIBUTE_NAME;
attributes[2] = PT_TYPE_ATTRIBUTE_NAME;
attributes[4] = NULL;
switch (which)
{
case headerfirst:
attributes[1] = "0";
attributes[3] = "header-first";
break;
case header:
attributes[1] = "1";
attributes[3] = "header";
break;
case footerfirst:
attributes[1] = "2";
attributes[3] = "footer-first";
break;
case footer:
attributes[1] = "3";
attributes[3] = "footer";
break;
}
appendStrux(PTX_Section, attributes);
}
|