From 215b7c644edb4aa72ed52902e692d312b3a22445 Mon Sep 17 00:00:00 2001 From: Nick Zatkovich Date: Sun, 30 Jul 2017 23:26:52 -0700 Subject: [PATCH] BUILD: remove the last of the imagemagick stuff The only remaining thing depending on imagemagick are the dist scripts --- Makefile | 4 +- graphics/text/.gitignore | 2 + graphics/text/Makefile | 49 +++++++++++++------ graphics/text/common.py | 46 ------------------ graphics/text/create_caption.py | 40 +++++++++++++++ graphics/text/image_dimensions.py | 23 +++++++++ graphics/text/{smtextgen => smtextgen.py} | 59 ++++++++++++----------- graphics/text/{textgen => textgen.py} | 29 +++++------ graphics/text/tint.py | 12 ++--- graphics/titlepic/.gitignore | 2 - graphics/titlepic/Makefile | 19 -------- graphics/titlepic/README | 6 +-- graphics/titlepic/create_caption | 40 --------------- 13 files changed, 149 insertions(+), 182 deletions(-) delete mode 100644 graphics/text/common.py create mode 100644 graphics/text/create_caption.py create mode 100644 graphics/text/image_dimensions.py rename graphics/text/{smtextgen => smtextgen.py} (66%) rename graphics/text/{textgen => textgen.py} (83%) delete mode 100644 graphics/titlepic/.gitignore delete mode 100644 graphics/titlepic/Makefile delete mode 100755 graphics/titlepic/create_caption diff --git a/Makefile b/Makefile index 32a8cde1..4508f4ff 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,7 @@ OBJS=$(FREEDM) $(FREEDOOM1) $(FREEDOOM2) all: $(OBJS) subdirs: - $(MAKE) -C graphics/text - $(MAKE) VERSION=$(VERSION) -C graphics/titlepic + $(MAKE) VERSION=$(VERSION) -C graphics/text $(MAKE) -C lumps/playpal $(MAKE) -C lumps/colormap $(MAKE) -C lumps/genmidi @@ -111,7 +110,6 @@ clean: $(MAKE) -C dist clean $(MAKE) -C graphics/text clean - $(MAKE) -C graphics/titlepic clean $(MAKE) -C lumps/playpal clean $(MAKE) -C lumps/colormap clean $(MAKE) -C lumps/genmidi clean diff --git a/graphics/text/.gitignore b/graphics/text/.gitignore index 4701c6de..817187ca 100644 --- a/graphics/text/.gitignore +++ b/graphics/text/.gitignore @@ -16,3 +16,5 @@ credtext*.png freettl.png helptext*.png wikilrs_horiz.png +fd?title.png +m_*.png diff --git a/graphics/text/Makefile b/graphics/text/Makefile index f6107cd1..a9727b28 100644 --- a/graphics/text/Makefile +++ b/graphics/text/Makefile @@ -52,20 +52,20 @@ TEXTGEN_GRAPHIC_LUMPS = \ TEXTGEN_GRAPHICS = $(TEXTGEN_GRAPHIC_LUMPS) \ helpttl.png freettl.png -all: textgen.mk help.png credit.png wikilrs.png wivctms.png +all: text_strings help.png credit.png wikilrs.png wivctms.png titlepic # Generate the menu and level strings -textgen.mk: config.py fontchars ../../lumps/dehacked.lmp - ./textgen $@ +text_strings: config.py fontchars ../../lumps/dehacked.lmp + python textgen.py cp $(TEXTGEN_GRAPHIC_LUMPS) ../ # Background for the help screen is a color shifted version of INTERPIC: helpbg.png: ../interpic.png - python tint.py ../interpic.png '#5599ff' helpbg.png + python tint.py ../interpic.png '#57b9b0' helpbg.png # Generate transparent image containing text for the HELP screen: -help.png: helpttl.png helpbg.png - python smtextgen help.png 320x200 \ +help.png: text_strings helpbg.png + python smtextgen.py help.png 320x200 \ -background "helpbg.png" \ 150,5 "file:helpttl.png" \ 10,25 "Weapons" \ @@ -97,8 +97,8 @@ help.png: helpttl.png helpbg.png 128,107 "file:../../sprites/pstra0.png" \ 170,115 "Armor" \ 220,113 "file:../../sprites/bon2a0.png" \ - 240,105 "file:../../sprites/arm1b0.png" \ - 280,105 "file:../../sprites/arm2b0.png" \ + 240,113 "file:../../sprites/arm1b0.png" \ + 280,110 "file:../../sprites/arm2b0.png" \ 145,140 "Map" \ 175,130 "file:../../sprites/pmapa0.png" \ 10,140 "Overdrive" \ @@ -122,30 +122,47 @@ help.png: helpttl.png helpbg.png cp $@ ../ -credit.png: freettl.png helpbg.png credit.txt - python smtextgen credit.png 320x200 \ +credit.png: text_strings helpbg.png credit.txt + python smtextgen.py credit.png 320x200 \ -background "helpbg.png" \ 120,5 "file:freettl.png" \ 10,30 "include:credit.txt" + cp $@ ../ wikilrs_horiz.png: - python smtextgen $@ 49x7 \ + python smtextgen.py $@ 49x7 \ 0,0 killers wikilrs.png: wikilrs_horiz.png - python rotate.py wikilrs_horiz.png 90 $@ + python rotate.py wikilrs_horiz.png -90 $@ cp $@ ../ wivctms.png: - python smtextgen $@ 54x7 \ + python smtextgen.py $@ 54x7 \ 0,0 victims cp $@ ../ +titlepic: fd1title.png fd2title.png fdmtitle.png + +fd1title.png: text_strings ../titlepic/titlepic.png ../m_doom.png ../t_phase1.png + python create_caption.py ../titlepic/titlepic.png ../m_doom.png ../t_phase1.png $@ + cp $@ ../ + +fd2title.png: text_strings ../titlepic/titlepic.png ../m_doom.png ../t_phase2.png + python create_caption.py ../titlepic/titlepic.png ../m_doom.png ../t_phase2.png $@ + cp $@ ../ + +fdmtitle.png: ../titlepic/freedm_titlepic.png + python create_caption.py $< $@ + cp $@ ../ + clean: - rm -f $(TEXTGEN_GRAPHICS) helpbg.png help.png helptext.png \ - helptext2.png graphics.stamp *.pyc credtext.png \ + rm -f $(TEXTGEN_GRAPHICS) helpbg.png help.png \ + graphics.stamp *.pyc credtext.png \ credtext.png credit.png dmwilv*.png wikilrs.png \ wivctms.png wikilrs_horiz.png ../credit.png ../help.png \ - ../wikilrs.png ../wivctms.png + ../wikilrs.png ../wivctms.png \ + fd1title.png fd2title.png fdmtitle.png \ + ../fd1title.png ../fd2title.png ../fdmtitle.png for graphic in $(TEXTGEN_GRAPHICS); do rm -f ../$$graphic; done rm -fr __pycache__ diff --git a/graphics/text/common.py b/graphics/text/common.py deleted file mode 100644 index 53fee9cd..00000000 --- a/graphics/text/common.py +++ /dev/null @@ -1,46 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -import re -import subprocess -import sys - -from PIL import Image - -# Output from 'identify' looks like this: -# fontchars/font033.png 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+)(\+\d+\+\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): - """Get image dimensions w x h - - Args: - filename: filename of the image - """ - with Image.open(filename) as img: - width, height = img.size - return (width, height) - - -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) \ No newline at end of file diff --git a/graphics/text/create_caption.py b/graphics/text/create_caption.py new file mode 100644 index 00000000..281b0d70 --- /dev/null +++ b/graphics/text/create_caption.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# coding=utf-8 +from PIL import Image, ImageFont, ImageDraw +import sys +import os + +#create_caption.py + +try: + font = ImageFont.truetype("DejaVuSansCondensed-Bold.ttf", 11) +except IOError: + font = ImageFont.load_default() + + +txt1= u"© 2001-2017" +txt2= os.environ['VERSION'] +background_image = Image.open(sys.argv[1]) +background_image.load() +background_image = background_image.convert("RGBA") +image = Image.new("RGBA", background_image.size, (0, 0, 0, 0)) +draw = ImageDraw.Draw(image) +txt1_size = draw.textsize(txt1, font=font) +txt2_size = draw.textsize(txt2, font=font) +draw.text((5, (image.height - txt1_size[1] - 5)), txt1, font=font, fill=(255,165,0,255)) +draw.text(((image.width - txt2_size[0] - 10), (image.height - txt2_size[1] - 5)), txt2, font=font, fill=(255,165,0,255)) + +if len(sys.argv) > 3: + #paste the other stuff onto the thing. + logo = Image.open(sys.argv[2]) + logo.load() + phase = Image.open(sys.argv[3]) + phase.load() + image.paste(logo, ((image.width/2 - logo.width/2), 18)) + image.paste(phase, ((image.width/2 - phase.width/2), (image.height - logo.height - 10))) + outfile_name = sys.argv[4] +else: + outfile_name = sys.argv[2] + +image = Image.alpha_composite(background_image, image) +image.save(outfile_name) diff --git a/graphics/text/image_dimensions.py b/graphics/text/image_dimensions.py new file mode 100644 index 00000000..98d1807e --- /dev/null +++ b/graphics/text/image_dimensions.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: BSD-3-Clause + +import re + +from PIL import Image + +def get_image_dimensions(filename): + """Get image dimensions w x h + + Args: + filename: filename of the image + """ + with Image.open(filename) as img: + width, height = img.size + return (width, height) + +if __name__ == '__main__': + import sys + + x,y = get_image_dimensions(sys.argv[1]) + string = "%i %i" % (x, y) + sys.stdout.write(string) + diff --git a/graphics/text/smtextgen b/graphics/text/smtextgen.py similarity index 66% rename from graphics/text/smtextgen rename to graphics/text/smtextgen.py index e9265a48..5dc4128f 100755 --- a/graphics/text/smtextgen +++ b/graphics/text/smtextgen.py @@ -9,7 +9,7 @@ from glob import glob import sys import re -from common import * +from image_dimensions import * from tint import image_tint # Background color for output files. @@ -108,41 +108,42 @@ def parse_command_line(args): return result +if __name__ == '__main__': -args = parse_command_line(sys.argv[1:]) + args = parse_command_line(sys.argv[1:]) -if not args: - print("Usage: smtextgen [...text commands...]") - print("Where each text command looks like:") - print(" [x,y] [text]") - sys.exit(0) + if not args: + print("Usage: smtextgen [...text commands...]") + print("Where each text command looks like:") + print(" [x,y] [text]") + sys.exit(0) -smallfont = Font() + smallfont = Font() -if args['background'] is not None: - background_image = Image.open(args['background']) - background_image.load() - background_image = background_image.convert("RGBA") + if args['background'] is not None: + background_image = Image.open(args['background']) + background_image.load() + background_image = background_image.convert("RGBA") -image = Image.new("RGBA", args['dimensions'],(0,0,0,0)) + image = Image.new("RGBA", args['dimensions'],(0,0,0,0)) -for xy, string in args['strings']: - # Allow contents of a file to be included with special prefix: - if string.startswith(':'): - with open(string[8:]) as f: - string = f.read() + for xy, string in args['strings']: + # Allow contents of a file to be included with special prefix: + if string.startswith('include:'): + with open(string[8:]) as f: + string = f.read() - # Allow special notation to indicate an image file to just draw - # rather than rendering a string. - if string.startswith('file:'): - src_image = Image.open(string[5:]) - src_image.load() - image.paste(src_image, (xy[0], xy[1])) - else: - smallfont.draw_for_text(image, string, xy[0], xy[1]) + # Allow special notation to indicate an image file to just draw + # rather than rendering a string. + if string.startswith('file:'): + src_image = Image.open(string[5:]) + src_image.load() + image.paste(src_image, (xy[0], xy[1])) + else: + smallfont.draw_for_text(image, string, xy[0], xy[1]) -if args['background'] is not None: - image = Image.alpha_composite(background_image, image) + if args['background'] is not None: + image = Image.alpha_composite(background_image, image) -image.save(args['filename']) + image.save(args['filename']) diff --git a/graphics/text/textgen b/graphics/text/textgen.py similarity index 83% rename from graphics/text/textgen rename to graphics/text/textgen.py index 7e49f031..327534ac 100755 --- a/graphics/text/textgen +++ b/graphics/text/textgen.py @@ -11,10 +11,10 @@ import re import sys from config import * -from common import * +from image_dimensions import * from tint import image_tint -# ImageMagick -colorize parameters for colorizing text: +# Tinting parameters for colorizing text: COLOR_BLUE = "#000001" COLOR_RED = "#010000" COLOR_WHITE = None @@ -31,6 +31,7 @@ UPPERCASE_FONT = False # Width of a space character in pixels. SPACE_WIDTH = 7 +LOWERCASE_RE = re.compile(r'^[a-z\!\. ]*$') class Font(object): @@ -109,8 +110,7 @@ class Font(object): if c is None: return x - def generate_graphic(self, text, output_filename, - color=COLOR_WHITE, bgcolor=BACKGROUND_COLOR): + def generate_graphic(self, text, color=COLOR_WHITE): """Get command to render text to a file with the given background color. """ @@ -136,15 +136,15 @@ class Font(object): txt_image.paste(char_image, (x, height - FONT_HEIGHT)) txt_image = image_tint(txt_image, color) - txt_image.save(output_filename) + return txt_image -def generate_graphics(graphics, color=COLOR_WHITE, bgcolor=BACKGROUND_COLOR): +def generate_graphics(graphics, color=COLOR_WHITE): for name, text in sorted(graphics.items()): # write a makefile fragment target = '%s.png' % name - font.generate_graphic(text, target, - color=color, bgcolor=bgcolor) + image = font.generate_graphic(text, color=color) + image.save(target) def generate_kerning_test(): @@ -158,12 +158,9 @@ def generate_kerning_test(): cmd = font.generate_graphic(" ".join(pairs), "kerning.png") +if __name__ == '__main__': -font = Font('fontchars', kerning_table=FONT_KERNING_RULES) - -# Enable to generate test image file for tweaking kerning values: -#generate_kerning_test() - -generate_graphics(red_graphics, color=COLOR_RED) -generate_graphics(blue_graphics, color=COLOR_BLUE) -generate_graphics(white_graphics, color=COLOR_WHITE) + font = Font('fontchars', kerning_table=FONT_KERNING_RULES) + generate_graphics(red_graphics, color=COLOR_RED) + generate_graphics(blue_graphics, color=COLOR_BLUE) + generate_graphics(white_graphics, color=COLOR_WHITE) diff --git a/graphics/text/tint.py b/graphics/text/tint.py index 22eead0a..31c7ece4 100644 --- a/graphics/text/tint.py +++ b/graphics/text/tint.py @@ -1,12 +1,10 @@ #/usr/bin/env python # SPDX-License-Identifier: MIT -# Copyright (c) 2017 Martin Miller +# Copyright (c) 2017 Martin Miller, Nick Zatkovich # https://stackoverflow.com/questions/12251896/colorize-image-while-preserving-transparency-with-pil -from PIL import Image -from ImageColor import getcolor, getrgb -from ImageOps import grayscale +from PIL import Image, ImageColor, ImageOps def image_tint(image, tint=None): if tint is None: @@ -14,8 +12,8 @@ def image_tint(image, tint=None): if image.mode not in ['RGB', 'RGBA']: image = image.convert('RGBA') - tr, tg, tb = getrgb(tint) - tl = getcolor(tint, "L") # tint color's overall luminosity + tr, tg, tb = ImageColor.getrgb(tint) + tl = ImageColor.getcolor(tint, "L") # tint color's overall luminosity if not tl: tl = 1 # avoid division by zero tl = float(tl) # compute luminosity preserving tint factors @@ -27,7 +25,7 @@ def image_tint(image, tint=None): luts = (map(lambda lr: int(lr * sr + 0.5), range(256)) + map(lambda lg: int(lg * sg + 0.5), range(256)) + map(lambda lb: int(lb * sb + 0.5), range(256))) - l = grayscale(image) # 8-bit luminosity version of whole image + l = ImageOps.grayscale(image) # 8-bit luminosity version of whole image if Image.getmodebands(image.mode) < 4: merge_args = (image.mode, (l, l, l)) # for RGB verion of grayscale else: # include copy of image's alpha layer diff --git a/graphics/titlepic/.gitignore b/graphics/titlepic/.gitignore deleted file mode 100644 index e1ce9728..00000000 --- a/graphics/titlepic/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -fd?title.png -m_*.png diff --git a/graphics/titlepic/Makefile b/graphics/titlepic/Makefile deleted file mode 100644 index c2dbf5ac..00000000 --- a/graphics/titlepic/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -titlepic: fd1title.png fd2title.png fdmtitle.png - -fd1title.png: titlepic.png ../m_doom.png ../t_phase1.png - ./create_caption titlepic.png ../m_doom.png ../t_phase1.png $@ - cp $@ ../ - -fd2title.png: titlepic.png ../m_doom.png ../t_phase2.png - ./create_caption titlepic.png ../m_doom.png ../t_phase2.png $@ - cp $@ ../ - -fdmtitle.png: freedm_titlepic.png - ./create_caption $< $@ - cp $@ ../ - -clean: - rm -f fd1title.png fd2title.png fdmtitle.png \ - ../fd1title.png ../fd2title.png ../fdmtitle.png - -.PHONY: clean diff --git a/graphics/titlepic/README b/graphics/titlepic/README index bed85007..d64e4114 100644 --- a/graphics/titlepic/README +++ b/graphics/titlepic/README @@ -1,4 +1,2 @@ -This directory contains a shell script that uses ImageMagick to layer -some text information about the build version onto the game's title -screen. The same is performed for each of the three IWADs. - +This directory contains template images for generating the title screen +for Freedoom Phase 1, Phase 2, and FreeDM diff --git a/graphics/titlepic/create_caption b/graphics/titlepic/create_caption deleted file mode 100755 index 3a45d2df..00000000 --- a/graphics/titlepic/create_caption +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh - -find_font() { - local font fontlist - fontlist=$(convert -list font | awk '$1=="Font:" { print $2 }') - for font in "$@" ; do - if echo $fontlist | grep -q $font ; then - echo $font - return - fi - done -} - -font=$(find_font Helvetica-Bold Liberation-Sans-Bold DejaVu-Sans-Condensed-Bold) -if [ -z "$font" ] ; then echo "Cannot find any fonts" ; exit 1 ; fi - -draw_with_footer() { - input_file=$1; - output_file=$2; - shift; shift - - convert $input_file -fill orange -font "$font" +dither \ - -pointsize 11 \ - -gravity southwest \ - -draw "text 5,5 '© 2001-2017'" \ - -gravity southeast \ - -draw "text 10,5 '$VERSION'" \ - "$@" \ - $output_file -} - -if [ $# = 4 ]; then - draw_with_footer "$1" "$4" \ - -gravity north \ - -draw "image over 0,18 0,0 '$2'" \ - -gravity south \ - -draw "image over 0,30 0,0 '$3'" -else - draw_with_footer "$1" "$2" -fi