// GBITMAP.SR One bit map per text line

// GYPSY made thicker bars between different documents

get "BRAVO.DF"
get "CHAR.DF"
get "DISPLAY.DF"
get "HEAP.DF"
get "GINN.DF" // ** for fquad, fcenter

// Incoming procedures

external

	[
	movec
	mapcp
	tabwidth
	scanconvert
	freedl
	formaty
	makeroomww
	drawunderline;
	ugt
	specstate
	nextspecstate
	move
	]

// Incoming statics

external
	[
	xleftmargstd
	vlookremark // $$
	rgdoc
	rgcpfdispl
	vww
	macdl
	rgxlast
	rgylast
	rgmaxdl
	rgxlastnew
	mpdldcb
	vfont
	vwidth
	rgul
	rgcpfirstnew
	vdoc
	vchremain
	vlb
	vpw
	vrgcc
	rgcplastnew
	vxfirst
	macww
	rghpused
	rgdlfirst
	rgdllast
	rgpbmnew
	pbmfirstfree
	vyorig
	vdcborig
	rgyfirst
	rgupdate
	vheight
	rgmask
	vjn;
	vjw;
	widthblmin;
	vcpfirstj
	vcplastd;
	widthblave;
// GYPSY ADDED:
	vlookcr
	vlooksp
	vlooktab
	rgsdoc
	ddoc
	vlookctrl
	vquad
	vlook
	vpara
	vcp
	vxleftmarg
	vlookmarker
	vchangemarker
	fontstd
	]


// Outgoing procedures

external
	[
	createdisplay;
	splitwindow;
	makeclearbitmap;
	makebitmap;
// GYPSY ADDED:
	bitmapwidth
	]

// Outgoing statics
external
	[
	dcb
	cdcb
	]


// Local procedures:
//	createdcb

// Local statics

static
	[
	dl
	y
	dcbt
	dcb
	cdcb
	widthlies // **
	]

// M A K E C L E A R B I T M A P
// SPE catalogue no.

let makeclearbitmap(dl) be
[
rgxlastnew ! dl = -1;
rgpbmnew ! dl = 0;
(mpdldcb ! dl) >> DCB.nwrds = 0;
]

// M A K E B I T M A P

and makebitmap(dl) be
[
let pconvert = vec convertl;
let char = nil;
vcp = specstate(vdoc,rgcpfirstnew ! dl,vpara) // ** format was called
let changecp = nextspecstate() // **
let x = vxfirst - (xleftmargstd & #177760) ; // **
let tpbm = rgpbmnew ! dl;
let wdadjust = tpbm-vwidth;
// ** let xfirstwd = (vxfirst & #177760);
let shifted = vxleftmarg - x // **
rgmask = #460;

// GYPSY MOVED UP:
movec(rgpbmnew ! dl,rgpbmnew ! dl+((rgpbmnew ! dl-(offset HP.use)/
16) >> HP.siz)-((offset HP.use)/16)-bsiz-1,0);

pconvert >> CONVERT.nwrds = vwidth;
if vlookmarker & vchangemarker then // **
	[
	pconvert >> CONVERT.font = fontstd+2;
	pconvert >> CONVERT.dwa = wdadjust;
// $$	for i = 1 to vchangemarker do
	for i = 1 to 1 do // $$
		[
		pconvert >> CONVERT.dba = 17 - i - i;
		scanconvert($|, pconvert)
		]
	]
pconvert >> CONVERT.font = vfont+2;
pconvert >> CONVERT.dba = 15- x << X.bitindex; // **
pconvert >> CONVERT.dwa = wdadjust+ x << X.wordindex; // **

// rgul ! dl = uloff; // not in GYPSY

let widthch = nil;
let cc = nil; let lastcc = nil;
let tnwrds = vwidth;
	[
	mapcp(vdoc,vcp);
	while vchremain do
		[
		test vlb ifso
			[
			char = vpw >> lh;
			vlb = false;
			]
		ifnot	[
			char = vpw >> rh;
			vlb = true;
			vpw = vpw+1;
			];
		if vcp eq changecp then // **
			[
			changecp = nextspecstate() // **
			if changecp eq -1 then char = chcr
			]
		vchremain = vchremain-1;
		cc = (vrgcc ! char);
		widthch = cc << CC.width;
		widthlies = false ; // **
		if ((vlook & mvanish) ne 0) %
		    (((vlook & mremark) ne 0) & (not vlookremark))
			then goto skipz // ** // $$
		if cc << CC.spec then
			[ 
			if char eq chblind then goto skipz;
			test char eq chsp
			ifso [
			    test vquad eq fjust
			    ifso
			       test ugt(vcp,vcpfirstj)
			       ifso
				[ widthch = vjw;
				if vjn gr 0 then
					widthch = widthch+1;
				vjn = vjn-1;
				]
			       ifnot widthch = widthblmin;
			     ifnot if vquad eq fcenter then
				[ 
				widthch = widthblave;
				if ugt(vcp,vcplastd) then
					widthch = 0;
				] 
			    unless vlooksp do goto skipnz // **
			    goto noskip;
			    ] 
			ifnot test char ls #40
			ifso
				[ switchon char into
					[
case chcr:				test vlookcr // **
					      ifso goto noskip // **
					      ifnot goto skipnz; // *
case chlf:				goto skipz;
case chtab:				widthch=vquad eq fcenter?64,
					    tabwidth(x+shifted)
					       // ** was +xfirstwd)
					widthlies = true ; // **
				 	test vlooktab // **
					      ifso goto noskip // **
					      ifnot goto skipnz; // *
default:				if vlookctrl then
					  [ char = char+#140;
					  widthch = (vrgcc+char) >>
							CC.width;
					  drawunderline(x,
						x+widthch-1, tpbm,
						1, tnwrds)
					  ] 
					goto noskip;
					] 
				] 
			ifnot if char gr #177 then
				[ if vlookctrl then
				   [ if char gr #300 then
				      [
				      widthch=(vrgcc+char-#200) >>
						CC.width;
				      drawunderline(x, x+widthch-1,
					tpbm, 5, tnwrds)
				      char = char-#100;
				      ] 
				    widthch = (vrgcc+char-#100) >>
						CC.width;
			  	    drawunderline(x, x+widthch-1,
					tpbm, 3, tnwrds)
			    	    drawunderline(x, x+widthch-1,
					tpbm, 1, tnwrds);
				    char = char-#100;
				    ] 
			goto noskip;
			] 
skipz:			if vcp eq vcplastd then
				goto endbitmap;
			vcp = vcp+1;
			loop;
skipnz:			x = x+widthch
			pconvert >> CONVERT.dwa = wdadjust+x << X.wordindex
			pconvert >> CONVERT.dba = 15-x << X.bitindex;
			if (vlook & mul) ne 0  then
				drawunderline(x-widthch, x-1,
					tpbm, vheight-2, tnwrds);
			if vcp eq vcplastd then
				goto endbitmap;
			vcp = vcp+1;
			loop;
noskip:			];
		lastcc = cc;
		test (vlook & (mbold % mitalic % mremark)) ne 0
		ifso scanconvertmod(char,pconvert) // **
		ifnot scanconvert(char,pconvert);
		if widthlies then goto skipnz// **
		if (vlook & mul) ne 0 then
			drawunderline(x, x+widthch-1, tpbm,
			vheight-2, tnwrds);
		if vcp eq vcplastd then
			goto endbitmap;
		vcp = vcp+1;
		x = x + widthch
		] // end while
	] repeat
endbitmap:		let pdcb = mpdldcb ! dl;
			pdcb >> DCB.nwrds = vwidth;
			pdcb >> DCB.htab = xleftmargstd << X.
wordindex; // **
			pdcb >> DCB.sa = tpbm;
			return;
] // end makebitmap

and scanconvertmod(char, pconvert) be
[
if (vlook & (mitalic % mremark)) ne 0 then
	[
	let pfont = vfont + 2
	let pchar = pfont+char + pfont!char
	let ht = (pchar+1) >> rh
	let pdummy = pfont+chmod + pfont!chmod
	move(pchar-ht, pdummy-ht, ht+2)
	pchar = pdummy
	char = chmod
	let bot = pdummy-1
	while bot ne pdummy-ht & @bot eq 0 do bot=bot-1
	if (vlook & mitalic) ne 0 then
		for j = bot-4 to pdummy-ht by -1 do
			@j = @j rshift ((bot-j) rshift 2)
	if (vlook & mremark) ne 0 then
		for j = bot to pdummy-ht by -2 do
			@ j = @j rshift 1
	]	
if (vlook & mbold) ne 0 then
	[
	let tw = pconvert >> CONVERT.dwa
	let tb = pconvert >> CONVERT.dba
	if tb eq 0 then pconvert >> CONVERT.dwa = tw+1
	pconvert >> CONVERT.dba = (tb-1) & #17
	scanconvert(char, pconvert)
	pconvert >> CONVERT.dwa = tw
	pconvert >> CONVERT.dba = tb
	]
scanconvert(char, pconvert)
]

// C R E A T E D I S P L A Y
// catalogue no.
and createdisplay( ) be
[
for ww = 0 to macww-1 do
	[
	rghpused ! ww = 0;
	for dl = rgdlfirst ! ww to rgdllast ! ww do
		[
		pbmfirstfree = 1; freedl(dl);
		];
	];
dl = 0;
y = vyorig;
rv vdcborig = 0;
dcbt = dcb;
cdcb = 0;
if (rgyfirst ! 0) gr y then
	[ createdcb(rgyfirst ! 0-y,false);
	createdcb(4,false); // ** was 2,true
	] 
for ww = 0 to macww-1 do
	[ rgupdate ! ww = true;
	 rgdlfirst ! ww = dl;
	rgdllast ! ww = rgdlfirst ! ww-1;
		[
		rgmaxdl ! ww = dl;
		rgxlast ! dl = -1;
		mpdldcb ! dl = dcbt;
		dl = dl+1;
		createdcb(vheight,false);
		if (dl ge maxdl) % (cdcb ge maxcdcb-1) then
			[ macww = ww+1;
			goto out;
			] 
		] repeatwhile (y+vheight) le rgylast ! ww;
	rgylast ! ww = y;
	unless ww eq macww-1 do
		rgyfirst ! (ww+1) = y;
	rgmaxdl ! ww = rgmaxdl ! ww +1;
	createdcb(ww eq 0 %
		   (ww ls macww-1&rgsdoc!ww eq rgsdoc!(ww+1))? 2,4,
		ww gr 0 & ww ls macww-1); //**  WAS ALWAYS 2,true
	] 
out:
createdcb(4,false); // ** 4 was 2
rv vdcborig = dcb;
macdl = dl;
] 

// C R E A T E D C B
// catalogue no. 
and createdcb(height,black) be
[ if height << odd then height = height+1;
dcbt >> DCB.next = 0;
if dcbt ne dcb then 
	(dcbt-dcbl) >> DCB.next = dcbt;
dcbt >> DCB.mode = 0;
test black ifso
	dcbt >> DCB.bw = 1
ifnot	dcbt >> DCB.bw = 0;
dcbt >> DCB.htab = 0;
dcbt >> DCB.nwrds = 0;
dcbt >> DCB.sa = 0;
dcbt >> DCB.slc = height rshift 1;
dcbt = dcbt+dcbl;
cdcb = cdcb+1;
y = y+height;
] 

// S P L I T W I N D O W 
// catalogue no. = 121
and splitwindow(y,doc) be
[ formaty(y);
vww = vww+1;
makeroomww(vww);
rgylast ! vww = rgylast ! (vww-1);
rgylast ! (vww-1) = y;
rgyfirst ! vww = y;
rgdoc ! vww = doc;
rgsdoc ! vww = doc eq ddoc? rgsdoc ! (vww-1), doc; // **
rgcpfdispl ! vww = 0;
rgdlfirst ! vww = 0; rgdllast ! vww = -1;
createdisplay( );
] 

// ** GYPSY ADDED (ignoring xfirst):

and bitmapwidth(xfirst, xlast) =
    ((xlast+3-((xleftmargstd & #177760)-31)) rshift 5) lshift 1;