added type1 fonts

This commit is contained in:
ari committed 2020-11-07 23:11:44 -05:00
1 parent a545692c20
commit 6bd957921b
4 files changed
+395 -4

No files matched your search

@@ -0,0 +1,14 @@
from typing import Any, Dict, List, NamedTuple, Union
from pdfmajor.fonts import PDFFont
class PDFResources(NamedTuple):
"""A class storing PDF Resources as described in PDF spec 1.7. section 7.8.3"""
extgstate: Dict[str, Any]
color_space: Dict[str, Union[str, List[str]]]
pattern: Dict[str, Any]
shading: Dict[str, Any]
xobject: Dict[str, Any]
font: Dict[str, PDFFont]
procset: Dict[str, Any]
+2 -1
View File
@@ -1,2 +1,3 @@
from .base import PDFFont
from .type1 import PDFType1
from .type1 import PDFFontType1
from . import standard14
+7 -2
View File
@@ -4,7 +4,7 @@ from .base import PDFFont
from . import standard14
class PDFType1(PDFFont):
class PDFFontType1(PDFFont):
"""A standard font in the PDF, as is defined by PDF spec 1.7 section 9.6.2"""
def __init__(
@@ -24,9 +24,14 @@ class PDFType1(PDFFont):
encoding=encoding,
to_unicode=to_unicode,
)
self.base_font = base_font
self.widths = widths
self.font_descriptor = font_descriptor
self.encoding = encoding
self.to_unicode = to_unicode
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "PDFType1":
def from_dict(cls, d: Dict[str, Any]) -> "PDFFontType1":
base_font = str(d["BaseFont"])
stnd_font = standard14.get_ifexists(base_font)
encoding = d.get("Encoding", None)
+372 -1
View File
@@ -1,5 +1,5 @@
from unittest import TestCase
from pdfmajor.fonts import standard14
from pdfmajor.fonts import standard14, PDFFontType1
class Standard14(TestCase):
@@ -25,3 +25,374 @@ class Standard14(TestCase):
def test_invalid_name(self):
self.assertIsNone(standard14.get_ifexists("Clowns!"))
class Parser(TestCase):
def test_type1(self):
raw = {
"BaseFont": "TimesNewRomanPS-ItalicMT",
"Encoding": "WinAnsiEncoding",
"FirstChar": 32,
"FontDescriptor": {
"Ascent": 891,
"CapHeight": 656,
"Descent": -216,
"Flags": 98,
},
"LastChar": 146,
"Subtype": "Type1",
"Widths": [
250,
0,
0,
0,
0,
0,
0,
0,
333,
333,
500,
0,
250,
333,
0,
0,
500,
500,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
611,
611,
667,
722,
611,
611,
722,
0,
333,
0,
0,
556,
833,
667,
722,
611,
0,
611,
500,
556,
722,
0,
833,
611,
556,
0,
0,
0,
0,
0,
0,
0,
500,
500,
444,
500,
444,
278,
500,
500,
278,
278,
444,
278,
722,
500,
500,
500,
500,
389,
389,
278,
500,
444,
667,
444,
444,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
333,
],
}
font = PDFFontType1.from_dict(raw)
self.assertEqual(font.subtype, "Type1")
self.assertEqual(font.base_font, "TimesNewRomanPS-ItalicMT")
self.assertEqual(font.encoding, "WinAnsiEncoding")
self.assertDictEqual(
font.font_descriptor,
{
"Ascent": 891,
"CapHeight": 656,
"Descent": -216,
"Flags": 98,
},
)
def test_type1_known(self):
raw = {"BaseFont": "Symbol", "Encoding": "WinAnsiEncoding"}
font = PDFFontType1.from_dict(raw)
self.assertEqual(font.subtype, "Type1")
self.assertEqual(font.base_font, "Symbol")
self.assertEqual(font.encoding, "WinAnsiEncoding")
self.assertDictEqual(
font.widths,
{
32: 250,
33: 333,
34: 713,
35: 500,
36: 549,
37: 833,
38: 778,
39: 439,
40: 333,
41: 333,
42: 500,
43: 549,
44: 250,
45: 549,
46: 250,
47: 278,
48: 500,
49: 500,
50: 500,
51: 500,
52: 500,
53: 500,
54: 500,
55: 500,
56: 500,
57: 500,
58: 278,
59: 278,
60: 549,
61: 549,
62: 549,
63: 444,
64: 549,
65: 722,
66: 667,
67: 722,
68: 612,
69: 611,
70: 763,
71: 603,
72: 722,
73: 333,
74: 631,
75: 722,
76: 686,
77: 889,
78: 722,
79: 722,
80: 768,
81: 741,
82: 556,
83: 592,
84: 611,
85: 690,
86: 439,
87: 768,
88: 645,
89: 795,
90: 611,
91: 333,
92: 863,
93: 333,
94: 658,
95: 500,
96: 500,
97: 631,
98: 549,
99: 549,
100: 494,
101: 439,
102: 521,
103: 411,
104: 603,
105: 329,
106: 603,
107: 549,
108: 549,
109: 576,
110: 521,
111: 549,
112: 549,
113: 521,
114: 549,
115: 603,
116: 439,
117: 576,
118: 713,
119: 686,
120: 493,
121: 686,
122: 494,
123: 480,
124: 200,
125: 480,
126: 549,
160: 750,
161: 620,
162: 247,
163: 549,
164: 167,
165: 713,
166: 500,
167: 753,
168: 753,
169: 753,
170: 753,
171: 1042,
172: 987,
173: 603,
174: 987,
175: 603,
176: 400,
177: 549,
178: 411,
179: 549,
180: 549,
181: 713,
182: 494,
183: 460,
184: 549,
185: 549,
186: 549,
187: 549,
188: 1000,
189: 603,
190: 1000,
191: 658,
192: 823,
193: 686,
194: 795,
195: 987,
196: 768,
197: 768,
198: 823,
199: 768,
200: 768,
201: 713,
202: 713,
203: 713,
204: 713,
205: 713,
206: 713,
207: 713,
208: 768,
209: 713,
210: 790,
211: 790,
212: 890,
213: 823,
214: 549,
215: 250,
216: 713,
217: 603,
218: 603,
219: 1042,
220: 987,
221: 603,
222: 987,
223: 603,
224: 494,
225: 329,
226: 790,
227: 790,
228: 786,
229: 713,
230: 384,
231: 384,
232: 384,
233: 384,
234: 384,
235: 384,
236: 494,
237: 494,
238: 494,
239: 494,
241: 329,
242: 274,
243: 686,
244: 686,
245: 686,
246: 384,
247: 384,
248: 384,
249: 384,
250: 384,
251: 384,
252: 494,
253: 494,
254: 494,
-1: 790,
},
)
self.assertDictEqual(
font.font_descriptor,
{
"StartFontMetrics": 4.1,
"Comment": "VMusage 30820 39997",
"FontName": "Symbol",
"FullName": "Symbol",
"FamilyName": "Symbol",
"Weight": "Medium",
"ItalicAngle": 0,
"IsFixedPitch": False,
"CharacterSet": "Special",
"FontBBox": [-180, -293, 1090, 1010],
"UnderlinePosition": -100,
"UnderlineThickness": 50,
"Version": 1.008,
"Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1997 Adobe Systems Incorporated. All rights reserved.",
"EncodingScheme": "FontSpecific",
"StdHW": 92,
"StdVW": 85,
},
)