From 21410e9142aa689f220e95a316097ecc2924ea50 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sun, 10 Apr 2022 14:07:36 -0700 Subject: [PATCH] port svgcolor_lib generation script to Python Note that this does not introduce an extra build dependency in any of the three build systems: 1. Autotools: these steps are done during construction of the portable source tarball, during which Python 3 is already required (see autogen.sh). 2. CMake: Python 3 is already required by and used in the top level CMakeLists.txt. 3. MS Build: Python 3 is already used in version generation in lib/version/version.vcxproj. This change is motivated by the goal of removing a dependency on Awk. Though it also conveniently side steps some line ending portability issues that were being worked around with `AWK_OPTIONS`. In Python, this is unnecessary because reading a file in text mode accounts for both flavors of line endings. Gitlab: #2118 --- awk/svgcolor.awk | 23 ------------------- lib/common/CMakeLists.txt | 13 ++++------- lib/common/Makefile.am | 12 +++++----- lib/common/make_svgcolor_lib.py | 40 +++++++++++++++++++++++++++++++++ lib/gvc.vcxproj | 4 ++-- 5 files changed, 51 insertions(+), 41 deletions(-) delete mode 100644 awk/svgcolor.awk create mode 100644 lib/common/make_svgcolor_lib.py diff --git a/awk/svgcolor.awk b/awk/svgcolor.awk deleted file mode 100644 index ab4d146d2..000000000 --- a/awk/svgcolor.awk +++ /dev/null @@ -1,23 +0,0 @@ -# -# /************************************************************************* -# * Copyright (c) 2011 AT&T Intellectual Property -# * All rights reserved. This program and the accompanying materials -# * are made available under the terms of the Eclipse Public License v1.0 -# * which accompanies this distribution, and is available at -# * http://www.eclipse.org/legal/epl-v10.html -# * -# * Contributors: Details at http://www.graphviz.org/ -# *************************************************************************/ -# -# Convert SVG-1.1 color data to same RGBA format used in color_names. -# See svgcolor_names for input format. -# -# All colors assumed opaque, so A = 255 in all colors -BEGIN { - FS = "[ ,()]*"; -} -/^[ ]*$/ { next; } -/^#/ { next; } -{ - printf ("/svg/%s %s %s %s 255\n", $1, $5, $6, $7); -} diff --git a/lib/common/CMakeLists.txt b/lib/common/CMakeLists.txt index 3ce347d6b..8b050df52 100644 --- a/lib/common/CMakeLists.txt +++ b/lib/common/CMakeLists.txt @@ -3,19 +3,14 @@ BISON_TARGET(HTMLparse ${CMAKE_CURRENT_SOURCE_DIR}/htmlparse.y add_definitions(-DGVC_EXPORTS) -if(CYGWIN) - set(AWK_OPTIONS, -v RS="\\r*\\n") -endif() - # Generate colortbl.h from sources add_custom_command( OUTPUT svgcolor_lib - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/svgcolor_names - ${CMAKE_SOURCE_DIR}/awk/svgcolor.awk - COMMAND ${AWK_EXECUTABLE} ${AWK_OPTIONS} -f - ${CMAKE_SOURCE_DIR}/awk/svgcolor.awk + MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/svgcolor_names + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/make_svgcolor_lib.py + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_svgcolor_lib.py ${CMAKE_CURRENT_SOURCE_DIR}/svgcolor_names - >${CMAKE_CURRENT_BINARY_DIR}/svgcolor_lib + ${CMAKE_CURRENT_BINARY_DIR}/svgcolor_lib COMMENT "generate SVG color library" ) add_custom_command( diff --git a/lib/common/Makefile.am b/lib/common/Makefile.am index fd07f1149..8cdf4b45e 100644 --- a/lib/common/Makefile.am +++ b/lib/common/Makefile.am @@ -46,12 +46,9 @@ color_lib : brewer_lib svgcolor_lib $(top_srcdir)/lib/common/color_names brewer_lib : $(top_srcdir)/lib/common/brewer_colors $(top_srcdir)/awk/brewer.awk $(AWK) -f $(top_srcdir)/awk/brewer.awk $(top_srcdir)/lib/common/brewer_colors > brewer_lib -if WITH_CYGWIN -AWK_OPTIONS=-v RS="\r*\n" -endif - -svgcolor_lib : $(top_srcdir)/lib/common/svgcolor_names $(top_srcdir)/awk/svgcolor.awk - $(AWK) $(AWK_OPTIONS) -f $(top_srcdir)/awk/svgcolor.awk $(top_srcdir)/lib/common/svgcolor_names > svgcolor_lib +svgcolor_lib: $(top_srcdir)/lib/common/svgcolor_names \ + $(top_srcdir)/lib/common/make_svgcolor_lib.py + python3 $(top_srcdir)/lib/common/make_svgcolor_lib.py "$<" "$@" htmllex.o htmllex.lo: htmllex.c htmllex.h htmlparse.h htmlparse.o htmlparse.lo: htmlparse.c htmlparse.h @@ -66,4 +63,5 @@ EXTRA_DIST = README.imap \ htmlparse.c htmlparse.h \ entities.html entities.tcl \ brewer_colors brewer_lib svgcolor_names svgcolor_lib \ - color_names color_lib colortbl.h + color_names color_lib colortbl.h \ + make_svgcolor_lib.py diff --git a/lib/common/make_svgcolor_lib.py b/lib/common/make_svgcolor_lib.py new file mode 100644 index 000000000..61d08f2de --- /dev/null +++ b/lib/common/make_svgcolor_lib.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +""" +svgcolor_names → svgcolor_lib generator +""" + +import argparse +import re +import sys +from typing import List + +def main(args: List[str]) -> int: + """entry point""" + + # parse command line arguments + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("input", type=argparse.FileType("rt"), + help="input SVG data") + parser.add_argument("output", type=argparse.FileType("wt"), + help="output color table entries") + options = parser.parse_args(args[1:]) + + for line in options.input: + + # skip comments and empty lines + if line.startswith("#") or line.strip() == "": + continue + + # split the line into columns + items = re.split(r"[ \t,()]+", line) + assert len(items) == 8, f"unexpected line {line}" + + # write this as a color table entry + options.output.write(f"/svg/{items[0]} {items[4]} {items[5]} {items[6]} 255\n") + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/lib/gvc.vcxproj b/lib/gvc.vcxproj index dfa61a7e8..6c7e6410b 100644 --- a/lib/gvc.vcxproj +++ b/lib/gvc.vcxproj @@ -72,7 +72,7 @@ win_bison -dy -Wno-yacc common\htmlparse.y -o common\htmlparse.c -awk -f $(SolutionDir)awk\svgcolor.awk common\svgcolor_names > common\svgcolor_lib +python common\make_svgcolor_lib.py common\svgcolor_names common\svgcolor_lib awk -f $(SolutionDir)awk\brewer.awk common\brewer_colors > common\brewer_lib type common\brewer_lib common\svgcolor_lib common\color_names | sort /L C > color_lib awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h @@ -103,7 +103,7 @@ awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h win_bison -dy -Wno-yacc common\htmlparse.y -o common\htmlparse.c -awk -f $(SolutionDir)awk\svgcolor.awk common\svgcolor_names > common\svgcolor_lib +python common\make_svgcolor_lib.py common\svgcolor_names common\svgcolor_lib awk -f $(SolutionDir)awk\brewer.awk common\brewer_colors > common\brewer_lib type common\brewer_lib common\svgcolor_lib common\color_names | sort /L C > color_lib awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h -- 2.40.0