// S C A N
//  errors 800
//
// ScanPressPage(pDoc, page, part)
// ScanBreakPage(pDoc, page, pass)
//

get "Spruce.d"
get "sprucefiles.d"
get "PressFile.d"

// outgoing procedures
external
	[
	ScanPressPage
	ScanBreakPage
	]

// incoming procedures
external
	[
//SHOW
	ShowCharacters
	ShowCharactersImmediate
	ShowRectangle
	ShowX
	ShowY
	ShowXY
	ShowCharSet
	ShowCharFont
	ShowCharSetSpace
	ShowOnCopy
	ShowDots
	FSGetRelease

//WINDOW
	WindowRead
	WindowReadBlock
	WindowReadByte
	WindowSetPosition

//PARTS
	SetPartBounds
	SetPositioninPart
	SetBytePositioninPart
	GetPositioninPart
	SkipinPart

//SPRUCE
	SpruceError
	SpruceCondition
	DblShift
	FSGetX
	FSGet
	FSPut
	DisableComments

//SPRUCEML
	DoubleAdd; DoubleSub; DoubleCop
	TGr

//Time stuff
	CONVUDT

//OS
	MoveBlock
	SetBlock; Zero
	]

// incoming statics
external
	[
	printerName
	printerDevice
	breakPage
	Version; MinorVersion; SpruceVersion; SpruceMinorVersion

	Capabilities
	comments
	commentFree
	LogoText
	numComments
	]

// internal statics
static
	[
	Entity
	]

// File-wide structure and manifest declarations.

structure EHC :			//EH + some stuff
[
	@EH
	next	word			//Pointer to next entity
	ELCPos	word 2		//Part pos of entity commands
]

// Procedures

let ScanPressPage(pDoc, page, part) be
[
	let EL=pDoc>>DocG.EL
	let DL=pDoc>>DocG.DL
]

// Break-page maker

// For three color and four color printers, create three (or four) color separated images.
// On three-color printers, black is the sum of all three toners;  on the four-color puffin there
// is a black developer housing.  On black printers, this routine returns unless it is the first pass.

and ScanBreakPage(pDoc, page, pass) be
[
manifest [
	textLeft=30; textFirst=70; textSpace=4

	commentLeft = 15; commentFirst = textFirst - 8*textSpace; commentSpace = 2

	logoLeft = 15; logoRight = 60; logoTop = 95; logoBottom = 10
	]
	let blackPass, magentaPass, yellowPass, cyanPass = true, true, true, true
	test breakPage eq 1  ifso
		[
		if pass ne 1 return
		]
	ifnot
		[
		magentaPass = pass eq 1
		yellowPass = pass eq 2
		cyanPass = pass eq 3
		if breakPage eq 4 do blackPass = pass eq 4   // for threecolor all passes are black
		]
	let f = commentFree		// Temporarily disable comment entries
	DisableComments()

	ShowCharSetSpace(0)
	ShowCharSet(64)		// Font set reserved for break page
	ShowCharFont(0)		// Normal printing
if blackPass do
	[
	BreakString("Printer ", textLeft, textFirst)
	BreakString(printerName)
	BreakString("Spruce version ", textLeft, textFirst-textSpace)
	BreakNumber(Version)
	BreakString(".")
	BreakNumber(MinorVersion)
	BreakString(" -- spooler version ")
	BreakNumber(SpruceVersion)
	BreakString(".")
	BreakNumber(SpruceMinorVersion)
	]
if blackPass % cyanPass do BreakString("File: ", textLeft, textFirst-3*textSpace)
if cyanPass do 	BreakString(lv pDoc>>DocG.FileStr)
if blackPass do
	[
	BreakString("Creation date: ", textLeft, textFirst-4*textSpace)
	BreakString(lv pDoc>>DocG.DateStr)
	BreakString("Printing date: ", textLeft, textFirst-5*textSpace)
	let s=vec 100
	CONVUDT(s,0,true)
	BreakString(s)
	]
if blackPass % magentaPass % yellowPass do  BreakString("For: ", textLeft, textFirst-6*textSpace)
if magentaPass % yellowPass do 	BreakString(lv pDoc>>DocG.CreatStr)
if blackPass do
	[
	if (lv pDoc>>DocG.ByStr)>>STR.length ne 0 do 
		[
		BreakString("  By: ")
		BreakString(lv pDoc>>DocG.ByStr)
		]
	BreakString("", textLeft, textFirst-7*textSpace)
	 let p=(pDoc>>DocG.nPages-breakPage)/breakPage
	 let c=pDoc>>DocG.nCopies
	 let s = p
	if pDoc>>DocG.duplex then s= (p + 1)/2
	 BreakNumber(s*c+1)
	 BreakString(" total sheets = ")
	 BreakNumber(p)
	 BreakString((p eq s? ( p eq 1? " page, ", " pages, "), (p eq 1? " side, ", " sides, ")))
	 BreakNumber(c)
	 BreakString((c eq 1? " copy.", " copies."))

	if numComments then for i = 0 to numComments do
		[
		let comment = comments!i
		unless comment loop
		BreakString(comments+comment, commentLeft, commentFirst-i*commentSpace)
		]
	//Now sprinkle top of break page with first letter of "For" name
	let p=(lv pDoc>>DocG.CreatStr
	let s=vec 2
	s>>STR.length=2
	s>>STR.char↑1=(c ge $a)? c-$a+$A,c
	s>>STR.char↑2=$*s
	BreakString(s,12,100
	for i=0 to 5 do BreakString(s)
	BreakString(p,42,100)
	]
	if (Capabilities & mMailbox) do
		[
		ShowOnCopy(1001)
		BreakString("MORE IN OVERFLOW BIN", textLeft, commentFirst-(numComments + 1)*commentSpace)
		ShowOnCopy(1002)
		BreakString("MORE IN MAILBOX", textLeft, commentFirst-(numComments + 1)*commentSpace)
		ShowOnCopy(0)
		]
	if (Capabilities & mDuplex) do
		[
		ShowOnCopy(1003)
		BreakString("Probable two-sided print problem", textLeft, commentFirst-(numComments + 1)*commentSpace)
		ShowOnCopy(0)
		]
// Now pepper the logo around...
	ShowCharFont(1)
	if cyanPass do for x=0 to 1 do for y=0 to 1 do
		BreakString(LogoText, logoLeft+x*(logoRight-logoLeft),
			logoBottom+y*(logoTop-logoBottom))

// And print some strong vertical lines for easy identification:
	let x=5
	// Red
if magentaPass % yellowPass do for i=0 to 1 do
		[
		for j=0 to 5 do
			[
			ShowXY((x+j)*254, 10*254)		// x=.5",.6",,7" ; y=1.0"
			// 10.0 " takes too much time on Alto II XM at 384 7-31-78
			ShowRectangle(254/2, 90*254)		// .05" by 9.0" -- penguin 1/29/79
			]
		x=75
		]
	commentFree = f		// reenable comments if were enabled before
]

and BreakString(str, x, y; numargs n) be
[
	if n ne 1 then ShowXY(x*254, y*254)	// x,y in tenths of inches
    // any errors rendering these strings will be ignored
	let len = str>>STR.length
	ShowCharactersImmediate(str, 1, len+1)
]

and BreakNumber(n) be
[
	let nn=n/10
	if nn then BreakNumber(nn)
	nn=(n rem 10)+$0+400b
	BreakString(lv nn)
]

// DCS, ,July 27, 1977  10:42 PM minor mods (WindowRead2Bytes -> WindowRead (!))
// September 30, 1977  12:32 AM, implement ShowDots (not opaque tho)
// October 3, 1977  6:46 AM, handle three-color break page
// October 10, 1977  2:52 PM, add "Version", externally settable
// December 16, 1977  11:23 AM, narrower spacing between comments
// January 20, 1978  4:43 PM, remove PimlicoAlt
// January 20, 1978  5:34 PM, disable Durango
// May 15, 1978  10:08 PM, be less paternalistic about errors in BreakString
// June 7, 1978  9:40 PM, add skipcontrolbytesimmediate, alternative features
// June 14, 1978  8:43 PM, repair DL skip in alternative
// July 31, 1978  11:38 PM, make break page verticals .1" shorter to avoid breakup
// September 14, 1978  5:25 PM, add printer name, new version stuff
// October 3, 1978  5:20 PM, new calling sequence to ShowCharacters . . . for fast (mu) inner loop
// October 16, 1978 1:18 PM, puffin turned on - BWB
// December 6, 1978  11:46 AM  add Penguin, use Capabilities to select nBreaks
// March 7, 1979  1:56 PM make four color break pages
// May 22, 1979  12:18 PM fix break page maker
// August 1, 1979  3:18 PM, mBlack became mBlackHousing ??!!
// August 5, 1979  7:44 AM, add "By" to break page and correct sheet count for twosided print
// August 7, 1979  2:49 PM, fix same
// August 24, 1979  1:53 PM, add ShowOnCopy stuff for Penguin print-time break page messages
// September 24, 1979  10:21 AM, remove log and proprietary stuff from breakpage
// November 18, 1979  9:49 PM, add logo text from user.cm
// January 18, 1980  12:11 PM, decide number of sheets from DocG.duplex
// May 9, 1980, 4:15 PM, check size of entity list in ShowEntity. error 803
//July 16, 1980,4:21 PM use breakPage for count of images on break page
// January 28, 1981, 12:30 PM, added  Sproull-style  "Printing date" and user first letters
	//to the break page
//