interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / scripts / tests / check-render.sh
| 1 | #!/bin/sh |
| 2 | # Rendered width check: every built page, laid out by a real browser in frames 1280, 390 and 360 px |
| 3 | # wide (WIDTHS="1280 390 360" by default), must not be wider than the frame shows (its view, the |
| 4 | # frame less any scrollbar). Command boxes scroll inside themselves and do not count. |
| 5 | # No link of the header nav may be narrower than its label: a squeezed nav item lets its text run into |
| 6 | # its neighbour without making the page wider. |
| 7 | # A page set in the paper look (class "paper-type") must also have loaded the three self-hosted Latin |
| 8 | # Modern faces; a page that fell back to the system serif fails. |
| 9 | # No page may fetch anything from outside the copy (X= below): Chinese text needs no font download. |
| 10 | # No visible CJK character (U+2E80 and up: ideographs, kana, CJK and full-width punctuation) may be |
| 11 | # drawn as a missing glyph: each one is drawn on a canvas in the font its element computes and compared |
| 12 | # with the drawing of three codepoints that no installed font covers (U+A980, U+1C00, U+A000). A |
| 13 | # character drawn like them, or drawn blank, is a missing glyph (T= below), as a screenshot shows it. |
| 14 | # |
| 15 | # sh scripts/tests/check-render.sh OUT [HOPOUT] |
| 16 | # FONTBASE=https://host sh scripts/tests/check-render.sh OUT also asks that server how it labels |
| 17 | # each font file of OUT: font/otf and |
| 18 | # application/octet-stream both pass |
| 19 | # SHOTS=DIR SHOT=/papers/ sh scripts/tests/check-render.sh OUT also writes full-page PNGs of the |
| 20 | # page SHOT at 360 and 1440 px |
| 21 | # SHOTS=DIR SHOT=all sh scripts/tests/check-render.sh OUT a PNG of every page at every width |
| 22 | # |
| 23 | # Needs Chrome or Chromium (CHROME=/path/to/browser, default google-chrome). Without a browser this |
| 24 | # exits 3 and prints NOT_RUN: a check that did not run is not a pass. The glyph check also exits 3 |
| 25 | # with NOT_RUN when one of the three reference codepoints has a font on this machine (fc-list). |
| 26 | # How it measures: the output is copied, its root-absolute addresses (href, src and CSS url) are |
| 27 | # pointed at the copy so the stylesheet and fonts load from disk, and each page is opened in a frame |
| 28 | # of each width inside a small wrapper page. The wrapper reads the frame's scrollWidth, the header nav |
| 29 | # links whose label is wider than their box, the load status of its font faces, the addresses it |
| 30 | # fetched and the drawing of its CJK characters; those reads are the only script involved, they live |
| 31 | # in a temporary file, and they are never part of the site. |
| 32 | set -eu |
| 33 | [ "$#" -ge 1 ] || { echo 'usage: check-render.sh OUT [HOPOUT]' >&2; exit 2; } |
| 34 | CHROME=${CHROME:-google-chrome} |
| 35 | if ! command -v "$CHROME" >/dev/null 2>&1; then |
| 36 | echo "NOT_RUN: no browser ($CHROME); set CHROME to Chrome or Chromium" |
| 37 | exit 3 |
| 38 | fi |
| 39 | WIDTHS=${WIDTHS:-1280 390 360} |
| 40 | SHOTS=${SHOTS:-} |
| 41 | FONTBASE=${FONTBASE:-} |
| 42 | SHOT=${SHOT:-/papers/} |
| 43 | if command -v fc-list >/dev/null 2>&1; then |
| 44 | for c in a980 1c00 a000; do |
| 45 | if [ -n "$(fc-list ":charset=$c" family)" ]; then |
| 46 | echo "NOT_RUN: a font on this machine covers U+$c, so it cannot stand for a missing glyph"; exit 3 |
| 47 | fi |
| 48 | done |
| 49 | else |
| 50 | echo 'NOT_RUN: fc-list is missing, so the missing-glyph references cannot be confirmed'; exit 3 |
| 51 | fi |
| 52 | W=$(mktemp -d) |
| 53 | trap 'rm -rf "$W"' 0 |
| 54 | trap 'exit 1' 1 2 15 |
| 55 | N=0 |
| 56 | BAD=0 |
| 57 | S=0 |
| 58 | # measure FILE WIDTH: prints "W=.. V=.. H=.. H1=.. CSS=.. O=.. T=.. C=.. X=.. F=.." for the page |
| 59 | # laid out WIDTH px wide (T missing glyphs among C distinct CJK characters, X outside fetches) |
| 60 | measure() { |
| 61 | printf '%s\n' '<!doctype html><html><head><meta charset="utf-8"><title>pending</title></head><body>' \ |
| 62 | "<iframe id=\"f\" width=\"$2\" height=\"800\" src=\"file://$1\"></iframe>" \ |
| 63 | '<script>function img(ch,font){var c=document.createElement("canvas");c.width=64;c.height=64;var x=c.getContext("2d");x.font=font;x.textBaseline="top";x.fillText(ch,4,4);var d=x.getImageData(0,0,64,64).data,h=0;for(var i=3;i<d.length;i+=4){h=(h*31+d[i])>>>0}return h}' \ |
| 64 | 'function tofu(d){var refs={},seen={},t=0,c=0,w=d.createTreeWalker(d.body,4);while(w.nextNode()){var n=w.currentNode,e=n.parentElement;if(!e||!e.getClientRects().length)continue;var s=getComputedStyle(e);if(s.visibility=="hidden")continue;var f=s.fontStyle+" "+s.fontWeight+" 40px "+s.fontFamily;for(var ch of n.data){if(ch.codePointAt(0)<0x2e80||/\s/.test(ch)||seen[f+ch])continue;seen[f+ch]=1;c++;if(!(f in refs)){var a=img("\ua980",f),b=img("\u1c00",f),y=img("\ua000",f);refs[f]=(a==b&&b==y)?a:-1}var h=img(ch,f);if(refs[f]<0||h==refs[f]||h==0){t++}}}return " T="+t+" C="+c}' \ |
| 65 | 'addEventListener("load",function(){var fr=document.getElementById("f"),d=fr.contentDocument;d.fonts.ready.then(function(){var n=0;d.fonts.forEach(function(x){if(x.status=="loaded"){n++}});var o=0;d.querySelectorAll(".site-head nav a").forEach(function(a){if(a.scrollWidth>a.clientWidth){o++}});var xr=0;fr.contentWindow.performance.getEntriesByType("resource").forEach(function(r){if(r.name.indexOf("file:")!=0){xr++}});document.title="W="+d.documentElement.scrollWidth+" V="+d.documentElement.clientWidth+" H="+d.documentElement.scrollHeight+" H1="+d.getElementsByTagName("h1").length+" CSS="+d.styleSheets.length+" O="+o+tofu(d)+" X="+xr+" F="+n})})</script>' \ |
| 66 | '</body></html>' > "$W/wrap.html" |
| 67 | "$CHROME" --headless --disable-gpu --no-sandbox --no-first-run --allow-file-access-from-files \ |
| 68 | --user-data-dir="$W/profile" --virtual-time-budget=5000 --dump-dom "file://$W/wrap.html" 2>/dev/null \ |
| 69 | | sed -n 's/.*<title>\(W=[0-9]* V=[0-9]* H=[0-9]* H1=[0-9]* CSS=[0-9]* O=[0-9]* T=[0-9]* C=[0-9]* X=[0-9]* F=[0-9]*\)<\/title>.*/\1/p' | head -n 1 |
| 70 | } |
| 71 | # shot FILE WIDTH PNG: a full-page screenshot of the page laid out WIDTH px wide |
| 72 | shot() { |
| 73 | G=$(measure "$1" "$2") |
| 74 | H=${G#*H=}; H=${H%% *} |
| 75 | case $H in ''|*[!0-9]*) echo "FAIL screenshot $3: no page height"; BAD=1; return ;; esac |
| 76 | "$CHROME" --headless --disable-gpu --no-sandbox --no-first-run --allow-file-access-from-files \ |
| 77 | --user-data-dir="$W/profile" --hide-scrollbars --virtual-time-budget=5000 \ |
| 78 | --window-size="$2,$H" --screenshot="$3" "file://$1" > /dev/null 2>&1 || true |
| 79 | if [ -s "$3" ]; then echo "SHOT $3 ($2 x $H px)"; else echo "FAIL screenshot $3 was not written"; BAD=1; fi |
| 80 | } |
| 81 | for OUT in "$@"; do |
| 82 | [ -d "$OUT" ] || continue |
| 83 | S=$((S + 1)) |
| 84 | D=$W/s$S |
| 85 | cp -R "$OUT" "$D" |
| 86 | find "$D" -name '*.css' | while IFS= read -r c; do |
| 87 | sed -e "s|url(/|url($D/|g" "$c" > "$c.tmp" && mv "$c.tmp" "$c" |
| 88 | done |
| 89 | find "$D" -name index.html | sort > "$W/pages" |
| 90 | while IFS= read -r f; do |
| 91 | sed -e "s|href=\"/|href=\"$D/|g" -e "s|src=\"/|src=\"$D/|g" "$f" > "$f.tmp" && mv "$f.tmp" "$f" |
| 92 | R=${f#"$D"}; R=${R%index.html} |
| 93 | PAPER=0 |
| 94 | if grep -q 'class="[^"]*paper-type' "$f"; then PAPER=1; fi |
| 95 | for LIMIT in $WIDTHS; do |
| 96 | GOT=$(measure "$f" "$LIMIT") |
| 97 | N=$((N + 1)) |
| 98 | WIDTH=${GOT#W=}; WIDTH=${WIDTH%% *} |
| 99 | VIEW=${GOT#*V=}; VIEW=${VIEW%% *} |
| 100 | CSS=${GOT#*CSS=}; CSS=${CSS%% *} |
| 101 | OVER=${GOT#*O=}; OVER=${OVER%% *} |
| 102 | TOFU=${GOT#*T=}; TOFU=${TOFU%% *} |
| 103 | CJK=${GOT#*C=}; CJK=${CJK%% *} |
| 104 | EXT=${GOT#*X=}; EXT=${EXT%% *} |
| 105 | FONTS=${GOT##*F=} |
| 106 | case $WIDTH$VIEW$OVER$TOFU$CJK$EXT$FONTS in |
| 107 | ''|*[!0-9]*) echo "FAIL site $S $R: the browser gave no measurement (frame $LIMIT px)"; BAD=1; continue ;; |
| 108 | esac |
| 109 | if [ "$CSS" != 1 ]; then echo "FAIL site $S $R: the stylesheet did not load ($GOT)"; BAD=1; continue; fi |
| 110 | if [ "$PAPER" = 1 ] && [ "$FONTS" != 3 ]; then |
| 111 | echo "FAIL site $S $R: the paper look loaded $FONTS of the 3 Latin Modern faces"; BAD=1; continue |
| 112 | fi |
| 113 | if [ "$VIEW" -gt "$LIMIT" ] || [ "$WIDTH" -gt "$VIEW" ]; then |
| 114 | echo "FAIL site $S $R: content is $WIDTH px wide in a view of $VIEW px (frame $LIMIT px)"; BAD=1 |
| 115 | elif [ "$OVER" != 0 ]; then |
| 116 | echo "FAIL site $S $R: $OVER header nav links are narrower than their labels (frame $LIMIT px)"; BAD=1 |
| 117 | elif [ "$EXT" != 0 ]; then |
| 118 | echo "FAIL site $S $R: $EXT addresses were fetched from outside the output (frame $LIMIT px)"; BAD=1 |
| 119 | elif [ "$TOFU" != 0 ]; then |
| 120 | echo "FAIL site $S $R: $TOFU of $CJK CJK characters are drawn as a missing glyph (frame $LIMIT px)"; BAD=1 |
| 121 | elif [ "$PAPER" = 1 ]; then |
| 122 | echo "PASS site $S $R @$LIMIT: content $WIDTH px, view $VIEW px, Latin Modern faces loaded $FONTS" |
| 123 | elif [ "$CJK" != 0 ]; then |
| 124 | echo "PASS site $S $R @$LIMIT: content $WIDTH px, view $VIEW px, CJK characters $CJK drawn" |
| 125 | else |
| 126 | echo "PASS site $S $R @$LIMIT: content $WIDTH px, view $VIEW px" |
| 127 | fi |
| 128 | done |
| 129 | if [ -n "$SHOTS" ] && [ "$S" = 1 ] && [ "$R" = "$SHOT" ]; then |
| 130 | mkdir -p "$SHOTS" |
| 131 | NAME=$(printf '%s' "$SHOT" | tr -c 'a-z0-9' '-' | sed -e 's/^-*//' -e 's/-*$//') |
| 132 | [ -n "$NAME" ] || NAME=home |
| 133 | shot "$f" 360 "$SHOTS/$NAME-360.png" |
| 134 | shot "$f" 1440 "$SHOTS/$NAME-1440.png" |
| 135 | fi |
| 136 | if [ -n "$SHOTS" ] && [ "$SHOT" = all ]; then |
| 137 | mkdir -p "$SHOTS" |
| 138 | NAME=$(printf '%s' "s$S$R" | tr -c 'a-zA-Z0-9' '-' | sed -e 's/^-*//' -e 's/-*$//') |
| 139 | for LIMIT in $WIDTHS; do shot "$f" "$LIMIT" "$SHOTS/$NAME-$LIMIT.png"; done |
| 140 | fi |
| 141 | done < "$W/pages" |
| 142 | done |
| 143 | # Optional: how a server labels the font files. The page measurements above load fonts from disk, so |
| 144 | # they do not depend on the label. A browser accepts an OpenType file sent as font/otf and, as |
| 145 | # measured with Chromium, as application/octet-stream; both pass here. Anything else fails. |
| 146 | if [ -n "$FONTBASE" ]; then |
| 147 | command -v curl > /dev/null 2>&1 || { echo 'FAIL FONTBASE needs curl'; exit 1; } |
| 148 | for f in "$1"/public/fonts/*.otf; do |
| 149 | [ -f "$f" ] || continue |
| 150 | U=$FONTBASE/public/fonts/$(basename "$f") |
| 151 | HEAD=$(curl -sS -I --max-time 30 "$U" 2>&1 | tr -d '\r') || HEAD= |
| 152 | CODE=$(printf '%s\n' "$HEAD" | sed -n '1s/^[^ ]* \([0-9]*\).*/\1/p') |
| 153 | TYPE=$(printf '%s\n' "$HEAD" | sed -n 's/^[Cc]ontent-[Tt]ype: *\([^;]*\).*/\1/p' | head -n 1) |
| 154 | case "$CODE $TYPE" in |
| 155 | '200 font/otf') echo "PASS served font $U: font/otf" ;; |
| 156 | '200 application/octet-stream') echo "PASS served font $U: application/octet-stream (accepted; font/otf is the right label)" ;; |
| 157 | *) echo "FAIL served font $U: status ${CODE:-none}, type ${TYPE:-none}"; BAD=1 ;; |
| 158 | esac |
| 159 | done |
| 160 | fi |
| 161 | [ "$N" -gt 0 ] || { echo 'FAIL no pages found'; exit 1; } |
| 162 | [ "$BAD" -eq 0 ] || { echo "RESULT render fail (pages $N)"; exit 1; } |
| 163 | echo "RESULT render pass=$N fail=0 (pages x widths $WIDTHS: no page wider than its view, no squeezed nav label, nothing fetched from outside, no CJK character drawn as a missing glyph; paper look in Latin Modern)" |