|
23 | 23 | NUM_PADDING_CHARS = 0.5 |
24 | 24 | DEFAULT_COLOR = '#4c1' |
25 | 25 | DEFAULT_TEXT_COLOR = '#fff' |
| 26 | +MASK_ID_PREFIX = 'anybadge_' |
26 | 27 |
|
27 | 28 | # Dictionary for looking up approx pixel widths of |
28 | 29 | # supported fonts and font sizes. |
|
51 | 52 | <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
52 | 53 | <stop offset="1" stop-opacity=".1"/> |
53 | 54 | </linearGradient> |
54 | | - <mask id="a"> |
| 55 | + <mask id="{{ mask id }}"> |
55 | 56 | <rect width="{{ badge width }}" height="20" rx="3" fill="#fff"/> |
56 | 57 | </mask> |
57 | | - <g mask="url(#a)"> |
| 58 | + <g mask="url(#{{ mask id }})"> |
58 | 59 | <path fill="#555" d="M0 0h{{ color split x }}v20H0z"/> |
59 | 60 | <path fill="{{ color }}" d="M{{ color split x }} 0h{{ value width }}v20H{{ color split x }}z"/> |
60 | 61 | <path fill="url(#b)" d="M0 0h{{ badge width }}v20H0z"/> |
@@ -207,6 +208,7 @@ def __init__(self, label, value, font_name=None, font_size=None, |
207 | 208 | self.value_text_color = text_colors[1] |
208 | 209 |
|
209 | 210 | self.use_max_when_value_exceeds = use_max_when_value_exceeds |
| 211 | + self.mask_id = self.__class__._get_next_mask_id() |
210 | 212 |
|
211 | 213 | def __repr__(self): |
212 | 214 | """Return a representation of the Badge object instance. |
@@ -259,6 +261,19 @@ def __repr__(self): |
259 | 261 | optional_args |
260 | 262 | ) |
261 | 263 |
|
| 264 | + @classmethod |
| 265 | + def _get_next_mask_id(cls): |
| 266 | + """Return a new mask ID from a singleton sequence maintained on the class. |
| 267 | +
|
| 268 | + Returns: str |
| 269 | + """ |
| 270 | + if not hasattr(cls, 'mask_id'): |
| 271 | + cls.mask_id = 0 |
| 272 | + |
| 273 | + cls.mask_id += 1 |
| 274 | + |
| 275 | + return MASK_ID_PREFIX + str(cls.mask_id) |
| 276 | + |
262 | 277 | @property |
263 | 278 | def value_is_float(self): |
264 | 279 | """Identify whether the value text is a float. |
@@ -412,7 +427,8 @@ def badge_svg_text(self): |
412 | 427 | .replace('{{ label text color }}', self.label_text_color) \ |
413 | 428 | .replace('{{ value text color }}', self.value_text_color) \ |
414 | 429 | .replace('{{ color split x }}', str(self.color_split_position)) \ |
415 | | - .replace('{{ value width }}', str(self.badge_width - self.color_split_position)) |
| 430 | + .replace('{{ value width }}', str(self.badge_width - self.color_split_position))\ |
| 431 | + .replace('{{ mask id }}', self.mask_id) |
416 | 432 |
|
417 | 433 | def __str__(self): |
418 | 434 | """Return string representation of badge. |
|
0 commit comments