textgen: Move common code into a common file.

Reduce code duplication by refactoring the textgen and smtextgen
scripts.
This commit is contained in:
Simon Howard 2014-09-07 03:56:45 +00:00
parent b6c26942b4
commit 5405104814
3 changed files with 50 additions and 82 deletions

46
graphics/text/common.py Normal file
View file

@ -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)

View file

@ -35,12 +35,9 @@
from glob import glob from glob import glob
import sys import sys
import subprocess
import re import re
# ImageMagick commands used by this script: from common import *
CONVERT_COMMAND = 'convert'
IDENTIFY_COMMAND = 'identify'
# Background color for output files. # Background color for output files.
BACKGROUND_COLOR = '#00ffff' BACKGROUND_COLOR = '#00ffff'
@ -49,45 +46,11 @@ BACKGROUND_COLOR = '#00ffff'
SPACE_WIDTH = 4 SPACE_WIDTH = 4
# Height of the font. # Height of the font.
FONT_HEIGHT = 7 FONT_HEIGHT = 8
# Regexp to match dimensions/x,y coordinate pair. # Regexp to match dimensions/x,y coordinate pair.
DIMENSION_MATCH_RE = re.compile(r'(\d+)[x,](\d+)') 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): class Font(object):
def __init__(self): def __init__(self):
self.get_font_widths() self.get_font_widths()

View file

@ -35,15 +35,11 @@
# #
from glob import glob from glob import glob
from sys import stdout
import subprocess
import re import re
import sys
from config import * from config import *
from common import *
# ImageMagick commands used by this script:
CONVERT_COMMAND = 'convert'
IDENTIFY_COMMAND = 'identify'
# ImageMagick -colorize parameters for colorizing text: # ImageMagick -colorize parameters for colorizing text:
COLOR_BLUE = (100, 100, 0) COLOR_BLUE = (100, 100, 0)
@ -63,43 +59,6 @@ UPPERCASE_FONT = False
# Width of a space character in pixels. # Width of a space character in pixels.
SPACE_WIDTH = 7 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): class Font(object):
def __init__(self, fontdir, kerning_table={}): def __init__(self, fontdir, kerning_table={}):
self.fontdir = fontdir self.fontdir = fontdir