1
0
forked from 0ad/0ad

Port remaining python2 helper scripts to python3, fixes #5694.

Differential Revision: https://code.wildfiregames.com/D2501
This was SVN commit r24892.
This commit is contained in:
Nicolas Auvray 2021-02-12 21:25:33 +00:00
parent f175bc4f8d
commit c59d569767
9 changed files with 26 additions and 307 deletions

View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
from ctypes import *
import sys
import os
@ -19,12 +21,12 @@ os.environ['PATH'] = '%s/system/' % binaries
library = cdll.LoadLibrary('%s/system/%s' % (binaries, dll_filename))
def log(severity, message):
print '[%s] %s' % (('INFO', 'WARNING', 'ERROR')[severity], message)
print('[%s] %s' % (('INFO', 'WARNING', 'ERROR')[severity], message))
clog = CFUNCTYPE(None, c_int, c_char_p)(log)
# (the CFUNCTYPE must not be GC'd, so try to keep a reference)
library.set_logger(clog)
skeleton_definitions = open('%s/data/tools/collada/skeletons.xml' % binaries).read()
skeleton_definitions = open('%s/data/tests/collada/skeletons.xml' % binaries).read()
library.set_skeleton_definitions(skeleton_definitions, len(skeleton_definitions))
def _convert_dae(func, filename, expected_status=0):
@ -107,7 +109,7 @@ clean_dir(test_mod + '/art/animation')
for test_file in ['xsitest3c','xsitest3e','jav2d','jav2d2']:
#for test_file in ['xsitest3']:
#for test_file in []:
print "* Converting PMD %s" % (test_file)
print("* Converting PMD %s" % (test_file))
input_filename = '%s/%s.dae' % (test_data, test_file)
output_filename = '%s/art/meshes/%s.pmd' % (test_mod, test_file)
@ -125,7 +127,7 @@ for test_file in ['xsitest3c','xsitest3e','jav2d','jav2d2']:
#for test_file in ['jav2','jav2b', 'jav2d']:
for test_file in ['xsitest3c','xsitest3e','jav2d','jav2d2']:
#for test_file in []:
print "* Converting PSA %s" % (test_file)
print("* Converting PSA %s" % (test_file))
input_filename = '%s/%s.dae' % (test_data, test_file)
output_filename = '%s/art/animation/%s.psa' % (test_mod, test_file)

View File

@ -47,7 +47,7 @@ def create_cairo_font_face_for_file (filename, faceindex=0, loadoptions=0):
cairo_ctx = cairo.Context (_surface)
cairo_t = PycairoContext.from_address(id(cairo_ctx)).ctx
if FT_Err_Ok != _freetype_so.FT_New_Face (_ft_lib, filename, faceindex, ctypes.byref(ft_face)):
if FT_Err_Ok != _freetype_so.FT_New_Face (_ft_lib, filename.encode('ascii'), faceindex, ctypes.byref(ft_face)):
raise Exception("Error creating FreeType font face for " + filename)
# create cairo font face for freetype face

View File

@ -176,7 +176,7 @@ class CygonRectanglePacker(RectanglePacker):
# rectangle at its current placement. We cannot put the rectangle
# any lower than this without overlapping the other rectangles.
highest = self.heightSlices[leftSliceIndex].y
for index in xrange(leftSliceIndex + 1, rightSliceIndex):
for index in range(leftSliceIndex + 1, rightSliceIndex):
if self.heightSlices[index].y > highest:
highest = self.heightSlices[index].y

View File

@ -8,11 +8,12 @@ def dump_font(ttf):
(face, indexes) = FontLoader.create_cairo_font_face_for_file("../../../binaries/data/tools/fontbuilder/fonts/%s" % ttf, 0, FontLoader.FT_LOAD_DEFAULT)
mappings = [ (c, indexes(unichr(c))) for c in range(1, 65535) ]
print ttf,
print ' '.join(str(c) for (c, g) in mappings if g != 0)
mappings = [ (c, indexes(chr(c))) for c in range(1, 65535) ]
print(ttf, end=' ')
print(' '.join(str(c) for (c, g) in mappings if g != 0))
dump_font("DejaVuSansMono.ttf")
dump_font("DejaVuSans.ttf")
dump_font("FreeSans.ttf")
dump_font("LinBiolinum_Rah.ttf")
dump_font("texgyrepagella-regular.otf")
dump_font("texgyrepagella-bold.otf")

View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
import cairo
import codecs
import math
@ -111,7 +113,7 @@ def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes):
# TODO this gets the line height from the default font
# while entire texts can be in the fallback font
ctx.set_font_face(faceList[0]);
ctx.set_font_face(faceList[0])
ctx.set_font_size(size + dsizes[ttfNames[0]])
(_, _, linespacing, _, _) = ctx.font_extents()
@ -124,11 +126,11 @@ def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes):
#for c in chars:
for c in range(0x20, 0xFFFE):
for i in range(len(indexList)):
idx = indexList[i](unichr(c))
idx = indexList[i](chr(c))
if c == 0xFFFD and idx == 0: # use "?" if the missing-glyph glyph is missing
idx = indexList[i]("?")
if idx:
glyphs.append(Glyph(ctx, renderstyle, unichr(c), idx, faceList[i], size + dsizes[ttfNames[i]]))
glyphs.append(Glyph(ctx, renderstyle, chr(c), idx, faceList[i], size + dsizes[ttfNames[i]]))
break
# Sort by decreasing height (tie-break on decreasing width)
@ -139,7 +141,7 @@ def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes):
for h in [32, 64, 128, 256, 512, 1024, 2048, 4096]:
sizes.append((h, h))
sizes.append((h*2, h))
sizes.sort(key = lambda (w, h): (w*h, max(w, h))) # prefer smaller and squarer
sizes.sort(key = lambda w_h: (w_h[0]*w_h[1], max(w_h[0], w_h[1]))) # prefer smaller and squarer
for w, h in sizes:
try:
@ -154,7 +156,7 @@ def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes):
ctx, surface = setup_context(w, h, renderstyle)
for g in glyphs:
g.render(ctx)
g.render(ctx)
surface.write_to_png("%s.png" % outname)
# Output the .fnt file with all the glyph positions etc
@ -184,7 +186,7 @@ def generate_font(outname, ttfNames, loadopts, size, renderstyle, dsizes):
fnt.close()
return
print "Failed to fit glyphs in texture"
print("Failed to fit glyphs in texture")
filled = { "fill": [(1, 1, 1, 1)] }
stroked1 = { "colour": True, "stroke": [((0, 0, 0, 1), 2.0), ((0, 0, 0, 1), 2.0)], "fill": [(1, 1, 1, 1)] }
@ -230,5 +232,5 @@ fonts = (
)
for (name, (fontnames, loadopts), size, style) in fonts:
print "%s..." % name
print("%s..." % name)
generate_font("../../../binaries/data/mods/mod/fonts/%s" % name, fontnames, loadopts, size, style, dsizes)

View File

@ -1,288 +0,0 @@
"""
Generates basic square and circle selection overlay textures by parsing all the entity XML files and reading
their Footprint components.
For usage, invoke this script with --help.
"""
# This script uses PyCairo for plotting, since PIL (Python Imaging Library) is absolutely horrible. On Linux,
# this should be merely a matter of installing a package (e.g. 'python-cairo' for Debian/Ubuntu), but on Windows
# it's kind of tricky and requires some Google-fu. Fortunately, I have saved the working instructions below:
#
# Grab a Win32 binary from http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.8/ and install PyCairo using
# the installer. The installer extracts the necessary files into Lib\site-packages\cairo within the folder where
# Python is installed. There are some extra DLLs which are required to make Cairo work, so we have to get these
# as well.
#
# Head to http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ and get the binary versions of Cairo
# (cairo_1.8.10-3_win32.zip at the time of writing), Fontconfig (fontconfig_2.8.0-2_win32.zip), Freetype
# (freetype_2.4.4-1_win32.zip), Expat (expat_2.0.1-1_win32.zip), libpng (libpng_1.4.3-1_win32.zip) and zlib
# (zlib_1.2.5-2_win32.zip). Version numbers may vary, so be adaptive! Each ZIP file will contain a bin subfolder
# with a DLL file in it. Put the following DLLs in Lib\site-packages\cairo within your Python installation:
#
# freetype6.dll (from freetype_2.4.4-1_win32.zip)
# libcairo-2.dll (from cairo_1.8.10-3_win32.zip)
# libexpat-1.dll (from expat_2.0.1-1_win32.zip)
# libfontconfig-1.dll (from fontconfig_2.8.0-2_win32.zip)
# libpng14-14.dll (from libpng_1.4.3-1_win32.zip)
# zlib1.dll (from zlib_1.2.5-2_win32.zip).
#
# Should be all set now.
import optparse
import sys, os
import math
import operator
import cairo # Requires PyCairo (see notes above)
from os.path import *
from xml.dom import minidom
def geqPow2(x):
"""Returns the smallest power of two that's equal to or greater than x"""
return int(2**math.ceil(math.log(x, 2)))
def generateSelectionTexture(shape, textureW, textureH, outerStrokeW, innerStrokeW, outputDir):
outputBasename = "%dx%d" % (textureW, textureH)
# size of the image canvas containing the texture (may be larger to ensure power-of-two dimensions)
canvasW = geqPow2(textureW)
canvasH = geqPow2(textureH)
# draw texture
texture = cairo.ImageSurface(cairo.FORMAT_ARGB32, canvasW, canvasH)
textureMask = cairo.ImageSurface(cairo.FORMAT_RGB24, canvasW, canvasH)
ctxTexture = cairo.Context(texture)
ctxTextureMask = cairo.Context(textureMask)
# fill entire image with transparent pixels
ctxTexture.set_source_rgba(1.0, 1.0, 1.0, 0.0) # transparent
ctxTexture.rectangle(0, 0, textureW, textureH)
ctxTexture.fill() # fill current path
ctxTextureMask.set_source_rgb(0.0, 0.0, 0.0) # black
ctxTextureMask.rectangle(0, 0, canvasW, canvasH) # (!)
ctxTextureMask.fill()
pasteX = (canvasW - textureW)//2 # integer division, floored result
pasteY = (canvasH - textureH)//2 # integer division, floored result
ctxTexture.translate(pasteX, pasteY) # translate all drawing so that the result is centered
ctxTextureMask.translate(pasteX, pasteY)
# outer stroke width should always be >= inner stroke width, but let's play it safe
maxStrokeW = max(outerStrokeW, innerStrokeW)
if shape == "square":
rectW = textureW
rectH = textureH
# draw texture (4px white outline, then overlay a 2px black outline)
ctxTexture.rectangle(maxStrokeW/2, maxStrokeW/2, rectW - maxStrokeW, rectH - maxStrokeW)
ctxTexture.set_line_width(outerStrokeW)
ctxTexture.set_source_rgba(1.0, 1.0, 1.0, 1.0) # white
ctxTexture.stroke_preserve() # stroke and maintain path
ctxTexture.set_line_width(innerStrokeW)
ctxTexture.set_source_rgba(0.0, 0.0, 0.0, 1.0) # black
ctxTexture.stroke() # stroke and clear path
# draw mask (2px white)
ctxTextureMask.rectangle(maxStrokeW/2, maxStrokeW/2, rectW - maxStrokeW, rectH - maxStrokeW)
ctxTextureMask.set_line_width(innerStrokeW)
ctxTextureMask.set_source_rgb(1.0, 1.0, 1.0)
ctxTextureMask.stroke()
elif shape == "circle":
centerX = textureW//2
centerY = textureH//2
radius = textureW//2 - maxStrokeW/2 # allow for the strokes to fit
# draw texture
ctxTexture.arc(centerX, centerY, radius, 0, 2*math.pi)
ctxTexture.set_line_width(outerStrokeW)
ctxTexture.set_source_rgba(1.0, 1.0, 1.0, 1.0) # white
ctxTexture.stroke_preserve() # stroke and maintain path
ctxTexture.set_line_width(innerStrokeW)
ctxTexture.set_source_rgba(0.0, 0.0, 0.0, 1.0) # black
ctxTexture.stroke()
# draw mask
ctxTextureMask.arc(centerX, centerY, radius, 0, 2*math.pi)
ctxTextureMask.set_line_width(innerStrokeW)
ctxTextureMask.set_source_rgb(1.0, 1.0, 1.0)
ctxTextureMask.stroke()
finalOutputDir = outputDir + "/" + shape
if not isdir(finalOutputDir):
os.makedirs(finalOutputDir)
print "Generating " + os.path.normcase(finalOutputDir + "/" + outputBasename + ".png")
texture.write_to_png(finalOutputDir + "/" + outputBasename + ".png")
textureMask.write_to_png(finalOutputDir + "/" + outputBasename + "_mask.png")
def generateSelectionTextures(xmlTemplateDir, outputDir, outerStrokeScale, innerStrokeScale, snapSizes = False):
# recursively list XML files
xmlFiles = []
for dir, subdirs, basenames in os.walk(xmlTemplateDir):
for basename in basenames:
filename = join(dir, basename)
if filename[-4:] == ".xml":
xmlFiles.append(filename)
textureTypesRaw = set() # set of (type, w, h) tuples (so we can eliminate duplicates)
# parse the XML files, and look for <Footprint> nodes that are a child of <Entity> and
# that do not have the disable attribute defined
for xmlFile in xmlFiles:
xmlDoc = minidom.parse(xmlFile)
rootNode = xmlDoc.childNodes[0]
# we're only interested in entity templates
if not rootNode.nodeName == "Entity":
continue
# check if this entity has a footprint definition
rootChildNodes = [n for n in rootNode.childNodes if n.localName is not None] # remove whitespace text nodes
footprintNodes = filter(lambda x: x.localName == "Footprint", rootChildNodes)
if not len(footprintNodes) == 1:
continue
footprintNode = footprintNodes[0]
if footprintNode.hasAttribute("disable"):
continue
# parse the footprint declaration
# Footprints can either have either one of these children:
# <Circle radius="xx.x" />
# <Square width="xx.x" depth="xx.x"/>
# There's also a <Height> node, but we don't care about it here.
squareNodes = footprintNode.getElementsByTagName("Square")
circleNodes = footprintNode.getElementsByTagName("Circle")
numSquareNodes = len(squareNodes)
numCircleNodes = len(circleNodes)
if not (numSquareNodes + numCircleNodes == 1):
print "Invalid Footprint definition: insufficient or too many Square and/or Circle definitions in %s" % xmlFile
texShape = None
texW = None # in world-space units
texH = None # in world-space units
if numSquareNodes == 1:
texShape = "square"
texW = float(squareNodes[0].getAttribute("width"))
texH = float(squareNodes[0].getAttribute("depth"))
elif numCircleNodes == 1:
texShape = "circle"
texW = float(circleNodes[0].getAttribute("radius"))
texH = texW
textureTypesRaw.add((texShape, texW, texH))
# endfor xmlFiles
print "Found: %d footprints (%d square, %d circle)" % (
len(textureTypesRaw),
len([x for x in textureTypesRaw if x[0] == "square"]),
len([x for x in textureTypesRaw if x[0] == "circle"])
)
textureTypes = set()
for type, w, h in textureTypesRaw:
if snapSizes:
# "snap" texture sizes to close-enough neighbours that will still look good enough so we can get away with fewer
# actual textures than there are unique footprint outlines
w = 1*math.ceil(w/1) # round up to the nearest world-space unit
h = 1*math.ceil(h/1) # round up to the nearest world-space unit
textureTypes.add((type, w, h))
if snapSizes:
print "Reduced: %d footprints (%d square, %d circle)" % (
len(textureTypes),
len([x for x in textureTypes if x[0] == "square"]),
len([x for x in textureTypes if x[0] == "circle"])
)
# create list from texture types set (so we can sort and have prettier output)
textureTypes = sorted(list(textureTypes), key=operator.itemgetter(0,1,2)) # sort by the first tuple element, then by the second, then the third
# ------------------------------------------------------------------------------------
# compute the size of the actual texture we want to generate (in px)
scale = 8 # world-space-units-to-pixels scale
for type, w, h in textureTypes:
# if we have a circle, update the w and h so that they're the full width and height of the texture
# and not just the radius
if type == "circle":
assert w == h
w *= 2
h *= 2
w = int(math.ceil(w*scale))
h = int(math.ceil(h*scale))
# apply a minimum size for really small textures
w = max(24, w)
h = max(24, h)
generateSelectionTexture(type, w, h, w/outerStrokeScale, innerStrokeScale * (w/outerStrokeScale), outputDir)
if __name__ == "__main__":
parser = optparse.OptionParser(usage="Usage: %prog [filenames]")
parser.add_option("--template-dir", type="str", default=None, help="Path to simulation template XML definition folder. Will be searched recursively for templates containing Footprint definitions. If not specified and this script is run from its directory, it will be automatically determined.")
parser.add_option("--output-dir", type="str", default=".", help="Output directory. Will be created if it does not already exist. Defaults to the current directory.")
parser.add_option("--oss", "--outer-stroke-scale", type="float", default=12.0, dest="outer_stroke_scale", metavar="SCALE", help="Width of the outer (white) stroke, as a divisor of each generated texture's width. Defaults to 12. Larger values produce thinner overall outlines.")
parser.add_option("--iss", "--inner-stroke-scale", type="float", default=0.5, dest="inner_stroke_scale", metavar="PERCENTAGE", help="Width of the inner (black) stroke, as a percentage of the outer stroke's calculated width. Must be between 0 and 1. Higher values produce thinner black/player color strokes inside the surrounding outer white stroke. Defaults to 0.5.")
(options, args) = parser.parse_args()
templateDir = options.template_dir
if templateDir is None:
scriptDir = dirname(abspath(__file__))
# 'autodetect' location if run from its own dir
if normcase(scriptDir).replace('\\', '/').endswith("source/tools/selectiontexgen"):
templateDir = "../../../binaries/data/mods/public/simulation/templates"
else:
print "No template dir specified; use the --template-dir command line argument."
sys.exit()
# check if the template dir exists
templateDir = abspath(templateDir)
if not isdir(templateDir):
print "No such template directory: %s" % templateDir
sys.exit()
# check if the output dir exists, create it if needed
outputDir = abspath(options.output_dir)
print outputDir
if not isdir(outputDir):
print "Creating output directory: %s" % outputDir
os.makedirs(outputDir)
print "Template directory:\t%s" % templateDir
print "Output directory: \t%s" % outputDir
print "------------------------------------------------"
generateSelectionTextures(
templateDir,
outputDir,
max(0.0, options.outer_stroke_scale),
min(1.0, max(0.0, options.inner_stroke_scale)),
)

View File

@ -26,4 +26,4 @@ This extension, called TableFilter, is released under the MIT license. The versi
All contents of this folder are under the MIT License.
Enjoy!
Enjoy!

View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
# Copyright (C) 2015 Wildfire Games.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import argparse
import os
import re