Add the OpenPU application icon (SVG, HVIF, PNG)
Original artwork: a generic shield with a "PU" monogram and a small download arrow, deep blue and white. tools/make_icons.py describes the shapes once and writes the master SVG, a simplified 16/24 px variant, the Haiku HVIF and PNG renderings from 16 to 512 px (own antialiased rasterizer, standard library only). - Linux: icons installed in the hicolor theme as io.github.openpu.OpenPU (the name the .desktop file and the About dialog already use), with a build-tree copy for uninstalled runs. - Haiku: the HVIF is embedded as the vector_icon resource; the executable is relinked when only its resources change. - Fix: po/ did not include GNUInstallDirs, so translations were installed outside the locale directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
No files matched your search
@@ -16,8 +16,12 @@ complete; every release is an annotated git tag `vX.Y.Z` (see `tools/release.sh`
|
||||
adding a vehicle, updates, download folder, resumable download, USB stick, writing, installation
|
||||
guide), Preferences window, About window and new-version check. It uses the gettext catalogs of
|
||||
the GTK interface, and the language of the Locale preferences by default.
|
||||
- Application icon: an original shield with a "PU" monogram, as SVG, HVIF (embedded in the Haiku
|
||||
executable) and PNG from 16 to 512 px (installed in the hicolor icon theme on Linux), generated
|
||||
by `tools/make_icons.py`.
|
||||
|
||||
### Fixed
|
||||
- Translations were installed in `/<language>/LC_MESSAGES` instead of the locale directory.
|
||||
- Archive extraction failed when the destination path contained a symbolic link (e.g. `/tmp` on
|
||||
Haiku).
|
||||
- `tools/release.sh` produced empty release notes.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
<p align="center"><img src="data/icons/png/openpu-128.png" width="128" height="128" alt="OpenPU logo"></p>
|
||||
|
||||
# OpenPU
|
||||
|
||||
A free and native client to download updates for Peugeot head units
|
||||
@@ -81,5 +83,8 @@ the Locale preferences. To add a language:
|
||||
interfaces.
|
||||
|
||||
## Tools
|
||||
- `tools/make_icons.py`: generates the application icon (original artwork, MIT like the rest of
|
||||
the project) from a single description: `data/icons/openpu.svg`, a simplified variant for 16 and
|
||||
24 px, `openpu.hvif` for Haiku and PNG files from 16 to 512 px. Standard library only.
|
||||
- `tools/asar_extract.py`: minimal, read-only asar extractor used to study the reference
|
||||
application. Extracted sources stay **outside the repository**.
|
||||
@@ -0,0 +1,13 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
|
||||
<title>OpenPU</title>
|
||||
<defs>
|
||||
<linearGradient id="bg" gradientUnits="userSpaceOnUse" x1="32" y1="4" x2="32" y2="60">
|
||||
<stop offset="0" stop-color="#2563b8"/>
|
||||
<stop offset="1" stop-color="#163a6b"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<path fill="url(#bg)" d="M12 4 L52 4 C54.2 4 56 5.8 56 8 L56 34 C56 49 45 57 32 61 C19 57 8 49 8 34 L8 8 C8 5.8 9.8 4 12 4Z"/>
|
||||
<path fill="#ffffff" d="M15 19 L23 19 C26.9 19 29 22.1 29 26 C29 29.9 26.9 33 23 33 L21 33 L21 41 L15 41Z"/>
|
||||
<path fill="url(#bg)" d="M21 24 L23 24 C24.1 24 25 24.9 25 26 C25 27.1 24.1 28 23 28 L21 28Z"/>
|
||||
<path fill="#ffffff" d="M33 19 L39 19 L39 33 C39 34.1 39.9 35 41 35 C42.1 35 43 34.1 43 33 L43 19 L49 19 L49 33 C49 37.4 45.4 41 41 41 C36.6 41 33 37.4 33 33Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 841 B |
@@ -0,0 +1,16 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
|
||||
<title>OpenPU</title>
|
||||
<defs>
|
||||
<linearGradient id="bg" gradientUnits="userSpaceOnUse" x1="32" y1="4" x2="32" y2="60">
|
||||
<stop offset="0" stop-color="#2563b8"/>
|
||||
<stop offset="1" stop-color="#163a6b"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<path fill="url(#bg)" d="M12 4 L52 4 C54.2 4 56 5.8 56 8 L56 34 C56 49 45 57 32 61 C19 57 8 49 8 34 L8 8 C8 5.8 9.8 4 12 4Z"/>
|
||||
<path fill="#ffffff" d="M14 6.85 L50 6.85 C51.98 6.85 53.6 8.47 53.6 10.45 L53.6 33.85 C53.6 47.35 43.7 54.55 32 58.15 C20.3 54.55 10.4 47.35 10.4 33.85 L10.4 10.45 C10.4 8.47 12.02 6.85 14 6.85Z"/>
|
||||
<path fill="url(#bg)" d="M14.8 7.99 L49.2 7.99 C51.09 7.99 52.64 9.54 52.64 11.43 L52.64 33.79 C52.64 46.69 43.18 53.57 32 57.01 C20.82 53.57 11.36 46.69 11.36 33.79 L11.36 11.43 C11.36 9.54 12.91 7.99 14.8 7.99Z"/>
|
||||
<path fill="#ffffff" d="M15 15 L23 15 C26.9 15 29 18.1 29 22 C29 25.9 26.9 29 23 29 L21 29 L21 37 L15 37Z"/>
|
||||
<path fill="url(#bg)" d="M21 20 L23 20 C24.1 20 25 20.9 25 22 C25 23.1 24.1 24 23 24 L21 24Z"/>
|
||||
<path fill="#ffffff" d="M33 15 L39 15 L39 29 C39 30.1 39.9 31 41 31 C42.1 31 43 30.1 43 29 L43 15 L49 15 L49 29 C49 33.4 45.4 37 41 37 C36.6 37 33 33.4 33 29Z"/>
|
||||
<path fill="#8ec5ff" d="M30 41 L34 41 L34 47 L39 47 L32 54 L25 47 L30 47Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 407 B |
|
After Width: | Height: | Size: 613 B |
|
After Width: | Height: | Size: 8.2 KiB |
|
After Width: | Height: | Size: 982 B |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
@@ -1,5 +1,6 @@
|
||||
# Translations (gettext). Source strings are English; add a language by adding <lang>.po and
|
||||
# listing it in LINGUAS.
|
||||
include(GNUInstallDirs)
|
||||
find_package(Gettext)
|
||||
if(NOT GETTEXT_FOUND)
|
||||
message(WARNING "gettext (msgfmt) not found: translations are not built")
|
||||
|
||||
@@ -9,9 +9,24 @@ add_executable(openpu
|
||||
target_link_libraries(openpu PRIVATE openpu_core PkgConfig::GTKMM openpu_warnings)
|
||||
target_compile_definitions(openpu PRIVATE
|
||||
GETTEXT_PACKAGE="openpu"
|
||||
OPENPU_LOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}")
|
||||
OPENPU_LOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}"
|
||||
OPENPU_BUILD_ICONDIR="${CMAKE_BINARY_DIR}/icons")
|
||||
# gtkmm headers trigger -Wshadow/-Wconversion noise we cannot fix.
|
||||
target_compile_options(openpu PRIVATE -Wno-shadow -Wno-conversion)
|
||||
|
||||
install(TARGETS openpu RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install(FILES io.github.openpu.OpenPU.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
|
||||
|
||||
# Application icon (data/icons, generated by tools/make_icons.py), installed in the hicolor theme
|
||||
# and mirrored in the build tree so that an uninstalled binary finds it too.
|
||||
set(OPENPU_ICON io.github.openpu.OpenPU)
|
||||
set(OPENPU_ICONS ${PROJECT_SOURCE_DIR}/data/icons)
|
||||
configure_file(${OPENPU_ICONS}/openpu.svg ${CMAKE_BINARY_DIR}/icons/hicolor/scalable/apps/${OPENPU_ICON}.svg COPYONLY)
|
||||
install(FILES ${OPENPU_ICONS}/openpu.svg
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps RENAME ${OPENPU_ICON}.svg)
|
||||
foreach(size 16 24 32 48 64 128 256 512)
|
||||
configure_file(${OPENPU_ICONS}/png/openpu-${size}.png
|
||||
${CMAKE_BINARY_DIR}/icons/hicolor/${size}x${size}/apps/${OPENPU_ICON}.png COPYONLY)
|
||||
install(FILES ${OPENPU_ICONS}/png/openpu-${size}.png
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/${size}x${size}/apps RENAME ${OPENPU_ICON}.png)
|
||||
endforeach()
|
||||
@@ -94,6 +94,11 @@ MainWindow::MainWindow()
|
||||
{
|
||||
set_title("OpenPU");
|
||||
set_default_size(640, 520);
|
||||
// Installed icon from the hicolor theme; the build tree has a copy for uninstalled runs.
|
||||
auto theme = Gtk::IconTheme::get_for_display(get_display());
|
||||
if (!theme->has_icon("io.github.openpu.OpenPU"))
|
||||
theme->add_search_path(OPENPU_BUILD_ICONDIR);
|
||||
set_icon_name("io.github.openpu.OpenPU");
|
||||
|
||||
try {
|
||||
store_.load();
|
||||
|
||||
@@ -14,6 +14,10 @@ target_compile_definitions(OpenPU PRIVATE
|
||||
target_compile_options(OpenPU PRIVATE -Wno-shadow -Wno-conversion)
|
||||
|
||||
# Application resources: signature, version, flags (Haiku rc + xres).
|
||||
# The vector icon (data/icons/openpu.hvif, generated by tools/make_icons.py) is embedded as hex.
|
||||
set(OPENPU_HVIF ${PROJECT_SOURCE_DIR}/data/icons/openpu.hvif)
|
||||
file(READ ${OPENPU_HVIF} OPENPU_HVIF_HEX HEX)
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${OPENPU_HVIF})
|
||||
configure_file(OpenPU.rdef.in ${CMAKE_CURRENT_BINARY_DIR}/OpenPU.rdef @ONLY)
|
||||
find_program(HAIKU_RC rc)
|
||||
find_program(HAIKU_XRES xres)
|
||||
@@ -24,6 +28,8 @@ if(HAIKU_RC AND HAIKU_XRES)
|
||||
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/OpenPU.rdef)
|
||||
add_custom_target(OpenPU_rsrc DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/OpenPU.rsrc)
|
||||
add_dependencies(OpenPU OpenPU_rsrc)
|
||||
# Relink (and so re-attach the resources) when only the resources change.
|
||||
set_target_properties(OpenPU PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/OpenPU.rsrc)
|
||||
add_custom_command(TARGET OpenPU POST_BUILD
|
||||
COMMAND ${HAIKU_XRES} -o $<TARGET_FILE:OpenPU> ${CMAKE_CURRENT_BINARY_DIR}/OpenPU.rsrc
|
||||
COMMAND ${HAIKU_MIMESET} -f $<TARGET_FILE:OpenPU>)
|
||||
|
||||
@@ -4,6 +4,8 @@ resource app_signature "application/x-vnd.openpu-OpenPU";
|
||||
|
||||
resource app_flags B_SINGLE_LAUNCH;
|
||||
|
||||
resource vector_icon $"@OPENPU_HVIF_HEX@";
|
||||
|
||||
resource app_version {
|
||||
major = @PROJECT_VERSION_MAJOR@,
|
||||
middle = @PROJECT_VERSION_MINOR@,
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Generates the OpenPU application icon (original artwork) in every format the project needs.
|
||||
|
||||
One description of the shapes (64x64 grid, the native grid of HVIF) produces:
|
||||
data/icons/openpu.svg master SVG
|
||||
data/icons/openpu-small.svg simplified variant for 16 and 24 px
|
||||
data/icons/openpu.hvif Haiku Vector Icon Format (embedded in the Haiku executable)
|
||||
data/icons/png/openpu-N.png PNG renderings, N in SIZES
|
||||
|
||||
Standard library only: the PNGs come from a small antialiased rasterizer below, so no SVG tool
|
||||
is required. Run from the repository root: python3 -I tools/make_icons.py
|
||||
"""
|
||||
import math
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import zlib
|
||||
|
||||
SIZES = (16, 24, 32, 48, 64, 128, 256, 512)
|
||||
SMALL_SIZES = (16, 24)
|
||||
|
||||
GRADIENT_TOP = (0x25, 0x63, 0xB8)
|
||||
GRADIENT_BOTTOM = (0x16, 0x3A, 0x6B)
|
||||
WHITE = (0xFF, 0xFF, 0xFF)
|
||||
ARROW = (0x8E, 0xC5, 0xFF)
|
||||
GRADIENT_Y = (4.0, 60.0) # vertical extent of the gradient, in icon units
|
||||
|
||||
# --- geometry ------------------------------------------------------------------------------------
|
||||
# A path is a closed list of points (x, y, in_handle, out_handle); a None handle means a straight
|
||||
# segment on that side. The segment from p[i] to p[i+1] uses p[i].out and p[i+1].in.
|
||||
|
||||
|
||||
def pt(x, y, hin=None, hout=None):
|
||||
return (x, y, hin, hout)
|
||||
|
||||
|
||||
def shield():
|
||||
return [
|
||||
pt(12, 4, hin=(9.8, 4)),
|
||||
pt(52, 4, hout=(54.2, 4)),
|
||||
pt(56, 8, hin=(56, 5.8)),
|
||||
pt(56, 34, hout=(56, 49)),
|
||||
pt(32, 61, hin=(45, 57), hout=(19, 57)),
|
||||
pt(8, 34, hin=(8, 49)),
|
||||
pt(8, 8, hout=(8, 5.8)),
|
||||
]
|
||||
|
||||
|
||||
def transform(path, fn):
|
||||
def f(h):
|
||||
return None if h is None else fn(*h)
|
||||
|
||||
return [(*fn(x, y), f(hin), f(hout)) for x, y, hin, hout in path]
|
||||
|
||||
|
||||
def scaled(path, s, cx=32.0, cy=32.5):
|
||||
return transform(path, lambda x, y: (cx + s * (x - cx), cy + s * (y - cy)))
|
||||
|
||||
|
||||
def moved(path, dx, dy):
|
||||
return transform(path, lambda x, y: (x + dx, y + dy))
|
||||
|
||||
|
||||
def letter_p():
|
||||
return [
|
||||
pt(16, 15),
|
||||
pt(24, 15, hout=(27.9, 15)),
|
||||
pt(30, 22, hin=(30, 18.1), hout=(30, 25.9)),
|
||||
pt(24, 29, hin=(27.9, 29)),
|
||||
pt(22, 29),
|
||||
pt(22, 37),
|
||||
pt(16, 37),
|
||||
]
|
||||
|
||||
|
||||
def letter_p_counter():
|
||||
return [
|
||||
pt(22, 20),
|
||||
pt(24, 20, hout=(25.1, 20)),
|
||||
pt(26, 22, hin=(26, 20.9), hout=(26, 23.1)),
|
||||
pt(24, 24, hin=(25.1, 24)),
|
||||
pt(22, 24),
|
||||
]
|
||||
|
||||
|
||||
def letter_u():
|
||||
return [
|
||||
pt(34, 15),
|
||||
pt(40, 15),
|
||||
pt(40, 29, hout=(40, 30.1)),
|
||||
pt(42, 31, hin=(40.9, 31), hout=(43.1, 31)),
|
||||
pt(44, 29, hin=(44, 30.1)),
|
||||
pt(44, 15),
|
||||
pt(50, 15),
|
||||
pt(50, 29, hout=(50, 33.4)),
|
||||
pt(42, 37, hin=(46.4, 37), hout=(37.6, 37)),
|
||||
pt(34, 29, hin=(34, 33.4)),
|
||||
]
|
||||
|
||||
|
||||
def arrow():
|
||||
return [pt(30, 41), pt(34, 41), pt(34, 47), pt(39, 47), pt(32, 54), pt(25, 47), pt(30, 47)]
|
||||
|
||||
|
||||
def layers(small):
|
||||
"""(style, path) pairs, painted in order. Holes are painted over with the background style,
|
||||
so every path is a simple filled outline (same result in SVG, HVIF and the rasterizer)."""
|
||||
dy = 4 if small else 0 # without the arrow, the letters move down to the optical centre
|
||||
letters = [
|
||||
("white", moved(letter_p(), -1, dy)),
|
||||
("gradient", moved(letter_p_counter(), -1, dy)),
|
||||
("white", moved(letter_u(), -1, dy)),
|
||||
]
|
||||
out = [("gradient", shield())]
|
||||
if not small:
|
||||
out += [("white", scaled(shield(), 0.90)), ("gradient", scaled(shield(), 0.86))]
|
||||
out += letters
|
||||
if not small:
|
||||
out.append(("arrow", arrow()))
|
||||
return out
|
||||
|
||||
|
||||
# --- SVG -----------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def fmt(v):
|
||||
return ("%.2f" % v).rstrip("0").rstrip(".")
|
||||
|
||||
|
||||
def svg_path(path):
|
||||
x0, y0 = path[0][:2]
|
||||
d = ["M%s %s" % (fmt(x0), fmt(y0))]
|
||||
for i in range(len(path)):
|
||||
a, b = path[i], path[(i + 1) % len(path)]
|
||||
if a[3] is None and b[2] is None:
|
||||
if i + 1 < len(path):
|
||||
d.append("L%s %s" % (fmt(b[0]), fmt(b[1])))
|
||||
continue
|
||||
c1 = a[3] or a[:2]
|
||||
c2 = b[2] or b[:2]
|
||||
d.append("C%s %s %s %s %s %s" % (fmt(c1[0]), fmt(c1[1]), fmt(c2[0]), fmt(c2[1]), fmt(b[0]), fmt(b[1])))
|
||||
return " ".join(d) + "Z"
|
||||
|
||||
|
||||
def hexcolor(c):
|
||||
return "#%02x%02x%02x" % c
|
||||
|
||||
|
||||
def write_svg(file, small):
|
||||
fills = {"gradient": "url(#bg)", "white": hexcolor(WHITE), "arrow": hexcolor(ARROW)}
|
||||
lines = [
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">',
|
||||
" <title>OpenPU</title>",
|
||||
" <defs>",
|
||||
' <linearGradient id="bg" gradientUnits="userSpaceOnUse" x1="32" y1="%s" x2="32" y2="%s">'
|
||||
% (fmt(GRADIENT_Y[0]), fmt(GRADIENT_Y[1])),
|
||||
' <stop offset="0" stop-color="%s"/>' % hexcolor(GRADIENT_TOP),
|
||||
' <stop offset="1" stop-color="%s"/>' % hexcolor(GRADIENT_BOTTOM),
|
||||
" </linearGradient>",
|
||||
" </defs>",
|
||||
]
|
||||
for style, path in layers(small):
|
||||
lines.append(' <path fill="%s" d="%s"/>' % (fills[style], svg_path(path)))
|
||||
lines.append("</svg>")
|
||||
with open(file, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
|
||||
|
||||
# --- HVIF ----------------------------------------------------------------------------------------
|
||||
# Format of Haiku's FlatIconImporter: "ncif", then styles, paths and shapes, each list prefixed
|
||||
# with a uint8 count. Coordinates: one byte (value + 32) for integers in -32..95, otherwise two
|
||||
# bytes, 0x8000 | round((value + 128) * 102). Matrices use 24-bit floats.
|
||||
|
||||
STYLE_SOLID_NO_ALPHA = 3
|
||||
STYLE_GRADIENT = 2
|
||||
GRADIENT_LINEAR = 0
|
||||
GRADIENT_FLAG_TRANSFORM = 1 << 1
|
||||
GRADIENT_FLAG_NO_ALPHA = 1 << 2
|
||||
PATH_FLAG_CLOSED = 1 << 1
|
||||
PATH_FLAG_NO_CURVES = 1 << 3
|
||||
SHAPE_TYPE_PATH_SOURCE = 10
|
||||
|
||||
|
||||
def hvif_coord(v):
|
||||
if abs(v - round(v)) < 1e-9 and -32 <= round(v) <= 95:
|
||||
return bytes([int(round(v)) + 32])
|
||||
n = int(round((v + 128.0) * 102.0))
|
||||
if not 0 <= n < 0x8000:
|
||||
raise ValueError("coordinate out of range: %r" % v)
|
||||
return bytes([0x80 | (n >> 8), n & 0xFF])
|
||||
|
||||
|
||||
def float24(v):
|
||||
if v == 0:
|
||||
return b"\0\0\0"
|
||||
bits = struct.unpack("<I", struct.pack("<f", v))[0]
|
||||
sign = bits >> 31
|
||||
exponent = ((bits >> 23) & 0xFF) - 127 + 32
|
||||
if not 0 <= exponent < 64:
|
||||
raise ValueError("float out of range: %r" % v)
|
||||
value = (sign << 23) | (exponent << 17) | ((bits & 0x7FFFFF) >> 6)
|
||||
return bytes([(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF])
|
||||
|
||||
|
||||
def hvif_path(path):
|
||||
curved = any(p[2] is not None or p[3] is not None for p in path)
|
||||
out = bytearray([PATH_FLAG_CLOSED | (0 if curved else PATH_FLAG_NO_CURVES), len(path)])
|
||||
for x, y, hin, hout in path:
|
||||
hin = hin or (x, y)
|
||||
hout = hout or (x, y)
|
||||
values = (x, y, *hin, *hout) if curved else (x, y)
|
||||
for v in values:
|
||||
out += hvif_coord(v)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def write_hvif(file, small):
|
||||
# Linear gradients run from -64 to 64 on their own x axis: map it to the vertical extent.
|
||||
half = (GRADIENT_Y[1] - GRADIENT_Y[0]) / 128.0
|
||||
mid = (GRADIENT_Y[0] + GRADIENT_Y[1]) / 2.0
|
||||
matrix = (0.0, half, -half, 0.0, 32.0, mid) # agg::trans_affine: sx, shy, shx, sy, tx, ty
|
||||
gradient = bytearray([STYLE_GRADIENT, GRADIENT_LINEAR, GRADIENT_FLAG_TRANSFORM | GRADIENT_FLAG_NO_ALPHA, 2])
|
||||
for m in matrix:
|
||||
gradient += float24(m)
|
||||
gradient += bytes([0, *GRADIENT_TOP, 255, *GRADIENT_BOTTOM])
|
||||
styles = {
|
||||
"gradient": bytes(gradient),
|
||||
"white": bytes([STYLE_SOLID_NO_ALPHA, *WHITE]),
|
||||
"arrow": bytes([STYLE_SOLID_NO_ALPHA, *ARROW]),
|
||||
}
|
||||
used = []
|
||||
for style, _ in layers(small):
|
||||
if style not in used:
|
||||
used.append(style)
|
||||
data = bytearray(b"ncif")
|
||||
data.append(len(used))
|
||||
for s in used:
|
||||
data += styles[s]
|
||||
ls = layers(small)
|
||||
data.append(len(ls))
|
||||
for _, path in ls:
|
||||
data += hvif_path(path)
|
||||
data.append(len(ls))
|
||||
for i, (style, _) in enumerate(ls):
|
||||
data += bytes([SHAPE_TYPE_PATH_SOURCE, used.index(style), 1, i, 0])
|
||||
with open(file, "wb") as f:
|
||||
f.write(data)
|
||||
return len(data)
|
||||
|
||||
|
||||
# --- rasterizer and PNG ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def flatten(path, steps=24):
|
||||
poly = []
|
||||
for i in range(len(path)):
|
||||
a, b = path[i], path[(i + 1) % len(path)]
|
||||
poly.append(a[:2])
|
||||
if a[3] is None and b[2] is None:
|
||||
continue
|
||||
c1, c2 = a[3] or a[:2], b[2] or b[:2]
|
||||
for k in range(1, steps):
|
||||
t = k / steps
|
||||
u = 1 - t
|
||||
poly.append((
|
||||
u ** 3 * a[0] + 3 * u * u * t * c1[0] + 3 * u * t * t * c2[0] + t ** 3 * b[0],
|
||||
u ** 3 * a[1] + 3 * u * u * t * c1[1] + 3 * u * t * t * c2[1] + t ** 3 * b[1],
|
||||
))
|
||||
return poly
|
||||
|
||||
|
||||
def coverage(poly, size, sub=16):
|
||||
"""Antialiased non-zero coverage (0..1) of a polygon given in 64-unit coordinates."""
|
||||
scale = size / 64.0
|
||||
pts = [(x * scale, y * scale) for x, y in poly]
|
||||
edges = []
|
||||
for i in range(len(pts)):
|
||||
(x0, y0), (x1, y1) = pts[i], pts[(i + 1) % len(pts)]
|
||||
if y0 != y1:
|
||||
edges.append((x0, y0, x1, y1, 1 if y1 > y0 else -1))
|
||||
cov = [0.0] * (size * size)
|
||||
w = 1.0 / sub
|
||||
for row in range(size):
|
||||
base = row * size
|
||||
for s in range(sub):
|
||||
y = row + (s + 0.5) / sub
|
||||
crossings = []
|
||||
for x0, y0, x1, y1, d in edges:
|
||||
if (y0 <= y < y1) or (y1 <= y < y0):
|
||||
crossings.append((x0 + (y - y0) * (x1 - x0) / (y1 - y0), d))
|
||||
crossings.sort()
|
||||
winding = 0
|
||||
for i in range(len(crossings) - 1):
|
||||
winding += crossings[i][1]
|
||||
if winding == 0:
|
||||
continue
|
||||
xa, xb = max(crossings[i][0], 0.0), min(crossings[i + 1][0], float(size))
|
||||
if xb <= xa:
|
||||
continue
|
||||
ia, ib = int(xa), int(xb)
|
||||
if ia == ib:
|
||||
cov[base + ia] += (xb - xa) * w
|
||||
continue
|
||||
cov[base + ia] += (ia + 1 - xa) * w
|
||||
for px in range(ia + 1, min(ib, size)):
|
||||
cov[base + px] += w
|
||||
if ib < size:
|
||||
cov[base + ib] += (xb - ib) * w
|
||||
return cov
|
||||
|
||||
|
||||
def render(size, small):
|
||||
rgba = [[0.0, 0.0, 0.0, 0.0] for _ in range(size * size)] # premultiplied
|
||||
for style, path in layers(small):
|
||||
cov = coverage(flatten(path), size)
|
||||
for i, c in enumerate(cov):
|
||||
if c <= 0:
|
||||
continue
|
||||
a = min(c, 1.0)
|
||||
if style == "gradient":
|
||||
y = ((i // size) + 0.5) * 64.0 / size
|
||||
t = min(max((y - GRADIENT_Y[0]) / (GRADIENT_Y[1] - GRADIENT_Y[0]), 0.0), 1.0)
|
||||
color = [GRADIENT_TOP[k] + (GRADIENT_BOTTOM[k] - GRADIENT_TOP[k]) * t for k in range(3)]
|
||||
else:
|
||||
color = WHITE if style == "white" else ARROW
|
||||
p = rgba[i]
|
||||
for k in range(3):
|
||||
p[k] = color[k] / 255.0 * a + p[k] * (1 - a)
|
||||
p[3] = a + p[3] * (1 - a)
|
||||
return rgba
|
||||
|
||||
|
||||
def write_png(file, size, rgba):
|
||||
raw = bytearray()
|
||||
for row in range(size):
|
||||
raw.append(0)
|
||||
for p in rgba[row * size:(row + 1) * size]:
|
||||
a = p[3]
|
||||
if a <= 0:
|
||||
raw += b"\0\0\0\0"
|
||||
continue
|
||||
raw += bytes([min(255, int(round(p[k] / a * 255))) for k in range(3)] + [int(round(a * 255))])
|
||||
|
||||
def chunk(kind, payload):
|
||||
body = kind + payload
|
||||
return struct.pack(">I", len(payload)) + body + struct.pack(">I", zlib.crc32(body) & 0xFFFFFFFF)
|
||||
|
||||
with open(file, "wb") as f:
|
||||
f.write(b"\x89PNG\r\n\x1a\n")
|
||||
f.write(chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0)))
|
||||
f.write(chunk(b"IDAT", zlib.compress(bytes(raw), 9)))
|
||||
f.write(chunk(b"IEND", b""))
|
||||
|
||||
|
||||
def main():
|
||||
root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data", "icons")
|
||||
os.makedirs(os.path.join(root, "png"), exist_ok=True)
|
||||
write_svg(os.path.join(root, "openpu.svg"), small=False)
|
||||
write_svg(os.path.join(root, "openpu-small.svg"), small=True)
|
||||
n = write_hvif(os.path.join(root, "openpu.hvif"), small=False)
|
||||
print("openpu.hvif: %d bytes" % n)
|
||||
sizes = [int(a) for a in sys.argv[1:]] or SIZES
|
||||
for size in sizes:
|
||||
write_png(os.path.join(root, "png", "openpu-%d.png" % size), size, render(size, size in SMALL_SIZES))
|
||||
print("openpu-%d.png" % size)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||