luatex-fonts-demo-tt.lua /size: 5399 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luatex-fonts-demo-tt'] = {
2    version   = 1.001,
3    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
4    copyright = "PRAGMA ADE / ConTeXt Development Team",
5    license   = "see context related readme files",
6}
7
8-- Someone asked on the list if we could fake bad typewriters. Actually there are
9-- already enough examples in successive articles and it's not too complex to mess
10-- with fonts. There is a nicer way to do this (with a bit of metapost) but I have
11-- no time now. After all, features like this are never used in practice, so it's a
12-- waste of time to code them.
13--
14-- Todo: force emwidth/5 for fonts > 1 ... only when requested.
15--
16-- \starttext
17--
18-- \raggedright
19--
20-- \definefontfeature[badtypewritera][ttgrayness=.5]
21-- \definefontfeature[badtypewriterb][ttshift=.5]
22-- \definefontfeature[badtypewriterc][ttthickness=2]
23-- \definefontfeature[badtypewriterd][ttgrayness=.5,ttshift=.5,ttthickness=2,ttfont={dejavusansmono}]
24-- \definefontfeature[badtypewritere][ttgrayness=.5,ttthickness=2,ttstep=2,ttmax=1.5]
25--
26-- \definefont[MyFontA][file:luatex-fonts-demo-tt.lua*badtypewritera]
27-- \definefont[MyFontB][file:luatex-fonts-demo-tt.lua*badtypewriterb]
28-- \definefont[MyFontC][file:luatex-fonts-demo-tt.lua*badtypewriterc]
29-- \definefont[MyFontD][file:luatex-fonts-demo-tt.lua*badtypewriterd @ 10pt]
30-- \definefont[MyFontE][file:luatex-fonts-demo-tt.lua*badtypewritere @ 10pt]
31--
32-- \MyFontA \input tufte \blank {\righttoleft  لَيْسَ لَدَيَّ أَيُّ فِكْرَةٍ عَمَّا يَعْنِيهِ هٰذَا.} \page
33-- \MyFontB \input tufte \blank {\righttoleft  لَيْسَ لَدَيَّ أَيُّ فِكْرَةٍ عَمَّا يَعْنِيهِ هٰذَا.} \page
34-- \MyFontC \input tufte \blank {\righttoleft  لَيْسَ لَدَيَّ أَيُّ فِكْرَةٍ عَمَّا يَعْنِيهِ هٰذَا.} \page
35-- \MyFontD \input tufte \blank {\righttoleft  لَيْسَ لَدَيَّ أَيُّ فِكْرَةٍ عَمَّا يَعْنِيهِ هٰذَا.} \page
36-- \MyFontE \input tufte \blank {\righttoleft  لَيْسَ لَدَيَّ أَيُّ فِكْرَةٍ عَمَّا يَعْنِيهِ هٰذَا.} \page
37--
38-- \stoptext
39
40local random, sin = math.random, math.sin
41local formatters = string.formatters
42
43local now  = 0
44local max  = 2 * math.pi
45local step = max/20
46
47-- The sin trick is first shown by Hartmut in 2005 when we had a few more plugs into
48-- the backend. His demo was a rather colorful sheet that looked like it was crumpled.
49-- There were no virtual fonts involved the, just pdf.print hooked into an always
50-- applied Lua function.
51
52function fonts.helpers.FuzzyFontStart(exheight,ttgrayness,ttthickness,ttshift,ttstep,ttmax)
53    local grayness  = ttgrayness  * random(0,5)/10
54    local thickness = ttthickness * random(1,2)/10
55    local shift     = 0
56    if ttstep > 0 then
57        if now > max then
58            now = 0
59        else
60            now = now + step * ttstep
61        end
62        shift = ttmax * sin(now) * exheight/5
63    else
64        shift = ttshift * random(-1,1) * exheight/20
65    end
66    -- We can optimize for one of them being zero or the default but no one will
67    -- use this in production so efficiency hardly matters.
68    local template = formatters["pdf:page:q %0.2F g %.2F G %0.2F w 2 Tr %.3F Ts"](
69        grayness, grayness, thickness, shift
70    )
71    vf.special(template)
72    -- will be:
73 -- local template = formatters["q %0.2F g %.2F G %0.2F w 2 Tr %.3F Ts"](
74 --     grayness, grayness, thickness, shift
75 -- )
76 -- vf.pdf("page",template)
77end
78
79function fonts.helpers.FuzzyFontStop()
80    vf.special("pdf:page:Q")
81    -- will be:
82 -- vf.pdf("page","Q")
83end
84
85return function(specification)
86    local features = specification.features.normal
87    local list = features.ttfont
88    if list then
89        list = utilities.parsers.settings_to_array(list)
90    else
91        list = {
92            'lmtypewriter10-regular',
93            'almfixed',
94        }
95    end
96    local f  = { }
97    local id = { }
98    for i=1,#list do
99        f[i], id[i] = fonts.constructors.readanddefine(list[i],specification.size)
100    end
101    local f1 = f[1]
102    if f1 then
103        f1.name = specification.name -- needs checking (second time used an error)
104        f1.properties.name = specification.name
105        f1.properties.virtualized = true
106        f1.fonts = { }
107        local target = f1.characters
108        local exbp = f1.parameters.exheight * number.dimenfactors.bp
109        local stop = {
110            "lua",
111            "fonts.helpers.FuzzyFontStop()",
112        }
113        local start = {
114            "lua",
115            formatters["fonts.helpers.FuzzyFontStart(%.3F,%.2F,%.2F,%.2F,%.2F,%.2F)"](
116                exbp,
117                tonumber(features.ttgrayness)  or 1,
118                tonumber(features.ttthickness) or 1,
119                tonumber(features.ttshift)     or 1,
120                tonumber(features.ttstep)      or 0,
121                tonumber(features.ttmax)       or 1
122            ),
123        }
124        for i=1,#list do
125            f1.fonts[i] = { id = id[i] }
126            local characters = f[i].characters
127            for u, v in next, characters do
128                v.commands = { start, { "slot", i, u }, stop }
129                if characters ~= target then
130                    target[u] = v
131                end
132            end
133        end
134    end
135    return f1
136end
137