Skip to content

Commit a618e0a

Browse files
author
Steve Canny
committed
font: add Font boolean properties
Transplanted essentially intact from Run.
1 parent a74b0bf commit a618e0a

File tree

3 files changed

+375
-15
lines changed

3 files changed

+375
-15
lines changed

docx/oxml/text/run.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ def underline(self):
131131
rPr = self.rPr
132132
if rPr is None:
133133
return None
134-
return rPr.underline
134+
return rPr.u_val
135135

136136
@underline.setter
137137
def underline(self, value):
138138
rPr = self.get_or_add_rPr()
139-
rPr.underline = value
139+
rPr.u_val = value
140140

141141

142142
class CT_RPr(BaseOxmlElement):
@@ -275,23 +275,22 @@ def u_val(self, value):
275275
if value is not None:
276276
self._add_u().val = value
277277

278-
@property
279-
def underline(self):
278+
def _get_bool_val(self, name):
280279
"""
281-
Underline type specified in <w:u> child, or None if that element is
282-
not present.
280+
Return the value of the boolean child element having *name*, e.g.
281+
'b', 'i', and 'smallCaps'.
283282
"""
284-
u = self.u
285-
if u is None:
283+
element = getattr(self, name)
284+
if element is None:
286285
return None
287-
return u.val
286+
return element.val
288287

289-
@underline.setter
290-
def underline(self, value):
291-
self._remove_u()
292-
if value is not None:
293-
u = self._add_u()
294-
u.val = value
288+
def _set_bool_val(self, name, value):
289+
if value is None:
290+
getattr(self, '_remove_%s' % name)()
291+
return
292+
element = getattr(self, 'get_or_add_%s' % name)()
293+
element.val = value
295294

296295

297296
class CT_Text(BaseOxmlElement):

docx/text/run.py

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,139 @@ class Font(ElementProxy):
381381

382382
__slots__ = ()
383383

384+
@property
385+
def all_caps(self):
386+
"""
387+
Read/write. Causes text in this font to appear in capital letters.
388+
"""
389+
return self._get_bool_prop('caps')
390+
391+
@all_caps.setter
392+
def all_caps(self, value):
393+
self._set_bool_prop('caps', value)
394+
395+
@property
396+
def bold(self):
397+
"""
398+
Read/write. Causes text in this font to appear in bold.
399+
"""
400+
return self._get_bool_prop('b')
401+
402+
@bold.setter
403+
def bold(self, value):
404+
self._set_bool_prop('b', value)
405+
406+
@property
407+
def complex_script(self):
408+
"""
409+
Read/write tri-state value. When |True|, causes the characters in the
410+
run to be treated as complex script regardless of their Unicode
411+
values.
412+
"""
413+
return self._get_bool_prop('cs')
414+
415+
@complex_script.setter
416+
def complex_script(self, value):
417+
self._set_bool_prop('cs', value)
418+
419+
@property
420+
def cs_bold(self):
421+
"""
422+
Read/write tri-state value. When |True|, causes the complex script
423+
characters in the run to be displayed in bold typeface.
424+
"""
425+
return self._get_bool_prop('bCs')
426+
427+
@cs_bold.setter
428+
def cs_bold(self, value):
429+
self._set_bool_prop('bCs', value)
430+
431+
@property
432+
def cs_italic(self):
433+
"""
434+
Read/write tri-state value. When |True|, causes the complex script
435+
characters in the run to be displayed in italic typeface.
436+
"""
437+
return self._get_bool_prop('iCs')
438+
439+
@cs_italic.setter
440+
def cs_italic(self, value):
441+
self._set_bool_prop('iCs', value)
442+
443+
@property
444+
def double_strike(self):
445+
"""
446+
Read/write tri-state value. When |True|, causes the text in the run
447+
to appear with double strikethrough.
448+
"""
449+
return self._get_bool_prop('dstrike')
450+
451+
@double_strike.setter
452+
def double_strike(self, value):
453+
self._set_bool_prop('dstrike', value)
454+
455+
@property
456+
def emboss(self):
457+
"""
458+
Read/write tri-state value. When |True|, causes the text in the run
459+
to appear as if raised off the page in relief.
460+
"""
461+
return self._get_bool_prop('emboss')
462+
463+
@emboss.setter
464+
def emboss(self, value):
465+
self._set_bool_prop('emboss', value)
466+
467+
@property
468+
def hidden(self):
469+
"""
470+
Read/write tri-state value. When |True|, causes the text in the run
471+
to be hidden from display, unless applications settings force hidden
472+
text to be shown.
473+
"""
474+
return self._get_bool_prop('vanish')
475+
476+
@hidden.setter
477+
def hidden(self, value):
478+
self._set_bool_prop('vanish', value)
479+
480+
@property
481+
def italic(self):
482+
"""
483+
Read/write tri-state value. When |True|, causes the text of the run
484+
to appear in italics. |None| indicates the effective value is
485+
inherited from the style hierarchy.
486+
"""
487+
return self._get_bool_prop('i')
488+
489+
@italic.setter
490+
def italic(self, value):
491+
self._set_bool_prop('i', value)
492+
493+
@property
494+
def imprint(self):
495+
"""
496+
Read/write tri-state value. When |True|, causes the text in the run
497+
to appear as if pressed into the page.
498+
"""
499+
return self._get_bool_prop('imprint')
500+
501+
@imprint.setter
502+
def imprint(self, value):
503+
self._set_bool_prop('imprint', value)
504+
505+
@property
506+
def math(self):
507+
"""
508+
Read/write tri-state value. When |True|, specifies this run contains
509+
WML that should be handled as though it was Office Open XML Math.
510+
"""
511+
return self._get_bool_prop('oMath')
512+
513+
@math.setter
514+
def math(self, value):
515+
self._set_bool_prop('oMath', value)
516+
384517
@property
385518
def name(self):
386519
"""
@@ -400,6 +533,56 @@ def name(self, value):
400533
rPr.rFonts_ascii = value
401534
rPr.rFonts_hAnsi = value
402535

536+
@property
537+
def no_proof(self):
538+
"""
539+
Read/write tri-state value. When |True|, specifies that the contents
540+
of this run should not report any errors when the document is scanned
541+
for spelling and grammar.
542+
"""
543+
return self._get_bool_prop('noProof')
544+
545+
@no_proof.setter
546+
def no_proof(self, value):
547+
self._set_bool_prop('noProof', value)
548+
549+
@property
550+
def outline(self):
551+
"""
552+
Read/write tri-state value. When |True| causes the characters in the
553+
run to appear as if they have an outline, by drawing a one pixel wide
554+
border around the inside and outside borders of each character glyph.
555+
"""
556+
return self._get_bool_prop('outline')
557+
558+
@outline.setter
559+
def outline(self, value):
560+
self._set_bool_prop('outline', value)
561+
562+
@property
563+
def rtl(self):
564+
"""
565+
Read/write tri-state value. When |True| causes the text in the run
566+
to have right-to-left characteristics.
567+
"""
568+
return self._get_bool_prop('rtl')
569+
570+
@rtl.setter
571+
def rtl(self, value):
572+
self._set_bool_prop('rtl', value)
573+
574+
@property
575+
def shadow(self):
576+
"""
577+
Read/write tri-state value. When |True| causes the text in the run
578+
to appear as if each character has a shadow.
579+
"""
580+
return self._get_bool_prop('shadow')
581+
582+
@shadow.setter
583+
def shadow(self, value):
584+
self._set_bool_prop('shadow', value)
585+
403586
@property
404587
def size(self):
405588
"""
@@ -426,6 +609,60 @@ def size(self, emu):
426609
rPr = self._element.get_or_add_rPr()
427610
rPr.sz_val = emu
428611

612+
@property
613+
def small_caps(self):
614+
"""
615+
Read/write tri-state value. When |True| causes the lowercase
616+
characters in the run to appear as capital letters two points smaller
617+
than the font size specified for the run.
618+
"""
619+
return self._get_bool_prop('smallCaps')
620+
621+
@small_caps.setter
622+
def small_caps(self, value):
623+
self._set_bool_prop('smallCaps', value)
624+
625+
@property
626+
def snap_to_grid(self):
627+
"""
628+
Read/write tri-state value. When |True| causes the run to use the
629+
document grid characters per line settings defined in the docGrid
630+
element when laying out the characters in this run.
631+
"""
632+
return self._get_bool_prop('snapToGrid')
633+
634+
@snap_to_grid.setter
635+
def snap_to_grid(self, value):
636+
self._set_bool_prop('snapToGrid', value)
637+
638+
@property
639+
def spec_vanish(self):
640+
"""
641+
Read/write tri-state value. When |True|, specifies that the given run
642+
shall always behave as if it is hidden, even when hidden text is
643+
being displayed in the current document. The property has a very
644+
narrow, specialized use related to the table of contents. Consult the
645+
spec (§17.3.2.36) for more details.
646+
"""
647+
return self._get_bool_prop('specVanish')
648+
649+
@spec_vanish.setter
650+
def spec_vanish(self, value):
651+
self._set_bool_prop('specVanish', value)
652+
653+
@property
654+
def strike(self):
655+
"""
656+
Read/write tri-state value. When |True| causes the text in the run
657+
to appear with a single horizontal line through the center of the
658+
line.
659+
"""
660+
return self._get_bool_prop('strike')
661+
662+
@strike.setter
663+
def strike(self, value):
664+
self._set_bool_prop('strike', value)
665+
429666
@property
430667
def underline(self):
431668
"""
@@ -446,6 +683,35 @@ def underline(self, value):
446683
rPr = self._element.get_or_add_rPr()
447684
rPr.u_val = value
448685

686+
@property
687+
def web_hidden(self):
688+
"""
689+
Read/write tri-state value. When |True|, specifies that the contents
690+
of this run shall be hidden when the document is displayed in web
691+
page view.
692+
"""
693+
return self._get_bool_prop('webHidden')
694+
695+
@web_hidden.setter
696+
def web_hidden(self, value):
697+
self._set_bool_prop('webHidden', value)
698+
699+
def _get_bool_prop(self, name):
700+
"""
701+
Return the value of boolean child of `w:rPr` having *name*.
702+
"""
703+
rPr = self._element.rPr
704+
if rPr is None:
705+
return None
706+
return rPr._get_bool_val(name)
707+
708+
def _set_bool_prop(self, name, value):
709+
"""
710+
Assign *value* to the boolean child *name* of `w:rPr`.
711+
"""
712+
rPr = self._element.get_or_add_rPr()
713+
rPr._set_bool_val(name, value)
714+
449715

450716
class _Text(object):
451717
"""

0 commit comments

Comments
 (0)