-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add char['matrix'], ctm submodule, and CTM class
- Loading branch information
Showing
6 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import math | ||
from typing import NamedTuple | ||
|
||
# For more details, see the PDF Reference, 6th Ed., Section 4.2.2 ("Common | ||
# Transformations") | ||
|
||
|
||
class CTM(NamedTuple): | ||
a: float | ||
b: float | ||
c: float | ||
d: float | ||
e: float | ||
f: float | ||
|
||
@property | ||
def scale_x(self) -> float: | ||
return math.sqrt(pow(self.a, 2) + pow(self.b, 2)) | ||
|
||
@property | ||
def scale_y(self) -> float: | ||
return math.sqrt(pow(self.c, 2) + pow(self.d, 2)) | ||
|
||
@property | ||
def skew_x(self) -> float: | ||
return (math.atan2(self.d, self.c) * 180 / math.pi) - 90 | ||
|
||
@property | ||
def skew_y(self) -> float: | ||
return math.atan2(self.b, self.a) * 180 / math.pi | ||
|
||
@property | ||
def translation_x(self) -> float: | ||
return self.e | ||
|
||
@property | ||
def translation_y(self) -> float: | ||
return self.f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import unittest | ||
|
||
import pdfplumber | ||
from pdfplumber.ctm import CTM | ||
|
||
HERE = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def test_pdffill_demo(self): | ||
path = os.path.join(HERE, "pdfs/pdffill-demo.pdf") | ||
pdf = pdfplumber.open(path) | ||
left_r = pdf.pages[3].chars[97] | ||
right_r = pdf.pages[3].chars[105] | ||
|
||
left_ctm = CTM(*left_r["matrix"]) | ||
right_ctm = CTM(*right_r["matrix"]) | ||
|
||
assert round(left_ctm.translation_x) == 126 | ||
assert round(right_ctm.translation_x) == 372 | ||
|
||
assert round(left_ctm.translation_y) == 519 | ||
assert round(right_ctm.translation_y) == 562 | ||
|
||
assert left_ctm.skew_x == 45 | ||
assert right_ctm.skew_x == -45 | ||
|
||
assert left_ctm.skew_y == 45 | ||
assert right_ctm.skew_y == -45 | ||
|
||
assert round(left_ctm.scale_x, 3) == 1 | ||
assert round(right_ctm.scale_x, 3) == 1 | ||
|
||
assert round(left_ctm.scale_y, 3) == 1 | ||
assert round(right_ctm.scale_y, 3) == 1 |