8000 Add basic support for adding SVG pictures to docx files · Hagai/python-docx@682cc27 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 682cc27

Browse files
takishagaid-savicell
authored andcommitted
Add basic support for adding SVG pictures to docx files
See issues python-openxml#351, python-openxml#651, python-openxml#659. (cherry picked from commit 8f54818)
1 parent 57d3b9e commit 682cc27

File tree

6 files changed

+908
-0
lines changed

6 files changed

+908
-0
lines changed

docx/image/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides objects that can characterize image streams as to content type and
5+
size, as a required step in including them in a document.
6+
"""
7+
8+
from __future__ import (
9+
absolute_import, division, print_function, unicode_literals
10+
)
11+
12+
from docx.image.bmp import Bmp
13+
from docx.image.gif import Gif
14+
from docx.image.jpeg import Exif, Jfif
15+
from docx.image.png import Png
16+
from docx.image.tiff import Tiff
17+
from docx.image.svg import Svg
18+
19+
20+
SIGNATURES = (
21+
# class, offset, signature_bytes
22+
(Png, 0, b'\x89PNG\x0D\x0A\x1A\x0A'),
23+
(Jfif, 6, b'JFIF'),
24+
(Exif, 6, b'Exif'),
25+
(Gif, 0, b'GIF87a'),
26+
(Gif, 0, b'GIF89a'),
27+
(Tiff, 0, b'MM\x00*'), # big-endian (Motorola) TIFF
28+
(Tiff, 0, b'II*\x00'), # little-endian (Intel) TIFF
29+
(Bmp, 0, b'BM'),
30+
(Svg, 0, b'<svg '),
31+
)

docx/image/constants.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Constants specific the the image sub-package
5+
"""
6+
7+
8+
class JPEG_MARKER_CODE(object):
9+
"""
10+
JPEG marker codes
11+
"""
12+
TEM = b'\x01'
13+
DHT = b'\xC4'
14+
DAC = b'\xCC'
15+
JPG = b'\xC8'
16+
17+
SOF0 = b'\xC0'
18+
SOF1 = b'\xC1'
19+
SOF2 = b'\xC2'
20+
SOF3 = b'\xC3'
21+
SOF5 = b'\xC5'
22+
SOF6 = b'\xC6'
23+
SOF7 = b'\xC7'
24+
SOF9 = b'\xC9'
25+
SOFA = b'\xCA'
26+
SOFB = b'\xCB'
27+
SOFD = b'\xCD'
28+
SOFE = b'\xCE'
29+
SOFF = b'\xCF'
30+
31+
RST0 = b'\xD0'
32+
RST1 = b'\xD1'
33+
RST2 = b'\xD2'
34+
RST3 = b'\xD3'
35+
RST4 = b'\xD4'
36+
RST5 = b'\xD5'
37+
RST6 = b'\xD6'
38+
RST7 = b'\xD7'
39+
40+
SOI = b'\xD8'
41+
EOI = b'\xD9'
42+
SOS = b'\xDA'
43+
DQT = b'\xDB' # Define Quantization Table(s)
44+
DNL = b'\xDC'
45+
DRI = b'\xDD'
46+
DHP = b'\xDE'
47+
EXP = b'\xDF'
48+
49+
APP0 = b'\xE0'
50+
APP1 = b'\xE1'
51+
APP2 = b'\xE2'
52+
APP3 = b'\xE3'
53+
APP4 = b'\xE4'
54+
APP5 = b'\xE5'
55+
APP6 = b'\xE6'
56+
APP7 = b'\xE7'
57+
APP8 = b'\xE8'
58+
APP9 = b'\xE9'
59+
APPA = b'\xEA'
60+
APPB = b'\xEB'
61+
APPC = b'\xEC'
62+
APPD = b'\xED'
63+
APPE = b'\xEE'
64+
APPF = b'\xEF'
65+
66+
STANDALONE_MARKERS = (
67+
TEM, SOI, EOI, RST0, RST1, RST2, RST3, RST4, RST5, RST6, RST7
68+
)
69+
70+
SOF_MARKER_CODES = (
71+
SOF0, SOF1, SOF2, SOF3, SOF5, SOF6, SOF7, SOF9, SOFA, SOFB, SOFD,
72+
SOFE, SOFF
73+
)
74+
75+
marker_names = {
76+
b'\x00': 'UNKNOWN',
77+
b'\xC0': 'SOF0',
78+
b'\xC2': 'SOF2',
79+
b'\xC4': 'DHT',
80+
b'\xDA': 'SOS', # start of scan
81+
b'\xD8': 'SOI', # start of image
82+
b'\xD9': 'EOI', # end of image
83+
b'\xDB': 'DQT',
84+
b'\xE0': 'APP0',
85+
b'\xE1': 'APP1',
86+
b'\xE2': 'APP2',
87+
b'\xED': 'APP13',
88+
b'\xEE': 'APP14',
89+
}
90+
91+
@classmethod
92+
def is_standalone(cls, marker_code):
93+
return marker_code in cls.STANDALONE_MARKERS
94+
95+
96+
class MIME_TYPE(object):
97+
"""
98+
Image content types
99+
"""
100+
BMP = 'image/bmp'
101+
GIF = 'image/gif'
102+
JPEG = 'image/jpeg'
103+
PNG = 'image/png'
104+
TIFF = 'image/tiff'
105+
SVG = 'image/svg+xml'
106+
107+
108+
class PNG_CHUNK_TYPE(object):
109+
"""
110+
PNG chunk type names
111+
"""
112+
IHDR = 'IHDR'
113+
pHYs = 'pHYs'
114+
IEND = 'IEND'
115+
116+
117+
class TIFF_FLD_TYPE(object):
118+
"""
119+
Tag codes for TIFF Image File Directory (IFD) entries.
120+
"""
121+
BYTE = 1
122+
ASCII = 2
123+
SHORT = 3
124+
LONG = 4
125+
RATIONAL = 5
126+
127+
field_type_names = {
128+
1: 'BYTE', 2: 'ASCII char', 3: 'SHORT', 4: 'LONG',
129+
5: 'RATIONAL'
130+
}
131+
132+
133+
TIFF_FLD = TIFF_FLD_TYPE
134+
135+
136+
class TIFF_TAG(object):
137+
"""
138+
Tag codes for TIFF Image File Directory (IFD) entries.
139+
"""
140+
IMAGE_WIDTH = 0x0100
141+
IMAGE_LENGTH = 0x0101
142+
X_RESOLUTION = 0x011A
143+
Y_RESOLUTION = 0x011B
144+
RESOLUTION_UNIT = 0x0128
145+
146+
tag_names = {
147+
0x00FE: 'NewSubfileType',
148+
0x0100: 'ImageWidth',
149+
0x0101: 'ImageLength',
150+
0x0102: 'BitsPerSample',
151+
0x0103: 'Compression',
152+
0x0106: 'PhotometricInterpretation',
153+
0x010E: 'ImageDescription',
154+
0x010F: 'Make',
155+
0x0110: 'Model',
156+
0x0111: 'StripOffsets',
157+
0x0112: 'Orientation',
158+
0x0115: 'SamplesPerPixel',
159+
0x0117: 'StripByteCounts',
160+
0x011A: 'XResolution',
161+
0x011B: 'YResolution',
162+
0x011C: 'PlanarConfiguration',
163+
0x0128: 'ResolutionUnit',
164+
0x0131: 'Software',
165+
0x0132: 'DateTime',
166+
0x0213: 'YCbCrPositioning',
167+
0x8769: 'ExifTag',
168+
0x8825: 'GPS IFD',
169+
0xC4A5: 'PrintImageMatching',
170+
}

0 commit comments

Comments
 (0)
0