From 8e8c0d8290409358488b2a04af81771989944552 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 22 Feb 2021 16:02:47 -0500 Subject: [PATCH] Add new placeFormXObject that takes a matrix reference --- ChangeLog | 7 +++++++ include/qpdf/QPDFPageObjectHelper.hh | 11 +++++++++++ libqpdf/QPDFPageObjectHelper.cc | 17 ++++++++++++++++- qpdf/qpdf.cc | 10 ++++------ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index bb9c8003..a3de5ecb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2021-02-22 Jay Berkenbilt + + * Add a new version of QPDFPageObjectHelper::placeFormXObject that + initializes the transformation matrix that was used so you don't + have to call both placeFormXObject and + getMatrixForFormXObjectPlacement. + 2021-02-21 Jay Berkenbilt * From qpdf CLI, --overlay and --underlay will copy annotations diff --git a/include/qpdf/QPDFPageObjectHelper.hh b/include/qpdf/QPDFPageObjectHelper.hh index 1e3bdb3e..2feb1199 100644 --- a/include/qpdf/QPDFPageObjectHelper.hh +++ b/include/qpdf/QPDFPageObjectHelper.hh @@ -309,6 +309,17 @@ class QPDFPageObjectHelper: public QPDFObjectHelper bool allow_shrink = true, bool allow_expand = false); + // Alternative version that also fills in the transformation + // matrix that was used. + QPDF_DLL + std::string placeFormXObject( + QPDFObjectHandle fo, std::string const& name, + QPDFObjectHandle::Rectangle rect, + QPDFMatrix& cm, + bool invert_transformations = true, + bool allow_shrink = true, + bool allow_expand = false); + // Return the transformation matrix that translates from the given // form XObject's coordinate system into the given rectangular // region on the page. The parameters have the same meaning as for diff --git a/libqpdf/QPDFPageObjectHelper.cc b/libqpdf/QPDFPageObjectHelper.cc index aa7eb912..4460415b 100644 --- a/libqpdf/QPDFPageObjectHelper.cc +++ b/libqpdf/QPDFPageObjectHelper.cc @@ -1070,7 +1070,22 @@ QPDFPageObjectHelper::placeFormXObject( bool invert_transformations, bool allow_shrink, bool allow_expand) { - QPDFMatrix cm = getMatrixForFormXObjectPlacement( + QPDFMatrix cm; + return placeFormXObject( + fo, name, rect, cm, invert_transformations, + allow_shrink, allow_expand); +} + +std::string +QPDFPageObjectHelper::placeFormXObject( + QPDFObjectHandle fo, std::string const& name, + QPDFObjectHandle::Rectangle rect, + QPDFMatrix& cm, + bool invert_transformations, + bool allow_shrink, + bool allow_expand) +{ + cm = getMatrixForFormXObjectPlacement( fo, rect, invert_transformations, allow_shrink, allow_expand); return ( "q\n" + diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index 6f587d57..0935e4f8 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -5191,20 +5191,18 @@ static void do_under_overlay_for_page( pdf.copyForeignObject( from_page.getFormXObjectForPage()); } - auto cm = dest_page.getMatrixForFormXObjectPlacement( - fo[from_pageno], - dest_page.getTrimBox().getArrayAsRectangle()); - dest_page.copyAnnotations( - from_page, cm, dest_afdh, make_afdh(from_page)); // If the same page is overlaid or underlaid multiple times, // we'll generate multiple names for it, but that's harmless // and also a pretty goofy case that's not worth coding // around. std::string name = resources.getUniqueResourceName("/Fx", min_suffix); + QPDFMatrix cm; std::string new_content = dest_page.placeFormXObject( fo[from_pageno], name, - dest_page.getTrimBox().getArrayAsRectangle()); + dest_page.getTrimBox().getArrayAsRectangle(), cm); + dest_page.copyAnnotations( + from_page, cm, dest_afdh, make_afdh(from_page)); if (! new_content.empty()) { resources.mergeResources(