libpagemaker 0.0.4 drew a double-sided publication's pages on top of each other. Each PAGE record is one page in order, and a spread's objects sit on either of its two records, measured from the spine; the reader took every record for a right page and put left-of-spine objects on the page before, so when a left page's record held its spread, that spread landed on the previous page and the left page stayed blank. Now each record's spread (left and right page) is worked out from the page number, using Document Setup's start page (an even start makes page 1 a left page). An object reaching across the spine is drawn whole on both pages, each copy placed for its own page and marked "pmd:across-spine" (only the page's bounds hide the part on the other page, so a viewer that shows more than the page must cut it itself); a text box goes only on the page holding its centre. The reader also read only the first PAGE record, so a publication whose pages fill several PAGE records (one 26-page sample keeps them in four) showed only its first few; all are read now, each once, up to 10000 pages, and a damaged later record ends the reading with the pages before it kept (written for johnmcm.com, MPL-2.0 like libpagemaker). --- a/src/lib/OutputShape.h +++ b/src/lib/OutputShape.h @@ -35,6 +35,7 @@ class OutputShape std::vector m_paraProps; librevenge::RVNGBinaryData m_bitmap; double m_width,m_height; + bool m_acrossSpine = false; public: OutputShape(bool isClosed, int shape, double rotation, double skew, const PMDFillProperties &fillProps, const PMDStrokeProperties &strokeProps) @@ -113,6 +114,17 @@ public: return m_paraProps; } + /* Drawn on both pages of a spread: it reaches across the spine. */ + bool getAcrossSpine() const + { + return m_acrossSpine; + } + + void setAcrossSpine(bool acrossSpine) + { + m_acrossSpine = acrossSpine; + } + librevenge::RVNGBinaryData getBitmap() const { return m_bitmap; --- a/src/lib/PMDCollector.cpp +++ b/src/lib/PMDCollector.cpp @@ -164,7 +164,7 @@ void writeBorder(librevenge::RVNGPropertyList &props, const char *const name, co PMDCollector::PMDCollector() : m_pageWidth(), m_pageHeight(), m_pages(), m_color(),m_font(), - m_doubleSided(false) + m_doubleSided(false), m_startPage(1) { } void PMDCollector::setDoubleSided(bool doubleSided) @@ -172,6 +172,16 @@ void PMDCollector::setDoubleSided(bool doubleSided) m_doubleSided = doubleSided; } +void PMDCollector::setStartPage(unsigned startPage) +{ + m_startPage = startPage; +} + +unsigned PMDCollector::numPages() const +{ + return m_pages.size(); +} + /* State-mutating functions */ void PMDCollector::setPageWidth(PMDShapeUnit pageWidth) { @@ -219,6 +229,8 @@ void PMDCollector::paintShape(const OutputShape &shape, } librevenge::RVNGPropertyList points; points.insert("svg:points", vertices); + if (shape.getAcrossSpine()) + points.insert("pmd:across-spine", true); PMDFillProperties fillProps = shape.getFillProperties(); PMDStrokeProperties strokeProps = shape.getStrokeProperties(); @@ -496,6 +508,8 @@ void PMDCollector::paintShape(const OutputShape &shape, else if (shape.shapeType() == SHAPE_TYPE_BITMAP) { librevenge::RVNGPropertyList props; + if (shape.getAcrossSpine()) + props.insert("pmd:across-spine", true); props.insert("svg:x", shape.getPoint(0).m_x,librevenge::RVNG_INCH); props.insert("svg:y", shape.getPoint(0).m_y,librevenge::RVNG_INCH); props.insert("svg:width", shape.getWidth(),librevenge::RVNG_INCH); @@ -525,6 +539,8 @@ void PMDCollector::paintShape(const OutputShape &shape, PMD_DEBUG_MSG(("Rotation is %f \n",rotation)); PMD_DEBUG_MSG(("Skew is %f \n",skew)); librevenge::RVNGPropertyList propList; + if (shape.getAcrossSpine()) + propList.insert("pmd:across-spine", true); if (false) { @@ -671,41 +687,72 @@ void PMDCollector::writePage(const PMDPage & /*page*/, painter->endPage(); } +/* In a double-sided publication each PAGE record is one page, in page order; + a page is a right page when its number (start page + index) is odd. The + shapes of a spread are listed on either of its two records, with x measured + from the spine: left of it is the left page, right of it the right page. + Objects on the pasteboard beyond a page's outer edge stay with that page, on + both sides alike, as one-sided pages keep theirs. */ void PMDCollector::fillOutputShapesByPage_TwoSided(PageShapesList_t &pageShapes) const { - pageShapes.assign(m_pages.size() * 2 - 1, PageShapes_t()); // the first "page" only has right side + const std::size_t numPages = m_pages.size(); + pageShapes.assign(numPages, PageShapes_t()); double centerToEdge_x = m_pageWidth.get_value_or(0).toInches() / 2; double centerToEdge_y = m_pageHeight.get_value_or(0).toInches() / 2; InchPoint translateForLeftPage(centerToEdge_x * 2, centerToEdge_y); InchPoint translateForRightPage(0, centerToEdge_y); + // An object counts as reaching across the spine only past this (1 pt). + const double overlap = 1.0 / 72; + const bool firstIsRight = (m_startPage % 2) == 1; - for (unsigned i = 0; i < m_pages.size(); ++i) + for (std::size_t i = 0; i < numPages; ++i) { - const bool leftPageExists = (i > 0); + const bool isRight = ((i % 2) == 0) == firstIsRight; + const bool hasLeft = !isRight || i > 0; + const bool hasRight = isRight || i + 1 < numPages; + const std::size_t leftIdx = isRight ? i - 1 : i; + const std::size_t rightIdx = isRight ? i : i + 1; const PMDPage &page = m_pages[i]; for (unsigned j = 0; j < page.numShapes(); ++j) { - std::shared_ptr right = newOutputShape(page.getShape(j), translateForRightPage); - if (right->getBoundingBox().second.m_x >= 0) + std::shared_ptr right = newOutputShape(page.getShape(j), translateForRightPage); + const double x0 = right->getBoundingBox().first.m_x; + const double x1 = right->getBoundingBox().second.m_x; + const bool centreRight = x0 + x1 >= 0; + bool onRight = x1 > overlap || x0 >= 0; + bool onLeft = x0 < -overlap || x1 < 0; + if (!onRight && !onLeft) + (centreRight ? onRight : onLeft) = true; + // A text box crossing the spine goes on one page only (the one holding + // its centre, if that page exists) so its text is not listed twice. + // Any other object crossing it is drawn whole on both pages, each copy + // placed for its own page and marked "pmd:across-spine": the page's + // bounds hide the part on the other page, so the reader of this output + // can tell such a copy from an object that really runs off the page. + if (onRight && onLeft && right->shapeType() == SHAPE_TYPE_TEXTBOX) { - pageShapes[i].push_back(right); - continue; + if (centreRight ? hasRight : !hasLeft) + onLeft = false; + else + onRight = false; } - if (leftPageExists) + onRight = onRight && hasRight; + onLeft = onLeft && hasLeft; + if (onRight) { - std::shared_ptr left = newOutputShape(page.getShape(j), translateForLeftPage); - if (left->getBoundingBox().first.m_x <= centerToEdge_x * 2) - { - pageShapes[i - 1].push_back(left); - } + right->setAcrossSpine(onLeft); + pageShapes[rightIdx].push_back(right); + } + if (onLeft) + { + std::shared_ptr left = newOutputShape(page.getShape(j), translateForLeftPage); + left->setAcrossSpine(onRight); + pageShapes[leftIdx].push_back(left); } } } - - if ((pageShapes.size() > 1) && pageShapes.back().empty()) // the last "page" only has left side - pageShapes.pop_back(); } void PMDCollector::fillOutputShapesByPage_OneSided(PageShapesList_t &pageShapes) const --- a/src/lib/PMDCollector.h +++ b/src/lib/PMDCollector.h @@ -51,6 +51,7 @@ class PMDCollector std::vector m_color; std::vector m_font; bool m_doubleSided; + unsigned m_startPage; void writePage(const PMDPage &, librevenge::RVNGDrawingInterface *, @@ -69,11 +70,13 @@ public: void setPageWidth(PMDShapeUnit); void setPageHeight(PMDShapeUnit); void setDoubleSided(bool); + void setStartPage(unsigned); void addShapeToPage(unsigned pageID, const std::shared_ptr &shape); void addColor(const PMDColor &color); void addFont(const PMDFont &font); unsigned addPage(); + unsigned numPages() const; /* Output functions */ void draw(librevenge::RVNGDrawingInterface *) const; --- a/src/lib/PMDParser.cpp +++ b/src/lib/PMDParser.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -254,7 +255,10 @@ void PMDParser::parseGlobalInfo(const PMDRecordContainer &container) const unsigned opts = readU8(m_input, m_bigEndian); - skip(m_input, 0x35); + skip(m_input, 0x2d); + // Document Setup's "Start page #": an even start makes page 1 a left page. + const uint16_t startPage = readU16(m_input, m_bigEndian); + skip(m_input, 0x6); int16_t left = 0; int16_t right = 0; @@ -265,6 +269,7 @@ void PMDParser::parseGlobalInfo(const PMDRecordContainer &container) readDims(m_input, m_bigEndian, right, bottom); m_collector->setDoubleSided(m_bigEndian ? opts & 0x40 : opts & 0x2); + m_collector->setStartPage(startPage >= 1 && startPage <= 9999 ? startPage : 1); m_collector->setPageWidth(right - left); m_collector->setPageHeight(bottom - top); } @@ -818,6 +823,9 @@ void PMDParser::parsePages(const PMDRecordContainer &container) for (unsigned i = 0; i < container.m_numRecords; ++i) { + // PageMaker allows 999 pages; a crafted table of contents gets no more than this. + if (m_collector->numPages() >= 10000) + return; seekToRecord(m_input, container, i); skip(m_input, 2); @@ -985,7 +993,29 @@ void PMDParser::parse() if (i != m_records.end() && !(i->second.empty())) { - parsePages(m_recordsInOrder[i->second[0]]); + // A long publication keeps its pages in several PAGE records, in page order. + // A record listed twice is read once; a damaged later record ends the + // reading there, keeping the pages before it (the file opened before). + std::set seen; + for (std::size_t k = 0; k < i->second.size(); ++k) + { + const PMDRecordContainer &pages = m_recordsInOrder[i->second[k]]; + if (!seen.insert(pages.m_offset).second) + continue; + if (k == 0) + { + parsePages(pages); + continue; + } + try + { + parsePages(pages); + } + catch (...) + { + break; + } + } } else {