InterpressPageUp.mesa
Copyright © 1987 by Xerox Corporation. All rights reserved.
Mark Weiser, November 15, 1988 4:43:59 pm PST
Wes Irish, December 3, 1987 4:14:02 pm PST
DIRECTORY
Commander USING [CommandProc, Handle, Register],
CommandTool USING [ArgumentVector, Parse, Failed],
Convert USING [RealFromRope],
FS USING [Error],
Imager USING [Context, DoSave, ScaleT, TranslateT],
ImagerInterpress USING [Close, Create, DoPage, Ref ],
Interpress USING [DoPage, LogProc, Master, Open ],
IO USING [PutF, STREAM],
Rope USING [ ROPE, Fetch, Equal, Length, Substr ];
IMPORTS Commander, CommandTool, Convert,
FS, Imager, ImagerInterpress, Interpress,
IO, Rope ~ {
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
paperWidth: REAL ← 0.01 * 2.54 * 8.5; -- meters
paperHight: REAL ← 0.01 * 2.54 * 11.0; -- meters
margin: REAL ← 0.01 * 1.0; -- one centimeter margin
defaultScaleFactor: REAL = 0.455; -- enough to leave 2 centimeters free across whole page
scaleFactor: REAL ← defaultScaleFactor;
scaleIt: BOOL ← TRUE;
input: ROPE;
output: ROPE;
PageUp:
PROC [output, input:
ROPE, logProc: Interpress.LogProc, ms:
STREAM] ~ {
ref: ImagerInterpress.Ref ~ ImagerInterpress.Create[fileName: output];
master: Interpress.Master ~ Interpress.Open[fileName: input, log: logProc];
inPages: INT = master.pages;
outPages: INT = (inPages+3)/4;
outPage: INT ← 1;
lowPage: INT ← 1;
highPage: INT ← outPages * 4;
WriteSide:
PROC [context: Imager.Context] ~ {
Page1:
PROC ~ {
IF lowPage > inPages THEN RETURN;
Imager.TranslateT[context, [margin, paperHight/2.0]];
IF scaleIt THEN Imager.ScaleT[context, scaleFactor];
Interpress.DoPage[master: master, page: lowPage, context: context, log: logProc];
lowPage ← lowPage + 1;
};
Page2:
PROC ~ {
IF lowPage > inPages THEN RETURN;
Imager.TranslateT[context, [(paperWidth/2.0), paperHight/2.0]];
IF scaleIt THEN Imager.ScaleT[context, scaleFactor];
Interpress.DoPage[master: master, page: lowPage, context: context, log: logProc];
lowPage ← lowPage + 1;
};
Page3:
PROC ~ {
IF lowPage > inPages THEN RETURN;
Imager.TranslateT[context, [margin, margin]];
IF scaleIt THEN Imager.ScaleT[context, scaleFactor];
Interpress.DoPage[master: master, page: lowPage, context: context, log: logProc];
lowPage ← lowPage + 1;
};
Page4:
PROC ~ {
IF lowPage > inPages THEN RETURN;
Imager.TranslateT[context, [(paperWidth/2.0), margin]];
IF scaleIt THEN Imager.ScaleT[context, scaleFactor];
Interpress.DoPage[master: master, page: lowPage, context: context, log: logProc];
lowPage ← lowPage + 1;
};
Imager.DoSave[context, Page1];
Imager.DoSave[context, Page2];
Imager.DoSave[context, Page3];
Imager.DoSave[context, Page4];
};
ms.PutF["Making new interpress file %g (%g pages) from %g (%g input pages) ... ",
[rope[output]],
[integer[outPages]],
[rope[input]],
[integer[inPages]]];
WHILE lowPage <= inPages
DO
ms.PutF["[%g", [integer[outPage]]];
ImagerInterpress.DoPage[self: ref, action: WriteSide];
ms.PutF["] "];
outPage ← outPage + 1;
ENDLOOP;
ImagerInterpress.Close[self: ref];
ms.PutF["\n%g written.\n", [rope[output]]];
};
ProcessArgs:
PROC [cmd: Commander.Handle, argv: CommandTool.ArgumentVector]
RETURNS [msg:
ROPE ←
NIL] ~ {
i: INT ← 1;
arrowSpecified: BOOL ← FALSE;
input ← NIL;
output ← NIL;
scaleFactor ← defaultScaleFactor;
scaleIt ← TRUE;
WHILE i < argv.argc
DO
len: INT ~ Rope.Length[argv[i]];
IF len = 0 THEN { msg ← "Null argument"; GOTO Bad };
IF Rope.Fetch[argv[i], 0] = '-
THEN {
IF len = 1 THEN { msg ← "Missing option"; GOTO Bad };
SELECT Rope.Fetch[argv[i], 1]
FROM
'b => scaleIt ← FALSE; -- booklet style, no scaling
'f => scaleIt ← TRUE; -- fit a "standard" 8.5 x 11
's => {
-- use an arbitrary scaleing factor
scaleIt ← TRUE;
scaleFactor ← Convert.RealFromRope[Rope.Substr[argv[i], 2]];
};
ENDCASE => { msg ← "Invalid option"; GOTO Bad };
}
ELSE {
SELECT
TRUE
FROM
Rope.Equal[argv[i], "←"] => {
IF input = NIL THEN { msg ← "No output file specified"; GOTO Bad };
arrowSpecified ← TRUE;
};
input = NIL => input ← argv[i];
(output =
NIL)
AND arrowSpecified => {
output ← input;
input ← argv[i];
};
ENDCASE => {
IF arrowSpecified
THEN msg ← "Too much in command line"
ELSE msg ← "Missing \"←\"";
GOTO Bad;
};
};
i ← i.SUCC;
ENDLOOP;
IF input = NIL OR output = NIL THEN { msg ← "File name missing"; GOTO Bad };
};
PageUpLogProc: Interpress.LogProc ~ {
-- doesn't do anything!
PROC [class: INT, code: ATOM, explanation: ROPE];
};
PageUpCmd: Commander.CommandProc ~ {
ENABLE {
FS.Error => {
result ← $Failure;
msg ← error.explanation;
GOTO Failure;
};
};
argv: CommandTool.ArgumentVector ← CommandTool.Parse[cmd
! CommandTool.Failed => { msg ← errorMsg; result ← $Failure; GOTO Failure }];
IF argv = NIL THEN RETURN;
msg ← ProcessArgs[cmd, argv];
IF msg # NIL THEN { result ← $Failure; RETURN };
PageUp[output, input, PageUpLogProc, cmd.out];
};
doc: ROPE ← "Create a new IP master that has 4 pages per page.";
Commander.Register["InterpressPageUp", PageUpCmd, doc];
Commander.Register["IPPageUp", PageUpCmd, doc];
}.