Skip to content

Commit 7afac0d

Browse files
committed
Fixed #21 - Do not use same styling on the parent and the children
1 parent 78aba5f commit 7afac0d

1 file changed

Lines changed: 61 additions & 50 deletions

File tree

ooxml/serialize.py

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def get_style_fontsize(node):
372372
return 0
373373

374374

375-
def get_style_css(ctx, node, embed=True):
375+
def get_style_css(ctx, node, embed=True, fontsize=-1):
376376
"""Returns as string defined CSS for this node.
377377
378378
Defined CSS can be different if it is embeded or no. When it is embeded styling
@@ -393,59 +393,60 @@ def get_style_css(ctx, node, embed=True):
393393
if not node:
394394
return
395395

396-
# temporarily
397-
if not embed:
398-
if 'b' in node.rpr:
399-
style.append('font-weight: bold')
400-
401-
if 'i' in node.rpr:
402-
style.append('font-style: italic')
396+
if fontsize in [-1, 2]:
397+
if 'sz' in node.rpr:
398+
size = int(node.rpr['sz']) / 2
403399

404-
if 'u' in node.rpr:
405-
style.append('text-decoration: underline')
400+
if ctx.options['embed_fontsize']:
401+
if ctx.options['scale_to_size']:
402+
multiplier = size-ctx.options['scale_to_size']
403+
scale = 100 + int(math.trunc(8.3*multiplier))
404+
style.append('font-size: {}%'.format(scale))
405+
else:
406+
style.append('font-size: {}pt'.format(size))
406407

407-
if 'small_caps' in node.rpr:
408-
style.append('font-variant: small-caps')
408+
if fontsize in [-1, 1]:
409+
# temporarily
410+
if not embed:
411+
if 'b' in node.rpr:
412+
style.append('font-weight: bold')
409413

410-
if 'strike' in node.rpr:
411-
style.append('text-decoration: line-through')
414+
if 'i' in node.rpr:
415+
style.append('font-style: italic')
412416

413-
if 'color' in node.rpr:
414-
if node.rpr['color'] != '000000':
415-
style.append('color: #{}'.format(node.rpr['color']))
417+
if 'u' in node.rpr:
418+
style.append('text-decoration: underline')
416419

417-
if 'jc' in node.ppr:
418-
# left right both
419-
align = node.ppr['jc']
420-
if align.lower() == 'both':
421-
align = 'justify'
420+
if 'small_caps' in node.rpr:
421+
style.append('font-variant: small-caps')
422422

423-
style.append('text-align: {}'.format(align))
423+
if 'strike' in node.rpr:
424+
style.append('text-decoration: line-through')
424425

425-
if 'sz' in node.rpr:
426-
size = int(node.rpr['sz']) / 2
426+
if 'color' in node.rpr:
427+
if node.rpr['color'] != '000000':
428+
style.append('color: #{}'.format(node.rpr['color']))
427429

428-
if ctx.options['embed_fontsize']:
429-
if ctx.options['scale_to_size']:
430-
multiplier = size-ctx.options['scale_to_size']
431-
scale = 100 + int(math.trunc(8.3*multiplier))
432-
style.append('font-size: {}%'.format(scale))
433-
else:
434-
style.append('font-size: {}pt'.format(size))
430+
if 'jc' in node.ppr:
431+
# left right both
432+
align = node.ppr['jc']
433+
if align.lower() == 'both':
434+
align = 'justify'
435435

436-
if 'ind' in node.ppr:
437-
if 'left' in node.ppr['ind']:
438-
size = int(node.ppr['ind']['left']) / 10
439-
style.append('margin-left: {}px'.format(size))
436+
style.append('text-align: {}'.format(align))
440437

441-
if 'right' in node.ppr['ind']:
442-
size = int(node.ppr['ind']['right']) / 10
443-
style.append('margin-right: {}px'.format(size))
438+
if 'ind' in node.ppr:
439+
if 'left' in node.ppr['ind']:
440+
size = int(node.ppr['ind']['left']) / 10
441+
style.append('margin-left: {}px'.format(size))
444442

445-
if 'first_line' in node.ppr['ind']:
446-
size = int(node.ppr['ind']['first_line']) / 10
447-
style.append('text-indent: {}px'.format(size))
443+
if 'right' in node.ppr['ind']:
444+
size = int(node.ppr['ind']['right']) / 10
445+
style.append('margin-right: {}px'.format(size))
448446

447+
if 'first_line' in node.ppr['ind']:
448+
size = int(node.ppr['ind']['first_line']) / 10
449+
style.append('text-indent: {}px'.format(size))
449450

450451
if len(style) == 0:
451452
return ''
@@ -516,7 +517,10 @@ def get_css_classes(document, style):
516517
>>> get_css_classes(doc, st)
517518
'header1 normal'
518519
"""
519-
return ' '.join([st.lower() for st in get_all_styles(document, style)[-1:]])
520+
lst = [st.lower() for st in get_all_styles(document, style)[-1:]] + \
521+
['{}-fontsize'.format(st.lower()) for st in get_all_styles(document, style)[-1:]]
522+
523+
return ' '.join(lst)
520524

521525

522526
def serialize_paragraph(ctx, document, par, root, embed=True):
@@ -600,8 +604,9 @@ def _add_formatting(f, new_element, _element):
600604

601605

602606
if ctx.options['embed_styles']:
603-
if _text_style != '':
607+
if _text_style != '' and _style != _text_style:
604608
new_element.set('style', _text_style)
609+
605610
# This is for situations when style has options and
606611
# text is trying to unset them
607612
# else:
@@ -612,23 +617,27 @@ def _add_formatting(f, new_element, _element):
612617

613618
if len(children) > 0:
614619
_child_style = children[-1].get('style') or ''
615-
616-
if new_element.tag == children[-1].tag and _text_style == _child_style and children[-1].tail is None:
620+
621+
if new_element.tag == children[-1].tag and (_text_style == _child_style or _child_style == '') and children[-1].tail is None:
617622
txt = children[-1].text or ''
618623
txt2 = new_element.text or ''
619624
children[-1].text = u'{}{}'.format(txt, txt2)
620625
was_inserted = True
621626

622627
if not was_inserted:
623-
if _style == '' and _text_style == '' and new_element.tag == 'span':
628+
# if _style == '' and _text_style == '' and new_element.tag == 'span':
629+
if _style == _text_style and new_element.tag == 'span':
630+
624631
_e = children[-1]
625632

626633
txt = _e.tail or ''
627634
_e.tail = u'{}{}'.format(txt, new_element.text)
628635
was_inserted = True
629636

630637
if not was_inserted:
631-
if _style == '' and _text_style == '' and new_element.tag == 'span':
638+
# if _style == '' and _text_style == '' and new_element.tag == 'span':
639+
if _style == _text_style and new_element.tag == 'span':
640+
632641
txt = elem.text or ''
633642
elem.text = u'{}{}'.format(txt, new_element.text)
634643
else:
@@ -1173,10 +1182,12 @@ def _generate(ctx, style_id, n):
11731182
else:
11741183
break
11751184

1176-
content = "\n".join([get_style_css(ctx, st, embed=False) for st in styles])
1177-
1185+
content = "\n".join([get_style_css(ctx, st, embed=False, fontsize=1) for st in styles])
11781186
css_content += "{0} .{1} {{ {2} }}\n\n".format(prefix, style_id.lower(), content)
11791187

1188+
content = "\n".join([get_style_css(ctx, st, embed=False, fontsize=2) for st in styles])
1189+
css_content += "{0} .{1}-fontsize {{ {2} }}\n\n".format(prefix, style_id.lower(), content)
1190+
11801191
return css_content
11811192

11821193
# Serialize list of elements into HTML

0 commit comments

Comments
 (0)