Files

168 lines
6.0 KiB
Python

# Docs inputs for the 3D, CAD and font guide pages (tools/fixtures.py, tools/docs_check.py): Khronos glTF samples
# from tests/fixtures/gltf (BoxTextured embedded and with external .bin + PNG, Box.glb, RiggedSimple.glb), a
# hand-written OBJ + MTL cube, MagicaVoxel's chr_knight.vox, a small hand-written STEP file, an ASCII subset of
# DejaVu Sans (nixpkgs dejavu_fonts) made by fontTools as TTF, WOFF and WOFF2, and a two-glyph BMFont (text
# descriptor + PNG page) written here.
# tests/gen/docs_3d.luau { files = { [name] = bytes }, vars = { texturedGltfText = ..., ... } }
import importlib.util, json, os, struct, subprocess, zlib
INPUTS = ["tools/docs_check.py", "tests/fixtures/gltf", "tests/fixtures/vox"]
OBJ = """# a unit cube, one material
mtllib cube.mtl
o cube
v 0 0 0
v 1 0 0
v 1 1 0
v 0 1 0
v 0 0 1
v 1 0 1
v 1 1 1
v 0 1 1
vt 0 0
vt 1 0
vt 1 1
vt 0 1
usemtl red
f 1/1 4/4 3/3 2/2
f 5/1 6/2 7/3 8/4
f 1/1 2/2 6/3 5/4
f 2/1 3/2 7/3 6/4
f 3/1 4/2 8/3 7/4
f 4/1 1/2 5/3 8/4
"""
MTL = """newmtl red
Ka 0 0 0
Kd 0.8 0.1 0.1
Ks 0.5 0.5 0.5
Ns 32
"""
STEP = """ISO-10303-21;
HEADER;
FILE_DESCRIPTION(('a point and a direction'),'2;1');
FILE_NAME('part.step','2026-10-31T12:00:00',('docs'),(''),'rosetta docs','','');
FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));
ENDSEC;
DATA;
#1=CARTESIAN_POINT('origin',(0.,0.,0.));
#2=DIRECTION('z',(0.,0.,1.));
#3=DIRECTION('x',(1.,0.,0.));
#4=AXIS2_PLACEMENT_3D('',#1,#2,#3);
#5=CARTESIAN_POINT('tip',(10.,20.,5.5));
#6=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
ENDSEC;
END-ISO-10303-21;
"""
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 fonts(ctx):
"""ASCII subset of DejaVu Sans as ttf / woff / woff2 bytes"""
out = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#dejavu_fonts"],
capture_output=True, check=True, text=True).stdout.strip().split("\n")[-1]
path = os.path.join(out, "share", "fonts", "truetype", "DejaVuSans.ttf")
code = r"""
import io, sys, json, base64
from fontTools import subset
from fontTools.ttLib import TTFont
res = {}
for flavor in (None, "woff", "woff2"):
f = TTFont(sys.argv[1] if len(sys.argv) > 1 else %r)
opts = subset.Options()
opts.layout_features = ["kern"]
opts.name_IDs = ["*"]
opts.hinting = False
opts.flavor = flavor
s = subset.Subsetter(opts)
s.populate(unicodes=range(32, 127))
s.subset(f)
b = io.BytesIO()
f.flavor = flavor
f.save(b)
res[flavor or "ttf"] = base64.b64encode(b.getvalue()).decode()
print(json.dumps(res))
""" % path
import base64
res = json.loads(ctx.python(["fonttools", "brotli"], code))
return {k: base64.b64decode(v) for k, v in res.items()}
def bmfont():
"""a 16 x 8 page with 'A' (a filled 6 x 7 block) and 'B' (an outline), and its text descriptor"""
w, h = 16, 8
px = bytearray(w * h * 4)
for y in range(7):
for x in range(6):
o = (y * w + x) * 4
px[o:o + 4] = b"\xff\xff\xff\xff"
bx = 8 + x
if x in (0, 5) or y in (0, 6):
o = (y * w + bx) * 4
px[o:o + 4] = b"\xff\xff\xff\xff"
fnt = ('info face="Mini" size=8 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=0 aa=1 '
'padding=0,0,0,0 spacing=1,1\n'
'common lineHeight=9 base=7 scaleW=16 scaleH=8 pages=1 packed=0\n'
'page id=0 file="mini_0.png"\n'
'chars count=2\n'
'char id=65 x=0 y=0 width=6 height=7 xoffset=0 yoffset=0 xadvance=7 page=0 chnl=15\n'
'char id=66 x=8 y=0 width=6 height=7 xoffset=0 yoffset=0 xadvance=7 page=0 chnl=15\n'
'kernings count=1\n'
'kerning first=65 second=66 amount=-1\n')
return fnt, png(w, h, bytes(px))
def generate(ctx):
dc = docs_check(ctx)
g = "tests/fixtures/gltf/"
files = {
"box_textured_embedded.gltf": ctx.read(g + "BoxTextured/glTF-Embedded/BoxTextured.gltf"),
"BoxTextured.gltf": ctx.read(g + "BoxTextured/glTF/BoxTextured.gltf"),
"BoxTextured0.bin": ctx.read(g + "BoxTextured/glTF/BoxTextured0.bin"),
"CesiumLogoFlat.png": ctx.read(g + "BoxTextured/glTF/CesiumLogoFlat.png"),
"Box.glb": ctx.read(g + "Box/glTF-Binary/Box.glb"),
"RiggedSimple.glb": ctx.read(g + "RiggedSimple/glTF-Binary/RiggedSimple.glb"),
"cube.obj": OBJ.encode(),
"cube.mtl": MTL.encode(),
"chr_knight.vox": ctx.read("tests/fixtures/vox/chr_knight.vox"),
"part.step": STEP.encode(),
}
f = fonts(ctx)
files["DejaVuSans-ascii.ttf"] = f["ttf"]
files["DejaVuSans-ascii.woff"] = f["woff"]
files["DejaVuSans-ascii.woff2"] = f["woff2"]
fnt, page = bmfont()
files["mini.fnt"] = fnt.encode()
files["mini_0.png"] = page
dc.emit_inputs(ctx, files, {
"texturedGltfText": "box_textured_embedded.gltf", # glTF with its texture embedded (data: URIs)
"boxGltfText": "BoxTextured.gltf", # glTF referencing BoxTextured0.bin and CesiumLogoFlat.png
"boxBinBytes": "BoxTextured0.bin",
"logoPngBytes": "CesiumLogoFlat.png",
"boxGlbBytes": "Box.glb",
"riggedGlbBytes": "RiggedSimple.glb", # skin + animation
"objText": "cube.obj",
"mtlText": "cube.mtl",
"voxBytes": "chr_knight.vox",
"stepText": "part.step",
"ttfBytes": "DejaVuSans-ascii.ttf",
"woffBytes": "DejaVuSans-ascii.woff",
"woff2Bytes": "DejaVuSans-ascii.woff2",
"fntText": "mini.fnt",
"fntPageBytes": "mini_0.png",
})