Added full support for center_image

This commit is contained in:
nshan651
2022-10-08 21:17:29 -05:00
parent ba57b76687
commit df5fe9aaeb
3 changed files with 37 additions and 16 deletions

View File

@@ -122,7 +122,7 @@ def badge(
logo: Optional[str] = None,
left_color: str = '#555',
right_color: str = '#007ec6',
center_color: str = '#555',
center_color: Optional[str] = None,
measurer: Optional[text_measurer.TextMeasurer] = None,
left_title: Optional[str] = None,
right_title: Optional[str] = None,
@@ -193,11 +193,13 @@ def badge(
raise ValueError(
'whole_link may not bet set with left_link, right_link, or center_link'
)
# NOTE: Can only have a center_* element if also a right_* element
if center_image and not (right_image or right_text):
raise ValueError('cannot have a center_image without a right element')
if right_image and center_image:
raise ValueError('cannot have both right_image and center_image')
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
if (center_image and not center_color) or (not center_image and center_color):
raise ValueError('must have both a center_image and a center_color')
if logo and embed_logo:
logo = _embed_image(logo)
@@ -208,10 +210,15 @@ def badge(
if center_image and embed_center_image:
center_image = _embed_image(center_image)
if center_color:
center_color = _NAME_TO_COLOR.get(center_color, center_color)
right_text_width = None
if right_text:
right_text_width = measurer.text_width(right_text) / 10.0
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
svg = template.render(
left_text=left_text,
right_text=right_text,
@@ -224,7 +231,7 @@ def badge(
logo=logo,
left_color=_NAME_TO_COLOR.get(left_color, left_color),
right_color=_NAME_TO_COLOR.get(right_color, right_color),
center_color=_NAME_TO_COLOR.get(center_color, center_color),
center_color=center_color,
left_title=left_title,
right_title=right_title,
center_title=center_title,

View File

@@ -22,6 +22,7 @@ import sys
import tempfile
import webbrowser
sys.path.append('/home/nick/git/pybadges/')
import pybadges
from pybadges.version import __version__
@@ -71,7 +72,7 @@ def main():
help='the background color of the right-hand-side of the badge')
parser.add_argument(
'--center-color',
default='#555',
default=None,
help='the background color of the right-hand-side of the badge')
parser.add_argument('--browser',
action='store_true',

View File

@@ -1,8 +1,9 @@
{% set logo_width = 14 if logo else 0 %}
{% set logo_padding = 3 if (logo and left_text) else 0 %}
{% set image_width = 107 if center_image else 0 %}
{% set left_width = left_text_width + 10 + logo_width + logo_padding %}
{% set right_width = right_text_width + 10 if right_text_width else 0 %}
{% set image_width = 107 if right_image else 0 %}
{% set center_width = image_width + 10 if center_image else 0 %}
{% set right_width = center_width + right_text_width + 10 if right_text else 0 %}
{% set id_smooth = 'smooth' + id_suffix %}
{% set id_round = 'round' + id_suffix %}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{{ left_width + right_width }}" height="20">
@@ -19,31 +20,43 @@
</clipPath>
<g clip-path="url(#{{ id_round }})">
<rect width="{{ left_width }}" height="20" fill="{{ left_color }}">
<rect width="{{ left_width }}" height="20" fill="{{ left_color }}">
{% if left_title %}
<title>{{ left_title }}</title>
{% endif %}
</rect>
<rect x="{{ left_width }}" width="{{ right_width }}" height="20" fill="{{ right_color }}">
{% if center_image %}
<rect x="{{ left_width }}" width="{{ center_width }}" height="20" fill="{{ center_color }}">
{% if center_title %}
<title>{{ center_title }}</title>
{% endif %}
</rect>
{% endif %}
<rect x="{{ left_width + center_width }}" width="{{ right_width }}" height="20" fill="{{ right_color }}">
{% if right_title %}
<title>{{ right_title }}</title>
{% endif %}
</rect>
<rect x="{{ logo_width + 2*logo_padding }}" width="{{ left_text_width + 7 }}" height="20" fill="{{ left_color }}"/>
<rect width="{{ left_width + right_width }}" height="20" fill="url(#{{ id_smooth }})"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110">
{% if logo %}
<image x="5" y="3" width="{{ logo_width}}" height="14" xlink:href="{{ logo}}"/>
<image x="5" y="3" width="{{ logo_width }}" height="14" xlink:href="{{ logo}}"/>
{% endif %}
<text x="{{ (((left_width+logo_width+logo_padding)/2)+1)*10 }}" y="150" fill="#010101" fill-opacity=".3" transform="scale(0.1)" textLength="{{ (left_width-(10+logo_width+logo_padding))*10 }}" lengthAdjust="spacing">{{ left_text }}</text>
<text x="{{ (((left_width+logo_width+logo_padding)/2)+1)*10 }}" y="140" transform="scale(0.1)" textLength="{{ (left_width-(10+logo_width+logo_padding))*10 }}" lengthAdjust="spacing">{{ left_text }}</text>
{% if right_image %}
<image x="{{ left_width-image_width }}" y="3" width="{{ image_width }}" height="14" xlink:href="{{ right_image }}"/>
{% if center_image %}
<image x="{{ left_width }}" y="3" width="{{ center_width }}" height="14" xlink:href="{{ center_image }}"/>
{% endif %}
{% if right_text %}
<text x="{{ (left_width+right_width/2-1)*10 }}" y="150" fill="#010101" fill-opacity=".3" transform="scale(0.1)" textLength="{{ (right_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>
<text x="{{ (left_width+right_width/2-1)*10 }}" y="140" transform="scale(0.1)" textLength="{{ (right_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>
<text x="{{ (left_width+center_width/2+right_width/2-1)*10 }}" y="150" fill="#010101" fill-opacity=".3" transform="scale(0.1)" textLength="{{ (right_width-center_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>
<text x="{{ (left_width+center_width/2+right_width/2-1)*10 }}" y="140" transform="scale(0.1)" textLength="{{ (right_width-center_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>
{% endif %}
{% if left_link or whole_link %}