1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 181 182 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
| % Randomized drawing of individual glyphs
% Author: István Szántai (szantaii)
% Original at: http://tex.stackexchange.com/a/197677/8844
\documentclass[10pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{luacode}
\usepackage{tikz}
\usetikzlibrary{svg.path, positioning}
\pagestyle{empty}
\tikzset{%
glyph node/.style={%
inner sep=0pt,%
outer sep=0pt%
},%
glyph outline/.style={%
line width=0pt%
}%
}
\begin{luacode*}
function read_font_data(file)
local glyphs = {}
local fd = io.open(file, "r")
local content = fd:read("*all")
fd.close()
for glyph in string.gmatch(content, "<glyph[^/>]*") do
local glyph_tag = string.gsub(glyph, "\n", " ")
local unicode = string.match(glyph_tag, "unicode="[^"]*")
local outline = string.match(glyph_tag, "d="[^"]*")
local width = string.match(glyph_tag, "horiz%-adv%-x="[^"]*")
if unicode ~= nil and #unicode >= 10 then
unicode = string.sub(unicode, 10, #unicode)
end
if outline ~= nil and #outline > 4 then
outline = string.sub(outline, 4, #outline)
end
if width ~= nil and #width >= 14 then
width = string.sub(width, 14, #width)
end
if unicode ~= nil then
glyphs[unicode] = {width, outline}
end
end
return glyphs
end
-- returns a random float number between the specified boundaries (floats)
function random_in_interval(lower_boundary, upper_boundary)
return ((math.random() * (upper_boundary - lower_boundary)) + lower_boundary)
end
-- note: scaling is applied before randomization
function scale_and_randomize(glyph, scale_factor, lower_boundary, upper_boundary)
local width = glyph[1]
local outline = glyph[2]
local previous_was_number = false
local processed_outline = ""
local number = ""
if width ~= nil then
width = width * scale_factor
end
if outline ~= nil then
for i = 1, #outline, 1 do
local char = string.sub(outline, i, i)
if previous_was_number then
if string.match(char, '%d') ~= nil or
char == "." then
number = number .. char
else
-- scale and randomize
number = number * scale_factor
number = number * random_in_interval(lower_boundary, upper_boundary)
number = string.format("%.3f", number)
processed_outline = processed_outline .. number .. char
number = ""
previous_was_number = false
end
else
if string.match(char, '%d') ~= nil or
char == "-" then
number = number .. char
previous_was_number = true
else
processed_outline = processed_outline .. char
previous_was_number = false
end
end
end
end
return {width, processed_outline}
end
function print_glyph(glyph, scale_factor, lower_boundary, upper_boundary)
local randomized_glyph = scale_and_randomize(glyph, scale_factor, lower_boundary, upper_boundary)
local width = randomized_glyph[1]
local outline = randomized_glyph[2]
if outline ~= nil then
tex.sprint("\\filldraw[glyph outline] svg "" .. outline .. "";")
end
end
function return_glyph(glyph, scale_factor, lower_boundary, upper_boundary)
local randomized_glyph = scale_and_randomize(glyph, scale_factor, lower_boundary, upper_boundary)
local width = randomized_glyph[1]
local outline = randomized_glyph[2]
if outline ~= nil then
return "\\filldraw[glyph outline] svg "" .. outline .. "";"
else
return ""
end
end
function draw_sample_glyphs(glyphs)
tex.sprint("\\begin{tikzpicture}")
tex.sprint("\\node[glyph node, matrix, anchor=south west] (a1) {" ..
return_glyph(glyphs["a"], 0.05, 1, 1) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, anchor=south west, right=7.5mm of a1] (a2) {" ..
return_glyph(glyphs["a"], 0.05, 0.8, 1.2) ..
"\\\\};")
tex.sprint("\\end{tikzpicture}")
end
function draw_sample_text(glyphs)
local horizontal_space = "0.5mm"
local vertical_space = "1.25mm"
local scale = 0.05
local lower_boundary = 0.9
local upper_boundary = 1.1
tex.sprint("\\begin{tikzpicture}")
tex.sprint("\\node[glyph node, matrix] (m1) {" ..
return_glyph(glyphs["m"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of m1] (a1) {" ..
return_glyph(glyphs["a"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of a1] (t1) {" .. "\\raisebox{" .. vertical_space .. "}{" ..
return_glyph(glyphs["t"], scale, lower_boundary, upper_boundary) ..
"}" .. "\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of t1] (h1) {" .. "\\raisebox{" .. vertical_space .. "}{" ..
return_glyph(glyphs["h"], scale, lower_boundary, upper_boundary) ..
"}" .. "\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of h1] (e1) {" ..
return_glyph(glyphs["e"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of e1] (m2) {" ..
return_glyph(glyphs["m"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of m2] (a2) {" ..
return_glyph(glyphs["a"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of a2] (t2) {" .. "\\raisebox{" .. vertical_space .. "}{" ..
return_glyph(glyphs["t"], scale, lower_boundary, upper_boundary) ..
"}" .. "\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of t2] (i1) {" .. "\\raisebox{" .. vertical_space .. "}{" ..
return_glyph(glyphs["i"], scale, lower_boundary, upper_boundary) ..
"}" .. "\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of i1] (c1) {" ..
return_glyph(glyphs["c"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\node[glyph node, matrix, right=" .. horizontal_space ..
" of c1] (s1) {" ..
return_glyph(glyphs["s"], scale, lower_boundary, upper_boundary) ..
"\\\\};")
tex.sprint("\\end{tikzpicture}")
end
function draw_sample_glyph(glyphs)
tex.sprint("\\begin{tikzpicture}")
print_glyph(glyphs["Ω"], 0.05, 0.95, 1.05)
tex.sprint("\\end{tikzpicture}")
end
function main()
local cmr10_glyphs = {}
math.randomseed(os.time())
cmr10_glyphs = read_font_data("cmr10.svg")
tex.sprint("\\noindent")
draw_sample_glyphs(cmr10_glyphs)
tex.sprint("\\\\[2cm]")
draw_sample_text(cmr10_glyphs)
tex.sprint("\\\\[2cm]")
draw_sample_glyph(cmr10_glyphs)
end
\end{luacode*}
\begin{document}
\luadirect{main()}
\end{document} |