From 732a4c79bfa360aca7315b7156dc622edd916ad2 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 16 Dec 2016 13:17:42 -0800 Subject: [PATCH] rewrite inline image replacement (#135) * rewrite inline image replacement * make a note about `python setup.py develop` * Tweak to the VP size --- README.md | 4 +++ domdiv/draw.py | 78 +++++++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 5256310..5a5490a 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,8 @@ Run `python setup.py install`. This should install the needed prerequisites and ## Using as a library The library will be installed as `domdiv` with the main entry point being `domdiv.main.generate(options)`. It takes a `Namespace` of options as generated by python's `argparser` module. You can either use `domdiv.main.parse_opts(cmdline_args)` to get such an object by passing in a list of command line options (like `sys.argv`), or directly create an appropriate object by assigning the correct values to its attributes, starting from an empty class or an actual argparse `Namespace` object. +## Developing + +You can use `python setup.py develop` to install the `dominion_dividers` script so that it calls your checked out code, enabling you to run edited code without having to perform an install every time. + Feel free to comment on boardgamegeek at or file issues on github (). diff --git a/domdiv/draw.py b/domdiv/draw.py index 696195f..9c2c9a4 100644 --- a/domdiv/draw.py +++ b/domdiv/draw.py @@ -225,46 +225,52 @@ class DividerDrawer(object): self.canvas.save() def add_inline_images(self, text, fontsize): + def replace_image_tag(text, + fontsize, + tag_pattern, + fname_replace, + fontsize_multiplier, + height_percent, + text_fontsize_multiplier=None): + replace = ' ' + offset = 0 + for match in re.finditer(tag_pattern, text): + tag = match.group(0) + fname = re.sub(tag_pattern, fname_replace, tag) + if text_fontsize_multiplier is not None: + font_replace = re.sub(tag_pattern, + '\\1'.format(fontsize * text_fontsize_multiplier), + tag) + replace = font_replace + replace + replace = replace.format(fpath=DividerDrawer.get_image_filepath(fname), + width=fontsize * fontsize_multiplier, + height_percent=height_percent) + text = text[:match.start() + offset] + replace + text[match.end() + offset:] + offset += len(replace) - len(match.group(0)) + return text # Coins - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('coin_small_1.png'), fontsize * 2.4) - text = re.sub('(\d+)\s*\<\*COIN\*\>', replace, text) - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('coin_small_1.png'), fontsize * 1.2) - text = re.sub('(\d+)\s(c|C)oin(s)?', replace, text) - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('coin_small_question.png'), fontsize * 1.2) - text = re.sub('\?\s(c|C)oin(s)?', replace, text) - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('coin_small_empty.png'), fontsize * 1.2) - text = re.sub('empty\s(c|C)oin(s)?', replace, text) - text = re.sub('\_\s(c|C)oin(s)?', replace, text) + replace_specs = [ + # Coins + (r'(\d+)\s\<\*COIN\*\>', 'coin_small_\\1.png', 2.4, 200), + (r'(\d+)\s(c|C)oin(s)?', 'coin_small_\\1.png', 1.2, 100), + (r'\?\s(c|C)oin(s)?', 'coin_small_question.png', 1.2, 100), + (r'(empty|\_)\s(c|C)oin(s)?', 'coin_small_empty.png', 1.2, 100), - # VP - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('victory_emblem.png'), fontsize * 1.5) - text = re.sub('(?:\s+|\<)VP(?:\s+|\>|\.|$)', replace, text) - replace = '\\1 ' - replace += '' - replace = replace.format(fontsize * 1.5, DividerDrawer.get_image_filepath('victory_emblem.png'), fontsize * 2.5) - text = re.sub('(\d+)\s*\<\*VP\*\>', replace, text) + # VP + (r'(?:\s+|\<)VP(?:\s+|\>|\.|$)', 'victory_emblem.png', 1.25, 100), + (r'(\d+)\s*\<\*VP\*\>', 'victory_emblem.png', 2, 160, 1.3), - # Debt - replace = ' ' - replace = replace.format(DividerDrawer.get_image_filepath('debt_1.png'), fontsize * 1.2) - text = re.sub('(\d+)\sDebt', replace, text) - replace = ' ' - replace = replace.format(DividerDrawer.get_image_filepath('debt.png'), fontsize * 1.2) - text = re.sub('Debt', replace, text) + # Debt + (r'(\d+)\sDebt', 'debt_\\1.png', 1.2, 105), + (r'Debt', 'debt.png', 1.2, 105), - # Potion - replace = '\\1 ' - replace += '' - replace = replace.format(fontsize * 1.5, DividerDrawer.get_image_filepath('potion_small.png'), fontsize * 2.0) - text = re.sub('(\d+)\s*\<\*POTION\*\>', replace, text) - replace = '' - replace = replace.format(DividerDrawer.get_image_filepath('potion_small.png'), fontsize * 1.2) - text = re.sub('Potion', replace, text) + # Potion + (r'(\d+)\s*\<\*POTION\*\>', 'potion_small.png', 2, 140, 1.5), + (r'Potion', 'potion_small.png', 1.2, 100) + + ] + for args in replace_specs: + text = replace_image_tag(text, fontsize, *args) return text.strip()