Skip to content

Commit

Permalink
Remove unused SVGs and export to subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Aug 15, 2024
1 parent d61e34a commit e463d12
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 768 deletions.
21 changes: 7 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Install black
run: pip3 install black
- name: Run black
run: python3 -m black .
- name: Check output
run: git --no-pager diff --exit-code HEAD
- run: pip3 install black
- run: python3 -m black .
- run: git --no-pager diff --exit-code HEAD

export:
runs-on: ubuntu-22.04
Expand All @@ -23,11 +20,7 @@ jobs:
sudo add-apt-repository ppa:inkscape.dev/stable
sudo apt-get update -q
sudo apt-get install inkscape imagemagick -y
- name: Run inkscape export
run: |
inkscape --version
./export.py
- name: Check output
# Ignore .svg files because Ubuntu 22.04 inkscape has round-off issues and
# .ico files because Ubuntu 22.04 ImageMagick gives different results
run: git --no-pager diff --exit-code HEAD -- . ':!ico' ':!svg'
- run: ./export.py
# Ignore .svg files because Ubuntu 22.04 inkscape has round-off issues and
# .ico files because Ubuntu 22.04 ImageMagick gives different results
- run: git --no-pager diff --exit-code HEAD -- . ':!export/ico' ':!export/svg'
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
![logo](https://rawgithub.com/wpilibsuite/branding/master/svg/wpilib-horiz-team.svg)

# WPILib Branding Standards

This repository contains WPILib's branding standards and guidelines.
Expand Down Expand Up @@ -57,6 +55,6 @@ The distance between the bottom of the hexagon and the top of the first line of

To resize the canvas to the page content, first unselect all parts of the document. Then select "File > Document Properties...", expand the "Resize page to content..." section, and press the "Resize page to drawing or selection" button.

After saving the Inkscape SVG, run `export.py` before committing. It will strip unnecessary metadata from the SVG and generate plain SVGs and PNG rasterizations automatically. ImageMagick is required to generate ICO rasterizations.
After saving the Inkscape SVG, run `export.py` before committing. It will strip unnecessary metadata from the SVG and generate plain SVGs and PNG rasterizations automatically in the export folder. ImageMagick is required to generate ICO rasterizations.

If rasterizations are needed beyond what `export.py` generates, invoke `rasterize.py` directly. `rasterize.py --help` lists possible arguments.
75 changes: 35 additions & 40 deletions export.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
#!/usr/bin/env python3

import os
import re
import subprocess
import sys


def main():
svg = [f for f in os.listdir(".") if os.path.isfile(f) and f.endswith(".svg")]

# Generate plain SVG
print("Generating plain SVGs from Inkscape SVGs...")
try:
os.mkdir("svg/")
except Exception:
pass
plain_svg = ["svg/" + re.sub("-edittable", "", f) for f in svg]
for i in range(len(svg)):
print(plain_svg[i] + "...", end="")
svgs = [f for f in os.listdir(".") if os.path.isfile(f) and f.endswith(".svg")]

# Export plain SVGs
os.makedirs(os.path.join("export", "svg"), exist_ok=True)
for svg in svgs:
print(f"Exporting {svg} to plain SVG export/svg/{svg}...", end="")
sys.stdout.flush()

subprocess.run(
[
"inkscape",
"--export-type=svg",
"--export-filename=" + plain_svg[i],
f"--export-filename=export/svg/{svg}",
"--export-text-to-path",
svg[i],
svg,
]
)
print(" done.")

# Compile regexes
rgxes = [
re.compile(r'^\s+sodipodi:docname="((.|\n)*?")\n', re.M),
re.compile(r"^\s+<metadata\s+((.|\n)*?</metadata>)\n", re.M),
re.compile(r"^\s+<defs\s+((.|\n)*?/>)\n", re.M),
re.compile(r"^\s+<sodipodi:namedview\s+((.|\n)*?/>)\n", re.M),
]

# Remove excess metadata from Inkscape and plain SVGs
print("Removing excess metadata...")
lines = ""
for name in svg + plain_svg:
print(name + "...", end="")
with open(name, "r") as file:
lines = file.read()
output = lines
for rgx in rgxes:
output = rgx.sub("", output)
if lines != output:
with open(name, "wb") as file:
file.write(output.encode())
print(" done.")

# Rasterize SVGs
args = ["./rasterize.py", "--svgs", "svg/wpilib.svg", "--raster-extension"]
subprocess.run(args + ["png", "--raster-sizes", "16", "128"])
subprocess.run(args + ["ico", "--raster-sizes", "256"])
# PNG rasterizations
subprocess.run(
[
"./rasterize.py",
"--svgs",
"export/svg/wpilib-icon.svg",
"--raster-extension",
"png",
"--raster-sizes",
"16",
"128",
]
)

# ICO rasterizations
subprocess.run(
[
"./rasterize.py",
"--svgs",
"export/svg/wpilib-icon.svg",
"--raster-extension",
"ico",
"--raster-sizes",
"256",
]
)


if __name__ == "__main__":
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
12 changes: 12 additions & 0 deletions svg/wpilib.svg → export/svg/wpilib-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions svg/wpilib-vert-indiv.svg → export/svg/wpilib-polo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 16 additions & 16 deletions rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ def rasterize(svg, raster_extension, raster_sizes):
raster_extension -- file extension of raster image output format
raster_sizes -- list of heights for raster images
"""
try:
os.mkdir(raster_extension + "/")
except Exception:
pass
os.makedirs(os.path.join("export", raster_extension), exist_ok=True)
for size in raster_sizes:
out_name = (
raster_extension
+ "/"
+ os.path.splitext(os.path.basename(svg))[0]
+ "-"
+ str(size)
+ "."
+ raster_extension
out_name = os.path.join(
"export",
raster_extension,
f"{os.path.splitext(os.path.basename(svg))[0]}-{size}.{raster_extension}",
)
print("Rasterizing " + svg + " to " + out_name + "...")

print(f"Rasterizing {svg} to {out_name}...", end="")
sys.stdout.flush()

if raster_extension == "png":
try:
subprocess.run(
[
"inkscape",
"--export-type=png",
"--export-filename=" + out_name,
"--export-height=" + str(size),
f"--export-filename={out_name}",
f"--export-height={size}",
svg,
]
)
Expand Down Expand Up @@ -69,9 +64,14 @@ def rasterize(svg, raster_extension, raster_sizes):
)
sys.exit(1)
else:
print("Error: unknown raster file extension '{}'".format(raster_extension))
print(
f"Error: unknown raster file extension '{raster_extension}'",
file=sys.stderr,
)
sys.exit(1)

print(" done.")


def main():
# Parse command-line arguments
Expand Down
Loading

0 comments on commit e463d12

Please sign in to comment.