summaryrefslogtreecommitdiff
path: root/tex/context/base/typo-itc.lua
blob: 3220853461baa8faa98fd2bf19305b1a876e49f9 (plain)
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
if not modules then modules = { } end modules ['typo-itc'] = {
    version   = 1.001,
    comment   = "companion to typo-itc.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local utfchar = utf.char

local trace_italics      = false  trackers.register("typesetters.italics", function(v) trace_italics = v end)

local report_italics     = logs.reporter("nodes","italics")

typesetters.italics      = typesetters.italics or { }
local italics            = typesetters.italics

local nodecodes          = nodes.nodecodes
local glyph_code         = nodecodes.glyph
local kern_code          = nodecodes.kern
local glue_code          = nodecodes.glue

local tasks              = nodes.tasks

local insert_node_after  = node.insert_after
local delete_node        = nodes.delete
local has_attribute      = node.has_attribute

local texattribute       = tex.attribute
local a_italics          = attributes.private("italics")
local unsetvalue         = attributes.unsetvalue

----- new_correction     = nodes.pool.fontkern
----- new_correction     = nodes.pool.fontkern
local new_correction     = nodes.pool.glue

local points             = number.points

local fonthashes         = fonts.hashes
local fontdata           = fonthashes.identifiers
local chardata           = fonthashes.characters
local italicsdata        = fonthashes.italics

local forcedvariant      = false

function typesetters.italics.forcevariant(variant)
    forcedvariant = variant
end

-- we could delay the calculations in the font scaler to here:
--
-- local description = descdata[char]
-- local vi = description.italic or (description.boundingbox[3] - description.width + properties[font].auto_italic_correction)
-- if vi and vi ~= 0 then
--     italic = vi*parameters[font].hfactor
-- end
--
-- this saves us quite entries in the characters table

local function process(namespace,attribute,head)
    local done     = false
    local italic   = 0
    local lastfont = nil
    local lastattr = nil
    local previous = nil
    local prevchar = nil
    local current  = head
    local inserted = nil
    while current do
        local id = current.id
        if id == glyph_code then
            local font = current.font
            local char = current.char
            local data = italicsdata[font]
            if font ~= lastfont then
                if italic ~= 0 then
                    if data then
                        if trace_italics then
                            report_italics("ignoring %s between italic %s and italic %s",points(italic),utfchar(prevchar),utfchar(char))
                        end
                    else
                        if trace_italics then
                            report_italics("inserting %s between italic %s and regular %s",points(italic),utfchar(prevchar),utfchar(char))
                        end
                        insert_node_after(head,previous,new_correction(italic),new_correction(italic))
                        done = true
                    end
                elseif inserted and data then
                    if trace_italics then
                        report_italics("deleting last correction before %s",utfchar(char))
                    end
                    delete_node(head,inserted)
                end
            end
            if data then
                local attr = forcedvariant or has_attribute(current,attribute)
                if attr and attr > 0 then
                    local cd = data[char]
                    if not cd then
                        -- this really can happen
                        italic = 0
                    else
                        italic = cd.italic or cd.italic_correction
                        if not italic then
                            italic = 0
                        elseif italic ~= 0 then
                            lastfont = font
                            lastattr = attr
                            previous = current
                            prevchar = char
                        end
                    end
                else
                    italic = 0
                end
            else
                italic = 0
            end
            inserted = nil
        elseif id == kern_code then
            inserted = nil
            italic = 0
        elseif id == glue_code then
            if italic ~= 0 then
                if trace_italics then
                    report_italics("inserting %s between italic %s and glue",points(italic),utfchar(prevchar))
                end
                inserted = new_correction(italic)
                insert_node_after(head,previous,inserted)
                italic = 0
                done = true
            end
        elseif italic ~= 0 then
            if trace_italics then
                report_italics("inserting %s between italic %s and whatever",points(italic),utfchar(prevchar))
            end
            inserted = nil
            insert_node_after(head,previous,new_correction(italic),new_correction(italic))
            italic = 0
            done = true
        end
        current = current.next
    end
    if italic ~= 0 and lastattr > 1 then -- more control is needed here
        if trace_italics then
            report_italics("inserting %s between italic %s and end of list",points(italic),utfchar(prevchar))
        end
        insert_node_after(head,previous,new_correction(italic),new_correction(italic))
        done = true
    end
    return head, done
end

local enable

enable = function()
    tasks.enableaction("processors","typesetters.italics.handler")
    if trace_italics then
        report_italics("enabling italics")
    end
    enable = false
end

function italics.set(n)
    if enable then
        enable()
    end
    texattribute[a_italics] = n
end

function italics.reset()
    texattribute[a_italics] = unsetvalue
end

italics.handler = nodes.installattributehandler {
    name      = "italics",
    namespace = italics,
    processor = process,
}

local variables        = interfaces.variables
local settings_to_hash = utilities.parsers.settings_to_hash

function commands.setupitaliccorrection(option) -- no grouping !
    if enable then
        enable()
    end
    local options = settings_to_hash(option)
    local variant = unsetvalue
    if options[variables.text] then
        variant = 1
    elseif options[variables.always] then
        variant = 2
    end
    if options[variables.global] then
        forcevariant = variant
        texattribute[a_italics] = unsetvalue
    else
        forcevariant = false
        texattribute[a_italics] = variant
    end
    if trace_italics then
        report_italics("force: %s, variant: %s",tostring(forcevariant),tostring(variant ~= unsetvalue and variant))
    end
end

-- for manuals:

local stack = { }

function commands.pushitaliccorrection()
    table.insert(stack,{forcevariant, texattribute[a_italics] })
end

function commands.popitaliccorrection()
    local top = table.remove(stack)
    forcevariant = top[1]
    texattribute[a_italics] = top[2]
end