| 1 |
-- parstat.opm |
| 2 |
-- Copyright 2025 Petr Krajník |
| 3 |
-- |
| 4 |
-- This work may be distributed and/or modified under the |
| 5 |
-- conditions of the LaTeX Project Public License, either version 1.3 |
| 6 |
-- of this license or (at your option) any later version. |
| 7 |
-- The latest version of this license is in |
| 8 |
-- https://www.latex-project.org/lppl.txt |
| 9 |
-- and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
-- version 2008 or later. |
| 11 |
-- |
| 12 |
-- This work has the LPPL maintenance status 'author-maintained'. |
| 13 |
-- |
| 14 |
-- The Current Maintainer of this work is Petr Krajník. |
| 15 |
-- |
| 16 |
-- This work consists of the files parstat.opm and parstat.lua. |
| 17 |
-- |
| 18 |
|
| 19 |
-- Node types |
| 20 |
local NODE_HLIST = node.id("hlist") |
| 21 |
local NODE_VLIST = node.id("vlist") |
| 22 |
local NODE_GLYPH = node.id("glyph") |
| 23 |
local NODE_GLUE = node.id("glue") |
| 24 |
|
| 25 |
-- Node subtypes |
| 26 |
local GLYPH_LIGATURE_FLAG = 2 |
| 27 |
local GLYPH_GHOST_FLAG = 4 |
| 28 |
local GLUE_SPACE = 13 |
| 29 |
local GLUE_XSPACE = 14 |
| 30 |
|
| 31 |
-- Line statistic outputs per console line |
| 32 |
local STATS_PER_LINE = 5 |
| 33 |
|
| 34 |
-- Statistic structure factory |
| 35 |
local function stat_new(name) |
| 36 |
return { |
| 37 |
name = name, |
| 38 |
count = 0, |
| 39 |
average = 0, |
| 40 |
M2 = 0, -- Sum of squared diffs for stddev |
| 41 |
min = math.huge, |
| 42 |
max = 0 } |
| 43 |
end |
| 44 |
|
| 45 |
local function make_stats() |
| 46 |
return stat_new("Glyphs"), stat_new("Spaces") |
| 47 |
end |
| 48 |
|
| 49 |
-- Total statistics |
| 50 |
local glyph_stat_total, space_stat_total = make_stats() |
| 51 |
local par_count = 0 |
| 52 |
|
| 53 |
-- Line skips (only valid when processing) |
| 54 |
local front_skip, back_skip |
| 55 |
|
| 56 |
local function init_line_skips() |
| 57 |
front_skip = tex.count["_parstat_fskip"] |
| 58 |
assert(front_skip >= 0, "negative front line skip") |
| 59 |
back_skip = tex.count["_parstat_bskip"] |
| 60 |
assert(back_skip >= 0, "negative back line skip") |
| 61 |
end |
| 62 |
|
| 63 |
local function enough_lines(line_count) |
| 64 |
return (front_skip + back_skip) < line_count |
| 65 |
end |
| 66 |
|
| 67 |
local function is_line_ignored(line, line_count) |
| 68 |
return line <= front_skip or line > (line_count - back_skip) |
| 69 |
end |
| 70 |
|
| 71 |
local function print_header() |
| 72 |
texio.write_nl(string.format( |
| 73 |
"Parstat %d in '%s' at line %d", |
| 74 |
par_count, status.filename, status.linenumber)) |
| 75 |
end |
| 76 |
|
| 77 |
local function is_real_glyph(n) |
| 78 |
return n.id == NODE_GLYPH and (n.subtype & GLYPH_GHOST_FLAG) == 0 |
| 79 |
end |
| 80 |
|
| 81 |
local function is_ligature(n) |
| 82 |
return (n.subtype & GLYPH_LIGATURE_FLAG) ~= 0 |
| 83 |
end |
| 84 |
|
| 85 |
local function is_space(n) |
| 86 |
return n.id == NODE_GLUE and (n.subtype == GLUE_SPACE |
| 87 |
or n.subtype == GLUE_XSPACE) |
| 88 |
end |
| 89 |
|
| 90 |
local function is_nested_node(n) |
| 91 |
return n.id == NODE_HLIST or n.id == NODE_VLIST |
| 92 |
end |
| 93 |
|
| 94 |
local function count_glyphs(head) |
| 95 |
local glyph_count = 0 |
| 96 |
local space_count = 0 |
| 97 |
|
| 98 |
for n in node.traverse(head) do |
| 99 |
if is_real_glyph(n) then |
| 100 |
if is_ligature(n) and n.components then |
| 101 |
-- Count Ligature components (see limitations) |
| 102 |
local gc, sc = count_glyphs(n.components) |
| 103 |
glyph_count = glyph_count + gc |
| 104 |
space_count = space_count + sc |
| 105 |
else |
| 106 |
glyph_count = glyph_count + 1 |
| 107 |
end |
| 108 |
elseif is_space(n) then |
| 109 |
glyph_count = glyph_count + 1 |
| 110 |
space_count = space_count + 1 |
| 111 |
elseif is_nested_node(n) then |
| 112 |
local gc, sc = count_glyphs(n.head) |
| 113 |
glyph_count = glyph_count + gc |
| 114 |
space_count = space_count + sc |
| 115 |
end |
| 116 |
end |
| 117 |
return glyph_count, space_count |
| 118 |
end |
| 119 |
|
| 120 |
local function get_ignore_mark(line_ignored) |
| 121 |
if line_ignored then |
| 122 |
return "X" -- Ignored lines are marked |
| 123 |
end |
| 124 |
return "" |
| 125 |
end |
| 126 |
|
| 127 |
local function print_line_stat(line, line_ignored, glyph_count, space_count) |
| 128 |
if (line % STATS_PER_LINE) == 1 then |
| 129 |
texio.write_nl(" ") -- Limit line stats per line |
| 130 |
end |
| 131 |
texio.write(string.format(" %d%s:(%dg,%ds)", line, |
| 132 |
get_ignore_mark(line_ignored), glyph_count, space_count)) |
| 133 |
end |
| 134 |
|
| 135 |
local function stat_add(stat, value) |
| 136 |
stat.count = stat.count + 1 |
| 137 |
|
| 138 |
-- Average and stddev (Welford’s Algorithm) |
| 139 |
local delta_old = value - stat.average |
| 140 |
stat.average = stat.average + delta_old / stat.count |
| 141 |
local delta_new = value - stat.average |
| 142 |
stat.M2 = stat.M2 + delta_old * delta_new |
| 143 |
|
| 144 |
stat.min = math.min(stat.min, value) |
| 145 |
stat.max = math.max(stat.max, value) |
| 146 |
end |
| 147 |
|
| 148 |
local function stat_stddev(stat) |
| 149 |
if stat.count > 1 then |
| 150 |
return math.sqrt(stat.M2 / (stat.count - 1)) |
| 151 |
end |
| 152 |
return 0.0 |
| 153 |
end |
| 154 |
|
| 155 |
local function stat_min(stat) |
| 156 |
if stat.min == math.huge then |
| 157 |
return 0 |
| 158 |
end |
| 159 |
return stat.min |
| 160 |
end |
| 161 |
|
| 162 |
local function stat_print(stat) |
| 163 |
texio.write_nl(string.format( |
| 164 |
" %s: ave %.2f, stddev %.2f, min %d, max %d", stat.name, |
| 165 |
stat.average, stat_stddev(stat), stat_min(stat), stat.max)) |
| 166 |
end |
| 167 |
|
| 168 |
local function print_par_stat(head, line_count) |
| 169 |
local glyph_stat, space_stat = make_stats() |
| 170 |
local curr_line = 0 |
| 171 |
|
| 172 |
for line in node.traverse_id(NODE_HLIST, head) do |
| 173 |
curr_line = curr_line + 1 |
| 174 |
local line_ignored = is_line_ignored(curr_line, line_count) |
| 175 |
|
| 176 |
-- Single line stat |
| 177 |
local glyph_count, space_count = count_glyphs(line.head) |
| 178 |
print_line_stat(curr_line, line_ignored, glyph_count, space_count) |
| 179 |
|
| 180 |
-- Calculate par stats |
| 181 |
if not line_ignored then |
| 182 |
stat_add(glyph_stat, glyph_count) |
| 183 |
stat_add(space_stat, space_count) |
| 184 |
stat_add(glyph_stat_total, glyph_count) |
| 185 |
stat_add(space_stat_total, space_count) |
| 186 |
end |
| 187 |
end |
| 188 |
assert(curr_line == line_count) |
| 189 |
|
| 190 |
stat_print(glyph_stat) |
| 191 |
stat_print(space_stat) |
| 192 |
texio.write_nl("") |
| 193 |
end |
| 194 |
|
| 195 |
-------------------------------- |
| 196 |
|
| 197 |
local parstat = _ENV.parstat or {} |
| 198 |
_ENV.parstat = parstat |
| 199 |
|
| 200 |
function parstat.run(head) |
| 201 |
if tex.count["_parstat_enabled"] > 0 then |
| 202 |
init_line_skips() |
| 203 |
local line_count = node.count(NODE_HLIST, head) |
| 204 |
|
| 205 |
if enough_lines(line_count) then |
| 206 |
par_count = par_count + 1 |
| 207 |
print_header() |
| 208 |
print_par_stat(head, line_count) |
| 209 |
end |
| 210 |
end |
| 211 |
return head |
| 212 |
end |
| 213 |
|
| 214 |
function parstat.print_summary() |
| 215 |
texio.write_nl(string.format( |
| 216 |
"Parstat summary from '%s' at line %d", |
| 217 |
status.filename, status.linenumber)) |
| 218 |
texio.write_nl(string.format( |
| 219 |
" Analyzed %d paragraphs, %d lines in total", |
| 220 |
par_count, glyph_stat_total.count)) |
| 221 |
stat_print(glyph_stat_total) |
| 222 |
stat_print(space_stat_total) |
| 223 |
texio.write_nl("") |
| 224 |
end |
| 225 |
|
| 226 |
function parstat.reset() |
| 227 |
glyph_stat_total, space_stat_total = make_stats() |
| 228 |
par_count = 0 |
| 229 |
end |