def _get_selected_edge(self, pos: QPointF, transform: QTransform, horizontal_selection: bool): x1, x2 = self.x, self.x + self.width y1, y2 = self.y, self.y + self.height x, y = pos.x(), pos.y() spacing = 5 spacing /= transform.m11() if horizontal_selection else transform.m22() if horizontal_selection: x1a, x1b = x1 - spacing, x1 + spacing y1a, y1b = y1, y2 x2a, x2b = x2 - spacing, x2 + spacing y2a, y2b = y1, y2 else: x1a, x1b, x2a, x2b = x1, x2, x1, x2 y1a, y1b = min(y1 - spacing, y1 + spacing), max(y1 - spacing, y1 + spacing) y2a, y2b = min(y2 - spacing, y2 + spacing), max(y2 - spacing, y2 + spacing) if x1a < x < x1b and y1a < y < y1b: self.selected_edge = 0 return 0 if x2a < x < x2b and y2a < y < y2b: self.selected_edge = 1 return 1 self.selected_edge = None return None
def serialize_transform(matrix: QTransform) -> t.Sequence[int]: return ( matrix.m11(), matrix.m12(), matrix.m13(), matrix.m21(), matrix.m22(), matrix.m23(), matrix.m31(), matrix.m32(), matrix.m33(), )
def print_transform(text: str, transform:QTransform = QTransform()): print(text) print(transform.m11(), " ", end="") print(transform.m12(), " ", end="") print(transform.m13(), " ", end="") print(" ") print(transform.m21(), " ", end="") print(transform.m22(), " ", end="") print(transform.m23(), " ", end="") print(" ") print(transform.m31(), " ", end="") print(transform.m32(), " ", end="") print(transform.m33(), " ", end="") print(" ")
def hash_transform(t: QTransform) -> int: return hash( ( t.m11(), t.m12(), t.m21(), t.m22(), t.dx(), t.dy(), t.m13(), t.m23(), t.m33(), ) )
def generatePDFFromPDF(fileName, inPage, cropRect, outSize, pageLayout, trim=False, registrationMarks=False, progress=None): assert isinstance(inPage, InputPDFPage) inReaderPage = inPage.getPyPDF2PageObject() overlayPage = None if trim or registrationMarks: with tempfile.TemporaryDirectory(prefix='pdfXplode-tmpPDF') as d: # Print our trimming and registration marks to a temporary # PDF so we can merge it with the input PDF. Sadly, # QPrinter has no way to print to an in-memory stream so we # have to use a temporary file. tmpFileName = os.path.join(d, 'overlay.pdf') printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(tmpFileName) printer.setPageLayout(pageLayout) printOverlayPage(printer, trim, registrationMarks) with open(tmpFileName, 'rb') as f: overlayPDFBytes = f.read() reader = PyPDF2.PdfFileReader(io.BytesIO(overlayPDFBytes)) overlayPage = reader.getPage(0) fullRect = printer.pageLayout().fullRectPoints() margin = printer.pageLayout().marginsPoints() printableWidth = fullRect.width() - margin.left() - margin.right() printableHeight = fullRect.height() - margin.top() - margin.bottom() numPagesX = math.ceil(outSize.width() / printableWidth) numPagesY = math.ceil(outSize.height() / printableHeight) numPages = numPagesX * numPagesY outPDF = PyPDF2.PdfFileWriter() for y in range(numPagesY): for x in range(numPagesX): percentComplete = ((numPagesX * y + x) * 100) // (numPages + 1) if progress and not progress(percentComplete): return xt = x * printableWidth yt = y * printableHeight # PDF coordinates start at the bottom-left but everything # else is top-down so flip the Y transform yt = outSize.height() - yt - printableHeight xform = QTransform() xform.translate(margin.left(), margin.bottom()) xform.translate(-xt, -yt) xform.scale(outSize.width() / cropRect.width(), outSize.height() / cropRect.height()) xform.translate(-cropRect.x(), -cropRect.y()) assert xform.isAffine() ctm = (xform.m11(), xform.m12(), xform.m21(), xform.m22(), xform.m31(), xform.m32()) page = outPDF.addBlankPage(fullRect.width(), fullRect.height()) page.mergeTransformedPage(inReaderPage, ctm) if overlayPage: page.mergePage(overlayPage) if progress: progress((numPages * 100) // (numPages + 1)) with open(fileName, 'wb') as f: outPDF.write(f) if progress: progress(100)