mtx-context-compare.tex /size: 4491 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=mtx-context-compare,
3%D        version=2015.07.14,
4%D          title=\CONTEXT\ Extra Trickry,
5%D       subtitle=Compare Files,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% begin help
15%
16% usage: context --extra=compare [options] file-1 file-2
17%
18% example: context --extra=compare file1.pdf file-2.pdf
19%          context --extra=compare file1.pdf file-2.pdf --colors=red,blue
20%
21% end help
22
23\input mtx-context-common.tex
24
25\starttext
26
27% \setupinteraction
28%   [state=start]
29
30% \showmakeup
31
32\definecolor[colorred]    [r=1,a=1,t=.5]
33\definecolor[colorgreen]  [g=1,a=1,t=.5]
34\definecolor[colorblue]   [b=1,a=1,t=.5]
35\definecolor[colorgray]   [s=0,a=1,t=.5]
36\definecolor[colorcyan]   [g=1,b=1,a=1,t=.5]
37\definecolor[colormagenta][r=1,b=1,a=1,t=.5]
38\definecolor[coloryellow] [r=1,g=1,a=1,t=.5]
39
40% \definecolor[colorone][black] % no, keep undefined !
41% \definecolor[colortwo][black] % no, keep undefined !
42
43% \defineviewerlayer[one] % we use filename as title
44% \defineviewerlayer[two] % we use filename as title
45
46\starttexdefinition unexpanded ShowBoth #1#2#3
47    \startTEXpage[width=21cm]
48        \startoverlay
49            {
50                \startviewerlayer[one]
51                    \signalcharacter\externalfigure[#1][page=#3,width=21cm,foregroundcolor=colorone]
52                \stopviewerlayer
53            }
54            {
55                \startviewerlayer[two]
56                    \signalcharacter\externalfigure[#2][page=#3,width=21cm,foregroundcolor=colortwo]
57                \stopviewerlayer
58            }
59        \stopoverlay
60    \stopTEXpage
61\stoptexdefinition
62
63\starttexdefinition unexpanded ShowOne #1#2
64    \startTEXpage[width=21cm]
65        \startviewerlayer[one]
66            \signalcharacter
67            \externalfigure[#1][page=#2,width=21cm,foregroundcolor=colorone]
68        \stopviewerlayer
69    \stopTEXpage
70\stoptexdefinition
71
72\starttexdefinition unexpanded ShowTwo #1#2
73    \startTEXpage[width=21cm]
74        \startviewerlayer[two]
75            \signalcharacter
76            \externalfigure[#1][page=#2,width=21cm,foregroundcolor=colortwo]
77        \stopviewerlayer
78    \stopTEXpage
79\stoptexdefinition
80
81\startluacode
82
83local report = logs.reporter("compare")
84
85local fileone = document.files[1] or ""
86local filetwo = document.files[2] or ""
87
88local colorone = false
89local colortwo = false
90
91local colors = document.arguments.colors
92
93if colors then
94    colorone, colortwo = string.splitup(colors,",")
95end
96
97local valid = {
98    red     = "colorred",
99    green   = "colorgreen",
100    blue    = "colorblue",
101    cyan    = "colorcyan",
102    magenta = "colormagenta",
103    yellow  = "coloryellow",
104    gray    = "colorgray",
105    grey    = "colorgray",
106}
107
108if fileone == "" or filetwo == "" then
109    report("provide two filenames")
110    os.exit()
111end
112
113if not lfs.isfile(fileone) then
114    report("unknown file %a",fileone)
115    os.exit()
116end
117
118if not lfs.isfile(filetwo) then
119    report("unknown file %a",filetwo)
120    os.exit()
121end
122
123if valid[colorone] and valid[colortwo] then
124    context.definecolor({ "colorone" }, { valid[colorone] })
125    context.definecolor({ "colortwo" }, { valid[colortwo] })
126end
127
128context.defineviewerlayer({ "one" }, { state = "start", title = file.nameonly(fileone) })
129context.defineviewerlayer({ "two" }, { state = "start", title = file.nameonly(filetwo) })
130
131local function check(name)
132    local fig = figures.push { name = name }
133    figures.identify()
134    figures.check()
135    local used = fig.used
136    figures.pop()
137    return used
138end
139
140local one = check(fileone) -- can crash
141local two = check(filetwo) -- can crash
142
143if not one then
144    report("invalid file %a",fileone)
145    os.exit()
146end
147
148if not two then
149    report("invalid file %a",filetwo)
150    os.exit()
151end
152
153local n_one = tonumber(one.pages) or 0
154local n_two = tonumber(two.pages) or 0
155
156if n_one == 0 or n_two == 0 or n_one ~= n_two then
157    report("files have different nofpages (%s vs %s)",n_one or "?",n_two or "?")
158end
159
160if n_one > n_two then
161    for i=1,n_two do
162        context.ShowBoth(fileone,filetwo,i)
163    end
164    for i=n_two+1,n_one do
165        context.ShowOne(fileone,i)
166    end
167else
168    for i=1,n_one do
169        context.ShowBoth(fileone,filetwo,i)
170    end
171    for i=n_one+1,n_two do
172        context.ShowTwo(filetwo,i)
173    end
174end
175
176\stopluacode
177
178\stoptext
179
180\endinput
181