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

-- We need to share information between the TeX and Lua end about the typographical
-- model. This happens here. This code might move.

local texgetcount  = tex.getcount
local conditionals = tex.conditionals

local c_realpageno  = tex.iscount("realpageno")
local c_pagenoshift = tex.iscount("pagenoshift")

layouts = {
    status = { },
}

local status = layouts.status

function status.leftorrightpageaction(left,right)
    if left == nil then
        left, right = false, true
    end
    if not conditionals.layoutisdoublesided then
        return left, right
    elseif conditionals.layoutissinglesided then
        return left, right
    elseif texgetcount(c_pagenoshift) % 2 == 0 then
        if texgetcount(c_realpageno) % 2 == 0 then
            return right, left
        else
            return left, right
        end
    else
        if texgetcount(c_realpageno) % 2 == 0 then
            return left, right
        else
            return right, left
        end
    end
end

function status.isleftpage(r)
    if not conditionals.layoutisdoublesided then
        return false
    elseif conditionals.layoutissinglesided then
        return false
    elseif texgetcount(c_pagenoshift) % 2 == 0 then
        return (r or texgetcount(c_realpageno)) % 2 == 0
    else
        return not (r or texgetcount(c_realpageno)) % 2 == 0
    end
end

-- Instead of making these these driver specific we make them generic. We can even consider
-- to make these registers at the tex end.

local canvas = {
    pagespec     = "default", -- v_default
    paperwidth   = 0,
    paperheight  = 0,
    topoffset    = 0,
    leftoffset   = 0,
    height       = 0,
    width        = 0,
    cropoffset   = 0,
    bleedoffset  = 0,
    trimoffset   = 0,
    artoffset    = 0,
    doublesided  = false,
    marked       = false,
    copies       = false,
}

function layouts.setupcanvas(specification)
    local paperheight = specification.paperheight or canvas.paperheight
    local paperwidth  = specification.paperwidth  or canvas.paperwidth
    local cropoffset  = specification.cropoffset  or 0
    local trimoffset  = cropoffset  - (specification.trimoffset  or 0)
    local bleedoffset = trimoffset  - (specification.bleedoffset or 0)
    local artoffset   = bleedoffset - (specification.artoffset   or 0)
    --
    canvas.paperheight = paperheight
    canvas.paperwidth  = paperwidth
    canvas.cropoffset  = cropoffset
    canvas.trimoffset  = trimoffset
    canvas.bleedoffset = bleedoffset
    canvas.artoffset   = artoffset
    --
    canvas.pagespec    = specification.mode       or pagespec
    canvas.topoffset   = specification.topoffset  or 0
    canvas.leftoffset  = specification.leftoffset or 0
    canvas.height      = specification.height     or paperheight
    canvas.width       = specification.width      or paperwidth
    canvas.marked      = specification.print
    --
    local copies = specification.copies
    if type(copies) == "number" then
        if copies < 2 then
            canvas.copies = false
        else
            canvas.copies = copies
        end
    end
    --
    local doublesided = specification.doublesided
    if doublesided ~= nil then
        canvas.doublesided = doublesided
    end
end

function layouts.getpagedimensions()
    return canvas.paperwidth, canvas.paperheight
end

function layouts.getcanvas()
    return canvas
end