コード例 #1
0
def p_border_style(key, value):
    style_attrs = {}
    if key == 'border':
        tab = value.split()
        for element in tab:
            size = format_size(element, None)
            if size is not None:
                style_attrs['borderWidth'] = size
                continue
            color = get_color_as_hexa(element, None)
            if color is not None:
                style_attrs['borderColor'] = color
                continue
    elif key == 'border-bottom':
        tab = value.split()
        for element in tab:
            size = format_size(element, None)
            if size is not None:
                style_attrs['borderWidth'] = size
                continue
            color = get_color_as_hexa(element, None)
            if color is not None:
                style_attrs['borderColor'] = color
                continue
    elif key == 'border-color':
        color = get_color_as_hexa(value, None)
        if color is not None:
            style_attrs['borderColor'] = color
    elif key == 'border-width':
        size = format_size(value, None)
        if size is not None:
            style_attrs['borderWidth'] = size
    return style_attrs
コード例 #2
0
def inline_color_style(key, value, context):
    style = None
    if key == 'color':
        style = ('span', {(None, key): get_color_as_hexa(value)})
    elif key in ('background-color'):
        style = ('span', {(None, 'backColor'): get_color_as_hexa(value)})
    return style
コード例 #3
0
ファイル: style.py プロジェクト: kennym/itools
def inline_color_style(key, value, context):
    style = None
    if key == 'color':
        style = ('span', {(None, key): get_color_as_hexa(value)})
    elif key in ('background-color'):
        style = ('span', {(None, 'backColor'): get_color_as_hexa(value)})
    return style
コード例 #4
0
ファイル: style.py プロジェクト: kennym/itools
def p_border_style(key, value):
    style_attrs = {}
    if key == 'border':
        tab = value.split()
        for element in tab:
            size = format_size(element, None)
            if size is not None:
                style_attrs['borderWidth'] = size
                continue
            color = get_color_as_hexa(element, None)
            if color is not None:
                style_attrs['borderColor'] = color
                continue
    elif key == 'border-bottom':
        tab = value.split()
        for element in tab:
            size = format_size(element, None)
            if size is not None:
                style_attrs['borderWidth'] = size
                continue
            color = get_color_as_hexa(element, None)
            if color is not None:
                style_attrs['borderColor'] = color
                continue
    elif key == 'border-color':
        color = get_color_as_hexa(value, None)
        if color is not None:
            style_attrs['borderColor'] = color
    elif key == 'border-width':
        size = format_size(value, None)
        if size is not None:
            style_attrs['borderWidth'] = size
    return style_attrs
コード例 #5
0
def compute_table_border(key, value):
    """Key is one of these values
    border
    border-XXX-width
    border-XXX-style
    border-XXX-color
    width XXX equals to top, right, bottom, left

    with border equals to
    border-top + border-right + border-bottom + border-left
    """

    if key == 'border':
        # explode the key in sub key
        # border-top
        # border-right
        # border-bottom
        # border-left
        return _compute_table_border_full(value)
    elif key in ('border-top-width', 'border-right-width',
                 'border-bottom-width', 'border-left-width'):
        return {key: format_size(value, 1)}
    elif key in ('border-top-style', 'border-right-style',
                 'border-bottom-style', 'border-left-style'):
        return {key: compute_border_style(value)}
    elif key in ('border-top-color', 'border-right-color',
                 'border-bottom-color', 'border-left-color'):
        return {key: get_color_as_hexa(value)}
    return None
コード例 #6
0
ファイル: style.py プロジェクト: kennym/itools
def compute_table_border(key, value):
    """Key is one of these values
    border
    border-XXX-width
    border-XXX-style
    border-XXX-color
    width XXX equals to top, right, bottom, left

    with border equals to
    border-top + border-right + border-bottom + border-left
    """

    if key == 'border':
        # explode the key in sub key
        # border-top
        # border-right
        # border-bottom
        # border-left
        return _compute_table_border_full(value)
    elif key in ('border-top-width',
                 'border-right-width',
                 'border-bottom-width',
                 'border-left-width'):
        return {key: format_size(value, 1)}
    elif key in ('border-top-style',
                 'border-right-style',
                 'border-bottom-style',
                 'border-left-style'):
        return {key: compute_border_style(value)}
    elif key in ('border-top-color',
                 'border-right-color',
                 'border-bottom-color',
                 'border-left-color'):
        return {key: get_color_as_hexa(value)}
    return None
コード例 #7
0
def _calculate_table_border(value):
    """INPUT border: 1px solid red
    Calculate the 3 attributes
    width, style, color"""
    css_value = value.strip()
    if css_value.endswith(';'):
        css_value = css_value[:-1]
    values = value.split()
    if len(values) == 3:
        # 1px solid red
        width, style, color = values
        width = format_size(width, 1)
        style = compute_border_style(style)
        color = get_color_as_hexa(color)
    else:
        # Not well formed
        return None

    return (width, style, color)
コード例 #8
0
ファイル: style.py プロジェクト: kennym/itools
def _calculate_table_border(value):
    """INPUT border: 1px solid red
    Calculate the 3 attributes
    width, style, color"""
    css_value = value.strip()
    if css_value.endswith(';'):
        css_value = css_value[:-1]
    values = value.split()
    if len(values) == 3:
        # 1px solid red
        width, style, color = values
        width = format_size(width, 1)
        style = compute_border_style(style)
        color = get_color_as_hexa(color)
    else:
        # Not well formed
        return None

    return (width, style, color)
コード例 #9
0
def build_paragraph_style(context, element, style_css):
    style_attr = {}
    # The default style is Normal
    parent_style_name = 'Normal'
    bulletText = None

    style_attr['autoLeading'] = 'max'
    style_attr['borderPadding'] = 0.1 * cm  # TODO Check if it's correct
    style_attr['leading'] = 0.3 * cm

    #FIXME must be moved in default css
    style_attr['spaceBefore'] = 0.3 * cm
    style_attr['spaceAfter'] = 0.3 * cm
    # Leading
    leading_forced = False
    font_size = None

    for key, value in style_css.iteritems():
        if key == 'color':
            style_attr['textColor'] = get_color_as_hexa(value)
        elif key in ('background-color'):
            style_attr['backColor'] = get_color_as_hexa(value)
        elif key == 'text-indent':
            style_attr['firstLineIndent'] = format_size(value)
        elif key == 'text-align':
            if value in P_ALIGNMENTS.keys():
                style_attr['alignment'] = P_ALIGNMENTS.get(value)
        elif key == 'text-decoration':
            if value == 'underline':
                context.style_tag_stack.append(('u'))
        elif element[0] not in ('td', 'th') and key.startswith('border'):
            style_attr.update(p_border_style(key, value))
        elif key.startswith('font'):
            font_style = p_font_style(key, value, context)
            if 'fontSize' in font_style:
                font_size = font_style['fontSize']
            style_attr.update(font_style)
        elif key.startswith('padding'):
            style_attr.update(p_padding_style(key, value))
        elif key.startswith('line-height'):
            leading_forced = True
            style_attr['leading'] = format_size(value)
        elif key == 'width':
            style_attr['width'] = value
        elif key == 'float':
            style_attr['float'] = value
        elif key == 'text-transform':
            if value in ('uppercase', 'lowercase'):
                style_attr['textTransform'] = value

    # Overload the attributes values
    for key, attr_value in element[1].iteritems():
        key = key[1]  # (None, key)
        if key == 'class':
            # Set the parent style for inheritance
            parent_style_name = attr_value
        elif key == 'bulletText':
            bulletText = attr_value
        elif key == 'style':
            # TODO parse inline style attribute
            continue

    if element[0] in HEADING + ('toctitle', ):
        parent_style_name = element[0]
    style_name = parent_style_name
    parent_style = context.get_style(parent_style_name)

    # Calulate the leading
    if leading_forced is False and font_size:
        # Reportlab UserGuide
        # a good rule of thumb is to make this 20% larger than the point size
        # But we choose to use a ratio of 33%
        style_attr['leading'] = font_size * 1.33

    return (ParagraphStyle(style_name, parent=parent_style,
                           **style_attr), bulletText)
コード例 #10
0
ファイル: style.py プロジェクト: kennym/itools
def build_paragraph_style(context, element, style_css):
    style_attr = {}
    # The default style is Normal
    parent_style_name = 'Normal'
    bulletText = None

    style_attr['autoLeading'] = 'max'
    style_attr['borderPadding'] = 0.1 * cm # TODO Check if it's correct
    style_attr['leading'] = 0.3 * cm

    #FIXME must be moved in default css
    style_attr['spaceBefore'] = 0.3 * cm
    style_attr['spaceAfter'] = 0.3 * cm
    # Leading
    leading_forced = False
    font_size = None

    for key, value in style_css.iteritems():
        if key == 'color':
            style_attr['textColor'] = get_color_as_hexa(value)
        elif key in ('background-color'):
            style_attr['backColor'] = get_color_as_hexa(value)
        elif key == 'text-indent':
            style_attr['firstLineIndent'] = format_size(value)
        elif key == 'text-align':
            if value in P_ALIGNMENTS.keys():
                style_attr['alignment'] = P_ALIGNMENTS.get(value)
        elif key == 'text-decoration':
            if value == 'underline':
                context.style_tag_stack.append(('u'))
        elif element[0] not in ('td', 'th') and key.startswith('border'):
            style_attr.update(p_border_style(key, value))
        elif key.startswith('font'):
            font_style = p_font_style(key, value, context)
            if 'fontSize' in font_style:
                font_size = font_style['fontSize']
            style_attr.update(font_style)
        elif key.startswith('padding'):
            style_attr.update(p_padding_style(key, value))
        elif key.startswith('line-height'):
            leading_forced = True
            style_attr['leading'] = format_size(value)
        elif key == 'width':
            style_attr['width'] = value
        elif key == 'float':
            style_attr['float'] = value
        elif key == 'text-transform':
            if value in ('uppercase', 'lowercase'):
                style_attr['textTransform'] = value


    # Overload the attributes values
    for key, attr_value in element[1].iteritems():
        key = key[1] # (None, key)
        if key == 'class':
            # Set the parent style for inheritance
            parent_style_name = attr_value
        elif key == 'bulletText':
            bulletText = attr_value
        elif key == 'style':
            # TODO parse inline style attribute
            continue

    if element[0] in HEADING + ('toctitle', ):
        parent_style_name = element[0]
    style_name = parent_style_name
    parent_style = context.get_style(parent_style_name)

    # Calulate the leading
    if leading_forced is False and font_size:
        # Reportlab UserGuide
        # a good rule of thumb is to make this 20% larger than the point size
        # But we choose to use a ratio of 33%
        style_attr['leading'] = font_size * 1.33

    return (ParagraphStyle(style_name, parent=parent_style, **style_attr),
            bulletText)