177 Commits
Author SHA1 Message Date
ari c383d992a1 docs 2020-11-15 17:22:19 -05:00
ari 18cb345e0d added some more fonts 2020-11-15 17:22:14 -05:00
ari 9dfde10c39 docs 2020-11-15 16:32:41 -05:00
ari 96ba7f25cd docs 2020-11-15 16:32:27 -05:00
ari 6bd957921b added type1 fonts 2020-11-07 23:11:44 -05:00
ari a545692c20 added strip for zlib 2020-11-07 23:05:50 -05:00
ari 00d8fe72d8 added numpy export 2020-11-06 14:02:33 -05:00
ari 69d562ce19 updated gitignore 2020-11-06 14:02:20 -05:00
ari 188c97c469 moving things around 2020-11-06 14:02:10 -05:00
ari e823f939cf initial font format 2020-11-06 14:01:33 -05:00
ari 3a7cffb10b added standard fornt loader 2020-11-06 13:46:20 -05:00
ari 11e466cd5d - afm parser
- added core14 files
2020-11-06 13:37:07 -05:00
ari 0ba835d577 hardcore linting 2020-11-03 20:19:11 -05:00
ari 14df0a1600 renamed parser to pdf-parser 2020-11-03 16:23:54 -05:00
ari e47475b95f light testing for filters 2020-11-03 15:47:50 -05:00
ari 6e43a92517 added more files to gitignore 2020-11-03 13:59:56 -05:00
ari 8813532b83 added metadata parser + tests 2020-11-02 20:54:11 -05:00
ari 14cb2e31d5 using absolute objects import 2020-11-02 19:57:26 -05:00
ari 3bb668a705 changing imports naming 2020-11-02 16:15:11 -05:00
ari 3e81a6c910 added noq 2020-11-02 15:55:54 -05:00
ari 50d52cda47 renamed files 2020-11-02 15:54:38 -05:00
ari 54179b7dd1 added test for warnings 2020-11-02 13:36:55 -05:00
ari f6b3252088 counting number of pages and added a warning 2020-11-02 13:23:27 -05:00
ari 76d64a2f20 added page counts 2020-11-02 13:21:41 -05:00
ari b48d635ec7 parsing pdf page leafs 2020-11-02 13:11:31 -05:00
ari ffb7b843ea cleaned up the form for the parsing context 2020-11-01 14:14:31 -05:00
ari da6e7a7a28 pages nodes 2020-11-01 14:02:20 -05:00
ari e33d13a3e1 rect obj 2020-10-30 19:52:57 -04:00
ari 3f5bac98b3 stable version 2020-10-30 19:13:25 -04:00
ari df50637e70 ran flake 2020-10-30 17:15:31 -04:00
ari 1829277245 safer files 2020-10-30 16:43:48 -04:00
ari 44ad73a44c safe file loading 2020-10-30 16:43:11 -04:00
ari e8b31ebee0 more parsed cases for xref 2020-10-30 14:32:46 -04:00
ari d40da248de fixed various type issues 2020-10-30 14:23:04 -04:00
ari 838ed91c5f unhiding vscode 2020-10-30 14:14:39 -04:00
ari 3b82f4c309 xref initial trial 2020-10-30 14:14:21 -04:00
ari 68dd42fea1 better parsing of indirects 2020-10-30 14:12:34 -04:00
ari 343a0ae4de ran isort 2020-10-30 14:11:43 -04:00
ari b40d2bf6e5 - fixed tests
- using bytes for strings
2020-10-29 18:02:12 -04:00
ari ee011a5da0 added support for invalid endstream 2020-10-29 17:55:21 -04:00
ari 4e3b3e10b7 added parsing for object types 2020-10-28 10:07:30 -04:00
ari 78175d3a64 split out xref from parser 2020-10-27 20:12:07 -04:00
ari 5af06a3f1e fixed up tests 2020-10-27 12:30:43 -04:00
ari 4653b53a6c fixed stream parsing 2020-10-27 12:26:56 -04:00
ari 3d39bd575f converted parser to functional
fixed a bufsize issue on the lexer
2020-10-27 09:46:51 -04:00
ari 087c1c2fe9 using a more functional approach for lexer 2020-10-26 17:55:55 -04:00
ari e2a92517cd working xref db 2020-10-23 22:17:50 -04:00
ari cfc2dfca47 cleaned up parser class 2020-10-21 17:52:46 -04:00
ari 754e248aac removed oldpdfmajor and ran linters 2020-10-21 17:48:14 -04:00
ari b1edcbe51d added reverse line reader 2020-10-21 17:02:53 -04:00
ari 37e567a352 added stream parsing 2020-10-21 16:32:18 -04:00
ari 39501b174b correct test port 2020-10-20 20:20:33 -04:00
ari cb7219a152 better error naming 2020-10-20 19:42:37 -04:00
ari f587a4389f - reorginized repo 2020-10-20 19:37:14 -04:00
ari 4d1e0f2389 - removed old tests
- ported all custom filters and using python std implemetation for ascii and bin filters
2020-10-20 19:24:15 -04:00
ari 5fd03d1250 added more metadata for streams 2020-10-19 20:15:13 -04:00
ari 77b3754ff4 initial support for streams 2020-10-19 20:11:21 -04:00
ari 07244aaf11 new attempt for parser
- now support clear indirect objects
2020-10-19 15:00:31 -04:00
ari b802f782fa more docs 2020-10-18 13:19:17 -04:00
ari 6e7c1f3888 ensuring comments do not break parsing 2020-10-18 13:17:07 -04:00
ari cc0f802a10 l1 emits TokenKeyword
removed need to Object Keyword which made no sense to have
2020-10-18 13:01:09 -04:00
ari 43ed8b4019 working prototype for l2 2020-10-18 11:48:19 -04:00
ari c7d618284b got rid of the parse context in favour of a contextual trait 2020-10-17 23:53:43 -04:00
ari 3fec7a1747 undoing lexing of obj and stream 2020-10-17 23:45:31 -04:00
ari 3a5f8f44a3 added indirect Objects and stream tokens 2020-10-17 22:52:33 -04:00
ari adf04aa50c docs 2020-10-17 22:24:58 -04:00
ari 0f147bb66a added another complex testcase for parsing 2020-10-17 22:24:14 -04:00
ari 4f8bcf1fff - using a common generic type for PDFObject as oppose to Union (needed a common data contract)
- parsing arrays
2020-10-17 22:11:51 -04:00
ari 6d9a57c722 clean up 2020-10-17 21:50:47 -04:00
ari 039f06c396 PDFName is just a fancy str 2020-10-17 21:50:32 -04:00
ari 71a37477ae added name wrapper 2020-10-17 21:48:34 -04:00
ari 01ace1c57d succesful dictionary parsing 2020-10-17 21:41:33 -04:00
ari 7ee8254edb more docs 2020-10-17 20:17:47 -04:00
ari 4e1f45e205 added Null token 2020-10-17 20:14:03 -04:00
ari da936e6a48 renamed decimal to real 2020-10-17 18:13:10 -04:00
ari 472942867b complete lexer 2020-10-17 17:31:11 -04:00
ari 24b9c99ea1 adjusted tests for streambuffer 2020-10-17 15:56:26 -04:00
ari fa9e936f4a renamed safebufiterator to streambuffer 2020-10-17 15:55:23 -04:00
ari a03857d0f7 - clean up
- renamed safebuf to streambuf
- added arrays
2020-10-17 15:54:39 -04:00
ari c752346638 moved num parsing 2020-10-16 17:07:40 -04:00
ari 0eb5e4b8fc implement dd bracket 2020-10-16 16:55:54 -04:00
ari 370a1b40a1 moved string parsing to new method 2020-10-16 16:31:02 -04:00
ari faaf7925bf using new approach for lexing 2020-10-16 16:10:10 -04:00
ari a2d2b7808d added literal parsing to main tokenizer 2020-10-15 09:37:30 -04:00
ari 1acd59c008 correct literal parsing 2020-10-15 09:37:06 -04:00
ari 052a3a071e added docs to tokens with references to spec 2020-10-14 22:22:21 -04:00
ari 81c664ae86 docs 2020-10-14 22:03:36 -04:00
ari 28110c9a0e number tests pass 2020-10-14 21:56:33 -04:00
ari af927e86ec string tokenizer works 2020-10-14 21:53:27 -04:00
ari 4a6b0608a0 strict hex format 2020-10-14 21:29:46 -04:00
ari 5f19f71c4d fixed up literal parsing in global tokenizer 2020-10-14 11:03:01 -04:00
ari 96d68cdf68 added string tokenizer 2020-10-14 08:32:34 -04:00
ari d8be2885b8 generalized form buf iteration 2020-10-13 21:35:17 -04:00
ari be2778bffc fixed bandit issus 2020-10-13 21:21:09 -04:00
ari 9e6422e0eb added linting command 2020-10-13 21:17:12 -04:00
ari 0e0a28154d fixed minor linting issues 2020-10-13 21:14:55 -04:00
ari e1e644cc38 ran linter 2020-10-13 21:05:25 -04:00
ari c430c57fe0 split digit tokenizer 2020-10-13 20:54:59 -04:00
ari ab657deda4 working literal parser
first mock of main tokenizer
2020-10-13 20:52:23 -04:00
ari a0cc862804 changed layout + literal tokens 2020-10-13 16:41:41 -04:00
ari da066edaf5 clean up 2020-10-13 10:13:40 -04:00
ari 460d213700 initial tokenizer 2020-10-13 10:13:20 -04:00
ari fe9fd304ab added linters
ran linters
2020-10-12 18:44:46 -04:00
ari 5b9bbcacbf modernize stack with makefile 2020-10-12 18:42:00 -04:00
ari 7e24c64b5b Centralized all exceptions 2019-12-19 22:04:01 -05:00
ari cdcd2eabe2 ensured tests are passing 2019-12-19 21:58:44 -05:00
ari fdd2fbd85a removed old exception files 2019-12-19 20:18:49 -05:00
ari 087012dd81 all errors caught in one location 2019-12-19 20:08:06 -05:00
ari 630b3b2418 removed unused cmd_record 2019-12-12 21:45:22 -05:00
ari 441abc3f55 version bump 2019-12-11 22:18:51 -05:00
ari 152d3aaf55 v1.3.12 2019-12-11 22:16:28 -05:00
ari 36a9371657 generated docs 2019-12-08 14:45:28 -05:00
ari a29d465ea4 added support for ignore bad char flag
- updated unit tests to include
- using the textstate.to_unichr abstraction
2019-12-08 14:44:46 -05:00
ari 841576d9b8 exposed exceptions to top module
updated dependencies
2019-12-07 23:15:55 -05:00
ari bfc0b6ba34 added venv 2019-12-07 23:15:12 -05:00
ari 336f5f1fba propograted exceptions 2019-12-07 22:58:13 -05:00
ari 1b1db13b5b updated deps
- upgraded to Pillow 6.2.0 (passes tests)
- removed unnecessary nose dep
2019-12-07 22:57:57 -05:00
ari 3f561f3488 - removed none-develop dependency on tqdm
- ensured all test pdfs are properly pushed
2019-08-18 18:00:50 -04:00
ari 989de3122e corrected gitignore to not ignore lorem-v5.pdf 2019-08-18 17:59:14 -04:00
ari 20be0aa49e removed dependency on tqdm 2019-08-18 17:55:08 -04:00
ari d37b56a685 Merge branch 'master' of github.com:asosnovsky/pdfmajor 2019-06-10 16:28:23 -04:00
ari 9e923de7ec - added better mypy support
- updated most unit-test to use the unitttest module
- added a single unit-test run file
2019-06-10 16:26:52 -04:00
Ari Sosnovsky 2c9593ac21 Update README.md 2019-04-01 17:37:13 -04:00
Ari Sosnovsky 057cc0cf28 Update README.md 2019-04-01 17:36:36 -04:00
Ari Sosnovsky 82b47f1b19 Update README.md 2019-04-01 17:35:24 -04:00
ari 290de97cb5 - fixed invisible color bug 2019-01-26 23:08:57 -05:00
ari b32aef7fda - fixed the curve ignoring of the color error 2019-01-24 21:38:47 -05:00
ari 14ba20be55 - better repr for curves and colors 2019-01-23 22:12:14 -05:00
ari 18938d91ea - better names for commands 2019-01-23 22:11:58 -05:00
ari eb38854076 updated gitinore 2019-01-23 22:11:46 -05:00
ari 671ae5bc4a running tests before every pip deploy 2019-01-23 22:10:51 -05:00
ari 844408c6e3 - ensured that all tests are working
- added a main test script
2019-01-23 22:06:12 -05:00
ari bab104885f - fixed caching error when running the interpreter multiple times on items with xobjects 2019-01-23 15:56:46 -05:00
ari 98f18c3e32 - corrected caching error
- added lorem ipsum pdf test docs
- introduced a new test for detecting if some unwanted caching is occuring
- muting "Unsupported" warning
2019-01-23 15:56:25 -05:00
ari ac2c462561 - improved html converter 2019-01-22 13:16:46 -05:00
ari 19712f1f0f cleaner html converter 2019-01-22 13:16:17 -05:00
ari ec02a7d38b - yaml converter
- adjusting curve detection
2019-01-16 16:56:14 -05:00
ari 0345ca01ba - slightly more accurate shape detection 2019-01-16 16:55:30 -05:00
ari b36f6bc907 silencing the unknown operator warning 2019-01-16 12:08:15 -05:00
ari f9f9a59793 - added yaml converter 2019-01-16 12:08:03 -05:00
ari b7026793d7 - minor bug fix -- xobj invalid name 2019-01-13 13:13:37 -05:00
ari addd59ed0f updated readme 2019-01-11 15:25:03 -05:00
ari d43b617309 - new doc build 2019-01-11 15:23:15 -05:00
ari 17b25c7d9b - correcting the docs 2019-01-11 15:21:57 -05:00
ari b7fab64801 - correcting the doc 2019-01-11 15:21:27 -05:00
ari 609bcda76a - returned image writer
- added .save_image method to LTImage
2019-01-04 15:43:30 -05:00
ari ef230847ac - image extraction 2019-01-04 15:43:07 -05:00
ari d1d1b23f2f updated docs 2018-12-24 01:52:52 -05:00
ari abfbe98897 - correct usage of ET BT tags
- introduced LTTextBlocks
2018-12-24 01:49:51 -05:00
ari e7701ce2ac - correct usage of the BT ET tags 2018-12-24 01:49:32 -05:00
ari c239f79b2c added settings to setup.py 2018-12-22 23:37:12 -05:00
ari 711b67a9d2 - rewrite of interpreter to use more functional-friendly methods
- introducing of concurent processing
- new text/json/xml/html conversions (where all work)
- added several tests for writer modules, conversion and interpreter
2018-12-22 23:32:48 -05:00
ari b8b167e827 - updated docs
- added text conversion
2018-12-22 23:32:11 -05:00
ari 0590d8c8f9 - moved sample folder to tests
- updated tests dep
2018-12-22 22:49:02 -05:00
ari bf46dee422 - renamed convert_sample to test 2018-12-22 22:45:20 -05:00
ari bc20627d9f - custom formatters done
- json formatter works now
- added some tests for writers
2018-12-22 22:44:53 -05:00
ari 3ba0b95001 - moved parser files from old interpeter into parser folder
- renamed interpreter_v2 to just interpreter
- removed legacy code
2018-12-20 10:41:10 -05:00
ari 487f9b6c08 - working intrepreter v2 2018-12-20 10:33:14 -05:00
ari 1d11cd1c7f new interperter 2018-12-19 11:24:02 -05:00
ari 56dd4c7ef4 - added a functional library access 2018-12-09 16:46:43 -05:00
ari 924596e38d - added consistency to PDFGraphicStateCol 2018-11-29 15:39:16 -05:00
ari 2ea0b717f9 - added better logging options
- added test case for bad chars
2018-11-23 14:44:09 -05:00
ari 38f8ca76a6 - better loggin system
- adde test case for bad characters
2018-11-23 14:43:51 -05:00
ari ac49ad1bcf - sanitizing the xml chars 2018-11-21 09:19:33 -05:00
ari d74bf1eaa9 - added support for ICCSBAsed colors 2018-11-09 10:24:04 -05:00
ari cf3c89bf25 supporting ICCSBased colors 2018-11-09 10:22:44 -05:00
ari b491cd2181 - v1.1.8 2018-11-08 14:53:28 -05:00
ari 70144c8fe1 - updated docx
- sanitizing xml attributes
2018-11-04 16:28:29 -05:00
ari 0d832bf7fa version inc 2018-10-31 14:45:37 -04:00
ari 7319b7f584 xml chars output bbx instead of width/height 2018-10-31 14:45:02 -04:00
ari 5b641d6243 comment out leading for now 2018-10-29 16:35:32 -04:00
ari 10d679d379 clean up 2018-10-29 16:26:57 -04:00
ari 5378a4f34c - better color grabbing for scn
- show custom type in converters
2018-10-29 16:25:50 -04:00
ari 42cbc8e39e - using snake case
- not outputting is_bold
2018-10-29 13:18:12 -04:00
ari 66004988f4 actually cleaning it all prior to deployment 2018-10-29 13:04:32 -04:00
ari 7722b99f70 Deployong new version - v1.1.4 2018-10-29 13:03:28 -04:00
ari 66b5fea65c - compatible xml-converter with char-blocks
- consitent attr naming convention for fonts
2018-10-29 13:03:11 -04:00
214 changed files with 31921 additions and 12487 deletions

No files matched your search

+1 -1
View File
@@ -6,4 +6,4 @@ indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
insert_final_newline = true
+11 -4
View File
@@ -8,7 +8,14 @@ docs/_build
tests/*.xml
tests/*.txt
.idea/
.vscode
samples/output
samples/pdfmajor
**__pycache**
tests/samples/output
./samples
**__pycache**\
.mypy_cache
.ipynb_checkpoints
venv
oldpdfmajor
.tmp*
/_*py
*.profile
*.calltree
-3
View File
@@ -2,8 +2,5 @@ language: python
python:
- "3.6.4"
- "3.7"
install:
- pip install pycryptodome
- pip install chardet
script:
nosetests --nologcapture
+1 -1
View File
@@ -1 +1 @@
v1.1.3
v1.3.13
+15
View File
@@ -0,0 +1,15 @@
{
"python.pythonPath": "venv/bin/python",
"python.formatting.provider": "black",
"editor.tabSize": 2,
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
+33
View File
@@ -0,0 +1,33 @@
install-development:
python3 -m venv venv
./venv/bin/pip install -e .[dev]
clean:
rm -rf venv
rm -rf build
rm -rf dist
rm -rf *.egg-info
lint:
autoflake --remove-all-unused-imports --ignore-init-module-imports --remove-duplicate-keys --remove-unused-variables -r -i pdfmajor
autoflake --remove-all-unused-imports --ignore-init-module-imports --remove-duplicate-keys --remove-unused-variables -r -i tests
isort pdfmajor
black pdfmajor
isort tests
black tests
mypy pdfmajor
mypy tests
bandit -r pdfmajor
test:
./venv/bin/python -m unittest discover tests
build-docs:
mkdocs build
deploy:
python setup.py sdist bdist_wheel
git tag $(cat .version)
git push -u origin $(cat .version)
twine upload dist/*
+47 -9
View File
@@ -1,28 +1,66 @@
# PDFMajor
PDF-Major is a fork of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.
# TODO:
- [ ] Re-write docs
- [ ] Encryption
- [ ] Support all stream filters
- [ ] Font support
- [ ] Add Type 3/0/CID support
- [ ] Color spacing
# PDFMajor
<table>
<tr>
<td>Latest Release</td>
<td>
<a href="https://pypi.org/project/pdfmajor/">
<img src="https://img.shields.io/pypi/v/pdfmajor.svg" alt="latest release" />
</a>
</td>
</tr>
</table>
PDF-Major is a **complete re-write** of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.
## Documentation
See documentation [here](https://asosnovsky.github.io/pdfmajor/).
See [full API documentation here](https://asosnovsky.github.io/pdfmajor/api/).
## Why Another Fork?
## Basic Example
```py
from pdfmajor.interpreter import PDFInterpreter
`PDFMiner` was designed to run in python 2.7>, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with.
for page in PDFInterpreter("/path/to/pdf.pdf"):
print("page start", page.page_num)
for item in page:
print(" >", item)
print("page end", page.page_num)
```
## Why a re-write?
`PDFMiner` was designed to run in python 2.7, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon `PDFMiner.six` I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.
## Features
* Written entirely in Python.
* Functional extraction method based on generators
* Parse, analyze, and convert PDF documents.
* PDF-1.7 specification support. (well, almost)
* Font-Color extraction
* Shape fill and stroke color extraction
* CJK languages and vertical writing scripts support.
* Various font types (Type1, TrueType, Type3, and CID) support.
* Basic encryption (RC4) support.
* Font-Color extraction
* Shape fill and stroke color extraction
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the `pdfmajor.layouts.PDFLayoutAnalyzer` class, but at the current time is not supported.
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the `pdfmajor.interpreter.PageInterpreter` class, but at the current time is not supported.
## How to Install
-7
View File
@@ -1,7 +0,0 @@
#!/bin/bash
# Get working dir (this will make sure that the script will work the same anywhere you run it)
WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR
cd ..
mkdocs build
-9
View File
@@ -1,9 +0,0 @@
#!/bin/bash
# Get working dir (this will make sure that the script will work the same anywhere you run it)
WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR
cd ..
rm build -rf
rm dist -rf
rm pdfmajor.egg-info -rf
-18
View File
@@ -1,18 +0,0 @@
#!/bin/bash
# Get working dir (this will make sure that the script will work the same anywhere you run it)
WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR
cd ..
sh bin/clean.sh
python setup.py sdist bdist_wheel
git add .
git commit -m "Deployong new version - $(cat .version)"
git push
git tag $(cat .version)
git push -u origin $(cat .version)
twine upload dist/*
+6 -96
View File
@@ -2,102 +2,12 @@
The library is constructed from 4 base modules:
* **interpreter**: contains low-level classes for extracting fundamental data-structures from the documents
* **converters**: contains high-level classes for conversion of the fundamental pdf structures to other formats
* **imagewriter**: contains a simple implementation for converting PDF Image Streams to png/bmp/img formats
* **layouts**: contains an abstraction layer for the data-structures found in the pdf
* **lexer**: contains low-level classes for tokenization
* **pdf_parser**: a parser based on the tokens that attempts to map pdf values to python values
* **xref**: a database for caching indirect objects in memory
* **filters**: a decryption module for decoding pdf-streams
* **fonts**: a font's database and evaluation module
* **document**: top-level abstraction to be used by end users
## interpreter
WIP
## converters
Contains high-level classes for conversion of the fundamental pdf structures to other formats. This library includes 4 high-level conversion classes:
- HTMLConverter
- JSONConverter
- XMLConverter
- TextConverter
These classes all extend the [PDFConverter](#converters.PDFConverter). To use them simply call the static method [parse_file](#converterspdfconverterparse_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos).
### Example
```py
from pdfmajor.converters import HTMLConverter
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
HTMLConverter.parse_file(
input_file,
output_file
)
input_file.close()
output_file.close()
```
### converters.convert_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos, out_type)
A high-level abstraction for the conversion classes.
- `input_file`: [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)
- `output_file`: [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)
- `image_folder_path`: [str](#) defaults to None
- `codec`: [str](#) defaults to 'utf-8'
- `maxpages`: [int](#) defaults to 0
- `password`: [str](#) defaults to None
- `caching`: [bool](#) defaults to True
- `check_extractable`: [bool](#) defaults to True
- `pagenos`: [List[int]](#) defaults to None
- `out_type`: [str](#) defaults to 'html'
### Example (converters.convert_file)
```py
from pdfmajor.converters import convert_file
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
convert_file(
input_file,
output_file
)
input_file.close()
output_file.close()
```
### converters.PDFConverter
Extends `layouts.PDFLayoutAnalyzer`.
#### converters.PDFConverter.\_\_init\_\_(rsrcmgr, outfp, imagewriter, codec, pageno)
- `rsrcmgr`: [PDFResourceManager](#interpreter.PDFResourceManager)
- `outfp`: [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)
- `imagewriter`: [ImageWriter](#imagewriter.ImageWriter)
- `codec`: [str](#) defaults to 'utf-8'
- `pageno`: [int](#) defaults to 1
initialization function for the class
#### converters.PDFConverter.parse_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos)
returns the output file [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper).
- `input_file`: [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)
- `output_file`: [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)
- `image_folder_path`: [str](#) defaults to None
- `codec`: [str](#) defaults to 'utf-8'
- `maxpages`: [int](#) defaults to 0
- `password`: [str](#) defaults to None
- `caching`: [bool](#) defaults to True
- `check_extractable`: [bool](#) defaults to True
- `pagenos`: [List[int]](#) defaults to None
## imagewriter
WIP
## layouts
WIP
+30 -8
View File
@@ -1,28 +1,50 @@
# PDFMajor
# PDFMajor
PDF-Major is a fork of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.
<table>
<tr>
<td>Latest Release</td>
<td>
<a href="https://pypi.org/project/pdfmajor/">
<img src="https://img.shields.io/pypi/v/pdfmajor.svg" alt="latest release" />
</a>
</td>
</tr>
</table>
PDF-Major is a fork of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.
## Documentation
See documentation [here](https://asosnovsky.github.io/pdfmajor/).
## Basic Example
```py
from pdfmajor.interpreter import PDFInterpreter
for page in PDFInterpreter("/path/to/pdf.pdf"):
print("page start", page.page_num)
for item in page:
print(" >", item)
print("page end", page.page_num)
```
## Why Another Fork?
`PDFMiner` was designed to run in python 2.7>, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with.
`PDFMiner` was designed to run in python 2.7>, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon `PDFMiner.six` I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.
## Features
* Written entirely in Python.
* Functional extraction method based on generators
* Parse, analyze, and convert PDF documents.
* PDF-1.7 specification support. (well, almost)
* Font-Color extraction
* Shape fill and stroke color extraction
* CJK languages and vertical writing scripts support.
* Various font types (Type1, TrueType, Type3, and CID) support.
* Basic encryption (RC4) support.
* Font-Color extraction
* Shape fill and stroke color extraction
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the `pdfmajor.layouts.PDFLayoutAnalyzer` class, but at the current time is not supported.
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the `pdfmajor.interpreter.PageInterpreter` class, but at the current time is not supported.
## How to Install
+72 -65
View File
@@ -62,14 +62,14 @@
<ul>
<li><a class="toctree-l3" href="#parser">parser</a></li>
<li><a class="toctree-l3" href="#interpreter">interpreter</a></li>
<li><a class="toctree-l3" href="#converters">converters</a></li>
<li><a class="toctree-l3" href="#imagewriter">imagewriter</a></li>
<li><a class="toctree-l3" href="#layouts">layouts</a></li>
</ul>
@@ -111,37 +111,83 @@
<h1 id="api">API</h1>
<p>The library is constructed from 4 base modules:</p>
<ul>
<li><strong>interpreter</strong>: contains low-level classes for extracting fundamental data-structures from the documents</li>
<li><strong>converters</strong>: contains high-level classes for conversion of the fundamental pdf structures to other formats</li>
<li><strong>parser</strong>: contains low-level classes for extracting fundamental data-structures from the documents</li>
<li><strong>interpreter</strong>: a interpreter of the pdf-standard commands</li>
<li><strong>converters</strong>: contains high-level functions for conversion of the fundamental pdf structures to other formats</li>
<li><strong>imagewriter</strong>: contains a simple implementation for converting PDF Image Streams to png/bmp/img formats</li>
<li><strong>layouts</strong>: contains an abstraction layer for the data-structures found in the pdf</li>
</ul>
<h2 id="parser">parser</h2>
<p>WIP</p>
<h2 id="interpreter">interpreter</h2>
<pre><code>WIP
</code></pre>
<h2 id="converters">converters</h2>
<p>Contains high-level classes for conversion of the fundamental pdf structures to other formats. This library includes 4 high-level conversion classes:</p>
<ul>
<li>HTMLConverter</li>
<li>JSONConverter</li>
<li>XMLConverter</li>
<li>TextConverter</li>
</ul>
<p>These classes all extend the <a href="#converters.PDFConverter">PDFConverter</a>. To use them simply call the static method <a href="#converterspdfconverterparse_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos">parse_file</a>.</p>
<p>a interpreter of the pdf-standard commands</p>
<h3 id="example">Example</h3>
<pre><code class="py">from pdfmajor.converters import HTMLConverter
<pre><code class="py">from pdfmajor.interpreter import PDFInterpreter
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
HTMLConverter.parse_file(
input_file,
output_file
)
input_file.close()
output_file.close()
for page in PDFInterpreter(&quot;/path/to/pdf.pdf&quot;):
print(&quot;page start&quot;, page.page_num)
for item in page:
print(&quot; &gt;&quot;, item)
print(&quot;page end&quot;, page.page_num)
</code></pre>
<h3 id="convertersconvert_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos-out_type">converters.convert_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos, out_type)</h3>
<h3 id="interpreterpdfinterpreter">interpreter.PDFInterpreter</h3>
<p>This generator-function yields individual pages which contain their respected items.</p>
<h4 id="arguments">Arguments</h4>
<ul>
<li><code>input_file_path</code>: <a href="#">str</a></li>
<li><code>preload</code>: <a href="#">bool</a> defaults to False</li>
<li><code>maxpages</code>: <a href="#">int</a> defaults to 0 </li>
<li><code>password</code>: <a href="#">str</a> defaults to None </li>
<li><code>caching</code>: <a href="#">bool</a> defaults to True </li>
<li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li>
<li><code>ignore_bad_chars</code>: <a href="#">bool</a> defaults to False</li>
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
<li><code>debug_level</code>: <a href="#https://docs.python.org/3/library/logging.html#levels">logging.levels</a> defaults logging.WARNING</li>
</ul>
<h4 id="yield-value">Yield Value</h4>
<p>This function returns a generator that yields <a href="#interpreterpageinterpreter">PDFInterpreter</a>.</p>
<h3 id="interpreterpageinterpreter">interpreter.PageInterpreter</h3>
<p>This generator-function-class yields individual <a href="#layout-items">layout items</a>.</p>
<h3 id="layout-items">Layout Items</h3>
<p>All layout items extend the <code>LTItem</code> class. There are two kinds of layout items:</p>
<ul>
<li>LTComponent: extends the base <code>LTItem</code> class, this class will have additional values such as boundary boxes, height and width</li>
<li>LTContainer: extends the <code>LTComponent</code> class, this class is used to contain elements of the pdf that would have child elements. Iterating on this element will output its child elements.</li>
</ul>
<h4 id="layout-containers">Layout Containers</h4>
<p>All of these classes extend the LTContainer class.</p>
<ul>
<li>LTXObject: a layout item containing other additional layout items</li>
<li>LTCharBlock: a layout item containing LTChars, this corresponds to whenever a <code>TJ</code> or <code>Tj</code> operators is issued within a text object.</li>
<li>LTTextBlock: a layout item containing LTCharBlocks, note that this directly corresponds to the <code>BT</code> and <code>ET</code> operators pair in the pdf standard</li>
</ul>
<h4 id="layout-components">Layout Components</h4>
<p>All of these classes extend the LTComponent class.</p>
<ul>
<li>LTChar: an individual character</li>
<li>LTCurves: represents a collection of svg-paths (available under <code>self.paths</code>)</li>
<li>LTImage: a component containing information regarding an image</li>
</ul>
<h2 id="converters">converters</h2>
<p>Contains high-level functions for conversion of the fundamental pdf structures to other formats. This library includes 4 high-level conversion cases:</p>
<ul>
<li>HTML</li>
<li>JSON</li>
<li>XML</li>
<li>Text</li>
</ul>
<p>These formats are all generated using the <a href="#PDFInterpreter">PageInterpreter</a>. To use them simply call the static method <a href="#converterspdfconverter">parse_file</a>.</p>
<h3 id="example_1">Example</h3>
<pre><code class="py">from pdfmajor.converters import convert_file
convert_file(
&quot;path/to/input/file.pdf&quot;,
&quot;path/to/output/file.html&quot;,
out_type=&quot;html&quot;
)
</code></pre>
<h3 id="convertersconvert_file">converters.convert_file</h3>
<p>A high-level abstraction for the conversion classes.</p>
<ul>
<li><code>input_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
@@ -155,46 +201,7 @@ output_file.close()
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
<li><code>out_type</code>: <a href="#">str</a> defaults to 'html'</li>
</ul>
<h3 id="example-convertersconvert_file">Example (converters.convert_file)</h3>
<pre><code class="py">from pdfmajor.converters import convert_file
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
convert_file(
input_file,
output_file
)
input_file.close()
output_file.close()
</code></pre>
<h3 id="converterspdfconverter">converters.PDFConverter</h3>
<p>Extends <code>layouts.PDFLayoutAnalyzer</code>.</p>
<h4 id="converterspdfconverter9595init9595rsrcmgr-outfp-imagewriter-codec-pageno">converters.PDFConverter.__init__(rsrcmgr, outfp, imagewriter, codec, pageno)</h4>
<ul>
<li><code>rsrcmgr</code>: <a href="#interpreter.PDFResourceManager">PDFResourceManager</a></li>
<li><code>outfp</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a></li>
<li><code>imagewriter</code>: <a href="#imagewriter.ImageWriter">ImageWriter</a></li>
<li><code>codec</code>: <a href="#">str</a> defaults to 'utf-8'</li>
<li><code>pageno</code>: <a href="#">int</a> defaults to 1</li>
</ul>
<p>initialization function for the class</p>
<h4 id="converterspdfconverterparse_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos">converters.PDFConverter.parse_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos)</h4>
<p>returns the output file <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a>.</p>
<ul>
<li><code>input_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
<li><code>output_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
<li><code>image_folder_path</code>: <a href="#">str</a> defaults to None</li>
<li><code>codec</code>: <a href="#">str</a> defaults to 'utf-8'</li>
<li><code>maxpages</code>: <a href="#">int</a> defaults to 0 </li>
<li><code>password</code>: <a href="#">str</a> defaults to None </li>
<li><code>caching</code>: <a href="#">bool</a> defaults to True </li>
<li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li>
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
</ul>
<h2 id="imagewriter">imagewriter</h2>
<p>WIP </p>
<h2 id="layouts">layouts</h2>
<p>WIP </p>
</div>
+33 -9
View File
@@ -53,12 +53,14 @@
<a class="current" href=".">Home</a>
<ul class="subnav">
<li class="toctree-l2"><a href="#pdf-major">PDF-Major</a></li>
<li class="toctree-l2"><a href="#pdfmajor">PDFMajor</a></li>
<ul>
<li><a class="toctree-l3" href="#documentation">Documentation</a></li>
<li><a class="toctree-l3" href="#basic-example">Basic Example</a></li>
<li><a class="toctree-l3" href="#why-another-fork">Why Another Fork?</a></li>
<li><a class="toctree-l3" href="#features">Features</a></li>
@@ -110,24 +112,46 @@
<div role="main">
<div class="section">
<h1 id="pdf-major">PDF-Major</h1>
<p>PDF-Major is a fork of <code>PDFMiner.six</code>. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.</p>
<h1 id="pdfmajor">PDFMajor</h1>
<table>
<tr>
<td>Latest Release</td>
<td>
<a href="https://pypi.org/project/pdfmajor/">
<img src="https://img.shields.io/pypi/v/pdfmajor.svg" alt="latest release" />
</a>
</td>
</tr>
</table>
<p>PDF-Major is a fork of <code>PDFMiner.six</code>. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.</p>
<h2 id="documentation">Documentation</h2>
<p>See documentation <a href="https://asosnovsky.github.io/pdfmajor/">here</a>.</p>
<h2 id="basic-example">Basic Example</h2>
<pre><code class="py">from pdfmajor.interpreter import PDFInterpreter
for page in PDFInterpreter(&quot;/path/to/pdf.pdf&quot;):
print(&quot;page start&quot;, page.page_num)
for item in page:
print(&quot; &gt;&quot;, item)
print(&quot;page end&quot;, page.page_num)
</code></pre>
<h2 id="why-another-fork">Why Another Fork?</h2>
<p><code>PDFMiner</code> was designed to run in python 2.7&gt;, <code>PDFMiner.six</code> was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with. </p>
<p><code>PDFMiner</code> was designed to run in python 2.7&gt;, <code>PDFMiner.six</code> was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon <code>PDFMiner.six</code> I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.</p>
<h2 id="features">Features</h2>
<ul>
<li>Written entirely in Python.</li>
<li>Functional extraction method based on generators</li>
<li>Parse, analyze, and convert PDF documents.</li>
<li>PDF-1.7 specification support. (well, almost)</li>
<li>Font-Color extraction</li>
<li>Shape fill and stroke color extraction</li>
<li>CJK languages and vertical writing scripts support.</li>
<li>Various font types (Type1, TrueType, Type3, and CID) support.</li>
<li>Basic encryption (RC4) support.</li>
<li>Font-Color extraction</li>
<li>Shape fill and stroke color extraction</li>
</ul>
<p><strong>Note</strong>: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the <code>pdfmajor.layouts.PDFLayoutAnalyzer</code> class, but at the current time is not supported.</p>
<p><strong>Note</strong>: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the <code>pdfmajor.interpreter.PageInterpreter</code> class, but at the current time is not supported.</p>
<h2 id="how-to-install">How to Install</h2>
<h3 id="source">Source</h3>
<ul>
@@ -216,5 +240,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<!--
MkDocs version : 1.0.4
Build Date UTC : 2018-10-12 22:22:34
Build Date UTC : 2019-12-08 19:44:26
-->
+1 -1
View File
@@ -1 +1 @@
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDF-Major PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability. Documentation See documentation here . Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with. Features Written entirely in Python. Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Font-Color extraction Shape fill and stroke color extraction Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the pdfmajor.layouts.PDFLayoutAnalyzer class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdf-major","text":"PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.","title":"PDF-Major"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with.","title":"Why Another Fork?"},{"location":"#features","text":"Written entirely in Python. Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Font-Color extraction Shape fill and stroke color extraction Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the pdfmajor.layouts.PDFLayoutAnalyzer class, but at the current time is not supported.","title":"Features"},{"location":"#how-to-install","text":"","title":"How to Install"},{"location":"#source","text":"Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install","title":"Source"},{"location":"#pypi","text":"Install Python 3.6.4 or newer. install repo pip install pdfmajor","title":"Pypi"},{"location":"#terms-and-conditions","text":"(This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining aLine truncated
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDFMajor Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon. Documentation See documentation here . Basic Example from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num) Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports. Features Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the pdfmajor.interpreter.PageInterpreter class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdfmajor","text":"Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#basic-example","text":"from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num)","title":"Basic Example"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.","title":"Why Another Fork?"},{"location":"#features","text":"Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTexLine truncated
+2 -2
View File
@@ -2,12 +2,12 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>None</loc>
<lastmod>2018-10-12</lastmod>
<lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2018-10-12</lastmod>
<lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
[mypy]
python_version = 3.7
warn_return_any = True
warn_unused_configs = True
+36 -17
View File
@@ -1,33 +1,54 @@
#
#
# DO NOT EDIT THIS PAGE
#
#
"""
# PDFMajor
# PDFMajor
PDF-Major is a fork of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.
<table>
<tr>
<td>Latest Release</td>
<td>
<a href="https://pypi.org/project/pdfmajor/">
<img src="https://img.shields.io/pypi/v/pdfmajor.svg" alt="latest release" />
</a>
</td>
</tr>
</table>
PDF-Major is a **complete re-write** of `PDFMiner.six`. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.
## Documentation
See documentation [here](https://asosnovsky.github.io/pdfmajor/).
See [full API documentation here](https://asosnovsky.github.io/pdfmajor/api/).
## Why Another Fork?
## Basic Example
```py
from pdfmajor.interpreter import PDFInterpreter
`PDFMiner` was designed to run in python 2.7>, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with.
for page in PDFInterpreter("/path/to/pdf.pdf"):
print("page start", page.page_num)
for item in page:
print(" >", item)
print("page end", page.page_num)
```
## Why a re-write?
`PDFMiner` was designed to run in python 2.7, `PDFMiner.six` was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon `PDFMiner.six` I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.
## Features
* Written entirely in Python.
* Functional extraction method based on generators
* Parse, analyze, and convert PDF documents.
* PDF-1.7 specification support. (well, almost)
* Font-Color extraction
* Shape fill and stroke color extraction
* CJK languages and vertical writing scripts support.
* Various font types (Type1, TrueType, Type3, and CID) support.
* Basic encryption (RC4) support.
* Font-Color extraction
* Shape fill and stroke color extraction
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by extending the `pdfmajor.layouts.PDFLayoutAnalyzer` class, but at the current time is not supported.
**Note**: We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the `pdfmajor.interpreter.PageInterpreter` class, but at the current time is not supported.
## How to Install
@@ -75,10 +96,8 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__version__ = (1, 1, 3)
__version__ = (1, 3, 13)
if __name__ == '__main__':
print(
"PDFMajor v" + '.'.join(map(str, __version__))
)
if __name__ == "__main__":
print("PDFMajor v" + ".".join(map(str, __version__)))
print(__doc__)
-247
View File
@@ -1,247 +0,0 @@
import logging
from typing import Dict
from contextlib import contextmanager
from ..layouts import LTPage
from ..layouts import LTItem
from ..layouts import LTCurve, LTLine, LTRect, CurvePath
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar, LTCharBlock
from ..utils import enc
from .PDFConverter import PDFConverter
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
log = logging.getLogger(__name__)
def get_color(col: PDFGraphicStateColor):
color_type, color_val = col.get_color()
if color_type is not None:
if color_type == 'rgb':
return f"rgb({','.join(map(str, color_val))})"
elif color_type == 'cmyk':
return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray':
return f'rgba(0,0,0,{1-color_val})'
return "none"
class HTMLConverter(PDFConverter):
def __init__(self,
rsrcmgr,
outfp,
codec='utf-8',
pageno=1,
imagewriter=None
):
PDFConverter.__init__(self, rsrcmgr, outfp, imagewriter=imagewriter, codec=codec, pageno=pageno)
self.__levels_deep = 0
self.__current_height = None
self.__current_width = None
self.write_header()
def write(self, text: str, lineend: str = '\n', deep_space: str = ' '):
text = deep_space*self.__levels_deep + text + lineend
self.write_raw(text)
def write_raw(self, text):
if self.codec:
text = text.encode(self.codec)
self.outfp.write(text)
def write_header(self):
self.write('<html>')
self.__levels_deep += 1
self.write('<head>')
self.__levels_deep += 1
meta_attr = {
'http-equiv': "Content-Type",
'content': "text/html",
}
if self.codec:
meta_attr['charset'] = self.codec
self.place_elm_close('meta', attr_dict=meta_attr, as_childless=False)
with self.place_elm_with_child('style'):
self.write('''.page { position: relative; overflow: hidden;border: 1px black solid;}''')
self.__levels_deep -= 1
self.write('</head>')
self.write('<body>')
self.__levels_deep += 1
def write_footer(self):
self.write('</body>')
self.__levels_deep -= 1
self.write('</html>')
self.__levels_deep -= 1
@contextmanager
def place_elm_with_child(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None, no_additional_char = False):
lineend = '\n'
deep_space = ' '
if no_additional_char:
lineend = ''
deep_space = ''
if css is not None:
attr_dict['style'] = ";".join([
f'{name}: {value}'
for name, value in css.items()
])
attrs = " ".join([
f'{name}="{value}"'
for name, value in attr_dict.items()
])
self.write(f"<{tag_name} {attrs}>", lineend=lineend)
self.__levels_deep += 1
yield
self.__levels_deep -= 1
self.write(f"</{tag_name}>", deep_space=deep_space)
def place_elm_close(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None, as_childless = True):
if css is not None:
attr_dict['style'] = ";".join([
f'{name}: {value}'
for name, value in css.items()
])
attrs = " ".join([
f'{name}="{value}"'
for name, value in attr_dict.items()
])
if as_childless:
self.write(f"<{tag_name} {attrs}></{tag_name}>")
else:
self.write(f"<{tag_name} {attrs}/>")
def place_image(self, item: LTImage):
name = item.name
if self.imagewriter is not None:
name = self.imagewriter.export_image(item)
self.place_elm_close('img', {
'src': enc(name, None),
'width': f"{item.width}px",
'height': f"{item.height}px",
})
return
def render_curve(self, item: LTCurve):
css = {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
}
path = ""
svg_attr = {
'class': 'curve',
'z-index': 2
}
if item.height > 0:
dy = 1.0/item.height
dx = (item.width/item.height)/(item.x1-item.x0)
svg_attr.update({
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
})
else:
dx = 1.0/(item.x1-item.x0)
dy = 1.0
svg_attr.update({
"viewBox": "0 0 1 1"
})
if item.height > 1:
svg_attr.update({
'height': f'{item.height}px',
})
else:
svg_attr.update({
'height': '1px',
})
if item.width > 1:
svg_attr.update({
'width': f'{item.width}px',
})
else:
svg_attr.update({
'width': '1px',
})
for p in item.paths:
if p.method == CurvePath.METHOD.MOVE_TO:
path += f'M{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {(item.height- (p.y-item.y0) )*dy}" for p in p.points )} '
if p.method == CurvePath.METHOD.LINE_TO:
path += f'L{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {( item.height - (p.y-item.y0) )*dy}" for p in p.points )} '
if p.method == CurvePath.METHOD.CLOSE_PATH:
path += 'Z'
with self.place_elm_with_child('svg', svg_attr, css):
self.place_elm_close('path', {
"d": path,
'stroke': get_color(item.stroke),
'fill': get_color(item.fill),
})
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
if isinstance(item, LTPage):
with self.place_elm_with_child('div', { "class": 'page', "id": f"page-{item.pageid}" }, { 'width': f'{item.width}px', 'height': f'{item.height}px' }):
self.__current_height = item.height
self.__current_width = item.width
for child in item:
render(child)
elif isinstance(item, LTCurve):
self.render_curve(item)
elif isinstance(item, LTFigure):
with self.place_elm_with_child('div', { "class": 'figure' }, {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}):
for child in item:
render(child)
elif isinstance(item, LTImage):
self.place_image(item)
elif isinstance(item, LTChar):
with self.place_elm_with_child('span', { 'class': 'char' }, {
'width': item.width,
'min-width': item.width,
'max-width': item.width,
'height': item.height,
'min-height': item.height,
'max-height': item.height,
}):
self.write(item.get_text())
elif isinstance(item, LTCharBlock):
with self.place_elm_with_child('span', { 'class': 'char-block' }, {
'position': 'absolute',
'left': f'{item.x0}',
'bottom': f'{item.y0}',
'text-align': 'justify',
'font-size': f'{item.size}px',
'font-family': item.fontname,
'font-weight': item.font.font_weight,
'color': get_color(item.graphicstate.ncolor),
'width': item.width,
'min-width': item.width,
'max-width': item.width,
'height': item.height,
'min-height': item.height,
'max-height': item.height,
'transform': f'skew({item.font.italic_angle}deg, 0deg)',
'text-align': 'center',
'display': 'flex',
'flex-flow': 'row nowrap',
'justify-content': 'space-between',
'padding-left': item.font.leading
}):
for char in item:
render(char)
render(ltpage)
def close(self):
self.write_footer()
-183
View File
@@ -1,183 +0,0 @@
import logging
from typing import Dict, Union
from io import BytesIO
from contextlib import contextmanager
from ..layouts import LTPage
from ..layouts import LTItem
from ..layouts import LTCurve
from ..layouts import LTLine
from ..layouts import LTRect
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar, LTCharBlock
from ..utils import enc
from .PDFConverter import PDFConverter
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
log = logging.getLogger(__name__)
def get_color(col: PDFGraphicStateColor):
color_type, color_val = col.get_color()
if color_type is not None:
if color_type == 'rgb':
return f"rgb({','.join(map(str, color_val))})"
elif color_type == 'cmyk':
return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray':
return f'gray({color_val})'
return ""
class JSONConverter(PDFConverter):
def __init__(self,
rsrcmgr,
outfp: BytesIO,
codec: str='utf-8',
pageno: int=1,
imagewriter=None,
):
PDFConverter.__init__(self, rsrcmgr, outfp, imagewriter=imagewriter, codec=codec, pageno=pageno)
self.__last_levels_deep:int = None
self.__levels_deep = 0
self.write_header()
@property
def _levels_deep(self): return self.__levels_deep
@_levels_deep.setter
def _levels_deep(self, val: int):
self.__last_levels_deep = self.__levels_deep
self.__levels_deep = val
def write(self, text: str):
if self.__last_levels_deep == self._levels_deep:
text = ',' + text
self.write_raw(text)
def write_raw(self, text):
if self.codec:
text = text.encode(self.codec)
self.outfp.write(text)
self._levels_deep = self._levels_deep
def write_header(self):
self.write("{")
self._levels_deep += 1
if self.codec:
self.write('"encoding": "%s"' % self.codec)
self.write('"pages": [')
self._levels_deep += 1
def write_footer(self):
self._levels_deep -= 1
self.write(']')
self._levels_deep -= 1
self.write('}')
@contextmanager
def place_elm_with_child(self, tag_name: str, attr_dict: Dict[str, str] = {}):
attrs = ", ".join([
f'"{name}": "{value}"'
for name, value in attr_dict.items()
])
self.write('{ "type": "%s", "attrs": { %s } , "children": [' % (tag_name, attrs) )
self._levels_deep += 1
yield
self._levels_deep -= 1
self.write("]}")
def place_elm_close(self, tag_name: str, attr_dict: Dict[str, str] = {}):
attrs = ", ".join([
f'"{name}": "{value}"'
for name, value in attr_dict.items()
])
self.write('{ "type": "%s", "attrs": { %s } }' % (tag_name, attrs) )
def place_image(self, item: LTImage):
name = item.name
if self.imagewriter is not None:
name = self.imagewriter.export_image(item)
self.place_elm_close('img', {
'src': enc(name, None),
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
})
def render_curve(self, item: LTCurve):
attr = {
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
"stroke-color": get_color(item.stroke),
"fill-color": get_color(item.fill),
}
if isinstance(item, LTRect):
return self.place_elm_close('rect', attr)
elif isinstance(item, LTLine):
return self.place_elm_close('line', attr)
with self.place_elm_with_child('curve', attr):
for path in item.paths:
with self.place_elm_with_child('path', { "type": path.method.name }):
for point in path.points:
self.place_elm_close('point', { 'x': point.x, 'y': point.y })
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
if isinstance(item, LTPage):
with self.place_elm_with_child('page', {
"width": item.width,
"height": item.height
}):
for child in item:
render(child)
elif isinstance(item, LTCurve):
self.render_curve(item)
elif isinstance(item, LTFigure):
with self.place_elm_with_child('figure', {
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
}):
for child in item:
render(child)
elif isinstance(item, LTImage):
self.place_image(item)
elif isinstance(item, LTChar):
self.place_elm_close('char', {
'font-size': item.size,
'font-family': item.fontname,
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
"stroke-color": get_color(item.graphicstate.scolor),
"fill-color": get_color(item.graphicstate.ncolor),
"text": item.get_text()
})
elif isinstance(item, LTCharBlock):
self.place_elm_close('char-block', {
'font-size': item.size,
'font-family': item.fontname,
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
"stroke-color": get_color(item.graphicstate.scolor),
"fill-color": get_color(item.graphicstate.ncolor),
"text": item.get_text()
})
render(ltpage)
def close(self):
self.write_footer()
-83
View File
@@ -1,83 +0,0 @@
import logging
import re
import os
from typing import List
from io import BytesIO, StringIO, TextIOWrapper
from ..layouts import PDFLayoutAnalyzer
from ..interpreter.PDFResourceManager import PDFResourceManager
from ..interpreter.PDFPage import PDFPage
from ..interpreter.PDFPageInterpreter import PDFPageInterpreter
from ..imagewriter import ImageWriter
log = logging.getLogger(__name__)
## PDFConverter
##
class PDFConverter(PDFLayoutAnalyzer):
@classmethod
def parse_file(cls,
input_file: TextIOWrapper,
output_file: TextIOWrapper,
image_folder_path: str = None,
codec: str = 'utf-8',
maxpages: int = 0,
password: str = None,
caching: bool = True,
check_extractable: bool = True,
pagenos: List[int] = None,
dont_export_images: bool = False,
debug: bool = False,
) -> TextIOWrapper:
if debug:
log.setLevel(logging.DEBUG)
if dont_export_images:
image_folder_path = None
if not dont_export_images and image_folder_path is None and output_file.name is not None:
image_folder_path = os.path.dirname(output_file.name)
log.debug("Creating resources....")
rsrcmgr = PDFResourceManager(caching=caching)
imagewriter=ImageWriter(image_folder_path)
device = cls(
rsrcmgr,
output_file,
codec=codec,
imagewriter=imagewriter
)
interpreter = PDFPageInterpreter(rsrcmgr, device)
log.debug("Reading pages....")
pages = PDFPage.get_pages(input_file, pagenos=pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=check_extractable)
for idx, page in enumerate(pages):
log.debug(f"Reading page #{idx}")
interpreter.process_page(page)
device.close()
return output_file
def __init__(self, rsrcmgr: PDFResourceManager, outfp: TextIOWrapper, imagewriter: ImageWriter, codec: str='utf-8', pageno: int=1):
PDFLayoutAnalyzer.__init__(self, rsrcmgr, pageno=pageno)
self.imagewriter = imagewriter
self.outfp = outfp
self.codec = codec
if hasattr(self.outfp, 'mode'):
if 'b' in self.outfp.mode:
self.outfp_binary = True
else:
self.outfp_binary = False
else:
if isinstance(self.outfp, BytesIO):
self.outfp_binary = True
elif isinstance(self.outfp, StringIO):
self.outfp_binary = False
else:
try:
self.outfp.write(u"é")
self.outfp_binary = False
except TypeError:
self.outfp_binary = True
return
-52
View File
@@ -1,52 +0,0 @@
import logging
from typing import Dict
from contextlib import contextmanager
from ..layouts import LTPage
from ..layouts import LTItem
from ..layouts import LTCurve
from ..layouts import LTLine
from ..layouts import LTRect
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar, LTCharBlock
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
from ..utils import enc
from .PDFConverter import PDFConverter
log = logging.getLogger(__name__)
class TextConverter(PDFConverter):
def __init__(self,
rsrcmgr,
outfp,
codec='utf-8',
pageno=1,
imagewriter=None,
):
PDFConverter.__init__(self, rsrcmgr, outfp, imagewriter=imagewriter, codec=codec, pageno=pageno)
def write(self, text: str):
if self.codec:
text = text.encode(self.codec)
self.outfp.write(text)
def place_text(self, char: LTChar):
self.write(char.get_text())
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
if isinstance(item, LTPage):
self.write(f'\n-----[Page {self.pageno-1}]-----\n')
for child in item:
render(child)
self.write(f'\n-----[Page {self.pageno-1}]-----\n')
elif isinstance(item, LTFigure):
for child in item:
render(child)
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
self.place_text(item)
render(ltpage)
-173
View File
@@ -1,173 +0,0 @@
import logging
from typing import Dict
from contextlib import contextmanager
from ..layouts import LTPage
from ..layouts import LTItem
from ..layouts import LTCurve
from ..layouts import LTLine
from ..layouts import LTRect
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar, LTCharBlock
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
from ..utils import enc
from .PDFConverter import PDFConverter
log = logging.getLogger(__name__)
def get_color(col: PDFGraphicStateColor):
color_type, color_val = col.get_color()
if color_type is not None:
if color_type == 'rgb':
return f"rgb({','.join(map(str, color_val))})"
elif color_type == 'cmyk':
return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray':
return f'gray({color_val})'
return ""
class XMLConverter(PDFConverter):
def __init__(self,
rsrcmgr,
outfp,
codec='utf-8',
pageno=1,
imagewriter=None,
):
PDFConverter.__init__(self, rsrcmgr, outfp, imagewriter=imagewriter, codec=codec, pageno=pageno)
self.__levels_deep = 0
self.write_header()
def write(self, text: str, lineend: str = '\n', deep_space: str = ' '):
text = deep_space*self.__levels_deep + text + lineend
self.write_raw(text)
def write_raw(self, text):
if self.codec:
text = text.encode(self.codec)
self.outfp.write(text)
def write_header(self):
if self.codec:
self.write('<?xml version="1.0" encoding="%s" ?>' % self.codec)
else:
self.write('<?xml version="1.0" ?>')
self.write('<pages>')
self.__levels_deep += 1
def write_footer(self):
self.write('</pages>')
@contextmanager
def place_elm_with_child(self, tag_name: str, attr_dict: Dict[str, str] = {}, no_additional_char = False):
lineend = '\n'
deep_space = ' '
if no_additional_char:
lineend = ''
deep_space = ''
attrs = " ".join([
f'{name}="{value}"'
for name, value in attr_dict.items()
])
self.write(f"<{tag_name} {attrs}>", lineend=lineend)
self.__levels_deep += 1
yield
self.__levels_deep -= 1
self.write(f"</{tag_name}>", deep_space=deep_space)
def place_elm_close(self, tag_name: str, attr_dict: Dict[str, str] = {}, as_childless = True):
attrs = " ".join([
f'{name}="{value}"'
for name, value in attr_dict.items()
])
if as_childless:
self.write(f"<{tag_name} {attrs}></{tag_name}>")
else:
self.write(f"<{tag_name} {attrs}/>")
def place_full_container(self, class_name: str, item: LTItem):
return self.place_elm_with_child(class_name, {
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
})
def place_image(self, item: LTImage):
name = item.name
if self.imagewriter is not None:
name = self.imagewriter.export_image(item)
self.place_elm_close('img', {
'src': enc(name, None),
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
})
def place_text(self, char: LTChar):
tag_name = 'char'
if isinstance(char, LTCharBlock):
tag_name = 'char-block'
attr = {
'size': char.size,
"color": get_color(char.graphicstate.ncolor),
"x0": char.x0,
"x1": char.x1,
"y0": char.y0,
"y1": char.y1,
}
for key, value in char.font.descriptor.items():
if key != "Type" and "FontFile" not in key:
attr["font-" + key] = value
with self.place_elm_with_child(tag_name, attr, no_additional_char=True):
self.write(char.get_text(), lineend='', deep_space="")
def render_curve(self, item: LTCurve):
attr = {
"x0": item.x0,
"x1": item.x1,
"y0": item.y0,
"y1": item.y1,
"stroke-color": get_color(item.stroke),
"fill-color": get_color(item.fill),
}
if isinstance(item, LTRect):
return self.place_elm_close('rect', attr)
elif isinstance(item, LTLine):
return self.place_elm_close('line', attr)
with self.place_elm_with_child('curve', attr):
for path in item.paths:
with self.place_elm_with_child('path', { "type": path.method.name }):
for point in path.points:
self.place_elm_close('point', { 'x': point.x, 'y': point.y })
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
if isinstance(item, LTPage):
with self.place_full_container('page', item):
for child in item:
render(child)
elif isinstance(item, LTCurve):
self.render_curve(item)
elif isinstance(item, LTFigure):
with self.place_full_container('figure', item):
for child in item:
render(child)
elif isinstance(item, LTImage):
self.place_image(item)
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
self.place_text(item)
render(ltpage)
def close(self):
self.write_footer()
-89
View File
@@ -1,89 +0,0 @@
import io
import os
import re
from typing import List
from .HTMLConverter import HTMLConverter
from .JSONConverter import JSONConverter
from .XMLConverter import XMLConverter
from .TextConverter import TextConverter
def convert_file(
input_file: io.TextIOWrapper,
output_file: io.TextIOWrapper = None,
image_folder_path: str = None,
codec: str = 'utf-8',
maxpages: int = 0,
password: str = None,
caching: bool = True,
check_extractable: bool = True,
pagenos: List[int] = None,
debug: bool = False,
out_type: str = 'html',
):
if output_file is None:
output_file = open(
os.path.join(
os.path.dirname(input_file.name),
os.path.basename(
re.sub(r"\.\w+$", "."+out_type, input_file.name)
)
),
'wb'
)
if re.search(re.escape(out_type) + r'$', output_file.name ) is None:
raise Exception("Please make sure that the file name and output type match!")
if out_type == 'html':
return HTMLConverter.parse_file(
input_file,
output_file,
image_folder_path,
codec,
maxpages,
password,
caching,
check_extractable,
pagenos,
debug,
)
elif out_type == 'xml':
return XMLConverter.parse_file(
input_file,
output_file,
image_folder_path,
codec,
maxpages,
password,
caching,
check_extractable,
pagenos,
debug,
)
elif out_type == 'json':
return JSONConverter.parse_file(
input_file,
output_file,
image_folder_path,
codec,
maxpages,
password,
caching,
check_extractable,
pagenos,
debug,
)
elif out_type == 'text':
return TextConverter.parse_file(
input_file,
output_file,
image_folder_path,
codec,
maxpages,
password,
caching,
check_extractable,
pagenos,
debug,
)
else: raise Exception("Please specify out_type as 'html' or 'xml' or 'json' or 'text'")
+1
View File
@@ -0,0 +1 @@
from .document import PDFDocument
+18
View File
@@ -0,0 +1,18 @@
from typing import Any, Dict, NamedTuple, Optional
from pdfmajor.document.metadata import PDFMetadata
from pdfmajor.document.pages import PDFPageTreeNode
from pdfmajor.pdf_parser.objects import PDFDictionary, PDFName
class PDFDocumentCatalog(NamedTuple):
"""A Parsed version of the PDF Catalog object (see PDF spec 1.7 section 7.7.2 for more detail)"""
version: Optional[PDFName]
pages: PDFPageTreeNode
page_labels: Optional[PDFDictionary]
page_layout: Optional[PDFName]
page_mode: Optional[PDFName]
metadata: Optional[PDFMetadata]
raw: Dict[str, Any] # for other properties
+69
View File
@@ -0,0 +1,69 @@
import warnings
from pathlib import Path
from typing import BinaryIO, Iterator, List, Optional
from pdfmajor.document.pages import PDFPage
from pdfmajor.document.parse_context import PDFParsingContext
from pdfmajor.document.parsers.pages import iter_all_page_leafs
from pdfmajor.document.parsers.root import get_catalog, get_info
from pdfmajor.streambuffer import BufferStream
class PDFDocument:
"""A class encapsulating all of the access behavior to the document"""
@classmethod
def from_io(cls, fp: BinaryIO, buffer_size=4096):
return cls(BufferStream(fp, buffer_size))
@classmethod
def from_path(cls, path: Path, buffer_size=4096):
return cls(BufferStream(path.open("rb"), buffer_size))
def __init__(self, buffer: BufferStream) -> None:
self.__parser = PDFParsingContext(buffer)
self.__processed_pages: Optional[List[PDFPage]] = None
self.__processed_pages_done = False
self.catalog = get_catalog(self.__parser)
self.info = get_info(self.__parser)
@property
def health_report(self):
"""A log of errors that occured in the document during the processing of the file"""
return self.__parser.health_report
@property
def num_pages(self) -> int:
"""Returns the number of pages that the metadata of the document claims it has.
This number should be trusted, unless you suspect the document may have encoutered some corruption. To get an excat count do `len(list(self.iter_pages()))`. Please note that this will load all of the metadata of each document into memory.
Returns:
int
"""
return self.catalog.pages.leaft_count
def iter_pages(self) -> Iterator[PDFPage]:
"""Iterate over all of the pages in the document, this will also keep a record of the pdf page in memory for quicker future retrieval! (use self.clear() to free it up)
Yields:
Iterator[PDFPage]
"""
if self.__processed_pages is not None and self.__processed_pages_done:
return iter(self.__processed_pages)
else:
self.__processed_pages = []
for page in iter_all_page_leafs(self.__parser, self.catalog.pages):
self.__processed_pages.append(page)
yield page
self.__processed_pages_done = True
if len(self.__processed_pages) != self.num_pages:
warnings.warn(
"Number of pages proceseed with iter_pages did not match number of pages specified by the document's metadata!"
f"\n --> expected pages = {self.num_pages}"
f"\n --> found pages = {len(self.__processed_pages)}"
)
def clear(self):
"""Clears out memory used by the class as much as possible"""
self.__processed_pages = None
self.__processed_pages_done = False
+37
View File
@@ -0,0 +1,37 @@
from decimal import Decimal
from typing import List, Set
from pdfmajor.exceptions import BrokenFile, PDFMajorException
from pdfmajor.pdf_parser.objects import ObjectRef
class PDFDocumentError(PDFMajorException):
pass
class BrokenFilePDF(BrokenFile):
pass
class MissingCatalogObj(BrokenFilePDF):
pass
class InvalidCatalogObj(BrokenFilePDF):
pass
class TooManyInfoObj(BrokenFilePDF):
def __init__(self, elms: Set[ObjectRef]) -> None:
super().__init__(f"Too many info elements found | elms={elms}")
self.elms = elms
class TooManyRectField(BrokenFilePDF):
def __init__(self, values: List[Decimal]) -> None:
super().__init__(f"Too many elements found for rectangle | values={values}")
self.values = values
class InvalidPagesNodeKids(BrokenFilePDF):
pass
+8
View File
@@ -0,0 +1,8 @@
from typing import NamedTuple
class PDFMetadata(NamedTuple):
"""A structure representing the data in a PDF Metadata object as is specified in PDF spec 1.7 section 14.3.2"""
sub_type: str
stream_data: bytes
@@ -0,0 +1,18 @@
from typing import Any, Dict, List, NamedTuple, Union # noqa
from pdfmajor.fonts import PDFFont
class PDFResources(NamedTuple):
"""A class storing PDF Resources as described in PDF spec 1.7. section 7.8.3"""
font: Dict[str, PDFFont]
# TODO: the rest
# extgstate: Dict[str, Any]
# color_space: Dict[str, Union[str, List[str]]]
# pattern: Dict[str, Any]
# shading: Dict[str, Any]
# xobject: Dict[str, Any]
# procset: Dict[str, Any]
raw: Dict[str, Any]
+74
View File
@@ -0,0 +1,74 @@
from typing import List, NamedTuple, Optional
from pdfmajor.document.exceptions import InvalidPagesNodeKids
from pdfmajor.document.parse_context import PDFParsingContext
from pdfmajor.document.structures import PDFRectangle
from pdfmajor.exceptions import PDFMajorException
from pdfmajor.pdf_parser.objects import (
ObjectRef,
PDFDictionary,
PDFInteger,
PDFReal,
PDFStream,
validate_object_or_none,
)
from .utils import iter_single_ref_as_array_ref
class PDFPageTreeNode(NamedTuple):
"""A PDF Pages Node representation as it confirms with PDF spec 1.7 section 7.7.3.2"""
kids: List[ObjectRef]
parent: Optional[ObjectRef]
leaft_count: int
raw: PDFDictionary
@classmethod
def from_pdfdict(cls, pdfdict: PDFDictionary):
parent = validate_object_or_none(pdfdict.get("Parent"), ObjectRef)
kids = pdfdict["Kids"]
count = validate_object_or_none(pdfdict.get("Count"), PDFInteger)
try:
vetted_kids: List[ObjectRef] = list(
iter_single_ref_as_array_ref(pdfdict["Kids"])
)
except PDFMajorException:
raise InvalidPagesNodeKids(f"{kids}")
return cls(
kids=vetted_kids,
parent=parent,
raw=pdfdict,
leaft_count=0 if count is None else count.to_python(),
)
class PDFDataPage(NamedTuple):
"""The data of a PDF Page as it confirms with PDF Spec 1.7 section 7.7.3.3"""
parent: ObjectRef
resources: PDFDictionary
mediabox: PDFRectangle
cropbox: PDFRectangle
bleedbox: PDFRectangle
trimbox: PDFRectangle
artbox: PDFRectangle
contents: List[PDFStream]
boxcolor_info: Optional[PDFDictionary]
rotate: PDFInteger
metadata: Optional[PDFStream]
user_unit: PDFReal
raw: PDFDictionary
class PDFPage:
"""A class encapsulating interactions with a PDF Page"""
def __init__(self, pctx: PDFParsingContext, data: PDFDataPage) -> None:
self.__pctx = pctx
self.data = data
def __repr__(self) -> str:
return f"PDFPage(data={repr(self.data)})"
+237
View File
@@ -0,0 +1,237 @@
from pathlib import Path
from typing import Any, BinaryIO, Dict, Iterator, Optional, Tuple, Type, TypeVar, Union
from pdfmajor.document.exceptions import BrokenFilePDF
from pdfmajor.document.parsers.stream import decode_stream, iter_filters_in_stream
from pdfmajor.document.structures import PDFRectangle
from pdfmajor.healthlog import PDFHealthReport
from pdfmajor.pdf_parser.objects import (
IndirectObject,
ObjectRef,
PDFArray,
PDFDictionary,
PDFInteger,
PDFObject,
PDFReal,
validate_number_or_none,
validate_object_or_none,
)
from pdfmajor.pdf_parser.objects.stream import PDFStream
from pdfmajor.streambuffer import BufferStream
from pdfmajor.xref.xrefdb import GenNum, ObjNum, XRefDB
class PDFParsingContext:
"""A utility class to encapsulate various interactions with the raw document stream"""
@classmethod
def from_io(cls, fp: BinaryIO, buffer_size=4096):
return cls(BufferStream(fp, buffer_size))
@classmethod
def from_path(cls, path: Path, buffer_size=4096):
return cls(BufferStream(path.open("rb"), buffer_size))
def __init__(self, buffer: BufferStream) -> None:
self.buffer = buffer
self.health_report = PDFHealthReport()
with buffer.get_window():
self.xrefdb = XRefDB(buffer)
def get_object_from_ref(self, objref: ObjectRef) -> IndirectObject:
"""Get an indirect object from it's object reference
Args:
objref (ObjectRef)
Returns:
IndirectObject
"""
with self.buffer.get_window():
return self.xrefdb.get_obj(
objref.obj_num, objref.gen_num, buffer=self.buffer
)
ExpectedType = TypeVar("ExpectedType", bound=PDFObject)
def get_validate_object(
self, obj: Optional[PDFObject], exptype: Type[ExpectedType]
) -> Optional[ExpectedType]:
"""Validates the object's type if its not None
Args:
obj (Optional[PDFObject])
objtype (Type[ExpectedType]): expected type of the object
Raises:
AssertionError: is raised if the object has an invalid type
Returns:
Optional[ExpectedType]
"""
if isinstance(obj, ObjectRef):
return validate_object_or_none(
self.get_object_from_ref(obj).get_object(), exptype
)
return validate_object_or_none(obj, exptype)
def get_validate_numeric(
self, obj: Optional[PDFObject]
) -> Optional[Union[PDFInteger, PDFReal]]:
"""Validates the object is numeric, if its not None
Args:
obj (Optional[PDFObject])
Raises:
AssertionError: is raised if the object has an invalid type
Returns:
Optional[Union[PDFInteger, PDFReal]]
"""
if isinstance(obj, ObjectRef):
return validate_number_or_none(self.get_object_from_ref(obj).get_object())
return validate_number_or_none(obj)
def convert_pdfdict_to_validated_pythondict(
self,
pdfdict: PDFDictionary,
field_mapping: Dict[str, Tuple[str, Type[PDFObject]]],
) -> Dict[str, Optional[PDFObject]]:
"""Takes a pdf-dictionary and converts it to a regular dictionary (but first validates the types of the objects)
Args:
pdfdict (PDFDictionary)
field_mapping (Dict[str, Tuple[Type[PDFObject]]]): mapping of the name as it in the pdf to a tuple with a (new name, expected objcet type)
Returns:
dict
"""
out: Dict[str, Optional[PDFObject]] = {}
for pdf_name, (new_name, exp_type) in field_mapping.items():
try:
out[new_name] = self.get_validate_object(
pdfdict.get(pdf_name), exp_type
)
except AssertionError as err:
self.health_report.write_error(err)
return out
def extract_and_validate_pdf_rectangles(
self, pdfdict: PDFDictionary, field_mapping: Dict[str, str]
) -> Dict[str, Optional[PDFRectangle]]:
"""Takes a pdf-dictionary and extracts the specified fields. Next it will validate and convert the fields into PDFRectangles
Args:
pdfdict (PDFDictionary)
field_mapping (Dict[str, str]): a mapping of names as they appear in the pdf to requested names
Returns:
Dict[str, Optional[PDFRectangle]]
"""
vetted_values: Any = self.convert_pdfdict_to_validated_pythondict(
pdfdict,
{
pdfname: (newname, PDFArray)
for pdfname, newname in field_mapping.items()
},
)
return {
newname: PDFRectangle.from_pdfarray(value) if value is not None else value
for newname, value in vetted_values.items()
}
def get_obj_by_type(self, obj_type: str) -> Iterator[IndirectObject]:
"""Iterates over the objects in memeory and filters out for the ones that match the requested type
If the object is not loaded into memory this function will load it.
Args:
obj_type (ObjType)
Raises:
InvalidXref: if the object could not be found in memory or the buffer
XRefError: if the object type if found but the object was never loaded into memory
Yields:
Iterator[IndirectObject]
"""
with self.buffer.get_window() as buffer:
return self.xrefdb.get_obj_by_type(buffer, obj_type)
def get_obj(
self,
obj_num: ObjNum,
gen_num: GenNum,
) -> IndirectObject:
"""Get's an indirect object from either the pdf stream or a cached version in memeory
Args:
obj_num (ObjNum)
gen_num (GenNum)
buffer (BufferStream)
Raises:
InvalidXref: if the reference returned was not an indirect object
Returns:
IndirectObject
"""
with self.buffer.get_window() as buffer:
return self.xrefdb.get_obj(obj_num, gen_num, buffer)
def validated_and_iter_stream(
self, obj_ref: PDFObject
) -> Tuple[PDFDictionary, bytes]:
"""Validates that an object is either an ObjectRef or IndirectObject and then checks that it has a stream.
Once those checks are passed, this method will decode the stream and return it's contents witht the indirect objects dictionary
Args:
obj_ref (PDFObject)
Raises:
BrokenFilePDF: if a validation fails
Returns:
Tuple[PDFDictionary, bytes]: (metadata, stream data)
"""
if isinstance(obj_ref, ObjectRef):
obj = self.get_object_from_ref(obj_ref)
elif isinstance(obj_ref, IndirectObject):
obj = obj_ref
else:
raise BrokenFilePDF(f"Expected a pdf-stream, instead found {obj_ref}")
if obj.stream is None:
raise BrokenFilePDF(f"Expected a pdf-stream, instead found {obj}")
else:
obj_def = obj.get_object()
if not isinstance(obj_def, PDFDictionary):
self.health_report.write_error(
BrokenFilePDF(
f"Stream object is missing a defining dictionary {obj_def} {obj_def}"
)
)
obj_def = PDFDictionary()
return obj_def, self.decode_stream(obj.stream)
def decode_stream(self, stream: PDFStream) -> bytes:
"""Decode the bytes associated with the PDF-Stream
Args:
stream (PDFStream)
Returns:
bytes
"""
offset = stream.offset
length_ref = stream.length
if isinstance(length_ref, ObjectRef):
length_obj = self.get_object_from_ref(length_ref).get_object()
if not isinstance(length_obj, PDFInteger):
raise BrokenFilePDF(f"Length should be an int not {length_obj}")
else:
length_obj = length_ref
length = length_obj.value
return decode_stream(
offset, length, iter_filters_in_stream(stream), self.buffer
)
File renamed without changes.
+18
View File
@@ -0,0 +1,18 @@
from pdfmajor.document.metadata import PDFMetadata
from pdfmajor.pdf_parser.objects import PDFName, PDFObject
from ..exceptions import BrokenFilePDF
from ..parse_context import PDFParsingContext
def get_metadata(pctx: PDFParsingContext, ref: PDFObject):
obj_def, data = pctx.validated_and_iter_stream(ref)
subtype = obj_def["Subtype"]
if not isinstance(subtype, PDFName):
pctx.health_report.write_error(
BrokenFilePDF(
f"Metadata data object is missing a valid subtype {ref} {obj_def}"
)
)
return PDFMetadata("", data)
return PDFMetadata(subtype.value, data)
+250
View File
@@ -0,0 +1,250 @@
from decimal import Decimal
from typing import Any, Dict, Iterator, List, Optional
from pdfmajor.document.exceptions import BrokenFilePDF
from pdfmajor.pdf_parser.objects import (
IndirectObject,
ObjectRef,
PDFArray,
PDFDictionary,
PDFInteger,
PDFObject,
PDFReal,
PDFStream,
validate_object_or_none,
)
from ..pages import PDFDataPage, PDFPage, PDFPageTreeNode
from ..parse_context import PDFParsingContext
from ..utils import iter_single_ref_as_array_ref
PDFRawFields = Dict[str, Any]
def iter_all_page_leafs(
pctx: PDFParsingContext, root_node: PDFPageTreeNode
) -> Iterator[PDFPage]:
"""Iterate over every page in the pdf
Args:
pctx (PDFParsingContext)
root_node (PDFPageTreeNode)
Yields:
Iterator[PDFPage]
"""
current_root: List[PDFPageTreeNode] = [root_node]
current_root_aspage: List[Dict[str, Optional[PDFObject]]] = [
parse_pdfpage_fields(pctx, root_node.raw, allow_missing=True)
]
current_child_idx: List[int] = [0]
while len(current_root) > 0:
croot = current_root[-1]
croot_pagedata = current_root_aspage[-1]
ckids = croot.kids
cidx = current_child_idx[-1]
if len(ckids) < cidx + 1:
try:
current_root.pop()
current_root_aspage.pop()
current_child_idx.pop()
continue
except IndexError:
break
page_ref = ckids[cidx]
obj = pctx.get_object_from_ref(page_ref).get_object()
if isinstance(obj, PDFDictionary):
if obj["Type"].to_python() == "/Pages":
current_root.append(PDFPageTreeNode.from_pdfdict(obj))
current_root_aspage.append(
{
**croot_pagedata,
**parse_pdfpage_fields(pctx, obj, allow_missing=True),
}
)
current_child_idx[-1] += 1
current_child_idx.append(0)
continue
elif obj["Type"].to_python() == "/Page":
yield make_pdfpage(croot_pagedata, pctx, obj)
current_child_idx[-1] += 1
continue
# catch all other cases (the above expression will issue a 'continue')
pctx.health_report.write(
"InvalidPageobject",
f"Expecting page ref {page_ref} to be a dictionary with type 'Pages' or 'Page', but got a {obj}",
)
current_child_idx[-1] += 1
def make_pdfpage(
lastpage: Optional[Dict[str, Optional[PDFObject]]],
pctx: PDFParsingContext,
pdfdict: PDFDictionary,
) -> PDFPage:
"""Creates a PDFPage object, and handle the inheritance of properties
Args:
lastpage (Optional[Dict[str, Optional[PDFObject]]]): the last inheritable properties for the pdf
pctx (PDFParsingContext)
pdfdict (PDFDictionary)
Returns:
PDFPage
"""
vetted_fields: Any = parse_pdfpage_fields(
pctx, pdfdict, lastpage=lastpage, allow_missing=False
)
parent = validate_object_or_none(pdfdict.get("Parent"), ObjectRef)
content_streams: List[PDFStream] = []
if pdfdict.get("Contents", None) is not None:
content_streams = list(_parse_page_contents(pdfdict["Contents"], pctx))
else:
pctx.health_report.write(
"MissingPDFPageContents",
f"Page with data {pdfdict} is missing the 'Contents' property."
"Will assume that the page has no content.",
)
vetted_fields["parent"] = parent
vetted_fields["contents"] = content_streams
vetted_fields["raw"] = pdfdict
vetted_fields["metadata"] = _parse_metadata(pdfdict.get("Metadata", None), pctx)
return PDFPage(pctx, PDFDataPage(**vetted_fields))
def parse_pdfpage_fields(
pctx: PDFParsingContext,
pdfdict: PDFDictionary,
allow_missing: bool = False,
lastpage: Optional[PDFRawFields] = None,
) -> PDFRawFields:
parsed_values = pctx.convert_pdfdict_to_validated_pythondict(
pdfdict,
{
"Resources": ("resources", PDFDictionary),
"BoxColorInfo": ("boxcolor_info", PDFDictionary),
"Rotate": ("rotate", PDFInteger),
},
)
mediaboxes = pctx.extract_and_validate_pdf_rectangles(
pdfdict,
{
"MediaBox": "mediabox",
"CropBox": "cropbox",
"BleedBox": "bleedbox",
"TrimBox": "trimbox",
"ArtBox": "artbox",
},
)
return _deal_with_inheritance(
{
**parsed_values, # type: ignore
**mediaboxes, # type: ignore
"user_unit": pctx.get_validate_numeric(pdfdict.get("UserUnit")), # type: ignore
},
lastpage,
allow_missing,
)
def _parse_metadata(
metadata_raw: Optional[PDFObject], pctx: PDFParsingContext
) -> Optional[PDFStream]:
if metadata_raw is None:
return None
if isinstance(metadata_raw, ObjectRef):
metadata_obj = pctx.get_object_from_ref(metadata_raw)
elif isinstance(metadata_raw, IndirectObject):
metadata_obj = metadata_raw
else:
raise BrokenFilePDF(f"Metadata object is not a valid type {metadata_raw}")
if metadata_obj.stream is None:
raise BrokenFilePDF(f"Metadata object is missing it's stream {metadata_obj}")
else:
return metadata_obj.stream
def _parse_page_contents(contents_raw: PDFObject, pctx: PDFParsingContext):
for content_ref in iter_single_ref_as_array_ref(contents_raw):
content_obj = pctx.get_object_from_ref(content_ref)
if isinstance(content_obj, PDFArray):
for subref in iter_single_ref_as_array_ref(content_obj):
sub_obj = pctx.get_object_from_ref(subref)
if isinstance(sub_obj, IndirectObject):
if sub_obj.stream is not None:
yield sub_obj.stream
continue
raise BrokenFilePDF(f"Invalid PDFStream found for page {sub_obj}")
continue
elif isinstance(content_obj, IndirectObject):
if content_obj.stream is not None:
yield content_obj.stream
continue
raise BrokenFilePDF(f"Invalid PDFStream found for page {content_obj}")
def _deal_with_inheritance(
field_dict: Dict[str, Optional[PDFObject]],
lastpage: Optional[PDFRawFields],
allow_missing: bool = False,
) -> Dict[str, Any]:
_deal_with_inheritance_for_field("resources", field_dict, lastpage, PDFDictionary())
_deal_with_inheritance_for_field(
"rotate", field_dict, lastpage, PDFInteger(0, 0, 0)
)
_deal_with_inheritance_for_field(
"user_unit", field_dict, lastpage, PDFReal(Decimal("1.0"), 0, 0)
)
_deal_with_inheritance_for_field(
"mediabox", field_dict, lastpage, allow_missing=allow_missing
)
_deal_with_inheritance_for_field(
"cropbox",
field_dict,
lastpage,
default_value=field_dict["mediabox"],
allow_missing=allow_missing,
)
_deal_with_inheritance_for_field(
"bleedbox",
field_dict,
lastpage,
default_value=field_dict["cropbox"],
allow_missing=allow_missing,
)
_deal_with_inheritance_for_field(
"trimbox",
field_dict,
lastpage,
default_value=field_dict["cropbox"],
allow_missing=allow_missing,
)
_deal_with_inheritance_for_field(
"artbox",
field_dict,
lastpage,
default_value=field_dict["cropbox"],
allow_missing=allow_missing,
)
return field_dict
def _deal_with_inheritance_for_field(
field_name: str,
field_dict: Dict[str, Optional[PDFObject]],
lastpage: Optional[PDFRawFields],
default_value: Optional[PDFObject] = None,
allow_missing: bool = False,
):
if not field_dict[field_name]:
if not lastpage:
if default_value is not None:
field_dict[field_name] = default_value
elif not allow_missing:
raise BrokenFilePDF(f"Page is missing {field_name}")
elif lastpage.get(field_name, None):
field_dict[field_name] = lastpage[field_name]
elif default_value is not None:
field_dict[field_name] = default_value
elif not allow_missing:
raise BrokenFilePDF(f"Page is missing {field_name}")
+35
View File
@@ -0,0 +1,35 @@
from pdfmajor.document.exceptions import BrokenFilePDF
from pdfmajor.pdf_parser.objects.ref import ObjectRef
from pdfmajor.fonts import PDFFont, PDFFontType1
from typing import Dict, Iterator, Tuple
from pdfmajor.pdf_parser import PDFDictionary
from ..page_parser.resources import PDFResources
from ..parse_context import PDFParsingContext
def get_resources(
pctx: PDFParsingContext, resource_pdfdict: PDFDictionary
) -> PDFResources:
fonts_obj = resource_pdfdict.get("Font", PDFDictionary())
if not isinstance(fonts_obj, PDFDictionary):
raise BrokenFilePDF(f"Invalid font obj {fonts_obj}")
fonts: Dict[str, PDFFont] = dict(_parse_out_fonts(pctx, fonts_obj))
return PDFResources(font=fonts, raw=resource_pdfdict)
def _parse_out_fonts(
pctx: PDFParsingContext, font_pdfdict: PDFDictionary
) -> Iterator[Tuple[str, PDFFont]]:
for font_alias, font_ref in font_pdfdict.items():
if isinstance(font_ref, ObjectRef):
font_obj = pctx.get_object_from_ref(font_ref).get_object()
if not isinstance(font_obj, PDFDictionary):
raise BrokenFilePDF(f"Invalid font {font_obj}")
elif isinstance(font_ref, PDFDictionary):
font_obj = font_ref
else:
raise BrokenFilePDF(f"Invalid font {font_ref}")
subtype = font_obj["Subtype"]
if subtype.to_python() in ["/Type1", "/MMType1"]:
yield font_alias, PDFFontType1.from_dict(font_obj)
+98
View File
@@ -0,0 +1,98 @@
from typing import Any, Optional, Set
from pdfmajor.document.pages import PDFPageTreeNode
from pdfmajor.document.parsers.metadata import get_metadata
from pdfmajor.pdf_parser.objects import (
IndirectObject,
ObjectRef,
PDFDictionary,
PDFName,
PDFObject,
)
from pdfmajor.xref.exceptions import InvalidNumberOfRoots, NotRootElement
from pdfmajor.xref.trailer import get_root_obj
from ..catalog import PDFDocumentCatalog
from ..exceptions import InvalidCatalogObj, MissingCatalogObj, TooManyInfoObj
from ..parse_context import PDFParsingContext
def get_info(pctx: PDFParsingContext) -> Optional[PDFObject]:
"""get a Parsed version of the PDF Info object if it exists (see PDF spec 1.7 section 14.3.3 for more detail)"""
info_obj = _find_indirect_obj_for_info(pctx)
if info_obj is None:
return None
else:
return info_obj.get_object()
def get_catalog(pctx: PDFParsingContext) -> PDFDocumentCatalog:
"""Gets the catalog for a document
Raises:
InvalidCatalogObj: if something is not correctly set in the catalog
Returns:
PDFDocumentCatalog
"""
cat_obj = _find_indirect_obj_for_root(pctx).get_object()
if not isinstance(cat_obj, PDFDictionary):
raise InvalidCatalogObj("not a dictionary")
validated_fields: Any = pctx.convert_pdfdict_to_validated_pythondict(
cat_obj,
{
"Version": ("version", PDFName),
"PageLabels": ("page_labels", PDFDictionary),
"PageLayout": ("page_layout", PDFName),
"PageMode": ("page_mode", PDFName),
},
)
try:
pages_obj = cat_obj["Pages"]
except KeyError:
raise InvalidCatalogObj(f"Missing Pages entry {cat_obj}")
if isinstance(pages_obj, ObjectRef):
pages = pctx.get_object_from_ref(pages_obj).get_object()
if not isinstance(pages, PDFDictionary):
raise InvalidCatalogObj(f"Invalid pages ref entry {pages} from {pages_obj}")
elif isinstance(pages_obj, PDFDictionary):
pages = pages_obj
else:
raise InvalidCatalogObj(f"Invalid pages entry {pages_obj}")
if cat_obj.get("Metadata"):
validated_fields["metadata"] = get_metadata(pctx, cat_obj["Metadata"])
else:
validated_fields["metadata"] = None
validated_fields["pages"] = PDFPageTreeNode.from_pdfdict(pages)
validated_fields["raw"] = cat_obj
return PDFDocumentCatalog(**validated_fields)
def _find_indirect_obj_for_root(pctx: PDFParsingContext) -> IndirectObject:
try:
obj_ref = get_root_obj(pctx.xrefdb.trailers)
root_element = pctx.get_obj(obj_num=obj_ref.obj_num, gen_num=obj_ref.gen_num)
except InvalidNumberOfRoots as e:
pctx.health_report.write_error(e)
obj_ref = next(iter(e.roots))
root_element = pctx.get_obj(obj_num=obj_ref.obj_num, gen_num=obj_ref.gen_num)
except NotRootElement as e:
pctx.health_report.write_error(e)
try:
root_element = next(pctx.get_obj_by_type("/Catalog"))
except StopIteration:
raise MissingCatalogObj()
return root_element
def _find_indirect_obj_for_info(pctx: PDFParsingContext) -> Optional[IndirectObject]:
elements: Set[ObjectRef] = set()
for trailer in pctx.xrefdb.trailers:
if trailer.info is not None:
elements.add(trailer.info)
if len(elements) > 1:
pctx.health_report.write_error(TooManyInfoObj(elements))
elif len(elements) == 0:
return None
ref = next(iter(elements))
return pctx.get_obj(ref.obj_num, ref.gen_num)
+55
View File
@@ -0,0 +1,55 @@
from itertools import chain
from typing import Any, Dict, Iterator
from pdfmajor.filters import FilterPair, process_filters_on_data
from pdfmajor.pdf_parser.exceptions import IncompleteStream
from pdfmajor.pdf_parser.objects import PDFDictionary, PDFName, PDFStream
from pdfmajor.streambuffer import BufferStream
def iter_filters_in_stream(stream: PDFStream) -> Iterator[FilterPair]:
"""loops over all filters and their params in the stream
Args:
stream (PDFStream)
Raises:
IncompleteStream: if a filter-name is invalid
Yields:
Iterator[FilterPair]
"""
for f, dp in chain(
zip(stream.filter, stream.decode_parms),
zip(stream.ffilter, stream.fdecode_parms),
):
if not isinstance(f, PDFName):
raise IncompleteStream(f"Invalid filter name {f}")
else:
filter_name = f.value
params: Dict[str, Any] = {}
if isinstance(dp, PDFDictionary):
params = dp.to_python()
yield filter_name, params
def decode_stream(
offset: int, length: int, it_filters: Iterator[FilterPair], buffer: BufferStream
) -> bytes:
"""Decode the bytes associated with the PDF-Stream
Args:
offset (int)
length (int)
it_filters (Iterator[FilterPair])
buffer (BufferStream)
Returns:
bytes
"""
with buffer.get_window():
buffer.seek(offset)
data = buffer.read(length).data
data = process_filters_on_data(data, it_filters)
return data
+48
View File
@@ -0,0 +1,48 @@
from decimal import Decimal
from typing import List, NamedTuple
import numpy as np
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
from pdfmajor.pdf_parser.objects import PDFArray, PDFInteger, PDFReal
class PDFRectangle(NamedTuple):
"""A PDF Rectangle as specified by PDF spec 1.7 section 7.9.5"""
llx: Decimal
lly: Decimal
urx: Decimal
ury: Decimal
@classmethod
def from_pdfarray(cls, arr: PDFArray):
vals: List[Decimal] = []
for obj in arr:
if isinstance(obj, PDFInteger):
vals.append(Decimal(obj.to_python()))
elif isinstance(obj, PDFReal):
vals.append(obj.to_python())
else:
raise BrokenFilePDF(f"Invalid type for initilizing rectangle {obj}")
if len(vals) > 4:
raise TooManyRectField(vals)
elif len(vals) < 4:
raise BrokenFilePDF(
f"Insufficient number of fields for rectangle {arr} {vals}"
)
return cls(*vals)
def to_numpy(self) -> np.ndarray:
"""convert the rectangle to a numpy array
Returns:
np.ndarray[np.float64]
"""
return np.array(
[
[np.float64(self.llx), np.float64(self.lly)],
[np.float64(self.urx), np.float64(self.ury)],
],
dtype=np.float64,
)
+33
View File
@@ -0,0 +1,33 @@
from typing import Iterator
from pdfmajor.pdf_parser.objects import (
ObjectRef,
PDFArray,
PDFObject,
validate_object_or_none,
)
from .exceptions import BrokenFilePDF
def iter_single_ref_as_array_ref(kids: PDFObject) -> Iterator[ObjectRef]:
"""Validate that an object is either a list of refs or a single ref, then iterate over it
Args:
kids (PDFObject)
Raises:
BrokenFile: if the provided object is not a valid PDFObject
Yields:
Iterator[ObjectRef]
"""
if isinstance(kids, ObjectRef):
yield kids
elif isinstance(kids, PDFArray):
for kid in kids:
vetted_kid = validate_object_or_none(kid, ObjectRef)
if vetted_kid:
yield vetted_kid
else:
raise BrokenFilePDF(f"expected a list of refs and got {kids}")
+6
View File
@@ -0,0 +1,6 @@
class PDFMajorException(Exception):
pass
class BrokenFile(PDFMajorException):
pass
+71
View File
@@ -0,0 +1,71 @@
from typing import Any, Dict, Iterator, List, Optional, Type
from .decoders import (
ASCII85Decode,
ASCIIHexDecode,
CCITTFaxDecode,
FlateDecode,
LZWDecode,
RunLengthDecode
)
from .types import FilterPair, PDFFilterDecoder
_filter_type_name_mapping: Dict[str, Optional[Type[PDFFilterDecoder]]] = {
"FlateDecode": FlateDecode,
"Fl": FlateDecode,
"LZWDecode": LZWDecode,
"LZW": LZWDecode,
"ASCII85Decode": ASCII85Decode,
"A85": ASCII85Decode,
"ASCIIHexDecode": ASCIIHexDecode,
"AHx": ASCIIHexDecode,
"RunLengthDecode": RunLengthDecode,
"RL": RunLengthDecode,
"CCITTFaxDecode": CCITTFaxDecode,
"CCF": CCITTFaxDecode,
"DCTDecode": None, # FilterType.DCTDecode,
"DCT": None, # FilterType.DCTDecode,
"Crypt": None, # FilterType.Crypt,
"JBIG2Decode": None, # FilterType.JBIG2Decode,
"JPXDecode": None, # FilterType.JPXDecode,
}
def process_filters_on_data(
data: bytes,
it_filters: Iterator[FilterPair],
) -> bytes:
"""runs the filters provided by the list with the provided params against the data
Args:
data (bytes)
filters (Optional[List[str]])
decode_params (Optional[List[Dict[str, Any]]])
Returns:
bytes
"""
for f, params in it_filters:
decoder = detect_decoder_type(f)
data = decoder.decode(data, **params)
return data
def detect_decoder_type(name: str) -> Type[PDFFilterDecoder]:
"""Check to see if the specified filter exists in our codebase
Args:
name (str)
Raises:
InvalidDecoderOrNotImplemented
Returns:
Type[PDFFilterDecoder]
"""
decoder = _filter_type_name_mapping.get(name)
if not decoder:
raise NotImplementedError(
f"Filter {name} is not implemented yet, feel free to submit a PR and help!"
)
return decoder
+568
View File
@@ -0,0 +1,568 @@
# CCITT Fax decoder
#
# Bugs: uncompressed mode untested.
#
# cf.
# ITU-T Recommendation T.4
# "Standardization of Group 3 facsimile terminals for document transmission"
# ITU-T Recommendation T.6
# "FACSIMILE CODING SCHEMES AND CODING CONTROL FUNCTIONS FOR GROUP 4 FACSIMILE APPARATUS"
import array
from .exceptions import CCITEOFB, CCITByteSkip, CCITInvalidData
def get_bytes(data):
for byte in data:
yield byte
## BitParser
##
class BitParser(object):
def __init__(self):
self._pos = 0
self._state = None
return
@classmethod
def add(cls, root, v, bits):
p = root
b = None
for i in range(len(bits)):
if 0 < i:
if p[b] is None:
p[b] = [None, None]
p = p[b]
if bits[i] == "1":
b = 1
else:
b = 0
p[b] = v
return
def feedbytes(self, data):
for byte in get_bytes(data):
for m in (128, 64, 32, 16, 8, 4, 2, 1):
self._parse_bit(byte & m)
return
def _parse_bit(self, x):
if x:
v = self._state[1]
else:
v = self._state[0]
self._pos += 1
if isinstance(v, list):
self._state = v
else:
self._state = self._accept(v)
return
def _accept(self, v):
raise NotImplementedError
## CCITTG4Parser
##
class CCITTG4Parser(BitParser):
MODE = [None, None]
BitParser.add(MODE, 0, "1")
BitParser.add(MODE, +1, "011")
BitParser.add(MODE, -1, "010")
BitParser.add(MODE, "h", "001")
BitParser.add(MODE, "p", "0001")
BitParser.add(MODE, +2, "000011")
BitParser.add(MODE, -2, "000010")
BitParser.add(MODE, +3, "0000011")
BitParser.add(MODE, -3, "0000010")
BitParser.add(MODE, "u", "0000001111")
BitParser.add(MODE, "x1", "0000001000")
BitParser.add(MODE, "x2", "0000001001")
BitParser.add(MODE, "x3", "0000001010")
BitParser.add(MODE, "x4", "0000001011")
BitParser.add(MODE, "x5", "0000001100")
BitParser.add(MODE, "x6", "0000001101")
BitParser.add(MODE, "x7", "0000001110")
BitParser.add(MODE, "e", "000000000001000000000001")
WHITE = [None, None]
BitParser.add(WHITE, 0, "00110101")
BitParser.add(WHITE, 1, "000111")
BitParser.add(WHITE, 2, "0111")
BitParser.add(WHITE, 3, "1000")
BitParser.add(WHITE, 4, "1011")
BitParser.add(WHITE, 5, "1100")
BitParser.add(WHITE, 6, "1110")
BitParser.add(WHITE, 7, "1111")
BitParser.add(WHITE, 8, "10011")
BitParser.add(WHITE, 9, "10100")
BitParser.add(WHITE, 10, "00111")
BitParser.add(WHITE, 11, "01000")
BitParser.add(WHITE, 12, "001000")
BitParser.add(WHITE, 13, "000011")
BitParser.add(WHITE, 14, "110100")
BitParser.add(WHITE, 15, "110101")
BitParser.add(WHITE, 16, "101010")
BitParser.add(WHITE, 17, "101011")
BitParser.add(WHITE, 18, "0100111")
BitParser.add(WHITE, 19, "0001100")
BitParser.add(WHITE, 20, "0001000")
BitParser.add(WHITE, 21, "0010111")
BitParser.add(WHITE, 22, "0000011")
BitParser.add(WHITE, 23, "0000100")
BitParser.add(WHITE, 24, "0101000")
BitParser.add(WHITE, 25, "0101011")
BitParser.add(WHITE, 26, "0010011")
BitParser.add(WHITE, 27, "0100100")
BitParser.add(WHITE, 28, "0011000")
BitParser.add(WHITE, 29, "00000010")
BitParser.add(WHITE, 30, "00000011")
BitParser.add(WHITE, 31, "00011010")
BitParser.add(WHITE, 32, "00011011")
BitParser.add(WHITE, 33, "00010010")
BitParser.add(WHITE, 34, "00010011")
BitParser.add(WHITE, 35, "00010100")
BitParser.add(WHITE, 36, "00010101")
BitParser.add(WHITE, 37, "00010110")
BitParser.add(WHITE, 38, "00010111")
BitParser.add(WHITE, 39, "00101000")
BitParser.add(WHITE, 40, "00101001")
BitParser.add(WHITE, 41, "00101010")
BitParser.add(WHITE, 42, "00101011")
BitParser.add(WHITE, 43, "00101100")
BitParser.add(WHITE, 44, "00101101")
BitParser.add(WHITE, 45, "00000100")
BitParser.add(WHITE, 46, "00000101")
BitParser.add(WHITE, 47, "00001010")
BitParser.add(WHITE, 48, "00001011")
BitParser.add(WHITE, 49, "01010010")
BitParser.add(WHITE, 50, "01010011")
BitParser.add(WHITE, 51, "01010100")
BitParser.add(WHITE, 52, "01010101")
BitParser.add(WHITE, 53, "00100100")
BitParser.add(WHITE, 54, "00100101")
BitParser.add(WHITE, 55, "01011000")
BitParser.add(WHITE, 56, "01011001")
BitParser.add(WHITE, 57, "01011010")
BitParser.add(WHITE, 58, "01011011")
BitParser.add(WHITE, 59, "01001010")
BitParser.add(WHITE, 60, "01001011")
BitParser.add(WHITE, 61, "00110010")
BitParser.add(WHITE, 62, "00110011")
BitParser.add(WHITE, 63, "00110100")
BitParser.add(WHITE, 64, "11011")
BitParser.add(WHITE, 128, "10010")
BitParser.add(WHITE, 192, "010111")
BitParser.add(WHITE, 256, "0110111")
BitParser.add(WHITE, 320, "00110110")
BitParser.add(WHITE, 384, "00110111")
BitParser.add(WHITE, 448, "01100100")
BitParser.add(WHITE, 512, "01100101")
BitParser.add(WHITE, 576, "01101000")
BitParser.add(WHITE, 640, "01100111")
BitParser.add(WHITE, 704, "011001100")
BitParser.add(WHITE, 768, "011001101")
BitParser.add(WHITE, 832, "011010010")
BitParser.add(WHITE, 896, "011010011")
BitParser.add(WHITE, 960, "011010100")
BitParser.add(WHITE, 1024, "011010101")
BitParser.add(WHITE, 1088, "011010110")
BitParser.add(WHITE, 1152, "011010111")
BitParser.add(WHITE, 1216, "011011000")
BitParser.add(WHITE, 1280, "011011001")
BitParser.add(WHITE, 1344, "011011010")
BitParser.add(WHITE, 1408, "011011011")
BitParser.add(WHITE, 1472, "010011000")
BitParser.add(WHITE, 1536, "010011001")
BitParser.add(WHITE, 1600, "010011010")
BitParser.add(WHITE, 1664, "011000")
BitParser.add(WHITE, 1728, "010011011")
BitParser.add(WHITE, 1792, "00000001000")
BitParser.add(WHITE, 1856, "00000001100")
BitParser.add(WHITE, 1920, "00000001101")
BitParser.add(WHITE, 1984, "000000010010")
BitParser.add(WHITE, 2048, "000000010011")
BitParser.add(WHITE, 2112, "000000010100")
BitParser.add(WHITE, 2176, "000000010101")
BitParser.add(WHITE, 2240, "000000010110")
BitParser.add(WHITE, 2304, "000000010111")
BitParser.add(WHITE, 2368, "000000011100")
BitParser.add(WHITE, 2432, "000000011101")
BitParser.add(WHITE, 2496, "000000011110")
BitParser.add(WHITE, 2560, "000000011111")
BLACK = [None, None]
BitParser.add(BLACK, 0, "0000110111")
BitParser.add(BLACK, 1, "010")
BitParser.add(BLACK, 2, "11")
BitParser.add(BLACK, 3, "10")
BitParser.add(BLACK, 4, "011")
BitParser.add(BLACK, 5, "0011")
BitParser.add(BLACK, 6, "0010")
BitParser.add(BLACK, 7, "00011")
BitParser.add(BLACK, 8, "000101")
BitParser.add(BLACK, 9, "000100")
BitParser.add(BLACK, 10, "0000100")
BitParser.add(BLACK, 11, "0000101")
BitParser.add(BLACK, 12, "0000111")
BitParser.add(BLACK, 13, "00000100")
BitParser.add(BLACK, 14, "00000111")
BitParser.add(BLACK, 15, "000011000")
BitParser.add(BLACK, 16, "0000010111")
BitParser.add(BLACK, 17, "0000011000")
BitParser.add(BLACK, 18, "0000001000")
BitParser.add(BLACK, 19, "00001100111")
BitParser.add(BLACK, 20, "00001101000")
BitParser.add(BLACK, 21, "00001101100")
BitParser.add(BLACK, 22, "00000110111")
BitParser.add(BLACK, 23, "00000101000")
BitParser.add(BLACK, 24, "00000010111")
BitParser.add(BLACK, 25, "00000011000")
BitParser.add(BLACK, 26, "000011001010")
BitParser.add(BLACK, 27, "000011001011")
BitParser.add(BLACK, 28, "000011001100")
BitParser.add(BLACK, 29, "000011001101")
BitParser.add(BLACK, 30, "000001101000")
BitParser.add(BLACK, 31, "000001101001")
BitParser.add(BLACK, 32, "000001101010")
BitParser.add(BLACK, 33, "000001101011")
BitParser.add(BLACK, 34, "000011010010")
BitParser.add(BLACK, 35, "000011010011")
BitParser.add(BLACK, 36, "000011010100")
BitParser.add(BLACK, 37, "000011010101")
BitParser.add(BLACK, 38, "000011010110")
BitParser.add(BLACK, 39, "000011010111")
BitParser.add(BLACK, 40, "000001101100")
BitParser.add(BLACK, 41, "000001101101")
BitParser.add(BLACK, 42, "000011011010")
BitParser.add(BLACK, 43, "000011011011")
BitParser.add(BLACK, 44, "000001010100")
BitParser.add(BLACK, 45, "000001010101")
BitParser.add(BLACK, 46, "000001010110")
BitParser.add(BLACK, 47, "000001010111")
BitParser.add(BLACK, 48, "000001100100")
BitParser.add(BLACK, 49, "000001100101")
BitParser.add(BLACK, 50, "000001010010")
BitParser.add(BLACK, 51, "000001010011")
BitParser.add(BLACK, 52, "000000100100")
BitParser.add(BLACK, 53, "000000110111")
BitParser.add(BLACK, 54, "000000111000")
BitParser.add(BLACK, 55, "000000100111")
BitParser.add(BLACK, 56, "000000101000")
BitParser.add(BLACK, 57, "000001011000")
BitParser.add(BLACK, 58, "000001011001")
BitParser.add(BLACK, 59, "000000101011")
BitParser.add(BLACK, 60, "000000101100")
BitParser.add(BLACK, 61, "000001011010")
BitParser.add(BLACK, 62, "000001100110")
BitParser.add(BLACK, 63, "000001100111")
BitParser.add(BLACK, 64, "0000001111")
BitParser.add(BLACK, 128, "000011001000")
BitParser.add(BLACK, 192, "000011001001")
BitParser.add(BLACK, 256, "000001011011")
BitParser.add(BLACK, 320, "000000110011")
BitParser.add(BLACK, 384, "000000110100")
BitParser.add(BLACK, 448, "000000110101")
BitParser.add(BLACK, 512, "0000001101100")
BitParser.add(BLACK, 576, "0000001101101")
BitParser.add(BLACK, 640, "0000001001010")
BitParser.add(BLACK, 704, "0000001001011")
BitParser.add(BLACK, 768, "0000001001100")
BitParser.add(BLACK, 832, "0000001001101")
BitParser.add(BLACK, 896, "0000001110010")
BitParser.add(BLACK, 960, "0000001110011")
BitParser.add(BLACK, 1024, "0000001110100")
BitParser.add(BLACK, 1088, "0000001110101")
BitParser.add(BLACK, 1152, "0000001110110")
BitParser.add(BLACK, 1216, "0000001110111")
BitParser.add(BLACK, 1280, "0000001010010")
BitParser.add(BLACK, 1344, "0000001010011")
BitParser.add(BLACK, 1408, "0000001010100")
BitParser.add(BLACK, 1472, "0000001010101")
BitParser.add(BLACK, 1536, "0000001011010")
BitParser.add(BLACK, 1600, "0000001011011")
BitParser.add(BLACK, 1664, "0000001100100")
BitParser.add(BLACK, 1728, "0000001100101")
BitParser.add(BLACK, 1792, "00000001000")
BitParser.add(BLACK, 1856, "00000001100")
BitParser.add(BLACK, 1920, "00000001101")
BitParser.add(BLACK, 1984, "000000010010")
BitParser.add(BLACK, 2048, "000000010011")
BitParser.add(BLACK, 2112, "000000010100")
BitParser.add(BLACK, 2176, "000000010101")
BitParser.add(BLACK, 2240, "000000010110")
BitParser.add(BLACK, 2304, "000000010111")
BitParser.add(BLACK, 2368, "000000011100")
BitParser.add(BLACK, 2432, "000000011101")
BitParser.add(BLACK, 2496, "000000011110")
BitParser.add(BLACK, 2560, "000000011111")
UNCOMPRESSED = [None, None]
BitParser.add(UNCOMPRESSED, "1", "1")
BitParser.add(UNCOMPRESSED, "01", "01")
BitParser.add(UNCOMPRESSED, "001", "001")
BitParser.add(UNCOMPRESSED, "0001", "0001")
BitParser.add(UNCOMPRESSED, "00001", "00001")
BitParser.add(UNCOMPRESSED, "00000", "000001")
BitParser.add(UNCOMPRESSED, "T00", "00000011")
BitParser.add(UNCOMPRESSED, "T10", "00000010")
BitParser.add(UNCOMPRESSED, "T000", "000000011")
BitParser.add(UNCOMPRESSED, "T100", "000000010")
BitParser.add(UNCOMPRESSED, "T0000", "0000000011")
BitParser.add(UNCOMPRESSED, "T1000", "0000000010")
BitParser.add(UNCOMPRESSED, "T00000", "00000000011")
BitParser.add(UNCOMPRESSED, "T10000", "00000000010")
def __init__(self, width, bytealign=False):
BitParser.__init__(self)
self.width = width
self.bytealign = bytealign
self.reset()
return
def feedbytes(self, data):
for byte in get_bytes(data):
try:
for m in (128, 64, 32, 16, 8, 4, 2, 1):
self._parse_bit(byte & m)
except CCITByteSkip:
self._accept = self._parse_mode
self._state = self.MODE
except CCITEOFB:
break
return
def _parse_mode(self, mode: str):
if mode == "p":
self._do_pass()
self._flush_line()
return self.MODE
elif mode == "h":
self._n1 = 0
self._accept = self._parse_horiz1 # type: ignore
if self._color:
return self.WHITE
else:
return self.BLACK
elif mode == "u":
self._accept = self._parse_uncompressed # type: ignore
return self.UNCOMPRESSED
elif mode == "e":
raise CCITEOFB
elif isinstance(mode, int):
self._do_vertical(mode)
self._flush_line()
return self.MODE
else:
raise CCITInvalidData(mode)
def _parse_horiz1(self, n):
if n is None:
raise CCITInvalidData
self._n1 += n
if n < 64:
self._n2 = 0
self._color = 1 - self._color
self._accept = self._parse_horiz2
if self._color:
return self.WHITE
else:
return self.BLACK
def _parse_horiz2(self, n):
if n is None:
raise CCITInvalidData
self._n2 += n
if n < 64:
self._color = 1 - self._color
self._accept = self._parse_mode
self._do_horizontal(self._n1, self._n2)
self._flush_line()
return self.MODE
elif self._color:
return self.WHITE
else:
return self.BLACK
def _parse_uncompressed(self, bits):
if not bits:
raise CCITInvalidData
if bits.startswith("T"):
self._accept = self._parse_mode
self._color = int(bits[1])
self._do_uncompressed(bits[2:])
return self.MODE
else:
self._do_uncompressed(bits)
return self.UNCOMPRESSED
def _get_bits(self):
return "".join(str(b) for b in self._curline[: self._curpos])
def _get_refline(self, i):
if i < 0:
return "[]" + "".join(str(b) for b in self._refline)
elif len(self._refline) <= i:
return "".join(str(b) for b in self._refline) + "[]"
else:
return (
"".join(str(b) for b in self._refline[:i])
+ "["
+ str(self._refline[i])
+ "]"
+ "".join(str(b) for b in self._refline[i + 1 :])
)
def reset(self):
self._y = 0
self._curline = array.array("b", [1] * self.width)
self._reset_line()
self._accept = self._parse_mode
self._state = self.MODE
return
def output_line(self, y, bits):
print(y, "".join(str(b) for b in bits))
return
def _reset_line(self):
self._refline = self._curline
self._curline = array.array("b", [1] * self.width)
self._curpos = -1
self._color = 1
return
def _flush_line(self):
if self.width <= self._curpos:
self.output_line(self._y, self._curline)
self._y += 1
self._reset_line()
if self.bytealign:
raise CCITByteSkip
return
def _do_vertical(self, dx):
x1 = self._curpos + 1
while 1:
if x1 == 0:
if self._color == 1 and self._refline[x1] != self._color:
break
elif x1 == len(self._refline):
break
elif (
self._refline[x1 - 1] == self._color
and self._refline[x1] != self._color
):
break
x1 += 1
x1 += dx
x0 = max(0, self._curpos)
x1 = max(0, min(self.width, x1))
if x1 < x0:
for x in range(x1, x0):
self._curline[x] = self._color
elif x0 < x1:
for x in range(x0, x1):
self._curline[x] = self._color
self._curpos = x1
self._color = 1 - self._color
return
def _do_pass(self):
x1 = self._curpos + 1
while 1:
if x1 == 0:
if self._color == 1 and self._refline[x1] != self._color:
break
elif x1 == len(self._refline):
break
elif (
self._refline[x1 - 1] == self._color
and self._refline[x1] != self._color
):
break
x1 += 1
while 1:
if x1 == 0:
if self._color == 0 and self._refline[x1] == self._color:
break
elif x1 == len(self._refline):
break
elif (
self._refline[x1 - 1] != self._color
and self._refline[x1] == self._color
):
break
x1 += 1
for x in range(self._curpos, x1):
self._curline[x] = self._color
self._curpos = x1
return
def _do_horizontal(self, n1, n2):
if self._curpos < 0:
self._curpos = 0
x = self._curpos
for _ in range(n1):
if len(self._curline) <= x:
break
self._curline[x] = self._color
x += 1
for _ in range(n2):
if len(self._curline) <= x:
break
self._curline[x] = 1 - self._color
x += 1
self._curpos = x
return
def _do_uncompressed(self, bits):
for c in bits:
self._curline[self._curpos] = int(c)
self._curpos += 1
self._flush_line()
return
class CCITTFaxDecoder(CCITTG4Parser):
def __init__(self, width, bytealign=False, reversed=False):
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
self.reversed = reversed
self._buf: bytes = b""
return
def close(self) -> bytes:
return self._buf
def output_line(self, y, bits):
bytes = array.array("B", [0] * ((len(bits) + 7) // 8))
if self.reversed:
bits = [1 - b for b in bits]
for (i, b) in enumerate(bits):
if b:
bytes[i // 8] += (128, 64, 32, 16, 8, 4, 2, 1)[i % 8]
self._buf += bytes.tostring()
return
def ccittfaxdecode(
data: bytes,
# arguments for algo, with defaults based on PDF spec 1.7 section 7.4.6
Columns: int = 1728,
EncodedByteAlign: bool = False,
BlackIs1: bool = False,
K: int = 0,
EndOfLine: bool = False,
Rows: int = 0,
DamagedRowsBeforeError: int = 0,
) -> bytes:
if K == -1:
parser = CCITTFaxDecoder(Columns, bytealign=EncodedByteAlign, reversed=BlackIs1)
else:
raise ValueError(K)
parser.feedbytes(data)
return parser.close()
+48
View File
@@ -0,0 +1,48 @@
import zlib
from base64 import a85decode
from binascii import a2b_hex
from typing import Any
from .ccit import ccittfaxdecode
from .lzw import lzwdecode
from .rld import rldecode
from .types import PDFFilterDecoder
class ASCIIHexDecode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
clean_data = b"".join(data.split(b" "))
if len(clean_data) & 2 != 0:
clean_data += b"0"
return a2b_hex(clean_data)
class ASCII85Decode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
return a85decode(data, adobe=True)
class LZWDecode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
return lzwdecode(data)
class FlateDecode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
return zlib.decompress(data.strip(b"\r\n"))
class RunLengthDecode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
return rldecode(data)
class CCITTFaxDecode(PDFFilterDecoder):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
return ccittfaxdecode(data, **decode_params)
+25
View File
@@ -0,0 +1,25 @@
from pdfmajor.exceptions import BrokenFile
class FilterError(BrokenFile):
pass
class DecodeFailed(FilterError):
pass
class InvalidDecoderOrNotImplemented(DecodeFailed, NotImplementedError):
pass
class CCITEOFB(DecodeFailed, EOFError):
pass
class CCITInvalidData(DecodeFailed):
pass
class CCITByteSkip(DecodeFailed):
pass
@@ -1,43 +1,38 @@
from io import BytesIO
from ..utils import int2byte
from typing import List, Optional
import logging
class CorruptDataError(Exception):
pass
from .exceptions import DecodeFailed
## LZWDecoder
##
class LZWDecoder(object):
def __init__(self, fp):
def __init__(self, fp: BytesIO):
self.fp = fp
self.buff = 0
self.bpos = 8
self.nbits = 9
self.table = None
self.prevbuf = None
self.buff: int = 0
self.bpos: int = 8
self.nbits: int = 9
self.table: Optional[List[Optional[bytes]]] = None
self.prevbuf: Optional[bytes] = None
return
def readbits(self, bits):
v = 0
while 1:
# the number of remaining bits we can get from the current buffer.
r = 8-self.bpos
r = 8 - self.bpos
if bits <= r:
# |-----8-bits-----|
# |-bpos-|-bits-| |
# | |----r----|
v = (v << bits) | ((self.buff >> (r-bits)) & ((1 << bits)-1))
v = (v << bits) | ((self.buff >> (r - bits)) & ((1 << bits) - 1))
self.bpos += bits
break
else:
# |-----8-bits-----|
# |-bpos-|---bits----...
# | |----r----|
v = (v << r) | (self.buff & ((1 << r)-1))
v = (v << r) | (self.buff & ((1 << r) - 1))
bits -= r
x = self.fp.read(1)
if not x:
@@ -47,27 +42,27 @@ class LZWDecoder(object):
return v
def feed(self, code):
x = b''
x = b""
if code == 256:
self.table = [int2byte(c) for c in range(256)] # 0-255
self.table = [bytes([c]) for c in range(256)] # 0-255
self.table.append(None) # 256
self.table.append(None) # 257
self.prevbuf = b''
self.prevbuf = b""
self.nbits = 9
elif code == 257:
pass
elif not self.prevbuf:
x = self.prevbuf = self.table[code]
else:
if code < len(self.table):
if code < len(self.table): # type: ignore
x = self.table[code]
self.table.append(self.prevbuf+x[:1])
elif code == len(self.table):
self.table.append(self.prevbuf+self.prevbuf[:1])
self.table.append(self.prevbuf + x[:1])
elif code == len(self.table): # type: ignore
self.table.append(self.prevbuf + self.prevbuf[:1])
x = self.table[code]
else:
raise CorruptDataError
l = len(self.table)
raise DecodeFailed("LZW")
l = len(self.table) # type: ignore
if l == 511:
self.nbits = 10
elif l == 1023:
@@ -81,21 +76,15 @@ class LZWDecoder(object):
while 1:
try:
code = self.readbits(self.nbits)
x = self.feed(code)
except EOFError:
break
try:
x = self.feed(code)
except CorruptDataError:
# just ignore corrupt data and stop yielding there
break
yield x
logging.debug('nbits=%d, code=%d, output=%r, table=%r' %
(self.nbits, code, x, self.table[258:]))
return
# lzwdecode
def lzwdecode(data):
def lzwdecode(data: bytes) -> bytes:
fp = BytesIO(data)
s=LZWDecoder(fp).run()
return b''.join(s)
s = LZWDecoder(fp).run()
return b"".join(s) # type: ignore
+36
View File
@@ -0,0 +1,36 @@
#
# RunLength decoder (Adobe version) implementation based on PDF Reference
# version 1.4 section 3.3.4.
#
# * public domain *
#
def rldecode(data: bytes) -> bytes:
"""
RunLength decoder (Adobe version) implementation based on PDF Reference
version 1.4 section 3.3.4:
The RunLengthDecode filter decodes data that has been encoded in a
simple byte-oriented format based on run length. The encoded data
is a sequence of runs, where each run consists of a length byte
followed by 1 to 128 bytes of data. If the length byte is in the
range 0 to 127, the following length + 1 (1 to 128) bytes are
copied literally during decompression. If length is in the range
129 to 255, the following single byte is to be copied 257 - length
(2 to 128) times during decompression. A length value of 128
denotes EOD.
"""
decoded = b""
i: int = 0
while i < len(data):
length = data[i]
if length == 128:
break
if length >= 0 and length < 128:
for j in range(i + 1, (i + 1) + (length + 1)):
decoded += bytes([data[j]])
i = (i + 1) + (length + 1)
if length > 128:
run: bytes = bytes([data[i + 1]]) * (257 - length)
decoded += run
i = (i + 1) + 1
return decoded
+13
View File
@@ -0,0 +1,13 @@
from abc import ABCMeta
from typing import Any, Dict, Tuple
from .exceptions import InvalidDecoderOrNotImplemented
class PDFFilterDecoder(metaclass=ABCMeta):
@staticmethod
def decode(data: bytes, **decode_params: Any) -> bytes:
raise InvalidDecoderOrNotImplemented
FilterPair = Tuple[str, Dict[str, Any]]
+30
View File
@@ -0,0 +1,30 @@
from pdfmajor.fonts.exceptions import InvalidFont
from typing import Any, Dict
from .base import PDFFont
from .type3 import PDFType3Font
from .simple_fonts import PDFSimpleFont
from .cidfont import PDFCIDFont
from .type0 import PDFType0Font
from . import standard14
def make_font(spec: Dict[str, Any]) -> PDFFont:
subtype = str(spec["Subtype"])
if subtype in ("Type1", "MMType1", "TrueType"):
return PDFSimpleFont.from_dict(spec)
elif subtype == "Type3":
return PDFType3Font.from_dict(spec)
elif subtype in ("CIDFontType0", "CIDFontType2"):
return PDFCIDFont.from_dict(spec)
elif subtype == "Type0":
return PDFType0Font.from_dict(spec)
# Type0 Font
# dfonts = list_value(spec["DescendantFonts"])
# assert dfonts
# subspec = dict_value(dfonts[0]).copy()
# for k in ("Encoding", "ToUnicode"):
# if k in spec:
# subspec[k] = resolve1(spec[k])
# font = get_font(None, subspec, cached_fonts)
else:
raise InvalidFont(subtype)
+17
View File
@@ -0,0 +1,17 @@
from abc import ABCMeta, abstractclassmethod
from typing import Any, Dict
class PDFFont(metaclass=ABCMeta):
"""an object reprsenting a PDF Font as decribed in PDF Spec 1.7 section 9.5"""
def __init__(self, subtype: str, **kwrg: Any) -> None:
self.subtype = subtype
self.raw = kwrg
def __repr__(self) -> str:
return f"PDFFont:{self.subtype}({self.raw})"
@abstractclassmethod
def from_dict(cls, d: Dict[str, Any]) -> "PDFFont":
raise NotImplementedError
+12
View File
@@ -0,0 +1,12 @@
from typing import Any, Dict
from .base import PDFFont
class PDFCIDFont(PDFFont):
"""PDF Type 3 object see PDF spec 1.7 section 9.7.2
TODO: finish this
"""
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "PDFCIDFont":
return cls("Type3", **d)
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 41139 52164", "FontName": "Courier-Bold", "FullName": "Courier Bold", "FamilyName": "Courier", "Weight": "Bold", "ItalicAngle": 0, "IsFixedPitch": true, "CharacterSet": "ExtendedRoman", "FontBBox": [-113, -250, 749, 801], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 3.0, "Notice": "Copyright (c) 1989, 1990, 1991, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 562, "XHeight": 439, "Ascender": 629, "Descender": -157, "StdHW": 84, "StdVW": 106}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 17529 79244", "FontName": "Courier-BoldOblique", "FullName": "Courier Bold Oblique", "FamilyName": "Courier", "Weight": "Bold", "ItalicAngle": -12, "IsFixedPitch": true, "CharacterSet": "ExtendedRoman", "FontBBox": [-57, -250, 869, 801], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 3.0, "Notice": "Copyright (c) 1989, 1990, 1991, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 562, "XHeight": 439, "Ascender": 629, "Descender": -157, "StdHW": 84, "StdVW": 106}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 16248 75829", "FontName": "Courier-Oblique", "FullName": "Courier Oblique", "FamilyName": "Courier", "Weight": "Medium", "ItalicAngle": -12, "IsFixedPitch": true, "CharacterSet": "ExtendedRoman", "FontBBox": [-27, -250, 849, 805], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 3.0, "Notice": "Copyright (c) 1989, 1990, 1991, 1992, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 562, "XHeight": 426, "Ascender": 629, "Descender": -157, "StdHW": 51, "StdVW": 51}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 39754 50779", "FontName": "Courier", "FullName": "Courier", "FamilyName": "Courier", "Weight": "Medium", "ItalicAngle": 0, "IsFixedPitch": true, "CharacterSet": "ExtendedRoman", "FontBBox": [-23, -250, 715, 805], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 3.0, "Notice": "Copyright (c) 1989, 1990, 1991, 1992, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 562, "XHeight": 426, "Ascender": 629, "Descender": -157, "StdHW": 51, "StdVW": 51}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 37169 48194", "FontName": "Helvetica-Bold", "FullName": "Helvetica Bold", "FamilyName": "Helvetica", "Weight": "Bold", "ItalicAngle": 0, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-170, -228, 1003, 962], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1997 Adobe Systems Incorporated. All Rights Reserved.Helvetica is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 718, "XHeight": 532, "Ascender": 718, "Descender": -207, "StdHW": 118, "StdVW": 140}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [278, 333, 474, 556, 556, 889, 722, 278, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611, 975, 722, 722, 722, 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 333, 278, 333, 584, 556, 278, 556, 611, 556, 611, 556, 333, 611, 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584, 333, 556, 556, 167, 556, 556, 556, 556, 238, 500, 556, 333, 333, 611, 611, 556, 556, 556, 278, 556, 350, 278, 500, 500, 556, 1000, 1000, 611, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 1000, 370, 611, 778, 1000, 365, 889, 278, 278, 611, 944, 611, 278, 556, 556, 611, 556, 667, 584, 667, 722, 556, 722, 556, 556, 556, 722, 722, 556, 722, 611, 667, 722, 250, 737, 667, 556, 556, 722, 278, 556, 611, 722, 556, 667, 556, 556, 278, 494, 722, 778, 611, 556, 722, 389, 556, 611, 667, 778, 722, 667, 743, 722, 611, 333, 778, 722, 722, 584, 611, 611, 494, 556, 722, 278, 667, 556, 556, 556, 611, 611, 722, 278, 584, 280, 737, 778, 278, 600, 667, 389, 611, 611, 611, 549, 722, 722, 278, 389, 556, 722, 722, 722, 556, 500, 278, 778, 611, 556, 556, 278, 778, 722, 612, 611, 333, 778, 611, 278, 611, 667, 611, 834, 667, 400, 722, 611, 1000, 556, 278, 278, 611, 834, 549, 611, 611, 722, 667, 556, 611, 834, 667, 667, 778, 400, 611, 722, 611, 549, 722, 389, 722, 611, 722, 611, 722, 722, 722, 778, 500, 667, 278, 556, 584, 278, 611, 333, 584, 611, 611, 549, 611, 611, 500, 611, 333, 278, 556]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 14482 68586", "FontName": "Helvetica-BoldOblique", "FullName": "Helvetica Bold Oblique", "FamilyName": "Helvetica", "Weight": "Bold", "ItalicAngle": -12, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-174, -228, 1114, 962], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1997 Adobe Systems Incorporated. All Rights Reserved.Helvetica is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 718, "XHeight": 532, "Ascender": 718, "Descender": -207, "StdHW": 118, "StdVW": 140}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [278, 333, 474, 556, 556, 889, 722, 278, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611, 975, 722, 722, 722, 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 333, 278, 333, 584, 556, 278, 556, 611, 556, 611, 556, 333, 611, 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584, 333, 556, 556, 167, 556, 556, 556, 556, 238, 500, 556, 333, 333, 611, 611, 556, 556, 556, 278, 556, 350, 278, 500, 500, 556, 1000, 1000, 611, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 1000, 370, 611, 778, 1000, 365, 889, 278, 278, 611, 944, 611, 278, 556, 556, 611, 556, 667, 584, 667, 722, 556, 722, 556, 556, 556, 722, 722, 556, 722, 611, 667, 722, 250, 737, 667, 556, 556, 722, 278, 556, 611, 722, 556, 667, 556, 556, 278, 494, 722, 778, 611, 556, 722, 389, 556, 611, 667, 778, 722, 667, 743, 722, 611, 333, 778, 722, 722, 584, 611, 611, 494, 556, 722, 278, 667, 556, 556, 556, 611, 611, 722, 278, 584, 280, 737, 778, 278, 600, 667, 389, 611, 611, 611, 549, 722, 722, 278, 389, 556, 722, 722, 722, 556, 500, 278, 778, 611, 556, 556, 278, 778, 722, 612, 611, 333, 778, 611, 278, 611, 667, 611, 834, 667, 400, 722, 611, 1000, 556, 278, 278, 611, 834, 549, 611, 611, 722, 667, 556, 611, 834, 667, 667, 778, 400, 611, 722, 611, 549, 722, 389, 722, 611, 722, 611, 722, 722, 722, 778, 500, 667, 278, 556, 584, 278, 611, 333, 584, 611, 611, 549, 611, 611, 500, 611, 333, 278, 556]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 14960 69346", "FontName": "Helvetica-Oblique", "FullName": "Helvetica Oblique", "FamilyName": "Helvetica", "Weight": "Medium", "ItalicAngle": -12, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-170, -225, 1116, 931], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1997 Adobe Systems Incorporated. All Rights Reserved.Helvetica is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 718, "XHeight": 523, "Ascender": 718, "Descender": -207, "StdHW": 76, "StdVW": 88}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [278, 278, 355, 556, 556, 889, 667, 222, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 222, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, 333, 556, 556, 167, 556, 556, 556, 556, 191, 333, 556, 333, 333, 500, 500, 556, 556, 556, 278, 537, 350, 222, 333, 333, 556, 1000, 1000, 611, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 1000, 370, 556, 778, 1000, 365, 889, 278, 222, 611, 944, 611, 278, 556, 556, 556, 556, 667, 584, 667, 667, 556, 722, 500, 500, 556, 722, 722, 556, 722, 556, 667, 722, 250, 737, 667, 500, 556, 722, 222, 556, 611, 722, 556, 667, 500, 500, 278, 471, 722, 778, 556, 556, 667, 333, 500, 611, 667, 778, 722, 667, 643, 722, 556, 333, 778, 667, 667, 584, 556, 611, 476, 500, 722, 278, 667, 556, 556, 500, 556, 556, 722, 278, 584, 260, 737, 778, 278, 600, 667, 333, 556, 611, 611, 549, 722, 722, 222, 317, 556, 722, 667, 667, 556, 500, 222, 778, 556, 556, 500, 278, 778, 722, 612, 556, 333, 778, 556, 278, 556, 667, 556, 834, 667, 299, 667, 556, 1000, 556, 278, 278, 556, 834, 549, 556, 556, 722, 667, 556, 556, 834, 667, 667, 778, 400, 556, 722, 556, 453, 722, 333, 722, 556, 722, 556, 667, 667, 667, 778, 500, 667, 278, 500, 584, 278, 556, 278, 584, 556, 556, 549, 556, 556, 500, 556, 333, 278, 556]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 37069 48094", "FontName": "Helvetica", "FullName": "Helvetica", "FamilyName": "Helvetica", "Weight": "Medium", "ItalicAngle": 0, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-166, -225, 1000, 931], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1997 Adobe Systems Incorporated. All Rights Reserved.Helvetica is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 718, "XHeight": 523, "Ascender": 718, "Descender": -207, "StdHW": 76, "StdVW": 88}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [278, 278, 355, 556, 556, 889, 667, 222, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 222, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, 333, 556, 556, 167, 556, 556, 556, 556, 191, 333, 556, 333, 333, 500, 500, 556, 556, 556, 278, 537, 350, 222, 333, 333, 556, 1000, 1000, 611, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 1000, 370, 556, 778, 1000, 365, 889, 278, 222, 611, 944, 611, 278, 556, 556, 556, 556, 667, 584, 667, 667, 556, 722, 500, 500, 556, 722, 722, 556, 722, 556, 667, 722, 250, 737, 667, 500, 556, 722, 222, 556, 611, 722, 556, 667, 500, 500, 278, 471, 722, 778, 556, 556, 667, 333, 500, 611, 667, 778, 722, 667, 643, 722, 556, 333, 778, 667, 667, 584, 556, 611, 476, 500, 722, 278, 667, 556, 556, 500, 556, 556, 722, 278, 584, 260, 737, 778, 278, 600, 667, 333, 556, 611, 611, 549, 722, 722, 222, 317, 556, 722, 667, 667, 556, 500, 222, 778, 556, 556, 500, 278, 778, 722, 612, 556, 333, 778, 556, 278, 556, 667, 556, 834, 667, 299, 667, 556, 1000, 556, 278, 278, 556, 834, 549, 556, 556, 722, 667, 556, 556, 834, 667, 667, 778, 400, 556, 722, 556, 453, 722, 333, 722, 556, 722, 556, 667, 667, 667, 778, 500, 667, 278, 500, 584, 278, 556, 278, 584, 556, 556, 549, 556, 556, 500, 556, 333, 278, 556]}}
+1
View File
@@ -0,0 +1 @@
{"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}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, -1], "widths": [250, 333, 713, 500, 549, 833, 778, 439, 333, 333, 500, 549, 250, 549, 250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 278, 278, 549, 549, 549, 444, 549, 722, 667, 722, 612, 611, 763, 603, 722, 333, 631, 722, 686, 889, 722, 722, 768, 741, 556, 592, 611, 690, 439, 768, 645, 795, 611, 333, 863, 333, 658, 500, 500, 631, 549, 549, 494, 439, 521, 411, 603, 329, 603, 549, 549, 576, 521, 549, 549, 521, 549, 603, 439, 576, 713, 686, 493, 686, 494, 480, 200, 480, 549, 750, 620, 247, 549, 167, 713, 500, 753, 753, 753, 753, 1042, 987, 603, 987, 603, 400, 549, 411, 549, 549, 713, 494, 460, 549, 549, 549, 549, 1000, 603, 1000, 658, 823, 686, 795, 987, 768, 768, 823, 768, 768, 713, 713, 713, 713, 713, 713, 713, 768, 713, 790, 790, 890, 823, 549, 250, 713, 603, 603, 1042, 987, 603, 987, 603, 494, 329, 790, 790, 786, 713, 384, 384, 384, 384, 384, 384, 494, 494, 494, 494, 329, 274, 686, 686, 686, 384, 384, 384, 384, 384, 384, 494, 494, 494, 790]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 41636 52661", "FontName": "Times-Bold", "FullName": "Times Bold", "FamilyName": "Times", "Weight": "Bold", "ItalicAngle": 0, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-168, -218, 1000, 935], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.Times is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 676, "XHeight": 461, "Ascender": 683, "Descender": -217, "StdHW": 44, "StdVW": 139}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [250, 333, 555, 500, 500, 1000, 833, 333, 333, 333, 500, 570, 250, 333, 250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333, 570, 570, 570, 500, 930, 722, 667, 722, 722, 667, 611, 778, 778, 389, 500, 778, 667, 944, 722, 778, 611, 778, 722, 556, 667, 722, 722, 1000, 722, 722, 667, 333, 278, 333, 581, 500, 333, 500, 556, 444, 556, 444, 333, 500, 556, 278, 333, 556, 278, 833, 556, 500, 556, 556, 444, 389, 333, 556, 500, 722, 500, 500, 444, 394, 220, 394, 520, 333, 500, 500, 167, 500, 500, 500, 500, 278, 500, 500, 333, 333, 556, 556, 500, 500, 500, 250, 540, 350, 333, 500, 500, 500, 1000, 1000, 500, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 1000, 300, 667, 778, 1000, 330, 722, 278, 278, 500, 722, 556, 389, 444, 500, 556, 444, 722, 570, 722, 722, 500, 722, 500, 389, 444, 722, 722, 500, 722, 556, 667, 722, 250, 747, 667, 444, 500, 722, 278, 500, 667, 722, 500, 667, 389, 389, 278, 494, 722, 778, 556, 500, 722, 444, 444, 667, 611, 778, 722, 556, 672, 722, 556, 300, 778, 722, 722, 570, 556, 667, 494, 500, 722, 278, 667, 500, 444, 444, 556, 556, 722, 389, 570, 220, 747, 778, 389, 600, 667, 444, 500, 667, 667, 549, 722, 722, 278, 416, 444, 722, 722, 722, 444, 444, 278, 778, 500, 500, 389, 278, 778, 722, 612, 556, 300, 778, 556, 278, 500, 667, 556, 750, 556, 394, 778, 667, 1000, 444, 389, 389, 667, 750, 549, 500, 556, 722, 667, 444, 500, 750, 556, 556, 778, 400, 500, 722, 556, 549, 722, 444, 722, 500, 722, 667, 722, 722, 722, 778, 444, 667, 389, 556, 570, 389, 556, 333, 570, 500, 556, 549, 500, 500, 444, 556, 300, 278, 500]}}
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 45874 56899", "FontName": "Times-BoldItalic", "FullName": "Times Bold Italic", "FamilyName": "Times", "Weight": "Bold", "ItalicAngle": -15, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-200, -218, 996, 921], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.Times is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 669, "XHeight": 462, "Ascender": 683, "Descender": -217, "StdHW": 42, "StdVW": 121}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [250, 389, 555, 500, 500, 833, 778, 333, 333, 333, 500, 570, 250, 333, 250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333, 570, 570, 570, 500, 832, 667, 667, 667, 722, 667, 667, 722, 778, 389, 500, 667, 611, 889, 722, 722, 611, 722, 667, 556, 611, 722, 667, 889, 667, 611, 611, 333, 278, 333, 570, 500, 333, 500, 500, 444, 500, 444, 333, 500, 556, 278, 278, 500, 278, 778, 556, 500, 500, 500, 389, 389, 278, 556, 444, 667, 500, 444, 389, 348, 220, 348, 570, 389, 500, 500, 167, 500, 500, 500, 500, 278, 500, 500, 333, 333, 556, 556, 500, 500, 500, 250, 500, 350, 333, 500, 500, 500, 1000, 1000, 500, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 944, 266, 611, 722, 944, 300, 722, 278, 278, 500, 722, 500, 389, 444, 500, 556, 444, 611, 570, 611, 667, 500, 722, 444, 389, 444, 722, 722, 500, 722, 556, 667, 722, 250, 747, 667, 444, 500, 722, 278, 500, 611, 667, 500, 667, 389, 389, 278, 494, 667, 722, 556, 500, 667, 389, 444, 611, 611, 722, 667, 556, 608, 722, 556, 300, 722, 667, 667, 570, 556, 611, 494, 444, 722, 278, 667, 500, 444, 444, 556, 556, 722, 389, 570, 220, 747, 722, 389, 600, 667, 389, 500, 611, 611, 549, 722, 667, 278, 366, 444, 722, 667, 667, 444, 389, 278, 722, 500, 500, 389, 278, 722, 722, 612, 500, 300, 722, 576, 278, 500, 667, 500, 750, 556, 382, 667, 611, 1000, 444, 389, 389, 611, 750, 549, 500, 556, 722, 667, 444, 500, 750, 556, 556, 722, 400, 500, 667, 556, 549, 722, 389, 722, 500, 667, 611, 667, 667, 667, 722, 389, 667, 389, 500, 606, 389, 556, 278, 606, 500, 556, 549, 500, 500, 389, 556, 300, 278, 500]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 47727 58752", "FontName": "Times-Italic", "FullName": "Times Italic", "FamilyName": "Times", "Weight": "Medium", "ItalicAngle": -15.5, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-169, -217, 1010, 883], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.Times is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 653, "XHeight": 441, "Ascender": 683, "Descender": -217, "StdHW": 32, "StdVW": 76}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [250, 333, 420, 500, 500, 833, 778, 333, 333, 333, 500, 675, 250, 333, 250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333, 675, 675, 675, 500, 920, 611, 611, 667, 722, 611, 611, 722, 722, 333, 444, 667, 556, 833, 667, 722, 611, 722, 611, 500, 556, 722, 611, 833, 611, 556, 556, 389, 278, 389, 422, 500, 333, 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, 389, 400, 275, 400, 541, 389, 500, 500, 167, 500, 500, 500, 500, 214, 556, 500, 333, 333, 500, 500, 500, 500, 500, 250, 523, 350, 333, 556, 556, 500, 889, 1000, 500, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 889, 889, 276, 556, 722, 944, 310, 667, 278, 278, 500, 667, 500, 333, 444, 500, 500, 444, 556, 675, 556, 611, 500, 722, 444, 389, 444, 722, 722, 500, 722, 500, 611, 722, 250, 760, 611, 444, 500, 667, 278, 500, 556, 667, 500, 611, 389, 389, 278, 471, 611, 722, 500, 500, 611, 389, 444, 556, 611, 722, 611, 500, 544, 722, 500, 300, 722, 611, 611, 675, 500, 556, 476, 444, 667, 278, 611, 500, 444, 444, 500, 500, 667, 333, 675, 275, 760, 722, 333, 600, 611, 389, 500, 556, 556, 549, 722, 667, 278, 300, 444, 722, 611, 611, 444, 389, 278, 722, 500, 500, 389, 278, 722, 722, 612, 500, 300, 722, 500, 278, 500, 611, 500, 750, 500, 300, 667, 556, 980, 444, 333, 333, 611, 750, 549, 500, 500, 722, 611, 444, 500, 750, 500, 500, 722, 400, 500, 667, 500, 453, 722, 389, 667, 500, 611, 556, 611, 611, 611, 722, 389, 611, 333, 444, 675, 333, 500, 278, 675, 500, 500, 549, 500, 500, 389, 500, 300, 278, 500]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 43909 54934", "FontName": "Times-Roman", "FullName": "Times Roman", "FamilyName": "Times", "Weight": "Roman", "ItalicAngle": 0, "IsFixedPitch": false, "CharacterSet": "ExtendedRoman", "FontBBox": [-168, -218, 1000, 898], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1989, 1990, 1993, 1997 Adobe Systems Incorporated. All Rights Reserved.Times is a trademark of Linotype-Hell AG and/or its subsidiaries.", "EncodingScheme": "AdobeStandardEncoding", "CapHeight": 662, "XHeight": 450, "Ascender": 683, "Descender": -217, "StdHW": 28, "StdVW": 84}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 189, 191, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 205, 206, 207, 208, 225, 227, 232, 233, 234, 235, 241, 245, 248, 249, 250, 251, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "widths": [250, 333, 408, 500, 500, 833, 778, 333, 333, 333, 500, 564, 250, 333, 250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 278, 278, 564, 564, 564, 444, 921, 722, 667, 667, 722, 611, 556, 722, 722, 333, 389, 722, 611, 889, 722, 722, 556, 722, 667, 556, 611, 722, 722, 944, 722, 722, 611, 333, 278, 333, 469, 500, 333, 444, 500, 444, 500, 444, 333, 500, 500, 278, 278, 500, 278, 778, 500, 500, 500, 500, 333, 389, 278, 500, 500, 722, 500, 500, 444, 480, 200, 480, 541, 333, 500, 500, 167, 500, 500, 500, 500, 180, 444, 500, 333, 333, 556, 556, 500, 500, 500, 250, 453, 350, 333, 444, 444, 500, 1000, 1000, 444, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 1000, 889, 276, 611, 722, 889, 310, 667, 278, 278, 500, 722, 500, 333, 444, 444, 500, 444, 722, 564, 722, 722, 444, 722, 500, 389, 444, 722, 722, 444, 722, 500, 611, 722, 250, 760, 611, 444, 444, 722, 278, 444, 611, 667, 444, 611, 389, 389, 278, 471, 667, 722, 500, 444, 722, 333, 444, 611, 556, 722, 667, 556, 588, 722, 500, 300, 722, 722, 722, 564, 500, 611, 476, 500, 722, 278, 611, 444, 444, 444, 500, 500, 722, 333, 564, 200, 760, 722, 333, 600, 611, 333, 500, 611, 611, 549, 722, 667, 278, 326, 444, 722, 722, 722, 444, 444, 278, 722, 500, 444, 389, 278, 722, 722, 612, 500, 300, 722, 500, 278, 500, 611, 500, 750, 556, 344, 722, 611, 980, 444, 333, 333, 611, 750, 549, 500, 500, 722, 611, 444, 500, 750, 556, 556, 722, 400, 500, 667, 500, 453, 722, 333, 722, 500, 667, 611, 722, 722, 722, 722, 444, 611, 333, 500, 564, 333, 500, 278, 564, 500, 500, 549, 500, 500, 444, 500, 300, 278, 500]}}
+1
View File
@@ -0,0 +1 @@
{"descriptor": {"StartFontMetrics": 4.1, "Comment": "VMusage 45775 55535", "FontName": "ZapfDingbats", "FullName": "ITC Zapf Dingbats", "FamilyName": "ZapfDingbats", "Weight": "Medium", "ItalicAngle": 0, "IsFixedPitch": false, "CharacterSet": "Special", "FontBBox": [-1, -143, 981, 820], "UnderlinePosition": -100, "UnderlineThickness": 50, "Version": 2.0, "Notice": "Copyright (c) 1985, 1987, 1988, 1989, 1997 Adobe Systems Incorporated. All Rights Reserved.ITC Zapf Dingbats is a registered trademark of International Typeface Corporation.", "EncodingScheme": "FontSpecific", "StdHW": 28, "StdVW": 90}, "metrics": {"codes": [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254], "widths": [278, 974, 961, 974, 980, 719, 789, 790, 791, 690, 960, 939, 549, 855, 911, 933, 911, 945, 974, 755, 846, 762, 761, 571, 677, 763, 760, 759, 754, 494, 552, 537, 577, 692, 786, 788, 788, 790, 793, 794, 816, 823, 789, 841, 823, 833, 816, 831, 923, 744, 723, 749, 790, 792, 695, 776, 768, 792, 759, 707, 708, 682, 701, 826, 815, 789, 789, 707, 687, 696, 689, 786, 787, 713, 791, 785, 791, 873, 761, 762, 762, 759, 759, 892, 892, 788, 784, 438, 138, 277, 415, 392, 392, 668, 668, 390, 390, 317, 317, 276, 276, 509, 509, 410, 410, 234, 234, 334, 334, 732, 544, 544, 910, 667, 760, 760, 776, 595, 694, 626, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 788, 894, 838, 1016, 458, 748, 924, 748, 918, 927, 928, 928, 834, 873, 828, 924, 924, 917, 930, 931, 463, 883, 836, 836, 867, 867, 696, 696, 874, 874, 760, 946, 771, 865, 771, 888, 967, 888, 831, 873, 927, 970, 918]}}
+10
View File
@@ -0,0 +1,10 @@
from pdfmajor.exceptions import PDFMajorException
class PDFFontError(PDFMajorException):
pass
class InvalidFont(PDFFontError):
def __init__(self, subtype: str) -> None:
super().__init__(f"The font {subtype} is invalid")
+65
View File
@@ -0,0 +1,65 @@
from typing import Any, Dict, Union
from .base import PDFFont
from . import standard14
class PDFSimpleFont(PDFFont):
"""A standard font in the PDF, as is defined by PDF spec 1.7 section 9.6.2"""
def __init__(
self,
subtype: str,
base_font: str,
widths: Dict[int, int],
font_descriptor: Dict[str, int],
encoding: Union[str, Dict[str, Any], None],
to_unicode: Any,
) -> None:
PDFFont.__init__(
self,
subtype,
base_font=base_font,
widths=widths,
font_descriptor=font_descriptor,
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]) -> "PDFSimpleFont":
subtype = str(d["Subtype"])
base_font = str(d["BaseFont"])
stnd_font = standard14.get_ifexists(base_font)
encoding = d.get("Encoding", None)
to_unicode = d.get("ToUnicode", None)
if stnd_font is not None:
return cls(
subtype=subtype,
base_font=base_font,
widths={k: v for k, v in zip(stnd_font.char_codes, stnd_font.widths)},
font_descriptor=stnd_font.descriptor,
encoding=encoding,
to_unicode=to_unicode,
)
else:
first_char = int(d.get("FirstChar", 0))
last_char = int(d.get("FirstChar", 255))
if d.get("Widths"):
widths_array = map(int, d["Widths"])
else:
widths_array = (0 for _ in range((last_char - first_char + 1)))
font_descriptor = dict(d["FontDescriptor"]) # type: Any
return cls(
subtype=subtype,
base_font=base_font,
widths={(i + first_char): w for (i, w) in enumerate(widths_array)},
font_descriptor=font_descriptor,
encoding=encoding,
to_unicode=to_unicode,
)
+31
View File
@@ -0,0 +1,31 @@
import json
from pathlib import Path
from typing import Any, Dict, List, NamedTuple, Optional
CORE_14_FOLDER = Path(__file__).parent / "core14"
ALL_FILES = {fpath.stem: fpath for fpath in CORE_14_FOLDER.iterdir()}
class StandardFontMetric(NamedTuple):
descriptor: Dict[str, Any]
char_codes: List[int]
widths: List[int]
def get_ifexists(font_name: str) -> Optional[StandardFontMetric]:
"""checks if have the font's parsed afm file, and return it's metadata if we do
Args:
font_name (str)
Returns:
Optional[StandardFontMetric]
"""
if font_name in ALL_FILES:
data = json.loads(ALL_FILES[font_name].read_text())
return StandardFontMetric(
descriptor=data["descriptor"],
char_codes=data["metrics"]["codes"],
widths=data["metrics"]["widths"],
)
return None
+12
View File
@@ -0,0 +1,12 @@
from typing import Any, Dict
from .base import PDFFont
class PDFType0Font(PDFFont):
"""PDF Type 30 object see PDF spec 1.7 section 9.6
TODO: finish this
"""
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "PDFType0Font":
return cls("Type3", **d)
+12
View File
@@ -0,0 +1,12 @@
from typing import Any, Dict
from .base import PDFFont
class PDFType3Font(PDFFont):
"""PDF Type 3 object see PDF spec 1.7 section 9.6.5
TODO: finish this
"""
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "PDFType3Font":
return cls("Type3", **d)
+32
View File
@@ -0,0 +1,32 @@
import traceback
import warnings
from typing import List
class PDFMajorWarning(Warning):
def __init__(self, issue_name: str, additional_info: str) -> None:
self.issue_name = issue_name
self.additional_info = additional_info
super().__init__(f"health issue detected: {issue_name}: {additional_info}")
class PDFHealthReport(List[PDFMajorWarning]):
"""a list of all issues that occured during the parsing of the document"""
def write(self, issue_name: str, additional_info: str):
log = PDFMajorWarning(issue_name, additional_info)
warnings.warn(log)
self.append(log)
def write_error(self, err: Exception):
tb = traceback.TracebackException.from_exception(err)
self.write(err.__class__.__name__, "\n".join(tb.format()))
def __repr__(self) -> str:
out = "<---HEALTH REPORT--->"
for log in self:
out += "\n"
out += f" > {log.issue_name}"
out += f" |-> {log.additional_info}"
out += "</---HEALTH REPORT--->"
return out
-127
View File
@@ -1,127 +0,0 @@
import struct
import os
import os.path
from io import BytesIO
from ..interpreter.PDFStream import LITERALS_DCT_DECODE
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_GRAY
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_RGB
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_CMYK
from ..layouts import LTImage
def align32(x):
return ((x+3)//4)*4
## BMPWriter
##
class BMPWriter(object):
def __init__(self, fp, bits, width, height):
self.fp = fp
self.bits = bits
self.width = width
self.height = height
if bits == 1:
ncols = 2
elif bits == 8:
ncols = 256
elif bits == 24:
ncols = 0
else:
raise ValueError(bits)
self.linesize = align32((self.width*self.bits+7)//8)
self.datasize = self.linesize * self.height
headersize = 14+40+ncols*4
info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, str(len(header))
self.fp.write(header)
self.fp.write(info)
if ncols == 2:
# B&W color table
for i in (0, 255):
self.fp.write(struct.pack('BBBx', i, i, i))
elif ncols == 256:
# grayscale color table
for i in range(256):
self.fp.write(struct.pack('BBBx', i, i, i))
self.pos0 = self.fp.tell()
self.pos1 = self.pos0 + self.datasize
return
def write_line(self, y, data):
self.fp.seek(self.pos1 - (y+1)*self.linesize)
self.fp.write(data)
return
## ImageWriter
##
class ImageWriter(object):
def __init__(self, outdir: str = None):
if outdir is None:
self.inactive = True
else:
self.inactive = False
self.outdir = outdir
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
def export_image(self, image: LTImage) -> str:
if self.inactive:
return "N/A"
stream = image.stream
filters = stream.get_filters()
(width, height) = image.srcsize
if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE:
ext = '.jpg'
elif (image.bits == 1 or
image.bits == 8 and image.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)):
ext = '.%dx%d.bmp' % (width, height)
else:
ext = '.%d.%dx%d.img' % (image.bits, width, height)
name = image.name+ext
path = os.path.join(self.outdir, name)
fp=open(path, 'wb')
if ext == '.jpg':
raw_data = stream.get_rawdata()
if LITERAL_DEVICE_CMYK in image.colorspace:
from PIL import Image
from PIL import ImageChops
ifp = BytesIO(raw_data)
i = Image.open(ifp)
i = ImageChops.invert(i)
i = i.convert('RGB')
i.save(fp, 'JPEG')
else:
fp.write(raw_data)
elif image.bits == 1:
bmp = BMPWriter(fp, 1, width, height)
data = stream.get_data()
i = 0
width = (width+7)//8
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_RGB:
bmp = BMPWriter(fp, 24, width, height)
data = stream.get_data()
i = 0
width = width*3
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_GRAY:
bmp = BMPWriter(fp, 8, width, height)
data = stream.get_data()
i = 0
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
else:
fp.write(stream.get_data())
fp.close()
return name
-44
View File
@@ -1,44 +0,0 @@
from ..utils import LATIN_ENCODING, name2unicode
from .PSStackParser import PSLiteral
class EncodingDB(object):
std2unicode = {}
mac2unicode = {}
win2unicode = {}
pdf2unicode = {}
for (name, std, mac, win, pdf) in LATIN_ENCODING:
c = name2unicode(name)
if std:
std2unicode[std] = c
if mac:
mac2unicode[mac] = c
if win:
win2unicode[win] = c
if pdf:
pdf2unicode[pdf] = c
encodings = {
'StandardEncoding': std2unicode,
'MacRomanEncoding': mac2unicode,
'WinAnsiEncoding': win2unicode,
'PDFDocEncoding': pdf2unicode,
}
@classmethod
def get_encoding(klass, name, diff=None):
cid2unicode = klass.encodings.get(name, klass.std2unicode)
if diff:
cid2unicode = cid2unicode.copy()
cid = 0
for x in diff:
if isinstance(x, int):
cid = x
elif isinstance(x, PSLiteral):
try:
cid2unicode[cid] = name2unicode(x.name)
except KeyError:
pass
cid += 1
return cid2unicode
-35
View File
@@ -1,35 +0,0 @@
import collections
from .PSStackParser import LIT
## PDFColorSpace
##
LITERAL_DEVICE_GRAY = LIT('DeviceGray')
LITERAL_DEVICE_RGB = LIT('DeviceRGB')
LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')
class PDFColorSpace(object):
def __init__(self, name: str, ncomponents: int):
self.name: str = name
self.ncomponents: int = ncomponents
return
def __repr__(self):
return '<PDFColorSpace: %s, ncomponents=%d>' % (self.name, self.ncomponents)
PREDEFINED_COLORSPACE = collections.OrderedDict()
for (name, n) in [
('DeviceGray', 1), # default value first
('CalRGB', 3),
('CalGray', 1),
('Lab', 3),
('DeviceRGB', 3),
('DeviceCMYK', 4),
('Separation', 1),
('Indexed', 1),
('Pattern', 1),
]:
PREDEFINED_COLORSPACE[name]=PDFColorSpace(name, n)
-109
View File
@@ -1,109 +0,0 @@
import re
import operator
from typing import List
from io import BytesIO
from ..utils import int2byte, choplist, settings
from .PSStackParser import literal_name
from .PSStackParser import PSStackParser
from .PSStackParser import PSEOF, PSTypeError
from .PSStackParser import KWD
from .PDFStream import PDFStream
class PDFContentParser(PSStackParser):
def __init__(self, streams: List[PDFStream]):
self.streams = streams
self.istream = 0
PSStackParser.__init__(self, None)
return
def fillfp(self):
if not self.fp:
if self.istream < len(self.streams):
strm = PDFStream.validated_stream(self.streams[self.istream])
self.istream += 1
else:
raise PSEOF('Unexpected EOF, file truncated?')
self.fp = BytesIO(strm.get_data())
return
def seek(self, pos):
self.fillfp()
PSStackParser.seek(self, pos)
return
def fillbuf(self):
if self.charpos < len(self.buf):
return
while 1:
self.fillfp()
self.bufpos = self.fp.tell()
self.buf = self.fp.read(self.BUFSIZ)
if self.buf:
break
self.fp = None
self.charpos = 0
return
def get_inline_data(self, pos, target=b'EI'):
self.seek(pos)
i = 0
data = b''
while i <= len(target):
self.fillbuf()
if i:
c = operator.getitem(self.buf,self.charpos)
c = int2byte(c)
data += c
self.charpos += 1
if len(target) <= i and c.isspace():
i += 1
elif i < len(target) and c == (int2byte(target[i])):
i += 1
else:
i = 0
else:
try:
j = self.buf.index(target[0], self.charpos)
data += self.buf[self.charpos:j+1]
self.charpos = j+1
i = 1
except ValueError:
data += self.buf[self.charpos:]
self.charpos = len(self.buf)
data = data[:-(len(target)+1)] # strip the last part
data = re.sub(br'(\x0d\x0a|[\x0d\x0a])$', b'', data)
return (pos, data)
def flush(self):
self.add_results(*self.popall())
return
KEYWORD_BI = KWD(b'BI')
KEYWORD_ID = KWD(b'ID')
KEYWORD_EI = KWD(b'EI')
def do_keyword(self, pos, token):
if token is self.KEYWORD_BI:
# inline image within a content stream
self.start_type(pos, 'inline')
elif token is self.KEYWORD_ID:
try:
(_, objs) = self.end_type('inline')
if len(objs) % 2 != 0:
raise PSTypeError('Invalid dictionary construct: %r' % objs)
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs))
(pos, data) = self.get_inline_data(pos+len(b'ID '))
obj = PDFStream(d, data)
self.push((pos, obj))
self.push((pos, self.KEYWORD_EI))
except PSTypeError:
if settings.STRICT:
raise
else:
self.push((pos, token))
return
@@ -1,235 +0,0 @@
import hashlib as md5
from Crypto.Cipher import ARC4, AES
from Crypto.Hash import SHA256
from .exceptions import * # pylint: disable=W0614
from ..PDFStream import int_value, str_value, dict_value
from ..PSStackParser import literal_name
from ...utils import int2byte, struct
## PDFSecurityHandler
##
class PDFStandardSecurityHandler(object):
PASSWORD_PADDING = (b'(\xbfN^Nu\x8aAd\x00NV\xff\xfa\x01\x08'
b'..\x00\xb6\xd0h>\x80/\x0c\xa9\xfedSiz')
supported_revisions = (2, 3)
def __init__(self, docid, param, password=''):
self.docid = docid
self.param = param
self.password = password
self.encrypt_metadata = False
self.init()
return
def init(self):
self.init_params()
if self.r not in self.supported_revisions:
raise PDFEncryptionError('Unsupported revision: param=%r' % self.param)
self.init_key()
return
def init_params(self):
self.v = int_value(self.param.get('V', 0))
self.r = int_value(self.param['R'])
self.p = int_value(self.param['P'])
self.o = str_value(self.param['O'])
self.u = str_value(self.param['U'])
self.length = int_value(self.param.get('Length', 40))
return
def init_key(self):
self.key = self.authenticate(self.password)
if self.key is None:
raise PDFPasswordIncorrect
return
def is_printable(self):
return bool(self.p & 4)
def is_modifiable(self):
return bool(self.p & 8)
def is_extractable(self):
return bool(self.p & 16)
def compute_u(self, key):
if self.r == 2:
# Algorithm 3.4
return ARC4.new(key).encrypt(self.PASSWORD_PADDING) # 2
else:
# Algorithm 3.5
hash = md5.md5(self.PASSWORD_PADDING) # 2
hash.update(self.docid[0]) # 3
result = ARC4.new(key).encrypt(hash.digest()) # 4
for i in range(1, 20): # 5
k = b''.join(int2byte(c ^ i) for c in iter(key))
result = ARC4.new(k).encrypt(result)
result += result # 6
return result
def compute_encryption_key(self, password):
# Algorithm 3.2
password = (password + self.PASSWORD_PADDING)[:32] # 1
hash = md5.md5(password) # 2
hash.update(self.o) # 3
hash.update(struct.pack('<l', self.p)) # 4
hash.update(self.docid[0]) # 5
if self.r >= 4:
if not self.encrypt_metadata:
hash.update(b'\xff\xff\xff\xff')
result = hash.digest()
n = 5
if self.r >= 3:
n = self.length // 8
for _ in range(50):
result = md5.md5(result[:n]).digest()
return result[:n]
def authenticate(self, password):
password = password.encode("latin1")
key = self.authenticate_user_password(password)
if key is None:
key = self.authenticate_owner_password(password)
return key
def authenticate_user_password(self, password):
key = self.compute_encryption_key(password)
if self.verify_encryption_key(key):
return key
else:
return None
def verify_encryption_key(self, key):
# Algorithm 3.6
u = self.compute_u(key)
if self.r == 2:
return u == self.u
return u[:16] == self.u[:16]
def authenticate_owner_password(self, password):
# Algorithm 3.7
password = (password + self.PASSWORD_PADDING)[:32]
hash = md5.md5(password)
if self.r >= 3:
for _ in range(50):
hash = md5.md5(hash.digest())
n = 5
if self.r >= 3:
n = self.length // 8
key = hash.digest()[:n]
if self.r == 2:
user_password = ARC4.new(key).decrypt(self.o)
else:
user_password = self.o
for i in range(19, -1, -1):
k = b''.join(int2byte(c ^ i) for c in iter(key))
user_password = ARC4.new(k).decrypt(user_password)
return self.authenticate_user_password(user_password)
def decrypt(self, objid, genno, data, attrs=None):
return self.decrypt_rc4(objid, genno, data)
def decrypt_rc4(self, objid, genno, data):
key = self.key + struct.pack('<L', objid)[:3] + struct.pack('<L', genno)[:2]
hash = md5.md5(key)
key = hash.digest()[:min(len(key), 16)]
return ARC4.new(key).decrypt(data)
class PDFStandardSecurityHandlerV4(PDFStandardSecurityHandler):
supported_revisions = (4,)
def init_params(self):
super(PDFStandardSecurityHandlerV4, self).init_params()
self.length = 128
self.cf = dict_value(self.param.get('CF'))
self.stmf = literal_name(self.param['StmF'])
self.strf = literal_name(self.param['StrF'])
self.encrypt_metadata = bool(self.param.get('EncryptMetadata', True))
if self.stmf != self.strf:
raise PDFEncryptionError('Unsupported crypt filter: param=%r' % self.param)
self.cfm = {}
for k, v in self.cf.items():
f = self.get_cfm(literal_name(v['CFM']))
if f is None:
raise PDFEncryptionError('Unknown crypt filter method: param=%r' % self.param)
self.cfm[k] = f
self.cfm['Identity'] = self.decrypt_identity
if self.strf not in self.cfm:
raise PDFEncryptionError('Undefined crypt filter: param=%r' % self.param)
return
def get_cfm(self, name):
if name == 'V2':
return self.decrypt_rc4
elif name == 'AESV2':
return self.decrypt_aes128
else:
return None
def decrypt(self, objid, genno, data, attrs=None, name=None):
if not self.encrypt_metadata and attrs is not None:
t = attrs.get('Type')
if t is not None and literal_name(t) == 'Metadata':
return data
if name is None:
name = self.strf
return self.cfm[name](objid, genno, data)
def decrypt_identity(self, objid, genno, data):
return data
def decrypt_aes128(self, objid, genno, data):
key = self.key + struct.pack('<L', objid)[:3] + struct.pack('<L', genno)[:2] + b'sAlT'
hash = md5.md5(key)
key = hash.digest()[:min(len(key), 16)]
return AES.new(key, mode=AES.MODE_CBC, IV=data[:16]).decrypt(data[16:])
class PDFStandardSecurityHandlerV5(PDFStandardSecurityHandlerV4):
supported_revisions = (5,)
def init_params(self):
super(PDFStandardSecurityHandlerV5, self).init_params()
self.length = 256
self.oe = str_value(self.param['OE'])
self.ue = str_value(self.param['UE'])
self.o_hash = self.o[:32]
self.o_validation_salt = self.o[32:40]
self.o_key_salt = self.o[40:]
self.u_hash = self.u[:32]
self.u_validation_salt = self.u[32:40]
self.u_key_salt = self.u[40:]
return
def get_cfm(self, name):
if name == 'AESV3':
return self.decrypt_aes256
else:
return None
def authenticate(self, password):
password = password.encode('utf-8')[:127]
hash = SHA256.new(password)
hash.update(self.o_validation_salt)
hash.update(self.u)
if hash.digest() == self.o_hash:
hash = SHA256.new(password)
hash.update(self.o_key_salt)
hash.update(self.u)
return AES.new(hash.digest(), mode=AES.MODE_CBC, IV=b'\x00' * 16).decrypt(self.oe)
hash = SHA256.new(password)
hash.update(self.u_validation_salt)
if hash.digest() == self.u_hash:
hash = SHA256.new(password)
hash.update(self.u_key_salt)
return AES.new(hash.digest(), mode=AES.MODE_CBC, IV=b'\x00' * 16).decrypt(self.ue)
return None
def decrypt_aes256(self, objid, genno, data):
return AES.new(self.key, mode=AES.MODE_CBC, IV=data[:16]).decrypt(data[16:])
-232
View File
@@ -1,232 +0,0 @@
import re
from logging import getLogger
from ...utils import settings, choplist, nunpack
from ..PSStackParser import PSEOF
from ..PSStackParser import KWD
from ..PDFParser import PDFStreamParser
from ..PDFStream import PDFStream
from ..PDFStream import dict_value
from .constants import LITERAL_OBJSTM, LITERAL_XREF
from .exceptions import PDFNoValidXRef, PDFSyntaxError
log = getLogger(__name__)
## XRefs
##
class PDFBaseXRef(object):
def get_trailer(self):
raise NotImplementedError
def get_objids(self):
return []
# Must return
# (strmid, index, genno)
# or (None, pos, genno)
def get_pos(self, objid):
raise KeyError(objid)
## PDFXRef
##
class PDFXRef(PDFBaseXRef):
def __init__(self):
self.offsets = {}
self.trailer = {}
return
def __repr__(self):
return '<PDFXRef: offsets=%r>' % (self.offsets.keys())
def load(self, parser):
while True:
try:
(pos, line) = parser.nextline()
if not line.strip():
continue
except PSEOF:
raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
if not line:
raise PDFNoValidXRef('Premature eof: %r' % parser)
if line.startswith(b'trailer'):
parser.seek(pos)
break
f = line.strip().split(b' ')
if len(f) != 2:
raise PDFNoValidXRef('Trailer not found: %r: line=%r' % (parser, line))
try:
(start, nobjs) = map(int, f)
except ValueError:
raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line))
for objid in range(start, start+nobjs):
try:
(_, line) = parser.nextline()
except PSEOF:
raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
f = line.strip().split(b' ')
if len(f) != 3:
raise PDFNoValidXRef('Invalid XRef format: %r, line=%r' % (parser, line))
(pos, genno, use) = f
if use != b'n':
continue
self.offsets[objid] = (None, int(pos), int(genno))
log.info('xref objects: %r', self.offsets)
self.load_trailer(parser)
return
def load_trailer(self, parser):
try:
(_, kwd) = parser.nexttoken()
assert kwd is KWD(b'trailer'), str(kwd)
(_, dic) = parser.nextobject()
except PSEOF:
x = parser.pop(1)
if not x:
raise PDFNoValidXRef('Unexpected EOF - file corrupted')
(_, dic) = x[0]
self.trailer.update(dict_value(dic))
log.debug('trailer=%r', self.trailer)
return
def get_trailer(self):
return self.trailer
def get_objids(self):
return iter(self.offsets.keys())
def get_pos(self, objid):
try:
return self.offsets[objid]
except KeyError:
raise
## PDFXRefFallback
##
class PDFXRefFallback(PDFXRef):
def __repr__(self):
return '<PDFXRefFallback: offsets=%r>' % (self.offsets.keys())
PDFOBJ_CUE = re.compile(r'^(\d+)\s+(\d+)\s+obj\b')
def load(self, parser):
parser.seek(0)
while 1:
try:
(pos, line) = parser.nextline()
except PSEOF:
break
if line.startswith(b'trailer'):
parser.seek(pos)
self.load_trailer(parser)
log.info('trailer: %r', self.trailer)
break
line=line.decode('latin-1') #default pdf encoding
m = self.PDFOBJ_CUE.match(line)
if not m:
continue
(objid, genno) = m.groups()
objid = int(objid)
genno = int(genno)
self.offsets[objid] = (None, pos, genno)
# expand ObjStm.
parser.seek(pos)
(_, obj) = parser.nextobject()
if isinstance(obj, PDFStream) and obj.get('Type') is LITERAL_OBJSTM:
stream = PDFStream.validated_stream(obj)
try:
n = stream['N']
except KeyError:
if settings.STRICT:
raise PDFSyntaxError('N is not defined: %r' % stream)
n = 0
parser1 = PDFStreamParser(stream.get_data())
objs = []
try:
while 1:
(_, obj) = parser1.nextobject()
objs.append(obj)
except PSEOF:
pass
n = min(n, len(objs)//2)
for index in range(n):
objid1 = objs[index*2]
self.offsets[objid1] = (objid, index, 0)
return
## PDFXRefStream
##
class PDFXRefStream(PDFBaseXRef):
def __init__(self):
self.data = None
self.entlen = None
self.fl1 = self.fl2 = self.fl3 = None
self.ranges = []
return
def __repr__(self):
return '<PDFXRefStream: ranges=%r>' % (self.ranges)
def load(self, parser):
(_, objid) = parser.nexttoken() # ignored
(_, genno) = parser.nexttoken() # ignored
(_, kwd) = parser.nexttoken()
(_, stream) = parser.nextobject()
if not isinstance(stream, PDFStream) or stream['Type'] is not LITERAL_XREF:
raise PDFNoValidXRef('Invalid PDF stream spec.')
size = stream['Size']
index_array = stream.get('Index', (0, size))
if len(index_array) % 2 != 0:
raise PDFSyntaxError('Invalid index number')
self.ranges.extend(choplist(2, index_array))
(self.fl1, self.fl2, self.fl3) = stream['W']
self.data = stream.get_data()
self.entlen = self.fl1+self.fl2+self.fl3
self.trailer = stream.attrs
log.info('xref stream: objid=%s, fields=%d,%d,%d',
', '.join(map(repr, self.ranges)),
self.fl1, self.fl2, self.fl3)
return
def get_trailer(self):
return self.trailer
def get_objids(self):
for (start, nobjs) in self.ranges:
for i in range(nobjs):
offset = self.entlen * i
ent = self.data[offset:offset+self.entlen]
f1 = nunpack(ent[:self.fl1], 1)
if f1 == 1 or f1 == 2:
yield start+i
return
def get_pos(self, objid):
index = 0
for (start, nobjs) in self.ranges:
if start <= objid and objid < start+nobjs:
index += objid - start
break
else:
index += nobjs
else:
raise KeyError(objid)
offset = self.entlen * index
ent = self.data[offset:offset+self.entlen]
f1 = nunpack(ent[:self.fl1], 1)
f2 = nunpack(ent[self.fl1:self.fl1+self.fl2])
f3 = nunpack(ent[self.fl1+self.fl2:])
if f1 == 1:
return (None, f2, f3)
elif f1 == 2:
return (f2, f3, 0)
else:
# this is a free object
raise KeyError(objid)
@@ -1,324 +0,0 @@
import hashlib as md5
from Crypto.Cipher import ARC4, AES
from Crypto.Hash import SHA256
from logging import getLogger
from ...utils import settings, choplist, decode_text
from ..PSStackParser import KWD
from ..PSStackParser import literal_name
from ..PSStackParser import PSEOF
from ..PDFStream import int_value, str_value, dict_value, list_value, decipher_all
from ..PDFStream import PDFObjectNotFound, PDFTypeError
from ..PDFParser import PDFStreamParser, PDFStream, PDFSyntaxError
from .exceptions import *
from .PDFSecurityHandler import PDFStandardSecurityHandler
from .PDFSecurityHandler import PDFStandardSecurityHandlerV4
from .PDFSecurityHandler import PDFStandardSecurityHandlerV5
from .PDFXRef import PDFXRefFallback, PDFXRef, PDFXRefStream
from .constants import LITERAL_CATALOG, LITERAL_OBJSTM
log = getLogger(__name__)
## PDFDocument
##
class PDFDocument(object):
"""PDFDocument object represents a PDF document.
Since a PDF file can be very big, normally it is not loaded at
once. So PDF document has to cooperate with a PDF parser in order to
dynamically import the data as processing goes.
Typical usage:
doc = PDFDocument(parser, password)
obj = doc.getobj(objid)
"""
security_handler_registry = {
1: PDFStandardSecurityHandler,
2: PDFStandardSecurityHandler,
}
if AES is not None:
security_handler_registry[4] = PDFStandardSecurityHandlerV4
if SHA256 is not None:
security_handler_registry[5] = PDFStandardSecurityHandlerV5
def __init__(self, parser, password='', caching=True, fallback=True):
"Set the document to use a given PDFParser object."
self.caching = caching
self.xrefs = []
self.info = []
self.catalog = None
self.encryption = None
self.decipher = None
self._parser = None
self._cached_objs = {}
self._parsed_objs = {}
self._parser = parser
self._parser.set_document(self)
self.is_printable = self.is_modifiable = self.is_extractable = True
# Retrieve the information of each header that was appended
# (maybe multiple times) at the end of the document.
try:
pos = self.find_xref(parser)
self.read_xref_from(parser, pos, self.xrefs)
except PDFNoValidXRef:
pass # fallback = True
if fallback:
parser.fallback = True
xref = PDFXRefFallback()
xref.load(parser)
self.xrefs.append(xref)
for xref in self.xrefs:
trailer = xref.get_trailer()
if not trailer:
continue
# If there's an encryption info, remember it.
if 'Encrypt' in trailer:
#assert not self.encryption, str(self.encryption)
self.encryption = (list_value(trailer['ID']),
dict_value(trailer['Encrypt']))
self._initialize_password(password)
if 'Info' in trailer:
self.info.append(dict_value(trailer['Info']))
if 'Root' in trailer:
# Every PDF file must have exactly one /Root dictionary.
self.catalog = dict_value(trailer['Root'])
break
else:
raise PDFSyntaxError('No /Root object! - Is this really a PDF?')
if self.catalog.get('Type') is not LITERAL_CATALOG:
if settings.STRICT:
raise PDFSyntaxError('Catalog not found!')
return
KEYWORD_OBJ = KWD(b'obj')
# _initialize_password(password=b'')
# Perform the initialization with a given password.
def _initialize_password(self, password=''):
(docid, param) = self.encryption
if literal_name(param.get('Filter')) != 'Standard':
raise PDFEncryptionError('Unknown filter: param=%r' % param)
v = int_value(param.get('V', 0))
factory = self.security_handler_registry.get(v)
if factory is None:
raise PDFEncryptionError('Unknown algorithm: param=%r' % param)
handler = factory(docid, param, password)
self.decipher = handler.decrypt
self.is_printable = handler.is_printable()
self.is_modifiable = handler.is_modifiable()
self.is_extractable = handler.is_extractable()
self._parser.fallback = False # need to read streams with exact length
return
def _getobj_objstm(self, stream, index, objid):
if stream.objid in self._parsed_objs:
(objs, n) = self._parsed_objs[stream.objid]
else:
(objs, n) = self._get_objects(stream)
if self.caching:
self._parsed_objs[stream.objid] = (objs, n)
i = n*2+index
try:
obj = objs[i]
except IndexError:
raise PDFSyntaxError('index too big: %r' % index)
return obj
def _get_objects(self, stream):
if stream.get('Type') is not LITERAL_OBJSTM:
if settings.STRICT:
raise PDFSyntaxError('Not a stream object: %r' % stream)
try:
n = stream['N']
except KeyError:
if settings.STRICT:
raise PDFSyntaxError('N is not defined: %r' % stream)
n = 0
parser = PDFStreamParser(stream.get_data())
parser.set_document(self)
objs = []
try:
while 1:
(_, obj) = parser.nextobject()
objs.append(obj)
except PSEOF:
pass
return (objs, n)
def _getobj_parse(self, pos, objid):
self._parser.seek(pos)
(_, objid1) = self._parser.nexttoken() # objid
(_, genno) = self._parser.nexttoken() # genno
(_, kwd) = self._parser.nexttoken()
# #### hack around malformed pdf files
# copied from https://github.com/jaepil/pdfminer3k/blob/master/pdfminer/pdfparser.py#L399
#to solve https://github.com/pdfminer/pdfminer.six/issues/56
#assert objid1 == objid, str((objid1, objid))
if objid1 != objid:
x = []
while kwd is not self.KEYWORD_OBJ:
(_,kwd) = self._parser.nexttoken()
x.append(kwd)
if x:
objid1 = x[-2]
genno = x[-1]
# #### end hack around malformed pdf files
if objid1 != objid:
raise PDFSyntaxError('objid mismatch: %r=%r' % (objid1, objid))
if kwd != KWD(b'obj'):
raise PDFSyntaxError('Invalid object spec: offset=%r' % pos)
(_, obj) = self._parser.nextobject()
return obj
# can raise PDFObjectNotFound
def getobj(self, objid):
assert objid != 0
if not self.xrefs:
raise PDFException('PDFDocument is not initialized')
log.debug('getobj: objid=%r', objid)
if objid in self._cached_objs:
(obj, genno) = self._cached_objs[objid]
else:
for xref in self.xrefs:
try:
(strmid, index, genno) = xref.get_pos(objid)
except KeyError:
continue
try:
if strmid is not None:
stream = PDFStream.validated_stream(self.getobj(strmid))
obj = self._getobj_objstm(stream, index, objid)
else:
obj = self._getobj_parse(index, objid)
if self.decipher:
obj = decipher_all(self.decipher, objid, genno, obj)
if isinstance(obj, PDFStream):
obj.set_objid(objid, genno)
break
except (PSEOF, PDFSyntaxError):
continue
else:
raise PDFObjectNotFound(objid)
log.debug('register: objid=%r: %r', objid, obj)
if self.caching:
self._cached_objs[objid] = (obj, genno)
return obj
def get_outlines(self):
if 'Outlines' not in self.catalog:
raise PDFNoOutlines
def search(entry, level):
entry = dict_value(entry)
if 'Title' in entry:
if 'A' in entry or 'Dest' in entry:
title = decode_text(str_value(entry['Title']))
dest = entry.get('Dest')
action = entry.get('A')
se = entry.get('SE')
yield (level, title, dest, action, se)
if 'First' in entry and 'Last' in entry:
for x in search(entry['First'], level+1):
yield x
if 'Next' in entry:
for x in search(entry['Next'], level):
yield x
return
return search(self.catalog['Outlines'], 0)
def lookup_name(self, cat, key):
try:
names = dict_value(self.catalog['Names'])
except (PDFTypeError, KeyError):
raise KeyError((cat, key))
# may raise KeyError
d0 = dict_value(names[cat])
def lookup(d):
if 'Limits' in d:
(k1, k2) = list_value(d['Limits'])
if key < k1 or k2 < key:
return None
if 'Names' in d:
objs = list_value(d['Names'])
names = dict(choplist(2, objs))
return names[key]
if 'Kids' in d:
for c in list_value(d['Kids']):
v = lookup(dict_value(c))
if v:
return v
raise KeyError((cat, key))
return lookup(d0)
def get_dest(self, name):
try:
# PDF-1.2 or later
obj = self.lookup_name('Dests', name)
except KeyError:
# PDF-1.1 or prior
if 'Dests' not in self.catalog:
raise PDFDestinationNotFound(name)
d0 = dict_value(self.catalog['Dests'])
if name not in d0:
raise PDFDestinationNotFound(name)
obj = d0[name]
return obj
# find_xref
def find_xref(self, parser):
"""Internal function used to locate the first XRef."""
# search the last xref table by scanning the file backwards.
prev = None
for line in parser.revreadlines():
line = line.strip()
log.debug('find_xref: %r', line)
if line == b'startxref':
break
if line:
prev = line
else:
raise PDFNoValidXRef('Unexpected EOF')
log.info('xref found: pos=%r', prev)
return int(prev)
# read xref table
def read_xref_from(self, parser, start, xrefs):
"""Reads XRefs from the given location."""
parser.seek(start)
parser.reset()
try:
(pos, token) = parser.nexttoken()
except PSEOF:
raise PDFNoValidXRef('Unexpected EOF')
log.info('read_xref_from: start=%d, token=%r', start, token)
if isinstance(token, int):
# XRefStream: PDF-1.5
parser.seek(pos)
parser.reset()
xref = PDFXRefStream()
xref.load(parser)
else:
if token is parser.KEYWORD_XREF:
parser.nextline()
xref = PDFXRef()
xref.load(parser)
xrefs.append(xref)
trailer = xref.get_trailer()
log.info('trailer: %r', trailer)
if 'XRefStm' in trailer:
pos = int_value(trailer['XRefStm'])
self.read_xref_from(parser, pos, xrefs)
if 'Prev' in trailer:
# find previous xref
pos = int_value(trailer['Prev'])
self.read_xref_from(parser, pos, xrefs)
return
@@ -1,6 +0,0 @@
from ..PSStackParser import LIT
# some predefined literals and keywords.
LITERAL_OBJSTM = LIT('ObjStm')
LITERAL_XREF = LIT('XRef')
LITERAL_CATALOG = LIT('Catalog')
@@ -1,22 +0,0 @@
from ..PDFParser import PDFSyntaxError
from ..PDFStream import PDFException
## Exceptions
##
class PDFNoValidXRef(PDFSyntaxError):
pass
class PDFNoOutlines(PDFException):
pass
class PDFDestinationNotFound(PDFException):
pass
class PDFEncryptionError(PDFException):
pass
class PDFPasswordIncorrect(PDFEncryptionError):
pass
class PDFTextExtractionNotAllowed(PDFEncryptionError):
pass
-755
View File
@@ -1,755 +0,0 @@
import sys
import struct
from io import BytesIO
from abc import abstractmethod
from .cmapdb import CMapDB
from .cmapdb import CMapParser
from .cmapdb import FileUnicodeMap
from .cmapdb import CMap
from .EncodingDB import EncodingDB, name2unicode
from .PSStackParser import PSStackParser
from .PSStackParser import PSEOF
from .PSStackParser import LIT
from .PSStackParser import KWD
from .PSStackParser import PSLiteral
from .PSStackParser import literal_name
from .PDFStream import PDFException
from .PDFStream import resolve1
from .PDFStream import int_value
from .PDFStream import num_value
from .PDFStream import list_value
from .PDFStream import dict_value
from .PDFStream import PDFStream
from ..utils import settings, FONT_METRICS, apply_matrix_norm, nunpack, choplist, isnumber
from .constants import COMMON_FONT_ATTRIBUTES
def get_widths(seq):
widths = {}
r = []
for v in seq:
if isinstance(v, list):
if r:
char1 = r[-1]
for (i, w) in enumerate(v):
widths[char1+i] = w
r = []
elif isnumber(v):
r.append(v)
if len(r) == 3:
(char1, char2, w) = r
for i in range(char1, char2+1):
widths[i] = w
r = []
return widths
#assert get_widths([1]) == {}
#assert get_widths([1,2,3]) == {1:3, 2:3}
#assert get_widths([1,[2,3],6,[7,8]]) == {1:2,2:3, 6:7,7:8}
def get_widths2(seq):
widths = {}
r = []
for v in seq:
if isinstance(v, list):
if r:
char1 = r[-1]
for (i, (w, vx, vy)) in enumerate(choplist(3, v)):
widths[char1+i] = (w, (vx, vy))
r = []
elif isnumber(v):
r.append(v)
if len(r) == 5:
(char1, char2, w, vx, vy) = r # pylint: disable=E0632
for i in range(char1, char2+1):
widths[i] = (w, (vx, vy))
r = []
return widths
#assert get_widths2([1]) == {}
#assert get_widths2([1,2,3,4,5]) == {1:(3, (4,5)), 2:(3, (4,5))}
#assert get_widths2([1,[2,3,4,5],6,[7,8,9]]) == {1:(2, (3,4)), 6:(7, (8,9))}
## FontMetricsDB
##
class FontMetricsDB(object):
@classmethod
def get_metrics(klass, fontname):
return FONT_METRICS[fontname]
## Type1FontHeaderParser
##
class Type1FontHeaderParser(PSStackParser):
KEYWORD_BEGIN = KWD(b'begin')
KEYWORD_END = KWD(b'end')
KEYWORD_DEF = KWD(b'def')
KEYWORD_PUT = KWD(b'put')
KEYWORD_DICT = KWD(b'dict')
KEYWORD_ARRAY = KWD(b'array')
KEYWORD_READONLY = KWD(b'readonly')
KEYWORD_FOR = KWD(b'for')
KEYWORD_FOR = KWD(b'for')
def __init__(self, data):
PSStackParser.__init__(self, data)
self._cid2unicode = {}
return
def get_encoding(self):
while 1:
try:
(cid, name) = self.nextobject()
except PSEOF:
break
try:
self._cid2unicode[cid] = name2unicode(name)
except KeyError:
pass
return self._cid2unicode
def do_keyword(self, pos, token):
if token is self.KEYWORD_PUT:
((_, key), (_, value)) = self.pop(2) # pylint: disable=E0632
if (isinstance(key, int) and
isinstance(value, PSLiteral)):
self.add_results((key, literal_name(value)))
return
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
## CFFFont
## (Format specified in Adobe Technical Note: #5176
## "The Compact Font Format Specification")
##
def getdict(data):
d = {}
fp = BytesIO(data)
stack = []
while 1:
c = fp.read(1)
if not c:
break
b0 = ord(c)
if b0 <= 21:
d[b0] = stack
stack = []
continue
if b0 == 30:
s = ''
loop = True
while loop:
b = ord(fp.read(1))
for n in (b >> 4, b & 15):
if n == 15:
loop = False
else:
s += NIBBLES[n]
value = float(s)
elif 32 <= b0 and b0 <= 246:
value = b0-139
else:
b1 = ord(fp.read(1))
if 247 <= b0 and b0 <= 250:
value = ((b0-247) << 8)+b1+108
elif 251 <= b0 and b0 <= 254:
value = -((b0-251) << 8)-b1-108
else:
b2 = ord(fp.read(1))
if 128 <= b1:
b1 -= 256
if b0 == 28:
value = b1 << 8 | b2
else:
value = b1 << 24 | b2 << 16 | struct.unpack('>H', fp.read(2))[0]
stack.append(value)
return d
class CFFFont(object):
STANDARD_STRINGS = (
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign',
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period',
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash',
'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown',
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section',
'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash',
'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet',
'quotesinglbase', 'quotedblbase', 'quotedblright',
'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve',
'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut',
'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash',
'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
'threequarters', 'twosuperior', 'registered', 'minus', 'eth',
'multiply', 'threesuperior', 'copyright', 'Aacute',
'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde',
'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde',
'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave',
'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex',
'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
'odieresis', 'ograve', 'otilde', 'scaron', 'uacute',
'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis',
'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
'dollarsuperior', 'ampersandsmall', 'Acutesmall',
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
'commasuperior', 'threequartersemdash', 'periodsuperior',
'questionsmall', 'asuperior', 'bsuperior', 'centsuperior',
'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior',
'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior',
'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall',
'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall',
'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall',
'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall',
'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall',
'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall',
'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths',
'onethird', 'twothirds', 'zerosuperior', 'foursuperior',
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
'seveninferior', 'eightinferior', 'nineinferior',
'centinferior', 'dollarinferior', 'periodinferior',
'commainferior', 'Agravesmall', 'Aacutesmall',
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall',
'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
'Igravesmall', 'Iacutesmall', 'Icircumflexsmall',
'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall',
'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book',
'Light', 'Medium', 'Regular', 'Roman', 'Semibold',
)
class INDEX(object):
def __init__(self, fp):
self.fp = fp
self.offsets = []
(count, offsize) = struct.unpack('>HB', self.fp.read(3))
for _ in range(count+1):
self.offsets.append(nunpack(self.fp.read(offsize)))
self.base = self.fp.tell()-1
self.fp.seek(self.base+self.offsets[-1])
return
def __repr__(self):
return '<INDEX: size=%d>' % len(self)
def __len__(self): # pylint: disable=E0303
return len(self.offsets)-1
def __getitem__(self, i):
self.fp.seek(self.base+self.offsets[i])
return self.fp.read(self.offsets[i+1]-self.offsets[i])
def __iter__(self):
return iter(self[i] for i in range(len(self)))
def __init__(self, name, fp):
self.name = name
self.fp = fp
# Header
(_major, _minor, hdrsize, _) = struct.unpack('BBBB', self.fp.read(4))
self.fp.read(hdrsize-4)
# Name INDEX
self.name_index = self.INDEX(self.fp)
# Top DICT INDEX
self.dict_index = self.INDEX(self.fp)
# String INDEX
self.string_index = self.INDEX(self.fp)
# Global Subr INDEX
self.subr_index = self.INDEX(self.fp)
# Top DICT DATA
self.top_dict = getdict(self.dict_index[0])
(charset_pos,) = self.top_dict.get(15, [0])
(encoding_pos,) = self.top_dict.get(16, [0])
(charstring_pos,) = self.top_dict.get(17, [0])
# CharStrings
self.fp.seek(charstring_pos)
self.charstring = self.INDEX(self.fp)
self.nglyphs = len(self.charstring)
# Encodings
self.code2gid = {}
self.gid2code = {}
self.fp.seek(encoding_pos)
format = self.fp.read(1)
if format == b'\x00':
# Format 0
(n,) = struct.unpack('B', self.fp.read(1))
for (code, gid) in enumerate(struct.unpack('B'*n, self.fp.read(n))):
self.code2gid[code] = gid
self.gid2code[gid] = code
elif format == b'\x01':
# Format 1
(n,) = struct.unpack('B', self.fp.read(1))
code = 0
for _ in range(n):
(first, nleft) = struct.unpack('BB', self.fp.read(2))
for gid in range(first, first+nleft+1):
self.code2gid[code] = gid
self.gid2code[gid] = code
code += 1
else:
raise ValueError('unsupported encoding format: %r' % format)
# Charsets
self.name2gid = {}
self.gid2name = {}
self.fp.seek(charset_pos)
format = self.fp.read(1)
if format == b'\x00':
# Format 0
n = self.nglyphs-1
for (gid, sid) in enumerate(struct.unpack('>'+'H'*n, self.fp.read(2*n))):
gid += 1
name = self.getstr(sid)
self.name2gid[name] = gid
self.gid2name[gid] = name
elif format == b'\x01':
# Format 1
(n,) = struct.unpack('B', self.fp.read(1))
sid = 0
for _ in range(n):
(first, nleft) = struct.unpack('BB', self.fp.read(2))
for gid in range(first, first+nleft+1):
name = self.getstr(sid)
self.name2gid[name] = gid
self.gid2name[gid] = name
sid += 1
elif format == b'\x02':
# Format 2
assert False, str(('Unhandled', format))
else:
raise ValueError('unsupported charset format: %r' % format)
#print self.code2gid
#print self.name2gid
#assert 0
return
def getstr(self, sid):
if sid < len(self.STANDARD_STRINGS):
return self.STANDARD_STRINGS[sid]
return self.string_index[sid-len(self.STANDARD_STRINGS)]
## TrueTypeFont
##
class TrueTypeFont(object):
class CMapNotFound(Exception):
pass
def __init__(self, name, fp):
self.name = name
self.fp = fp
self.tables = {}
self.fonttype = fp.read(4)
try:
(ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
for _ in range(ntables):
(name, _, offset, length) = struct.unpack('>4sLLL', fp.read(16))
self.tables[name] = (offset, length)
except struct.error:
# Do not fail if there are not enough bytes to read. Even for
# corrupted PDFs we would like to get as much information as
# possible, so continue.
pass
return
def create_unicode_map(self):
if 'cmap' not in self.tables:
raise TrueTypeFont.CMapNotFound
(base_offset, _) = self.tables['cmap']
fp = self.fp
fp.seek(base_offset)
(_, nsubtables) = struct.unpack('>HH', fp.read(4))
subtables = []
for i in range(nsubtables):
subtables.append(struct.unpack('>HHL', fp.read(8)))
char2gid = {}
# Only supports subtable type 0, 2 and 4.
for (_1, _2, st_offset) in subtables:
fp.seek(base_offset+st_offset)
(fmttype, _, _) = struct.unpack('>HHH', fp.read(6))
if fmttype == 0:
char2gid.update(enumerate(struct.unpack('>256B', fp.read(256))))
elif fmttype == 2:
subheaderkeys = struct.unpack('>256H', fp.read(512))
firstbytes = [0]*8192
for (i, k) in enumerate(subheaderkeys):
firstbytes[k//8] = i
nhdrs = max(subheaderkeys)//8 + 1
hdrs = []
for i in range(nhdrs):
(firstcode, entcount, delta, offset) = struct.unpack('>HHhH', fp.read(8))
hdrs.append((i, firstcode, entcount, delta, fp.tell()-2+offset))
for (i, firstcode, entcount, delta, pos) in hdrs:
if not entcount:
continue
first = firstcode + (firstbytes[i] << 8)
fp.seek(pos)
for c in range(entcount):
gid = struct.unpack('>H', fp.read(2))
if gid:
gid += delta
char2gid[first+c] = gid
elif fmttype == 4:
(segcount, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
segcount //= 2
ecs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
fp.read(2)
scs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
idds = struct.unpack('>%dh' % segcount, fp.read(2*segcount))
pos = fp.tell()
idrs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
for (ec, sc, idd, idr) in zip(ecs, scs, idds, idrs):
if idr:
fp.seek(pos+idr)
for c in range(sc, ec+1):
char2gid[c] = (struct.unpack('>H', fp.read(2))[0] + idd) & 0xffff
else:
for c in range(sc, ec+1):
char2gid[c] = (c + idd) & 0xffff
else:
assert False, str(('Unhandled', fmttype))
# create unicode map
unicode_map = FileUnicodeMap()
for (char, gid) in char2gid.items():
unicode_map.add_cid2unichr(gid, char)
return unicode_map
## Fonts
##
class PDFFontError(PDFException):
pass
class PDFUnicodeNotDefined(PDFFontError):
pass
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
LITERAL_TYPE1C = LIT('Type1C')
# PDFFont
class PDFFont(object):
def __init__(self, descriptor, widths, default_width=None):
self.descriptor = descriptor
self.widths = widths
self.fontname = resolve1(descriptor.get('FontName', 'unknown'))
if isinstance(self.fontname, PSLiteral):
self.fontname = literal_name(self.fontname)
self.flags = int_value(descriptor.get('Flags', 0))
self.ascent = num_value(descriptor.get('Ascent', 0))
self.descent = num_value(descriptor.get('Descent', 0))
self.italic_angle = num_value(descriptor.get('ItalicAngle', 0))
self.default_width = default_width or num_value(descriptor.get('MissingWidth', 0))
self.leading = num_value(descriptor.get('Leading', 0))
self.bbox = list_value(descriptor.get('FontBBox', (0, 0, 0, 0)))
self.hscale = self.vscale = .001
return
def __repr__(self):
return '<PDFFont>'
def is_vertical(self):
return False
def is_multibyte(self):
return False
def decode(self, bytes):
return bytearray(bytes) # map(ord, bytes)
def get_ascent(self):
return self.ascent * self.vscale
def get_descent(self):
return self.descent * self.vscale
def get_width(self):
w = self.bbox[2]-self.bbox[0]
if w == 0:
w = -self.default_width
return w * self.hscale
def get_height(self):
h = self.bbox[3]-self.bbox[1]
if h == 0:
h = self.ascent - self.descent
return h * self.vscale
def char_width(self, cid):
try:
return self.widths[cid] * self.hscale
except KeyError:
try:
return self.widths[self.to_unichr(cid)] * self.hscale
except (KeyError, PDFUnicodeNotDefined):
return self.default_width * self.hscale
def char_disp(self, cid):
return 0
def string_width(self, s):
return sum(self.char_width(cid) for cid in self.decode(s))
@abstractmethod
def to_unichr(self, cid):
raise NotImplementedError
@property
def font_weight(self):
if 'FontWeight' in self.descriptor.keys():
return self.descriptor['FontWeight']
elif 'bold' in self.fontname.lower():
return 'bold'
else:
return None
@property
def is_bold(self):
return self.font_weight == 'bold' or self.font_weight > 549
# PDFSimpleFont
class PDFSimpleFont(PDFFont):
def __init__(self, descriptor, widths, spec):
# Font encoding is specified either by a name of
# built-in encoding or a dictionary that describes
# the differences.
if 'Encoding' in spec:
encoding = resolve1(spec['Encoding'])
else:
encoding = LITERAL_STANDARD_ENCODING
if isinstance(encoding, dict):
name = literal_name(encoding.get('BaseEncoding', LITERAL_STANDARD_ENCODING))
diff = list_value(encoding.get('Differences', []))
self.cid2unicode = EncodingDB.get_encoding(name, diff)
else:
self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
self.unicode_map = None
if 'ToUnicode' in spec:
strm = PDFStream.validated_stream(spec['ToUnicode'])
self.unicode_map = FileUnicodeMap()
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
PDFFont.__init__(self, descriptor, widths)
return
def to_unichr(self, cid):
if self.unicode_map:
try:
return self.unicode_map.get_unichr(cid)
except KeyError:
pass
try:
return self.cid2unicode[cid]
except KeyError:
raise PDFUnicodeNotDefined(None, cid)
# PDFType1Font
class PDFType1Font(PDFSimpleFont):
def __init__(self, rsrcmgr, spec):
try:
self.basefont = literal_name(spec['BaseFont'])
except KeyError:
if settings.STRICT:
raise PDFFontError('BaseFont is missing')
self.basefont = 'unknown'
try:
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
except KeyError:
descriptor = dict_value(spec.get('FontDescriptor', {}))
firstchar = int_value(spec.get('FirstChar', 0))
#lastchar = int_value(spec.get('LastChar', 255))
widths = list_value(spec.get('Widths', [0]*256))
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
PDFSimpleFont.__init__(self, descriptor, widths, spec)
if 'Encoding' not in spec and 'FontFile' in descriptor:
# try to recover the missing encoding info from the font file.
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile'))
length1 = int_value(self.fontfile['Length1'])
data = self.fontfile.get_data()[:length1]
parser = Type1FontHeaderParser(BytesIO(data))
self.cid2unicode = parser.get_encoding()
return
def __repr__(self):
return '<PDFType1Font: basefont=%r>' % self.basefont
# PDFTrueTypeFont
class PDFTrueTypeFont(PDFType1Font):
def __repr__(self):
return '<PDFTrueTypeFont: basefont=%r>' % self.basefont
# PDFType3Font
class PDFType3Font(PDFSimpleFont):
def __init__(self, rsrcmgr, spec):
firstchar = int_value(spec.get('FirstChar', 0))
#lastchar = int_value(spec.get('LastChar', 0))
widths = list_value(spec.get('Widths', [0]*256))
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
if 'FontDescriptor' in spec:
descriptor = dict_value(spec['FontDescriptor'])
else:
descriptor = {'Ascent': 0, 'Descent': 0,
'FontBBox': spec['FontBBox']}
PDFSimpleFont.__init__(self, descriptor, widths, spec)
self.matrix = tuple(list_value(spec.get('FontMatrix')))
(_, self.descent, _, self.ascent) = self.bbox
(self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
return
def __repr__(self):
return '<PDFType3Font>'
# PDFCIDFont
class PDFCIDFont(PDFFont):
def __init__(self, rsrcmgr, spec, strict=settings.STRICT):
try:
self.basefont = literal_name(spec['BaseFont'])
except KeyError:
if strict:
raise PDFFontError('BaseFont is missing')
self.basefont = 'unknown'
self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {}))
self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"),
resolve1(self.cidsysteminfo.get('Ordering', b'unknown')).decode("latin1"))
try:
name = literal_name(spec['Encoding'])
except KeyError:
if strict:
raise PDFFontError('Encoding is unspecified')
name = 'unknown'
try:
self.cmap = CMapDB.get_cmap(name)
except CMapDB.CMapNotFound as e:
if strict:
raise PDFFontError(e)
self.cmap = CMap()
try:
descriptor = dict_value(spec['FontDescriptor'])
except KeyError:
if strict:
raise PDFFontError('FontDescriptor is missing')
descriptor = {}
ttf = None
if 'FontFile2' in descriptor:
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile2'))
ttf = TrueTypeFont(self.basefont,
BytesIO(self.fontfile.get_data()))
self.unicode_map = None
if 'ToUnicode' in spec:
strm = PDFStream.validated_stream(spec['ToUnicode'])
self.unicode_map = FileUnicodeMap()
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
if ttf:
try:
self.unicode_map = ttf.create_unicode_map()
except TrueTypeFont.CMapNotFound:
pass
else:
try:
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
except CMapDB.CMapNotFound as e:
pass
self.vertical = self.cmap.is_vertical()
if self.vertical:
# writing mode: vertical
widths = get_widths2(list_value(spec.get('W2', [])))
self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in iter(widths.items()))
(vy, w) = spec.get('DW2', [880, -1000])
self.default_disp = (None, vy)
widths = dict((cid, w) for (cid, (w, _)) in iter(widths.items()))
default_width = w
else:
# writing mode: horizontal
self.disps = {}
self.default_disp = 0
widths = get_widths(list_value(spec.get('W', [])))
default_width = spec.get('DW', 1000)
PDFFont.__init__(self, descriptor, widths, default_width=default_width)
return
def __repr__(self):
return '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)
def is_vertical(self):
return self.vertical
def is_multibyte(self):
return True
def decode(self, bytes):
return self.cmap.decode(bytes)
def char_disp(self, cid):
"Returns an integer for horizontal fonts, a tuple for vertical fonts."
return self.disps.get(cid, self.default_disp)
def to_unichr(self, cid):
try:
if not self.unicode_map:
raise KeyError(cid)
return self.unicode_map.get_unichr(cid)
except KeyError:
raise PDFUnicodeNotDefined(self.cidcoding, cid)
# main
def main(argv):
for fname in argv[1:]:
fp = open(fname, 'rb')
#font = TrueTypeFont(fname, fp)
font = CFFFont(fname, fp)
print (font)
fp.close()
return
if __name__ == '__main__':
sys.exit(main(sys.argv))
@@ -1,61 +0,0 @@
class PDFGraphicStateColor:
class NotSet(Exception): pass
def __init__(self):
self.__rgb = None
self.__gray = None
self.__cmyk = None
def get_color(self):
if self.__rgb is not None:
return 'rgb', self.rgb
elif self.__cmyk is not None:
return 'cmyk', self.cmyk
elif self.__gray is not None:
return 'gray', self.gray
else: return None, None
@property
def is_set(self):
return self.__rgb is not None or self.__cmyk is not None or self.__gray is not None
@property
def rgb(self):
if self.__rgb is not None: return [*self.__rgb]
else: raise self.NotSet
@property
def cmyk(self):
if self.__cmyk is not None: return [*self.__cmyk]
else: raise self.NotSet
@property
def gray(self):
if self.__gray is not None: return float(self.__gray)
else: raise self.NotSet
@rgb.setter
def rgb(self, rgb):
self.__rgb = rgb
@cmyk.setter
def cmyk(self, cmyk):
self.__cmyk = cmyk
@gray.setter
def gray(self, color):
self.__gray = color
def copy(self):
cp = self.__class__()
cp.gray = self.__gray
cp.rgb = self.__rgb
cp.cmyk = self.__cmyk
return cp
def __repr__(self):
rep = "<PDFGraphicStateColor"
if self.__gray is not None: rep += f' gray={self.__gray}'
if self.__rgb is not None: rep += f' rgb=[{",".join(map(str, self.__rgb))}]'
if self.__cmyk is not None: rep += f' cmyk=[{",".join(map(str, self.__cmyk))}]'
return rep + '/>'
@@ -1,45 +0,0 @@
from .PDFGraphicStateColor import PDFGraphicStateColor
class PDFGraphicState(object):
def __init__(self):
self.linewidth = 0
self.linecap = None
self.linejoin = None
self.miterlimit = None
self.dash = None
self.intent = None
self.flatness = None
# stroking color
self.scolor = PDFGraphicStateColor()
# non stroking color
self.ncolor = PDFGraphicStateColor()
return
def copy(self):
obj = PDFGraphicState()
obj.linewidth = self.linewidth
obj.linecap = self.linecap
obj.linejoin = self.linejoin
obj.miterlimit = self.miterlimit
obj.dash = self.dash
obj.intent = self.intent
obj.flatness = self.flatness
obj.scolor = self.scolor.copy()
obj.ncolor = self.ncolor.copy()
return obj
def __repr__(self):
return ('<PDFGraphicState: linewidth=%r, linecap=%r, linejoin=%r, '
' miterlimit=%r, dash=%r, intent=%r, flatness=%r, '
' stroking color=%r, non stroking color=%r'
'>' %
(
self.linewidth, self.linecap, self.linejoin,
self.miterlimit, self.dash, self.intent, self.flatness,
str(self.scolor), str(self.ncolor)
)
)
-135
View File
@@ -1,135 +0,0 @@
import logging
from ..utils import settings
from .constants import LIT
from .PDFStream import PDFObjectNotFound
from .PDFStream import resolve1, int_value, list_value, dict_value
from .PDFParser import PDFParser
from .PDFDocument import PDFDocument, PDFTextExtractionNotAllowed
log = logging.getLogger(__name__)
# some predefined literals and keywords.
LITERAL_PAGE = LIT('Page')
LITERAL_PAGES = LIT('Pages')
## PDFPage
##
class PDFPage(object):
"""An object that holds the information about a page.
A PDFPage object is merely a convenience class that has a set
of keys and values, which describe the properties of a page
and point to its contents.
Attributes:
doc: a PDFDocument object.
pageid: any Python object that can uniquely identify the page.
attrs: a dictionary of page attributes.
contents: a list of PDFStream objects that represents the page content.
lastmod: the last modified time of the page.
resources: a list of resources used by the page.
mediabox: the physical size of the page.
cropbox: the crop rectangle of the page.
rotate: the page rotation (in degree).
annots: the page annotations.
beads: a chain that represents natural reading order.
"""
def __init__(self, doc: PDFDocument, pageid, attrs):
"""Initialize a page object.
doc: a PDFDocument object.
pageid: any Python object that can uniquely identify the page.
attrs: a dictionary of page attributes.
"""
self.doc = doc
self.pageid = pageid
self.attrs = dict_value(attrs)
self.lastmod = resolve1(self.attrs.get('LastModified'))
self.resources = resolve1(self.attrs.get('Resources', dict()))
self.mediabox = resolve1(self.attrs['MediaBox'])
if 'CropBox' in self.attrs:
self.cropbox = resolve1(self.attrs['CropBox'])
else:
self.cropbox = self.mediabox
self.rotate = (int_value(self.attrs.get('Rotate', 0))+360) % 360
self.annots = self.attrs.get('Annots')
self.beads = self.attrs.get('B')
if 'Contents' in self.attrs:
contents = resolve1(self.attrs['Contents'])
else:
contents = []
if not isinstance(contents, list):
contents = [contents]
self.contents = contents
return
def __repr__(self):
return '<PDFPage: Resources=%r, MediaBox=%r>' % (self.resources, self.mediabox)
INHERITABLE_ATTRS = set(['Resources', 'MediaBox', 'CropBox', 'Rotate'])
@classmethod
def create_pages(cls, document: PDFDocument):
def search(obj, parent):
if isinstance(obj, int):
objid = obj
tree = dict_value(document.getobj(objid)).copy()
else:
objid = obj.objid
tree = dict_value(obj).copy()
for (k, v) in parent.items():
if k in cls.INHERITABLE_ATTRS and k not in tree:
tree[k] = v
tree_type = tree.get('Type')
if tree_type is None and not settings.STRICT: # See #64
tree_type = tree.get('type')
if tree_type is LITERAL_PAGES and 'Kids' in tree:
log.info('Pages: Kids=%r', tree['Kids'])
for c in list_value(tree['Kids']):
for x in search(c, tree):
yield x
elif tree_type is LITERAL_PAGE:
log.info('Page: %r', tree)
yield (objid, tree)
pages = False
if 'Pages' in document.catalog:
for (objid, tree) in search(document.catalog['Pages'], document.catalog):
yield cls(document, objid, tree)
pages = True
if not pages:
# fallback when /Pages is missing.
for xref in document.xrefs:
for objid in xref.get_objids():
try:
obj = document.getobj(objid)
if isinstance(obj, dict) and obj.get('Type') is LITERAL_PAGE:
yield cls(document, objid, obj)
except PDFObjectNotFound:
pass
return
@classmethod
def get_pages(cls, fp,
pagenos=None, maxpages=0, password='',
caching=True, check_extractable=True):
# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
doc = PDFDocument(parser, password=password, caching=caching)
# Check if the document allows text extraction. If not, abort.
if check_extractable and not doc.is_extractable:
raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)
# Process each page contained in the document.
for (pageno, page) in enumerate(cls.create_pages(doc)):
if pagenos and (pageno not in pagenos):
continue
yield page
if maxpages and maxpages <= pageno+1:
break
return
-615
View File
@@ -1,615 +0,0 @@
import logging
import operator
from typing import List
from .PDFColorSpace import PDFColorSpace
from .PDFColorSpace import PREDEFINED_COLORSPACE
from .cmapdb import CMapDB
from .cmapdb import CMap
from .PDFStream import PDFException
from .PDFStream.PDFObjRef import PDFObjRef
from .PDFStream import resolve1, list_value, dict_value
from .PDFStream import PDFStream
from .PSStackParser import PSEOF
from .PSStackParser import PSKeyword
from .PSStackParser import literal_name
from .PSStackParser import keyword_name
from .PSStackParser import PSStackParser
from .PSStackParser import LIT
from .PDFGraphicState import PDFGraphicState
from .PDFTextState import PDFTextState
from .PDFContentParser import PDFContentParser
from ..utils import mult_matrix, MATRIX_IDENTITY
from .constants import LITERAL_FORM, LITERAL_IMAGE
from .types import CurvePoint, CurvePath
log = logging.getLogger(__name__)
## Exceptions
##
class PDFInterpreterError(PDFException):
pass
class PDFPageInterpreter(object):
def __init__(self, rsrcmgr, device):
self.rsrcmgr = rsrcmgr
self.device = device
self.curpath: List[CurvePath] = []
return
def dup(self):
return self.__class__(self.rsrcmgr, self.device)
# init_resources(resources):
# Prepare the fonts and XObjects listed in the Resource attribute.
def init_resources(self, resources):
self.resources = resources
self.fontmap = {}
self.xobjmap = {}
self.csmap = PREDEFINED_COLORSPACE.copy()
if not resources:
return
def get_colorspace(spec):
if isinstance(spec, list):
name = literal_name(spec[0])
else:
name = literal_name(spec)
if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
return PDFColorSpace(name, PDFStream.validated_stream(spec[1])['N'])
elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
return PDFColorSpace(name, len(list_value(spec[1])))
else:
return PREDEFINED_COLORSPACE.get(name)
for (k, v) in iter(dict_value(resources).items()):
log.debug('Resource: %r: %r', k, v)
if k == 'Font':
for (fontid, spec) in iter(dict_value(v).items()):
objid = None
if isinstance(spec, PDFObjRef):
objid = spec.objid
spec = dict_value(spec)
self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
elif k == 'ColorSpace':
for (csid, spec) in iter(dict_value(v).items()):
self.csmap[csid] = get_colorspace(resolve1(spec))
elif k == 'ProcSet':
self.rsrcmgr.get_procset(list_value(v))
elif k == 'XObject':
for (xobjid, xobjstrm) in iter(dict_value(v).items()):
self.xobjmap[xobjid] = xobjstrm
return
# init_state(ctm)
# Initialize the text and graphic states for rendering a page.
def init_state(self, ctm):
# gstack: stack for graphical states.
self.gstack = []
self.ctm = ctm
self.device.set_ctm(self.ctm)
self.textstate = PDFTextState()
self.graphicstate = PDFGraphicState()
self.curpath = []
# argstack: stack for command arguments.
self.argstack = []
# set some global states.
self.scs = self.ncs = None
if self.csmap:
self.scs = self.ncs = next(iter(self.csmap.values()))
return
def push(self, obj):
self.argstack.append(obj)
return
def pop(self, n):
if n == 0:
return []
x = self.argstack[-n:]
self.argstack = self.argstack[:-n]
return x
def get_current_state(self):
return (self.ctm, self.textstate.copy(), self.graphicstate.copy())
def set_current_state(self, state):
(self.ctm, self.textstate, self.graphicstate) = state
self.device.set_ctm(self.ctm)
return
# gsave
def do_q(self):
self.gstack.append(self.get_current_state())
return
# grestore
def do_Q(self):
if self.gstack:
self.set_current_state(self.gstack.pop())
return
# concat-matrix
def do_cm(self, a1, b1, c1, d1, e1, f1):
self.ctm = mult_matrix((a1, b1, c1, d1, e1, f1), self.ctm)
self.device.set_ctm(self.ctm)
return
# setlinewidth
def do_w(self, linewidth):
self.graphicstate.linewidth = linewidth
return
# setlinecap
def do_J(self, linecap):
self.graphicstate.linecap = linecap
return
# setlinejoin
def do_j(self, linejoin):
self.graphicstate.linejoin = linejoin
return
# setmiterlimit
def do_M(self, miterlimit):
self.graphicstate.miterlimit = miterlimit
return
# setdash
def do_d(self, dash, phase):
self.graphicstate.dash = (dash, phase)
return
# setintent
def do_ri(self, intent):
self.graphicstate.intent = intent
return
# setflatness
def do_i(self, flatness):
self.graphicstate.flatness = flatness
return
# load-gstate
def do_gs(self, name):
#XXX
return
# moveto
def do_m(self, x: float, y: float):
self.curpath.append(CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y)))
return
# lineto
def do_l(self, x: float, y: float):
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y)))
return
# curveto
def do_c(self, x1: float, y1: float, x2: float, y2: float, x3: float, y3: float):
self.curpath.append(CurvePath(
CurvePath.METHOD.CURVE_BOTH_TO,
CurvePoint(x1, y1),
CurvePoint(x2, y2),
CurvePoint(x3, y3),
))
return
# urveto
def do_v(self, x2: float, y2: float, x3: float, y3: float):
self.curpath.append(CurvePath(
CurvePath.METHOD.CURVE_NEXT_TO,
CurvePoint(x2, y2),
CurvePoint(x3, y3),
))
return
# rveto
def do_y(self, x1: float, y1: float, x3: float, y3: float):
self.curpath.append(CurvePath(
CurvePath.METHOD.CURVE_FIRST_TO,
CurvePoint(x1, y1),
CurvePoint(x3, y3)
))
return
# closepath
def do_h(self):
self.curpath.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
return
# rectangle
def do_re(self, x, y, w, h):
self.curpath.append(CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y) ))
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y) ))
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y+h) ))
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y+h) ))
self.curpath.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
return
# stroke
def do_S(self):
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
self.curpath = []
return
# close-and-stroke
def do_s(self):
self.do_h()
self.do_S()
return
# fill
def do_f(self):
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
self.curpath = []
return
# fill (obsolete)
do_F = do_f
# fill-even-odd
def do_f_a(self):
self.device.paint_path(self.graphicstate.copy(), True, self.curpath)
self.curpath = []
return
# fill-and-stroke
def do_B(self):
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
self.curpath = []
return
# fill-and-stroke-even-odd
def do_B_a(self):
self.device.paint_path(self.graphicstate.copy(), True, self.curpath)
self.curpath = []
return
# close-fill-and-stroke
def do_b(self):
self.do_h()
self.do_B()
return
# close-fill-and-stroke-even-odd
def do_b_a(self):
self.do_h()
self.do_B_a()
return
# close-only
def do_n(self):
self.curpath = []
return
# clip
def do_W(self):
return
# clip-even-odd
def do_W_a(self):
return
# setcolorspace-stroking
def do_CS(self, name):
try:
self.scs = self.csmap[literal_name(name)]
except KeyError:
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
return
# setcolorspace-non-strokine
def do_cs(self, name):
try:
self.ncs = self.csmap[literal_name(name)]
except KeyError:
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
return
# setgray-stroking
def do_G(self, gray):
self.graphicstate.scolor.gray = gray
#self.do_CS(LITERAL_DEVICE_GRAY)
return
# setgray-non-stroking
def do_g(self, gray):
self.graphicstate.ncolor.gray = gray
#self.do_cs(LITERAL_DEVICE_GRAY)
return
# setrgb-stroking
def do_RG(self, r, g, b):
self.graphicstate.scolor.rgb = (r*255, g*255, b*255)
#self.do_CS(LITERAL_DEVICE_RGB)
return
# setrgb-non-stroking
def do_rg(self, r, g, b):
self.graphicstate.ncolor.rgb = (r*255, g*255, b*255)
#self.do_cs(LITERAL_DEVICE_RGB)
return
# setcmyk-stroking
def do_K(self, c, m, y, k):
self.graphicstate.scolor.cmyk = (c, m, y, k)
#self.do_CS(LITERAL_DEVICE_CMYK)
return
# setcmyk-non-stroking
def do_k(self, c, m, y, k):
self.graphicstate.ncolor.cmyk = (c, m, y, k)
#self.do_cs(LITERAL_DEVICE_CMYK)
return
# setcolor
def do_SCN(self):
if self.scs:
n = self.scs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
return
def do_scn(self):
if self.ncs:
n = self.ncs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
return
def do_SC(self):
self.do_SCN()
return
def do_sc(self):
self.do_scn()
return
# sharing-name
def do_sh(self, name):
return
# begin-text
def do_BT(self):
self.textstate.reset()
return
# end-text
def do_ET(self):
return
# begin-compat
def do_BX(self):
return
# end-compat
def do_EX(self):
return
# marked content operators
def do_MP(self, tag):
self.device.do_tag(tag)
return
def do_DP(self, tag, props):
self.device.do_tag(tag, props)
return
def do_BMC(self, tag):
self.device.begin_tag(tag)
return
def do_BDC(self, tag, props):
self.device.begin_tag(tag, props)
return
def do_EMC(self):
self.device.end_tag()
return
# setcharspace
def do_Tc(self, space):
self.textstate.charspace = space
return
# setwordspace
def do_Tw(self, space):
self.textstate.wordspace = space
return
# def do_Th(self, )
# textscale
def do_Tz(self, scale):
self.textstate.scaling = scale
return
# setleading
def do_TL(self, leading):
self.textstate.leading = -leading
return
# selectfont
def do_Tf(self, fontid, fontsize):
try:
self.textstate.font = self.fontmap[literal_name(fontid)]
except KeyError:
raise PDFInterpreterError('Undefined Font id: %r' % fontid)
# self.textstate.font = self.rsrcmgr.get_font(None, {})
self.textstate.fontsize = fontsize
return
# setrendering
def do_Tr(self, render):
self.textstate.render = render
return
# settextrise
def do_Ts(self, rise):
self.textstate.rise = rise
return
# text-move
def do_Td(self, tx, ty):
(a, b, c, d, e, f) = self.textstate.matrix
self.textstate.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
self.textstate.linematrix = [0, 0]
return
# text-move
def do_TD(self, tx, ty):
(a, b, c, d, e, f) = self.textstate.matrix
self.textstate.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
self.textstate.leading = ty
self.textstate.linematrix = [0, 0]
return
# textmatrix
def do_Tm(self, a, b, c, d, e, f):
self.textstate.matrix = (a, b, c, d, e, f)
self.textstate.linematrix = [0, 0]
return
# nextline
def do_T_a(self):
(a, b, c, d, e, f) = self.textstate.matrix
self.textstate.matrix = (a, b, c, d, self.textstate.leading*c+e, self.textstate.leading*d+f)
self.textstate.linematrix = [0, 0]
return
# show-pos
def do_TJ(self, seq: bytearray):
if self.textstate.font is None:
raise PDFInterpreterError('No font specified!')
self.device.render_string(self.textstate.copy(), seq, self.ncs, self.graphicstate.copy())
return
# show
def do_Tj(self, s):
self.do_TJ([s])
return
# quote
def do__q(self, s):
self.do_T_a()
self.do_TJ([s])
return
# doublequote
def do__w(self, aw, ac, s):
self.do_Tw(aw)
self.do_Tc(ac)
self.do_TJ([s])
return
# inline image
def do_BI(self): # never called
return
def do_ID(self): # never called
return
def do_EI(self, obj: PDFStream):
if 'W' in obj and 'H' in obj:
iobjid = str(id(obj))
self.device.begin_figure(iobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
self.device.render_image(iobjid, obj)
self.device.end_figure(iobjid)
return
# invoke an XObject
def do_Do(self, xobjid):
xobjid = literal_name(xobjid)
try:
xobj = PDFStream.validated_stream(self.xobjmap[xobjid])
except KeyError:
raise PDFInterpreterError('Undefined xobject id: %r' % xobjid)
log.info('Processing xobj: %r', xobj)
subtype = xobj.get('Subtype')
if subtype is LITERAL_FORM and 'BBox' in xobj:
interpreter = self.dup()
bbox = list_value(xobj['BBox'])
matrix = list_value(xobj.get('Matrix', MATRIX_IDENTITY))
# According to PDF reference 1.7 section 4.9.1, XObjects in
# earlier PDFs (prior to v1.2) use the page's Resources entry
# instead of having their own Resources entry.
xobjres = xobj.get('Resources')
resources = dict_value(xobjres) if xobjres else self.resources.copy()
self.device.begin_figure(xobjid, bbox, matrix)
interpreter.render_contents(resources, [xobj], ctm=mult_matrix(matrix, self.ctm))
self.device.end_figure(xobjid)
elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
self.device.begin_figure(xobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
self.device.render_image(xobjid, xobj)
self.device.end_figure(xobjid)
else:
# unsupported xobject type.
pass
return
def process_page(self, page):
log.info('Processing page: %r', page)
(x0, y0, x1, y1) = page.mediabox
if page.rotate == 90:
ctm = (0, -1, 1, 0, -y0, x1)
elif page.rotate == 180:
ctm = (-1, 0, 0, -1, x1, y1)
elif page.rotate == 270:
ctm = (0, 1, -1, 0, y1, -x0)
else:
ctm = (1, 0, 0, 1, -x0, -y0)
self.device.begin_page(page, ctm)
self.render_contents(page.resources, page.contents, ctm=ctm)
self.device.end_page(page)
return
# render_contents(resources, streams, ctm)
# Render the content streams.
# This method may be called recursively.
def render_contents(self, resources, streams, ctm=MATRIX_IDENTITY):
log.info('render_contents: resources=%r, streams=%r, ctm=%r',
resources, streams, ctm)
self.init_resources(resources)
self.init_state(ctm)
self.execute(list_value(streams))
return
def execute(self, streams):
try:
parser = PDFContentParser(streams)
except PSEOF:
# empty page
return
while True:
try:
(_, obj) = parser.nextobject()
except PSEOF:
break
if isinstance(obj, PSKeyword):
name = keyword_name(obj)
method = 'do_%s' % name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
if hasattr(self, method):
func = getattr(self, method)
nargs = func.__code__.co_argcount-1
if nargs:
args = self.pop(nargs)
if len(args) == nargs:
func(*args)
else:
log.debug('exec: %s', name)
func()
else:
raise PDFInterpreterError('Unknown operator: %r' % name)
else:
self.push(obj)
return
-178
View File
@@ -1,178 +0,0 @@
import logging
from io import BytesIO, TextIOWrapper
from ..utils import settings
from .PSStackParser import PSStackParser
from .PSStackParser import PSSyntaxError
from .PSStackParser import PSEOF
from .PSStackParser import KWD
from .PDFStream import PDFException
from .PDFStream import PDFStream
from .PDFStream.PDFObjRef import PDFObjRef
from .PDFStream import int_value
from .PDFStream import dict_value
log = logging.getLogger(__name__)
## Exceptions
##
class PDFSyntaxError(PDFException):
pass
## PDFParser
##
class PDFParser(PSStackParser):
"""
PDFParser fetch PDF objects from a file stream.
It can handle indirect references by referring to
a PDF document set by set_document method.
It also reads XRefs at the end of every PDF file.
Typical usage:
parser = PDFParser(fp)
parser.read_xref()
parser.read_xref(fallback=True) # optional
parser.set_document(doc)
parser.seek(offset)
parser.nextobject()
"""
def __init__(self, fp: TextIOWrapper):
PSStackParser.__init__(self, fp)
self.doc = None
self.fallback = False
return
def set_document(self, doc):
"""Associates the parser with a PDFDocument object."""
self.doc = doc
return
KEYWORD_R = KWD(b'R')
KEYWORD_NULL = KWD(b'null')
KEYWORD_ENDOBJ = KWD(b'endobj')
KEYWORD_STREAM = KWD(b'stream')
KEYWORD_XREF = KWD(b'xref')
KEYWORD_STARTXREF = KWD(b'startxref')
def do_keyword(self, pos, token):
"""Handles PDF-related keywords."""
if token in (self.KEYWORD_XREF, self.KEYWORD_STARTXREF):
self.add_results(*self.pop(1))
elif token is self.KEYWORD_ENDOBJ:
self.add_results(*self.pop(4))
elif token is self.KEYWORD_NULL:
# null object
self.push((pos, None))
elif token is self.KEYWORD_R:
# reference to indirect object
try:
((_, objid), (_, genno)) = self.pop(2) # pylint: disable=E0632
(objid, genno) = (int(objid), int(genno)) # pylint: disable=E0632
obj = PDFObjRef(self.doc, objid, genno) # pylint: disable=E0632
self.push((pos, obj))
except PSSyntaxError:
pass
elif token is self.KEYWORD_STREAM:
# stream object
((_, dic),) = self.pop(1) # pylint: disable=E0632
dic = dict_value(dic)
objlen = 0
if not self.fallback:
try:
objlen = int_value(dic['Length'])
except KeyError:
if settings.STRICT:
raise PDFSyntaxError('/Length is undefined: %r' % dic)
self.seek(pos)
try:
(_, line) = self.nextline() # 'stream'
except PSEOF:
if settings.STRICT:
raise PDFSyntaxError('Unexpected EOF')
return
pos += len(line)
self.fp.seek(pos)
data = bytearray(self.fp.read(objlen))
self.seek(pos+objlen)
while 1:
try:
(_, line) = self.nextline()
except PSEOF:
if settings.STRICT:
raise PDFSyntaxError('Unexpected EOF')
break
if b'endstream' in line:
i = line.index(b'endstream')
objlen += i
if self.fallback:
data += line[:i]
break
objlen += len(line)
if self.fallback:
data += line
data = bytes(data)
self.seek(pos+objlen)
# XXX limit objlen not to exceed object boundary
log.debug('Stream: pos=%d, objlen=%d, dic=%r, data=%r...', pos, objlen, dic, data[:10])
obj = PDFStream(dic, data, self.doc.decipher)
self.push((pos, obj))
else:
# others
self.push((pos, token))
return
## PDFStreamParser
##
class PDFStreamParser(PDFParser):
"""
PDFStreamParser is used to parse PDF content streams
that is contained in each page and has instructions
for rendering the page. A reference to a PDF document is
needed because a PDF content stream can also have
indirect references to other objects in the same document.
"""
def __init__(self, data):
PDFParser.__init__(self, BytesIO(data))
return
def flush(self):
self.add_results(*self.popall())
return
KEYWORD_OBJ = KWD(b'obj')
def do_keyword(self, pos, token):
if token is self.KEYWORD_R:
# reference to indirect object
try:
((_, objid), (_, genno)) = self.pop(2) # pylint: disable=E0632
(objid, genno) = (int(objid), int(genno))
obj = PDFObjRef(self.doc, objid, genno)
self.push((pos, obj))
except PSSyntaxError:
pass
return
elif token in (self.KEYWORD_OBJ, self.KEYWORD_ENDOBJ):
if settings.STRICT:
# See PDF Spec 3.4.6: Only the object values are stored in the
# stream; the obj and endobj keywords are not used.
raise PDFSyntaxError('Keyword endobj found in stream')
return
# others
self.push((pos, token))
return
-103
View File
@@ -1,103 +0,0 @@
import logging
from .cmapdb import CMapDB, CMap
from .PDFFont import PDFFontError
from .PDFFont import PDFType1Font
from .PDFFont import PDFTrueTypeFont
from .PDFFont import PDFType3Font
from .PDFFont import PDFCIDFont
from .PDFStream import PDFException
from .PDFStream import resolve1, dict_value, list_value
from .constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
from .PSStackParser import literal_name, settings
log = logging.getLogger(__name__)
## Exceptions
##
class PDFResourceError(PDFException):
pass
class PDFInterpreterError(PDFException):
pass
## Resource Manager
##
class PDFResourceManager(object):
"""Repository of shared resources.
ResourceManager facilitates reuse of shared resources
such as fonts and images so that large objects are not
allocated multiple times.
"""
def __init__(self, caching=True):
self.caching = caching
self._cached_fonts = {}
return
def get_procset(self, procs):
for proc in procs:
if proc is LITERAL_PDF:
pass
elif proc is LITERAL_TEXT:
pass
else:
#raise PDFResourceError('ProcSet %r is not supported.' % proc)
pass
return
def get_cmap(self, cmapname, strict=False):
try:
return CMapDB.get_cmap(cmapname)
except CMapDB.CMapNotFound:
if strict:
raise
return CMap()
def get_font(self, objid: int, spec: dict):
if objid and objid in self._cached_fonts:
font = self._cached_fonts[objid]
else:
log.info('get_font: create: objid=%r, spec=%r', objid, spec)
if settings.STRICT:
if spec['Type'] is not LITERAL_FONT:
raise PDFFontError('Type is not /Font')
# Create a Font object.
if 'Subtype' in spec:
subtype = literal_name(spec['Subtype'])
else:
if settings.STRICT:
raise PDFFontError('Font Subtype is not specified.')
subtype = 'Type1'
if subtype in ('Type1', 'MMType1'):
# Type1 Font
font = PDFType1Font(self, spec)
elif subtype == 'TrueType':
# TrueType Font
font = PDFTrueTypeFont(self, spec)
elif subtype == 'Type3':
# Type3 Font
font = PDFType3Font(self, spec)
elif subtype in ('CIDFontType0', 'CIDFontType2'):
# CID Font
font = PDFCIDFont(self, spec)
elif subtype == 'Type0':
# Type0 Font
dfonts = list_value(spec['DescendantFonts'])
assert dfonts
subspec = dict_value(dfonts[0]).copy()
for k in ('Encoding', 'ToUnicode'):
if k in spec:
subspec[k] = resolve1(spec[k])
font = self.get_font(None, subspec)
else:
if settings.STRICT:
raise PDFFontError('Invalid Font spec: %r' % spec)
font = PDFType1Font(self, spec) # this is so wrong!
if objid and self.caching:
self._cached_fonts[objid] = font
return font
@@ -1,32 +0,0 @@
import zlib
from ..PSStackParser import PSException
from ..PSStackParser import PSObject
from ...utils import lzwdecode, settings
from .constants import * # pylint: disable=W0614
from .types import * # pylint: disable=W0614
from .exceptions import * # pylint: disable=W0614
## PDFObjRef
##
class PDFObjRef(PDFObject):
def __init__(self, doc, objid, _):
if objid == 0:
if settings.STRICT:
raise PDFValueError('PDF object id cannot be 0.')
self.doc = doc
self.objid = objid
#self.genno = genno # Never used.
return
def __repr__(self):
return '<PDFObjRef:%d>' % (self.objid)
def resolve(self, default=None):
try:
return self.doc.getobj(self.objid)
except PDFObjectNotFound:
return default
-144
View File
@@ -1,144 +0,0 @@
import zlib
from ...utils import lzwdecode
from ...utils import ascii85decode, asciihexdecode, apply_png_predictor
from .constants import *
from .types import *
from .exceptions import *
from .util import *
from .ccitt import ccittfaxdecode
class PDFStream(PDFObject):
@classmethod
def validated_stream(cls, x):
x = resolve1(x)
if not isinstance(x, cls):
if settings.STRICT:
raise PDFTypeError('PDFStream required: %r' % x)
return PDFStream({}, b'')
return x
def __init__(self, attrs, rawdata, decipher=None):
assert isinstance(attrs, dict), str(type(attrs))
self.attrs = attrs
self.rawdata = rawdata
self.decipher = decipher
self.data = None
self.objid = None
self.genno = None
return
def set_objid(self, objid, genno):
self.objid = objid
self.genno = genno
return
def __repr__(self):
if self.data is None:
assert self.rawdata is not None
return '<PDFStream(%r): raw=%d, %r>' % (self.objid, len(self.rawdata), self.attrs)
else:
assert self.data is not None
return '<PDFStream(%r): len=%d, %r>' % (self.objid, len(self.data), self.attrs)
def __contains__(self, name):
return name in self.attrs
def __getitem__(self, name):
return self.attrs[name]
def get(self, name, default=None):
return self.attrs.get(name, default)
def get_any(self, names, default=None):
for name in names:
if name in self.attrs:
return self.attrs[name]
return default
def get_filters(self):
filters = self.get_any(('F', 'Filter'))
params = self.get_any(('DP', 'DecodeParms', 'FDecodeParms'), {})
if not filters:
return []
if not isinstance(filters, list):
filters = [filters]
if not isinstance(params, list):
# Make sure the parameters list is the same as filters.
params = [params] * len(filters)
if settings.STRICT and len(params) != len(filters):
raise PDFException("Parameters len filter mismatch")
# resolve filter if possible
_filters = []
for fltr in filters:
if hasattr(fltr, 'resolve'):
fltr = fltr.resolve()[0]
_filters.append(fltr)
return list(zip(_filters, params)) #solves https://github.com/pdfminer/pdfminer.six/issues/15
def decode(self):
assert self.data is None and self.rawdata is not None, str((self.data, self.rawdata))
data = self.rawdata
if self.decipher:
# Handle encryption
data = self.decipher(self.objid, self.genno, data, self.attrs)
filters = self.get_filters()
if not filters:
self.data = data
self.rawdata = None
return
for (f,params) in filters:
if f in LITERALS_FLATE_DECODE:
# will get errors if the document is encrypted.
try:
data = zlib.decompress(data)
except zlib.error as e:
if settings.STRICT:
raise PDFException('Invalid zlib bytes: %r, %r' % (e, data))
data = b''
elif f in LITERALS_LZW_DECODE:
data = lzwdecode(data)
elif f in LITERALS_ASCII85_DECODE:
data = ascii85decode(data)
elif f in LITERALS_ASCIIHEX_DECODE:
data = asciihexdecode(data)
elif f in LITERALS_RUNLENGTH_DECODE:
data = rldecode(data)
elif f in LITERALS_CCITTFAX_DECODE:
data = ccittfaxdecode(data, params)
elif f in LITERALS_DCT_DECODE:
# This is probably a JPG stream - it does not need to be decoded twice.
# Just return the stream to the user.
pass
elif f == LITERAL_CRYPT:
# not yet..
raise PDFNotImplementedError('/Crypt filter is unsupported')
else:
raise PDFNotImplementedError('Unsupported filter: %r' % f)
# apply predictors
if params and 'Predictor' in params:
pred = int_value(params['Predictor'])
if pred == 1:
# no predictor
pass
elif 10 <= pred:
# PNG predictor
colors = int_value(params.get('Colors', 1))
columns = int_value(params.get('Columns', 1))
bitspercomponent = int_value(params.get('BitsPerComponent', 8))
data = apply_png_predictor(pred, colors, columns, bitspercomponent, data)
else:
raise PDFNotImplementedError('Unsupported predictor: %r' % pred)
self.data = data
self.rawdata = None
return
def get_data(self):
if self.data is None:
self.decode()
return self.data
def get_rawdata(self):
return self.rawdata
-604
View File
@@ -1,604 +0,0 @@
# CCITT Fax decoder
#
# Bugs: uncompressed mode untested.
#
# cf.
# ITU-T Recommendation T.4
# "Standardization of Group 3 facsimile terminals for document transmission"
# ITU-T Recommendation T.6
# "FACSIMILE CODING SCHEMES AND CODING CONTROL FUNCTIONS FOR GROUP 4 FACSIMILE APPARATUS"
import sys
import array
from abc import abstractmethod
def get_bytes(data):
for byte in data:
yield byte
## BitParser
##
class BitParser(object):
def __init__(self):
self._pos = 0
self._state = None
return
@classmethod
def add(klass, root, v, bits):
p = root
b = None
for i in range(len(bits)):
if 0 < i:
if p[b] is None:
p[b] = [None, None]
p = p[b]
if bits[i] == '1':
b = 1
else:
b = 0
p[b] = v
return
def feedbytes(self, data):
for byte in get_bytes(data):
for m in (128, 64, 32, 16, 8, 4, 2, 1):
self._parse_bit(byte & m)
return
def _parse_bit(self, x):
if x:
v = self._state[1]
else:
v = self._state[0]
self._pos += 1
if isinstance(v, list):
self._state = v
else:
self._state = self._accept(v)
return
@abstractmethod
def _accept(self, v):
raise NotImplementedError
## CCITTG4Parser
##
class CCITTG4Parser(BitParser):
MODE = [None, None]
BitParser.add(MODE, 0, '1')
BitParser.add(MODE, +1, '011')
BitParser.add(MODE, -1, '010')
BitParser.add(MODE, 'h', '001')
BitParser.add(MODE, 'p', '0001')
BitParser.add(MODE, +2, '000011')
BitParser.add(MODE, -2, '000010')
BitParser.add(MODE, +3, '0000011')
BitParser.add(MODE, -3, '0000010')
BitParser.add(MODE, 'u', '0000001111')
BitParser.add(MODE, 'x1', '0000001000')
BitParser.add(MODE, 'x2', '0000001001')
BitParser.add(MODE, 'x3', '0000001010')
BitParser.add(MODE, 'x4', '0000001011')
BitParser.add(MODE, 'x5', '0000001100')
BitParser.add(MODE, 'x6', '0000001101')
BitParser.add(MODE, 'x7', '0000001110')
BitParser.add(MODE, 'e', '000000000001000000000001')
WHITE = [None, None]
BitParser.add(WHITE, 0 , '00110101')
BitParser.add(WHITE, 1 , '000111')
BitParser.add(WHITE, 2 , '0111')
BitParser.add(WHITE, 3 , '1000')
BitParser.add(WHITE, 4 , '1011')
BitParser.add(WHITE, 5 , '1100')
BitParser.add(WHITE, 6 , '1110')
BitParser.add(WHITE, 7 , '1111')
BitParser.add(WHITE, 8 , '10011')
BitParser.add(WHITE, 9 , '10100')
BitParser.add(WHITE, 10 , '00111')
BitParser.add(WHITE, 11 , '01000')
BitParser.add(WHITE, 12 , '001000')
BitParser.add(WHITE, 13 , '000011')
BitParser.add(WHITE, 14 , '110100')
BitParser.add(WHITE, 15 , '110101')
BitParser.add(WHITE, 16 , '101010')
BitParser.add(WHITE, 17 , '101011')
BitParser.add(WHITE, 18 , '0100111')
BitParser.add(WHITE, 19 , '0001100')
BitParser.add(WHITE, 20 , '0001000')
BitParser.add(WHITE, 21 , '0010111')
BitParser.add(WHITE, 22 , '0000011')
BitParser.add(WHITE, 23 , '0000100')
BitParser.add(WHITE, 24 , '0101000')
BitParser.add(WHITE, 25 , '0101011')
BitParser.add(WHITE, 26 , '0010011')
BitParser.add(WHITE, 27 , '0100100')
BitParser.add(WHITE, 28 , '0011000')
BitParser.add(WHITE, 29 , '00000010')
BitParser.add(WHITE, 30 , '00000011')
BitParser.add(WHITE, 31 , '00011010')
BitParser.add(WHITE, 32 , '00011011')
BitParser.add(WHITE, 33 , '00010010')
BitParser.add(WHITE, 34 , '00010011')
BitParser.add(WHITE, 35 , '00010100')
BitParser.add(WHITE, 36 , '00010101')
BitParser.add(WHITE, 37 , '00010110')
BitParser.add(WHITE, 38 , '00010111')
BitParser.add(WHITE, 39 , '00101000')
BitParser.add(WHITE, 40 , '00101001')
BitParser.add(WHITE, 41 , '00101010')
BitParser.add(WHITE, 42 , '00101011')
BitParser.add(WHITE, 43 , '00101100')
BitParser.add(WHITE, 44 , '00101101')
BitParser.add(WHITE, 45 , '00000100')
BitParser.add(WHITE, 46 , '00000101')
BitParser.add(WHITE, 47 , '00001010')
BitParser.add(WHITE, 48 , '00001011')
BitParser.add(WHITE, 49 , '01010010')
BitParser.add(WHITE, 50 , '01010011')
BitParser.add(WHITE, 51 , '01010100')
BitParser.add(WHITE, 52 , '01010101')
BitParser.add(WHITE, 53 , '00100100')
BitParser.add(WHITE, 54 , '00100101')
BitParser.add(WHITE, 55 , '01011000')
BitParser.add(WHITE, 56 , '01011001')
BitParser.add(WHITE, 57 , '01011010')
BitParser.add(WHITE, 58 , '01011011')
BitParser.add(WHITE, 59 , '01001010')
BitParser.add(WHITE, 60 , '01001011')
BitParser.add(WHITE, 61 , '00110010')
BitParser.add(WHITE, 62 , '00110011')
BitParser.add(WHITE, 63 , '00110100')
BitParser.add(WHITE, 64 , '11011')
BitParser.add(WHITE, 128 , '10010')
BitParser.add(WHITE, 192 , '010111')
BitParser.add(WHITE, 256 , '0110111')
BitParser.add(WHITE, 320 , '00110110')
BitParser.add(WHITE, 384 , '00110111')
BitParser.add(WHITE, 448 , '01100100')
BitParser.add(WHITE, 512 , '01100101')
BitParser.add(WHITE, 576 , '01101000')
BitParser.add(WHITE, 640 , '01100111')
BitParser.add(WHITE, 704 , '011001100')
BitParser.add(WHITE, 768 , '011001101')
BitParser.add(WHITE, 832 , '011010010')
BitParser.add(WHITE, 896 , '011010011')
BitParser.add(WHITE, 960 , '011010100')
BitParser.add(WHITE, 1024, '011010101')
BitParser.add(WHITE, 1088, '011010110')
BitParser.add(WHITE, 1152, '011010111')
BitParser.add(WHITE, 1216, '011011000')
BitParser.add(WHITE, 1280, '011011001')
BitParser.add(WHITE, 1344, '011011010')
BitParser.add(WHITE, 1408, '011011011')
BitParser.add(WHITE, 1472, '010011000')
BitParser.add(WHITE, 1536, '010011001')
BitParser.add(WHITE, 1600, '010011010')
BitParser.add(WHITE, 1664, '011000')
BitParser.add(WHITE, 1728, '010011011')
BitParser.add(WHITE, 1792, '00000001000')
BitParser.add(WHITE, 1856, '00000001100')
BitParser.add(WHITE, 1920, '00000001101')
BitParser.add(WHITE, 1984, '000000010010')
BitParser.add(WHITE, 2048, '000000010011')
BitParser.add(WHITE, 2112, '000000010100')
BitParser.add(WHITE, 2176, '000000010101')
BitParser.add(WHITE, 2240, '000000010110')
BitParser.add(WHITE, 2304, '000000010111')
BitParser.add(WHITE, 2368, '000000011100')
BitParser.add(WHITE, 2432, '000000011101')
BitParser.add(WHITE, 2496, '000000011110')
BitParser.add(WHITE, 2560, '000000011111')
BLACK = [None, None]
BitParser.add(BLACK, 0 , '0000110111')
BitParser.add(BLACK, 1 , '010')
BitParser.add(BLACK, 2 , '11')
BitParser.add(BLACK, 3 , '10')
BitParser.add(BLACK, 4 , '011')
BitParser.add(BLACK, 5 , '0011')
BitParser.add(BLACK, 6 , '0010')
BitParser.add(BLACK, 7 , '00011')
BitParser.add(BLACK, 8 , '000101')
BitParser.add(BLACK, 9 , '000100')
BitParser.add(BLACK, 10 , '0000100')
BitParser.add(BLACK, 11 , '0000101')
BitParser.add(BLACK, 12 , '0000111')
BitParser.add(BLACK, 13 , '00000100')
BitParser.add(BLACK, 14 , '00000111')
BitParser.add(BLACK, 15 , '000011000')
BitParser.add(BLACK, 16 , '0000010111')
BitParser.add(BLACK, 17 , '0000011000')
BitParser.add(BLACK, 18 , '0000001000')
BitParser.add(BLACK, 19 , '00001100111')
BitParser.add(BLACK, 20 , '00001101000')
BitParser.add(BLACK, 21 , '00001101100')
BitParser.add(BLACK, 22 , '00000110111')
BitParser.add(BLACK, 23 , '00000101000')
BitParser.add(BLACK, 24 , '00000010111')
BitParser.add(BLACK, 25 , '00000011000')
BitParser.add(BLACK, 26 , '000011001010')
BitParser.add(BLACK, 27 , '000011001011')
BitParser.add(BLACK, 28 , '000011001100')
BitParser.add(BLACK, 29 , '000011001101')
BitParser.add(BLACK, 30 , '000001101000')
BitParser.add(BLACK, 31 , '000001101001')
BitParser.add(BLACK, 32 , '000001101010')
BitParser.add(BLACK, 33 , '000001101011')
BitParser.add(BLACK, 34 , '000011010010')
BitParser.add(BLACK, 35 , '000011010011')
BitParser.add(BLACK, 36 , '000011010100')
BitParser.add(BLACK, 37 , '000011010101')
BitParser.add(BLACK, 38 , '000011010110')
BitParser.add(BLACK, 39 , '000011010111')
BitParser.add(BLACK, 40 , '000001101100')
BitParser.add(BLACK, 41 , '000001101101')
BitParser.add(BLACK, 42 , '000011011010')
BitParser.add(BLACK, 43 , '000011011011')
BitParser.add(BLACK, 44 , '000001010100')
BitParser.add(BLACK, 45 , '000001010101')
BitParser.add(BLACK, 46 , '000001010110')
BitParser.add(BLACK, 47 , '000001010111')
BitParser.add(BLACK, 48 , '000001100100')
BitParser.add(BLACK, 49 , '000001100101')
BitParser.add(BLACK, 50 , '000001010010')
BitParser.add(BLACK, 51 , '000001010011')
BitParser.add(BLACK, 52 , '000000100100')
BitParser.add(BLACK, 53 , '000000110111')
BitParser.add(BLACK, 54 , '000000111000')
BitParser.add(BLACK, 55 , '000000100111')
BitParser.add(BLACK, 56 , '000000101000')
BitParser.add(BLACK, 57 , '000001011000')
BitParser.add(BLACK, 58 , '000001011001')
BitParser.add(BLACK, 59 , '000000101011')
BitParser.add(BLACK, 60 , '000000101100')
BitParser.add(BLACK, 61 , '000001011010')
BitParser.add(BLACK, 62 , '000001100110')
BitParser.add(BLACK, 63 , '000001100111')
BitParser.add(BLACK, 64 , '0000001111')
BitParser.add(BLACK, 128 , '000011001000')
BitParser.add(BLACK, 192 , '000011001001')
BitParser.add(BLACK, 256 , '000001011011')
BitParser.add(BLACK, 320 , '000000110011')
BitParser.add(BLACK, 384 , '000000110100')
BitParser.add(BLACK, 448 , '000000110101')
BitParser.add(BLACK, 512 , '0000001101100')
BitParser.add(BLACK, 576 , '0000001101101')
BitParser.add(BLACK, 640 , '0000001001010')
BitParser.add(BLACK, 704 , '0000001001011')
BitParser.add(BLACK, 768 , '0000001001100')
BitParser.add(BLACK, 832 , '0000001001101')
BitParser.add(BLACK, 896 , '0000001110010')
BitParser.add(BLACK, 960 , '0000001110011')
BitParser.add(BLACK, 1024, '0000001110100')
BitParser.add(BLACK, 1088, '0000001110101')
BitParser.add(BLACK, 1152, '0000001110110')
BitParser.add(BLACK, 1216, '0000001110111')
BitParser.add(BLACK, 1280, '0000001010010')
BitParser.add(BLACK, 1344, '0000001010011')
BitParser.add(BLACK, 1408, '0000001010100')
BitParser.add(BLACK, 1472, '0000001010101')
BitParser.add(BLACK, 1536, '0000001011010')
BitParser.add(BLACK, 1600, '0000001011011')
BitParser.add(BLACK, 1664, '0000001100100')
BitParser.add(BLACK, 1728, '0000001100101')
BitParser.add(BLACK, 1792, '00000001000')
BitParser.add(BLACK, 1856, '00000001100')
BitParser.add(BLACK, 1920, '00000001101')
BitParser.add(BLACK, 1984, '000000010010')
BitParser.add(BLACK, 2048, '000000010011')
BitParser.add(BLACK, 2112, '000000010100')
BitParser.add(BLACK, 2176, '000000010101')
BitParser.add(BLACK, 2240, '000000010110')
BitParser.add(BLACK, 2304, '000000010111')
BitParser.add(BLACK, 2368, '000000011100')
BitParser.add(BLACK, 2432, '000000011101')
BitParser.add(BLACK, 2496, '000000011110')
BitParser.add(BLACK, 2560, '000000011111')
UNCOMPRESSED = [None, None]
BitParser.add(UNCOMPRESSED, '1', '1')
BitParser.add(UNCOMPRESSED, '01', '01')
BitParser.add(UNCOMPRESSED, '001', '001')
BitParser.add(UNCOMPRESSED, '0001', '0001')
BitParser.add(UNCOMPRESSED, '00001', '00001')
BitParser.add(UNCOMPRESSED, '00000', '000001')
BitParser.add(UNCOMPRESSED, 'T00', '00000011')
BitParser.add(UNCOMPRESSED, 'T10', '00000010')
BitParser.add(UNCOMPRESSED, 'T000', '000000011')
BitParser.add(UNCOMPRESSED, 'T100', '000000010')
BitParser.add(UNCOMPRESSED, 'T0000', '0000000011')
BitParser.add(UNCOMPRESSED, 'T1000', '0000000010')
BitParser.add(UNCOMPRESSED, 'T00000', '00000000011')
BitParser.add(UNCOMPRESSED, 'T10000', '00000000010')
class EOFB(Exception):
pass
class InvalidData(Exception):
pass
class ByteSkip(Exception):
pass
def __init__(self, width, bytealign=False):
BitParser.__init__(self)
self.width = width
self.bytealign = bytealign
self.reset()
return
def feedbytes(self, data):
for byte in get_bytes(data):
try:
for m in (128, 64, 32, 16, 8, 4, 2, 1):
self._parse_bit(byte & m)
except self.ByteSkip:
self._accept = self._parse_mode
self._state = self.MODE
except self.EOFB:
break
return
def _parse_mode(self, mode):
if mode == 'p':
self._do_pass()
self._flush_line()
return self.MODE
elif mode == 'h':
self._n1 = 0
self._accept = self._parse_horiz1
if self._color:
return self.WHITE
else:
return self.BLACK
elif mode == 'u':
self._accept = self._parse_uncompressed
return self.UNCOMPRESSED
elif mode == 'e':
raise self.EOFB
elif isinstance(mode, int):
self._do_vertical(mode)
self._flush_line()
return self.MODE
else:
raise self.InvalidData(mode)
def _parse_horiz1(self, n):
if n is None:
raise self.InvalidData
self._n1 += n
if n < 64:
self._n2 = 0
self._color = 1-self._color
self._accept = self._parse_horiz2
if self._color:
return self.WHITE
else:
return self.BLACK
def _parse_horiz2(self, n):
if n is None:
raise self.InvalidData
self._n2 += n
if n < 64:
self._color = 1-self._color
self._accept = self._parse_mode
self._do_horizontal(self._n1, self._n2)
self._flush_line()
return self.MODE
elif self._color:
return self.WHITE
else:
return self.BLACK
def _parse_uncompressed(self, bits):
if not bits:
raise self.InvalidData
if bits.startswith('T'):
self._accept = self._parse_mode
self._color = int(bits[1])
self._do_uncompressed(bits[2:])
return self.MODE
else:
self._do_uncompressed(bits)
return self.UNCOMPRESSED
def _get_bits(self):
return ''.join(str(b) for b in self._curline[:self._curpos])
def _get_refline(self, i):
if i < 0:
return '[]'+''.join(str(b) for b in self._refline)
elif len(self._refline) <= i:
return ''.join(str(b) for b in self._refline)+'[]'
else:
return (''.join(str(b) for b in self._refline[:i]) +
'['+str(self._refline[i])+']' +
''.join(str(b) for b in self._refline[i+1:]))
def reset(self):
self._y = 0
self._curline = array.array('b', [1]*self.width)
self._reset_line()
self._accept = self._parse_mode
self._state = self.MODE
return
def output_line(self, y, bits):
print (y, ''.join(str(b) for b in bits))
return
def _reset_line(self):
self._refline = self._curline
self._curline = array.array('b', [1]*self.width)
self._curpos = -1
self._color = 1
return
def _flush_line(self):
if self.width <= self._curpos:
self.output_line(self._y, self._curline)
self._y += 1
self._reset_line()
if self.bytealign:
raise self.ByteSkip
return
def _do_vertical(self, dx):
x1 = self._curpos+1
while 1:
if x1 == 0:
if (self._color == 1 and self._refline[x1] != self._color):
break
elif x1 == len(self._refline):
break
elif (self._refline[x1-1] == self._color and
self._refline[x1] != self._color):
break
x1 += 1
x1 += dx
x0 = max(0, self._curpos)
x1 = max(0, min(self.width, x1))
if x1 < x0:
for x in range(x1, x0):
self._curline[x] = self._color
elif x0 < x1:
for x in range(x0, x1):
self._curline[x] = self._color
self._curpos = x1
self._color = 1-self._color
return
def _do_pass(self):
x1 = self._curpos+1
while 1:
if x1 == 0:
if (self._color == 1 and self._refline[x1] != self._color):
break
elif x1 == len(self._refline):
break
elif (self._refline[x1-1] == self._color and
self._refline[x1] != self._color):
break
x1 += 1
while 1:
if x1 == 0:
if (self._color == 0 and self._refline[x1] == self._color):
break
elif x1 == len(self._refline):
break
elif (self._refline[x1-1] != self._color and
self._refline[x1] == self._color):
break
x1 += 1
for x in range(self._curpos, x1):
self._curline[x] = self._color
self._curpos = x1
return
def _do_horizontal(self, n1, n2):
if self._curpos < 0:
self._curpos = 0
x = self._curpos
for _ in range(n1):
if len(self._curline) <= x:
break
self._curline[x] = self._color
x += 1
for _ in range(n2):
if len(self._curline) <= x:
break
self._curline[x] = 1-self._color
x += 1
self._curpos = x
return
def _do_uncompressed(self, bits):
for c in bits:
self._curline[self._curpos] = int(c)
self._curpos += 1
self._flush_line()
return
class CCITTFaxDecoder(CCITTG4Parser):
def __init__(self, width, bytealign=False, reversed=False):
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
self.reversed = reversed
self._buf = b''
return
def close(self):
return self._buf
def output_line(self, y, bits):
bytes = array.array('B', [0]*((len(bits)+7)//8))
if self.reversed:
bits = [1-b for b in bits]
for (i, b) in enumerate(bits):
if b:
bytes[i//8] += (128, 64, 32, 16, 8, 4, 2, 1)[i % 8]
self._buf += bytes.tostring()
return
def ccittfaxdecode(data, params):
K = params.get('K')
cols = params.get('Columns')
bytealign = params.get('EncodedByteAlign')
reversed = params.get('BlackIs1')
if K == -1:
parser = CCITTFaxDecoder(cols, bytealign=bytealign, reversed=reversed)
else:
raise ValueError(K)
parser.feedbytes(data)
return parser.close()
# test
def main(argv):
if not argv[1:]:
import unittest
return unittest.main()
class Parser(CCITTG4Parser):
def __init__(self, width, bytealign=False):
import pygame
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
self.img = pygame.Surface((self.width, 1000))
return
def output_line(self, y, bits):
for (x, b) in enumerate(bits):
if b:
self.img.set_at((x, y), (255, 255, 255))
else:
self.img.set_at((x, y), (0, 0, 0))
return
def close(self):
import pygame
pygame.image.save(self.img, 'out.bmp')
return
for path in argv[1:]:
fp = open(path, 'rb')
(_, _, _, w, _, _) = path.split('.')
parser = Parser(int(w))
parser.feedbytes(fp.read())
parser.close()
fp.close()
return
if __name__ == '__main__':
sys.exit(main(sys.argv))
@@ -1,13 +0,0 @@
from ...utils import settings
from ..PSStackParser import LIT
LITERAL_CRYPT = LIT('Crypt')
# Abbreviation of Filter names in PDF 4.8.6. "Inline Images"
LITERALS_FLATE_DECODE = (LIT('FlateDecode'), LIT('Fl'))
LITERALS_LZW_DECODE = (LIT('LZWDecode'), LIT('LZW'))
LITERALS_ASCII85_DECODE = (LIT('ASCII85Decode'), LIT('A85'))
LITERALS_ASCIIHEX_DECODE = (LIT('ASCIIHexDecode'), LIT('AHx'))
LITERALS_RUNLENGTH_DECODE = (LIT('RunLengthDecode'), LIT('RL'))
LITERALS_CCITTFAX_DECODE = (LIT('CCITTFaxDecode'), LIT('CCF'))
LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
@@ -1,16 +0,0 @@
from ..PSStackParser import PSException
class PDFException(PSException):
pass
class PDFTypeError(PDFException):
pass
class PDFValueError(PDFException):
pass
class PDFObjectNotFound(PDFException):
pass
class PDFNotImplementedError(PDFException):
pass
-6
View File
@@ -1,6 +0,0 @@
from ..PSStackParser import PSObject
## PDF Objects
##
class PDFObject(PSObject):
pass
-139
View File
@@ -1,139 +0,0 @@
from .PDFObjRef import PDFObjRef
from ...utils import settings, isnumber, int2byte
from .exceptions import PDFTypeError
from logging import getLogger
log = getLogger(__name__)
# resolve
def resolve1(x, default=None):
"""Resolves an object.
If this is an array or dictionary, it may still contains
some indirect objects inside.
"""
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
return x
def resolve_all(x, default=None):
"""Recursively resolves the given object and all the internals.
Make sure there is no indirect reference within the nested object.
This procedure might be slow.
"""
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
if isinstance(x, list):
x = [resolve_all(v, default=default) for v in x]
elif isinstance(x, dict):
for (k, v) in x.iteritems():
x[k] = resolve_all(v, default=default)
return x
def decipher_all(decipher, objid, genno, x):
"""Recursively deciphers the given object.
"""
if isinstance(x, bytes):
return decipher(objid, genno, x)
if isinstance(x, list):
x = [decipher_all(decipher, objid, genno, v) for v in x]
elif isinstance(x, dict):
for (k, v) in x.items():
x[k] = decipher_all(decipher, objid, genno, v)
return x
# Type cheking
def int_value(x):
x = resolve1(x)
if not isinstance(x, int):
if settings.STRICT:
raise PDFTypeError('Integer required: %r' % x)
return 0
return x
def float_value(x):
x = resolve1(x)
if not isinstance(x, float):
if settings.STRICT:
raise PDFTypeError('Float required: %r' % x)
return 0.0
return x
def num_value(x):
x = resolve1(x)
if not isnumber(x):
if settings.STRICT:
raise PDFTypeError('Int or Float required: %r' % x)
return 0
return x
def str_value(x):
x = resolve1(x)
if not isinstance(x, bytes):
if settings.STRICT:
raise PDFTypeError('String required: %r' % x)
return ''
return x
def list_value(x):
x = resolve1(x)
if not isinstance(x, (list, tuple)):
if settings.STRICT:
raise PDFTypeError('List required: %r' % x)
return []
return x
def dict_value(x):
x = resolve1(x)
if not isinstance(x, dict):
if settings.STRICT:
log.error('PDFTypeError : Dict required: %r', x)
raise PDFTypeError('Dict required: %r' % x)
return {}
return x
#
# RunLength decoder (Adobe version) implementation based on PDF Reference
# version 1.4 section 3.3.4.
#
# * public domain *
#
def rldecode(data):
"""
RunLength decoder (Adobe version) implementation based on PDF Reference
version 1.4 section 3.3.4:
The RunLengthDecode filter decodes data that has been encoded in a
simple byte-oriented format based on run length. The encoded data
is a sequence of runs, where each run consists of a length byte
followed by 1 to 128 bytes of data. If the length byte is in the
range 0 to 127, the following length + 1 (1 to 128) bytes are
copied literally during decompression. If length is in the range
129 to 255, the following single byte is to be copied 257 - length
(2 to 128) times during decompression. A length value of 128
denotes EOD.
"""
decoded = b''
i = 0
while i < len(data):
length = data[i]
if length == 128:
break
if length >= 0 and length < 128:
for j in range(i+1,(i+1)+(length+1)):
decoded+=int2byte(data[j])
i = (i+1) + (length+1)
if length > 128:
run = int2byte(data[i+1])*(257-length)
decoded+=run
i = (i+1) + 1
return decoded
-53
View File
@@ -1,53 +0,0 @@
from .PDFFont import PDFFont
from ..utils import MATRIX_IDENTITY
## PDFTextState
##
class PDFTextState(object):
def __init__(self):
self.font: PDFFont = None
self.fontsize: int = 0
self.charspace: int = 0
self.wordspace: int = 0
self.scaling: int = 100
self.leading: int = 0
self.render: int = 0
self.rise: int = 0
self.reset()
# self.matrix is set
# self.linematrix is set
return
def __repr__(self):
return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%r, '
' scaling=%r, leading=%r, render=%r, rise=%r, '
' matrix=%r, linematrix=%r>' %
(self.font, self.fontsize, self.charspace, self.wordspace,
self.scaling, self.leading, self.render, self.rise,
self.matrix, self.linematrix))
def copy(self):
obj = PDFTextState()
obj.font = self.font
obj.fontsize = self.fontsize
obj.charspace = self.charspace
obj.wordspace = self.wordspace
obj.scaling = self.scaling
obj.leading = self.leading
obj.render = self.render
obj.rise = self.rise
obj.matrix = self.matrix
obj.linematrix = self.linematrix
return obj
def reset(self):
self.matrix = MATRIX_IDENTITY
self.linematrix = [0, 0]
return
@property
def is_upright(self):
(a, b, c, d, _, _) = self.matrix
return (0 < a*d*self.scaling and b*c <= 0)
-163
View File
@@ -1,163 +0,0 @@
# -*- coding: utf-8 -*-
from .PDFFont import PDFUnicodeNotDefined
from ..utils import isnumber, apply_matrix_pt
from ..utils import Bbox, mult_matrix, translate_matrix
from .PDFResourceManager import PDFResourceManager
from .PDFGraphicState import PDFGraphicState
from .PDFTextState import PDFTextState
from .PDFColorSpace import PDFColorSpace
from .PDFFont import PDFFont
## PDFDevice
##
class PDFDevice(object):
def __init__(self, rsrcmgr: PDFResourceManager):
self.rsrcmgr = rsrcmgr
self.ctm = None
return
def __repr__(self):
return '<PDFDevice>'
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def close(self):
return
def set_ctm(self, ctm):
self.ctm = ctm
return
def begin_tag(self, tag, props=None):
return
def end_tag(self):
return
def do_tag(self, tag, props=None):
return
def begin_page(self, page, ctm):
return
def end_page(self, page):
return
def begin_figure(self, name, bbox: Bbox, matrix):
return
def end_figure(self, name):
return
def paint_path(self, graphicstate: PDFGraphicState, stroke, fill, evenodd, path):
return
def render_image(self, name, stream):
return
def render_string(self, textstate, seq, ncs, graphicstate: PDFGraphicState):
return
## PDFTextDevice
##
class PDFTextDevice(PDFDevice):
def render_string(self, textstate: PDFTextState, seq: bytearray, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
textstate.matrix = mult_matrix(textstate.matrix, self.ctm)
textstate.scaling = textstate.scaling * .01
textstate.charspace = textstate.charspace * textstate.scaling
textstate.wordspace = textstate.wordspace * textstate.scaling
if textstate.font.is_multibyte():
textstate.wordspace = 0
dxscale = .001 * textstate.fontsize * textstate.scaling
if textstate.font.is_vertical():
self.__render_string_along(1,
seq, textstate, dxscale, ncs, graphicstate
)
else:
self.__render_string_along(0,
seq, textstate, dxscale, ncs, graphicstate
)
def __render_string_along(self, idx: int, seq: bytearray,
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
needcharspace = False
char_meta_datas = []
for obj in seq:
if isnumber(obj):
textstate.linematrix[idx] -= obj*dxscale
needcharspace = True
else:
for cid in textstate.font.decode(obj):
if needcharspace:
textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid)
assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
textstate.linematrix[idx] += adv
char_meta_datas.append(
( text, bbox, )
)
if cid == 32 and textstate.wordspace:
textstate.linematrix[idx] += textstate.wordspace
needcharspace = True
self.render_char_box(char_meta_datas, textstate, graphicstate, ncs)
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
font = textstate.font
fontsize = textstate.fontsize
rise = textstate.rise
scaling = textstate.scaling
textwidth = font.char_width(char_id)
textdisp = font.char_disp(char_id)
adv = textwidth * fontsize * scaling
# compute the boundary rectangle.
if font.is_vertical():
# vertical
width = font.get_width() * fontsize
(vx, vy) = textdisp
if vx is None:
vx = width * 0.5
else:
vx = vx * fontsize * .001
vy = (1000 - vy) * fontsize * .001
tx = -vx
ty = vy + rise
bll = (tx, ty+adv)
bur = (tx+width, ty)
else:
# horizontal
height = font.get_height() * fontsize
descent = font.get_descent() * fontsize
ty = descent + rise
bll = (0, ty)
bur = (adv, ty+height)
(x0, y0) = apply_matrix_pt(matrix, bll)
(x1, y1) = apply_matrix_pt(matrix, bur)
if x1 < x0:
(x0, x1) = (x1, x0)
if y1 < y0:
(y0, y1) = (y1, y0)
return (adv, ((x0, y0), (x1, y1)))
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
pass
@@ -1,346 +0,0 @@
import logging
log = logging.getLogger(__name__)
from io import TextIOWrapper
from typing import List, Tuple, Union
from ...utils import int2byte
from .exceptions import PSEOF
from .constants import EOL, NONSPC, KWD, END_LITERAL, LIT, HEX
from .constants import SPC, HEX_PAIR
from .constants import END_NUMBER, END_STRING, END_KEYWORD, END_HEX_STRING
from .constants import OCT_STRING, ESC_STRING, KEYWORD_DICT_BEGIN, KEYWORD_DICT_END
from .types import PSLiteral
class PSBaseParser(object):
"""Most basic PostScript parser that performs only tokenization.
"""
BUFSIZ = 4096
def __init__(self, fp: TextIOWrapper):
self.fp = fp
self.seek(0)
return
def __repr__(self):
return '<%s: %r, bufpos=%d>' % (self.__class__.__name__, self.fp, self.bufpos)
def flush(self):
return
def close(self):
self.flush()
return
def tell(self):
return self.bufpos+self.charpos
def poll(self, pos=None, n=80):
pos0 = self.fp.tell()
if not pos:
pos = self.bufpos+self.charpos
self.fp.seek(pos)
log.info('poll(%d): %r', pos, self.fp.read(n))
self.fp.seek(pos0)
return
def seek(self, pos: int):
"""Seeks the parser to the given position.
"""
log.debug('seek: %r', pos)
self.fp.seek(pos)
# reset the status for nextline()
self.bufpos: int = pos
self.buf: bytes = b''
self.charpos: int = 0
# reset the status for nexttoken()
self._current_parse_func = self._parse_main
self._curtoken: bytes = b''
self._curtokenpos: int = 0
self._tokens: List[Tuple[int, Union[int, float, bool, str, bytes, PSLiteral]]] = []
return
def fillbuf(self):
if self.charpos < len(self.buf):
return
# fetch next chunk.
self.bufpos = self.fp.tell()
self.buf = self.fp.read(self.BUFSIZ)
if not self.buf:
raise PSEOF('Unexpected EOF')
self.charpos = 0
return
def nextline(self):
"""Fetches a next line that ends either with \\r or \\n.
"""
linebuf = b''
linepos = self.bufpos + self.charpos
eol = False
while 1:
self.fillbuf()
if eol:
c = self.buf[self.charpos:self.charpos+1]
# handle b'\r\n'
if c == b'\n':
linebuf += c
self.charpos += 1
break
m = EOL.search(self.buf, self.charpos)
if m:
linebuf += self.buf[self.charpos:m.end(0)]
self.charpos = m.end(0)
if linebuf[-1:] == b'\r':
eol = True
else:
break
else:
linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf)
log.debug('nextline: %r, %r', linepos, linebuf)
return (linepos, linebuf)
def revreadlines(self):
"""Fetches a next line backword.
This is used to locate the trailers at the end of a file.
"""
self.fp.seek(0, 2)
pos = self.fp.tell()
buf = b''
while 0 < pos:
prevpos = pos
pos = max(0, pos-self.BUFSIZ)
self.fp.seek(pos)
s = self.fp.read(prevpos-pos)
if not s:
break
while 1:
n = max(s.rfind(b'\r'), s.rfind(b'\n'))
if n == -1:
buf = s + buf
break
yield s[n:] + buf
s = s[:n]
buf = b''
return
def _parse_main(self, s, i):
m = NONSPC.search(s, i)
if not m:
return len(s)
j = m.start(0)
c = s[j:j+1]
self._curtokenpos = self.bufpos+j
if c == b'%':
self._curtoken = b'%'
self._current_parse_func = self._parse_comment
return j+1
elif c == b'/':
self._curtoken = b''
self._current_parse_func = self._parse_literal
return j+1
elif c in b'-+' or c.isdigit():
self._curtoken = c
self._current_parse_func = self._parse_number
return j+1
elif c == b'.':
self._curtoken = c
self._current_parse_func = self._parse_float
return j+1
elif c.isalpha():
self._curtoken = c
self._current_parse_func = self._parse_keyword
return j+1
elif c == b'(':
self._curtoken = b''
self.paren = 1
self._current_parse_func = self._parse_string
return j+1
elif c == b'<':
self._curtoken = b''
self._current_parse_func = self._parse_wopen
return j+1
elif c == b'>':
self._curtoken = b''
self._current_parse_func = self._parse_wclose
return j+1
else:
self._add_token(KWD(c))
return j+1
def _add_token(self, obj):
self._tokens.append((self._curtokenpos, obj))
return
def _parse_comment(self, s, i):
m = EOL.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
self._current_parse_func = self._parse_main
# We ignore comments.
#self._tokens.append(self._curtoken)
return j
def _parse_literal(self, s, i):
m = END_LITERAL.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
c = s[j:j+1]
if c == b'#':
self.hex = b''
self._current_parse_func = self._parse_literal_hex
return j+1
try:
self._curtoken=str(self._curtoken,'utf-8')
except:
pass
self._add_token(LIT(self._curtoken))
self._current_parse_func = self._parse_main
return j
def _parse_literal_hex(self, s, i):
c = s[i:i+1]
if HEX.match(c) and len(self.hex) < 2:
self.hex += c
return i+1
if self.hex:
self._curtoken += int2byte(int(self.hex, 16))
self._current_parse_func = self._parse_literal
return i
def _parse_number(self, s, i):
m = END_NUMBER.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
c = s[j:j+1]
if c == b'.':
self._curtoken += c
self._current_parse_func = self._parse_float
return j+1
try:
self._add_token(int(self._curtoken))
except ValueError:
pass
self._current_parse_func = self._parse_main
return j
def _parse_float(self, s, i):
m = END_NUMBER.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
try:
self._add_token(float(self._curtoken))
except ValueError:
pass
self._current_parse_func = self._parse_main
return j
def _parse_keyword(self, s, i):
m = END_KEYWORD.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
if self._curtoken == b'true':
token = True
elif self._curtoken == b'false':
token = False
else:
token = KWD(self._curtoken)
self._add_token(token)
self._current_parse_func = self._parse_main
return j
def _parse_string(self, s, i):
m = END_STRING.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
c = s[j:j+1]
if c == b'\\':
self.oct = b''
self._current_parse_func = self._parse_string_1
return j+1
if c == b'(':
self.paren += 1
self._curtoken += c
return j+1
if c == b')':
self.paren -= 1
if self.paren: # WTF, they said balanced parens need no special treatment.
self._curtoken += c
return j+1
self._add_token(self._curtoken)
self._current_parse_func = self._parse_main
return j+1
def _parse_string_1(self, s, i):
c = s[i:i+1]
if OCT_STRING.match(c) and len(self.oct) < 3:
self.oct += c
return i+1
if self.oct:
self._curtoken += int2byte(int(self.oct, 8))
self._current_parse_func = self._parse_string
return i
if c in ESC_STRING:
self._curtoken += int2byte(ESC_STRING[c])
self._current_parse_func = self._parse_string
return i+1
def _parse_wopen(self, s, i):
c = s[i:i+1]
if c == b'<':
self._add_token(KEYWORD_DICT_BEGIN)
self._current_parse_func = self._parse_main
i += 1
else:
self._current_parse_func = self._parse_hexstring
return i
def _parse_wclose(self, s, i):
c = s[i:i+1]
if c == b'>':
self._add_token(KEYWORD_DICT_END)
i += 1
self._current_parse_func = self._parse_main
return i
def _parse_hexstring(self, s, i):
m = END_HEX_STRING.search(s, i)
if not m:
self._curtoken += s[i:]
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
token = HEX_PAIR.sub(lambda m: int2byte(int(m.group(0), 16)),SPC.sub(b'', self._curtoken))
self._add_token(token)
self._current_parse_func = self._parse_main
return j
def nexttoken(self):
while not self._tokens:
self.fillbuf()
self.charpos = self._current_parse_func(self.buf, self.charpos)
token = self._tokens.pop(0)
log.debug('nexttoken: %r', token)
return token
@@ -1,160 +0,0 @@
import logging
from io import TextIOWrapper
from ...utils import choplist, settings
from .PSBaseParser import PSBaseParser
from .types import *
from .exceptions import *
from .constants import *
log = logging.getLogger(__name__)
def literal_name(x):
if not isinstance(x, PSLiteral):
if settings.STRICT:
raise PSTypeError('Literal required: %r' % (x,))
else:
name=x
else:
name=x.name
try:
name = str(name,'utf-8')
except:
pass
return name
def keyword_name(x):
if not isinstance(x, PSKeyword):
if settings.STRICT:
raise PSTypeError('Keyword required: %r' % x)
else:
name=x
else:
name=x.name
name = str(name,'utf-8','ignore')
return name
class PSStackParser(PSBaseParser):
def __init__(self, fp: TextIOWrapper):
PSBaseParser.__init__(self, fp)
self.reset()
return
def reset(self):
self.context = []
self.curtype = None
self.curstack = []
self.results = []
return
def seek(self, pos):
PSBaseParser.seek(self, pos)
self.reset()
return
def push(self, *objs):
self.curstack.extend(objs)
return
def pop(self, n):
objs = self.curstack[-n:]
self.curstack[-n:] = []
return objs
def popall(self):
objs = self.curstack
self.curstack = []
return objs
def add_results(self, *objs):
try:
log.debug('add_results: %r', objs)
except:
log.debug('add_results: (unprintable object)')
self.results.extend(objs)
return
def start_type(self, pos, type):
self.context.append((pos, self.curtype, self.curstack))
(self.curtype, self.curstack) = (type, [])
log.debug('start_type: pos=%r, type=%r', pos, type)
return
def end_type(self, type):
if self.curtype != type:
raise PSTypeError('Type mismatch: %r != %r' % (self.curtype, type))
objs = [obj for (_, obj) in self.curstack]
(pos, self.curtype, self.curstack) = self.context.pop()
log.debug('end_type: pos=%r, type=%r, objs=%r', pos, type, objs)
return (pos, objs)
def do_keyword(self, pos, token):
return
def nextobject(self):
"""Yields a list of objects.
Returns keywords, literals, strings, numbers, arrays and dictionaries.
Arrays and dictionaries are represented as Python lists and dictionaries.
"""
while not self.results:
(pos, token) = self.nexttoken()
if isinstance(token, (int, float, bool, str, bytes, PSLiteral)):
# normal token
self.push((pos, token))
elif token == KEYWORD_ARRAY_BEGIN:
# begin array
self.start_type(pos, 'a')
elif token == KEYWORD_ARRAY_END:
# end array
try:
self.push(self.end_type('a'))
except PSTypeError:
if settings.STRICT:
raise
elif token == KEYWORD_DICT_BEGIN:
# begin dictionary
self.start_type(pos, 'd')
elif token == KEYWORD_DICT_END:
# end dictionary
try:
(pos, objs) = self.end_type('d')
if len(objs) % 2 != 0:
raise PSSyntaxError('Invalid dictionary construct: %r' % objs)
# construct a Python dictionary.
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
self.push((pos, d))
except PSTypeError:
if settings.STRICT:
raise
elif token == KEYWORD_PROC_BEGIN:
# begin proc
self.start_type(pos, 'p')
elif token == KEYWORD_PROC_END:
# end proc
try:
self.push(self.end_type('p'))
except PSTypeError:
if settings.STRICT:
raise
elif isinstance(token,PSKeyword):
log.debug('do_keyword: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
self.do_keyword(pos, token)
else:
log.error('unknown token: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
self.do_keyword(pos, token)
raise Exception("unknown tokens")
if self.context:
continue
else:
self.flush()
obj = self.results.pop(0)
try:
log.debug('nextobject: %r', obj)
except:
log.debug('nextobject: (unprintable object)')
return obj
@@ -1,28 +0,0 @@
import re
from .types import PSLiteral, PSSymbolTable, PSKeyword
PSLiteralTable = PSSymbolTable(PSLiteral)
PSKeywordTable = PSSymbolTable(PSKeyword)
LIT = PSLiteralTable.intern
KWD = PSKeywordTable.intern
KEYWORD_PROC_BEGIN = KWD(b'{')
KEYWORD_PROC_END = KWD(b'}')
KEYWORD_ARRAY_BEGIN = KWD(b'[')
KEYWORD_ARRAY_END = KWD(b']')
KEYWORD_DICT_BEGIN = KWD(b'<<')
KEYWORD_DICT_END = KWD(b'>>')
## PSBaseParser
##
EOL = re.compile(br'[\r\n]')
SPC = re.compile(br'\s')
NONSPC = re.compile(br'\S')
HEX = re.compile(br'[0-9a-fA-F]')
END_LITERAL = re.compile(br'[#/%\[\]()<>{}\s]')
END_HEX_STRING = re.compile(br'[^\s0-9a-fA-F]')
HEX_PAIR = re.compile(br'[0-9a-fA-F]{2}|.')
END_NUMBER = re.compile(br'[^0-9]')
END_KEYWORD = re.compile(br'[#/%\[\]()<>{}\s]')
END_STRING = re.compile(br'[()\134]')
OCT_STRING = re.compile(br'[0-7]')
ESC_STRING = {b'b': 8, b't': 9, b'n': 10, b'f': 12, b'r': 13, b'(': 40, b')': 41, b'\\': 92}
Loaded 100 of 214 files, more files were not shown because too many files have changed in this diff. Show more