Skip to content

Commit f54ccd7

Browse files
author
Steve Canny
committed
reorg: extract docx.text.font
1 parent 0f5ad58 commit f54ccd7

File tree

8 files changed

+722
-692
lines changed

8 files changed

+722
-692
lines changed

docx/styles/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from . import BabelFish
1212
from ..enum.style import WD_STYLE_TYPE
1313
from ..shared import ElementProxy
14+
from ..text.font import Font
1415
from ..text.parfmt import ParagraphFormat
15-
from ..text.run import Font
1616

1717

1818
def StyleFactory(style_elm):

docx/text/font.py

Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Font-related proxy objects.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..shared import ElementProxy
12+
13+
14+
class Font(ElementProxy):
15+
"""
16+
Proxy object wrapping the parent of a ``<w:rPr>`` element and providing
17+
access to character properties such as font name, font size, bold, and
18+
subscript.
19+
"""
20+
21+
__slots__ = ()
22+
23+
@property
24+
def all_caps(self):
25+
"""
26+
Read/write. Causes text in this font to appear in capital letters.
27+
"""
28+
return self._get_bool_prop('caps')
29+
30+
@all_caps.setter
31+
def all_caps(self, value):
32+
self._set_bool_prop('caps', value)
33+
34+
@property
35+
def bold(self):
36+
"""
37+
Read/write. Causes text in this font to appear in bold.
38+
"""
39+
return self._get_bool_prop('b')
40+
41+
@bold.setter
42+
def bold(self, value):
43+
self._set_bool_prop('b', value)
44+
45+
@property
46+
def complex_script(self):
47+
"""
48+
Read/write tri-state value. When |True|, causes the characters in the
49+
run to be treated as complex script regardless of their Unicode
50+
values.
51+
"""
52+
return self._get_bool_prop('cs')
53+
54+
@complex_script.setter
55+
def complex_script(self, value):
56+
self._set_bool_prop('cs', value)
57+
58+
@property
59+
def cs_bold(self):
60+
"""
61+
Read/write tri-state value. When |True|, causes the complex script
62+
characters in the run to be displayed in bold typeface.
63+
"""
64+
return self._get_bool_prop('bCs')
65+
66+
@cs_bold.setter
67+
def cs_bold(self, value):
68+
self._set_bool_prop('bCs', value)
69+
70+
@property
71+
def cs_italic(self):
72+
"""
73+
Read/write tri-state value. When |True|, causes the complex script
74+
characters in the run to be displayed in italic typeface.
75+
"""
76+
return self._get_bool_prop('iCs')
77+
78+
@cs_italic.setter
79+
def cs_italic(self, value):
80+
self._set_bool_prop('iCs', value)
81+
82+
@property
83+
def double_strike(self):
84+
"""
85+
Read/write tri-state value. When |True|, causes the text in the run
86+
to appear with double strikethrough.
87+
"""
88+
return self._get_bool_prop('dstrike')
89+
90+
@double_strike.setter
91+
def double_strike(self, value):
92+
self._set_bool_prop('dstrike', value)
93+
94+
@property
95+
def emboss(self):
96+
"""
97+
Read/write tri-state value. When |True|, causes the text in the run
98+
to appear as if raised off the page in relief.
99+
"""
100+
return self._get_bool_prop('emboss')
101+
102+
@emboss.setter
103+
def emboss(self, value):
104+
self._set_bool_prop('emboss', value)
105+
106+
@property
107+
def hidden(self):
108+
"""
109+
Read/write tri-state value. When |True|, causes the text in the run
110+
to be hidden from display, unless applications settings force hidden
111+
text to be shown.
112+
"""
113+
return self._get_bool_prop('vanish')
114+
115+
@hidden.setter
116+
def hidden(self, value):
117+
self._set_bool_prop('vanish', value)
118+
119+
@property
120+
def italic(self):
121+
"""
122+
Read/write tri-state value. When |True|, causes the text of the run
123+
to appear in italics. |None| indicates the effective value is
124+
inherited from the style hierarchy.
125+
"""
126+
return self._get_bool_prop('i')
127+
128+
@italic.setter
129+
def italic(self, value):
130+
self._set_bool_prop('i', value)
131+
132+
@property
133+
def imprint(self):
134+
"""
135+
Read/write tri-state value. When |True|, causes the text in the run
136+
to appear as if pressed into the page.
137+
"""
138+
return self._get_bool_prop('imprint')
139+
140+
@imprint.setter
141+
def imprint(self, value):
142+
self._set_bool_prop('imprint', value)
143+
144+
@property
145+
def math(self):
146+
"""
147+
Read/write tri-state value. When |True|, specifies this run contains
148+
WML that should be handled as though it was Office Open XML Math.
149+
"""
150+
return self._get_bool_prop('oMath')
151+
152+
@math.setter
153+
def math(self, value):
154+
self._set_bool_prop('oMath', value)
155+
156+
@property
157+
def name(self):
158+
"""
159+
Get or set the typeface name for this |Font| instance, causing the
160+
text it controls to appear in the named font, if a matching font is
161+
found. |None| indicates the typeface is inherited from the style
162+
hierarchy.
163+
"""
164+
rPr = self._element.rPr
165+
if rPr is None:
166+
return None
167+
return rPr.rFonts_ascii
168+
169+
@name.setter
170+
def name(self, value):
171+
rPr = self._element.get_or_add_rPr()
172+
rPr.rFonts_ascii = value
173+
rPr.rFonts_hAnsi = value
174+
175+
@property
176+
def no_proof(self):
177+
"""
178+
Read/write tri-state value. When |True|, specifies that the contents
179+
of this run should not report any errors when the document is scanned
180+
for spelling and grammar.
181+
"""
182+
return self._get_bool_prop('noProof')
183+
184+
@no_proof.setter
185+
def no_proof(self, value):
186+
self._set_bool_prop('noProof', value)
187+
188+
@property
189+
def outline(self):
190+
"""
191+
Read/write tri-state value. When |True| causes the characters in the
192+
run to appear as if they have an outline, by drawing a one pixel wide
193+
border around the inside and outside borders of each character glyph.
194+
"""
195+
return self._get_bool_prop('outline')
196+
197+
@outline.setter
198+
def outline(self, value):
199+
self._set_bool_prop('outline', value)
200+
201+
@property
202+
def rtl(self):
203+
"""
204+
Read/write tri-state value. When |True| causes the text in the run
205+
to have right-to-left characteristics.
206+
"""
207+
return self._get_bool_prop('rtl')
208+
209+
@rtl.setter
210+
def rtl(self, value):
211+
self._set_bool_prop('rtl', value)
212+
213+
@property
214+
def shadow(self):
215+
"""
216+
Read/write tri-state value. When |True| causes the text in the run
217+
to appear as if each character has a shadow.
218+
"""
219+
return self._get_bool_prop('shadow')
220+
221+
@shadow.setter
222+
def shadow(self, value):
223+
self._set_bool_prop('shadow', value)
224+
225+
@property
226+
def size(self):
227+
"""
228+
Read/write |Length| value or |None|, indicating the font height in
229+
English Metric Units (EMU). |None| indicates the font size should be
230+
inherited from the style hierarchy. |Length| is a subclass of |int|
231+
having properties for convenient conversion into points or other
232+
length units. The :class:`docx.shared.Pt` class allows convenient
233+
specification of point values::
234+
235+
>> font.size = Pt(24)
236+
>> font.size
237+
304800
238+
>> font.size.pt
239+
24.0
240+
"""
241+
rPr = self._element.rPr
242+
if rPr is None:
243+
return None
244+
return rPr.sz_val
245+
246+
@size.setter
247+
def size(self, emu):
248+
rPr = self._element.get_or_add_rPr()
249+
rPr.sz_val = emu
250+
251+
@property
252+
def small_caps(self):
253+
"""
254+
Read/write tri-state value. When |True| causes the lowercase
255+
characters in the run to appear as capital letters two points smaller
256+
than the font size specified for the run.
257+
"""
258+
return self._get_bool_prop('smallCaps')
259+
260+
@small_caps.setter
261+
def small_caps(self, value):
262+
self._set_bool_prop('smallCaps', value)
263+
264+
@property
265+
def snap_to_grid(self):
266+
"""
267+
Read/write tri-state value. When |True| causes the run to use the
268+
document grid characters per line settings defined in the docGrid
269+
element when laying out the characters in this run.
270+
"""
271+
return self._get_bool_prop('snapToGrid')
272+
273+
@snap_to_grid.setter
274+
def snap_to_grid(self, value):
275+
self._set_bool_prop('snapToGrid', value)
276+
277+
@property
278+
def spec_vanish(self):
279+
"""
280+
Read/write tri-state value. When |True|, specifies that the given run
281+
shall always behave as if it is hidden, even when hidden text is
282+
being displayed in the current document. The property has a very
283+
narrow, specialized use related to the table of contents. Consult the
284+
spec (§17.3.2.36) for more details.
285+
"""
286+
return self._get_bool_prop('specVanish')
287+
288+
@spec_vanish.setter
289+
def spec_vanish(self, value):
290+
self._set_bool_prop('specVanish', value)
291+
292+
@property
293+
def strike(self):
294+
"""
295+
Read/write tri-state value. When |True| causes the text in the run
296+
to appear with a single horizontal line through the center of the
297+
line.
298+
"""
299+
return self._get_bool_prop('strike')
300+
301+
@strike.setter
302+
def strike(self, value):
303+
self._set_bool_prop('strike', value)
304+
305+
@property
306+
def subscript(self):
307+
"""
308+
Boolean indicating whether the characters in this |Font| appear as
309+
subscript. |None| indicates the subscript/subscript value is
310+
inherited from the style hierarchy.
311+
"""
312+
rPr = self._element.rPr
313+
if rPr is None:
314+
return None
315+
return rPr.subscript
316+
317+
@subscript.setter
318+
def subscript(self, value):
319+
rPr = self._element.get_or_add_rPr()
320+
rPr.subscript = value
321+
322+
@property
323+
def superscript(self):
324+
"""
325+
Boolean indicating whether the characters in this |Font| appear as
326+
superscript. |None| indicates the subscript/superscript value is
327+
inherited from the style hierarchy.
328+
"""
329+
rPr = self._element.rPr
330+
if rPr is None:
331+
return None
332+
return rPr.superscript
333+
334+
@superscript.setter
335+
def superscript(self, value):
336+
rPr = self._element.get_or_add_rPr()
337+
rPr.superscript = value
338+
339+
@property
340+
def underline(self):
341+
"""
342+
The underline style for this |Font|, one of |None|, |True|, |False|,
343+
or a value from :ref:`WdUnderline`. |None| indicates the font
344+
inherits its underline value from the style hierarchy. |False|
345+
indicates no underline. |True| indicates single underline. The values
346+
from :ref:`WdUnderline` are used to specify other outline styles such
347+
as double, wavy, and dotted.
348+
"""
349+
rPr = self._element.rPr
350+
if rPr is None:
351+
return None
352+
return rPr.u_val
353+
354+
@underline.setter
355+
def underline(self, value):
356+
rPr = self._element.get_or_add_rPr()
357+
rPr.u_val = value
358+
359+
@property
360+
def web_hidden(self):
361+
"""
362+
Read/write tri-state value. When |True|, specifies that the contents
363+
of this run shall be hidden when the document is displayed in web
364+
page view.
365+
"""
366+
return self._get_bool_prop('webHidden')
367+
368+
@web_hidden.setter
369+
def web_hidden(self, value):
370+
self._set_bool_prop('webHidden', value)
371+
372+
def _get_bool_prop(self, name):
373+
"""
374+
Return the value of boolean child of `w:rPr` having *name*.
375+
"""
376+
rPr = self._element.rPr
377+
if rPr is None:
378+
return None
379+
return rPr._get_bool_val(name)
380+
381+
def _set_bool_prop(self, name, value):
382+
"""
383+
Assign *value* to the boolean child *name* of `w:rPr`.
384+
"""
385+
rPr = self._element.get_or_add_rPr()
386+
rPr._set_bool_val(name, value)

0 commit comments

Comments
 (0)