Debian Codegraph @

texmf-dist/tex/luatex/cstypo/cstypo.lua

raw
1 --[[--
2 (c) 2016 Václav Haisman
3
4 This program can be redistributed and/or modified under the terms of the MIT
5 license. See LICENSE file.
6 --]]--
7
8 local GLYPH = node.id("glyph")
9 --print('GLYPH value: ', GLYPH)
10
11 local GLUE = node.id("glue")
12 --print('GLUE value: ', GLUE)
13
14 local CZECH_ID = cstypo_czech_language_id
15 --print('CZECH_ID value: ', CZECH_ID)
16
17 local function prevent_single_letter (head)
18 while head do
19 -- glyph
20 if (head.id == GLYPH
21 -- and in Czech or unspecified (???)
22 and (head.lang == nil
23 or head.lang == CZECH_ID)) then
24 -- only if we are at one letter word
25 if unicode.utf8.match(unicode.utf8.char(head.char), "[zZsSuUkKoOvViI]") then
26 -- and left of it is either a space
27 if ((head.prev.id == GLUE
28 -- or one of '{[('
29 or (head.prev.id == GLYPH
30 and unicode.utf8.match(unicode.utf8.char(head.prev.char),
31 "[%[%]()%{%}]")))
32 -- and right of the one letter word is also a space
33 and head.next.id == GLUE) then
34 -- then avoid line break between the single letter word and the
35 -- word following it
36 local p = node.new("penalty")
37 p.penalty = 10000
38 node.insert_after(head, head, p)
39 --print('inserting penalty at ', head)
40 end
41 end
42 end
43 head = head.next
44 end
45 return true
46 end
47
48 function cstypo_single_letter_enable ()
49 print('cstypo: Enabling single letter hook.')
50 luatexbase.add_to_callback("pre_linebreak_filter", prevent_single_letter,
51 "cstyposingleletter")
52 end
53
54 function cstypo_single_letter_disable ()
55 print('cstypo: Disabling single letter hook.')
56 luatexbase.remove_from_callback("pre_linebreak_filter", "cstyposingleletter")
57 end
58
59
60 local function prevent_a_letter (head)
61 while head do
62 -- glyph
63 if (head.id == GLYPH
64 -- and in Czech or unspecified (???)
65 and (head.lang == nil
66 or head.lang == CZECH_ID)) then
67 -- only if we are at one letter word
68 if unicode.utf8.match(unicode.utf8.char(head.char), "[aA]") then
69 -- and previous is space
70 if ((head.prev.id == GLUE
71 -- or previous is one of '{[('
72 or (head.prev.id == GLYPH
73 and unicode.utf8.match(unicode.utf8.char(head.prev.char),
74 "[%[%]()%{%}]")))
75 -- and right of the one letter word is also a space
76 and head.next.id == GLUE) then
77 -- then avoid line break between the single letter word and the
78 -- word following it
79 local p = node.new("penalty")
80 p.penalty = 10000
81 node.insert_after(head, head, p)
82 end
83 end
84 end
85 head = head.next
86 end
87 return true
88 end
89
90 function cstypo_a_letter_enable ()
91 print('cstypo: Enabling \'a\' letter hook.')
92 luatexbase.add_to_callback("pre_linebreak_filter", prevent_a_letter,
93 "cstypoaletter")
94 end
95
96 function cstypo_a_letter_disable ()
97 print('cstypo: Disabling \'a\' letter hook.')
98 luatexbase.remove_from_callback("pre_linebreak_filter", "cstypoaletter")
99 end
100
101
102 local function prevent_percents (head)
103 while head do
104 -- glyph
105 if (head.id == GLYPH
106 -- and in Czech or unspecified (???)
107 and (head.lang == nil
108 or head.lang == CZECH_ID)) then
109 -- only if we are at percentage sign
110 if unicode.utf8.match(unicode.utf8.char(head.char), "[%%‰°℃℉]") then
111 -- and left of it is a space
112 if (head.prev.id == GLUE
113 -- and left of the space is a digit.
114 and head.prev.prev.id == GLYPH
115 and unicode.utf8.match(unicode.utf8.char(head.prev.prev.char),
116 "[0-9]")) then
117 local p = node.new("penalty")
118 p.penalty = 10000
119 node.insert_after(head.prev.prev, head.prev.prev, p)
120 end
121 end
122 end
123 head = head.next
124 end
125 return true
126 end
127
128 function cstypo_percents_enable()
129 print('cstypo: Enabling percents hook.')
130 luatexbase.add_to_callback("pre_linebreak_filter", prevent_percents,
131 "cstypopercents")
132 end
133
134 function cstypo_percents_disable()
135 print('cstypo: Disabling percents hook.')
136 luatexbase.remove_from_callback("pre_linebreak_filter", "cstypopercents")
137 end
138
139
140 local function prevent_paragraph (head)
141 while head do
142 -- glyph
143 if (head.id == GLYPH
144 -- and in Czech or unspecified (???)
145 and (head.lang == nil
146 or head.lang == CZECH_ID)) then
147 -- only if we are at paragraph symbol
148 if unicode.utf8.match(unicode.utf8.char(head.char), "[§]") then
149 -- and right of it is a space
150 if (head.next.id == GLUE
151 and (head.next.next.id == GLYPH
152 and unicode.utf8.match(unicode.utf8.char(head.next.next.char),
153 "[0-9]"))) then
154 -- then avoid line break between the paragraph and the number
155 -- following it
156 local p = node.new("penalty")
157 p.penalty = 10000
158 node.insert_after(head, head, p)
159 end
160 end
161 end
162 head = head.next
163 end
164 return true
165 end
166
167 function cstypo_paragraph_enable()
168 print('cstypo: Enabling paragraph hook.')
169 luatexbase.add_to_callback("pre_linebreak_filter", prevent_paragraph,
170 "cstypoparagraph")
171 end
172
173 function cstypo_paragraph_disable()
174 print('cstypo: Disabling paragraph hook.')
175 luatexbase.remove_from_callback("pre_linebreak_filter", "cstypoparagraph")
176 end