Nullify page rotation. To correctly detect tables, page rotation must be zero. This function performs the necessary adjustments and returns information for reverting this changes.
(page)
| 2534 | |
| 2535 | |
| 2536 | def page_rotation_set0(page): |
| 2537 | """Nullify page rotation. |
| 2538 | |
| 2539 | To correctly detect tables, page rotation must be zero. |
| 2540 | This function performs the necessary adjustments and returns information |
| 2541 | for reverting this changes. |
| 2542 | """ |
| 2543 | mediabox = page.mediabox |
| 2544 | rot = page.rotation # contains normalized rotation value |
| 2545 | # need to derotate the page's content |
| 2546 | mb = page.mediabox # current mediabox |
| 2547 | |
| 2548 | if rot == 90: |
| 2549 | # before derotation, shift content horizontally |
| 2550 | mat0 = pymupdf.Matrix(1, 0, 0, 1, mb.y1 - mb.x1 - mb.x0 - mb.y0, 0) |
| 2551 | elif rot == 270: |
| 2552 | # before derotation, shift content vertically |
| 2553 | mat0 = pymupdf.Matrix(1, 0, 0, 1, 0, mb.x1 - mb.y1 - mb.y0 - mb.x0) |
| 2554 | else: |
| 2555 | mat0 = pymupdf.Matrix(1, 0, 0, 1, -2 * mb.x0, -2 * mb.y0) |
| 2556 | |
| 2557 | # prefix with derotation matrix |
| 2558 | mat = mat0 * page.derotation_matrix |
| 2559 | cmd = b"%g %g %g %g %g %g cm " % tuple(mat) |
| 2560 | xref = pymupdf.TOOLS._insert_contents(page, cmd, 0) |
| 2561 | |
| 2562 | # swap x- and y-coordinates |
| 2563 | if rot in (90, 270): |
| 2564 | x0, y0, x1, y1 = mb |
| 2565 | mb.x0 = y0 |
| 2566 | mb.y0 = x0 |
| 2567 | mb.x1 = y1 |
| 2568 | mb.y1 = x1 |
| 2569 | page.set_mediabox(mb) |
| 2570 | |
| 2571 | page.set_rotation(0) |
| 2572 | |
| 2573 | # refresh the page to apply these changes |
| 2574 | doc = page.parent |
| 2575 | pno = page.number |
| 2576 | page = doc[pno] |
| 2577 | return page, xref, rot, mediabox |
| 2578 | |
| 2579 | |
| 2580 | def page_rotation_reset(page, xref, rot, mediabox): |
no test coverage detected
searching dependent graphs…