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
|
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
/* AbiSource
*
* Copyright (C) 2007 Philippe Milot <PhilMilot@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
// Class definition include
#include <OXML_Document.h>
// Internal includes
#include <OXML_Section.h>
#include <OXML_Style.h>
#include <OXML_Types.h>
#include <OXML_Theme.h>
#include <OXML_FontManager.h>
// AbiWord includes
#include <fp_PageSize.h>
#include <ut_types.h>
#include <pd_Document.h>
// External includes
#include <string>
OXML_Document* OXML_Document::s_docInst = NULL;
OXML_Document* OXML_Document::getNewInstance()
{
OXML_Document::destroyInstance();
return OXML_Document::getInstance();
}
OXML_Document* OXML_Document::getInstance()
{
if (s_docInst == NULL)
s_docInst = new OXML_Document();
return s_docInst;
}
void OXML_Document::destroyInstance()
{
DELETEP(s_docInst);
}
OXML_SharedSection OXML_Document::getCurrentSection()
{
UT_return_val_if_fail(s_docInst != NULL, OXML_SharedSection() );
return s_docInst->getLastSection();
}
OXML_Document::OXML_Document() :
OXML_ObjectWithAttrProp(),
m_theme(),
m_fontManager()
{
clearStyles();
clearHeaders();
clearFooters();
clearSections();
clearFootnotes();
clearEndnotes();
}
OXML_Document::~OXML_Document()
{
clearStyles();
clearHeaders();
clearFooters();
clearSections();
clearFootnotes();
clearEndnotes();
}
OXML_SharedStyle OXML_Document::getStyleById(const std::string & id) const
{
OXML_StyleMap::const_iterator it;
it = m_styles_by_id.find(id);
return it != m_styles_by_id.end() ? it->second : OXML_SharedStyle() ;
}
OXML_SharedStyle OXML_Document::getStyleByName(const std::string & name) const
{
OXML_StyleMap::const_iterator it;
it = m_styles_by_name.find(name);
return it != m_styles_by_name.end() ? it->second : OXML_SharedStyle() ;
}
UT_Error OXML_Document::addStyle(const std::string & id, const std::string & name,
const gchar ** attributes)
{
OXML_SharedStyle obj;
try {
obj.reset( new OXML_Style(id, name) );
}
catch(...) {
UT_DEBUGMSG(("Object creation failed!\n"));
return UT_OUTOFMEM;
}
obj->setAttributes(attributes);
return addStyle(obj);
}
UT_Error OXML_Document::addStyle(const OXML_SharedStyle & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_styles_by_id[obj->getId()] = obj;
m_styles_by_name[obj->getName()] = obj;
return UT_OK;
}
UT_Error OXML_Document::addList(const OXML_SharedList & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_lists_by_id[obj->getId()] = obj;
return UT_OK;
}
OXML_SharedList OXML_Document::getListById(UT_uint32 id) const
{
OXML_ListMap::const_iterator it;
it = m_lists_by_id.find(id);
return it != m_lists_by_id.end() ? it->second : OXML_SharedList() ;
}
OXML_SharedImage OXML_Document::getImageById(const std::string & id) const
{
OXML_ImageMap::const_iterator it;
it = m_images_by_id.find(id);
return it != m_images_by_id.end() ? it->second : OXML_SharedImage() ;
}
UT_Error OXML_Document::addImage(const OXML_SharedImage & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
std::string str;
str += obj->getId();
m_images_by_id[str] = obj;
return UT_OK;
}
UT_Error OXML_Document::clearStyles()
{
m_styles_by_id.clear();
m_styles_by_name.clear();
return m_styles_by_id.size() == 0 && m_styles_by_name.size() == 0 ? UT_OK : UT_ERROR;
}
OXML_SharedSection OXML_Document::getHeader(const std::string & id) const
{
OXML_SectionMap::const_iterator it;
it = m_headers.find(id);
return it != m_headers.end() ? it->second : OXML_SharedSection() ;
}
UT_Error OXML_Document::addHeader(const OXML_SharedSection & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_headers[obj->getId()] = obj;
return UT_OK;
}
UT_Error OXML_Document::clearHeaders()
{
m_headers.clear();
return m_headers.size() == 0 ? UT_OK : UT_ERROR;
}
bool OXML_Document::isAllDefault(const bool & header) const
{
const gchar* type = NULL;
OXML_SectionMap::const_iterator it;
if(!header)
{
for (it = m_footers.begin(); it != m_footers.end(); it++)
{
if(it->second->getAttribute("type", type) == UT_OK)
{
if(strcmp(type, "default") != 0)
{
return false;
}
}
}
}
else
{
for (it = m_headers.begin(); it != m_headers.end(); it++)
{
if(it->second->getAttribute("type", type) == UT_OK)
{
if(strcmp(type, "default") != 0)
{
return false;
}
}
}
}
return true;
}
OXML_SharedSection OXML_Document::getHdrFtrById(const bool & header, const std::string & id) const
{
const gchar* hdrFtrId = NULL;
OXML_SectionMap::const_iterator it;
if(!header)
{
for (it = m_footers.begin(); it != m_footers.end(); it++)
{
if(it->second->getAttribute("id", hdrFtrId) == UT_OK)
{
if(!strcmp(hdrFtrId, id.c_str()))
{
return it->second;
}
}
}
}
else
{
for (it = m_headers.begin(); it != m_headers.end(); it++)
{
if(it->second->getAttribute("id", hdrFtrId) == UT_OK)
{
if(!strcmp(hdrFtrId, id.c_str()))
{
return it->second;
}
}
}
}
return OXML_SharedSection();
}
OXML_SharedSection OXML_Document::getFootnote(const std::string & id) const
{
OXML_SectionMap::const_iterator it;
it = m_footnotes.find(id);
return it != m_footnotes.end() ? it->second : OXML_SharedSection() ;
}
UT_Error OXML_Document::addFootnote(const OXML_SharedSection & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_footnotes[obj->getId()] = obj;
return UT_OK;
}
UT_Error OXML_Document::clearFootnotes()
{
m_footnotes.clear();
return m_footnotes.size() == 0 ? UT_OK : UT_ERROR;
}
OXML_SharedSection OXML_Document::getEndnote(const std::string & id) const
{
OXML_SectionMap::const_iterator it;
it = m_endnotes.find(id);
return it != m_endnotes.end() ? it->second : OXML_SharedSection() ;
}
UT_Error OXML_Document::addEndnote(const OXML_SharedSection & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_endnotes[obj->getId()] = obj;
return UT_OK;
}
UT_Error OXML_Document::clearEndnotes()
{
m_endnotes.clear();
return m_endnotes.size() == 0 ? UT_OK : UT_ERROR;
}
OXML_SharedSection OXML_Document::getFooter(const std::string & id) const
{
OXML_SectionMap::const_iterator it;
it = m_footers.find(id);
return it != m_footers.end() ? it->second : OXML_SharedSection() ;
}
UT_Error OXML_Document::addFooter(const OXML_SharedSection & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
m_footers[obj->getId()] = obj;
return UT_OK;
}
UT_Error OXML_Document::clearFooters()
{
m_footers.clear();
return m_footers.size() == 0 ? UT_OK : UT_ERROR;
}
OXML_SharedSection OXML_Document::getLastSection() const
{
UT_return_val_if_fail(!m_sections.empty(), OXML_SharedSection() );
return m_sections.back();
}
OXML_SharedSection OXML_Document::getSection(const std::string & id) const
{
OXML_SectionVector::const_iterator it;
it = std::find(m_sections.begin(), m_sections.end(), id);
return ( it != m_sections.end() ) ? (*it) : OXML_SharedSection() ;
}
UT_Error OXML_Document::appendSection(const OXML_SharedSection & obj)
{
UT_return_val_if_fail(obj, UT_ERROR);
try {
m_sections.push_back(obj);
}
catch(...) {
UT_DEBUGMSG(("Bad alloc!\n"));
return UT_OUTOFMEM;
}
return UT_OK;
}
UT_Error OXML_Document::clearSections()
{
m_sections.clear();
return m_sections.size() == 0 ? UT_OK : UT_ERROR;
}
OXML_SharedTheme OXML_Document::getTheme()
{
if (m_theme.get() == NULL) {
try {
m_theme.reset(new OXML_Theme());
}
catch(...) {
UT_DEBUGMSG(("Bad alloc!\n"));
return OXML_SharedTheme();
}
}
return m_theme;
}
OXML_SharedFontManager OXML_Document::getFontManager()
{
if (m_fontManager.get() == NULL) {
try {
m_fontManager.reset(new OXML_FontManager());
}
catch(...) {
UT_DEBUGMSG(("Bad alloc!\n"));
return OXML_SharedFontManager();
}
}
return m_fontManager;
}
UT_Error OXML_Document::serialize(IE_Exp_OpenXML* exporter)
{
UT_Error ret = UT_OK;
ret = exporter->startDocument();
if(ret != UT_OK)
return ret;
OXML_StyleMap::iterator it1;
for (it1 = m_styles_by_id.begin(); it1 != m_styles_by_id.end(); it1++) {
ret = it1->second->serialize(exporter);
if (ret != UT_OK)
return ret;
}
//serialize abstract numbering definitions
OXML_ListMap::iterator it2;
for (it2 = m_lists_by_id.begin(); it2 != m_lists_by_id.end(); it2++) {
ret = it2->second->serialize(exporter);
if (ret != UT_OK)
return ret;
}
//serialize actual numbering definitions
OXML_ListMap::iterator it3;
for (it3 = m_lists_by_id.begin(); it3 != m_lists_by_id.end(); it3++) {
ret = it3->second->serializeNumbering(exporter);
if (ret != UT_OK)
return ret;
}
OXML_ImageMap::iterator it4;
for (it4 = m_images_by_id.begin(); it4 != m_images_by_id.end(); it4++) {
ret = it4->second->serialize(exporter);
if (ret != UT_OK)
return ret;
}
OXML_SectionMap::iterator it5;
for (it5 = m_headers.begin(); it5 != m_headers.end(); it5++)
{
it5->second->setHandledHdrFtr(false); // Headers are not handled before serialization of sections
}
OXML_SectionMap::iterator it6;
for (it6 = m_footers.begin(); it6 != m_footers.end(); it6++)
{
it6->second->setHandledHdrFtr(false); // Footers are not handled before serialization of sections
}
OXML_SectionVector::size_type i;
for (i = 0; i < m_sections.size(); i++)
{
ret = m_sections[i]->serialize(exporter);
if(ret != UT_OK)
return ret;
}
ret = exporter->startSectionProperties();
if(ret != UT_OK)
return ret;
bool firstPageHdrFtr = false;
bool evenPageHdrFtr = false;
bool handled = false;
//serialize headers
for (it5 = m_headers.begin(); it5 != m_headers.end(); it5++) {
if(it5->second->hasFirstPageHdrFtr())
firstPageHdrFtr = true;
if(it5->second->hasEvenPageHdrFtr())
evenPageHdrFtr = true;
handled = it5->second->getHandledHdrFtr();
if(!handled)
{
it5->second->setHandledHdrFtr(true);
ret = it5->second->serializeHeader(exporter);
if (ret != UT_OK)
return ret;
}
}
//serialize footers
for (it6 = m_footers.begin(); it6 != m_footers.end(); it6++) {
if(it6->second->hasFirstPageHdrFtr())
firstPageHdrFtr = true;
if(it6->second->hasEvenPageHdrFtr())
evenPageHdrFtr = true;
handled = it6->second->getHandledHdrFtr();
if(!handled)
{
it6->second->setHandledHdrFtr(true);
ret = it6->second->serializeFooter(exporter);
if (ret != UT_OK)
return ret;
}
}
ret = exporter->setContinuousSection(TARGET_DOCUMENT);
if(ret != UT_OK)
return ret;
if(firstPageHdrFtr)
{
ret = exporter->setTitlePage();
if(ret != UT_OK)
return ret;
}
if(evenPageHdrFtr)
{
ret = exporter->setEvenAndOddHeaders();
if(ret != UT_OK)
return ret;
}
//set page size and orientation here
if(!m_pageWidth.empty() && !m_pageHeight.empty())
{
ret = exporter->setPageSize(TARGET_DOCUMENT, m_pageWidth.c_str(), m_pageHeight.c_str(), m_pageOrientation.c_str());
if(ret != UT_OK)
return ret;
}
//set page margins
if(!m_pageMarginTop.empty() && !m_pageMarginLeft.empty() && !m_pageMarginRight.empty() && !m_pageMarginBottom.empty())
{
ret = exporter->setPageMargins(TARGET_DOCUMENT, m_pageMarginTop.c_str(), m_pageMarginLeft.c_str(),
m_pageMarginRight.c_str(), m_pageMarginBottom.c_str());
if(ret != UT_OK)
return ret;
}
//set page columns
if(!m_colNum.empty() && !m_colSep.empty())
{
ret = exporter->setColumns(TARGET_DOCUMENT, m_colNum.c_str(), m_colSep.c_str());
if(ret != UT_OK)
return ret;
}
ret = exporter->finishSectionProperties();
if(ret != UT_OK)
return ret;
//serialize footnotes
OXML_SectionMap::iterator it7;
for (it7 = m_footnotes.begin(); it7 != m_footnotes.end(); it7++) {
ret = it7->second->serializeFootnote(exporter);
if (ret != UT_OK)
return ret;
}
//serialize endnotes
OXML_SectionMap::iterator it8;
for (it8 = m_endnotes.begin(); it8 != m_endnotes.end(); it8++) {
ret = it8->second->serializeEndnote(exporter);
if (ret != UT_OK)
return ret;
}
return exporter->finishDocument();
}
UT_Error OXML_Document::addToPT(PD_Document * pDocument)
{
UT_Error ret = UT_OK;
//Adding styles to PT
OXML_StyleMap::iterator it1;
for (it1 = m_styles_by_id.begin(); it1 != m_styles_by_id.end(); it1++) {
ret = it1->second->addToPT(pDocument);
if (ret != UT_OK) return ret;
}
_assignHdrFtrIds(); //Must be done before appending sections to the PT
//Adding sections to PT
OXML_SectionVector::iterator it2;
for (it2 = m_sections.begin(); it2 != m_sections.end(); it2++) {
//set page margins here
ret = (*it2)->setPageMargins(m_pageMarginTop, m_pageMarginLeft, m_pageMarginRight, m_pageMarginBottom);
if (ret != UT_OK) return ret;
ret = (*it2)->addToPT(pDocument);
if (ret != UT_OK) return ret;
}
//Adding header and footer sections to PT
OXML_SectionMap::iterator it3;
for (it3 = m_headers.begin(); it3 != m_headers.end(); it3++) {
ret = it3->second->addToPTAsHdrFtr(pDocument);
if (ret != UT_OK) return ret;
}
for (it3 = m_footers.begin(); it3 != m_footers.end(); it3++) {
ret = it3->second->addToPTAsHdrFtr(pDocument);
if (ret != UT_OK) return ret;
}
//Adding lists to PT
OXML_ListMap::iterator it4;
for (it4 = m_lists_by_id.begin(); it4 != m_lists_by_id.end(); it4++) {
ret = it4->second->addToPT(pDocument);
if (ret != UT_OK) return ret;
}
//Adding images to PT
OXML_ImageMap::iterator it5;
for (it5 = m_images_by_id.begin(); it5 != m_images_by_id.end(); it5++) {
ret = it5->second->addToPT(pDocument);
if (ret != UT_OK) return ret;
}
return applyPageProps(pDocument);
}
UT_Error OXML_Document::applyPageProps(PD_Document* pDocument)
{
const gchar* pageAtts[13];
int index = 0;
if(m_pageOrientation.empty())
m_pageOrientation = "portrait";
if(!m_pageWidth.empty())
{
pageAtts[index++] = "width";
pageAtts[index++] = m_pageWidth.c_str();
}
if(!m_pageHeight.empty())
{
pageAtts[index++] = "height";
pageAtts[index++] = m_pageHeight.c_str();
}
if(!m_pageOrientation.empty())
{
pageAtts[index++] = "orientation";
pageAtts[index++] = m_pageOrientation.c_str();
}
pageAtts[index++] = "units";
pageAtts[index++] = "in";
pageAtts[index++] = "page-scale";
pageAtts[index++] = "1.0";
fp_PageSize ps(UT_convertDimensionless(m_pageWidth.c_str()), UT_convertDimensionless(m_pageHeight.c_str()), DIM_IN);
pageAtts[index++] = "pagetype";
pageAtts[index++] = ps.getPredefinedName();
pageAtts[index] = 0; // To signal the end of the array
return pDocument->setPageSizeFromFile(pageAtts) ? UT_OK : UT_ERROR;
}
void OXML_Document::_assignHdrFtrIds()
{
OXML_SectionMap::iterator it;
UT_uint32 index = 0;
for (it = m_headers.begin(); it != m_headers.end(); it++) {
it->second->setAttribute("id", UT_convertToDimensionlessString(index, ".0") );
index++;
}
for (it = m_footers.begin(); it != m_footers.end(); it++) {
it->second->setAttribute("id", UT_convertToDimensionlessString(index, ".0") );
index++;
}
}
std::string OXML_Document::getMappedNumberingId(const std::string & numId) const
{
std::map<std::string, std::string>::const_iterator iter = m_numberingMap.find(numId);
if(iter == m_numberingMap.end())
return "";
return iter->second;
}
bool OXML_Document::setMappedNumberingId(const std::string & numId, const std::string & abstractNumId)
{
m_numberingMap.insert(std::make_pair(numId, abstractNumId));
return m_numberingMap.find(numId) != m_numberingMap.end();
}
std::string OXML_Document::getBookmarkName(const std::string & bookmarkId) const
{
std::map<std::string, std::string>::const_iterator iter = m_bookmarkMap.find(bookmarkId);
if(iter == m_bookmarkMap.end())
{
UT_DEBUGMSG(("FRT:Can't find bookmark name with id:%s\n", bookmarkId.c_str()));
return "";
}
return iter->second;
}
std::string OXML_Document::getBookmarkId(const std::string & bookmarkName) const
{
std::map<std::string, std::string>::const_iterator iter;
for (iter = m_bookmarkMap.begin(); iter != m_bookmarkMap.end(); iter++) {
if(!(iter->second).compare(bookmarkName))
{
return iter->first;
}
}
return "";
}
bool OXML_Document::setBookmarkName(const std::string & bookmarkId, const std::string & bookmarkName)
{
m_bookmarkMap.insert(std::make_pair(bookmarkId, bookmarkName));
return m_bookmarkMap.find(bookmarkId) != m_bookmarkMap.end();
}
void OXML_Document::setPageWidth(const std::string & width)
{
m_pageWidth = width;
}
void OXML_Document::setPageHeight(const std::string & height)
{
m_pageHeight = height;
}
void OXML_Document::setPageOrientation(const std::string & orientation)
{
m_pageOrientation = orientation;
}
void OXML_Document::setPageMargins(const std::string & top, const std::string & left, const std::string & right, const std::string & bottom)
{
m_pageMarginTop = top;
m_pageMarginLeft = left;
m_pageMarginRight = right;
m_pageMarginBottom = bottom;
}
void OXML_Document::setColumns(const std::string & colNum, const std::string & colSep)
{
m_colNum = colNum;
m_colSep = colSep;
}
|