Layout API
The layout module adds protected machine metadata and maps codec frames to
physical pages. read_pages() is the inverse validation step.
glyphive.layout
Encoded line stream ⇄ paginated document with protected layout metadata.
This geometry-agnostic module groups codec L/P frames into physical
pages. Page 1 starts with a human-readable #!glyphive summary and then
fixed-width H machine-header frames. Every page ends in a T machine
footer followed by a human PAGE n/total hint on the same line.
Restore trusts only the H/T frames. They use the measured-safe 16-character bootstrap alphabet and CRC-16 checks; the compact H-frame envelope additionally carries its exact length and a digest. Thus the selected payload codec, compression method, page count, and document digest are recoverable without trusting or repairing unrestricted ASCII. The human representations may be corrupted or clipped without changing machine interpretation.
H/T payload lines are capped at the same 60 safe characters used by codec data
frames. Footer page identity and the first 8 bytes of the SHA-256 of that page's
"\n"-joined data frames live inside the protected T payload. Pages may be
read out of order; a missing or integrity-invalid metadata frame fails loud.
COMMENT_PREFIX = '#!'
module-attribute
HEADER_PREFIX = '#!glyphive'
module-attribute
LAYOUT_VERSION = 1
module-attribute
PAGE_HASH_CHARS = 16
module-attribute
__all__ = ['HEADER_PREFIX', 'PAGE_HASH_CHARS', 'LayoutError', 'MissingPageError', 'Page', 'format_header', 'parse_header', 'format_page_footer', 'verify_page_footer', 'page_data_hash', 'paginate', 'iter_paginate', 'read_pages', 'read_pages_to_spool']
module-attribute
LayoutError
Bases: ValueError
Raised on a malformed header/footer or an unrecoverable page structure.
MissingPageError(missing, total)
Bases: LayoutError
Raised when the footers show a page number is absent from the transcript.
missing lists the 1-based page numbers that were not found.
Source code in src/glyphive/layout.py
129 130 131 132 133 134 135 | |
missing = list(missing)
instance-attribute
total = total
instance-attribute
Page
Bases: NamedTuple
One physical page.
Attributes
number:
1-based page number.
total:
Total page count of the document.
text_lines:
Every text line that goes on this physical page, in order: the document
header first (page 1 only), then the codec-framed lines, then the PAGE
footer last. This is what a renderer prints.
encoded_lines:
Just the raw codec-framed (L/P) lines this page carries — i.e.
text_lines without the header/footer. This is what feeds
:func:codec.decode.
encoded_lines
instance-attribute
number
instance-attribute
text_lines
instance-attribute
total
instance-attribute
format_header(meta)
Render the compact single-line, display-only document header from meta.
The line is display-only — restore reads authoritative metadata from the CRC-protected H frames, never from this summary — so it is kept minimal to waste as few OCR characters as possible:
``#!glyphive v<N> <codec>[,<comp>] files=<f> bytes=<b> pages=<p>[ pgpar=<k>]``
v is a bare positional token (v1); codec and compression collapse to
one positional codec[,comp] token (,comp omitted when compression is
none/absent). sha256 and meta are deliberately NOT emitted here
(they live in the protected header). pgpar is emitted only when non-zero.
meta must supply codec, files, bytes, pages (and v,
defaulted to :data:LAYOUT_VERSION). No value may contain whitespace. The
inverse is :func:parse_header.
Source code in src/glyphive/layout.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
format_page_footer(n, total, page_lines)
Render the per-page footer for page n of total.
page_lines are the codec-framed data/parity lines on this page (NOT the
header, NOT the footer). Grammar: PAGE <n>/<total> sha256=<first16hex>.
Source code in src/glyphive/layout.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
iter_paginate(encoded_lines, n_encoded, meta, *, lines_per_page, parity_pages=0, emit_human_header=True)
Yield pages without retaining the full encoded line or page lists.
parity_pages (K, default 0) requests K additional document-level
whole-page-recovery pages, emitted after the D data pages. Each data
page's printed lines ("\n".join(page.encoded_lines), the same
convention as :func:page_data_hash) form one RS "block", zero-padded to
a common size B (the max data-page block length); K parity blocks are
computed over the D data blocks (:mod:glyphive.codec.pagers) and printed
as K additional pages carrying Q-framed lines. Parity page numbers
continue past the data pages (D+1 .. D+K); footers' total becomes
D+K. The machine envelope's pages field always stores D (the data
page count); K and B are recorded separately as pgpar/
page_block_bytes. K=0 (the default) reproduces the pre-parity-pages
byte-for-byte output exactly: no Q frames, no behavior change.
Source code in src/glyphive/layout.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 | |
page_data_hash(page_lines)
Full hex SHA-256 of a page's data-line text block.
The block is "\n".join(page_lines) — the codec-framed lines carried by
the page, in printed order. The footer keeps only the first
:data:PAGE_HASH_CHARS characters of this digest.
Source code in src/glyphive/layout.py
584 585 586 587 588 589 590 591 592 | |
paginate(encoded_lines, meta, *, lines_per_page, parity_pages=0, emit_human_header=True)
Group encoded_lines into :class:Page objects with header/footer.
encoded_lines are the framed lines from :func:codec.encode (already
RS-interleaved — layout does NOT recompute FEC, it only chunks). meta is
the header dict (codec/comp/files/bytes/sha256 …); this
function fills in meta["pages"] with the final page count BEFORE the
header is formatted, so the printed pages= matches the physical count.
Chunking: each page's line budget is lines_per_page minus its overhead —
2 on page 1 (document header + footer) and 1 on every other page (footer).
The document header is the first text_line of page 1; the footer is the
last text_line of every page.
parity_pages (K) requests K additional whole-page-recovery pages after
the data pages — see :func:iter_paginate.
Returns the list of pages in order. Raises ValueError if
lines_per_page is too small to fit header+footer+data.
Source code in src/glyphive/layout.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | |
parse_header(line)
Parse the compact display-only header line (inverse of :func:format_header).
Grammar: #!glyphive v<N> <codec>[,<comp>] files=<f> bytes=<b> pages=<p>.
The first two non-prefix tokens are positional: a bare v<N> version and a
codec[,comp] token (comp defaults to none when absent). Remaining
tokens are k=v; integer keys (files/bytes/pages/pgpar) are
coerced to int. Tolerates extra unknown k=v tokens (forward-compat),
returned as strings. Raises :class:LayoutError if the #!glyphive prefix,
the positional version/codec tokens, or a required k=v key is missing, or
if an integer key is non-numeric. Restore never trusts this summary;
:func:read_pages uses only the protected H frames.
Source code in src/glyphive/layout.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
read_pages(all_text_lines)
Parse a full transcript back into (header_meta, encoded_lines).
all_text_lines is every text line of a scanned/typed document — pages may
be concatenated in any order and may repeat blank lines or OCR noise. This:
- Finds and parses the
#!glyphiveheader (raises if none is present). - Reads every
PAGE n/totalfooter, using them to detect a missing page (raises :class:MissingPageErrornaming the absent page numbers) and to verify each page's data-block hash. - Collects the codec-framed
L/Plines and returns them (in transcript order — codec.decode re-sorts by embedded index, so order does not matter).
Page-footer hash mismatches are advisory and collected separately in
meta["_footer_hash_notes"] (they fire on essentially every OCR restore,
because OCR-inserted spaces change the page-text hash while the L/P lines
still decode via CRC/RS). They do NOT raise. Genuine page-integrity issues
(reconstructed/missing pages) go in meta["_page_warnings"]. A missing
header raises, and a whole missing page raises only when it is unrecoverable
(beyond the page-parity budget and no surviving lines).
The returned meta is the parsed header dict plus:
meta["_page_warnings"]: real page-integrity warnings (missing/ reconstructed pages) — worth surfacing at WARNING.meta["_footer_hash_notes"]: advisory per-page footer-hash mismatches — expected on OCR input, surfaced quietly.meta["_pages_seen"]: sorted list of page numbers found.
Source code in src/glyphive/layout.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 | |
read_pages_to_spool(all_text_lines, sink, *, line_conf=None)
Parse a transcript once and spool normalized codec lines sequentially.
line_conf (plan 3, optional): raw per-character OCR confidence,
ONE ENTRY PER ELEMENT OF all_text_lines in the same order (None
for a line with no confidence -- e.g. plain-text input, or a shorter
line_conf than all_text_lines, padded with None). Only the
entries belonging to lines that survive into the L/P codec
stream matter; they are carried through page reordering/reconstruction
and returned, in the FINAL SPOOL'S OWN LINE ORDER, as
header_meta["_line_conf"] -- the shape :meth:Base16GCodec.decode_spool
expects. When line_conf is None (the default), this is a no-op
and behaves byte-identically to a build without this feature.
Source code in src/glyphive/layout.py
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 | |
verify_page_footer(footer_line, page_lines)
Return True iff footer_line's hash matches page_lines.
A structurally invalid footer line returns False. Comparison is
case-insensitive on the hex digest.
Source code in src/glyphive/layout.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |