Text/Mod/Setters.odc

MODULE TextSetters; (* SP410 *)
(**
	project	= "BlackBox"
	organization	= "www.oberon.ch"
	contributors	= "Oberon microsystems"
	version	= "System/Rsrc/About"
	copyright	= "System/Rsrc/About"
	license	= "Docu/BB-License"
	changes	= "
	- 20111211, Fyodor Tkachov, switched from National to i21sysCharacters
	- 20080215, Fyodor Tkachov, reviewed
	- 20050626, Fyodor Tkachov, russification edited
	- 20050430, Ivan Goryachev, russification
	- YYYYMMDD, nn, ...
	"
	issues	= "
	- ...
	"

**)

	(* correct NextPage postcond in docu *)
	(* make s.r, s.rd reducible? *)
	(* paraShutoff needs to be controlled by an approx flag to certain ops (later ...?) *)

	IMPORT
		Fonts, Ports, Printers, Stores, Models, Views, Properties,
		TextModels, TextRulers, i21sysCharacters;

	CONST
		(** Pref.opts, options of setter-aware views; 0 overrides 1 **)
		lineBreak* = 0; wordJoin* = 1; wordPart* = 2; flexWidth* = 3;

		tab = TextModels.tab; line = TextModels.line; para = TextModels.para;
		zwspace = TextModels.zwspace; nbspace = TextModels.nbspace;
		hyphen = TextModels.hyphen; nbhyphen = TextModels.nbhyphen;
		digitspace = TextModels.digitspace;
		softhyphen = TextModels.softhyphen;

		mm = Ports.mm;
		minTabWidth = 2 * Ports.point; stdTabWidth = 4 * mm;
		leftLineGap = 2 * Ports.point; rightLineGap = 3 * Ports.point;
		adjustMask = {TextRulers.leftAdjust, TextRulers.rightAdjust};
		centered = {}; leftFlush = {TextRulers.leftAdjust}; rightFlush = {TextRulers.rightAdjust};
		blocked = adjustMask;

		boxCacheLen = 64;
		seqCacheLen = 16;

		paraShutoff = MAX(INTEGER);	(* longest stretch read backwards to find start of paragraph *)
															(* unsafe: disabled *)
		cachedRulers = FALSE;	(* caching ruler objects trades speed against GC effectiveness *)
		periodInWords = FALSE;
		colonInWords = FALSE;

		minVersion = 0; maxVersion = 0; maxStdVersion = 0;


	TYPE
		Pref* = RECORD (Properties.Preference)
			opts*: SET;
			endW*: INTEGER;	(** preset (to width of view) **)
			dsc*: INTEGER	(** preset (to dominating line descender) **)
		END;


		Reader* = POINTER TO ABSTRACT RECORD
			r-: TextModels.Reader;	(** look-ahead state **)
			(** unit **)
			sString*: ARRAY 64 OF SHORTCHAR;	(** single chars in sString[0] **)
			string*: ARRAY 64 OF CHAR;	(** single lchars in string[0] **)
			view*: Views.View;
			(** unit props **)
			textOpts*: SET;
			mask*: CHAR;
			setterOpts*: SET;
			w*, endW*, h*, dsc*: INTEGER;
			attr*: TextModels.Attributes;
			(** reading state **)
			eot*: BOOLEAN;
			pos*: INTEGER;
			x*: INTEGER;	(** to be advanced by client! **)
			adjStart*: INTEGER;
			spaces*: INTEGER;
			tabIndex*: INTEGER;	(** tabs being processed; initially -1 **)
			tabType*: SET;	(** type of tab being processed; initially {} **)
			(** line props **)
			vw*: INTEGER;
			hideMarks*: BOOLEAN;
			ruler*: TextRulers.Ruler;
			rpos*: INTEGER
		END;

		Setter* = POINTER TO ABSTRACT RECORD (Stores.Store)
			text-: TextModels.Model;	(** connected iff text # NIL **)
			defRuler-: TextRulers.Ruler;
			vw-: INTEGER;
			hideMarks-: BOOLEAN
		END;


		LineBox* = RECORD
			len*: INTEGER;
			ruler*: TextRulers.Ruler;
			rpos*: INTEGER;
			left*, right*, asc*, dsc*: INTEGER;
			rbox*, bop*, adj*, eot*: BOOLEAN;	(** adj => adjW > 0; adj & blocked => spaces > 0 **)
			views*: BOOLEAN;
			skipOff*: INTEGER;	(** chars in [skipOff, len) take endW **)
			adjOff*: INTEGER;	(** offset of last block in box - adjust only this block **)
			spaces*: INTEGER;	(** valid, > 0 if adj & blocked **)
			adjW*: INTEGER;	(** valid if adj - to be distributed over spaces **)
			tabW*: ARRAY TextRulers.maxTabs OF INTEGER	(** delta width of tabs (<= 0) **)
		END;


		Directory* = POINTER TO ABSTRACT RECORD END;


		Worder = RECORD
			box: LineBox; next: INTEGER;
			i: INTEGER
		END;

		StdReader = POINTER TO RECORD (Reader) END;

		StdSetter = POINTER TO RECORD (Setter)
			rd: Reader;	(* subject to reduction? *)
			r: TextModels.Reader;	(* subject to reduction? *)
			ruler: TextRulers.Ruler;
			rpos: INTEGER;
			key: INTEGER
		END;

		StdDirectory = POINTER TO RECORD (Directory) END;


	VAR
		dir-, stdDir-: Directory;

		nextKey: INTEGER;
		boxIndex, seqIndex: INTEGER;
		boxCache: ARRAY boxCacheLen OF RECORD
			key: INTEGER;	(* valid iff key > 0 *)
			start: INTEGER;
			line: LineBox	(* inv ruler = NIL *)
		END;
		seqCache: ARRAY seqCacheLen OF RECORD
			key: INTEGER;	(* valid iff key > 0 *)
			start, pos: INTEGER	(* sequence [start, end), end >= pos *)
		END;


	(** Reader **)

	PROCEDURE (rd: Reader) Set* (
		old: TextModels.Reader;
		text: TextModels.Model; x, pos: INTEGER;
		ruler: TextRulers.Ruler; rpos: INTEGER; vw: INTEGER; hideMarks: BOOLEAN
	), NEW, EXTENSIBLE;
	BEGIN
		ASSERT(text # NIL, 20);
		ASSERT(ruler # NIL, 22);
		rd.r := text.NewReader(old); rd.r.SetPos(pos); rd.r.Read;
		rd.sString[0] := 0X; rd.string[0] := 0X; rd.view := NIL;
		rd.textOpts := {};
		rd.setterOpts := {}; rd.w := 0; rd.endW := 0; rd.h := 0; rd.dsc := 0;
		rd.attr := NIL;
		rd.eot := FALSE; rd.pos := pos; rd.x := x;
		rd.tabIndex := -1; rd.tabType := {};
		rd.adjStart := pos; rd.spaces := 0;
		rd.ruler := ruler; rd.rpos := rpos; rd.vw := vw; rd.hideMarks := hideMarks
	END Set;

	PROCEDURE (rd: Reader) Read*, NEW, EXTENSIBLE;
	(** pre: rd set **)
	(** post: rd.pos = rd.pos' + Length(rd.sString) + Length(rd.string) **)
	BEGIN
		IF rd.r.char < 100X THEN
			rd.sString[0] := SHORT(rd.r.char); rd.sString[1] := 0X;
			rd.string[0] := 0X
		ELSE
			rd.sString[0] := 0X;
			rd.string[0] := rd.r.char; rd.string[1] := 0X
		END;
		rd.view := rd.r.view;
		rd.textOpts := {};
		rd.setterOpts := {};
		rd.w := rd.r.w; rd.endW := rd.w; rd.h := rd.r.h; rd.dsc := 0;
		rd.attr := rd.r.attr;
		rd.eot := rd.r.eot;
		INC(rd.pos);
		rd.r.Read
	END Read;

	PROCEDURE (rd: Reader) AdjustWidth* (start, pos: INTEGER; IN box: LineBox;
		VAR w: INTEGER
	), NEW, ABSTRACT;

	PROCEDURE (rd: Reader) SplitWidth* (w: INTEGER): INTEGER, NEW, ABSTRACT;


	(** Setter **)

	PROCEDURE (s: Setter) CopyFrom- (source: Stores.Store), EXTENSIBLE;
	BEGIN
		WITH source: Setter DO
			s.text := source.text; s.defRuler := source.defRuler;
			s.vw := source.vw;  s.hideMarks := source.hideMarks
		END
	END CopyFrom;

	PROCEDURE (s: Setter) Internalize- (VAR rd: Stores.Reader), EXTENSIBLE;
		VAR thisVersion: INTEGER;
	BEGIN
		s.Internalize^(rd);
		IF rd.cancelled THEN RETURN END;
		rd.ReadVersion(minVersion, maxVersion, thisVersion)
	END Internalize;

	PROCEDURE (s: Setter) Externalize- (VAR wr: Stores.Writer), EXTENSIBLE;
	BEGIN
		s.Externalize^(wr);
		wr.WriteVersion(maxVersion)
	END Externalize;

	PROCEDURE (s: Setter) ConnectTo* (text: TextModels.Model;
		defRuler: TextRulers.Ruler; vw: INTEGER; hideMarks: BOOLEAN
	), NEW, EXTENSIBLE;
	BEGIN
		IF text # NIL THEN
			s.text := text; s.defRuler := defRuler; s.vw := vw; s.hideMarks := hideMarks
		ELSE
			s.text := NIL; s.defRuler := NIL
		END
	END ConnectTo;


	PROCEDURE (s: Setter) ThisPage* (pageH: INTEGER; pageNo: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, 0 <= pageNo **)
	(** post: (result = -1) & (pageNo >= maxPageNo) OR (result = pageStart(pageNo)) **)

	PROCEDURE (s: Setter) NextPage* (pageH: INTEGER; start: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, ThisPage(pageH, pageNo) = start [with pageNo = NumberOfPageAt(start)] **)
	(** post: (result = start) & last-page(start) OR result = next-pageStart(start) **)


	PROCEDURE (s: Setter) ThisSequence* (pos: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, 0 <= pos <= s.text.Length() **)
	(** post: (result = 0) OR (char(result - 1) IN {line, para}) **)

	PROCEDURE (s: Setter) NextSequence* (start: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, ThisSequence(start) = start **)
	(** post: (result = start) & last-line(start) OR (ThisSequence(t, result - 1) = start) **)

	PROCEDURE (s: Setter) PreviousSequence* (start: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, ThisSequence(t, start) = start **)
	(** post: (result = 0) & (start = 0) OR (result = ThisSequence(t, start - 1)) **)


	PROCEDURE (s: Setter) ThisLine* (pos: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, 0 <= pos <= s.text.Length() **)
	(** post: result <= pos, (pos < NextLine(result)) OR last-line(result) **)

	PROCEDURE (s: Setter) NextLine* (start: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, ThisLine(start) = start **)
	(** post: (result = 0) & (start = 0) OR
				(result = start) & last-line(start) OR
				(ThisLine(result - 1) = start) **)

	PROCEDURE (s: Setter) PreviousLine* (start: INTEGER): INTEGER, NEW, ABSTRACT;
	(** pre: connected, ThisLine(start) = start **)
	(** post: (result = 0) & (start = 0) OR (result = ThisLine(start - 1)) **)


	PROCEDURE (s: Setter) GetWord* (pos: INTEGER; OUT beg, end: INTEGER), NEW, ABSTRACT;
	(** pre: connected, 0 <= pos <= s.text.Length() **)
	(** post: c set, beg <= pos <= end **)

	PROCEDURE (s: Setter) GetLine* (start: INTEGER; OUT box: LineBox), NEW, ABSTRACT;
	(** pre: connected, ThisLine(start) = start, 0 <= start <= s.text.Length() **)
	(** post: (c, box) set (=> box.ruler # NIL), (box.len > 0) OR box.eot,
		 0 <= box.left <= box.right <= ruler.right **)

	PROCEDURE (s: Setter) GetBox* (start, end, maxW, maxH: INTEGER;
		OUT w, h: INTEGER
	), NEW, ABSTRACT;
	(** pre: connected, ThisLine(start) = start, 0 <= start <= end <= s.text.Length() **)
	(** post: c set, maxW > undefined => w <= maxW, maxH > undefined => h <= maxH **)


	PROCEDURE (s: Setter) NewReader* (old: Reader): Reader, NEW, ABSTRACT;
	(** pre: connected **)


	PROCEDURE (s: Setter) GridOffset* (dsc: INTEGER; IN box: LineBox): INTEGER, NEW, ABSTRACT;
	(** pre: connected, dsc >= 0: dsc is descender of previous line; dsc = -1 for first line **)
	(** post: dsc + GridOffset(dsc, box) + box.asc = k*ruler.grid (k >= 0) >= ruler.asc + ruler.grid **)


	(** Directory **)

	PROCEDURE (d: Directory) New* (): Setter, NEW, ABSTRACT;


	(* line box cache *)

	PROCEDURE InitCache;
		VAR i: INTEGER;
	BEGIN
		nextKey := 1; boxIndex := 0; seqIndex := 0;
		i := 0; WHILE i < boxCacheLen DO boxCache[i].key := -1; INC(i) END;
		i := 0; WHILE i < seqCacheLen DO seqCache[i].key := -1; INC(i) END
	END InitCache;

	PROCEDURE ClearCache (key: INTEGER);
		VAR i, j: INTEGER;
	BEGIN
		i := 0; j := boxIndex;
		WHILE i < boxCacheLen DO
			IF boxCache[i].key = key THEN boxCache[i].key := -1; j := i END;
			INC(i)
		END;
		boxIndex := j;
		i := 0; j := seqIndex;
		WHILE i < seqCacheLen DO
			IF seqCache[i].key = key THEN seqCache[i].key := -1; j := i END;
			INC(i)
		END;
		seqIndex := j
	END ClearCache;


	PROCEDURE CacheIndex (key, start: INTEGER): INTEGER;
		VAR i: INTEGER;
	BEGIN
RETURN -1;
		i := 0;
		WHILE (i < boxCacheLen) & ~((boxCache[i].key = key) & (boxCache[i].start = start)) DO
			INC(i)
		END;
		IF i = boxCacheLen THEN i := -1 END;
		RETURN i
	END CacheIndex;

	PROCEDURE GetFromCache (s: StdSetter; i: INTEGER; VAR l: LineBox);
	BEGIN
		l := boxCache[i].line;
		IF ~cachedRulers THEN
			IF l.rpos >= 0 THEN
				s.r := s.text.NewReader(s.r); s.r.SetPos(l.rpos); s.r.Read;
				l.ruler := s.r.view(TextRulers.Ruler)
			ELSE l.ruler := s.defRuler
			END
		END
	END GetFromCache;

	PROCEDURE AddToCache (key, start: INTEGER; VAR l: LineBox);
		VAR i: INTEGER;
	BEGIN
		i := boxIndex; boxIndex := (i + 1) MOD boxCacheLen;
		boxCache[i].key := key; boxCache[i].start := start; boxCache[i].line := l;
		IF ~cachedRulers THEN
			boxCache[i].line.ruler := NIL
		END
	END AddToCache;


	PROCEDURE CachedSeqStart (key, pos: INTEGER): INTEGER;
		VAR start: INTEGER; i: INTEGER;
	BEGIN
		i := 0;
		WHILE (i < seqCacheLen)
		& ~((seqCache[i].key = key) & (seqCache[i].start <= pos) & (pos <= seqCache[i].pos)) DO
			INC(i)
		END;
		IF i < seqCacheLen THEN start := seqCache[i].start ELSE start := -1 END;
		RETURN start
	END CachedSeqStart;

	PROCEDURE AddSeqStartToCache (key, pos, start: INTEGER);
		VAR i: INTEGER;
	BEGIN
		i := 0;
		WHILE (i < seqCacheLen) & ~((seqCache[i].key = key) & (seqCache[i].start = start)) DO
			INC(i)
		END;
		IF i < seqCacheLen THEN
			IF seqCache[i].pos < pos THEN seqCache[i].pos := pos END
		ELSE
			i := seqIndex; seqIndex := (i + 1) MOD seqCacheLen;
			seqCache[i].key := key; seqCache[i].pos := pos; seqCache[i].start := start
		END
	END AddSeqStartToCache;


	(* StdReader *)

	PROCEDURE WordPart (ch, ch1: CHAR): BOOLEAN;
	(* needs more work ... put elsewhere? *)
	BEGIN
		CASE ORD(ch) OF
		  ORD(digitspace), ORD(nbspace), ORD(nbhyphen): (*
		  , ORD("_"),
		  ORD("0") .. ORD("9"), ORD("A") .. ORD("Z"), ORD("a") .. ORD("z"),
		  0C0H .. 0C6H, 0E0H .. 0E6H,	(* ~ A *)
		  0C7H, 0E7H,	(* ~ C *)
		  0C8H .. 0CBH, 0E8H .. 0EBH,	(* ~ E *)
		  0CCH .. 0CFH, 0ECH .. 0EFH,	(* ~ I *)
		  0D1H, 0F1H,	(* ~ N *)
		  0D2H .. 0D6H, 0D8H, 0F2H .. 0F6H, 0F8H,	(* ~ O *)
		  0D9H .. 0DCH, 0F9H .. 0FCH,	(* ~ U *)
		  0DDH, 0FDH, 0FFH,	(* ~ Y *)
		  0DFH:	(* ~ ss *)
*)
			RETURN TRUE
		| ORD("."), ORD(":"):
			IF (ch = ".") & periodInWords OR (ch = ":") & colonInWords THEN
				CASE ch1 OF
				  0X, TextModels.viewcode, tab, line, para, " ":
					RETURN FALSE
				ELSE RETURN TRUE
				END
			ELSE RETURN FALSE
			END
(*
		ELSE RETURN FALSE 
*)
		ELSE RETURN i21sysCharacters.IsIdentChar(ch)
		END
	END WordPart;

(*
	PROCEDURE ExtendToEOL (x, right: INTEGER): INTEGER;
	BEGIN
		IF right - x > 5 * mm THEN RETURN right - x ELSE RETURN 5 * mm END
	END ExtendToEOL;
*)

	PROCEDURE Right (ra: TextRulers.Attributes; vw: INTEGER): INTEGER;
	BEGIN
		IF TextRulers.rightFixed IN ra.opts THEN
			RETURN ra.right
		ELSE
			RETURN vw
		END
	END Right;

	PROCEDURE GetViewPref (rd: StdReader);
		CONST maxH = 1600 * Ports.point;
		VAR ra: TextRulers.Attributes; tp: TextModels.Pref; sp: Pref;
	BEGIN
		ra := rd.ruler.style.attr;
		tp.opts := {}; Views.HandlePropMsg(rd.view, tp);
		rd.textOpts := tp.opts; rd.mask := tp.mask;
		sp.opts := {}; sp.dsc := ra.dsc; sp.endW := rd.w; Views.HandlePropMsg(rd.view, sp);
		rd.setterOpts := sp.opts; rd.dsc := sp.dsc; rd.endW := sp.endW;
		IF rd.w >= 10000 * mm THEN rd.w := 10000 * mm END;
		IF (TextModels.hideable IN tp.opts) & rd.hideMarks THEN
			rd.h := 0; sp.dsc := 0;
(*
rd.w := 0;
*)
			IF ~( (rd.view IS TextRulers.Ruler)
					OR (TextModels.maskChar IN rd.textOpts) & (rd.mask = para) ) THEN
				rd.w := 0
			END
(**)
		ELSIF rd.h > maxH THEN rd.h := maxH
		END;
		IF TextModels.maskChar IN rd.textOpts THEN
			IF rd.mask < 100X THEN rd.sString[0] := SHORT(rd.mask)
			ELSE rd.sString[0] := 0X; rd.string[0] := rd.mask; rd.string[1] := 0X
			END
		ELSE rd.sString[0] := TextModels.viewcode
		END
	END GetViewPref;

	PROCEDURE GatherString (rd: StdReader);
		VAR i, len: INTEGER; ch: CHAR;
	BEGIN
		i := 1; len := LEN(rd.sString) - 1; ch := rd.r.char;
		WHILE (i < len)
			& (rd.r.view = NIL) & (rd.r.attr = rd.attr)
			& (	 (" " < ch) & (ch <= "~") & (ch # "-")
				OR  (ch = digitspace)
				OR  (ch >= nbspace) & (ch < 100X) & (ch # softhyphen)
				)
		DO	(* rd.r.char > " " => ~rd.eot *)
			rd.sString[i] := SHORT(ch); INC(i);
			rd.eot := rd.r.eot;
			rd.r.Read; ch := rd.r.char; INC(rd.pos)
		END;
		rd.sString[i] := 0X; rd.setterOpts := {wordJoin};
		IF i = 1 THEN
			IF WordPart(rd.sString[0], 0X) THEN INCL(rd.setterOpts, wordPart) END
		END;
		rd.w := rd.attr.font.SStringWidth(rd.sString); rd.endW := rd.w
	END GatherString;

	PROCEDURE SpecialChar (rd: StdReader);
		VAR ra: TextRulers.Attributes; i, tabs, spaceW, dW: INTEGER; type: SET;
	BEGIN
		ra := rd.ruler.style.attr;
		CASE ORD(rd.sString[0]) OF
		  ORD(tab):
			rd.textOpts := {TextModels.hideable};
			rd.endW := minTabWidth;
			rd.adjStart := rd.pos; rd.spaces := 0;
			(*
			i := 0; WHILE (i < ra.tabs.len) & (ra.tabs.tab[i].stop < rd.x + minTabWidth) DO INC(i) END;
			*)
			i := rd.tabIndex + 1;
			IF i < ra.tabs.len THEN
				type := ra.tabs.tab[i].type;
				rd.w := MAX(minTabWidth, ra.tabs.tab[i].stop - rd.x);
				IF TextRulers.barTab IN type THEN
					IF TextRulers.rightTab IN type THEN
						rd.w := MAX(minTabWidth, rd.w - leftLineGap)
					ELSIF ~(TextRulers.centerTab IN type) THEN
						INC(rd.w, rightLineGap)
					END
				END;
				rd.tabIndex := i; rd.tabType := type
			ELSE	(* for "reasonable" fonts: round to closest multiple of spaces of this font *)
				spaceW := rd.attr.font.SStringWidth(" ");
				IF (1 <= spaceW) & (spaceW <= stdTabWidth) THEN
					rd.w := (stdTabWidth + spaceW DIV 2) DIV spaceW * spaceW
				ELSE
					rd.w := stdTabWidth
				END;
				rd.tabIndex := TextRulers.maxTabs; rd.tabType := {}
			END
		| ORD(line):
			rd.setterOpts := {lineBreak}; rd.w := 0; rd.endW := 0
		| ORD(para):
(*
			IF rd.hideMarks THEN
				rd.w := 0; rd.h := 0; rd.dsc := 0
			ELSE
				rd.w := ExtendToEOL(rd.x, Right(ra, rd.vw)) + 1
			END;
			INC(rd.h, ra.lead);
			rd.textOpts := {TextModels.hideable};
			rd.endW := rd.w
*)
(*
			rd.setterOpts := {lineBreak};
*)
			IF rd.hideMarks THEN rd.h := 0; rd.dsc := 0 END;
			INC(rd.h, ra.lead); rd.textOpts := {TextModels.hideable};
			IF (rd.view = NIL) OR ~(rd.view IS TextRulers.Ruler) THEN
				rd.w := 10000 * Ports.mm (* ExtendToEOL(rd.x, Right(ra, rd.vw)) + 1 *)
			END;
			rd.endW := rd.w
(**)
		| ORD(" "):
			rd.setterOpts := {flexWidth};
			rd.w := rd.attr.font.SStringWidth(rd.sString); rd.endW := 0; INC(rd.spaces)
		| ORD(zwspace):
			rd.w := 0; rd.endW := 0
		| ORD(digitspace):
			rd.setterOpts := {wordPart};
			rd.w := rd.attr.font.StringWidth("0"); rd.endW := rd.w
		| ORD("-"):
			rd.setterOpts := {};
			rd.w := rd.attr.font.StringWidth("-"); rd.endW := rd.w
		| ORD(hyphen):
			rd.setterOpts := {};
			rd.sString[0] := "-" (*softhyphen*);
			rd.w := rd.attr.font.StringWidth("-" (*softhyphen*)); rd.endW := rd.w
		| ORD(nbhyphen):
			rd.setterOpts := {wordJoin, wordPart};
			rd.sString[0] := "-" (*softhyphen*);
			rd.w := rd.attr.font.StringWidth("-" (*softhyphen*)); rd.endW := rd.w
		| ORD(softhyphen):
			rd.setterOpts := {wordPart}; rd.textOpts := {TextModels.hideable};
			rd.sString[0] := "-";
			rd.endW := rd.attr.font.StringWidth("-" (*softhyphen*));
			IF rd.hideMarks THEN rd.w := 0 ELSE rd.w := rd.endW END
		ELSE
			rd.setterOpts := {wordJoin};
			IF WordPart(rd.sString[0], rd.r.char) THEN INCL(rd.setterOpts, wordPart) END;
			rd.w := rd.attr.font.SStringWidth(rd.sString); rd.endW := rd.w
		END
	END SpecialChar;

	PROCEDURE LongChar (rd: StdReader);
		VAR ra: TextRulers.Attributes;
	BEGIN
		ra := rd.ruler.style.attr;
		rd.setterOpts := {wordJoin, wordPart};
		rd.w := rd.attr.font.StringWidth(rd.string); rd.endW := rd.w
	END LongChar;


	PROCEDURE (rd: StdReader) Read;
	(* pre: connected *)
		VAR ra: TextRulers.Attributes; asc, dsc, w: INTEGER; ch: CHAR;
	BEGIN
		rd.Read^;
		IF ~rd.eot THEN
			IF rd.view = NIL THEN
				rd.attr.font.GetBounds(asc, dsc, w);
				rd.h := asc + dsc; rd.dsc := dsc
			ELSE
				GetViewPref(rd)
			END;
			IF (rd.view = NIL) OR (TextModels.maskChar IN rd.textOpts) THEN
				IF rd.string[0] # 0X THEN
					LongChar(rd)
				ELSE
					ch := rd.sString[0];
					IF (rd.view = NIL)
					 & (	(" " < ch) & (ch < "~") & (ch # "-")
					 	OR (ch = digitspace)
					 	OR (ch >= nbspace) & (ch # softhyphen)
					  	)
					THEN
						GatherString(rd)
					ELSE
						SpecialChar(rd)
					END
				END
			END
		ELSE
			ra := rd.ruler.style.attr;
			rd.w := 0; rd.endW := 0; rd.h := ra.asc + ra.dsc; rd.dsc := ra.dsc
		END
	END Read;

	PROCEDURE (rd: StdReader) AdjustWidth (start, pos: INTEGER; IN box: LineBox; VAR w: INTEGER);
		VAR i: INTEGER; form: SET;
	BEGIN
		IF box.adj & (pos >= start + box.adjOff) THEN
			form := box.ruler.style.attr.opts * adjustMask;
			IF (form = blocked) & (rd.sString[0] = " ") THEN
				INC(w, box.adjW DIV box.spaces)
			ELSIF (form # blocked) & (rd.sString[0] = tab) THEN
				INC(w, box.adjW)	(* is this correct ??? *)
			END
		END;
		i := rd.tabIndex;	(* rd.sString[0] = tab  =>  i >= 0 *)
		IF (rd.sString[0] = tab) & (i < box.ruler.style.attr.tabs.len) THEN
			w := box.tabW[i]
		END
	END AdjustWidth;

	PROCEDURE (rd: StdReader) SplitWidth (w: INTEGER): INTEGER;
	BEGIN
		IF (rd.sString[1] = 0X) & (rd.view = NIL) THEN
			RETURN (w + 1) DIV 2
		ELSE RETURN w
		END
	END SplitWidth;


	(* Worder *)

	PROCEDURE SetWorder (VAR w: Worder; s: StdSetter; pos: INTEGER; OUT start: INTEGER);
		CONST wordCutoff = LEN(s.rd.sString);
	BEGIN
		start := s.ThisSequence(pos);
		IF pos - start >= wordCutoff THEN
			start := pos; WHILE pos - start < wordCutoff DO start := s.PreviousLine(start) END
		END;
		s.GetLine(start, w.box); w.next := start + w.box.len;
		s.rd.Set(s.r, s.text, w.box.left, start, w.box.ruler, w.box.rpos, s.vw, s.hideMarks);
		w.i := 0; s.rd.sString[0] := 0X
	END SetWorder;

	PROCEDURE StepWorder (VAR w: Worder; s: StdSetter; VAR part: BOOLEAN);
		VAR rd: Reader;
	BEGIN
		rd := s.rd;
		IF rd.sString[w.i] = 0X THEN
			IF rd.pos < w.next THEN
				rd.Read; w.i := 0
			ELSE
				IF ~w.box.eot THEN
					s.GetLine(w.next, w.box);
					s.rd.Set(s.r, s.text, w.box.left, w.next, w.box.ruler, w.box.rpos, s.vw, s.hideMarks);
					rd.Read; w.i := 0;
					INC(w.next, w.box.len)
				ELSE
					rd.sString[0] := 0X
				END
			END
		END;
		IF rd.sString[0] = 0X THEN	(* end of text *)
			part := TRUE
		ELSIF rd.sString[1] = 0X THEN	(* special character *)
			part := wordPart IN rd.setterOpts; INC(w.i)
		ELSE	(* gathered sString *)
			part := WordPart(rd.sString[w.i], rd.sString[w.i + 1]); INC(w.i)
		END
	END StepWorder;


	(* StdSetter *)

	PROCEDURE (s: StdSetter) CopyFrom (source: Stores.Store);
	BEGIN
		s.CopyFrom^(source);
		WITH source: StdSetter DO
			s.ruler := source.ruler; s.rpos := source.rpos; s.key := source.key;
			s.rd := NIL; s.r := NIL
		END
	END CopyFrom;

	PROCEDURE (s: StdSetter) Externalize (VAR wr: Stores.Writer);
	BEGIN
		s.Externalize^(wr);
		wr.WriteVersion(maxStdVersion)
	END Externalize;

	PROCEDURE (s: StdSetter) Internalize (VAR rd: Stores.Reader);
		VAR thisVersion: INTEGER;
	BEGIN
		s.Internalize^(rd);
		IF rd.cancelled THEN RETURN END;
		rd.ReadVersion(minVersion, maxStdVersion, thisVersion);
		IF rd.cancelled THEN RETURN END;
		s.text := NIL; s.defRuler := NIL; s.ruler := NIL; s.rd := NIL; s.r := NIL
	END Internalize;


	PROCEDURE (s: StdSetter) ConnectTo (text: TextModels.Model;
		defRuler: TextRulers.Ruler; vw: INTEGER; hideMarks: BOOLEAN
	);
	BEGIN
		s.ConnectTo^(text, defRuler, vw, hideMarks);
		ClearCache(s.key);
		IF text # NIL THEN
			s.ruler := defRuler; s.rpos := -1; s.key := nextKey; INC(nextKey)
		ELSE
			s.ruler := NIL
		END
	END ConnectTo;


	PROCEDURE (s: StdSetter) ThisPage (pageH: INTEGER; pageNo: INTEGER): INTEGER;
	(* pre: connected, 0 <= pageNo *)
	(* post: (result = -1) & (pageNo >= maxPageNo) OR (result = pageStart(pageNo)) *)
		VAR start, prev: INTEGER;
	BEGIN
		ASSERT(s.text # NIL, 20); ASSERT(pageNo >= 0, 21);
		start := 0;
		WHILE pageNo > 0 DO
			prev := start; DEC(pageNo); start := s.NextPage(pageH, start);
			IF start = prev THEN start := -1; pageNo := 0 END
		END;
		RETURN start
	END ThisPage;

	PROCEDURE (s: StdSetter) NextPage (pageH: INTEGER; start: INTEGER): INTEGER;
	(* pre: connected, ThisPage(pageH, x) = start *)
	(* post: (result = s.text.Length()) OR result = next-pageStart(start) *)
		CONST
			noBreakInside = TextRulers.noBreakInside;
			pageBreak = TextRulers.pageBreak;
			parJoin = TextRulers.parJoin;
			regular = 0; protectInside = 1; joinFirst = 2; joinNext = 3; confirmSpace = 4;	(* state *)
		VAR
			box: LineBox; ra: TextRulers.Attributes;
			h, asc, dsc, backup, pos, state: INTEGER; isRuler: BOOLEAN;

		PROCEDURE FetchNextLine;
		BEGIN
			s.GetLine(pos, box);
			IF box.len > 0 THEN
				ra := box.ruler.style.attr; isRuler := box.rpos = pos;
				asc := box.asc + s.GridOffset(dsc, box); dsc := box.dsc; h := asc + dsc
			END
		END FetchNextLine;

		PROCEDURE HandleRuler;
			CONST norm = 0; nbi = 1; pj = 2;
			VAR strength: INTEGER;
		BEGIN
			IF isRuler & (pos > start) & ~(pageBreak IN ra.opts) THEN
				IF parJoin IN ra.opts THEN strength := pj
				ELSIF noBreakInside IN ra.opts THEN strength := nbi
				ELSE strength := norm
				END;
				CASE state OF
				| regular:
					CASE strength OF
					| norm:
					| nbi: state := protectInside; backup := pos
					| pj: state := joinFirst; backup := pos
					END
				| protectInside:
					CASE strength OF
					| norm: state := regular
					| nbi: backup := pos
					| pj: state := joinFirst; backup := pos
					END
				| joinFirst:
					CASE strength OF
					| norm: state := confirmSpace
					| nbi: state := protectInside
					| pj: state := joinNext
					END
				| joinNext:
					CASE strength OF
					| norm: state := confirmSpace
					| nbi: state := protectInside
					| pj:
					END
				| confirmSpace:
					CASE strength OF
					| norm: state := regular
					| nbi: state := protectInside; backup := pos
					| pj: state := joinFirst; backup := pos
					END
				END
			END
		END HandleRuler;

		PROCEDURE IsEmptyLine (): BOOLEAN;
		BEGIN
			RETURN (box.right = box.left)  OR  s.hideMarks & isRuler & ~(pageBreak IN ra.opts)
		END IsEmptyLine;

	BEGIN
		ASSERT(s.text # NIL, 20);
		ASSERT(0 <= start, 21); ASSERT(start <= s.text.Length(), 22);
		pos := start; dsc := -1;
		FetchNextLine;
		IF box.len > 0 THEN
			state := regular;
			REPEAT	(* at least one line per page *)
				HandleRuler; DEC(pageH, h); INC(pos, box.len);
				IF (state = confirmSpace) & ~IsEmptyLine() THEN state := regular END;
				FetchNextLine
			UNTIL (box.len = 0)  OR  (pageH - h < 0)  OR  isRuler & (pageBreak IN ra.opts);
			IF ~isRuler OR ~(pageBreak IN ra.opts) THEN
				WHILE (box.len > 0) & IsEmptyLine() DO	(* skip empty lines at top of page *)
					HandleRuler; INC(pos, box.len); FetchNextLine
				END
			END;
			HandleRuler;
			IF (state # regular) & ~(isRuler & (pageBreak IN ra.opts) OR (box.len = 0)) THEN pos := backup END
		END;
		RETURN pos
	END NextPage;


	PROCEDURE (s: StdSetter) NextSequence (start: INTEGER): INTEGER;
	(* pre: connected, ThisSequence(start) = start *)
	(* post: (result = start) & last-line(start) OR (ThisSequence(t, result - 1) = start) *)
		VAR rd: TextModels.Reader; ch: CHAR;
	BEGIN
		ASSERT(s.text # NIL, 20);
		s.r := s.text.NewReader(s.r); rd := s.r; rd.SetPos(start);
		REPEAT rd.ReadChar(ch) UNTIL rd.eot OR (ch = line) OR (ch = para);
		IF rd.eot THEN RETURN start ELSE RETURN rd.Pos() END
	END NextSequence;

	PROCEDURE (s: StdSetter) ThisSequence (pos: INTEGER): INTEGER;
	(* pre: connected, 0 <= pos <= t.Length() *)
	(* post: (result = 0) OR (char(result - 1) IN {line, para}) *)
		VAR rd: TextModels.Reader; start, limit: INTEGER; ch: CHAR;
	BEGIN
		ASSERT(s.text # NIL, 20); ASSERT(0 <= pos, 21); ASSERT(pos <= s.text.Length(), 22);
		IF pos = 0 THEN
			RETURN 0
		ELSE
			start := CachedSeqStart(s.key, pos);
			IF start < 0 THEN
				s.r := s.text.NewReader(s.r); rd := s.r; rd.SetPos(pos);
				limit := paraShutoff;
				REPEAT rd.ReadPrevChar(ch); DEC(limit)
				UNTIL rd.eot OR (ch = line) OR (ch = para) OR (limit = 0);
				IF rd.eot THEN start := 0 ELSE start := rd.Pos() + 1 END;
				AddSeqStartToCache(s.key, pos, start)
			END;
			RETURN start
		END
	END ThisSequence;

	PROCEDURE (s: StdSetter) PreviousSequence (start: INTEGER): INTEGER;
	(* pre: connected, ThisSequence(t, start) = start *) 
	(* post: (result = 0) & (start = 0) OR (result = ThisSequence(t, start - 1)) *)
	BEGIN
		IF start <= 1 THEN RETURN 0 ELSE RETURN s.ThisSequence(start - 1) END
	END PreviousSequence;


	PROCEDURE (s: StdSetter) ThisLine (pos: INTEGER): INTEGER;
	(* pre: connected *)
		VAR start, next: INTEGER;
	BEGIN
		next := s.ThisSequence(pos);
		REPEAT start := next; next := s.NextLine(start) UNTIL (next > pos) OR (next = start);
		RETURN start
	END ThisLine;

	PROCEDURE (s: StdSetter) NextLine (start: INTEGER): INTEGER;
	(* pre: connected, ThisLine(start) = start *)
	(* post: (result = 0) & (start = 0) OR
				(result = start) & last-line(start) OR
				(ThisLine(result - 1) = start) *)
		VAR box: LineBox; len: INTEGER; i: INTEGER; eot: BOOLEAN;
	BEGIN
		i := CacheIndex(s.key, start);
		IF i >= 0 THEN
			len := boxCache[i].line.len; eot := boxCache[i].line.eot
		ELSE
			s.GetLine(start, box); len := box.len; eot := box.eot
		END;
		IF ~eot THEN RETURN start + len ELSE RETURN start END
	END NextLine;

	PROCEDURE (s: StdSetter) PreviousLine (start: INTEGER): INTEGER;
	(* pre: connected, ThisLine(start) = start *)
	(* post: (result = 0) & (start = 0) OR (result = ThisLine(start - 1)) *)
	BEGIN
		IF start <= 1 THEN start := 0 ELSE start := s.ThisLine(start - 1) END;
		RETURN start
	END PreviousLine;


	PROCEDURE (s: StdSetter) GetWord (pos: INTEGER; OUT beg, end: INTEGER);
	(* pre: connected, 0 <= pos <= s.text.Length() *)
	(* post: beg <= pos <= end *)
		CONST wordCutoff = LEN(s.rd.sString);
		VAR w: Worder; part: BOOLEAN;
	BEGIN
		ASSERT(s.text # NIL, 20); ASSERT(0 <= pos, 21); ASSERT(pos <= s.text.Length(), 22);
		SetWorder(w, s, pos, beg); end := beg;
		REPEAT
			StepWorder(w, s, part); INC(end);
			IF ~part THEN beg := end END
		UNTIL end >= pos;
		DEC(end);
		REPEAT
			StepWorder(w, s, part); INC(end)
		UNTIL ~part OR (s.rd.sString[0] = 0X) OR (end - beg > wordCutoff)
	END GetWord;

	PROCEDURE (s: StdSetter) GetLine (start: INTEGER; OUT box: LineBox);
		VAR rd: Reader; ra: TextRulers.Attributes; brk: LineBox;
			d, off, right, w: INTEGER; i, tabsN: INTEGER; form: SET; adj: BOOLEAN; ch: CHAR;

		PROCEDURE TrueW (VAR b: LineBox; w: INTEGER): INTEGER;
			VAR i: INTEGER; type: SET;
		BEGIN
			i := rd.tabIndex;
			IF (0 <= i ) & (i < TextRulers.maxTabs) & (rd.sString[0] # tab) THEN
				type := rd.tabType * {TextRulers.centerTab, TextRulers.rightTab};
				IF type = {TextRulers.centerTab} THEN
					DEC(w, b.tabW[i] - MAX(minTabWidth, b.tabW[i] - w DIV 2))
				ELSIF type = {TextRulers.rightTab} THEN
					DEC(w, b.tabW[i] - MAX(minTabWidth, b.tabW[i] - w))
				END
			END;
			RETURN w
		END TrueW;

		PROCEDURE Enclose (VAR b: LineBox; w: INTEGER);
			VAR off, i, d: INTEGER; type: SET;
		BEGIN
			b.len := rd.pos - start; INC(b.right, w);
			off := rd.attr.offset; i := rd.tabIndex;
			IF rd.h - rd.dsc + off > b.asc THEN b.asc := rd.h - rd.dsc + off END;
			IF rd.dsc - off > b.dsc THEN b.dsc := rd.dsc - off END;
			IF rd.view # NIL THEN b.views := TRUE END;
			IF (0 <= i ) & (i < TextRulers.maxTabs) THEN
				IF rd.sString[0] = tab THEN
					b.tabW[i] := w
				ELSE
					type := rd.tabType * {TextRulers.centerTab, TextRulers.rightTab};
					IF type = {TextRulers.centerTab} THEN
						d := b.tabW[i] - MAX(minTabWidth, b.tabW[i] - w DIV 2);
						DEC(b.tabW[i], d); DEC(b.right, d)
					ELSIF type = {TextRulers.rightTab} THEN
						d := b.tabW[i] - MAX(minTabWidth, b.tabW[i] - w);
						DEC(b.tabW[i], d); DEC(b.right, d)
					END
				END
			END
		END Enclose;

	BEGIN
		ASSERT(s.text # NIL, 20); ASSERT(0 <= start, 21); ASSERT(start <= s.text.Length(), 22);
		i := CacheIndex(s.key, start);
		IF i >= 0 THEN
			GetFromCache(s, i, box)
		ELSE
			TextRulers.GetValidRuler(s.text, start, s.rpos, s.ruler, s.rpos);
			IF s.rpos > start THEN s.ruler := s.defRuler; s.rpos := -1 END;
			box.ruler := s.ruler; box.rpos := s.rpos;
			ra := s.ruler.style.attr; tabsN := ra.tabs.len; right := Right(ra, s.vw);
			s.r := s.text.NewReader(s.r);
			IF start = 0 THEN s.r.SetPos(start); ch := para
			ELSE s.r.SetPos(start - 1); s.r.ReadChar(ch)
			END;
			s.r.Read;

(*
			IF s.r.char = para THEN box.rbox := ~s.hideMarks; box.bop := s.hideMarks; box.left := 0
			ELSIF ch = para THEN box.rbox := FALSE; box.bop := TRUE; box.left := ra.first
			ELSE box.rbox := FALSE; box.bop := FALSE; box.left := ra.left
			END;
*)
			IF s.r.char = para THEN box.rbox := TRUE; box.bop := FALSE; box.left := 0
			ELSIF ch = para THEN box.rbox := FALSE; box.bop := TRUE; box.left := ra.first
			ELSE box.rbox := FALSE; box.bop := FALSE; box.left := ra.left
			END;
(**)
			box.views := FALSE;
			box.asc := 0; box.dsc := 0; box.right := box.left;
			box.len := 0; box.adjOff := 0; box.spaces := 0;
			brk.right := 0;
	
			s.rd := s.NewReader(s.rd); rd := s.rd;
			rd.Set(s.r, s.text, box.left, start, box.ruler, box.rpos, s.vw, s.hideMarks);
			rd.Read;
			WHILE ~rd.eot & (box.right + (*rd.w*) TrueW(box, rd.w) <= right)
			& ~(lineBreak IN rd.setterOpts) DO
				IF ~(wordJoin IN rd.setterOpts) & (box.right + rd.endW <= right) THEN
					(*brk := box;*)
					brk.len := box.len; brk.ruler := box.ruler; brk.rpos := box.rpos;
					brk.left := box.left; brk.right := box.right; brk.asc := box.asc; brk.dsc := box.dsc;
					brk.rbox := box.rbox; brk.bop := box.bop; brk.adj := box.adj; brk.eot := box.eot;
					brk.views := box.views; brk.skipOff := box.skipOff; brk.adjOff := box.adjOff;
					brk.spaces := box.spaces; brk.adjW := box.adjW;
					i := 0; WHILE i < tabsN DO brk.tabW[i] := box.tabW[i]; INC(i) END;
					(*---*)
					Enclose(brk, rd.endW);
					brk.eot := rd.r.eot	(* rd.r.eot one ahead of rd.eot *)
				END;
				box.adjOff := rd.adjStart - start; box.spaces := rd.spaces;
				Enclose(box, rd.w);
				rd.x := box.right; rd.Read
			END;
			IF (lineBreak IN rd.setterOpts) (* & ~box.rbox *) THEN Enclose(box, 0) END;
			box.eot := rd.eot; adj := FALSE; box.skipOff := box.len;
			IF box.right + rd.w > right THEN	(* rd.w > 0 => ~rd.eot & ~(lineBreak IN setterOpts) *)
				IF ~(wordJoin IN rd.setterOpts) & (box.right + rd.endW <= right) THEN
					IF rd.sString[0] = " " THEN DEC(box.spaces) END;
					Enclose(box, rd.endW);
					adj := TRUE
				ELSIF brk.right > 0 THEN
					(*box := brk;*)
					box.len := brk.len; box.ruler := brk.ruler; box.rpos := brk.rpos;
					box.left := brk.left; box.right := brk.right; box.asc := brk.asc; box.dsc := brk.dsc;
					box.rbox := brk.rbox; box.bop := brk.bop; box.adj := brk.adj; box.eot := brk.eot;
					box.views := brk.views; box.skipOff := brk.skipOff; box.adjOff := brk.adjOff;
					box.spaces := brk.spaces; box.adjW := brk.adjW;
					i := 0; WHILE i < tabsN DO box.tabW[i] := brk.tabW[i]; INC(i) END;
					(*---*)
					box.skipOff := box.len - 1; adj := TRUE
				ELSIF box.right = box.left THEN
					Enclose(box, rd.w)	(* force at least one per line *)
				END
			ELSIF (box.right = box.left) & box.eot THEN
				box.asc := ra.asc; box.dsc := ra.dsc	(* force empty line to ruler's default height *)
			END;
	
			box.adj := FALSE;
			d := right - box.right;
			IF d > 0 THEN
				form := ra.opts * adjustMask;
				IF form = blocked THEN
					IF adj & (box.spaces > 0) THEN
						box.right := right; box.adj := TRUE; box.adjW := d
					END
				ELSIF form = rightFlush THEN
					IF box.adjOff > 0 THEN
						box.adjW := d; box.adj := TRUE
					ELSE
						INC(box.left, d)
					END;
					box.right := right
				ELSIF form = centered THEN
					IF box.adjOff > 0 THEN
						box.adjW := d DIV 2; box.adj := TRUE
					ELSE
						INC(box.left, d DIV 2)
					END;
					INC(box.right, d DIV 2)
				END
			END;

			AddToCache(s.key, start, box)
		END;

		ASSERT(box.eot OR (box.len > 0), 100)
	END GetLine;


	PROCEDURE (s: StdSetter) GetBox (start, end, maxW, maxH: INTEGER; OUT w, h: INTEGER);
		VAR box: LineBox; asc, dsc: INTEGER;
	BEGIN
		ASSERT(s.text # NIL, 20);
		ASSERT(0 <= start, 21);
		ASSERT(start <= end, 22);
		ASSERT(end <= s.text.Length(), 23);
		w := 0; h := 0; dsc := -1;
		IF maxW <= Views.undefined THEN maxW := MAX(INTEGER) END;
		IF maxH <= Views.undefined THEN maxH := MAX(INTEGER) END;
		WHILE (start < end) & (h < maxH) DO
			s.GetLine(start, box);
			IF box.rbox THEN w := MAX(w, Right(box.ruler.style.attr, s.vw))
			ELSE w := MAX(w, box.right)
			END;
			asc := box.asc + s.GridOffset(dsc, box); dsc := box.dsc;
			INC(start, box.len); INC(h, asc + dsc)
		END;
		w := MIN(w, maxW); h := MIN(h, maxH)
	END GetBox;


	PROCEDURE (s: StdSetter) NewReader (old: Reader): Reader;
	(* pre: connected *)
		VAR rd: StdReader;
	BEGIN
		ASSERT(s.text # NIL, 20);
		IF (old # NIL) & (old IS StdReader) THEN RETURN old
		ELSE NEW(rd); RETURN rd
		END
	END NewReader;


	PROCEDURE (s: StdSetter) GridOffset (dsc: INTEGER; IN box: LineBox): INTEGER;
		VAR ra: TextRulers.Attributes; h, h0: INTEGER;
		(* minimal possible line spacing h0, minimal legal line spacing h *)
	BEGIN
		IF ~box.rbox THEN
			ra := box.ruler.style.attr;
			IF dsc < 0 THEN
RETURN 0	(* no longer try to correct first line's grid position -- should be done when printing... *)
(*
				h0 := box.asc; h := ra.asc;
				IF h < h0 THEN	(* override legal spacing if to small *)
					h := h - (h - h0) DIV ra.grid * ra.grid	(* adjust to next larger grid line *)
				END;
				RETURN h - h0
*)
			ELSE
				h0 := box.asc + dsc; h := ra.asc + ra.dsc;
				IF h < h0 THEN h := h0 END;	(* override legal spacing if to small *)
				RETURN - (-h) DIV ra.grid * ra.grid - h0	(* adjust to next larger grid line *)
			END
		ELSE
			RETURN 0
		END
	END GridOffset;


	(* StdDirectory *)

	PROCEDURE (d: StdDirectory) New (): Setter;
		VAR s: StdSetter;
	BEGIN
		NEW(s); s.text := NIL; RETURN s
	END New;


	(** miscellaneous **)

	PROCEDURE Init;
		VAR d: StdDirectory;
	BEGIN
		InitCache;
		NEW(d); dir := d; stdDir := d
	END Init;

	PROCEDURE SetDir* (d: Directory);
	BEGIN
		ASSERT(d # NIL, 20); dir := d
	END SetDir;

BEGIN
	Init
END TextSetters.