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
|
/*
Copyright (©) 2003-2025 Teus Benschop.
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 3 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.
*/
#include <odf/text.h>
#include <filter/string.h>
#include <filter/url.h>
#include <filter/archive.h>
#include <filter/image.h>
#include <filter/usfm.h>
#include <database/books.h>
#include <database/config/bible.h>
#include <database/bibleimages.h>
#include <styles/logic.h>
// Class for creating OpenDocument text documents.
// Initially the ODF Toolkit was used.
// But the Java code to generate this became too big for the Java compiler.
// The other thing is that Java is slow compared to the methods below written in C++.
odf_text::odf_text (std::string bible)
{
m_bible = bible;
// Unpack the .odt template.
std::string template_odf = filter_url_create_root_path ({"odf", "template.odt"});
unpacked_odt_folder = filter_archive_unzip (template_odf);
filter_url_rmdir (filter_url_create_path ({unpacked_odt_folder, "Configurations2"}));
// Create the Pictures folder.
// pictures_folder = filter_url_create_path (unpackedOdtFolder, "Pictures");
//filter_url_mkdir (pictures_folder);
initialize_content_xml ();
initialize_styles_xml ();
automatic_note_caller = database::config::bible::get_odt_automatic_note_caller(m_bible);
}
odf_text::~odf_text ()
{
filter_url_rmdir (unpacked_odt_folder);
}
// Build the default content.xml for the template.
void odf_text::initialize_content_xml ()
{
pugi::xml_node rootnode = content_dom.append_child ("office:document-content");
rootnode.append_attribute ("xmlns:office") = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
rootnode.append_attribute ("xmlns:style") = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
rootnode.append_attribute ("xmlns:text") = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
rootnode.append_attribute ("xmlns:table") = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
rootnode.append_attribute ("xmlns:draw") = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
rootnode.append_attribute ("xmlns:fo") = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
rootnode.append_attribute ("xmlns:xlink") = "http://www.w3.org/1999/xlink";
rootnode.append_attribute ("xmlns:dc") = "http://purl.org/dc/elements/1.1/";
rootnode.append_attribute ("xmlns:meta") = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0";
rootnode.append_attribute ("xmlns:number") = "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0";
rootnode.append_attribute ("xmlns:svg") = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0";
rootnode.append_attribute ("xmlns:chart") = "urn:oasis:names:tc:opendocument:xmlns:chart:1.0";
rootnode.append_attribute ("xmlns:dr3d") = "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0";
rootnode.append_attribute ("xmlns:math") = "http://www.w3.org/1998/Math/MathML";
rootnode.append_attribute ("xmlns:form") = "urn:oasis:names:tc:opendocument:xmlns:form:1.0";
rootnode.append_attribute ("xmlns:script") = "urn:oasis:names:tc:opendocument:xmlns:script:1.0";
rootnode.append_attribute ("xmlns:ooo") = "http://openoffice.org/2004/office";
rootnode.append_attribute ("xmlns:ooow") = "http://openoffice.org/2004/writer";
rootnode.append_attribute ("xmlns:oooc") = "http://openoffice.org/2004/calc";
rootnode.append_attribute ("xmlns:dom") = "http://www.w3.org/2001/xml-events";
rootnode.append_attribute ("xmlns:xforms") = "http://www.w3.org/2002/xforms";
rootnode.append_attribute ("xmlns:xsd") = "http://www.w3.org/2001/XMLSchema";
rootnode.append_attribute ("xmlns:xsi") = "http://www.w3.org/2001/XMLSchema-instance";
rootnode.append_attribute ("xmlns:rpt") = "http://openoffice.org/2005/report";
rootnode.append_attribute ("xmlns:of") = "urn:oasis:names:tc:opendocument:xmlns:of:1.2";
rootnode.append_attribute ("xmlns:xhtml") = "http://www.w3.org/1999/xhtml";
rootnode.append_attribute ("xmlns:grddl") = "http://www.w3.org/2003/g/data-view#";
rootnode.append_attribute ("xmlns:tableooo") = "http://openoffice.org/2009/table";
rootnode.append_attribute ("xmlns:field") = "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0";
rootnode.append_attribute ("xmlns:formx") = "urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0";
rootnode.append_attribute ("xmlns:css3t") = "http://www.w3.org/TR/css3-text/";
rootnode.append_attribute ("office:version") = "1.2";
rootnode.append_attribute ("grddl:transformation") = "http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl";
pugi::xml_node office_scripts = rootnode.append_child ("office:scripts");
if (office_scripts) {}
pugi::xml_node office_font_face_decls = rootnode.append_child ("office:font-face-decls");
{
pugi::xml_node childnode;
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Times New Roman";
childnode.append_attribute ("svg:font-family") = "'Times New Roman'";
childnode.append_attribute ("style:font-family-generic") = "roman";
childnode.append_attribute ("style:font-pitch") = "variable";
std::string fontname = database::config::bible::get_export_font (m_bible);
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = fontname.c_str();
fontname.insert (0, "'");
fontname.append ("'");
childnode.append_attribute ("svg:font-family") = fontname.c_str();
childnode.append_attribute ("style:font-family-generic") = "roman";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Arial";
childnode.append_attribute ("svg:font-family") = "Arial";
childnode.append_attribute ("style:font-family-generic") = "swiss";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Droid Sans Fallback";
childnode.append_attribute ("svg:font-family") = "'Droid Sans Fallback'";
childnode.append_attribute ("style:font-family-generic") = "system";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Lohit Hindi";
childnode.append_attribute ("svg:font-family") = "'Lohit Hindi'";
childnode.append_attribute ("style:font-family-generic") = "system";
childnode.append_attribute ("style:font-pitch") = "variable";
}
pugi::xml_node office_automatic_styles = rootnode.append_child ("office:automatic-styles");
{
pugi::xml_node style_style;
style_style = office_automatic_styles.append_child ("style:style");
style_style.append_attribute ("style:name") = "P1";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = "Header";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
{
pugi::xml_node style_tab_stops = style_paragraph_properties.append_child ("style:tab-stops");
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = "9.005cm";
style_tab_stop.append_attribute ("style:type") = "center";
style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = "17.986cm";
style_tab_stop.append_attribute ("style:type") = "right";
}
}
}
style_style = office_automatic_styles.append_child ("style:style");
style_style.append_attribute ("style:name") = "P2";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = "Header_20_left";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
{
pugi::xml_node style_tab_stops = style_paragraph_properties.append_child ("style:tab-stops");
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = "8.193cm";
style_tab_stop.append_attribute ("style:type") = "center";
style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = "17.963cm";
style_tab_stop.append_attribute ("style:type") = "right";
}
}
}
// Add the automatic image style.
// <style:style style:name="fr1" style:family="graphic" style:parent-style-name="Graphics">
// <style:graphic-properties style:mirror="none" fo:clip="rect(0mm, 0mm, 0mm, 0mm)" draw:luminance="0%" draw:contrast="0%" draw:red="0%" draw:green="0%" draw:blue="0%" draw:gamma="100%" draw:color-inversion="false" draw:image-opacity="100%" draw:color-mode="standard" />
// </style:style>
style_style = office_automatic_styles.append_child ("style:style");
style_style.append_attribute ("style:name") = "fr1";
style_style.append_attribute ("style:family") = "graphic";
style_style.append_attribute ("style:parent-style-name") = "Graphics";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
style_paragraph_properties.append_attribute ("style:mirror") = "none";
style_paragraph_properties.append_attribute ("fo:clip") = "rect(0mm, 0mm, 0mm, 0mm)";
style_paragraph_properties.append_attribute ("draw:luminance") = "0%";
style_paragraph_properties.append_attribute ("draw:contrast") = "0%";
style_paragraph_properties.append_attribute ("draw:red") = "0%";
style_paragraph_properties.append_attribute ("draw:green") = "0%";
style_paragraph_properties.append_attribute ("draw:blue") = "0%";
style_paragraph_properties.append_attribute ("draw:gamma") = "100%";
style_paragraph_properties.append_attribute ("draw:color-inversion") = "false";
style_paragraph_properties.append_attribute ("draw:image-opacity") = "100%";
style_paragraph_properties.append_attribute ("draw:color-mode") = "standard";
}
}
pugi::xml_node office_body = rootnode.append_child ("office:body");
{
office_text_node = office_body.append_child ("office:text");
{
pugi::xml_node text_sequence_decls = office_text_node.append_child ("text:sequence-decls");
{
pugi::xml_node text_sequence_decl = text_sequence_decls.append_child ("text:sequence-decl");
text_sequence_decl.append_attribute ("text:display-outline-level") = "0";
text_sequence_decl.append_attribute ("text:name") = "Illustration";
text_sequence_decl = text_sequence_decls.append_child ("text:sequence-decl");
text_sequence_decl.append_attribute ("text:display-outline-level") = "0";
text_sequence_decl.append_attribute ("text:name") = "Table";
text_sequence_decl = text_sequence_decls.append_child ("text:sequence-decl");
text_sequence_decl.append_attribute ("text:display-outline-level") = "0";
text_sequence_decl.append_attribute ("text:name") = "Text";
text_sequence_decl = text_sequence_decls.append_child ("text:sequence-decl");
text_sequence_decl.append_attribute ("text:display-outline-level") = "0";
text_sequence_decl.append_attribute ("text:name") = "Drawing";
}
// The default text:p element is not added so the document appears empty.
}
}
}
// Build the default styles.xml for the template.
void odf_text::initialize_styles_xml ()
{
pugi::xml_node rootnode = styles_dom.append_child ("office:document-styles");
rootnode.append_attribute ("xmlns:office") = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
rootnode.append_attribute ("xmlns:style") = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
rootnode.append_attribute ("xmlns:text") = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
rootnode.append_attribute ("xmlns:table") = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
rootnode.append_attribute ("xmlns:draw") = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
rootnode.append_attribute ("xmlns:fo") = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
rootnode.append_attribute ("xmlns:xlink") = "http://www.w3.org/1999/xlink";
rootnode.append_attribute ("xmlns:dc") = "http://purl.org/dc/elements/1.1/";
rootnode.append_attribute ("xmlns:meta") = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0";
rootnode.append_attribute ("xmlns:number") = "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0";
rootnode.append_attribute ("xmlns:svg") = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0";
rootnode.append_attribute ("xmlns:chart") = "urn:oasis:names:tc:opendocument:xmlns:chart:1.0";
rootnode.append_attribute ("xmlns:dr3d") = "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0";
rootnode.append_attribute ("xmlns:math") = "http://www.w3.org/1998/Math/MathML";
rootnode.append_attribute ("xmlns:form") = "urn:oasis:names:tc:opendocument:xmlns:form:1.0";
rootnode.append_attribute ("xmlns:script") = "urn:oasis:names:tc:opendocument:xmlns:script:1.0";
rootnode.append_attribute ("xmlns:ooo") = "http://openoffice.org/2004/office";
rootnode.append_attribute ("xmlns:ooow") = "http://openoffice.org/2004/writer";
rootnode.append_attribute ("xmlns:oooc") = "http://openoffice.org/2004/calc";
rootnode.append_attribute ("xmlns:dom") = "http://www.w3.org/2001/xml-events";
rootnode.append_attribute ("xmlns:rpt") = "http://openoffice.org/2005/report";
rootnode.append_attribute ("xmlns:of") = "urn:oasis:names:tc:opendocument:xmlns:of:1.2";
rootnode.append_attribute ("xmlns:xhtml") = "http://www.w3.org/1999/xhtml";
rootnode.append_attribute ("xmlns:grddl") = "http://www.w3.org/2003/g/data-view#";
rootnode.append_attribute ("xmlns:tableooo") = "http://openoffice.org/2009/table";
rootnode.append_attribute ("xmlns:css3t") = "http://www.w3.org/TR/css3-text/";
rootnode.append_attribute ("office:version") = "1.2";
rootnode.append_attribute ("grddl:transformation") = "http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl";
pugi::xml_node office_font_face_decls = rootnode.append_child ("office:font-face-decls");
{
pugi::xml_node childnode;
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Times New Roman";
childnode.append_attribute ("svg:font-family") = "'Times New Roman'";
childnode.append_attribute ("style:font-family-generic") = "roman";
childnode.append_attribute ("style:font-pitch") = "variable";
std::string fontname = database::config::bible::get_export_font (m_bible);
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = fontname.c_str();
fontname.insert (0, "'");
fontname.append ("'");
childnode.append_attribute ("svg:font-family") = fontname.c_str();
childnode.append_attribute ("style:font-family-generic") = "roman";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Arial";
childnode.append_attribute ("svg:font-family") = "Arial";
childnode.append_attribute ("style:font-family-generic") = "swiss";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Droid Sans Fallback";
childnode.append_attribute ("svg:font-family") = "'Droid Sans Fallback'";
childnode.append_attribute ("style:font-family-generic") = "system";
childnode.append_attribute ("style:font-pitch") = "variable";
childnode = office_font_face_decls.append_child ("style:font-face");
childnode.append_attribute ("style:name") = "Lohit Hindi";
childnode.append_attribute ("svg:font-family") = "'Lohit Hindi'";
childnode.append_attribute ("style:font-family-generic") = "system";
childnode.append_attribute ("style:font-pitch") = "variable";
}
office_styles_node = rootnode.append_child ("office:styles");
{
pugi::xml_node style_default_style = office_styles_node.append_child ("style:default-style");
style_default_style.append_attribute ("style:family") = "graphic";
pugi::xml_node style_paragraph_properties = style_default_style.append_child ("style:paragraph-properties");
if (style_paragraph_properties) {}
pugi::xml_node style_text_properties = style_default_style.append_child ("style:text-properties");
if (style_text_properties) {}
}
{
pugi::xml_node style_default_style = office_styles_node.append_child ("style:default-style");
style_default_style.append_attribute ("style:family") = "paragraph";
pugi::xml_node style_paragraph_properties = style_default_style.append_child ("style:paragraph-properties");
if (style_paragraph_properties) {}
pugi::xml_node style_text_properties = style_default_style.append_child ("style:text-properties");
if (style_text_properties) {}
}
{
pugi::xml_node style_default_style = office_styles_node.append_child ("style:default-style");
style_default_style.append_attribute ("style:family") = "table";
}
{
pugi::xml_node style_style = office_styles_node.append_child ("style:style");
style_style.append_attribute ("style:name") = stylesv2::standard_sheet ().c_str();
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:class") = "text";
}
{
pugi::xml_node style_style = office_styles_node.append_child ("style:style");
style_style.append_attribute ("style:name") = "Heading";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = stylesv2::standard_sheet ().c_str();
style_style.append_attribute ("style:next-style-name") = "Text_20_body";
style_style.append_attribute ("style:class") = "text";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
style_paragraph_properties.append_attribute ("fo:margin-top") = "0.423cm";
style_paragraph_properties.append_attribute ("fo:margin-bottom") = "0.212cm";
style_paragraph_properties.append_attribute ("fo:keep-with-next") = "always";
}
}
{
pugi::xml_node style_style = office_styles_node.append_child ("style:style");
style_style.append_attribute ("style:name") = "Text_20_body";
style_style.append_attribute ("style:display-name") = "Text body";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = stylesv2::standard_sheet ().c_str();
style_style.append_attribute ("style:class") = "text";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
style_paragraph_properties.append_attribute ("fo:margin-top") = "0cm";
style_paragraph_properties.append_attribute ("fo:margin-bottom") = "0.212cm";
}
}
{
pugi::xml_node style_style = office_styles_node.append_child ("style:style");
style_style.append_attribute ("style:name") = "Header";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = stylesv2::standard_sheet ().c_str();
style_style.append_attribute ("style:class") = "extra";
}
{
pugi::xml_node style_style = office_styles_node.append_child ("style:style");
style_style.append_attribute ("style:name") = "Header_20_left";
style_style.append_attribute ("style:display-name") = "Header left";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = stylesv2::standard_sheet ().c_str();
style_style.append_attribute ("style:class") = "extra";
}
// Update the tab-stops in the header style. The tab stops depend on page and margin dimensions.
int centerPosition = filter::strings::convert_to_int (database::config::bible::get_page_width (m_bible)) - filter::strings::convert_to_int (database::config::bible::get_inner_margin (m_bible)) - filter::strings::convert_to_int (database::config::bible::get_outer_margin (m_bible));
pugi::xml_node office_automatic_styles = rootnode.append_child ("office:automatic-styles");
{
pugi::xml_node style_style = office_automatic_styles.append_child ("style:style");
style_style.append_attribute ("style:name") = "MP1";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = "Header";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
{
pugi::xml_node style_tab_stops = style_paragraph_properties.append_child ("style:tab-stops");
centerPosition /= 2;
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = filter::strings::convert_to_string (std::to_string (centerPosition) + "mm").c_str();
style_tab_stop.append_attribute ("style:type") = "center";
}
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = filter::strings::convert_to_string (std::to_string (centerPosition * 2) + "mm").c_str();
style_tab_stop.append_attribute ("style:type") = "right";
}
}
}
}
{
pugi::xml_node style_style = office_automatic_styles.append_child ("style:style");
style_style.append_attribute ("style:name") = "MP2";
style_style.append_attribute ("style:family") = "paragraph";
style_style.append_attribute ("style:parent-style-name") = "Header_20_left";
{
pugi::xml_node style_paragraph_properties = style_style.append_child ("style:paragraph-properties");
{
pugi::xml_node style_tab_stops = style_paragraph_properties.append_child ("style:tab-stops");
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = filter::strings::convert_to_string (std::to_string (centerPosition) + "mm").c_str();
style_tab_stop.append_attribute ("style:type") = "center";
}
{
pugi::xml_node style_tab_stop = style_tab_stops.append_child ("style:tab-stop");
style_tab_stop.append_attribute ("style:position") = filter::strings::convert_to_string (std::to_string (centerPosition * 2) + "mm").c_str();
style_tab_stop.append_attribute ("style:type") = "right";
}
}
}
}
{
pugi::xml_node style_page_layout = office_automatic_styles.append_child ("style:page-layout");
style_page_layout.append_attribute ("style:name") = "Mpm1";
style_page_layout.append_attribute ("style:page-usage") = "mirrored";
{
pugi::xml_node style_page_layout_properties = style_page_layout.append_child ("style:page-layout-properties");
// Take the page size and margins from the Bible's settings.
style_page_layout_properties.append_attribute ("fo:page-width") = filter::strings::convert_to_string (database::config::bible::get_page_width (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("fo:page-height") = filter::strings::convert_to_string (database::config::bible::get_page_height (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("style:num-format") = "1";
style_page_layout_properties.append_attribute ("style:print-orientation") = "portrait";
style_page_layout_properties.append_attribute ("fo:margin-top") = filter::strings::convert_to_string (database::config::bible::get_top_margin (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("fo:margin-bottom") = filter::strings::convert_to_string (database::config::bible::get_bottom_margin (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("fo:margin-left") = filter::strings::convert_to_string (database::config::bible::get_inner_margin (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("fo:margin-right") = filter::strings::convert_to_string (database::config::bible::get_outer_margin (m_bible) + "mm").c_str();
style_page_layout_properties.append_attribute ("style:writing-mode") = "lr-tb";
style_page_layout_properties.append_attribute ("style:footnote-max-height") = "0cm";
{
pugi::xml_node style_footnote_sep = style_page_layout_properties.append_child ("style:footnote-sep");
style_footnote_sep.append_attribute ("style:width") = "0.018cm";
style_footnote_sep.append_attribute ("style:distance-before-sep") = "0.101cm";
style_footnote_sep.append_attribute ("style:distance-after-sep") = "0.101cm";
style_footnote_sep.append_attribute ("style:adjustment") = "left";
style_footnote_sep.append_attribute ("style:rel-width") = "25%";
style_footnote_sep.append_attribute ("style:color") = "#000000";
}
}
{
pugi::xml_node style_header_style = style_page_layout.append_child ("style:header-style");
{
pugi::xml_node style_header_footer_properties = style_header_style.append_child ("style:header-footer-properties");
style_header_footer_properties.append_attribute ("fo:min-height") = "0.799cm";
style_header_footer_properties.append_attribute ("fo:margin-left") = "0cm";
style_header_footer_properties.append_attribute ("fo:margin-right") = "0cm";
style_header_footer_properties.append_attribute ("fo:margin-bottom") = "0.3cm";
style_header_footer_properties.append_attribute ("style:dynamic-spacing") = "false";
}
[[maybe_unused]] pugi::xml_node style_footer_style = style_page_layout.append_child ("style:footer-style");
}
}
{
// Do not write the date style for the running headers, so that it takes the default style.
/*
xml_node number_date_style = .append_child ("number:date-style");
xmlAddChild (office_automatic_styles, number_date_style);
.append_attribute (number_date_style, "style:name") = "N81";
{
xml_node number_day = .append_child ("number:day");
xmlAddChild (number_date_style, number_day);
xml_node number_text = .append_child ("number:text");
xmlAddChild (number_date_style, number_text);
{
xml_node textnode = xmlNewText( ".");
xmlAddChild (number_text, textnode);
}
xml_node number_month = .append_child ("number:month");
xmlAddChild (number_date_style, number_month);
{
.append_attribute (number_month, "number:style") = "long";
.append_attribute (number_month, "number:textual") = "true";
}
number_text = .append_child ("number:text");
xmlAddChild (number_date_style, number_text);
xml_node number_year = .append_child ("number:year");
xmlAddChild (number_date_style, number_year);
{
.append_attribute (number_year, "number:style") = "long";
}
}
*/
}
pugi::xml_node office_master_styles = rootnode.append_child ("office:master-styles");
{
pugi::xml_node style_master_page = office_master_styles.append_child ("style:master-page");
style_master_page.append_attribute ("style:name") = stylesv2::standard_sheet ().c_str();
style_master_page.append_attribute ("style:page-layout-name") = "Mpm1";
{
pugi::xml_node style_header = style_master_page.append_child ("style:header");
{
pugi::xml_node text_p = style_header.append_child ("text:p");
text_p.append_attribute ("text:style-name") = "MP1";
{
pugi::xml_node node = text_p.append_child ("text:page-number");
node.append_attribute ("text:select-page") = "current";
node.text ().set ("1");
}
{
text_p.append_child ("text:tab");
}
// Whether and how to put the date in the running headers.
if (database::config::bible::get_date_in_header (m_bible)) {
pugi::xml_node node = text_p.append_child ("text:date");
node.append_attribute ("style:data-style-name") = "N81";
node.append_attribute ("text:date-value") = "";
node.text ().set ("");
}
{
text_p.append_child ("text:tab");
}
{
pugi::xml_node node = text_p.append_child ("text:chapter");
node.append_attribute ("text:display") = "name";
node.append_attribute ("text:outline-level") = "1";
}
}
pugi::xml_node style_header_left = style_master_page.append_child ("style:header-left");
{
pugi::xml_node text_p = style_header_left.append_child ("text:p");
text_p.append_attribute ("text:style-name") = "MP2";
{
pugi::xml_node node = text_p.append_child ("text:chapter");
node.append_attribute ("text:display") = "name";
node.append_attribute ("text:outline-level") = "1";
}
{
text_p.append_child ("text:tab");
}
// Whether and how to put the date in the running headers.
if (database::config::bible::get_date_in_header (m_bible)) {
pugi::xml_node node = text_p.append_child ("text:date");
node.append_attribute ("style:data-style-name") = "N81";
node.append_attribute ("text:date-value") = "";
node.text ().set ("");
}
{
text_p.append_child ("text:tab");
}
{
pugi::xml_node node = text_p.append_child ("text:page-number");
node.append_attribute ("text:select-page") = "current";
node.text ().set ("1");
}
}
}
}
}
void odf_text::new_paragraph (std::string style)
{
current_text_p_node = office_text_node.append_child ("text:p");
current_text_p_node_style_name = current_text_p_node.append_attribute ("text:style-name") = style.c_str();
m_current_text_p_node_opened = true;
m_current_paragraph_style = style;
m_current_paragraph_content.clear();
}
// This function adds text to the current paragraph.
// $text: The text to add.
void odf_text::add_text (std::string text)
{
// Bail out if there's no text.
if (text.empty()) return;
// Ensure a paragraph has started.
if (!m_current_text_p_node_opened) new_paragraph ();
// Temporal styles array should have at least one style for the code below to work.
// So ensure it has at least one style.
std::vector <std::string> styles (m_current_text_style.begin (), m_current_text_style.end ());
if (styles.empty()) styles.push_back (std::string());
// Write a text span element, nesting the second and later ones.
pugi::xml_node dom_node = current_text_p_node;
for (std::string style : styles) {
pugi::xml_node text_span_node = dom_node.append_child ("text:span");
if (!style.empty ()) {
text_span_node.append_attribute ("text:style-name") = convert_style_name (style).c_str();
}
dom_node = text_span_node;
}
// It used to escape the special XML characters.
// Ecaping e.g. the apostrophy (') would lead to the following fragment in the .odt file:
// '
// So it no longer escapes the special XML characters.
dom_node.text ().set (text.c_str());
// Update public paragraph text.
m_current_paragraph_content += text;
}
// This creates a heading with contents styled "Heading 1".
// $text: Contents.
void odf_text::new_heading1 (std::string text, bool hide)
{
new_named_heading ("Heading 1", text, hide);
}
// This creates the page break style.
void odf_text::create_page_break_style ()
{
// This is how the style looks in styles.xml:
// <style:style style:display-name="Page Break" style:family="paragraph" style:name="Page_20_Break">
// <style:paragraph-properties fo:break-after="page" fo:line-height="0.05cm" fo:margin-bottom="0cm" fo:margin-top="0cm"/>
// <style:text-properties fo:font-size="2pt" style:font-size-asian="2pt" style:font-size-complex="2pt"/>
// </style:style>
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = "Page_20_Break";
style_dom_element.append_attribute ("style:display-name") = "Page Break";
style_dom_element.append_attribute ("style:family") = "paragraph";
pugi::xml_node style_paragraph_properties_dom_element = style_dom_element.append_child ("style:paragraph-properties");
style_paragraph_properties_dom_element.append_attribute ("fo:break-after") = "page";
style_paragraph_properties_dom_element.append_attribute ("fo:line-height") = "0.05cm";
style_paragraph_properties_dom_element.append_attribute ("fo:margin-bottom") = "0cm";
style_paragraph_properties_dom_element.append_attribute ("fo:margin-top") = "0cm";
pugi::xml_node style_text_properties_dom_element = style_dom_element.append_child ("style:text-properties");
style_text_properties_dom_element.append_attribute ("fo:font-size") = "2pt";
style_text_properties_dom_element.append_attribute ("style:font-size-asian") = "2pt";
style_text_properties_dom_element.append_attribute ("style:font-size-complex") = "2pt";
}
// This applies a page break.
void odf_text::new_page_break ()
{
new_paragraph ("Page_20_Break");
// Always clear the paragraph-opened-flag,
// because we don't want subsequent text to be added to this page break,
// since it would be nearly invisible, and thus text would mysteriously get lost.
m_current_text_p_node_opened = false;
m_current_paragraph_style.clear ();
m_current_paragraph_content.clear ();
}
// This creates a paragraph style.
// $name: the name of the style, e.g. 'p'.
// $dropcaps: If 0, there are no drop caps.
// If greater than 0, it the number of characters in drop caps style.
void odf_text::create_paragraph_style (const std::string& name,
std::string font_name,
const float font_size,
const stylesv2::TwoState italic,
const stylesv2::TwoState bold,
const stylesv2::TwoState underline,
const stylesv2::TwoState smallcaps,
const stylesv2::TextAlignment text_alignment,
const float space_before, const float space_after,
const float left_margin, const float right_margin,
const float first_line_indent,
const bool keep_with_next,
const int dropcaps)
{
// Whether to align verse numbers in poetry to the left of the margin,
// and if so, whether this is one of the defined poetry styles.
bool is_poetry_q_style {false};
if (database::config::bible::get_odt_poetry_verses_left (m_bible)) {
is_poetry_q_style = filter::usfm::is_standard_q_poetry (name);
}
// It looks like this in styles.xml:
// <style:style style:display-name="p_c1" style:family="paragraph" style:name="p_c1">
// <style:paragraph-properties fo:margin-bottom="0mm" fo:margin-left="0mm" fo:margin-right="0mm" fo:margin-top="0mm" fo:text-align="justify" fo:text-indent="0mm"/>
// <style:drop-cap style:distance="0.15cm" style:length="1" style:lines="2"/>
// <style:paragraph-properties>
// <style:text-properties fo:font-size="12pt" style:font-size-asian="12pt" style:font-size-complex="12pt"/>
// </style:style>
pugi::xml_node style_style_node = office_styles_node.append_child ("style:style");
style_style_node.append_attribute ("style:name") = convert_style_name (name).c_str();
style_style_node.append_attribute ("style:display-name") = name.c_str();
style_style_node.append_attribute ("style:family") = "paragraph";
pugi::xml_node style_paragraph_properties_node = style_style_node.append_child ("style:paragraph-properties");
pugi::xml_node style_text_properties_node = style_style_node.append_child ("style:text-properties");
style_paragraph_properties_node.append_attribute ("style:font-name") = font_name.c_str();
font_name.insert (0, "'");
font_name.append ("'");
style_text_properties_node.append_attribute ("fo:font-family") = font_name.c_str();
const std::string sfontsize = filter::strings::convert_to_string (font_size) + "pt";
style_text_properties_node.append_attribute ("fo:font-size") = sfontsize.c_str();
style_text_properties_node.append_attribute ("style:font-size-asian") = sfontsize.c_str();
style_text_properties_node.append_attribute ("style:font-size-complex") = sfontsize.c_str();
// Italics, bold, underline, small caps can be either off or on for a paragraph.
if (italic != stylesv2::TwoState::off) {
style_text_properties_node.append_attribute ("fo:font-style") = "italic";
style_text_properties_node.append_attribute ("style:font-style-asian") = "italic";
style_text_properties_node.append_attribute ("style:font-style-complex") = "italic";
}
if (bold != stylesv2::TwoState::off) {
style_text_properties_node.append_attribute ("fo:font-weight") = "bold";
style_text_properties_node.append_attribute ("style:font-weight-asian") = "bold";
style_text_properties_node.append_attribute ("style:font-weight-complex") = "bold";
}
if (underline != stylesv2::TwoState::off) {
style_text_properties_node.append_attribute ("style:text-underline-style") = "solid";
style_text_properties_node.append_attribute ("style:text-underline-width") = "auto";
style_text_properties_node.append_attribute ("style:text-underline-color") = "font-color";
}
if (smallcaps != stylesv2::TwoState::off) {
style_text_properties_node.append_attribute ("fo:font-variant") = "small-caps";
}
// Text alignment can be left, center, right, or justify.
const auto get_alignment_text = [] (const auto ta) {
switch (ta) {
case stylesv2::TextAlignment::left:
return "start";
case stylesv2::TextAlignment::center:
return "center";
case stylesv2::TextAlignment::right:
return "end";
case stylesv2::TextAlignment::justify:
default:
return "justify";
}
};
style_paragraph_properties_node.append_attribute ("fo:text-align") = get_alignment_text(text_alignment);
style_paragraph_properties_node.append_attribute ("style:justify-single-word") = "false";
// Deal with the paragraph dimensions.
// The values are given in millimeters.
// First the top and bottom margins.
const std::string space_before_mm = filter::strings::convert_to_string (space_before) + "mm";
style_paragraph_properties_node.append_attribute ("fo:margin-top") = space_before_mm.c_str();
const std::string space_after_mm = filter::strings::convert_to_string (space_after) + "mm";
style_paragraph_properties_node.append_attribute ("fo:margin-bottom") = space_after_mm.c_str();
const std::string left_margin_mm = filter::strings::convert_to_string (left_margin) + "mm";
style_paragraph_properties_node.append_attribute ("fo:margin-left") = left_margin_mm.c_str();
const std::string right_margin_mm = filter::strings::convert_to_string (right_margin) + "mm";
style_paragraph_properties_node.append_attribute ("fo:margin-right") = right_margin_mm.c_str();
// In a normal paragraph the first line indent is as given in the stylesheet.
// In a poetry paragraph the first line indent is the negative left margin.
// The goal is that the left is at a 0 left margin,
// and that the verse is aligned at the very left of the column.
// (And then a tab puts the text at the desired first line indent space.)
int millimeters = static_cast<int>(first_line_indent);
if (is_poetry_q_style)
millimeters = static_cast <int> (0 - left_margin);
const std::string first_lineindent_mm = std::to_string (millimeters) + "mm";
style_paragraph_properties_node.append_attribute ("fo:text-indent") = first_lineindent_mm.c_str();
if (keep_with_next) {
style_paragraph_properties_node.append_attribute ("fo:keep-together") = "always";
style_paragraph_properties_node.append_attribute ("fo:keep-with-next") = "always";
}
if (dropcaps > 0) {
// E.g.: <style:drop-cap style:lines="2" style:length="2" style:distance="0.15cm"/>
std::string length = std::to_string (dropcaps);
pugi::xml_node style_drop_cap_node = style_paragraph_properties_node.append_child ("style:drop-cap");
style_drop_cap_node.append_attribute ("style:lines") = "2";
style_drop_cap_node.append_attribute ("style:length") = length.c_str();
style_drop_cap_node.append_attribute ("style:distance") = "0.15cm";
}
// For poetry styles like q, q1, and so on,
// there's an additional definition of the tab settings.
// Later there were more tab stops added,
// each tab stop slightly deeper than the previous one.
// The reason for adding more tab stops is this:
// The chapter number at times is wider than the first tab stop,
// pushing the indent of the first line too deep.
// See issue https://github.com/bibledit/cloud/issues/671
if (is_poetry_q_style) {
pugi::xml_node style_tab_stops = style_paragraph_properties_node.append_child("style:tab-stops");
int tab_indent = static_cast<int> (first_line_indent);
for (int i = 0; i < 10; i++) {
pugi::xml_node style_tab_stop = style_tab_stops.append_child("style:tab-stop");
const std::string tab_stop = std::to_string(tab_indent) + "mm";
style_tab_stop.append_attribute("style:position") = tab_stop.c_str();
tab_indent++;
}
}
}
// This updates the style name of the current paragraph.
// $name: the name of the style, e.g. 'p'.
void odf_text::update_current_paragraph_style (std::string name)
{
if (!m_current_text_p_node_opened) new_paragraph ();
current_text_p_node.remove_attribute (current_text_p_node_style_name);
current_text_p_node_style_name = current_text_p_node.append_attribute ("text:style-name");
current_text_p_node_style_name = convert_style_name (name).c_str();
}
// This opens a text style.
// $style: the object with the style variables.
// $note: Whether this refers to notes.
// $embed: boolean: Whether nest $style in an existing character style.
void odf_text::open_text_style (const stylesv2::Style* stylev2, bool note, bool embed)
{
const auto get_marker = [stylev2]() {
if (stylev2)
return stylev2->marker;
return std::string();
};
const std::string marker = get_marker();
if (find (created_styles.begin(), created_styles.end(), marker) == created_styles.end()) {
created_styles.push_back (marker);
const auto get_on_v2 = [](const stylesv2::FourState state) {
return ((state == stylesv2::FourState::on) || (state == stylesv2::FourState::toggle));
};
// The style entry looks like this in styles.xml, e.g., for italic:
// <style:style style:name="T1" style:family="text">
// <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
// </style:style>
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = convert_style_name (marker).c_str();
style_dom_element.append_attribute ("style:display-name") = marker.c_str();
style_dom_element.append_attribute ("style:family") = "text";
pugi::xml_node style_text_properties_dom_element = style_dom_element.append_child ("style:text-properties");
// Italics, bold, underline, small caps can be ooitOff or ooitOn or ooitInherit or ooitToggle.
// Not all features are implemented.
const auto get_italic = [stylev2, &get_on_v2]() {
if (stylev2)
if (stylev2->character)
return get_on_v2 (stylev2->character.value().italic);
return false;
};
if (get_italic()) {
style_text_properties_dom_element.append_attribute ("fo:font-style") = "italic";
style_text_properties_dom_element.append_attribute ("style:font-style-asian") = "italic";
style_text_properties_dom_element.append_attribute ("style:font-style-complex") = "italic";
}
const auto get_bold = [stylev2, &get_on_v2]() {
if (stylev2)
if (stylev2->character)
return get_on_v2 (stylev2->character.value().bold);
return false;
};
if (get_bold()) {
style_text_properties_dom_element.append_attribute ("fo:font-weight") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-weight-asian") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-weight-complex") = "bold";
}
const auto get_underline = [stylev2, &get_on_v2]() {
if (stylev2)
if (stylev2->character)
return get_on_v2 (stylev2->character.value().underline);
return false;
};
if (get_underline()) {
style_text_properties_dom_element.append_attribute ("style:text-underline-style") = "solid";
style_text_properties_dom_element.append_attribute ("style:text-underline-width") = "auto";
style_text_properties_dom_element.append_attribute ("style:text-underline-color") = "font-color";
}
const auto get_smallcaps = [stylev2, &get_on_v2]() {
if (stylev2)
if (stylev2->character)
return get_on_v2 (stylev2->character.value().smallcaps);
return false;
};
if (get_smallcaps()) {
style_text_properties_dom_element.append_attribute ("fo:font-variant") = "small-caps";
}
const auto get_superscript = [stylev2] () {
if (stylev2)
if (stylev2->character)
return (stylev2->character.value().superscript == stylesv2::TwoState::on);
return false;
};
if (get_superscript()) {
// If the percentage is not specified, an appropriate font height is used.
// Setting the superscript makes the font size smaller. No need to set it manually.
style_text_properties_dom_element.append_attribute ("style:text-position") = "super";
}
const auto get_foreground_color = [stylev2] () {
if (stylev2)
if (stylev2->character)
return stylev2->character.value().foreground_color;
return std::string();
};
const std::string foreground_color {get_foreground_color()};
if (foreground_color != "#000000") {
style_text_properties_dom_element.append_attribute ("fo:color") = foreground_color.c_str();
}
const auto get_background_color = [stylev2] () {
if (stylev2)
if (stylev2->character)
return stylev2->character.value().background_color;
return std::string();
};
const std::string background_color {get_background_color()};
if (background_color != "#FFFFFF") {
style_text_properties_dom_element.append_attribute ("fo:background-color") = background_color.c_str();
}
}
if (note) {
if (!embed)
m_current_note_text_style.clear();
m_current_note_text_style.push_back (marker);
} else {
if (!embed)
m_current_text_style.clear ();
m_current_text_style.push_back (marker);
}
}
// This closes any open text style.
// $note: Whether this refers to notes.
// $embed: boolean: Whether to close embedded style.
void odf_text::close_text_style (bool note, bool embed)
{
if (note) {
if (!embed) m_current_note_text_style.clear();
if (!m_current_note_text_style.empty ()) m_current_note_text_style.pop_back ();
} else {
if (!embed) m_current_text_style.clear();
if (!m_current_text_style.empty()) m_current_text_style.pop_back ();
}
}
// This places text in a frame in OpenDocument.
// It does all the housekeeping to get it display properly.
// $text - the text to place in the frame.
// $style - the name of the style of the $text.
// $fontsize - given in points.
// $italic, $bold - integer values.
void odf_text::place_text_in_frame (const std::string& text, const std::string& style,
const float fontsize, const stylesv2::TwoState italic, const stylesv2::TwoState bold)
{
// Empty text is discarded.
if (text.empty ()) return;
// The frame goes in an existing paragraph (text:p) element, just like a 'text:span' element.
// Ensure that a paragraph is open.
if (!m_current_text_p_node_opened) new_paragraph ();
// The frame looks like this, in content.xml:
// <draw:frame draw:style-name="fr1" draw:name="frame1" text:anchor-type="paragraph" svg:y="0cm" fo:min-width="0.34cm" draw:z-index="0">
// <draw:text-box fo:min-height="0.34cm">
// <text:p text:style-name="c">1</text:p>
// </draw:text-box>
// </draw:frame>
pugi::xml_node draw_frame_dom_element = current_text_p_node.append_child ("draw:frame");
draw_frame_dom_element.append_attribute ("draw:style-name") = "chapterframe";
m_frame_count++;
draw_frame_dom_element.append_attribute ("draw:name") = filter::strings::convert_to_string ("frame" + std::to_string (m_frame_count)).c_str();
draw_frame_dom_element.append_attribute ("text:anchor-type") = "paragraph";
draw_frame_dom_element.append_attribute ("svg:y") = "0cm";
draw_frame_dom_element.append_attribute ("fo:min-width") = "0.34cm";
draw_frame_dom_element.append_attribute ("draw:z-index") = "0";
pugi::xml_node draw_text_box_dom_element = draw_frame_dom_element.append_child ("draw:text-box");
draw_text_box_dom_element.append_attribute ("fo:min-height") = "0.34cm";
pugi::xml_node text_p_dom_element = draw_text_box_dom_element.append_child ("text:p");
text_p_dom_element.append_attribute ("text:style-name") = convert_style_name (style).c_str();
text_p_dom_element.text().set( filter::strings::escape_special_xml_characters (text).c_str());
// File styles.xml contains the appropriate styles for this frame and text box and paragraph.
// Create the styles once for the whole document.
if (find (created_styles.begin(), created_styles.end (), style) == created_styles.end()) {
created_styles.push_back (style);
{
// The style for the text:p element looks like this:
// <style:style style:name="c" style:family="paragraph">
// <style:paragraph-properties fo:text-align="justify" style:justify-single-word="false"/>
// <style:text-properties fo:font-size="24pt" fo:font-weight="bold" style:font-size-asian="24pt" style:font-weight-asian="bold" style:font-size-complex="24pt" style:font-weight-complex="bold"/>
// </style:style>
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = convert_style_name (style).c_str();
style_dom_element.append_attribute ("style:family") = "paragraph";
pugi::xml_node style_paragraph_properties_dom_element = style_dom_element.append_child ("style:paragraph-properties");
style_paragraph_properties_dom_element.append_attribute ("fo:text-align") = "justify";
style_paragraph_properties_dom_element.append_attribute ("style:justify-single-word") = "false";
pugi::xml_node style_text_properties_dom_element = style_dom_element.append_child ("style:text-properties");
std::string sfontsize = filter::strings::convert_to_string (fontsize) + "pt";
style_text_properties_dom_element.append_attribute ("fo:font-size") = sfontsize.c_str();
style_text_properties_dom_element.append_attribute ("style:font-size-asian") = sfontsize.c_str();
style_text_properties_dom_element.append_attribute ("style:font-size-complex") = sfontsize.c_str();
if (italic != stylesv2::TwoState::off) {
style_text_properties_dom_element.append_attribute ("fo:font-style") = "italic";
style_text_properties_dom_element.append_attribute ("style:font-style-asian") = "italic";
style_text_properties_dom_element.append_attribute ("style:font-style-complex") = "italic";
}
if (bold != stylesv2::TwoState::off) {
style_text_properties_dom_element.append_attribute ("fo:font-weight") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-weight-asian") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-weight-complex") = "bold";
}
}
{
// The style for the draw:frame element looks like this:
// <style:style style:name="chapterframe" style:family="graphic" style:parent-style-name="ChapterFrameParent">
// <style:graphic-properties fo:margin-left="0cm" fo:margin-right="0.199cm" fo:margin-top="0cm" fo:margin-bottom="0cm" style:vertical-pos="from-top" style:vertical-rel="paragraph-content" style:horizontal-pos="left" style:horizontal-rel="paragraph" fo:background-color="transparent" style:background-transparency="100%" fo:padding="0cm" fo:border="none" style:shadow="none" style:flow-with-text="true">
// <style:background-image/>
// </style:graphic-properties>
// </style:style>
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = "chapterframe";
style_dom_element.append_attribute ("style:family") = "graphic";
pugi::xml_node style_graphic_properties_dom_element = style_dom_element.append_child ("style:graphic-properties");
style_graphic_properties_dom_element.append_attribute ("fo:margin-left") = "0cm";
style_graphic_properties_dom_element.append_attribute ("fo:margin-right") = "0.2cm";
style_graphic_properties_dom_element.append_attribute ("fo:margin-top") = "0cm";
style_graphic_properties_dom_element.append_attribute ("fo:margin-bottom") = "0cm";
style_graphic_properties_dom_element.append_attribute ("style:vertical-pos") = "from-top";
style_graphic_properties_dom_element.append_attribute ("style:vertical-rel") = "paragraph-content";
style_graphic_properties_dom_element.append_attribute ("style:horizontal-pos") = "left";
style_graphic_properties_dom_element.append_attribute ("style:horizontal-rel") = "paragraph";
style_graphic_properties_dom_element.append_attribute ("fo:background-color") = "transparent";
style_graphic_properties_dom_element.append_attribute ("style:background-transparency") = "100%";
style_graphic_properties_dom_element.append_attribute ("fo:padding") = "0cm";
style_graphic_properties_dom_element.append_attribute ("fo:border") = "none";
style_graphic_properties_dom_element.append_attribute ("style:shadow") = "none";
style_graphic_properties_dom_element.append_attribute ("style:flow-with-text") = "true";
}
}
}
// This creates the superscript style.
void odf_text::create_superscript_style ()
{
// The style entry looks like this in styles.xml:
// <style:style style:name="superscript" style:family="text">
// <style:text-properties style:text-position="super 58%"/>
// </style:style>
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = "superscript";
style_dom_element.append_attribute ("style:family") = "text";
pugi::xml_node style_text_properties_dom_element = style_dom_element.append_child ("style:text-properties");
//$styleTextPropertiesDomElement->setAttribute ("style:text-position", "super 58%");
// If the percentage is not specified, an appropriate font height is used.
style_text_properties_dom_element.append_attribute ("style:text-position") = "super";
// Setting the superscript attribute automatically makes the font smaller. No need to set it manually.
//$styleTextPropertiesDomElement->setAttribute ("fo:font-size", "87%";
//$styleTextPropertiesDomElement->setAttribute ("style:font-size-asian", "87%";
//$styleTextPropertiesDomElement->setAttribute ("style:font-size-complex", "87%";
}
// This function adds a note to the current paragraph.
// $caller: The text of the note caller, that is, the note citation.
// $style: Style name for the paragraph in the footnote body.
// $endnote: Whether this is a footnote and cross reference (false), or an endnote (true).
void odf_text::add_note (std::string caller, std::string style, bool endnote)
{
// Ensure that a paragraph is open, so that the note can be added to it.
if (!m_current_text_p_node_opened) new_paragraph ();
pugi::xml_node text_note_dom_element = current_text_p_node.append_child ("text:note");
text_note_dom_element.append_attribute ("text:id") = filter::strings::convert_to_string ("ftn" + std::to_string (m_note_count)).c_str();
m_note_count++;
m_note_text_p_opened = true;
std::string noteclass;
if (endnote) noteclass = "endnote";
else noteclass = "footnote";
text_note_dom_element.append_attribute ("text:note-class") = noteclass.c_str();
// The note citation, the 'caller' is normally in superscript in the OpenDocument.
// The default values of the application are used.
// The Bibledit stylesheet is not consulted.
// It handles the setting on the export page for having an automatic note caller.
pugi::xml_node text_note_citation_dom_element = text_note_dom_element.append_child ("text:note-citation");
if (!automatic_note_caller) {
text_note_citation_dom_element.append_attribute ("text:label") = filter::strings::escape_special_xml_characters (caller).c_str();
}
text_note_citation_dom_element.text().set( filter::strings::escape_special_xml_characters (caller).c_str());
pugi::xml_node text_note_body_dom_element = text_note_dom_element.append_child ("text:note-body");
note_text_p_dom_element = text_note_body_dom_element.append_child ("text:p");
note_text_p_dom_element.append_attribute ("text:style-name") = convert_style_name (style).c_str();
close_text_style (true, false);
}
// This function adds text to the current footnote.
// $text: The text to add.
void odf_text::add_note_text (std::string text)
{
// Bail out if there's no text.
if (text == "") return;
// Ensure a note has started.
if (!m_note_text_p_opened) add_note ("?", "");
// Temporal styles array should have at least one style for the code below to work.
std::vector <std::string> styles (m_current_note_text_style.begin(), m_current_note_text_style.end());
if (styles.empty ()) styles.push_back ("");
// Write a text span element, nesting the second and later ones.
pugi::xml_node dom_element = note_text_p_dom_element;
for (std::string style : styles) {
pugi::xml_node text_span_dom_element = dom_element.append_child ("text:span");
if (!style.empty()) {
text_span_dom_element.append_attribute ("text:style-name") = convert_style_name (style).c_str();
}
dom_element = text_span_dom_element;
}
dom_element.text().set( filter::strings::escape_special_xml_characters (text).c_str());
}
// This function closes the current footnote.
void odf_text::close_current_note ()
{
close_text_style (true, false);
m_note_text_p_opened = false;
}
// This creates a heading with styled content.
// $style: A style name.
// $text: Content.
void odf_text::new_named_heading (std::string style, std::string text, bool hide)
{
// Heading looks like this in content.xml:
// <text:h text:style-name="Heading_20_1" text:outline-level="1">Text</text:h>
pugi::xml_node text_h_dom_element = office_text_node.append_child ("text:h");
text_h_dom_element.append_attribute ("text:style-name") = convert_style_name (style).c_str();
text_h_dom_element.append_attribute ("text:outline-level") = "1";
text_h_dom_element.text().set(filter::strings::escape_special_xml_characters (text).c_str());
// Heading style looks like this in styles.xml:
// <style:style style:name="Heading_20_1" style:display-name="Heading 1" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="1" style:class="text">
// <style:text-properties fo:font-size="115%" fo:font-weight="bold" style:font-size-asian="115%" style:font-weight-asian="bold" style:font-size-complex="115%" style:font-weight-complex="bold"/>
// </style:style>
// Create the style if it does not yet exist.
if (find (created_styles.begin(), created_styles.end (), style) == created_styles.end()) {
pugi::xml_node style_dom_element = office_styles_node.append_child ("style:style");
style_dom_element.append_attribute ("style:name") = convert_style_name (style).c_str();
style_dom_element.append_attribute ("style:display-name") = style.c_str();
style_dom_element.append_attribute ("style:family") = "paragraph";
style_dom_element.append_attribute ("style:parent-style-name") = "Heading";
style_dom_element.append_attribute ("style:next-style-name") = "Text_20_body";
style_dom_element.append_attribute ("style:default-outline-level") = "1";
style_dom_element.append_attribute ("style:class") = "text";
{
pugi::xml_node style_text_properties_dom_element = style_dom_element.append_child ("style:text-properties");
style_text_properties_dom_element.append_attribute ("fo:font-size") = "115%";
style_text_properties_dom_element.append_attribute ("fo:font-weight") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-size-asian") = "115%";
style_text_properties_dom_element.append_attribute ("style:font-weight-asian") = "bold";
style_text_properties_dom_element.append_attribute ("style:font-size-complex") = "115%";
style_text_properties_dom_element.append_attribute ("style:font-weight-complex") = "bold";
if (hide) {
style_text_properties_dom_element.append_attribute ("text:display") = "none";
}
}
created_styles.push_back (style);
}
// Make paragraph null, so that adding subsequent text creates a new paragraph.
m_current_text_p_node_opened = false;
m_current_paragraph_style.clear ();
m_current_paragraph_content.clear ();
}
// This converts the name of a style so that it is fit for use in OpenDocument files.
// E.g. 'Heading 1' becomes 'Heading_20_1'
// $style: Input
// It returns the converted style name.
std::string odf_text::convert_style_name (std::string style)
{
style = filter::strings::replace (" ", "_20_", style);
return style;
}
// This saves the OpenDocument to file
// $name: the name of the file to save to.
void odf_text::save (std::string name)
{
// Create the content.xml file.
// No formatting because some white space is processed.
std::string content_xml_path = filter_url_create_path ({unpacked_odt_folder, "content.xml"});
std::stringstream content_xml;
content_dom.print (content_xml, "", pugi::format_raw);
filter_url_file_put_contents (content_xml_path, content_xml.str ());
// Create the styles.xml file.
// No formatting because some white space is processed.
std::string styles_xml_path = filter_url_create_path ({unpacked_odt_folder, "styles.xml"});
std::stringstream styles_xml;
styles_dom.print (styles_xml, "", pugi::format_raw);
filter_url_file_put_contents (styles_xml_path, styles_xml.str ());
// Save the OpenDocument file.
std::string zippedfile = filter_archive_zip_folder (unpacked_odt_folder);
filter_url_file_put_contents (name, filter_url_file_get_contents (zippedfile));
filter_url_unlink (zippedfile);
}
// Add an image to the document.
// <text:p text:style-name="p">
// <draw:frame draw:style-name="fr1" draw:name="Image1" text:anchor-type="char" svg:width="180mm" svg:height="66.55mm" draw:z-index="0">
// <draw:image xlink:href="../bibleimage2.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" draw:filter-name="<All formats>" draw:mime-type="image/png" />
// </draw:frame>
// </text:p>
void odf_text::add_image (std::string style, [[maybe_unused]] std::string alt, std::string src, std::string caption)
{
// The parent paragraph for the image has the "p" style.
current_text_p_node = office_text_node.append_child ("text:p");
current_text_p_node_style_name = current_text_p_node.append_attribute ("text:style-name") = "p";
m_current_text_p_node_opened = true;
m_current_paragraph_style = style;
m_current_paragraph_content.clear();
// Get the width and height of the image in pixels.
int image_width_pixels {0};
int image_height_pixels {0};
{
std::string path = filter_url_create_root_path ({filter_url_temp_dir (), "image_contents"});
std::string contents = database::bible_images::get(src);
filter_url_file_put_contents(path, contents);
filter_image_get_sizes (path, image_width_pixels, image_height_pixels);
}
// Determine the width of the available space so the image width will be equal to that.
// Then the image height depends on the ratio of the image width and height in pixels.
int available_width_mm {0};
int available_height_mm {50};
{
available_width_mm = filter::strings::convert_to_int (database::config::bible::get_page_width (m_bible)) - filter::strings::convert_to_int (database::config::bible::get_inner_margin (m_bible)) - filter::strings::convert_to_int (database::config::bible::get_outer_margin (m_bible));
if (image_width_pixels && image_height_pixels) {
available_height_mm = available_width_mm * image_height_pixels / image_width_pixels;
}
}
{
m_image_counter++;
pugi::xml_node draw_frame_node = current_text_p_node.append_child("draw:frame");
draw_frame_node.append_attribute("draw:style-name") = "fr1";
draw_frame_node.append_attribute("draw:name") = ("Image" + std::to_string(m_image_counter)).c_str();
draw_frame_node.append_attribute("text:anchor-type") = "char";
draw_frame_node.append_attribute("svg:width") = (std::to_string (available_width_mm) + "mm").c_str();
// draw_frame_node.append_attribute("style:rel-width") = "100%";
draw_frame_node.append_attribute("svg:height") = (std::to_string (available_height_mm) + "mm").c_str();
// draw_frame_node.append_attribute("style:rel-height") = "scale";
draw_frame_node.append_attribute("draw:z-index") = "0";
{
pugi::xml_node draw_image_node = draw_frame_node.append_child("draw:image");
// draw_image_node.append_attribute("xlink:href") = string("Pictures/" + src).c_str();
draw_image_node.append_attribute("xlink:href") = ("../" + src).c_str();
draw_image_node.append_attribute("xlink:type") = "simple";
draw_image_node.append_attribute("xlink:show") = "embed";
draw_image_node.append_attribute("xlink:actuate") = "onLoad";
draw_image_node.append_attribute("draw:filter-name") = "<All formats>";
// The mime type should have been set according to the MIME type of the actual image.
// But omitting it is the easier solution.
// LibreOffice can still open the document without any problems.
//draw_image_node.append_attribute("draw:mime-type") = "image/png";
}
}
// Optionally add the caption if given.
if (!caption.empty()) {
new_paragraph (style);
add_text (caption);
// xml_node text_node = current_text_p_node.append_child(node_pcdata);
// text_node.set_value(caption.c_str());
}
// Save the picture into the Pictures output folder.
// Later on this was not done,
// because it would require an updated meta-inf/manifest.xml.
// <manifest:file-entry manifest:full-path="Pictures/100002010000035C0000013E97D1D9088C414E87.png" manifest:media-type="image/png"/>
// Another advantage of not including the pictures in the opendocument file is:
// The OpenDocument file without pictures in them would be smaller as it contains only text.
{
//string contents = database::bible_images::get(src);
//string path = filter_url_create_path(pictures_folder, src);
//filter_url_file_put_contents(path, contents);
}
// Close the current paragraph.
// Goal: Any text that will be added will be output into a new paragraph.
m_current_text_p_node_opened = false;
m_current_paragraph_style.clear ();
m_current_paragraph_content.clear ();
}
// This function adds a tab to the current paragraph.
void odf_text::add_tab ()
{
// Ensure a paragraph has started.
if (!m_current_text_p_node_opened) new_paragraph ();
// Write a text tab element.
current_text_p_node.append_child ("text:tab");
// Update public paragraph text.
m_current_paragraph_content += "\t";
}
|