From 5405104814a6d0b19f4f8b9d9f33a010a2008083 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 7 Sep 2014 03:56:45 +0000 Subject: [PATCH] textgen: Move common code into a common file. Reduce code duplication by refactoring the textgen and smtextgen scripts. --- graphics/text/common.py | 46 +++++++++++++++++++++++++++++++++++++++++ graphics/text/smtextgen | 41 ++---------------------------------- graphics/text/textgen | 45 ++-------------------------------------- 3 files changed, 50 insertions(+), 82 deletions(-) create mode 100644 graphics/text/common.py diff --git a/graphics/text/common.py b/graphics/text/common.py new file mode 100644 index 00000000..b44bc5cb --- /dev/null +++ b/graphics/text/common.py @@ -0,0 +1,46 @@ + +import re +import subprocess +import sys + +# ImageMagick commands used by this script: +CONVERT_COMMAND = 'convert' +IDENTIFY_COMMAND = 'identify' + +# Output from 'identify' looks like this: +# fontchars/font033.gif GIF 9x16 9x16+0+0 8-bit sRGB 32c 194B 0.000u 0:00.000 +IDENTIFY_OUTPUT_RE = re.compile(r'(\S+)\s(\S+)\s(\d+)x(\d+)\s') + +# Regexp to identify strings that are all lowercase (can use shorter height) +LOWERCASE_RE = re.compile(r'^[a-z\!\. ]*$') + +def get_image_dimensions(filename): + proc = subprocess.Popen([IDENTIFY_COMMAND, filename], + stdout=subprocess.PIPE) + proc.wait() + + line = proc.stdout.readline().decode('utf-8') + match = IDENTIFY_OUTPUT_RE.match(line) + assert match is not None + return (int(match.group(3)), int(match.group(4))) + +def invoke_command(command): + """Invoke a command, printing the command to stdout. + + Args: + command: Command and arguments as a list. + """ + for arg in command: + if arg.startswith('-'): + sys.stdout.write("\\\n ") + + if ' ' in arg or '#' in arg: + sys.stdout.write(repr(arg)) + else: + sys.stdout.write(arg) + + sys.stdout.write(' ') + + sys.stdout.write('\n') + return subprocess.call(command) + diff --git a/graphics/text/smtextgen b/graphics/text/smtextgen index f790d050..fb903fdf 100755 --- a/graphics/text/smtextgen +++ b/graphics/text/smtextgen @@ -35,12 +35,9 @@ from glob import glob import sys -import subprocess import re -# ImageMagick commands used by this script: -CONVERT_COMMAND = 'convert' -IDENTIFY_COMMAND = 'identify' +from common import * # Background color for output files. BACKGROUND_COLOR = '#00ffff' @@ -49,45 +46,11 @@ BACKGROUND_COLOR = '#00ffff' SPACE_WIDTH = 4 # Height of the font. -FONT_HEIGHT = 7 +FONT_HEIGHT = 8 # Regexp to match dimensions/x,y coordinate pair. DIMENSION_MATCH_RE = re.compile(r'(\d+)[x,](\d+)') -# Output from 'identify' looks like this: -# fontchars/font033.gif GIF 9x16 9x16+0+0 8-bit sRGB 32c 194B 0.000u 0:00.000 -IDENTIFY_OUTPUT_RE = re.compile(r'(\S+)\s(\S+)\s(\d+)x(\d+)\s') - -def get_image_dimensions(filename): - proc = subprocess.Popen([IDENTIFY_COMMAND, filename], - stdout=subprocess.PIPE) - proc.wait() - - line = proc.stdout.readline().decode('utf-8') - match = IDENTIFY_OUTPUT_RE.match(line) - assert match is not None - return (int(match.group(3)), int(match.group(4))) - -def invoke_command(command): - """Invoke a command, printing the command to stdout. - - Args: - command: Command and arguments as a list. - """ - for arg in command: - if arg.startswith('-'): - sys.stdout.write("\\\n ") - - if ' ' in arg or '#' in arg: - sys.stdout.write(repr(arg)) - else: - sys.stdout.write(arg) - - sys.stdout.write(' ') - - sys.stdout.write('\n') - return subprocess.call(command) - class Font(object): def __init__(self): self.get_font_widths() diff --git a/graphics/text/textgen b/graphics/text/textgen index 17f4ce73..186cdaf4 100755 --- a/graphics/text/textgen +++ b/graphics/text/textgen @@ -35,15 +35,11 @@ # from glob import glob -from sys import stdout -import subprocess import re +import sys from config import * - -# ImageMagick commands used by this script: -CONVERT_COMMAND = 'convert' -IDENTIFY_COMMAND = 'identify' +from common import * # ImageMagick -colorize parameters for colorizing text: COLOR_BLUE = (100, 100, 0) @@ -63,43 +59,6 @@ UPPERCASE_FONT = False # Width of a space character in pixels. SPACE_WIDTH = 7 -# Output from 'identify' looks like this: -# fontchars/font033.gif GIF 9x16 9x16+0+0 8-bit sRGB 32c 194B 0.000u 0:00.000 -IDENTIFY_OUTPUT_RE = re.compile(r'(\S+)\s(\S+)\s(\d+)x(\d+)\s') - -# Regexp to identify strings that are all lowercase (can use shorter height) -LOWERCASE_RE = re.compile(r'^[a-z\!\. ]*$') - -def get_image_dimensions(filename): - proc = subprocess.Popen([IDENTIFY_COMMAND, filename], - stdout=subprocess.PIPE) - proc.wait() - - line = proc.stdout.readline().decode('utf-8') - match = IDENTIFY_OUTPUT_RE.match(line) - assert match is not None - return (int(match.group(3)), int(match.group(4))) - -def invoke_command(command): - """Invoke a command, printing the command to stdout. - - Args: - command: Command and arguments as a list. - """ - for arg in command: - if arg.startswith('-'): - stdout.write("\\\n ") - - if ' ' in arg or '#' in arg: - stdout.write(repr(arg)) - else: - stdout.write(arg) - - stdout.write(' ') - - stdout.write('\n') - return subprocess.call(command) - class Font(object): def __init__(self, fontdir, kerning_table={}): self.fontdir = fontdir