Page.cpp 21.9 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (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, see <http://www.gnu.org/licenses/>.
 */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#include "Page.h"
#include "Document.h"

#include "ContentHandler.h"
#include "Exception.h"
#include "MediaBoxElementHandler.h"
#include "CropBoxElementHandler.h"
#include "TypeElementHandler.h"
#include "RemoveHimSelfHandler.h"
#include "AnnotsHandler.h"
#include "RotationHandler.h"
#include "FlateDecode.h"
#include "Utils.h"
#include "Rectangle.h"
#include "Filter.h"
#include <iostream>
#include <string.h>
#include "Parser.h"

35 36
#include "core/memcheck.h"

37 38 39
using namespace merge_lib;


40
Page::Page(unsigned int pageNumber): _root(NULL),_pageNumber(pageNumber), _rotation(0)
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
{

}



Page::~Page()
{

}


std::string & Page::getPageContent()
{
   return _root->getObjectContent();
}

const Object::Children &  Page::getPageRefs()
{
   return _root->getChildren();
}


void Page::recalculateObjectNumbers(unsigned int & newNumber)
{
   _root->recalculateObjectNumbers(newNumber);
}

Object * Page::pageToXObject(std::vector<Object *> & allObjects, std::vector<Object *> & annots, bool isCloneNeeded)
{
   Object * xObject = (isCloneNeeded) ? _root->getClone(allObjects) : _root;
   return _pageToXObject(xObject, annots);
}


Object * Page::_pageToXObject(Object *& page, std::vector<Object *> & annots)
{   
   RemoveHimselfHandler   * removeParent = new RemoveHimselfHandler(page, "/Parent");
   RemoveHimselfHandler   * removeBleedBox = new RemoveHimselfHandler(page, "/BleedBox");
   RemoveHimselfHandler   * removeTrimBox = new RemoveHimselfHandler(page, "/TrimBox");
   RemoveHimselfHandler   * removeArtBox = new RemoveHimselfHandler(page, "/ArtBox");
   RemoveHimselfHandler   * removeBoxColorInfo = new RemoveHimselfHandler(page, "/BoxColorInfo");
   RemoveHimselfHandler   * removeRotate = new RemoveHimselfHandler(page, "/Rotate");
   RemoveHimselfHandler   * removeThumb = new RemoveHimselfHandler(page, "/Thumb");
   RemoveHimselfHandler   * removeB = new RemoveHimselfHandler(page, "/B");
   RemoveHimselfHandler   * removeDur = new RemoveHimselfHandler(page, "/Dur");
   RemoveHimselfHandler   * removeTrans = new RemoveHimselfHandler(page, "/Trans");
   AnnotsHandler          * removeAnnots = new AnnotsHandler(page, "/Annots", annots);
   RemoveHimselfHandler   * removeAA = new RemoveHimselfHandler(page, "/AA");
   RemoveHimselfHandler   * removeID = new RemoveHimselfHandler(page, "/ID");
   RemoveHimselfHandler   * removePZ = new RemoveHimselfHandler(page, "/PZ");
   RemoveHimselfHandler   * removeSeparationInfo = new RemoveHimselfHandler(page, "/SeparationInfo");
   RemoveHimselfHandler   * removeTabs = new RemoveHimselfHandler(page, "/Tabs");
   RemoveHimselfHandler   * removeTemplateInstantiated = new RemoveHimselfHandler(page, "/TemplateInstantiated");
   RemoveHimselfHandler   * removePresSteps = new RemoveHimselfHandler(page, "/PresSteps");
   RemoveHimselfHandler   * removeUserUnit = new RemoveHimselfHandler(page, "/UserUnit");
   RemoveHimselfHandler   * removeVP = new RemoveHimselfHandler(page, "/VP");
   ContentHandler         * contentHandler = new ContentHandler(page, "/Contents");   
   CropBoxElementHandler  * cropBoxElementHandler = new CropBoxElementHandler(page);   
   MediaBoxElementHandler * mediaBoxElementHandler = new MediaBoxElementHandler(page);
   TypeElementHandler     * typeElementHandler = new TypeElementHandler(page);

   cropBoxElementHandler->addNextHandler(mediaBoxElementHandler);
   mediaBoxElementHandler->addNextHandler(removeParent);
   removeParent->addNextHandler(removeBleedBox);
   removeBleedBox->addNextHandler(removeTrimBox);
   removeTrimBox->addNextHandler(removeArtBox);
   removeArtBox->addNextHandler(removeBoxColorInfo);
   removeBoxColorInfo->addNextHandler(removeRotate);
   removeRotate->addNextHandler(removeThumb);
   removeThumb->addNextHandler(removeB);
   removeB->addNextHandler(removeDur);
   removeDur->addNextHandler(removeTrans);
   removeTrans->addNextHandler(removeAnnots);
   removeAnnots->addNextHandler(removeAA);
   removeAA->addNextHandler(removeID);
   removeID->addNextHandler(removePZ);
   removePZ->addNextHandler(removeSeparationInfo);
   removeSeparationInfo->addNextHandler(removeTabs);
   removeTabs->addNextHandler(removeTemplateInstantiated);
   removeTemplateInstantiated->addNextHandler(removePresSteps);
   removePresSteps->addNextHandler(removeUserUnit);
   removeUserUnit->addNextHandler(removeVP);
   removeVP->addNextHandler(typeElementHandler);
   typeElementHandler->addNextHandler(contentHandler);   

   cropBoxElementHandler->processObjectContent();
   cropBoxElementHandler->changeObjectContent();

   delete cropBoxElementHandler;
   return page;
}


std::string _getContentOfContentObject(MergePageDescription & description)
{
   std::string content("<<\n/Length ");
   std::string stream = "";
   if(!description.skipBasePage)
   {
      stream.append("1.000000 0 0 1.000000 0 0 cm\n"
         "q\n");
      stream.append(description.basePageTransformation.getCMT());
      stream.append("/OriginalPage2 Do\n"
         "Q\n");

   }
   if(!description.skipOverlayPage)
   {    
      stream.append("1.000000 0 0 1.000000 0 0 cm\n"
         "q\n");
      stream.append(description.overlayPageTransformation.getCMT());
      stream.append("/OriginalPage1 Do\n"
         "Q\n");
   }
   FlateDecode encoder;
   encoder.encode(stream);
   content.append(Utils::uIntToStr(stream.size()));
   content.append("\n/Filter /FlateDecode\n>>\nstream\n");
   content.append(stream);
   content.append("endstream\n");
   return content;
}

void _recalculateAnnotsCoordinates(Object * annotation, 
                                   const Rectangle & basePagesRectangle,
                                   const Rectangle & outputPagesRectangle,
                                   const MergePageDescription & description)
{
170 171
   Q_UNUSED(outputPagesRectangle);
   Q_UNUSED(basePagesRectangle);
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
   std::string annotsRectangleName("/Rect");
   Object * objectWithRectangle;
   unsigned int fake;
   annotation->findObject(annotsRectangleName, objectWithRectangle, fake);
   std::string annotContent = objectWithRectangle->getObjectContent();

   Rectangle annotsRectangle(annotsRectangleName.c_str(), annotContent);

   //we move annotation from base page to output page
   //that's way annotation should be scaled before all transformations.
   //Annotation's coordinates should be recalculated according to new 
   //page width and height

   annotsRectangle.recalculateInternalRectangleCoordinates(description.basePageTransformation.getAnnotsTransformations());
   annotsRectangle.updateRectangle(annotation, " ");
}

// function updates parent reference of annotation with new page object
static void _updateAnnotParentPage(Object *annotation,Object *newParentPage)
{
   if( annotation )
   {
      std::string strP = "/P";
      std::string &annotContent = annotation->getObjectContent();

      size_t startOfP = Parser::findTokenName(annotContent,strP);
198
      if((int) startOfP == -1 )
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
      {
         return;
      }
      size_t endOfP = Parser::findEndOfElementContent(annotContent,startOfP + strP.size());
      
      // lets find object with reference to parent
      std::vector<Object *> children = annotation->getChildrenByBounds(startOfP,endOfP);
      if( children.size() == 0 ) 
      {
         return;
      }

      Object *childWithP = children[0];
      if( childWithP )
      {
         Object::ReferencePositionsInContent pagePosition = annotation->removeChild(childWithP);
         annotation->eraseContent(startOfP,endOfP-startOfP);
         std::stringstream strout;
         strout <<"/P "<<newParentPage->getObjectNumber()<<" "<<newParentPage->getgenerationNumber()<<" R\n";
         // to compensate posible deviation
         for(size_t i =strout.str().size();i<endOfP-startOfP;i++)
         {
            strout<<" ";
         }
         annotation->insertToContent(startOfP,strout.str());
         annotation->addChild(newParentPage,pagePosition);
      }
   }
}

// function performs adjusting of some color parameters of annotation
// to avoid interference with overlay content
static void _updateAnnotFormColor(Object *annotation )
{
   std::string &objectContent = annotation->getObjectContent();
234
   if((int) objectContent.find("/Widget") == -1 )
235 236 237 238
   {
      return;
   }
   size_t startOfAP = Parser::findTokenName(objectContent,"/AP");
239
   if((int) startOfAP == -1 )
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
   {
      return;
   }
   size_t endOfAP = objectContent.find(">>", startOfAP);
   std::vector<Object *>  aps = annotation->getChildrenByBounds(startOfAP, endOfAP);
   for(size_t i = 0; i < aps.size(); ++i)
   {
      Object * childWithAP = aps[i];
      if( !childWithAP->hasStream() )
      {
         continue;
      }
      // first lets obtain and decode stream of Annotation appearrence stream
      std::string & content = childWithAP->getObjectContent();

      Filter filter(childWithAP);
      std::string decodedStream;
      filter.getDecodedStream(decodedStream);

      // lets iterate over stream and find operator f and remove it!
      size_t beg = 0;
      size_t found = 0;
      std::string token;
      while(Parser::getNextWord(token,decodedStream,beg,&found))
      {
         if( token == "f" || token == "F" )
         {
267
             if((int) found != -1 )
268 269 270 271 272 273 274 275 276
            {
               decodedStream[found] = ' ';
            }
            break;
         }
      }
      // Then we need to update Filter section (if any)
      std::string filterStr = "/Filter";
      size_t startOfFlate = Parser::findTokenName(content,filterStr);
277
      if((int) startOfFlate != -1 )
278 279 280 281 282 283 284 285 286 287 288 289
      {
         size_t endOfFlate = Parser::findEndOfElementContent(content,startOfFlate+filterStr.size());
         childWithAP->eraseContent(startOfFlate,endOfFlate-startOfFlate);
         //encode and put new stream to object content
         childWithAP->insertToContent(startOfFlate,"/Filter /FlateDecode ");
         FlateDecode flate;
         flate.encode(decodedStream);
      }

      // update the length field
      std::string lengthStr = "/Length";
      size_t startOfLength = Parser::findTokenName(content,lengthStr,0);
290
      if((int) startOfLength != -1 )
291 292 293 294 295 296 297 298 299 300
      {
         size_t endOfLength = Parser::findEndOfElementContent(content,startOfLength + lengthStr.size());
         childWithAP->eraseContent(startOfLength,endOfLength-startOfLength);
         std::stringstream ostr;
         ostr<<"/Length "<< decodedStream.size()<<"\n";
         childWithAP->insertToContent(startOfLength,ostr.str());

         // update the stream of object with new content
         std::string stream("stream");
         size_t leftBoundOfContentStream = content.find(stream);
301
         if((int) leftBoundOfContentStream != -1 )
302 303
         {
            size_t rightBoundOfContentStream = content.find("endstream", leftBoundOfContentStream);
304
            if((int) rightBoundOfContentStream == -1 )
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
            {
               rightBoundOfContentStream = content.size() - 1;
            }
            childWithAP->eraseContent(leftBoundOfContentStream, rightBoundOfContentStream - leftBoundOfContentStream);
            decodedStream.insert(0,"\nstream\n");
            childWithAP->insertToContent(leftBoundOfContentStream,decodedStream);
            childWithAP->appendContent("endstream\n");
            childWithAP->forgetStreamInFile();
         }
      }
   }
}
// sometimes page object does not have resources,  
// they are inherited from parent object
// this method processes such cases and insert resources from parent to page 
// for correct X-Object transformation
static void processBasePageResources(Object *basePage)
{
   if( basePage == NULL )
   {
      return;
   }
   std::string resourceToken = "/Resources";
328
   if((int) Parser::findTokenName(basePage->getObjectContent(),resourceToken) == -1 )
329 330 331 332 333 334 335
   {
      // it seems base page does not have resources, they can be located in parent!
      Object *resource = basePage->findPatternInObjOrParents(resourceToken);
      if( resource )
      {
         std::string &resContStr = resource->getObjectContent();
         size_t startOfRes = Parser::findTokenName(resContStr,resourceToken);
336
         if((int) startOfRes == -1 )
337 338 339 340 341
         {
            // no resources at all
            return;
         }
         size_t endOfRes = Parser::findEndOfElementContent(resContStr,startOfRes + resourceToken.size());
342
         if((int) endOfRes == -1 )
343 344 345 346 347 348
         {
            return; // broken resources
         }
         std::string resourceContent = resContStr.substr(startOfRes,endOfRes-startOfRes);

         size_t positionToInsert = basePage->getObjectContent().find("<<");
349
         if((int) positionToInsert == -1 )
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
         {
            positionToInsert = 0;
            resourceContent.insert(0,"<<");
            resourceContent.append(">>");
         }
         else
         {
            positionToInsert += strlen("<<");
         }
         // insert obtained resources to base page
         basePage->insertToContent(positionToInsert,resourceContent);

         // if resource contains childs, then we need to add reference to them to current object
         std::vector<Object *>  resChilds = resource->getChildrenByBounds(startOfRes, endOfRes);
         std::vector<Object *>::const_iterator objectIt(resChilds.begin());

         const Object::Children & children = resource->getChildren();

         for(; objectIt != resChilds.end(); objectIt++ )
         {
            Object::Children::const_iterator  childrenIt = children.find( (*objectIt)->getObjectNumber());
            if( childrenIt != children.end() )
            {
               Object::ReferencePositionsInContent refPositionInCont = (*childrenIt).second.second;

               Object::ReferencePositionsInContent::iterator positionIt(refPositionInCont.begin());

               Object::ReferencePositionsInContent newPositions;
               for( ;positionIt != refPositionInCont.end(); positionIt++ )
               {
                  newPositions.push_back( (*positionIt) - startOfRes + positionToInsert );
               }

               basePage->addChild( (*objectIt),newPositions );
            }
         }
      }
   }
}

std::string Page::_getMergedPageContent(  unsigned int & contentPosition, 
                                        unsigned int & parentPosition, 
                                        unsigned int & originalPage1Position, 
                                        unsigned int & originalPage2Position,
                                        std::pair<unsigned int, unsigned int> originalPageNumbers, 
                                        const MergePageDescription & description,
                                        Object * basePage,
                                        const std::vector<Object *> & annots,
                                        std::vector <Object::ChildAndItPositionInContent> & annotsPositions
                                        )
{
   std::string content("<<\n/Type /Page\n");   
   content.append("/Contents ");
   contentPosition = content.size();
   //object number 1 will be recalculated during serialization
   content.append("1 0 R\n");
   Rectangle mediaBox("/MediaBox");
   mediaBox.x2 = description.outPageWidth;
   mediaBox.y2 = description.outPageHeight;
   mediaBox.appendRectangleToString(content, " ");
   content.append("/Parent ");
   parentPosition = content.size();
   //object number 1 will be recalculated during serialization
   content.append("1 0 R\n");   
   content.append("/Resources <<\n"
      "/ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]\n"
      "/XObject <<\n");
   if(!description.skipOverlayPage)
   {
      content.append("/OriginalPage1 ");
      originalPage1Position = content.size();
      content.append(Utils::uIntToStr(originalPageNumbers.first));
      content.append(" 0 R\n");
   }
   if(!description.skipBasePage)
   {
      content.append("/OriginalPage2 ");
      originalPage2Position = content.size();
      content.append(Utils::uIntToStr(originalPageNumbers.second));
      content.append(" ");
      content.append(Utils::uIntToStr(basePage->getgenerationNumber()));
      content.append(" R\n");
   }
   content.append(">>\n>>\n");

   content.append("/Annots [ ");
   if(!description.skipBasePage)
   {
      Rectangle basePageRectangle("/BBox", basePage->getObjectContent());
      for(size_t i = 0; i < annots.size(); ++i)
      {
         _updateAnnotFormColor(annots[i]);
         _recalculateAnnotsCoordinates(annots[i], basePageRectangle, mediaBox, description);
         Object::ReferencePositionsInContent annotationPosition;
         annotationPosition.push_back(content.size());
         Object::ChildAndItPositionInContent annotAndItPosition(annots[i], annotationPosition);
         annotsPositions.push_back(annotAndItPosition);
         content.append(Utils::uIntToStr(annots[i]->getObjectNumber()));
         content.append(" ");
         content.append(Utils::uIntToStr(annots[i]->getgenerationNumber()));
         content.append(" R ");
      }
   }
   content.append("] \n>>\n");

   return content;
}

void Page::merge(Page * sourcePage, Document * parentDocument, MergePageDescription & description, bool isPageDuplicated)
{   
   if( sourcePage == NULL )
   {
      description.skipBasePage = true;
   }
   if(!description.skipOverlayPage)
   {
      // Lets recalculate final transformation of overlay page
      // before it will be places into XObject
      Rectangle mediaBox("/MediaBox",_root->getObjectContent());
      description.overlayPageTransformation.recalculateTranslation(mediaBox.getWidth(),mediaBox.getHeight());

      std::vector<Object *> fake;
      _pageToXObject(_root, fake);
   }

   std::vector<Object *> toAllObjects;
   std::vector<Object *> annotations;
   Object * sourcePageToXObject = 0;
   if(!description.skipBasePage)
   {  
      RotationHandler rotationHandler(sourcePage->_root, "/Rotate", *this);
      rotationHandler.processObjectContent();
      description.basePageTransformation.addRotation(_rotation);

484
      if((int) sourcePage->_root->getObjectContent().find("/Annots") != -1 )
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
      {
         Object *crop = sourcePage->_root->findPatternInObjOrParents("/CropBox");
         if( crop )
         {
            // we need to calculate special compensational shifting
            // for annotations if cropbox is starting not from 0,0
            Rectangle mediaBox("/CropBox", crop->getObjectContent());
            if( !Utils::doubleEquals(mediaBox.x1,0) || !Utils::doubleEquals(mediaBox.y1,0) )
            {
               double shiftX = Utils::doubleEquals(mediaBox.x1,0)?0:-mediaBox.x1;
               double shiftY = Utils::doubleEquals(mediaBox.y1,0)?0:-mediaBox.y1;
               Translation compensation(shiftX,shiftY);
               description.basePageTransformation.addAnnotsTransformation(compensation);
            }
         }
      }
      processBasePageResources(sourcePage->_root);
      sourcePageToXObject = sourcePage->pageToXObject(toAllObjects, annotations, isPageDuplicated);
      Rectangle mediaBox("/BBox", sourcePageToXObject->getObjectContent());
      description.basePageTransformation.recalculateTranslation(mediaBox.getWidth(),mediaBox.getHeight());      
   }

   Object * catalog = 0;
   unsigned int fake;
   if(!parentDocument->getDocumentObject()->findObject(std::string("/Kids"), catalog, fake))
   {
      std::string error("Wrong document ");
      error.append("There is no object with Kids field");
      throw Exception(error);
   }
   Object::ReferencePositionsInContent pagePosition = catalog->removeChild(_root);
   //create merged Page
   unsigned int contentPosition, parentPosition, originalPage1Position, originalPage2Position;
   std::pair<unsigned int, unsigned int> originalPageNumbers(_root->getObjectNumber(), 0);
   if(!description.skipBasePage)
      originalPageNumbers.second = sourcePageToXObject->getObjectNumber();
   std::vector <Object::ChildAndItPositionInContent> annotsAndItPositions;
   std::string mergedPageContent = _getMergedPageContent(contentPosition, 
      parentPosition, 
      originalPage1Position, 
      originalPage2Position, 
      originalPageNumbers, 
      description,
      sourcePageToXObject,
      annotations,
      annotsAndItPositions
      );
   Object * mergedPage = new Object(_root->getObjectNumber(), _root->getgenerationNumber(), mergedPageContent);
   toAllObjects.push_back(mergedPage);
   std::vector < unsigned int > contentPositionVec, parentPositionVec, originalPage1PositionVec, originalPage2PositionVec;
   contentPositionVec.push_back(contentPosition);
   parentPositionVec.push_back(parentPosition);
   originalPage1PositionVec.push_back(originalPage1Position);
   originalPage2PositionVec.push_back(originalPage2Position);
   Object * contentOfMergedPage = new Object(1, 0, _getContentOfContentObject(description));
   toAllObjects.push_back(contentOfMergedPage);
   parentDocument->addToAllObjects(toAllObjects);
   mergedPage->addChild(contentOfMergedPage, contentPositionVec);
   mergedPage->addChild(catalog, parentPositionVec);
   if(!description.skipOverlayPage)
      mergedPage->addChild(_root, originalPage1PositionVec);
   if(!description.skipBasePage)
      mergedPage->addChild(sourcePageToXObject, originalPage2PositionVec);
      // Annotation parent page should be changed, since we moved old page
      // to Xobject
   if( !description.skipBasePage )
   {
      for(size_t i = 0; i< annotations.size();i++)
      {
         _updateAnnotParentPage(annotations[i],mergedPage);
      }
   }
   for(size_t i = 0; i < annotsAndItPositions.size(); ++i)
   {
      mergedPage->addChild(annotsAndItPositions[i].first, annotsAndItPositions[i].second);
   }
   catalog->addChild(mergedPage, pagePosition);
   _root = mergedPage; 
}