91 lines
3.9 KiB
Python
91 lines
3.9 KiB
Python
# Docs inputs (tools/fixtures.py): the shared files the guide snippets read (tools/docs_check.py), made by reference
|
|
# tools: a PNG written here (zlib), GIF / JPEG / WebP by ImageMagick, a stereo tone as WAV (Python's wave), FLAC, MP3
|
|
# and Ogg Vorbis by ffmpeg with tags.
|
|
# tests/gen/docs_inputs.luau { files = { [name] = bytes }, vars = { pngBytes = "photo.png", ... } }
|
|
# tests/gen/docs_files/assets/<name> the same files, for the snippets that load by path
|
|
import importlib.util, io, math, os, struct, tempfile, wave, zlib
|
|
|
|
INPUTS = ["tools/docs_check.py"]
|
|
|
|
|
|
def docs_check(ctx):
|
|
spec = importlib.util.spec_from_file_location("docs_check", os.path.join(ctx.root, "tools", "docs_check.py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
def png(w, h, rgba):
|
|
def chunk(t, d):
|
|
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xffffffff)
|
|
raw = b"".join(b"\0" + rgba[y * w * 4:(y + 1) * w * 4] for y in range(h))
|
|
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0)) +
|
|
chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b""))
|
|
|
|
|
|
def photo(w, h):
|
|
"""a gradient with a soft-edged disc: red across, green down, a translucent right edge"""
|
|
out = bytearray()
|
|
for y in range(h):
|
|
for x in range(w):
|
|
d = math.hypot(x - w / 2, y - h / 2) / (h / 2)
|
|
out += bytes((x * 255 // (w - 1), y * 255 // (h - 1), 255 if d < 0.6 else 64, 255 if x < w - 8 else 128))
|
|
return bytes(out)
|
|
|
|
|
|
def tone(rate, seconds, channels=2):
|
|
n = int(rate * seconds)
|
|
b = io.BytesIO()
|
|
with wave.open(b, "wb") as wv:
|
|
wv.setnchannels(channels)
|
|
wv.setsampwidth(2)
|
|
wv.setframerate(rate)
|
|
frames = bytearray()
|
|
for i in range(n):
|
|
env = min(1.0, i / 441, (n - i) / 441)
|
|
for c in range(channels):
|
|
f = 440 if c == 0 else 660
|
|
frames += struct.pack("<h", int(12000 * env * math.sin(2 * math.pi * f * i / rate)))
|
|
wv.writeframes(bytes(frames))
|
|
return b.getvalue()
|
|
|
|
|
|
def generate(ctx):
|
|
dc = docs_check(ctx)
|
|
files = {}
|
|
w, h = 64, 48
|
|
files["photo.png"] = png(w, h, photo(w, h))
|
|
with tempfile.TemporaryDirectory() as d:
|
|
src = os.path.join(d, "photo.png")
|
|
with open(src, "wb") as f:
|
|
f.write(files["photo.png"])
|
|
for name, args in (("photo.jpg", ["-quality", "90"]), ("photo.webp", ["-define", "webp:lossless=true"])):
|
|
out = os.path.join(d, name)
|
|
ctx.nix("imagemagick", ["magick", src] + args + [out])
|
|
files[name] = open(out, "rb").read()
|
|
gif = os.path.join(d, "anim.gif")
|
|
ctx.nix("imagemagick", ["magick", "-delay", "10", "-size", "32x24", "xc:red", "xc:lime", "xc:blue",
|
|
"-loop", "0", gif])
|
|
files["anim.gif"] = open(gif, "rb").read()
|
|
files["tone.wav"] = tone(44100, 1.0)
|
|
wav = os.path.join(d, "tone.wav")
|
|
with open(wav, "wb") as f:
|
|
f.write(files["tone.wav"])
|
|
tags = ["-metadata", "title=Docs Tone", "-metadata", "artist=formats-luau", "-metadata", "album=Guide",
|
|
"-metadata", "date=2026"]
|
|
for name, codec in (("tone.flac", ["-c:a", "flac"]), ("tone.mp3", ["-c:a", "libmp3lame", "-b:a", "128k"]),
|
|
("tone.ogg", ["-c:a", "libvorbis", "-q:a", "3"])):
|
|
out = os.path.join(d, name)
|
|
ctx.nix("ffmpeg", ["ffmpeg", "-v", "error", "-y", "-i", wav] + codec + tags + [out])
|
|
files[name] = open(out, "rb").read()
|
|
dc.emit_inputs(ctx, files, {
|
|
"pngBytes": "photo.png",
|
|
"jpegBytes": "photo.jpg",
|
|
"webpBytes": "photo.webp",
|
|
"gifBytes": "anim.gif",
|
|
"wavBytes": "tone.wav",
|
|
"flacBytes": "tone.flac",
|
|
"mp3Bytes": "tone.mp3",
|
|
"oggBytes": "tone.ogg",
|
|
})
|