|
| 1 | +# Copyright 2021 Ben Rush |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +class Point: |
| 19 | + def __init__(self, x: float, y: float): |
| 20 | + self.x = x |
| 21 | + self.y = y |
| 22 | + |
| 23 | + def toList(self) -> list: |
| 24 | + return [self.x, self.y] |
| 25 | + |
| 26 | +class Rect: |
| 27 | + """ |
| 28 | + From PDF spec: |
| 29 | + a specific array object used to describe locations on a page and |
| 30 | + bounding boxes for a variety of objects and written as an array |
| 31 | + of four numbers giving the coordinates of a pair of diagonally |
| 32 | + opposite corners, typically in the form [ll.x, ll.y, ur.x, ur.x] |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, ll: Point, ur: Point): |
| 36 | + self.ll = ll |
| 37 | + self.ur = ur |
| 38 | + |
| 39 | + def intersects(self, rectB: Rect) -> bool: |
| 40 | + # To check if either rectangle is actually a line |
| 41 | + # For example : l1 ={-1,0} r1={1,1} l2={0,-1} r2={0,1} |
| 42 | + |
| 43 | + if (self.ll.x == self.ur.x or self.ll.y == self.ur.y or rectB.ll.x == rectB.ur.x or rectB.ll.y == rectB.ur.y): |
| 44 | + # the line cannot have positive overlap |
| 45 | + return False |
| 46 | + |
| 47 | + |
| 48 | + # If one rectangle is on left side of other |
| 49 | + if(self.ll.x >= rectB.ur.y or rectB.ll.x >= self.ur.y): |
| 50 | + return False |
| 51 | + |
| 52 | + # If one rectangle is above other |
| 53 | + if(self.ur.y <= rectB.ll.y or rectB.ur.y <= self.ll.y): |
| 54 | + return False |
| 55 | + |
| 56 | + return True |
| 57 | + |
| 58 | + def union(self, rectB: Rect) -> Rect: |
| 59 | + ll = Point(min(self.ll.x, rectB.ll.x), |
| 60 | + min(self.ll.y, rectB.ll.y)) |
| 61 | + ur = Point(max(self.ur.x, rectB.ur.x), |
| 62 | + max(self.ur.y, rectB.ur.y)) |
| 63 | + return Rect(ll, ur) |
| 64 | + |
| 65 | + def toList(self) -> list: |
| 66 | + return [self.ll.x, self.ll.y, self.ur.x, self.ur.y] |
| 67 | + |
| 68 | +class QuadPoints: |
| 69 | + """ |
| 70 | + From PDF spec: |
| 71 | + An array of 8 x n numbers specifying the coordinates of n quadrilaterals |
| 72 | + in default user space. Each quadrilateral shall encompass a word or group |
| 73 | + of contiguous words in the text underlying the annotation. The coordinates |
| 74 | + for each quadrilateral shall be given in the order x1, y1, x2, y2, x3, y3, x4, y4 |
| 75 | + specifying the quadrilateral's four vertices in counterclockwise order |
| 76 | + starting with the lower left. The text shall be oriented with respect to the |
| 77 | + edge connecting points (x1, y1) with (x2, y2). |
| 78 | + """ |
| 79 | + |
| 80 | + points: list[Point] |
| 81 | + |
| 82 | + def __init__(self, points: list[Point]): |
| 83 | + self.points = points |
| 84 | + |
| 85 | + def append(self, quadpoints: QuadPoints) -> QuadPoints: |
| 86 | + return QuadPoints(self.points + quadpoints.points) |
| 87 | + |
| 88 | + def toList(self) -> list: |
| 89 | + return [c for p in points for c in p.toList()] |
| 90 | + |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def fromRect(rect: Rect): |
| 94 | + """ |
| 95 | + Assumes that the rect is aligned with the text. Will return incorrect |
| 96 | + results otherwise |
| 97 | + """ |
| 98 | + # Needs to be in this order to account for rotations applied later? |
| 99 | + # ll.x, ur.y, ur.x, ur.y, ll.x, ll.y, ur.x, ll.y |
| 100 | + quadpoints = [Point(rect.ll.x, rect.ur.y), |
| 101 | + Point(rect.ur.x, rect.ur.y), |
| 102 | + Point(rect.ll.x, rect.ll.y), |
| 103 | + Point(rect.ur.x, rect.ll.y)] |
| 104 | + return QuadPoints(quadpoints) |
| 105 | + |
| 106 | +class Annotation(): |
| 107 | + annotype: str |
| 108 | + rect: Rect |
| 109 | + quadpoints: QuadPoints |
| 110 | + |
| 111 | + def __init__(self, annotype: str, rect: Rect, quadpoints: list = None): |
| 112 | + self.annotype = annotype |
| 113 | + self.rect = rect |
| 114 | + if quadpoints: |
| 115 | + self.quadpoints = quadpoints |
| 116 | + else: |
| 117 | + self.quadpoints = QuadPoints.fromRect(rect) |
| 118 | + |
| 119 | + def united(self, annot: Annotation) -> Annotation: |
| 120 | + if self.annotype != annot.annotype: |
| 121 | + raise Exception("Cannot merge annotations with different types") |
| 122 | + |
| 123 | + return Annotation(self.annotype, |
| 124 | + self.rect.union(annot.rect), |
| 125 | + self.quadpoints.append(annot.quadpoints)) |
| 126 | + |
| 127 | + def intersects(self, annot: Annotation) -> bool: |
| 128 | + return self.rect.intersects(annot.rect) |
0 commit comments