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
|
/*
* This file is part of Office 2007 Filters for Calligra
* Copyright (C) 2002 Laurent Montel <lmontel@mandrakesoft.com>
* Copyright (c) 2003 Lukas Tinkl <lukas@kde.org>
* Copyright (C) 2003 David Faure <faure@kde.org>
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
* Contact: Suresh Chande suresh.chande@nokia.com
* Copyright (C) 2011 Matus Uzak <matus.uzak@ixonos.com>
*
* Utils::columnName() based on Cell::columnName() from calligra/kspread/Utils.cpp:
* Copyright 2006-2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
* Copyright 2004 Tomas Mecir <mecirt@gmail.com>
* Copyright 1999-2002,2004 Laurent Montel <montel@kde.org>
* Copyright 2002,2004 Ariya Hidayat <ariya@kde.org>
* Copyright 2002-2003 Norbert Andres <nandres@web.de>
* Copyright 2003 Stefan Hetzl <shetzl@chello.at>
* Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
* Copyright 2002 Harri Porten <porten@kde.org>
* Copyright 2002 John Dailey <dailey@vt.edu>
* Copyright 1999-2001 David Faure <faure@kde.org>
* Copyright 2000-2001 Werner Trobin <trobin@kde.org>
* Copyright 2000 Simon Hausmann <hausmann@kde.org
* Copyright 1998-1999 Torben Weis <weis@kde.org>
* Copyright 1999 Michael Reiher <michael.reiher@gmx.de>
* Copyright 1999 Reginald Stadlbauer <reggie@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "MsooXmlUtils.h"
#include "MsooXmlUnits.h"
#include "MsooXmlContentTypes.h"
#include "MsooXmlSchemas.h"
#include "MsooXmlReader.h"
#include "ooxml_pole.h"
#include <styles/KoCharacterStyle.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>
#include <KoGenStyles.h>
#include <KoUnit.h>
#include <QDomDocument>
#include <QColor>
#include <QBrush>
#include <QImage>
#include <QImageReader>
#include <QPalette>
#include <QRegExp>
#include <QtXml>
#include <kdebug.h>
#include <kzip.h>
#include <memory>
// common officedocument content types
const char MSOOXML::ContentTypes::coreProps[] = "application/vnd.openxmlformats-package.core-properties+xml";
const char MSOOXML::ContentTypes::extProps[] = "application/vnd.openxmlformats-officedocument.extended-properties+xml";
const char MSOOXML::ContentTypes::theme[] = "application/vnd.openxmlformats-officedocument.theme+xml";
// wordprocessingml-specific content types
const char MSOOXML::ContentTypes::wordDocument[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
const char MSOOXML::ContentTypes::wordSettings[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml";
const char MSOOXML::ContentTypes::wordStyles[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml";
const char MSOOXML::ContentTypes::wordHeader[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml";
const char MSOOXML::ContentTypes::wordFooter[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml";
const char MSOOXML::ContentTypes::wordFootnotes[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml";
const char MSOOXML::ContentTypes::wordEndnotes[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml";
const char MSOOXML::ContentTypes::wordFontTable[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml";
const char MSOOXML::ContentTypes::wordWebSettings[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml";
const char MSOOXML::ContentTypes::wordTemplate[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";
const char MSOOXML::ContentTypes::wordComments[] = "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml";
// presentationml-specific content types
const char MSOOXML::ContentTypes::presentationDocument[] = "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml";
const char MSOOXML::ContentTypes::presentationSlide[] = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml";
const char MSOOXML::ContentTypes::presentationSlideLayout[] = "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml";
const char MSOOXML::ContentTypes::presentationSlideShow[] = "application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml";
const char MSOOXML::ContentTypes::presentationTemplate[] = "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml";
const char MSOOXML::ContentTypes::presentationNotes[] = "application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml";
const char MSOOXML::ContentTypes::presentationTableStyles[] = "application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml";
const char MSOOXML::ContentTypes::presentationProps[] = "application/vnd.openxmlformats-officedocument.presentationml.presProps+xml";
const char MSOOXML::ContentTypes::presentationViewProps[] = "application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml";
const char MSOOXML::ContentTypes::presentationComments[] = "application/vnd.openxmlformats-officedocument.presentationml.comments+xml";
// spreadsheetml-specific content types
const char MSOOXML::ContentTypes::spreadsheetDocument[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
const char MSOOXML::ContentTypes::spreadsheetMacroDocument[] = "application/vnd.ms-excel.sheet.macroEnabled.main+xml";
const char MSOOXML::ContentTypes::spreadsheetPrinterSettings[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings";
const char MSOOXML::ContentTypes::spreadsheetStyles[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml";
const char MSOOXML::ContentTypes::spreadsheetWorksheet[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml";
const char MSOOXML::ContentTypes::spreadsheetCalcChain[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml";
const char MSOOXML::ContentTypes::spreadsheetSharedStrings[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml";
const char MSOOXML::ContentTypes::spreadsheetTemplate[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml";
const char MSOOXML::ContentTypes::spreadsheetComments[] = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml";
//generic namespaces
const char MSOOXML::Schemas::dublin_core[] = "http://purl.org/dc/elements/1.1/";
// common namespaces
const char MSOOXML::Schemas::contentTypes[] = "http://schemas.openxmlformats.org/package/2006/content-types";
const char MSOOXML::Schemas::relationships[] = "http://schemas.openxmlformats.org/package/2006/relationships";
const char MSOOXML::Schemas::core_properties[] = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
// ISO/IEC 29500-1:2008(E), Annex A. (normative), p. 4355
// See also: specs/all.xsd
// A.1 WordprocessingML
const char MSOOXML::Schemas::wordprocessingml[] = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
// A.2 SpreadsheetML
const char MSOOXML::Schemas::spreadsheetml[] = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
// A.3 PresentationML
const char MSOOXML::Schemas::presentationml[] = "http://schemas.openxmlformats.org/presentationml/2006/main";
// A.4 DrawingML - Framework
const char MSOOXML::Schemas::drawingml::main[] = "http://schemas.openxmlformats.org/drawingml/2006/main";
const char MSOOXML::Schemas::drawingml::wordprocessingDrawing[] = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing";
const char MSOOXML::Schemas::drawingml::spreadsheetDrawing[] = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing";
const char MSOOXML::Schemas::drawingml::compatibility[] = "http://schemas.openxmlformats.org/drawingml/2006/compatibility";
const char MSOOXML::Schemas::drawingml::lockedCanvas[] = "http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas";
const char MSOOXML::Schemas::drawingml::picture[] = "http://schemas.openxmlformats.org/drawingml/2006/picture";
// A.5 DrawingML - Components
const char MSOOXML::Schemas::drawingml::chart[] = "http://schemas.openxmlformats.org/drawingml/2006/chart";
const char MSOOXML::Schemas::drawingml::chartDrawing[] = "http://schemas.openxmlformats.org/drawingml/2006/chartDrawing";
const char MSOOXML::Schemas::drawingml::diagram[] = "http://schemas.openxmlformats.org/drawingml/2006/diagram";
// A.6 Shared MLs
const char MSOOXML::Schemas::officeDocument::math[] = "http://schemas.openxmlformats.org/officeDocument/2006/math";
const char MSOOXML::Schemas::officeDocument::bibliography[] = "http://schemas.openxmlformats.org/officeDocument/2006/bibliography";
const char MSOOXML::Schemas::officeDocument::characteristics[] = "http://schemas.openxmlformats.org/officeDocument/2006/characteristics";
const char MSOOXML::Schemas::officeDocument::customXml[] = "http://schemas.openxmlformats.org/officeDocument/2006/customXml";
const char MSOOXML::Schemas::officeDocument::custom_properties[] = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
const char MSOOXML::Schemas::officeDocument::docPropsVTypes[] = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
const char MSOOXML::Schemas::officeDocument::extended_properties[] = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
const char MSOOXML::Schemas::officeDocument::relationships[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
const char MSOOXML::Schemas::officeDocument::sharedTypes[] = "http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes";
// A.7 Custom XML Schema References
const char MSOOXML::Schemas::schemaLibrary[] = "http://schemas.openxmlformats.org/schemaLibrary/2006/main";
// Marks that the value has not been modified;
static const char UNUSED[] = "UNUSED";
using namespace MSOOXML;
//-----------------------------------------
KoFilter::ConversionStatus Utils::loadAndParse(QIODevice* io, KoXmlDocument& doc,
QString& errorMessage, const QString & fileName)
{
errorMessage.clear();
QString errorMsg;
int errorLine, errorColumn;
bool ok = doc.setContent(io, true, &errorMsg, &errorLine, &errorColumn);
if (!ok) {
kError() << "Parsing error in " << fileName << ", aborting!" << endl
<< " In line: " << errorLine << ", column: " << errorColumn << endl
<< " Error message: " << errorMsg;
errorMessage = i18n("Parsing error in the main document at line %1, column %2.\n"
"Error message: %3", errorLine , errorColumn , i18n("QXml", errorMsg));
return KoFilter::ParsingError;
}
kDebug() << "File" << fileName << "loaded and parsed.";
return KoFilter::OK;
}
KoFilter::ConversionStatus Utils::loadAndParse(KoXmlDocument& doc, const KZip* zip,
QString& errorMessage, const QString& fileName)
{
errorMessage.clear();
KoFilter::ConversionStatus status;
std::auto_ptr<QIODevice> device(openDeviceForFile(zip, errorMessage, fileName, status));
if (!device.get())
return status;
return loadAndParse(device.get(), doc, errorMessage, fileName);
}
KoFilter::ConversionStatus Utils::loadAndParseDocument(MsooXmlReader* reader,
const KZip* zip,
KoOdfWriters *writers,
QString& errorMessage,
const QString& fileName,
MsooXmlReaderContext* context)
{
Q_UNUSED(writers)
errorMessage.clear();
KoFilter::ConversionStatus status;
std::auto_ptr<QIODevice> device(openDeviceForFile(zip, errorMessage, fileName, status));
if (!device.get())
return status;
reader->setDevice(device.get());
reader->setFileName(fileName); // for error reporting
status = reader->read(context);
if (status != KoFilter::OK) {
errorMessage = reader->errorString();
return status;
}
kDebug() << "File" << fileName << "loaded and parsed.";
return KoFilter::OK;
}
QIODevice* Utils::openDeviceForFile(const KZip* zip, QString& errorMessage, const QString& fileName,
KoFilter::ConversionStatus& status)
{
kDebug() << "Trying to open" << fileName;
errorMessage.clear();
const KArchiveEntry* entry = zip->directory()->entry(fileName);
if (!entry) {
errorMessage = i18n("Entry '%1' not found.", fileName);
kDebug() << errorMessage;
status = KoFilter::FileNotFound;
return 0;
}
if (!entry->isFile()) {
errorMessage = i18n("Entry '%1' is not a file.", fileName);
kDebug() << errorMessage;
status = KoFilter::WrongFormat;
return 0;
}
const KZipFileEntry* f = static_cast<const KZipFileEntry *>(entry);
kDebug() << "Entry" << fileName << "has size" << f->size();
status = KoFilter::OK;
// There seem to be some problems with kde/zlib when trying to read
// multiple streams, this functionality is needed in the filter
// Until there's another solution for this, this avoids the problem
//return f->createDevice();
QBuffer *device = new QBuffer();
device->setData(f->data());
device->open(QIODevice::ReadOnly);
return device;
}
#define BLOCK_SIZE 4096
static KoFilter::ConversionStatus copyOle(QString& errorMessage,
const QString sourceName, KoStore *outputStore,
const QString& destinationName, const KZip* zip)
{
KoFilter::ConversionStatus status = KoFilter::OK;
QIODevice* inputDevice = Utils::openDeviceForFile(zip, errorMessage, sourceName, status);
if (!inputDevice) {
// Source did not exist
return KoFilter::CreationError;
}
inputDevice->open(QIODevice::ReadOnly);
OOXML_POLE::Storage storage(inputDevice);
if (!storage.open()) {
kDebug(30513) << "Cannot open " << sourceName;
return KoFilter::WrongFormat;
}
std::list<std::string> lista = storage.entries();
std::string oleType = "Contents";
for (std::list<std::string>::iterator it = lista.begin(); it != lista.end(); ++it) {
//qDebug() << "ENTRY " << (*it).c_str();
if (QString((*it).c_str()).contains("Ole10Native")) {
oleType = "Ole10Native";
}
else if (QString((*it).c_str()).contains("CONTENTS")) {
oleType = "CONTENTS";
}
}
OOXML_POLE::Stream stream(&storage, oleType);
QByteArray array;
array.resize(stream.size());
unsigned long r = stream.read((unsigned char*)array.data(), stream.size());
if (r != stream.size()) {
kError(30513) << "Error while reading from stream";
return KoFilter::WrongFormat;
}
if (oleType == "Contents" || oleType == "Ole10Native") {
// Removing first 4 bytes which are the size
array = array.right(array.length() - 4);
}
// Uncomment to write any ole file for testing
//POLE::Stream streamTemp(&storage, "Ole");
//QByteArray arrayTemp;
//arrayTemp.resize(streamTemp.size());
//streamTemp.read((unsigned char*)arrayTemp.data(), streamTemp.size());
//QFile file("olething.ole");
//file.open(QIODevice::WriteOnly);
//QDataStream out(&file);
//out.writeRawData(arrayTemp.data(), arrayTemp.length());
kDebug() << "mode:" << outputStore->mode();
if (!outputStore->open(destinationName)) {
errorMessage = i18n("Could not open entry \"%1\" for writing.", destinationName);
return KoFilter::CreationError;
}
QByteArray array2;
while (true) {
array2 = array.left(BLOCK_SIZE);
array = array.right(array.size() - array2.size());
const qint64 in = array2.size();
if (in <= 0) {
break;
}
char *block = array2.data();
if (in != outputStore->write(block, in)) {
errorMessage = i18n("Could not write block");
status = KoFilter::CreationError;
break;
}
}
outputStore->close();
delete inputDevice;
inputDevice = 0;
return status;
}
#undef BLOCK_SIZE
#define BLOCK_SIZE 4096
KoFilter::ConversionStatus Utils::createImage(QString& errorMessage,
const QImage& source, KoStore *outputStore,
const QString& destinationName)
{
if (outputStore->hasFile(destinationName)) {
return KoFilter::OK;
}
KoFilter::ConversionStatus status = KoFilter::OK;
QByteArray array;
QBuffer inputDevice(&array);
inputDevice.open(QIODevice::ReadWrite);
QFileInfo info = QFileInfo(destinationName);
source.save(&inputDevice, info.suffix().toUtf8());
inputDevice.seek(0);
if (!outputStore->open(destinationName)) {
errorMessage = i18n("Could not open entry \"%1\" for writing.", destinationName);
return KoFilter::CreationError;
}
char block[BLOCK_SIZE];
while (true) {
const qint64 in = inputDevice.read(block, BLOCK_SIZE);
if (in <= 0) {
break;
}
if (in != outputStore->write(block, in)) {
errorMessage = i18n("Could not write block");
status = KoFilter::CreationError;
break;
}
}
outputStore->close();
return status;
}
#undef BLOCK_SIZE
#define BLOCK_SIZE 4096
KoFilter::ConversionStatus Utils::copyFile(const KZip* zip, QString& errorMessage,
const QString& sourceName, KoStore *outputStore,
const QString& destinationName, bool oleType)
{
if (outputStore->hasFile(destinationName)) {
return KoFilter::OK;
}
KoFilter::ConversionStatus status;
if (oleType) {
status = copyOle(errorMessage, sourceName, outputStore, destinationName, zip);
return status;
}
std::auto_ptr<QIODevice> inputDevice = std::auto_ptr<QIODevice>(Utils::openDeviceForFile(zip, errorMessage, sourceName, status));
if (!inputDevice.get()) {
return status;
}
kDebug() << "mode:" << outputStore->mode();
if (!outputStore->open(destinationName)) {
errorMessage = i18n("Could not open entry \"%1\" for writing.", destinationName);
return KoFilter::CreationError;
}
status = KoFilter::OK;
char block[BLOCK_SIZE];
while (true) {
const qint64 in = inputDevice->read(block, BLOCK_SIZE);
// kDebug() << "in:" << in;
if (in <= 0)
break;
if (in != outputStore->write(block, in)) {
errorMessage = i18n("Could not write block");
status = KoFilter::CreationError;
break;
}
}
outputStore->close();
return status;
}
#undef BLOCK_SIZE
KoFilter::ConversionStatus Utils::imageSize(const KZip* zip, QString& errorMessage, const QString& sourceName,
QSize* size)
{
Q_ASSERT(size);
KoFilter::ConversionStatus status;
std::auto_ptr<QIODevice> inputDevice(Utils::openDeviceForFile(zip, errorMessage, sourceName, status));
if (!inputDevice.get()) {
return status;
}
QImageReader r(inputDevice.get(), QFileInfo(sourceName).suffix().toLatin1());
if (!r.canRead())
return KoFilter::WrongFormat;
*size = r.size();
kDebug() << *size;
return KoFilter::OK;
}
KoFilter::ConversionStatus Utils::loadThumbnail(QImage& thumbnail, KZip* zip)
{
//! @todo
Q_UNUSED(thumbnail)
Q_UNUSED(zip)
return KoFilter::FileNotFound;
}
//! @return true if @a el has tag name is equal to @a expectedTag or false otherwise;
//! on failure optional @a warningPrefix message is prepended to the warning
static bool checkTag(const KoXmlElement& el, const char* expectedTag, const char* warningPrefix = 0)
{
if (el.tagName() != expectedTag) {
kWarning()
<< (warningPrefix ? QString::fromLatin1(warningPrefix) + ":" : QString())
<< "tag name=" << el.tagName() << " expected:" << expectedTag;
return false;
}
return true;
}
//! @return true if @a el has namespace URI is equal to @a expectedNSURI or false otherwise
static bool checkNsUri(const KoXmlElement& el, const char* expectedNsUri)
{
if (el.namespaceURI() != expectedNsUri) {
kWarning() << "Invalid namespace URI" << el.namespaceURI() << " expected:" << expectedNsUri;
return false;
}
return true;
}
bool Utils::convertBooleanAttr(const QString& value, bool defaultValue)
{
const QByteArray val(value.toLatin1());
if (val.isEmpty()) {
return defaultValue;
}
kDebug() << val;
return val != MsooXmlReader::constOff && val != MsooXmlReader::constFalse && val != MsooXmlReader::const0;
}
KoFilter::ConversionStatus Utils::loadContentTypes(
const KoXmlDocument& contentTypesXML, QMultiHash<QByteArray, QByteArray>& contentTypes)
{
KoXmlElement typesEl(contentTypesXML.documentElement());
if (!checkTag(typesEl, "Types", "documentElement")) {
return KoFilter::WrongFormat;
}
if (!checkNsUri(typesEl, Schemas::contentTypes)) {
return KoFilter::WrongFormat;
}
KoXmlElement e;
forEachElement(e, typesEl) {
const QString tagName(e.tagName());
if (!checkNsUri(e, Schemas::contentTypes)) {
return KoFilter::WrongFormat;
}
if (tagName == "Override") {
//ContentType -> PartName mapping
const QByteArray atrPartName(e.attribute("PartName").toLatin1());
const QByteArray atrContentType(e.attribute("ContentType").toLatin1());
if (atrPartName.isEmpty() || atrContentType.isEmpty()) {
kWarning() << "Invalid data for" << tagName
<< "element: PartName=" << atrPartName << "ContentType=" << atrContentType;
return KoFilter::WrongFormat;
}
//kDebug() << atrContentType << "->" << atrPartName;
contentTypes.insert(atrContentType, atrPartName);
} else if (tagName == "Default") {
//! @todo
// skip for now...
}
}
return KoFilter::OK;
}
KoFilter::ConversionStatus Utils::loadDocumentProperties(const KoXmlDocument& appXML, QMap<QString, QVariant>& properties)
{
KoXmlElement typesEl(appXML.documentElement());
KoXmlElement e, elem, element;
forEachElement(element, typesEl) {
QVariant v;
forEachElement(elem, element) {
if(elem.tagName() == "vector") {
QVariantList list;
forEachElement(e, elem)
list.append(e.text());
v = list;
}
}
if(!v.isValid())
v = element.text();
properties[element.tagName()] = v;
}
return KoFilter::OK;
}
bool Utils::ST_Lang_to_languageAndCountry(const QString& value, QString& language, QString& country)
{
int indexForCountry = value.indexOf('-');
if (indexForCountry <= 0)
return false;
indexForCountry++;
language = value.left(indexForCountry - 1);
country = value.mid(indexForCountry);
return !country.isEmpty();
}
class ST_HighlightColorMapping : public QHash<QString, QColor>
{
public:
ST_HighlightColorMapping() {
#define INSERT_HC(c, hex) insert(QLatin1String(c), QColor( QRgb( 0xff000000 | hex ) ) )
INSERT_HC("black", 0x000000);
INSERT_HC("blue", 0x0000ff);
INSERT_HC("cyan", 0x00ffff);
INSERT_HC("darkBlue", 0x000080);
INSERT_HC("darkCyan", 0x008080);
INSERT_HC("darkGray", 0x808080);
INSERT_HC("darkGreen", 0x008000);
INSERT_HC("darkMagenta", 0x800080);
INSERT_HC("darkRed", 0x800000);
INSERT_HC("darkYellow", 0x808000);
INSERT_HC("green", 0x00ff00);
INSERT_HC("lightGray", 0xc0c0c0);
INSERT_HC("magenta", 0xff00ff);
INSERT_HC("red", 0xff0000);
INSERT_HC("yellow", 0xffff00);
INSERT_HC("white", 0xffffff);
#undef INSERT_HC
}
};
QBrush Utils::ST_HighlightColor_to_QColor(const QString& colorName)
{
K_GLOBAL_STATIC(ST_HighlightColorMapping, s_ST_HighlightColor_to_QColor)
const QColor c(s_ST_HighlightColor_to_QColor->value(colorName));
if (c.isValid())
return QBrush(c);
return QBrush(); // for "none" or anything unsupported
}
class DefaultIndexedColors : public QList< QColor >
{
public:
DefaultIndexedColors()
{
push_back( QColor( 0, 0, 0 ) );
push_back( QColor( 255, 255, 255 ) );
push_back( QColor( 255, 0, 0 ) );
push_back( QColor( 0, 255, 0 ) );
push_back( QColor( 0, 0, 255 ) );
push_back( QColor( 255, 255, 0 ) );
push_back( QColor( 255, 0, 255 ) );
push_back( QColor( 0, 255, 255 ) );
push_back( QColor( 0, 0, 0 ) );
push_back( QColor( 255, 255, 255 ) );
push_back( QColor( 255, 0, 0 ) );
push_back( QColor( 0, 255, 0 ) );
push_back( QColor( 0, 0, 255 ) );
push_back( QColor( 255, 255, 0 ) );
push_back( QColor( 255, 0, 255 ) );
push_back( QColor( 0, 255, 255 ) );
push_back( QColor( 128, 0, 0 ) );
push_back( QColor( 0, 128, 0 ) );
push_back( QColor( 0, 0, 128 ) );
push_back( QColor( 128, 128, 0 ) );
push_back( QColor( 128, 0, 128 ) );
push_back( QColor( 0, 128, 128 ) );
push_back( QColor( 192, 192, 192 ) );
push_back( QColor( 128, 128, 128 ) );
push_back( QColor( 153, 153, 255 ) );
push_back( QColor( 153, 51, 102 ) );
push_back( QColor( 255, 255, 204 ) );
push_back( QColor( 204, 255, 255 ) );
push_back( QColor( 102, 0, 102 ) );
push_back( QColor( 255, 128, 128 ) );
push_back( QColor( 0, 102, 204 ) );
push_back( QColor( 204, 204, 255 ) );
push_back( QColor( 0, 0, 128 ) );
push_back( QColor( 255, 0, 255 ) );
push_back( QColor( 255, 255, 0 ) );
push_back( QColor( 0, 255, 255 ) );
push_back( QColor( 128, 0, 128 ) );
push_back( QColor( 128, 0, 0 ) );
push_back( QColor( 0, 128, 128 ) );
push_back( QColor( 0, 0, 255 ) );
push_back( QColor( 0, 204, 255 ) );
push_back( QColor( 204, 255, 255 ) );
push_back( QColor( 204, 255, 204 ) );
push_back( QColor( 255, 255, 153 ) );
push_back( QColor( 153, 204, 255 ) );
push_back( QColor( 255, 153, 204 ) );
push_back( QColor( 204, 153, 255 ) );
push_back( QColor( 255, 204, 153 ) );
push_back( QColor( 51, 102, 255 ) );
push_back( QColor( 51, 204, 204 ) );
push_back( QColor( 153, 204, 0 ) );
push_back( QColor( 255, 204, 0 ) );
push_back( QColor( 255, 153, 0 ) );
push_back( QColor( 255, 102, 0 ) );
push_back( QColor( 102, 102, 153 ) );
push_back( QColor( 150, 150, 150 ) );
push_back( QColor( 0, 51, 102 ) );
push_back( QColor( 51, 102, 153 ) );
push_back( QColor( 0, 51, 0 ) );
push_back( QColor( 51, 51, 0 ) );
push_back( QColor( 153, 51, 0 ) );
push_back( QColor( 153, 51, 102 ) );
push_back( QColor( 51, 51, 153 ) );
push_back( QColor( 51, 51, 51 ) );
push_back( QPalette().color( QPalette::Active, QPalette::WindowText ) );
push_back( QPalette().color( QPalette::Active, QPalette::Window ) );
}
};
QColor Utils::defaultIndexedColor( int index )
{
K_GLOBAL_STATIC( DefaultIndexedColors, s_defaultIndexedColors )
if( index < 0 || s_defaultIndexedColors->count() <= index )
return QColor();
return s_defaultIndexedColors->at( index );
}
class LangIdToLocaleMapping : public QMap< int, QString >
{
public:
LangIdToLocaleMapping()
{
#define DEFINELOCALE( ID, CODE ) insert( ID, QLatin1String( CODE ) );
DEFINELOCALE( 0x0436, "af-ZA" ) // Afrikaans South Africa
DEFINELOCALE( 0x041c, "sq-AL" ) // Albanian Albania
DEFINELOCALE( 0x0484, "gsw-FR" ) // Alsatian France
DEFINELOCALE( 0x045e, "am-ET" ) // Amharic Ethiopia
DEFINELOCALE( 0x0401, "ar-SA" ) // Arabic Saudi Arabia
DEFINELOCALE( 0x0801, "ar-IQ" ) // Arabic Iraq
DEFINELOCALE( 0x0c01, "ar-EG" ) // Arabic Egypt
DEFINELOCALE( 0x1001, "ar-LY" ) // Arabic Libya
DEFINELOCALE( 0x1401, "ar-DZ" ) // Arabic Algeria
DEFINELOCALE( 0x1801, "ar-MA" ) // Arabic Morocco
DEFINELOCALE( 0x1c01, "ar-TN" ) // Arabic Tunisia
DEFINELOCALE( 0x2001, "ar-OM" ) // Arabic Oman
DEFINELOCALE( 0x2401, "ar-YE" ) // Arabic Yemen
DEFINELOCALE( 0x2801, "ar-SY" ) // Arabic Syria
DEFINELOCALE( 0x2c01, "ar-JO" ) // Arabic Jordan
DEFINELOCALE( 0x3001, "ar-LB" ) // Arabic Lebanon
DEFINELOCALE( 0x3401, "ar-KW" ) // Arabic Kuwait
DEFINELOCALE( 0x3801, "ar-AE" ) // Arabic U.A.E.
DEFINELOCALE( 0x3c01, "ar-BH" ) // Arabic Bahrain
DEFINELOCALE( 0x4001, "ar-QA" ) // Arabic Qatar
DEFINELOCALE( 0x042b, "hy-AM" ) // Armenian Armenia
DEFINELOCALE( 0x044d, "as-IN" ) // Assamese India
DEFINELOCALE( 0x082c, "az-Cyrl-AZ" ) // Azeri (Cyrillic) Azerbaijan
DEFINELOCALE( 0x042c, "az-Latn-AZ" ) // Azeri (Latin) Azerbaijan
DEFINELOCALE( 0x046d, "ba-RU" ) // Bashkir Russia
DEFINELOCALE( 0x042d, "eu-ES" ) // Basque Basque
DEFINELOCALE( 0x0423, "be-BY" ) // Belarusian Belarus
DEFINELOCALE( 0x0445, "bn-IN" ) // Bengali India
DEFINELOCALE( 0x0845, "bn-BD" ) // Bengali Bangladesh
DEFINELOCALE( 0x201a, "bs-Cyrl-BA" ) // Bosnian (Cyrillic) Bosnia and Herzegovina
DEFINELOCALE( 0x141a, "bs-Latn-BA" ) // Bosnian (Latin) Bosnia and Herzegovina
DEFINELOCALE( 0x047e, "br-FR" ) // Breton France
DEFINELOCALE( 0x0402, "bg-BG" ) // Bulgarian Bulgaria
DEFINELOCALE( 0x0403, "ca-ES" ) // Catalan Catalan
DEFINELOCALE( 0x0404, "zh-TW" ) // Chinese Taiwan
DEFINELOCALE( 0x0804, "zh-CN" ) // Chinese PRC
DEFINELOCALE( 0x0c04, "zh-HK" ) // Chinese Hong Kong SAR
DEFINELOCALE( 0x1004, "zh-SG" ) // Chinese Singapore
DEFINELOCALE( 0x1404, "zh-MO" ) // Chinese Macao SAR
DEFINELOCALE( 0x0483, "co-FR" ) // Corsican France
DEFINELOCALE( 0x041a, "hr-HR" ) // Croatian Croatia
DEFINELOCALE( 0x101a, "hr-BA" ) // Croatian (Latin) Bosnia and Herzegovina
DEFINELOCALE( 0x0405, "cs-CZ" ) // Czech Czech Republic
DEFINELOCALE( 0x0406, "da-DK" ) // Danish Denmark
DEFINELOCALE( 0x048c, "prs-AF" ) // Dari Afghanistan
DEFINELOCALE( 0x0465, "dv-MV" ) // Divehi Maldives
DEFINELOCALE( 0x0813, "nl-BE" ) // Dutch Belgium
DEFINELOCALE( 0x0413, "nl-NL" ) // Dutch Netherlands
DEFINELOCALE( 0x1009, "en-CA" ) // English Canada
DEFINELOCALE( 0x2009, "en-JM" ) // English Jamaica
DEFINELOCALE( 0x2409, "en-029" ) // English Caribbean
DEFINELOCALE( 0x2809, "en-BZ" ) // English Belize
DEFINELOCALE( 0x2c09, "en-TT" ) // English Trinidad
DEFINELOCALE( 0x0809, "en-GB" ) // English United Kingdom
DEFINELOCALE( 0x1809, "en-IE" ) // English Ireland
DEFINELOCALE( 0x4009, "en-IN" ) // English India
DEFINELOCALE( 0x1c09, "en-ZA" ) // English South Africa
DEFINELOCALE( 0x3009, "en-ZW" ) // English Zimbabwe
DEFINELOCALE( 0x0c09, "en-AU" ) // English Australia
DEFINELOCALE( 0x1409, "en-NZ" ) // English New Zealand
DEFINELOCALE( 0x3409, "en-PH" ) // English Philippines
DEFINELOCALE( 0x0409, "en-US" ) // English United States
DEFINELOCALE( 0x4409, "en-MY" ) // English Malaysia
DEFINELOCALE( 0x4809, "en-SG" ) // English Singapore
DEFINELOCALE( 0x0425, "et-EE" ) // Estonian Estonia
DEFINELOCALE( 0x0438, "fo-FO" ) // Faroese Faroe Islands
DEFINELOCALE( 0x0464, "fil-PH" ) // Filipino Philippines
DEFINELOCALE( 0x040b, "fi-FI" ) // Finnish Finland
DEFINELOCALE( 0x0c0c, "fr-CA" ) // French Canada
DEFINELOCALE( 0x040c, "fr-FR" ) // French France
DEFINELOCALE( 0x180c, "fr-MC" ) // French Monaco
DEFINELOCALE( 0x100c, "fr-CH" ) // French Switzerland
DEFINELOCALE( 0x080c, "fr-BE" ) // French Belgium
DEFINELOCALE( 0x140c, "fr-LU" ) // French Luxembourg
DEFINELOCALE( 0x0462, "fy-NL" ) // Frisian Netherlands
DEFINELOCALE( 0x0456, "gl-ES" ) // Galician Galician
DEFINELOCALE( 0x0437, "ka-GE" ) // Georgian Georgia
DEFINELOCALE( 0x0407, "de-DE" ) // German Germany
DEFINELOCALE( 0x0807, "de-CH" ) // German Switzerland
DEFINELOCALE( 0x0c07, "de-AT" ) // German Austria
DEFINELOCALE( 0x1407, "de-LI" ) // German Liechtenstein
DEFINELOCALE( 0x1007, "de-LU" ) // German Luxembourg
DEFINELOCALE( 0x0408, "el-GR" ) // Greek Greece
DEFINELOCALE( 0x046f, "kl-GL" ) // Greenlandic Greenland
DEFINELOCALE( 0x0447, "gu-IN" ) // Gujarati India
DEFINELOCALE( 0x0468, "ha-Latn-NG" ) // Hausa Nigeria
DEFINELOCALE( 0x040d, "he-IL" ) // Hebrew Israel
DEFINELOCALE( 0x0439, "hi-IN" ) // Hindi India
DEFINELOCALE( 0x040e, "hu-HU" ) // Hungarian Hungary
DEFINELOCALE( 0x040f, "is-IS" ) // Icelandic Iceland
DEFINELOCALE( 0x0470, "ig-NG" ) // Igbo Nigeria
DEFINELOCALE( 0x0421, "id-ID" ) // Indonesian Indonesia
DEFINELOCALE( 0x045d, "iu-Cans-CA" ) // Inukitut (Syllabics) Canada
DEFINELOCALE( 0x085d, "iu-Latn-CA" ) // Inuktitut (Latin) Canada
DEFINELOCALE( 0x083c, "ga-IE" ) // Irish Ireland
DEFINELOCALE( 0x0434, "xh-ZA" ) // isiXhosa South Africa
DEFINELOCALE( 0x0435, "zu-ZA" ) // isiZulu South Africa
DEFINELOCALE( 0x0410, "it-IT" ) // Italian Italy
DEFINELOCALE( 0x0810, "it-CH" ) // Italian Switzerland
DEFINELOCALE( 0x0411, "ja-JP" ) // Japanese Japan
DEFINELOCALE( 0x044b, "kn-IN" ) // Kannada India
DEFINELOCALE( 0x043f, "kk-KZ" ) // Kazakh Kazakhstan
DEFINELOCALE( 0x0453, "km-KH" ) // Khmer Cambodia
DEFINELOCALE( 0x0486, "qut-GT" ) // K'iche Guatemala
DEFINELOCALE( 0x0487, "rw-RW" ) // Kinyarwanda Rwanda
DEFINELOCALE( 0x0441, "sw-KE" ) // Kiswahili Kenya
DEFINELOCALE( 0x0457, "kok-IN" ) // Konkani India
DEFINELOCALE( 0x0412, "ko-KR" ) // Korean Korea
DEFINELOCALE( 0x0440, "ky-KG" ) // Kyrgyz Kyrgyzistan
DEFINELOCALE( 0x0454, "lo-LA" ) // Lao Lao P.D.R.
DEFINELOCALE( 0x0426, "lv-LV" ) // Latvian Latvia
DEFINELOCALE( 0x0427, "lt-LT" ) // Lithuanian Lithuania
DEFINELOCALE( 0x082e, "dsb-DE" ) // Lower Sorbian Germany
DEFINELOCALE( 0x046e, "lb-LU" ) // Luxembourgish Luxembourg
DEFINELOCALE( 0x042f, "mk-MK" ) // Macedonian (FYROM) Macedonia (FYROM)
DEFINELOCALE( 0x043e, "ms-MY" ) // Malay Malaysia
DEFINELOCALE( 0x083e, "ms-BN" ) // Malay Brunei Darussalam
DEFINELOCALE( 0x044c, "ml-IN" ) // Malayalam India
DEFINELOCALE( 0x043a, "mt-MT" ) // Maltese Malta
DEFINELOCALE( 0x0481, "mi-NZ" ) // Maori New Zealand
DEFINELOCALE( 0x047a, "arn-CL" ) // Mapudungun Chile
DEFINELOCALE( 0x044e, "mr-IN" ) // Marathi India
DEFINELOCALE( 0x047c, "moh-CA" ) // Mohawk Mohawk
DEFINELOCALE( 0x0450, "mn-MN" ) // Mongolian (Cyrillic) Mongolia
DEFINELOCALE( 0x0850, "mn-Mong-CN" ) // Mongolian (Mongolian) PRC
DEFINELOCALE( 0x0461, "ne-NP" ) // Nepali Nepal
DEFINELOCALE( 0x0414, "nb-NO" ) // Norwegian (Bokmål) Norway
DEFINELOCALE( 0x0814, "nn-NO" ) // Norwegian (Nynorsk) Norway
DEFINELOCALE( 0x0482, "oc-FR" ) // Occitan France
DEFINELOCALE( 0x0448, "or-IN" ) // Oriya India
DEFINELOCALE( 0x0463, "ps-AF" ) // Pashto Afghanistan
DEFINELOCALE( 0x0429, "fa-IR" ) // Persian Iran
DEFINELOCALE( 0x0415, "pl-PL" ) // Polish Poland
DEFINELOCALE( 0x0416, "pt-BR" ) // Portuguese Brazil
DEFINELOCALE( 0x0816, "pt-PT" ) // Portuguese Portugal
DEFINELOCALE( 0x0446, "pa-IN" ) // Punjabi (Gurmukhi) India
DEFINELOCALE( 0x046b, "quz-BO" ) // Quechua Bolivia
DEFINELOCALE( 0x086b, "quz-EC" ) // Quechua Ecuador
DEFINELOCALE( 0x0c6b, "quz-PE" ) // Quechua Peru
DEFINELOCALE( 0x0418, "ro-RO" ) // Romanian Romania
DEFINELOCALE( 0x0417, "rm-CH" ) // Romansh Switzerland
DEFINELOCALE( 0x0419, "ru-RU" ) // Russian Russia
DEFINELOCALE( 0x243b, "smn-FI" ) // Sami, Inari Finland
DEFINELOCALE( 0x143b, "smj-SE" ) // Sami, Lule Sweden
DEFINELOCALE( 0x103b, "smj-NO" ) // Sami, Lule Norway
DEFINELOCALE( 0x043b, "se-NO" ) // Sami, Northern Norway
DEFINELOCALE( 0x083b, "se-SE" ) // Sami, Northern Sweden
DEFINELOCALE( 0x0c3b, "se-FI" ) // Sami, Northern Finland
DEFINELOCALE( 0x203b, "sms-FI" ) // Sami, Skolt Finland
DEFINELOCALE( 0x183b, "sma-NO" ) // Sami, Southern Norway
DEFINELOCALE( 0x1c3b, "sma-SE" ) // Sami, Southern Sweden
DEFINELOCALE( 0x044f, "sa-IN" ) // Sanskrit India
DEFINELOCALE( 0x0c1a, "sr-Cyrl-CS" ) // Serbian (Cyrillic) Serbia
DEFINELOCALE( 0x1c1a, "sr-Cyrl-BA" ) // Serbian (Cyrillic) Bosnia and Herzegovina
DEFINELOCALE( 0x081a, "sr-Latn-CS" ) // Serbian (Latin) Serbia
DEFINELOCALE( 0x181a, "sr-Latn-BA" ) // Serbian (Latin) Bosnia and Herzegovina
DEFINELOCALE( 0x046c, "nso-ZA" ) // Sesotho sa Leboa South Africa
DEFINELOCALE( 0x0432, "tn-ZA" ) // Setswana South Africa
DEFINELOCALE( 0x045b, "si-LK" ) // Sinhala Sri Lanka
DEFINELOCALE( 0x041b, "sk-SK" ) // Slovak Slovakia
DEFINELOCALE( 0x0424, "sl-SI" ) // Slovenian Slovenia
DEFINELOCALE( 0x080a, "es-MX" ) // Spanish Mexico
DEFINELOCALE( 0x100a, "es-GT" ) // Spanish Guatemala
DEFINELOCALE( 0x140a, "es-CR" ) // Spanish Costa Rica
DEFINELOCALE( 0x180a, "es-PA" ) // Spanish Panama
DEFINELOCALE( 0x1c0a, "es-DO" ) // Spanish Dominican Republic
DEFINELOCALE( 0x200a, "es-VE" ) // Spanish Venezuela
DEFINELOCALE( 0x240a, "es-CO" ) // Spanish Colombia
DEFINELOCALE( 0x280a, "es-PE" ) // Spanish Peru
DEFINELOCALE( 0x2c0a, "es-AR" ) // Spanish Argentina
DEFINELOCALE( 0x300a, "es-EC" ) // Spanish Ecuador
DEFINELOCALE( 0x340a, "es-CL" ) // Spanish Chile
DEFINELOCALE( 0x3c0a, "es-PY" ) // Spanish Paraguay
DEFINELOCALE( 0x400a, "es-BO" ) // Spanish Bolivia
DEFINELOCALE( 0x440a, "es-SV" ) // Spanish El Salvador
DEFINELOCALE( 0x480a, "es-HN" ) // Spanish Honduras
DEFINELOCALE( 0x4c0a, "es-NI" ) // Spanish Nicaragua
DEFINELOCALE( 0x500a, "es-PR" ) // Spanish Puerto Rico
DEFINELOCALE( 0x540a, "es-US" ) // Spanish United States
DEFINELOCALE( 0x380a, "es-UY" ) // Spanish Uruguay
DEFINELOCALE( 0x0c0a, "es-ES" ) // Spanish (International Sort) Spain
DEFINELOCALE( 0x040a, "es-ES_tradnl" ) // Spanish (Traditional Sort) Spain
DEFINELOCALE( 0x041d, "sv-SE" ) // Swedish Sweden
DEFINELOCALE( 0x081d, "sv-FI" ) // Swedish Finland
DEFINELOCALE( 0x045a, "syr-SY" ) // Syriac Syria
DEFINELOCALE( 0x0428, "tg-Cyrl-TJ" ) // Tajik Tajikistan
DEFINELOCALE( 0x085f, "tzm-Latn-DZ" ) // Tamazight (Latin) Algeria
DEFINELOCALE( 0x0449, "ta-IN" ) // Tamil India
DEFINELOCALE( 0x0444, "tt-RU" ) // Tatar Russia
DEFINELOCALE( 0x044a, "te-IN" ) // Telugu India
DEFINELOCALE( 0x041e, "th-TH" ) // Thai Thailand
DEFINELOCALE( 0x0451, "bo-CN" ) // Tibetan PRC
DEFINELOCALE( 0x041f, "tr-TR" ) // Turkish Turkey
DEFINELOCALE( 0x0442, "tk-TM" ) // Turkmen Turkmenistan
DEFINELOCALE( 0x0480, "ug-CN" ) // Uighur PRC
DEFINELOCALE( 0x0422, "uk-UA" ) // Ukrainian Ukraine
DEFINELOCALE( 0x042e, "wen-DE" ) // Upper Sorbian Germany
DEFINELOCALE( 0x0420, "ur-PK" ) // Urdu Pakistan
DEFINELOCALE( 0x0843, "uz-Cyrl-UZ" ) // Uzbek (Cyrillic) Uzbekistan
DEFINELOCALE( 0x0443, "uz-Latn-UZ" ) // Uzbek (Latin) Uzbekistan
DEFINELOCALE( 0x042a, "vi-VN" ) // Vietnamese Vietnam
DEFINELOCALE( 0x0452, "cy-GB" ) // Welsh United Kingdom
DEFINELOCALE( 0x0488, "wo-SN" ) // Wolof Senegal
DEFINELOCALE( 0x0485, "sah-RU" ) // Yakut Russia
DEFINELOCALE( 0x0478, "ii-CN" ) // Yi PRC
DEFINELOCALE( 0x046a, "yo-NG" ) // Yoruba Nigeria
#undef DEFINELOCALE
}
};
QLocale Utils::localeForLangId( int langid )
{
K_GLOBAL_STATIC( LangIdToLocaleMapping, s_LangIdToLocaleMapping )
return QLocale( s_LangIdToLocaleMapping->value( langid ) );
}
qreal Utils::ST_Percentage_to_double(const QString& val, bool& ok)
{
if (!val.endsWith('%')) {
ok = false;
return 0.0;
}
QString result(val);
result.truncate(1);
return result.toDouble(&ok);
}
qreal Utils::ST_Percentage_withMsooxmlFix_to_double(const QString& val, bool& ok)
{
const qreal result = ST_Percentage_to_double(val, ok);
if (ok)
return result;
// MSOOXML fix: the format is int({ST_Percentage}*1000)
const int resultInt = val.toInt(&ok);
if (!ok)
return 0.0;
return qreal(resultInt) / 1000.0;
}
QColor Utils::colorForLuminance(const QColor& color, const DoubleModifier& modulation, const DoubleModifier& offset)
{
if (modulation.valid) {
int r, g, b;
color.getRgb(&r, &g, &b);
if (offset.valid) {
return QColor(
int(floor((255 - r) * (100.0 - modulation.value) / 100.0 + r)),
int(floor((255 - g) * offset.value / 100.0 + g)),
int(floor((255 - b) * offset.value / 100.0 + b)),
color.alpha());
} else {
return QColor(
int(floor(r * modulation.value / 100.0)),
int(floor(g * modulation.value / 100.0)),
int(floor(b * modulation.value / 100.0)),
color.alpha());
}
}
return color;
}
KOMSOOXML_EXPORT void Utils::modifyColor(QColor& color, qreal tint, qreal shade, qreal satMod)
{
int red = color.red();
int green = color.green();
int blue = color.blue();
if (tint > 0) {
red = tint * red + (1 - tint) * 255;
green = tint * green + (1 - tint) * 255;
blue = tint * blue + (1 - tint) * 255;
}
if (shade > 0) {
red = shade * red;
green = shade * green;
blue = shade * blue;
}
// FIXME: This calculation for sure is incorrect,
// According to MS forums, RGB should first be converted to linear RGB
// Then to HSL and then multiply saturation value by satMod
// SatMod can be for example 3.5 so converting RGB -> HSL is not an option
// ADD INFO: MS document does not say that when calculating TINT and SHADE
// That whether one should use normal RGB or linear RGB, check it!
// This method is used temporarily, it seems to produce visually better results than the lower one.
if (satMod > 0) {
QColor temp = QColor(red, green, blue);
qreal saturationFromFull = 1.0 - temp.saturationF();
temp = QColor::fromHsvF(temp.hueF(), temp.saturationF() + saturationFromFull / 10 * satMod, temp.valueF());
red = temp.red();
green = temp.green();
blue = temp.blue();
}
/*
if (satMod > 0) {
red = red * satMod;
green = green * satMod;
blue = blue * satMod;
if (red > 255) {
red = 255;
}
if (green > 255) {
green = 255;
}
if (blue > 255) {
blue = 255;
}
}
*/
color = QColor(red, green, blue);
}
class ST_PlaceholderType_to_ODFMapping : public QHash<QByteArray, QByteArray>
{
public:
ST_PlaceholderType_to_ODFMapping() {
insert("body", "outline");
insert("chart", "chart");
insert("clipArt", "graphic");
insert("ctrTitle", "title");
//! @todo dgm->orgchart?
insert("dgm", "orgchart");
insert("dt", "date-time");
insert("ftr", "footer");
insert("hdr", "header");
//! @todo media->object?
insert("media", "object");
insert("obj", "object");
insert("pic", "graphic");
//! @todo sldImg->graphic?
insert("sldImg", "graphic");
insert("sldNum", "page-number");
insert("subTitle", "subtitle");
insert("tbl", "table");
insert("title", "title");
}
};
QString Utils::ST_PlaceholderType_to_ODF(const QString& ecmaType)
{
K_GLOBAL_STATIC(ST_PlaceholderType_to_ODFMapping, s_ST_PlaceholderType_to_ODF)
QHash<QByteArray, QByteArray>::ConstIterator it(s_ST_PlaceholderType_to_ODF->constFind(ecmaType.toLatin1()));
if (it == s_ST_PlaceholderType_to_ODF->constEnd())
return QLatin1String("text");
return QString(it.value());
}
//! Mapping for handling u element, used in setupUnderLineStyle()
struct UnderlineStyle {
UnderlineStyle(
KoCharacterStyle::LineStyle style_,
KoCharacterStyle::LineType type_,
KoCharacterStyle::LineWeight weight_,
KoCharacterStyle::LineMode mode_ = KoCharacterStyle::ContinuousLineMode)
: style(style_), type(type_), weight(weight_), mode(mode_) {
}
KoCharacterStyle::LineStyle style;
KoCharacterStyle::LineType type;
KoCharacterStyle::LineWeight weight;
KoCharacterStyle::LineMode mode;
};
typedef QHash<QByteArray, UnderlineStyle*> UnderlineStylesHashBase;
class UnderlineStylesHash : public UnderlineStylesHashBase
{
public:
UnderlineStylesHash() {
// default:
insert("-",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
// 17.18.99 ST_Underline (Underline Patterns), WML ECMA-376 p.1681:
insert("single",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("double",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::DoubleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("dbl",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::DoubleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("words",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight, KoCharacterStyle::SkipWhiteSpaceLineMode)
);
insert("thick",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::BoldLineWeight)
);
insert("dash",
new UnderlineStyle(KoCharacterStyle::DashLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("dashDotHeavy",
new UnderlineStyle(KoCharacterStyle::DotDashLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::BoldLineWeight)
);
insert("dotted",
new UnderlineStyle(KoCharacterStyle::DottedLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("dotDash",
new UnderlineStyle(KoCharacterStyle::DotDashLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("dotDotDash",
new UnderlineStyle(KoCharacterStyle::DotDotDashLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("wave",
new UnderlineStyle(KoCharacterStyle::WaveLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("wavyDouble",
new UnderlineStyle(KoCharacterStyle::WaveLine, KoCharacterStyle::DoubleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("wavyDbl",
new UnderlineStyle(KoCharacterStyle::WaveLine, KoCharacterStyle::DoubleLine,
KoCharacterStyle::AutoLineWeight)
);
insert("wavyHeavy",
new UnderlineStyle(KoCharacterStyle::WaveLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::BoldLineWeight)
);
//! @todo more styles
// 20.1.10.82 ST_TextUnderlineType (Text Underline Types), DrawingML ECMA-376 p.3450:
insert("none",
new UnderlineStyle(KoCharacterStyle::NoLineStyle, KoCharacterStyle::NoLineType,
KoCharacterStyle::AutoLineWeight)
);
insert("sng",
new UnderlineStyle(KoCharacterStyle::SolidLine, KoCharacterStyle::SingleLine,
KoCharacterStyle::AutoLineWeight)
);
//! @todo more styles
}
~UnderlineStylesHash() {
qDeleteAll(*this);
}
void setup(const QString& msooxmlName,
KoCharacterStyle* textStyleProperties) {
UnderlineStyle* style = value(msooxmlName.toLatin1());
if (!style)
style = value("-");
textStyleProperties->setUnderlineStyle(style->style);
// add style:text-underline-type if it is not "single"
if (KoCharacterStyle::SingleLine != style->type) {
textStyleProperties->setUnderlineType(style->type);
}
textStyleProperties->setUnderlineWidth(style->weight, 1.0);
// add style:text-underline-mode if it is not "continuous"
if (KoCharacterStyle::ContinuousLineMode != style->mode) {
textStyleProperties->setUnderlineMode(style->mode);
}
}
};
void Utils::rotateString(const qreal rotation, const qreal width, const qreal height, qreal& angle, qreal& xDiff, qreal& yDiff)
{
angle = -(qreal)rotation * ((qreal)(M_PI) / (qreal)180.0)/ (qreal)60000.0;
//position change is calculated based on the fact that center point stays in the same location
// Width/2 = Xnew + cos(angle)*Width/2 - sin(angle)*Height/2
// Height/2 = Ynew + sin(angle)*Width/2 + cos(angle)*Height/2
xDiff = width/2 - cos(-angle)*width/2 + sin(-angle)*height/2;
yDiff = height/2 - sin(-angle)*width/2 - cos(-angle)*height/2;
}
void Utils::setupUnderLineStyle(const QString& msooxmlName, KoCharacterStyle* textStyleProperties)
{
K_GLOBAL_STATIC(UnderlineStylesHash, s_underLineStyles)
s_underLineStyles->setup(msooxmlName, textStyleProperties);
}
//-----------------------------------------
// Marker styles
//-----------------------------------------
namespace
{
static const char* const markerStyles[6] = {
"", "msArrowEnd_20_5", "msArrowStealthEnd_20_5", "msArrowDiamondEnd_20_5",
"msArrowOvalEnd_20_5", "msArrowOpenEnd_20_5"
};
// trying to maintain compatibility with libmso
enum MSOLINEEND_CUSTOM {
msolineNoEnd,
msolineArrowEnd,
msolineArrowStealthEnd,
msolineArrowDiamondEnd,
msolineArrowOvalEnd,
msolineArrowOpenEnd
};
}
QString Utils::defineMarkerStyle(KoGenStyles& mainStyles, const QString& type)
{
uint id;
if (type == "arrow") {
id = msolineArrowOpenEnd;
} else if (type == "stealth") {
id = msolineArrowStealthEnd;
} else if (type == "diamond") {
id = msolineArrowDiamondEnd;
} else if (type == "oval") {
id = msolineArrowOvalEnd;
} else if (type == "triangle") {
id = msolineArrowEnd;
} else {
return QString();
}
const QString name(markerStyles[id]);
if (mainStyles.style(name, "")) {
return name;
}
KoGenStyle marker(KoGenStyle::MarkerStyle);
marker.addAttribute("draw:display-name", QString(markerStyles[id]).replace("_20_", " "));
// sync with LO
switch (id) {
case msolineArrowStealthEnd:
marker.addAttribute("svg:viewBox", "0 0 318 318");
marker.addAttribute("svg:d", "m159 0 159 318-159-127-159 127z");
break;
case msolineArrowDiamondEnd:
marker.addAttribute("svg:viewBox", "0 0 318 318");
marker.addAttribute("svg:d", "m159 0 159 159-159 159-159-159z");
break;
case msolineArrowOvalEnd:
marker.addAttribute("svg:viewBox", "0 0 318 318");
marker.addAttribute("svg:d", "m318 0c0-87-72-159-159-159s-159 72-159 159 72 159 159 159 159-72 159-159z");
break;
case msolineArrowOpenEnd:
marker.addAttribute("svg:viewBox", "0 0 477 477");
marker.addAttribute("svg:d", "m239 0 238 434-72 43-166-305-167 305-72-43z");
break;
case msolineArrowEnd:
default:
marker.addAttribute("svg:viewBox", "0 0 318 318");
marker.addAttribute("svg:d", "m159 0 159 318h-318z");
break;
}
return mainStyles.insert(marker, name, KoGenStyles::DontAddNumberToName);
}
qreal Utils::defineMarkerWidth(const QString &markerWidth, const qreal lineWidth)
{
int c = 0;
if (markerWidth == "lg") {
c = 3;
} else if (markerWidth == "med" || markerWidth.isEmpty()) {
c = 2; //MSOOXML default = "med"
} else if (markerWidth == "sm") {
c = 1;
}
return ( lineWidth * c );
}
//-----------------------------------------
// XmlWriteBuffer
//-----------------------------------------
Utils::XmlWriteBuffer::XmlWriteBuffer()
: m_origWriter(0), m_newWriter(0)
{
}
Utils::XmlWriteBuffer::~XmlWriteBuffer()
{
releaseWriterInternal();
}
KoXmlWriter* Utils::XmlWriteBuffer::setWriter(KoXmlWriter* writer)
{
Q_ASSERT(!m_origWriter && !m_newWriter);
if (m_origWriter || m_newWriter) {
return 0;
}
m_origWriter = writer; // remember
m_newWriter = new KoXmlWriter(&m_buffer, m_origWriter->indentLevel() + 1);
return m_newWriter;
}
KoXmlWriter* Utils::XmlWriteBuffer::releaseWriter()
{
Q_ASSERT(m_newWriter && m_origWriter);
if (!m_newWriter || !m_origWriter) {
return 0;
}
m_origWriter->addCompleteElement(&m_buffer);
return releaseWriterInternal();
}
KoXmlWriter* Utils::XmlWriteBuffer::releaseWriter(QString& bkpXmlSnippet)
{
Q_ASSERT(m_newWriter && m_origWriter);
if (!m_newWriter || !m_origWriter) {
return 0;
}
bkpXmlSnippet = QString::fromUtf8(m_buffer.buffer(), m_buffer.buffer().size());
return releaseWriterInternal();
}
KoXmlWriter* Utils::XmlWriteBuffer::releaseWriterInternal()
{
if (!m_newWriter || !m_origWriter) {
return 0;
}
delete m_newWriter;
m_newWriter = 0;
KoXmlWriter* tmp = m_origWriter;
m_origWriter = 0;
return tmp;
}
void Utils::XmlWriteBuffer::clear()
{
delete m_newWriter;
m_newWriter = 0;
m_origWriter = 0;
}
QString Utils::columnName(uint column)
{
uint digits = 1;
uint offset = 0;
for (uint limit = 26; column >= limit + offset; limit *= 26, digits++)
offset += limit;
QString str;
for (uint col = column - offset; digits > 0; --digits, col /= 26)
str.prepend(QChar('A' + (col % 26)));
return str;
}
void Utils::splitPathAndFile(const QString& pathAndFile, QString* path, QString* file)
{
Q_ASSERT(path);
Q_ASSERT(file);
*path = pathAndFile.left(pathAndFile.lastIndexOf('/'));
*file = pathAndFile.mid(pathAndFile.lastIndexOf('/') + 1);
}
// <units> -------------------
QString Utils::EMU_to_ODF(const QString& twipValue)
{
if (twipValue.isEmpty())
return QLatin1String("0cm");
bool ok;
const int emu = twipValue.toInt(&ok);
if (!ok)
return QString();
if (emu == 0)
return QLatin1String("0cm");
return EMU_TO_CM_STRING(emu);
}
QString Utils::TWIP_to_ODF(const QString& twipValue)
{
if (twipValue.isEmpty())
return QLatin1String("0cm");
bool ok;
const int twip = twipValue.toInt(&ok);
if (!ok)
return QString();
if (twip == 0)
return QLatin1String("0cm");
return cmString(TWIP_TO_CM(qreal(twip)));
}
QString Utils::ST_EighthPointMeasure_to_ODF(const QString& value)
{
if (value.isEmpty())
return QString();
bool ok;
const qreal point = qreal(value.toFloat(&ok)) / 8.0;
if (!ok)
return QString();
return QString::number(point, 'g', 2) + QLatin1String("pt");
}
//! @return true if @a string is non-negative integer number
static bool isPositiveIntegerNumber(const QString& string)
{
for (const QChar *c = string.constData(); !c->isNull(); c++) {
if (!c->isNumber())
return false;
}
return !string.isEmpty();
}
//! Splits number and unit
static bool splitNumberAndUnit(const QString& _string, qreal *number, QString* unit)
{
int unitIndex = 0;
QString string(_string);
for (const QChar *c = string.constData(); !c->isNull(); c++, unitIndex++) {
if (!c->isNumber() && *c != '.')
break;
}
*unit = string.mid(unitIndex);
string.truncate(unitIndex);
if (string.isEmpty()) {
kWarning() << "No unit found in" << _string;
return false;
}
bool ok;
*number = string.toFloat(&ok);
if (!ok)
kWarning() << "Invalid number in" << _string;
return ok;
}
//! @return true is @a unit is one of these mentioned in 22.9.2.15 ST_UniversalMeasure (Universal Measurement)
static bool isUnitAcceptable(const QString& unit)
{
if (unit.length() != 2)
return false;
return unit == QString::fromLatin1("cm")
|| unit == QString::fromLatin1("mm")
|| unit == QString::fromLatin1("in")
|| unit == QString::fromLatin1("pt")
|| unit == QString::fromLatin1("pc")
|| unit == QString::fromLatin1("pi");
}
static QString ST_TwipsMeasure_to_ODF_with_unit(const QString& value,
qreal (*convertFromTwips)(qreal), const char* unit)
{
if (value.isEmpty())
return QString();
if (isPositiveIntegerNumber(value)) {
// a positive number in twips (twentieths of a point, equivalent to 1/1440th of an inch)
bool ok;
const qreal point = convertFromTwips( qreal(value.toFloat(&ok)) );
if (!ok)
return QString();
return QString::number(point, 'g', 2) + QLatin1String(unit);
}
return Utils::ST_PositiveUniversalMeasure_to_ODF(value);
}
qreal twipToPt(qreal v)
{
return TWIP_TO_POINT(v);
}
KOMSOOXML_EXPORT QString Utils::ST_TwipsMeasure_to_pt(const QString& value)
{
return ST_TwipsMeasure_to_ODF_with_unit(value, twipToPt, "pt");
}
qreal twipToCm(qreal v)
{
return TWIP_TO_CM(v);
}
KOMSOOXML_EXPORT QString Utils::ST_TwipsMeasure_to_cm(const QString& value)
{
return ST_TwipsMeasure_to_ODF_with_unit(value, twipToCm, "cm");
}
KOMSOOXML_EXPORT QString Utils::ST_PositiveUniversalMeasure_to_ODF(const QString& value)
{
// a positive decimal number immediately following by a unit identifier.
qreal number(0.0);
QString unit;
if (!splitNumberAndUnit(value, &number, &unit))
return QString();
// special case: pc is another name for pica
if (unit == QString::fromLatin1("pc")) {
return QString::number(number) + QLatin1String("pi");
}
if (!isUnitAcceptable(unit)) {
kWarning() << "Unit" << unit << "not supported. Expected cm/mm/in/pt/pc/pi.";
return QString();
}
return value; // the original is OK
}
KOMSOOXML_EXPORT QString Utils::ST_PositiveUniversalMeasure_to_cm(const QString& value)
{
QString v(ST_PositiveUniversalMeasure_to_ODF(value));
if (v.isEmpty())
return QString();
return cmString(POINT_TO_CM(KoUnit::parseValue(v)));
}
// </units> -------------------
Utils::ParagraphBulletProperties::ParagraphBulletProperties()
{
clear();
}
void Utils::ParagraphBulletProperties::clear()
{
m_level = -1;
m_type = ParagraphBulletProperties::DefaultType;
m_startValue = "1"; //ECMA-376, p.4575
m_bulletFont = UNUSED;
m_bulletChar = UNUSED;
m_numFormat = UNUSED;
m_prefix = UNUSED;
m_suffix = UNUSED;
m_align = UNUSED;
m_indent = UNUSED;
m_margin = UNUSED;
m_picturePath = UNUSED;
m_bulletColor = UNUSED;
m_followingChar = UNUSED;
m_bulletRelativeSize = UNUSED;
m_bulletSize = UNUSED;
m_startOverride = false;
}
bool Utils::ParagraphBulletProperties::isEmpty() const
{
if (m_type == ParagraphBulletProperties::DefaultType) {
return true;
}
return false;
}
void Utils::ParagraphBulletProperties::setAlign(const QString& align)
{
m_align = align;
}
void Utils::ParagraphBulletProperties::setBulletChar(const QString& bulletChar)
{
m_bulletChar = bulletChar;
m_type = ParagraphBulletProperties::BulletType;
}
void Utils::ParagraphBulletProperties::setStartValue(const QString& value)
{
m_startValue = value;
}
void Utils::ParagraphBulletProperties::setMargin(const qreal margin)
{
m_margin = QString("%1").arg(margin);
}
void Utils::ParagraphBulletProperties::setIndent(const qreal indent)
{
m_indent = QString("%1").arg(indent);
}
void Utils::ParagraphBulletProperties::setPrefix(const QString& prefixChar)
{
m_prefix = prefixChar;
}
void Utils::ParagraphBulletProperties::setSuffix(const QString& suffixChar)
{
m_suffix = suffixChar;
}
void Utils::ParagraphBulletProperties::setNumFormat(const QString& numFormat)
{
m_numFormat = numFormat;
m_type = ParagraphBulletProperties::NumberType;
}
void Utils::ParagraphBulletProperties::setPicturePath(const QString& picturePath)
{
m_picturePath = picturePath;
m_type = ParagraphBulletProperties::PictureType;
}
void Utils::ParagraphBulletProperties::setBulletRelativeSize(const int size)
{
m_bulletRelativeSize = QString("%1").arg(size);
}
void Utils::ParagraphBulletProperties::setBulletSizePt(const qreal size)
{
m_bulletSize = QString("%1").arg(size);
}
void Utils::ParagraphBulletProperties::setBulletFont(const QString& font)
{
m_bulletFont = font;
}
void Utils::ParagraphBulletProperties::setBulletColor(const QString& bulletColor)
{
m_bulletColor = bulletColor;
}
void Utils::ParagraphBulletProperties::setFollowingChar(const QString& followingChar)
{
m_followingChar = followingChar;
}
void Utils::ParagraphBulletProperties::setTextStyle(const KoGenStyle& textStyle)
{
m_textStyle = textStyle;
//m_bulletFont
if (!(m_textStyle.property("fo:font-family")).isEmpty()) {
m_bulletFont = m_textStyle.property("fo:font-family");
}
if (!(m_textStyle.property("style:font-name")).isEmpty()) {
m_bulletFont = m_textStyle.property("style:font-name");
}
//m_bulletColor
if (!(m_textStyle.property("fo:color")).isEmpty()) {
m_bulletColor = m_textStyle.property("fo:color");
}
//m_bulletRelativeSize
//m_bulletSize
if (!m_textStyle.property("fo:font-size").isEmpty()) {
QString bulletSize = m_textStyle.property("fo:font-size");
if (bulletSize.endsWith(QLatin1Char('%'))) {
bulletSize.chop(1);
m_bulletRelativeSize = bulletSize;
} else if (bulletSize.endsWith(QLatin1String("pt"))) {
bulletSize.chop(2);
m_bulletSize = bulletSize;
} else {
kDebug() << "Unit of font-size NOT supported!";
}
}
}
void Utils::ParagraphBulletProperties::setStartOverride(const bool startOverride)
{
m_startOverride = startOverride;
}
QString Utils::ParagraphBulletProperties::startValue() const
{
return m_startValue;
}
QString Utils::ParagraphBulletProperties::bulletColor() const
{
return m_bulletColor;
}
QString Utils::ParagraphBulletProperties::bulletChar() const
{
return m_bulletChar;
}
QString Utils::ParagraphBulletProperties::bulletFont() const
{
return m_bulletFont;
}
QString Utils::ParagraphBulletProperties::margin() const
{
return m_margin;
}
QString Utils::ParagraphBulletProperties::indent() const
{
return m_indent;
}
QString Utils::ParagraphBulletProperties::bulletRelativeSize() const
{
return m_bulletRelativeSize;
}
QString Utils::ParagraphBulletProperties::bulletSizePt() const
{
return m_bulletSize;
}
QString Utils::ParagraphBulletProperties::followingChar() const
{
return m_followingChar;
}
KoGenStyle Utils::ParagraphBulletProperties::textStyle() const
{
return m_textStyle;
}
bool Utils::ParagraphBulletProperties::startOverride() const
{
return m_startOverride;
}
void Utils::ParagraphBulletProperties::addInheritedValues(const ParagraphBulletProperties& properties)
{
// This function is intented for helping to inherit some values from other properties
if (m_level == -1) {
m_level = properties.m_level;
}
if (properties.m_type != ParagraphBulletProperties::DefaultType) {
m_type = properties.m_type;
}
if (properties.m_startValue != "1") {
m_startValue = properties.m_startValue;
}
if (properties.m_bulletFont != UNUSED) {
m_bulletFont = properties.m_bulletFont;
}
if (properties.m_bulletChar != UNUSED) {
m_bulletChar = properties.m_bulletChar;
}
if (properties.m_numFormat != UNUSED) {
m_numFormat = properties.m_numFormat;
}
if (properties.m_prefix != UNUSED) {
m_prefix = properties.m_prefix;
}
if (properties.m_suffix != UNUSED) {
m_suffix = properties.m_suffix;
}
if (properties.m_align != UNUSED) {
m_align = properties.m_align;
}
if (properties.m_indent != UNUSED) {
m_indent = properties.m_indent;
}
if (properties.m_margin != UNUSED) {
m_margin = properties.m_margin;
}
if (properties.m_picturePath != UNUSED) {
m_picturePath = properties.m_picturePath;
}
if (properties.m_bulletColor != UNUSED) {
m_bulletColor = properties.m_bulletColor;
}
if (properties.m_bulletRelativeSize != UNUSED) {
m_bulletRelativeSize = properties.m_bulletRelativeSize;
}
if (properties.m_bulletSize != UNUSED) {
m_bulletSize = properties.m_bulletSize;
}
if (properties.m_followingChar != UNUSED) {
m_followingChar = properties.m_followingChar;
}
if (!(properties.m_textStyle == m_textStyle)) {
KoGenStyle::copyPropertiesFromStyle(properties.m_textStyle, m_textStyle, KoGenStyle::TextType);
}
}
QString Utils::ParagraphBulletProperties::convertToListProperties(KoGenStyles& mainStyles, Utils::MSOOXMLFilter currentFilter)
{
QBuffer buf;
buf.open(QIODevice::WriteOnly);
KoXmlWriter out(&buf);
//---------------------------------------------
// list-level-style-*
//---------------------------------------------
if (m_type == ParagraphBulletProperties::NumberType) {
out.startElement("text:list-level-style-number");
if (m_numFormat != UNUSED) {
out.addAttribute("style:num-format", m_numFormat);
}
if (m_prefix != UNUSED) {
out.addAttribute("style:num-prefix", m_prefix);
}
if (m_suffix != UNUSED) {
out.addAttribute("style:num-suffix", m_suffix);
}
out.addAttribute("text:start-value", m_startValue);
}
else if (m_type == ParagraphBulletProperties::PictureType) {
out.startElement("text:list-level-style-image");
out.addAttribute("xlink:href", m_picturePath);
out.addAttribute("xlink:type", "simple");
out.addAttribute("xlink:show", "embed");
out.addAttribute("xlink:actuate", "onLoad");
}
else {
out.startElement("text:list-level-style-bullet");
if (m_bulletChar.length() != 1) {
// TODO: if there is no bullet char this should not be
// saved as list but as normal paragraph. Both LO and MSO
// do export it just as paragraph and no list until there
// is a fix available that change that we use a Zero Width
// Space to not generate invalid xml
out.addAttribute("text:bullet-char", QChar(0x200B));
} else {
out.addAttribute("text:bullet-char", m_bulletChar);
}
}
out.addAttribute("text:level", m_level);
//---------------------------------------------
// text-properties
//---------------------------------------------
//
// NOTE: Setting a num. of text-properties to default values if
// not provided for the list style to maintain compatibility with
// both ODF and MSOffice.
QString bulletSize;
if (m_bulletRelativeSize != UNUSED) {
bulletSize = QString(m_bulletRelativeSize).append("%");
} else if (m_bulletSize != UNUSED) {
bulletSize = QString(m_bulletSize).append("pt");
} else {
bulletSize = "100%";
}
// MSWord: A label does NOT inherit Underline from text-properties
// of the paragraph style. A bullet does not inherit {Italics, Bold}.
if (currentFilter == Utils::DocxFilter && m_type != ParagraphBulletProperties::PictureType) {
if (m_type != ParagraphBulletProperties::NumberType) {
if ((m_textStyle.property("fo:font-style")).isEmpty()) {
m_textStyle.addProperty("fo:font-style", "normal");
}
if ((m_textStyle.property("fo:font-weight")).isEmpty()) {
m_textStyle.addProperty("fo:font-weight", "normal");
}
}
if ((m_textStyle.property("style:text-underline-style")).isEmpty()) {
m_textStyle.addProperty("style:text-underline-style", "none");
}
//fo:font-size
if ((m_textStyle.property("fo:font-size")).isEmpty()) {
m_textStyle.addProperty("fo:font-size", bulletSize);
}
out.addAttribute("text:style-name", mainStyles.insert(m_textStyle, "T"));
}
//---------------------------------------------
// list-level-properties
//---------------------------------------------
out.startElement("style:list-level-properties");
if (m_align != UNUSED) {
out.addAttribute("fo:text-align", m_align);
}
if ((m_type == ParagraphBulletProperties::PictureType) && (m_bulletSize != UNUSED)) {
QString size = QString(m_bulletSize).append("pt");
out.addAttribute("fo:width", size);
out.addAttribute("fo:height", size);
}
out.addAttribute("text:list-level-position-and-space-mode", "label-alignment");
// NOTE: DrawingML: If indent and marL were not provided by a master slide
// or defaults, then according to the spec. a value of -342900 is implied
// for indent and a value of 347663 is implied for marL (no matter which
// level and which type of text). However the result is not compliant with
// MS PowerPoint => using ZERO values as in the ppt filter.
double margin = 0;
double indent = 0;
bool ok = false;
if (m_margin != UNUSED) {
margin = m_margin.toDouble(&ok);
if (!ok) {
kDebug() << "STRING_TO_DOUBLE: error converting" << m_margin << "(attribute \"marL\")";
}
}
if (m_indent != UNUSED) {
indent = m_indent.toDouble(&ok);
if (!ok) {
kDebug() << "STRING_TO_DOUBLE: error converting" << m_indent << "(attribute \"indent\")";
}
}
out.startElement("style:list-level-label-alignment");
if (currentFilter == Utils::PptxFilter) {
//fo:margin-left
out.addAttributePt("fo:margin-left", margin);
if (((m_type == ParagraphBulletProperties::BulletType) && m_bulletChar.isEmpty()) ||
(m_type == ParagraphBulletProperties::DefaultType))
{
//hanging:
if (indent < 0) {
if (qAbs(indent) > margin) {
out.addAttributePt("fo:text-indent", -margin);
} else {
out.addAttributePt("fo:text-indent", indent);
}
}
//first-line and none:
else {
out.addAttributePt("fo:text-indent", indent);
}
out.addAttribute("text:label-followed-by", "nothing");
} else {
//hanging:
if (indent < 0) {
if (qAbs(indent) > margin) {
out.addAttributePt("fo:text-indent", -margin);
out.addAttribute("text:label-followed-by", "listtab");
out.addAttributePt("text:list-tab-stop-position", qAbs(indent));
} else {
out.addAttributePt("fo:text-indent", indent);
out.addAttribute("text:label-followed-by", "listtab");
out.addAttributePt("text:list-tab-stop-position", margin);
}
}
//first-line:
else if (indent > 0) {
out.addAttribute("fo:text-indent", "0pt");
out.addAttribute("text:label-followed-by", "listtab");
out.addAttributePt("text:list-tab-stop-position", margin + indent);
}
//none
else {
out.addAttribute("fo:text-indent", "0pt");
out.addAttribute("text:label-followed-by", "nothing");
}
}
} else {
//fo:margin-left
out.addAttributePt("fo:margin-left", margin);
//fo:text-indent
out.addAttributePt("fo:text-indent", indent);
//text:label-followed-by
if ((m_followingChar == "tab") || (m_followingChar == UNUSED)) {
out.addAttribute("text:label-followed-by", "listtab");
// Layout hints: none/first-line/hanging are values from the
// Special field of the Paragraph dialog in MS Word.
//
// first-line:
// IF (indent > 0) and (margin > 0), THEN use default tab stop OR a custom tab stop if defined.
// IF (indent > 0) and (margin == 0), THEN use default tab stop OR a custom tab stop if defined.
// IF (indent > 0) and (margin < 0), THEN use default tab stop OR a custom tab stop if defined.
//
// none:
// IF (indent == 0) and (margin > 0), THEN use default tab stop OR a custom tab stop if defined.
// IF (indent == 0) and (margin == 0), THEN use default tab stop OR a custom tab stop if defined.
// IF (indent == 0) and (margin < 0), THEN use default tab stop OR a custom tab stop if defined.
//
// hanging:
// 1. the tab should be placed at the margin position
// 2. bullet_position = margin - indent; (that's the indentation
// left value that can be seen in Paragraph dialog in MS Word)
}
//space and nothing are same in OOXML and ODF
else {
out.addAttribute("text:label-followed-by", m_followingChar);
}
}
out.endElement(); //style:list-level-label-alignment
out.endElement(); //style:list-level-properties
if (currentFilter != Utils::DocxFilter && m_type != ParagraphBulletProperties::PictureType) {
out.startElement("style:text-properties");
if (m_bulletColor != UNUSED) {
out.addAttribute("fo:color", m_bulletColor);
}
out.addAttribute("fo:font-size", bulletSize);
//MSPowerPoint: UI does not enable to change font of a numbered lists.
if (m_bulletFont != UNUSED) {
if ((currentFilter != Utils::PptxFilter) || (m_type == ParagraphBulletProperties::BulletType)) {
out.addAttribute("fo:font-family", m_bulletFont);
}
}
//MSPowerPoint: A label does NOT inherit Underline from text-properties
//of the 1st text chunk. A bullet does NOT inherit {Italics, Bold}.
if (currentFilter == Utils::PptxFilter) {
if (m_type != ParagraphBulletProperties::NumberType) {
out.addAttribute("fo:font-style", "normal");
out.addAttribute("fo:font-weight", "normal");
}
out.addAttribute("style:text-underline-style", "none");
}
out.endElement(); //style:text-properties
}
out.endElement(); //text:list-level-style-*
return QString::fromUtf8(buf.buffer(), buf.buffer().size());
}
|