Commit cfec7a0e authored by su-v's avatar su-v

Split fix_blank_lines() into reusable functions

parent 87791034
......@@ -64,7 +64,7 @@ def parse_length(val):
def max_length(length1, length2):
"""Compare length values, return max length of two."""
# TODO: compare lengths in same units
# FIXME: compare lengths in same base unit
val1 = length1[0] or 0
val2 = length2[0] or 0
return length1 if val1 >= val2 else length2
......@@ -72,7 +72,7 @@ def max_length(length1, length2):
def min_length(length1, length2):
"""Compare length values, return min length of two."""
# TODO: compare lengths in same units
# FIXME: compare lengths in same base unit
val1 = length1[0] or 0
val2 = length2[0] or 0
return length1 if val1 <= val2 else length2
......@@ -80,7 +80,7 @@ def min_length(length1, length2):
def eq_length(length1, length2):
"""Compare length values for equaliy."""
# TODO: comvert to same units, test for nearness.
# FIXME: test lengths for nearness in same base unit
val1 = length1[0] or 0
val2 = length2[0] or 0
return True if val1 == val2 else False
......@@ -209,6 +209,18 @@ def is_blank(node):
return not textify(node).strip()
def set_blank(node, fontsize, lineheight, debug=False):
"""Replace blank line with placeholder character, update props."""
cdict = simplestyle.parseStyle(node.get('style'))
node.text = BLANK_LINE if not debug else BLANK_DEBUG
if fontsize is not None:
cdict['font-size'] = fontsize
node.set('style', simplestyle.formatStyle(cdict))
if lineheight is not None:
cdict['line-height'] = lineheight
node.set('style', simplestyle.formatStyle(cdict))
def css_strut(node):
"""Return value of CSS 'strut'.
......@@ -228,6 +240,38 @@ def css_strut(node):
}
def get_max_line_height(node, strut):
"""Parse node and descendants for max height (CSS line box)."""
fs_length = get_max_length_prop(node, 'font-size')
if fs_length[0] is not None:
fontsize = print_length(fs_length)
else:
fontsize = strut['font-size']
# FIXME: support unitless line-height values (1.0 vs 100%)
lh_length = get_max_length_prop(node, 'line-height')
if lh_length[0] is not None:
lineheight = print_length(lh_length)
else:
lineheight = strut['line-height']
return fontsize, lineheight
def get_min_line_height(node, strut):
"""Parse node and descendants for min height (CSS line box)."""
fs_length = get_max_length_prop(node, 'font-size')
if fs_length[0] is not None:
fontsize = print_length(fs_length)
else:
fontsize = strut['font-size']
# FIXME: support unitless line-height values (1.0 vs 100%)
lh_length = get_min_length_prop(node, 'line-height')
if lh_length[0] is not None:
lineheight = print_length(lh_length)
else:
lineheight = strut['line-height']
return fontsize, lineheight
def fix_line_spacing(node):
"""Fix line spacing (copy to inner, zero on outer style)."""
prop = 'line-height'
......@@ -285,54 +329,27 @@ def fix_font_size(node):
def fix_blank_line(node, debug=False):
"""Fix blank lines (insert space, copy font-size from prior line)."""
# pylint: disable=too-many-branches
lineheight = lh_strut = css_strut(node)['line-height']
fontsize = fs_strut = css_strut(node)['font-size']
strut = css_strut(node)
fontsize = strut['font-size']
lineheight = strut['line-height']
# inner style
for child in node.iterchildren(tag=inkex.etree.Element):
cdict = simplestyle.parseStyle(child.get('style'))
if is_tspan(child) and is_line(child):
# regular text
if is_blank(child):
child.text = BLANK_LINE if not debug else BLANK_DEBUG
if fontsize is not None:
cdict['font-size'] = fontsize
child.set('style', simplestyle.formatStyle(cdict))
if lineheight is not None:
cdict['line-height'] = lineheight
child.set('style', simplestyle.formatStyle(cdict))
# add placeholder character, set props from prior line
set_blank(child, fontsize, lineheight, debug)
else:
fs_length = get_max_length_prop(child, 'font-size')
if fs_length[0] is not None:
fontsize = print_length(fs_length)
else:
fontsize = fs_strut
# TODO: does not handle unitless vs % (both relative)
lh_length = get_max_length_prop(child, 'line-height')
if lh_length[0] is not None:
lineheight = print_length(lh_length)
else:
lineheight = lh_strut
# store properties contributing to current CSS line box
fontsize, lineheight = get_max_line_height(child, strut)
elif is_flowpara(child):
# flowed text
if is_blank(child):
child.text = BLANK_LINE if not debug else BLANK_DEBUG
if fontsize is not None:
cdict['font-size'] = fontsize
child.set('style', simplestyle.formatStyle(cdict))
if lineheight is not None:
cdict['line-height'] = lineheight
child.set('style', simplestyle.formatStyle(cdict))
# add placeholder character, set props from prior line
set_blank(child, fontsize, lineheight, debug)
else:
fs_length = get_max_length_prop(child, 'font-size')
if fs_length[0] is not None:
fontsize = print_length(fs_length)
else:
fontsize = fs_strut
# TODO: does not handle unitless vs % (both relative)
lh_length = get_max_length_prop(child, 'line-height')
if lh_length[0] is not None:
lineheight = print_length(lh_length)
else:
lineheight = lh_strut
# store properties contributing to current CSS line box
fontsize, lineheight = get_max_line_height(child, strut)
def fix_font_name(node):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment