mlib-int.lmt /size: 10 Kb    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['mlib-int'] = {
2    version   = 1.001,
3    comment   = "companion to mlib-ctx.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9local factor         = number.dimenfactors.bp
10local mpstring       = mp.string
11local mpboolean      = mp.boolean
12local getdimen       = tex.getdimen
13local getcount       = tex.getcount
14local getmacro       = tokens.getters.macro
15local get            = tex.get
16local emwidths       = fonts.hashes.emwidths
17local exheights      = fonts.hashes.exheights
18
19local registerdirect = metapost.registerdirect
20
21local on_right_page      = structures.pages.on_right
22local is_odd_page        = structures.pages.is_odd
23local in_body_page       = structures.pages.in_body
24local page_fraction      = structures.pages.fraction
25local layout_has_changed = structures.pages.has_changed
26
27local function defaultcolormodel() -- can be helper
28    local colormethod = getcount("MPcolormethod")
29    return (colormethod == 0 or colormethod == 1) and 1 or 3
30end
31
32do
33
34    local t = os.date("*t") -- maybe this should be a very early on global
35
36    -- If we want to do a vardef then we first need to catch an endgroup and
37    -- that then fails because we have a variable sitting there, so they need
38    -- to be def's at the mp end.
39
40    local assignment_code = metapost.codes.assignment
41
42    local mpscannext      = mp.scan.next
43    local mpscaninteger   = mp.scan.integer
44
45    local function item(name)
46        local n = mpscannext(true) -- keep
47        if n == assignment_code then
48            mpscannext()
49            t[name] = mpscaninteger()
50        else
51            return t[name]
52        end
53    end
54
55    registerdirect("year",   function() return item("year")  end)
56    registerdirect("month",  function() return item("month") end)
57    registerdirect("day",    function() return item("day")   end)
58    registerdirect("hour",   function() return item("hour")  end)
59    registerdirect("minute", function() return item("min")   end)
60    registerdirect("second", function() return item("sec")   end)
61
62end
63
64local injectnumeric = mp.inject.numeric
65
66registerdirect("PaperHeight",          function() return getdimen("paperheight")          * factor end)
67registerdirect("PaperWidth",           function() return getdimen("paperwidth")           * factor end)
68registerdirect("PrintPaperHeight",     function() return getdimen("printpaperheight")     * factor end)
69registerdirect("PrintPaperWidth",      function() return getdimen("printpaperwidth")      * factor end)
70registerdirect("TopSpace",             function() return getdimen("topspace")             * factor end)
71registerdirect("BottomSpace",          function() return getdimen("bottomspace")          * factor end)
72registerdirect("BackSpace",            function() return getdimen("backspace")            * factor end)
73registerdirect("CutSpace",             function() return getdimen("cutspace")             * factor end)
74registerdirect("MakeupHeight",         function() return getdimen("makeupheight")         * factor end)
75registerdirect("MakeupWidth",          function() return getdimen("makeupwidth")          * factor end)
76registerdirect("TopHeight",            function() return getdimen("topheight")            * factor end)
77registerdirect("TopDistance",          function() return getdimen("topdistance")          * factor end)
78registerdirect("HeaderHeight",         function() return getdimen("headerheight")         * factor end)
79registerdirect("HeaderDistance",       function() return getdimen("headerdistance")       * factor end)
80registerdirect("TextHeight",           function() return getdimen("textheight")           * factor end)
81registerdirect("FooterDistance",       function() return getdimen("footerdistance")       * factor end)
82registerdirect("FooterHeight",         function() return getdimen("footerheight")         * factor end)
83registerdirect("BottomDistance",       function() return getdimen("bottomdistance")       * factor end)
84registerdirect("BottomHeight",         function() return getdimen("bottomheight")         * factor end)
85registerdirect("LeftEdgeWidth",        function() return getdimen("leftedgewidth")        * factor end)
86registerdirect("LeftEdgeDistance",     function() return getdimen("leftedgedistance")     * factor end)
87registerdirect("LeftMarginWidth",      function() return getdimen("leftmarginwidth")      * factor end)
88registerdirect("LeftMarginDistance",   function() return getdimen("leftmargindistance")   * factor end)
89registerdirect("TextWidth",            function() return getdimen("textwidth")            * factor end)
90registerdirect("RightMarginDistance",  function() return getdimen("rightmargindistance")  * factor end)
91registerdirect("RightMarginWidth",     function() return getdimen("rightmarginwidth")     * factor end)
92registerdirect("RightEdgeDistance",    function() return getdimen("rightedgedistance")    * factor end)
93registerdirect("RightEdgeWidth",       function() return getdimen("rightedgewidth")       * factor end)
94registerdirect("InnerMarginDistance",  function() return getdimen("innermargindistance")  * factor end)
95registerdirect("InnerMarginWidth",     function() return getdimen("innermarginwidth")     * factor end)
96registerdirect("OuterMarginDistance",  function() return getdimen("outermargindistance")  * factor end)
97registerdirect("OuterMarginWidth",     function() return getdimen("outermarginwidth")     * factor end)
98registerdirect("InnerEdgeDistance",    function() return getdimen("inneredgedistance")    * factor end)
99registerdirect("InnerEdgeWidth",       function() return getdimen("inneredgewidth")       * factor end)
100registerdirect("OuterEdgeDistance",    function() return getdimen("outeredgedistance")    * factor end)
101registerdirect("OuterEdgeWidth",       function() return getdimen("outeredgewidth")       * factor end)
102registerdirect("PageOffset",           function() return getdimen("pagebackgroundoffset") * factor end)
103registerdirect("PageDepth",            function() return getdimen("pagebackgrounddepth")  * factor end)
104registerdirect("LayoutColumns",        function() return getcount("layoutcolumns")                 end)
105registerdirect("LayoutColumnDistance", function() return getdimen("layoutcolumndistance") * factor end)
106registerdirect("LayoutColumnWidth",    function() return getdimen("layoutcolumnwidth")    * factor end)
107registerdirect("SpineWidth",           function() return getdimen("spinewidth")           * factor end)
108registerdirect("PaperBleed",           function() return getdimen("paperbleed")           * factor end)
109
110registerdirect("RealPageNumber",       function() return getcount("realpageno")                    end)
111registerdirect("LastPageNumber",       function() return getcount("lastpageno")                    end)
112
113registerdirect("PageNumber",           function() return getcount("pageno")                        end)
114registerdirect("NOfPages",             function() return getcount("lastpageno")                    end)
115
116registerdirect("SubPageNumber",        function() return getcount("subpageno")                     end)
117registerdirect("NOfSubPages",          function() return getcount("lastsubpageno")                 end)
118
119registerdirect("CurrentColumn",        function() return getcount("mofcolumns")                    end)
120registerdirect("NOfColumns",           function() return getcount("nofcolumns")                    end)
121
122registerdirect("BaseLineSkip",         function() return get     ("baselineskip",true)    * factor end)
123registerdirect("LineHeight",           function() return getdimen("lineheight")           * factor end)
124registerdirect("BodyFontSize",         function() return getdimen("bodyfontsize")         * factor end)
125
126registerdirect("TopSkip",              function() return get     ("topskip",true)         * factor end)
127registerdirect("StrutHeight",          function() return getdimen("strutht")              * factor end)
128registerdirect("StrutDepth",           function() return getdimen("strutdp")              * factor end)
129
130registerdirect("PageNumber",           function() return getcount("pageno")                        end)
131registerdirect("RealPageNumber",       function() return getcount("realpageno")                    end)
132registerdirect("NOfPages",             function() return getcount("lastpageno")                    end)
133
134registerdirect("CurrentWidth",         function() return get     ("hsize")                * factor end)
135registerdirect("CurrentHeight",        function() return get     ("vsize")                * factor end)
136
137registerdirect("EmWidth",              function() return emwidths [false]                 * factor end)
138registerdirect("ExHeight",             function() return exheights[false]                 * factor end)
139
140registerdirect("HSize",                function() return get     ("hsize")                * factor end)
141registerdirect("VSize",                function() return get     ("vsize")                * factor end)
142registerdirect("LastPageNumber",       function() return getcount("lastpageno")                    end)
143
144registerdirect("OverlayWidth",         function() return getdimen("d_overlay_width")      * factor end)
145registerdirect("OverlayHeight",        function() return getdimen("d_overlay_height")     * factor end)
146registerdirect("OverlayDepth",         function() return getdimen("d_overlay_depth")      * factor end)
147registerdirect("OverlayLineWidth",     function() return getdimen("d_overlay_linewidth")  * factor end)
148registerdirect("OverlayOffset",        function() return getdimen("d_overlay_offset")     * factor end)
149registerdirect("OverlayRegion",        function() return getmacro("m_overlay_region")              end)
150--------------("CurrentLayout",        function() return getmacro("currentlayout")                 end)
151
152-- registerscript("PageFraction",      function() return mpboolean(page_fraction     ()) end)
153-- registerscript("OnRightPage",       function() return mpboolean(on_right_page     ()) end)
154-- registerscript("OnOddPage",         function() return mpboolean(is_odd_page       ()) end)
155-- registerscript("InPageBody",        function() return mpboolean(in_body_page      ()) end)
156-- registerscript("LayoutHasChanged",  function() return mpboolean(layout_has_changed()) end)
157
158registerdirect("PageFraction",         function() return page_fraction     () end)
159registerdirect("OnRightPage",          function() return on_right_page     () end)
160registerdirect("OnOddPage",            function() return is_odd_page       () end)
161registerdirect("InPageBody",           function() return in_body_page      () end)
162registerdirect("LayoutHasChanged",     function() return layout_has_changed() end)
163
164registerdirect("PageFraction",         page_fraction     )
165registerdirect("OnRightPage",          on_right_page     )
166registerdirect("OnOddPage",            is_odd_page       )
167registerdirect("InPageBody",           in_body_page      )
168registerdirect("LayoutHasChanged",     layout_has_changed)
169
170registerdirect("defaultcolormodel",    defaultcolormodel)
171