Host/Mod/TextConv.odc

MODULE HostTextConv; (* 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
	- 20080523, Борис Рюмшин, коррекция русификации
	- 20060407, Ilya Ermakov, russification
	- YYYYMMDD, nn, ...
	"
	issues	= "
	- ...
	"

**)

	IMPORT
		SYSTEM, KERNEL32, GDI32, USER32, COM, WinApi, WinOle,
		Files, Fonts, Ports, Stores, Views, Dialog, Converters, Properties,
		HostFonts, HostWindows, HostClipboard, TextModels,
		TextRulers, TextViews, TextMappers, i21sysCharacters;
	
	CONST
		CR = 0DX; LF = 0AX; FF = 0EX; TAB = 09X;
		halfpoint = Ports.point DIV 2;
		twips = Ports.point DIV 20;

	TYPE
		Context = POINTER TO RECORD
			next: Context;
			dest: INTEGER;
			attr: TextModels.Attributes
		END;
		MemReader = POINTER TO RECORD (Files.Reader)
			adr, pos: INTEGER
		END;
	
	VAR 
		debug*: BOOLEAN;
		

	(* MemReader *)
	
	PROCEDURE (r: MemReader) Base (): Files.File;
	BEGIN
		RETURN NIL
	END Base;
	
	PROCEDURE (r: MemReader) Pos (): INTEGER;
	BEGIN
		RETURN r.pos
	END Pos;
	
	PROCEDURE (r: MemReader) SetPos (pos: INTEGER);
	BEGIN
		r.pos := pos
	END SetPos;
	
	PROCEDURE (r: MemReader) ReadByte (OUT x: BYTE);
	BEGIN
		SYSTEM.GET(r.adr + r.pos, x); INC(r.pos)
	END ReadByte;
	
	PROCEDURE (r: MemReader) ReadBytes (VAR x: ARRAY OF BYTE; beg, len: INTEGER);
	BEGIN
		HALT(126)
	END ReadBytes;
	
	
	PROCEDURE GenGlobalMedium (hg: WinApi.HGLOBAL; unk: COM.IUnknown; VAR sm: WinOle.STGMEDIUM);
	BEGIN
		sm.tymed := WinOle.TYMED_HGLOBAL;
		sm.u.hGlobal := hg;
		sm.pUnkForRelease := unk
	END GenGlobalMedium;
	
	PROCEDURE MediumGlobal (VAR sm: WinOle.STGMEDIUM): WinApi.HGLOBAL;
	BEGIN
		ASSERT(sm.tymed = WinOle.TYMED_HGLOBAL, 20);
		RETURN sm.u.hGlobal
	END MediumGlobal;
	
	
	PROCEDURE WriteWndChar (wr: TextModels.Writer; ch: SHORTCHAR);
	BEGIN
		CASE ch OF
		| CR, TAB, " "..7EX, 0A0X..0FFX: wr.WriteChar(ch)
		| LF:
		| 80X: wr.WriteChar(20ACX)	(* euro *)
		| 82X: wr.WriteChar(201AX)
		| 83X: wr.WriteChar(0192X)
		| 84X: wr.WriteChar(201EX)
		| 85X: wr.WriteChar(2026X)
		| 86X: wr.WriteChar(2020X)
		| 87X: wr.WriteChar(2021X)
		| 88X: wr.WriteChar(02C6X)
		| 89X: wr.WriteChar(2030X)
		| 8AX: wr.WriteChar(0160X)
		| 8BX: wr.WriteChar(2039X)
		| 8CX: wr.WriteChar(0152X)
		| 91X: wr.WriteChar(2018X)
		| 92X: wr.WriteChar(2019X)
		| 93X: wr.WriteChar(201CX)
		| 94X: wr.WriteChar(201DX)
		| 95X: wr.WriteChar(2022X)
		| 96X: wr.WriteChar(2013X)
		| 97X: wr.WriteChar(2014X)
		| 98X: wr.WriteChar(02DCX)
		| 99X: wr.WriteChar(2122X)
		| 9AX: wr.WriteChar(0161X)
		| 9BX: wr.WriteChar(203AX)
		| 9CX: wr.WriteChar(0153X)
		| 9FX: wr.WriteChar(0178X)
		| 0X..8X, 0BX, 0CX, 0EX..1FX, 7FX, 81X, 8DX..90X, 9DX, 9EX:
			wr.WriteChar(CHR(0EF00H + ORD(ch)))
		END
	END WriteWndChar;
	
	PROCEDURE ThisWndChar (ch: CHAR): SHORTCHAR;
	BEGIN
		ch := i21sysCharacters.ExtendedAsciiOf(ch);
		IF ch >= 100X THEN
			IF (ch >= 0EF00X) & (ch <= 0EFFFX) THEN ch := CHR(ORD(ch) - 0EF00H)
			ELSIF ch =  20ACX THEN ch := 80X	(* euro *)
			ELSIF ch =  201AX THEN ch := 82X
			ELSIF ch =  0192X THEN ch := 83X
			ELSIF ch =  201EX THEN ch := 84X
			ELSIF ch =  2026X THEN ch := 85X
			ELSIF ch =  2020X THEN ch := 86X
			ELSIF ch =  2021X THEN ch := 87X
			ELSIF ch =  02C6X THEN ch := 88X
			ELSIF ch =  2030X THEN ch := 89X
			ELSIF ch =  0160X THEN ch := 8AX
			ELSIF ch =  2039X THEN ch := 8BX
			ELSIF ch =  0152X THEN ch := 8CX
			ELSIF ch =  2018X THEN ch := 91X
			ELSIF ch =  2019X THEN ch := 92X
			ELSIF ch =  201CX THEN ch := 93X
			ELSIF ch =  201DX THEN ch := 94X
			ELSIF ch =  2022X THEN ch := 95X
			ELSIF ch =  2013X THEN ch := 96X
			ELSIF ch =  2014X THEN ch := 97X
			ELSIF ch =  02DCX THEN ch := 98X
			ELSIF ch =  2122X THEN ch := 99X
			ELSIF ch =  0161X THEN ch := 9AX
			ELSIF ch =  203AX THEN ch := 9BX
			ELSIF ch =  0153X THEN ch := 9CX
			ELSIF ch =  0178X THEN ch := 9FX
			ELSE ch := "?"
			END
		ELSIF ch = 08FX THEN ch := " "	(* digit space *)
		END;
		RETURN SHORT(ch)
	END ThisWndChar;
	
	PROCEDURE ParseRichText (rd: Files.Reader; wr: TextModels.Writer; VAR defRuler: TextRulers.Ruler);
		TYPE 
			FontInfo = POINTER TO RECORD id: INTEGER; f: Fonts.Typeface; next: FontInfo END;
			ColorInfo = POINTER TO RECORD id: INTEGER; c: Ports.Color; next: ColorInfo END;
		CONST text = 0; fonttab = 1; colortab = 2; skip = 3;
		VAR ch: SHORTCHAR; fact, val, defFont, dest, idx, fnum, cnum, bpos, spos, paraPos, i: INTEGER;
			fonts, font: FontInfo; colors: ColorInfo; hasNum, remPar: BOOLEAN;
			f: Fonts.Font; comm: ARRAY 32 OF SHORTCHAR; c, con: Context; ruler: TextRulers.Ruler;
			p0: Properties.Property; p: TextRulers.Prop; attr: TextRulers.Attributes; tabStyle: SET;
			
		PROCEDURE Color(i: INTEGER): ColorInfo;
			VAR c: ColorInfo;
		BEGIN
			ASSERT(colors # NIL, 20);
			c := colors;
			WHILE (c # NIL) & (c.id # i) DO c := c.next END;
			ASSERT(c # NIL, 100);
			RETURN c
		END Color;
		
		PROCEDURE SetColor(i: INTEGER; c: Ports.Color);
			VAR ci: ColorInfo;
		BEGIN
			NEW(ci); ci.id := i; ci.c := c; ci.next := colors; colors := ci
		END SetColor;
			
		PROCEDURE Font(i: INTEGER): FontInfo;
			VAR f: FontInfo;
		BEGIN
			ASSERT(fonts # NIL, 20);
			f := fonts;
			WHILE (f # NIL) & (f.id # i) DO f := f.next END;
			ASSERT(f # NIL, 100);
			RETURN f
		END Font;
		
		PROCEDURE SetFont(i: INTEGER; tf: Fonts.Typeface);
			VAR f: FontInfo;
		BEGIN
			NEW(f); f.id := i; f.f := tf; f.next := fonts; fonts := f
		END SetFont;
		
		PROCEDURE Next (VAR ch: SHORTCHAR);
			VAR b: BYTE;
		BEGIN
			rd.ReadByte(b); ch := SHORT(CHR(b MOD 256))
		END Next;
		
		PROCEDURE Write (ch: SHORTCHAR);
		BEGIN
			IF dest = text THEN WriteWndChar(wr, ch)
			ELSIF dest = fonttab THEN
				ASSERT(font # NIL, 20);
				font.f[idx] := ch; INC(idx); font.f[idx] := 0X
			END
		END Write;
		
		PROCEDURE NewPara;
		BEGIN
			IF dest = text THEN
				wr.WriteChar(CR); paraPos := wr.Pos(); remPar := FALSE;
				IF (ruler.style.attr.first # ruler.style.attr.left) OR (ruler.style.attr.lead > 0)
					OR (TextRulers.pageBreak IN ruler.style.attr.opts) THEN wr.WriteChar(FF); remPar := TRUE
				END
			END
		END NewPara;
		
		PROCEDURE CheckPara;
			VAR t: TextModels.Model; v: Views.View;
		BEGIN
			IF paraPos >= 0 THEN
				t := wr.Base();
				IF remPar THEN t.Delete(paraPos, paraPos + 1) END;
				wr.SetPos(paraPos);
				v := Views.CopyOf(ruler, Views.deep); ruler := v(TextRulers.Ruler);
				wr.WriteView(ruler, Views.undefined, Views.undefined);
				wr.SetPos(t.Length());
				paraPos := -1
			END
		END CheckPara;
		
	BEGIN
		defFont := 0; fnum := 1; f := Fonts.dir.Default(); NEW(fonts); fonts.f := f.typeface;
		cnum := 1; NEW(colors); SetColor(0, Ports.defaultColor);
		dest := text; con := NIL; paraPos := 0; remPar := FALSE;
		defRuler := TextRulers.dir.New(NIL); ruler := defRuler; tabStyle := {};
		Next(ch);
		WHILE ch # 0X DO
			IF ch = "{" THEN
				NEW(c); c.dest := dest; c.attr := wr.attr; c.next := con; con := c;
				bpos := rd.Pos(); Next(ch)
			ELSIF ch = "}" THEN
				IF con # NIL THEN
					dest := con.dest; wr.SetAttr(con.attr); con := con.next
				END;
				Next(ch)
			ELSIF ch = "\" THEN
				spos := rd.Pos() - 1; Next(ch); i := 0; val := 0;
				IF (ch >= "a") & (ch <= "z") THEN
					WHILE (ch >= "a") & (ch <= "z") DO comm[i] := ch; INC(i); Next(ch) END;
					comm[i] := 0X; fact := 1; hasNum := FALSE;
					IF ch = "-" THEN fact := -1; Next(ch) END;
					WHILE (ch >= "0") & (ch <= "9") DO
						val := 10 * val + ORD(ch) - ORD("0"); Next(ch); hasNum := TRUE
					END;
					val := val * fact;
					IF ch = " " THEN Next(ch) END;
					(* special characters *)
					IF comm = "tab" THEN wr.WriteChar(TAB)
					ELSIF comm = "line" THEN wr.WriteChar(CR)
					ELSIF comm = "par" THEN NewPara
(*
					ELSIF comm = "enspace" THEN wr.WriteChar(08FX)	(* digit space *)
*)
					ELSIF comm =  "ldblquote" THEN wr.WriteChar(201CX) (*  unicode: left double quote *)
					ELSIF comm = "rdblquote" THEN wr.WriteChar(201DX) (*  unicode: right double quote *)
					ELSIF comm = "lquote" THEN wr.WriteChar(2018X) (*  unicode: left single quote *)
					ELSIF comm = "rquote" THEN wr.WriteChar(2019X) (*  unicode: right single quote *)
					ELSIF comm = "page" THEN
						NewPara; CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {TextRulers.pageBreak}; p.opts.mask := p.opts.val;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					(* character attributes *)
					ELSIF comm = "plain" THEN
						wr.SetAttr(TextModels.NewWeight(wr.attr, Fonts.normal));
						wr.SetAttr(TextModels.NewStyle(wr.attr, {}));
						wr.SetAttr(TextModels.NewTypeface(wr.attr, Font(defFont).f));
						wr.SetAttr(TextModels.NewSize(wr.attr, 24 * halfpoint));
						wr.SetAttr(TextModels.NewColor(wr.attr, Ports.defaultColor));
						wr.SetAttr(TextModels.NewOffset(wr.attr, 0))
					ELSIF comm = "b" THEN
						IF hasNum & (val = 0) THEN wr.SetAttr(TextModels.NewWeight(wr.attr, Fonts.normal))
						ELSE wr.SetAttr(TextModels.NewWeight(wr.attr, Fonts.bold))
						END
					ELSIF comm = "i" THEN
						IF hasNum & (val = 0) THEN
							wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style - {Fonts.italic}))
						ELSE wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style + {Fonts.italic}))
						END
					ELSIF comm = "ul" THEN
						IF hasNum & (val = 0) THEN
							wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style - {Fonts.underline}))
						ELSE wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style + {Fonts.underline}))
						END
					ELSIF comm = "strike" THEN
						IF hasNum & (val = 0) THEN
							wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style - {Fonts.strikeout}))
						ELSE wr.SetAttr(TextModels.NewStyle(wr.attr, wr.attr.font.style + {Fonts.strikeout}))
						END
					ELSIF comm = "f" THEN
						IF ~hasNum THEN val := defFont END;
						IF dest = fonttab THEN 
							fnum := val; idx := 0; NEW(font); font.id := val; font.next := fonts; fonts := font
						ELSE 
							wr.SetAttr(TextModels.NewTypeface(wr.attr, Font(val).f))
						END
					ELSIF comm = "fs" THEN
						IF ~hasNum THEN val := 24 END;
						wr.SetAttr(TextModels.NewSize(wr.attr, val * halfpoint))
					ELSIF comm = "cf" THEN
						wr.SetAttr(TextModels.NewColor(wr.attr, Color(val).c))
					ELSIF comm = "dn" THEN
						IF ~hasNum THEN val := 6 END;
						wr.SetAttr(TextModels.NewOffset(wr.attr, -(val * halfpoint)))
					ELSIF comm = "up" THEN
						IF ~hasNum THEN val := 6 END;
						wr.SetAttr(TextModels.NewOffset(wr.attr, val * halfpoint))
					(* paragraph attributes *)
					ELSIF comm = "pard" THEN
						IF (ruler.style.attr.first # 0) OR (ruler.style.attr.left # 0)
							OR (ruler.style.attr.opts # {TextRulers.leftAdjust})
							OR (ruler.style.attr.tabs.len # 0) THEN
							CheckPara; NEW(p);
							p.valid := {TextRulers.first, TextRulers.left, TextRulers.opts, TextRulers.tabs};
							p.opts.mask := {TextRulers.leftAdjust, TextRulers.rightAdjust, TextRulers.pageBreak};
							p.first := 0; p.left := 0; p.opts.val := {TextRulers.leftAdjust}; p.tabs.len := 0;
							attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
						END
					ELSIF comm = "fi" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.first}; p.first := ruler.style.attr.left + val * twips;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "li" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.left, TextRulers.first};
						p.left := val * twips; p.first := p.left + ruler.style.attr.first - ruler.style.attr.left;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "ql" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {TextRulers.leftAdjust};
						p.opts.mask := {TextRulers.leftAdjust, TextRulers.rightAdjust};
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "qr" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {TextRulers.rightAdjust};
						p.opts.mask := {TextRulers.leftAdjust, TextRulers.rightAdjust};
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "qc" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {};
						p.opts.mask := {TextRulers.leftAdjust, TextRulers.rightAdjust};
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "qj" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {TextRulers.leftAdjust, TextRulers.rightAdjust};
						p.opts.mask := {TextRulers.leftAdjust, TextRulers.rightAdjust};
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "sb" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.lead}; p.lead := val * twips;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "sl" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.grid}; p.grid := val * twips;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "tqc" THEN
						tabStyle := {TextRulers.centerTab}
					ELSIF (comm = "tqr") OR (comm="tqdec") THEN
						tabStyle := {TextRulers.rightTab}
					ELSIF comm = "tb" THEN
						CheckPara; p0 := ruler.style.attr.Prop(); p := p0(TextRulers.Prop);
						p.valid := {TextRulers.tabs};
						p.tabs.tab[p.tabs.len].stop := val * twips; 
						p.tabs.tab[p.tabs.len].type := {TextRulers.barTab}; tabStyle := {}; 
						INC(p.tabs.len);
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "tx" THEN
						CheckPara; p0 := ruler.style.attr.Prop(); p := p0(TextRulers.Prop);
						p.valid := {TextRulers.tabs};
						p.tabs.tab[p.tabs.len].stop := val * twips; 
						p.tabs.tab[p.tabs.len].type := tabStyle; tabStyle := {}; 
						INC(p.tabs.len);
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					ELSIF comm = "pagebb" THEN
						CheckPara; NEW(p);
						p.valid := {TextRulers.opts}; p.opts.val := {TextRulers.pageBreak}; p.opts.mask := p.opts.val;
						attr := TextRulers.ModifiedAttr(ruler.style.attr, p); ruler.style.SetAttr(attr)
					(* header *)
					ELSIF comm = "deff" THEN
						IF hasNum THEN defFont := val END
					ELSIF comm = "fonttbl" THEN
						IF dest # skip THEN dest := fonttab END
					ELSIF comm = "colortbl" THEN
						IF dest # skip THEN dest := colortab; cnum := 0; SetColor(0, 0) END
					ELSIF comm = "red" THEN
						IF dest = colortab THEN SetColor(cnum, Color(cnum).c + val MOD 256) END
					ELSIF comm = "green" THEN
						IF dest = colortab THEN SetColor(cnum, Color(cnum).c + val MOD 256 * 256) END
					ELSIF comm = "blue" THEN
						IF dest = colortab THEN SetColor(cnum, Color(cnum).c + val MOD 256 *  65536) END
					ELSIF comm = "rtf" THEN
					ELSIF comm = "ansi" THEN
					ELSIF comm = "lang" THEN
					(* misc *)
					ELSIF comm = "bin" THEN rd.SetPos(rd.Pos() + val - 1); Next(ch)
					ELSE (* unknown *)
						IF (spos = bpos) & (con # NIL) & (con.next # NIL) THEN dest := skip END
					END
				ELSIF ch = "'" THEN
					Next(ch);
					IF ch <= "9" THEN val := ORD(ch) - ORD("0") ELSE val := ORD(CAP(ch)) - ORD("A") + 10 END;
					Next(ch);
					IF ch <= "9" THEN val := 16 * val + ORD(ch) - ORD("0")
					ELSE val := 16 * val + ORD(CAP(ch)) - ORD("A") + 10
					END;
					Write(SHORT(CHR(val))); Next(ch)
				ELSE
					IF ch = "~" THEN wr.WriteChar(0A0X)	(* non breaking space *)
					ELSIF ch = "-" THEN wr.WriteChar(0ADX)	(* soft hyphen *)
					ELSIF ch = "_" THEN wr.WriteChar(2011X)	(* non breaking hyphen *)
					ELSIF ch = "*" THEN bpos := rd.Pos()
					ELSIF (ch = LF) OR (ch = CR) THEN NewPara
					ELSIF (ch = "{") OR (ch = "}") OR (ch = "\") THEN Write(ch)
					END;
					Next(ch)
				END
			ELSIF ch = ";" THEN
				IF dest = fonttab THEN font := Font(fnum); font.f[idx] := 0X; INC(idx)
				ELSIF dest = colortab THEN INC(cnum); SetColor(cnum, 0)
				ELSIF dest = text THEN Write(";")
				END;
				Next(ch)
			ELSIF ch >= " " THEN
				Write(ch); Next(ch)
			ELSE 
				Next(ch)
			END
		END
	END ParseRichText;
	
	PROCEDURE ConvertToRichText (in: TextViews.View; beg, end: INTEGER; VAR out: TextModels.Model);
		VAR r: TextModels.Reader; w: TextMappers.Formatter; ch: CHAR; f: Fonts.Font;
			attr, attr0: TextModels.Attributes; col: Ports.Color; tf, atf: Fonts.Typeface; p, size, asize, offs: INTEGER;
			style, astyle: SET; weight, aweight: INTEGER; rattr, rattr0: TextRulers.Attributes; ruler: TextRulers.Ruler;
			text: TextModels.Model; firstLine, firstLine0: BOOLEAN; fonts: ARRAY 256 OF Fonts.Typeface;
			colors: ARRAY 256 OF Ports.Color; fnum, cnum, i: INTEGER;
	BEGIN
		out := TextModels.dir.New(); w.ConnectTo(out);
		f := Fonts.dir.Default(); tf := f.typeface;
		fnum := 1; fonts[0] := tf;
		cnum := 1; colors[0] := Ports.defaultColor;
		col := Ports.defaultColor; size := 12 * Ports.point;
		offs := 0; style := {}; weight := Fonts.normal;
		attr0 := NIL; rattr0 := NIL; firstLine := TRUE; firstLine0 := FALSE;
		text := in.ThisModel(); r := text.NewReader(NIL);
		ruler := TextViews.ThisRuler(in, beg); rattr := ruler.style.attr;
		r.SetPos(beg); r.ReadChar(ch);
		WHILE ~r.eot & (r.Pos() <= end) DO
			attr := r.attr;
			IF (r.view # NIL) & (r.view IS TextRulers.Ruler) THEN
				ruler := r.view(TextRulers.Ruler); rattr := ruler.style.attr;
				firstLine := TRUE
			ELSIF ch = FF THEN firstLine := TRUE
			END;
			IF (rattr # rattr0) OR (firstLine # firstLine0) THEN
				IF (rattr # rattr0) OR (rattr.first # rattr.left) OR (rattr.lead # 0) OR (TextRulers.pageBreak IN rattr.opts)
				THEN
					w.WriteSString("\pard");
					IF rattr.left # 0 THEN
						w.WriteSString("\li"); w.WriteInt(rattr.left DIV twips)
					END;
					IF firstLine & (rattr.first # rattr.left) THEN
						w.WriteSString("\fi"); w.WriteInt((rattr.first - rattr.left) DIV twips)
					END;
					IF firstLine & (rattr.lead # 0) THEN
						w.WriteSString("\sb"); w.WriteInt(rattr.lead DIV twips)
					END;
					IF rattr.grid > Ports.point THEN
						w.WriteSString("\sl"); w.WriteInt(rattr.grid DIV twips); w.WriteSString("\slmult1")
					END;
					IF {TextRulers.leftAdjust, TextRulers.rightAdjust} - rattr.opts = {} THEN w.WriteSString("\qj")
					ELSIF TextRulers.rightAdjust IN rattr.opts THEN w.WriteSString("\qr")
					ELSIF ~(TextRulers.leftAdjust IN rattr.opts) THEN w.WriteSString("\qc")
					END;
					IF firstLine & (TextRulers.pageBreak IN rattr.opts) THEN
						w.WriteSString("\pagebb")
					END;
					i := 0;
					WHILE i < rattr.tabs.len DO
						IF TextRulers.centerTab IN rattr.tabs.tab[i].type THEN w.WriteSString("\tqc") END; 
						IF TextRulers.rightTab IN rattr.tabs.tab[i].type THEN w.WriteSString("\tqr") END; 
						IF TextRulers.barTab IN rattr.tabs.tab[i].type THEN w.WriteSString("\tb") END; 
						w.WriteSString("\tx"); w.WriteInt(rattr.tabs.tab[i].stop DIV twips);
						INC(i)
					END;
					w.WriteChar(" ")
				END;
				rattr0 := rattr; firstLine0 := firstLine
			END;
			IF attr # attr0 THEN
				p := w.Pos();
				IF attr.color # col THEN
					i := 0; WHILE (i < cnum) & (colors[i] # attr.color) DO INC(i) END;
					IF i = cnum THEN colors[i] := attr.color; INC(cnum) END;
					w.WriteSString("\cf"); w.WriteInt(i);
					col := attr.color
				END;
				atf := attr.font.typeface$; asize := attr.font.size; astyle := attr.font.style; aweight := attr.font.weight;
				IF atf # tf THEN
					i := 0; WHILE (i < fnum) & (fonts[i] # atf) DO INC(i) END;
					IF i = fnum THEN fonts[i] := atf; INC(fnum) END;
					w.WriteSString("\f"); w.WriteInt(i);
					tf := atf
				END;
				IF asize # size THEN
					w.WriteSString("\fs"); w.WriteInt(asize DIV halfpoint);
					size := asize
				END;
				IF astyle # style THEN
					IF (Fonts.italic IN astyle) & ~(Fonts.italic IN style) THEN w.WriteSString("\i")
					ELSIF ~(Fonts.italic IN astyle) & (Fonts.italic IN style) THEN w.WriteSString("\i0")
					END;
					IF (Fonts.underline IN astyle) & ~(Fonts.underline IN style) THEN w.WriteSString("\ul")
					ELSIF ~(Fonts.underline IN astyle) & (Fonts.underline IN style) THEN w.WriteSString("\ul0")
					END;
					IF (Fonts.strikeout IN astyle) & ~(Fonts.strikeout IN style) THEN w.WriteSString("\strike")
					ELSIF ~(Fonts.strikeout IN astyle) & (Fonts.strikeout IN style) THEN w.WriteSString("\strike0")
					END;
					style := astyle
				END;
				IF aweight # weight THEN
					IF (aweight > Fonts.normal) & (weight = Fonts.normal) THEN w.WriteSString("\b")
					ELSIF (aweight = Fonts.normal) & (weight > Fonts.normal) THEN w.WriteSString("\b0")
					END;
					weight := aweight
				END;
				IF attr.offset # offs THEN
					IF attr.offset > 0 THEN w.WriteSString("\up"); w.WriteInt(attr.offset DIV halfpoint)
					ELSIF attr.offset < 0 THEN w.WriteSString("\dn"); w.WriteInt(-(attr.offset DIV halfpoint))
					ELSIF offs > 0 THEN w.WriteSString("\up0")
					ELSE w.WriteSString("\dn0")
					END;
					offs := attr.offset
				END;
				IF w.Pos() # p THEN w.WriteChar(" ") END;
				attr0 := attr
			END;
			IF ch >= 100X THEN
				IF r.char = 2007X THEN w.WriteSString("\enspace ")
				ELSIF r.char = 2010X THEN w.WriteChar("-")
				ELSIF r.char = 2011X THEN w.WriteSString("\_")
				ELSIF r.char = 201CX THEN (*  unicode: left double quote *) w.WriteSString("\ldblquote ")
				ELSIF r.char = 201DX THEN (*  unicode: right double quote *) w.WriteSString("\rdblquote ")
				ELSIF r.char = 2018X THEN (*  unicode: left single quote *) w.WriteSString("\lquote ")
				ELSIF r.char = 2019X THEN (*  unicode: right single quote *) w.WriteSString("\rquote ")					
				ELSE w.WriteSString("\'"); 
					w.WriteIntForm(ORD(ThisWndChar(r.char)) MOD 256, TextMappers.hexadecimal, 2, "0", FALSE)
				END
			ELSE
				CASE ch OF 
				| TAB: w.WriteSString("\tab ")
				| CR: w.WriteSString("\par "); w.WriteLn; firstLine := FALSE
				| " ".."[", "]".."z", "|", "~": w.WriteChar(ch)
				| "\": w.WriteSString("\\")
				| "{": w.WriteSString("\{")
				| "}": w.WriteSString("\}")
				| 8FX: (* digit space *) w.WriteChar(" ")
				| 90X: (* hyphen *) w.WriteChar("-")
				| 91X: (* non-breaking hyphen *) w.WriteSString("\_")
				| 0A0X: (* non-breaking space *) w.WriteSString("\~")
				| 0ADX: (* soft hyphen *) w.WriteSString("\-")
				| 0A1X..0ACX, 0AEX..0FFX:
					w.WriteSString("\'"); w.WriteIntForm(ORD(ch), TextMappers.hexadecimal, 2, "0", FALSE)
				ELSE
				END
			END;
			r.ReadChar(ch);
		END;
		w.WriteChar("}");
		(* header *)
		w.SetPos(0);
		w.WriteSString("{\rtf1\ansi\deff0");
		w.WriteSString("{\fonttbl"); i := 0;
		WHILE i < fnum DO
			IF fonts[i] = Fonts.default THEN fonts[i] := HostFonts.defFont.alias$ END;
			w.WriteSString("{\f"); w.WriteInt(i); w.WriteSString("\fnil "); w.WriteString(fonts[i]); w.WriteSString(";}");
			INC(i)
		END;
		w.WriteChar("}"); w.WriteLn;
		w.WriteSString("{\colortbl;"); i := 1;
		WHILE i < cnum DO
			w.WriteSString("\red"); w.WriteInt(colors[i] MOD 256);
			w.WriteSString("\green"); w.WriteInt(colors[i] DIV 256 MOD 256);
			w.WriteSString("\blue"); w.WriteInt(colors[i] DIV 65536 MOD 256);
			w.WriteChar(";"); INC(i)
		END;
		w.WriteChar("}"); w.WriteLn;
		w.WriteSString("\deftab216 ");
		w.WriteSString("\plain")
	END ConvertToRichText;
	
	
	PROCEDURE ImportDText* (VAR med: WinOle.STGMEDIUM; OUT v: Views.View;
											OUT w, h: INTEGER; OUT isSingle: BOOLEAN);
		VAR t: TextModels.Model; res, adr: INTEGER; wr: TextModels.Writer; ch: SHORTCHAR;
			hnd: KERNEL32.Handle; attr: TextModels.Attributes; p: Properties.StdProp; pref: Properties.BoundsPref;
	BEGIN
		hnd := MediumGlobal(med);
		ASSERT(hnd # 0, 20);
		adr := KERNEL32.GlobalLock(hnd);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		IF HostClipboard.cloneAttributes THEN
			Properties.CollectStdProp(p);
			NEW(attr); attr.InitFromProp(p);
			wr.SetAttr(attr)
		END;
		SYSTEM.GET(adr, ch);
		WHILE ch # 0X DO
			WriteWndChar(wr, ch);
			INC(adr); SYSTEM.GET(adr, ch)
		END;
		res := KERNEL32.GlobalUnlock(hnd);
		v := TextViews.dir.New(t);
		pref.w := Views.undefined; pref.h := Views.undefined;
		Views.HandlePropMsg(v, pref);
		w := pref.w; h := pref.h; isSingle := FALSE
	END ImportDText;
		
	PROCEDURE ImportDRichText* (VAR med: WinOle.STGMEDIUM; OUT v: Views.View;
												OUT w, h: INTEGER; OUT isSingle: BOOLEAN);
		VAR t: TextModels.Model; res, adr: INTEGER; wr: TextModels.Writer; rd: MemReader;
			hnd: KERNEL32.Handle; ruler: TextRulers.Ruler; pref: Properties.BoundsPref;
	BEGIN
		IF debug THEN
			ImportDText(med, v, w, h, isSingle);
			RETURN;
		END;		
		hnd := MediumGlobal(med);
		ASSERT(hnd # 0, 20);
		adr := KERNEL32.GlobalLock(hnd);
		NEW(rd); rd.adr := adr; rd.pos := 0;
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		ParseRichText(rd, wr, ruler);
		res := KERNEL32.GlobalUnlock(hnd);
		v := TextViews.dir.New(t);
		v(TextViews.View).SetDefaults(ruler, TextModels.dir.attr);
		pref.w := Views.undefined; pref.h := Views.undefined;
		Views.HandlePropMsg(v, pref);
		w := pref.w; h := pref.h; isSingle := FALSE
	END ImportDRichText;
	
	PROCEDURE ImportDUnicode* (VAR med: WinOle.STGMEDIUM; OUT v: Views.View;
												OUT w, h: INTEGER; OUT isSingle: BOOLEAN);
		VAR t: TextModels.Model; res, adr, len: INTEGER; wr: TextModels.Writer; uc: CHAR;
			hnd: KERNEL32.Handle; attr: TextModels.Attributes; p: Properties.StdProp; pref: Properties.BoundsPref;
	BEGIN
		hnd := MediumGlobal(med);
		ASSERT(hnd # 0, 20);
		adr := KERNEL32.GlobalLock(hnd);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		IF HostClipboard.cloneAttributes THEN
			Properties.CollectStdProp(p);
			NEW(attr); attr.InitFromProp(p);
			wr.SetAttr(attr)
		END;
		SYSTEM.GET(adr, uc);
		WHILE uc # 0X DO
			ASSERT(uc # 0FFFEX, 100);
			IF uc < 100X THEN WriteWndChar(wr, SHORT(uc))
			ELSIF uc # 0FEFFX THEN wr.WriteChar(uc)
			END;
			INC(adr, 2); SYSTEM.GET(adr, uc)
		END;
		res := KERNEL32.GlobalUnlock(hnd);
		v := TextViews.dir.New(t);
		pref.w := Views.undefined; pref.h := Views.undefined;
		Views.HandlePropMsg(v, pref);
		w := pref.w; h := pref.h; isSingle := FALSE
	END ImportDUnicode;

	PROCEDURE ExportDText* (
		v: Views.View; w, h, x, y: INTEGER; isSingle: BOOLEAN; VAR med: WinOle.STGMEDIUM
	);
		VAR t: TextModels.Model; r: TextModels.Reader; ch: CHAR; wch: SHORTCHAR;
			res, len, adr: INTEGER; hnd: KERNEL32.Handle;
	BEGIN
		ASSERT(v # NIL, 20);
		WITH v: TextViews.View DO
			t := v.ThisModel(); (* Alexander Iljin: fixed trap upon copying empty text view *)
			hnd := KERNEL32.GlobalAlloc({1, 13}, 2 * t.Length() + 1);	(* movable, sharable *)
			IF hnd # 0 THEN
				adr  := KERNEL32.GlobalLock(hnd); len := 0;
				r := t.NewReader(NIL); r.ReadChar(ch);
				WHILE ~r.eot DO
					IF (ch # TextModels.viewcode) & (ch # TextModels.para) THEN
						wch := ThisWndChar(ch);
						SYSTEM.PUT(adr, wch); INC(adr); INC(len);
						IF wch = CR THEN SYSTEM.PUT(adr, LF); INC(adr); INC(len) END;
					END;
					r.ReadChar(ch)
				END;
				SYSTEM.PUT(adr, 0X); INC(len);
				res := KERNEL32.GlobalUnlock(hnd);
				hnd := KERNEL32.GlobalReAlloc(hnd, len, {});
				GenGlobalMedium(hnd, NIL, med)
			END
		ELSE
		END
	END ExportDText;
	
	PROCEDURE ExportDRichText* (
		v: Views.View; w, h, x, y: INTEGER; isSingle: BOOLEAN; VAR med: WinOle.STGMEDIUM
	);
		VAR t: TextModels.Model; r: TextModels.Reader; ch: CHAR; res, adr: INTEGER; hnd: KERNEL32.Handle;
	BEGIN
		ASSERT(v # NIL, 20);
		WITH v: TextViews.View DO
			ConvertToRichText(v, 0, MAX(INTEGER), t);
			hnd := KERNEL32.GlobalAlloc({1, 13}, t.Length() + 1);	(* movable, sharable *)
			IF hnd # 0 THEN
				adr := KERNEL32.GlobalLock(hnd);
				r := t.NewReader(NIL); r.ReadChar(ch);
				WHILE ~r.eot DO
					SYSTEM.PUT(adr, ch); INC(adr);
					r.ReadChar(ch)
				END;
				SYSTEM.PUT(adr, 0X);
				res := KERNEL32.GlobalUnlock(hnd);
				GenGlobalMedium(hnd, NIL, med)
			END
		ELSE
		END
	END ExportDRichText;
	
	PROCEDURE ExportDUnicode* (
		v: Views.View; w, h, x, y: INTEGER; isSingle: BOOLEAN; VAR med: WinOle.STGMEDIUM
	);
		VAR t: TextModels.Model; r: TextModels.Reader; ch: CHAR; res, len, adr: INTEGER; hnd: KERNEL32.Handle;
	BEGIN
		ASSERT(v # NIL, 20);
		WITH v: TextViews.View DO
			t := v.ThisModel(); (* Alexander Iljin: fixed trap upon copying empty text view *)
			hnd := KERNEL32.GlobalAlloc({1, 13}, 4 * t.Length() + 2);	(* movable, sharable *)
			IF hnd # 0 THEN
				adr  := KERNEL32.GlobalLock(hnd); len := 0;
				r := t.NewReader(NIL); r.ReadChar(ch);
				WHILE ~r.eot DO
					ch := i21sysCharacters.UnicodeOf(ch);
					IF ch = CR THEN
						SYSTEM.PUT(adr, LONG(CR)); INC(adr, 2); INC(len, 2);
						SYSTEM.PUT(adr, LONG(LF)); INC(adr, 2); INC(len, 2)
					ELSIF (ch >= " ") OR (ch = TAB) THEN
						IF (ch >= 0EF00X) & (ch <= 0EFFFX) THEN ch := CHR(ORD(ch) - 0EF00H) END;
						SYSTEM.PUT(adr, ch); INC(adr, 2); INC(len, 2)
					END;
					r.ReadChar(ch)
				END;
				SYSTEM.PUT(adr, LONG(0X)); INC(len, 2);
				res := KERNEL32.GlobalUnlock(hnd);
				hnd := KERNEL32.GlobalReAlloc(hnd, len, {});
				GenGlobalMedium(hnd, NIL, med)
			END
		ELSE
		END
	END ExportDUnicode;

	PROCEDURE ImportText* (f: Files.File; OUT s: Stores.Store);
		VAR r: Stores.Reader; t: TextModels.Model; wr: TextModels.Writer; ch, nch: SHORTCHAR;
	BEGIN
		ASSERT(f # NIL, 20);
		r.ConnectTo(f); r.SetPos(0);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		r.ReadSChar(ch);
		WHILE ~r.rider.eof DO
			r.ReadSChar(nch);
			IF (ch = CR) & (nch = LF) THEN r.ReadSChar(nch)
			ELSIF ch = LF THEN ch := CR
			END;
			WriteWndChar(wr, ch); ch := nch
		END;
		s := TextViews.dir.New(t)
	END ImportText;

	PROCEDURE ImportTabText* (f: Files.File; OUT s: Stores.Store);
		VAR r: Stores.Reader; t: TextModels.Model; wr: TextModels.Writer; ch, nch: SHORTCHAR;
	BEGIN
		ASSERT(f # NIL, 20);
		r.ConnectTo(f); r.SetPos(0);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		r.ReadSChar(ch);
		WHILE ~r.rider.eof DO
			r.ReadSChar(nch);
			IF (ch = CR) & (nch = LF) THEN r.ReadSChar(nch)
			ELSIF ch = LF THEN ch := CR
			ELSIF (ch = " ") & (nch = " ") THEN ch := TAB; r.ReadSChar(nch)
			END;
			WriteWndChar(wr, ch); ch := nch
		END;
		s := TextViews.dir.New(t)
	END ImportTabText;

	PROCEDURE ImportRichText* (f: Files.File; OUT s: Stores.Store);
		VAR t: TextModels.Model; wr: TextModels.Writer; rd: Files.Reader; ruler: TextRulers.Ruler;
	BEGIN
		rd := f.NewReader(NIL); rd.SetPos(0);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		ParseRichText(rd, wr, ruler);
		s := TextViews.dir.New(t);
		s(TextViews.View).SetDefaults(ruler, TextModels.dir.attr)
	END ImportRichText;
	
	PROCEDURE ImportUnicode* (f: Files.File; OUT s: Stores.Store);
		VAR r: Stores.Reader; t: TextModels.Model; v: TextViews.View; w: TextModels.Writer;
			ch0, ch1: SHORTCHAR; len, res: INTEGER; uc: CHAR;
			rev: BOOLEAN; fh, dc: GDI32.Handle; name: Fonts.Typeface;
	BEGIN
		ASSERT(f # NIL, 20);
		r.ConnectTo(f); r.SetPos(0);
		len := f.Length(); rev := FALSE;
		t := TextModels.dir.New(); w := t.NewWriter(NIL); w.SetPos(0);
		WHILE len > 0 DO
			r.ReadSChar(ch0); r.ReadSChar(ch1);
			IF rev THEN uc := CHR(ORD(ch1) + 256 * ORD(ch0))
			ELSE uc := CHR(ORD(ch0) + 256 * ORD(ch1))
			END;
			DEC(len, 2);
			IF uc = 0FFFEX THEN rev := ~rev
			ELSIF uc < 100X THEN WriteWndChar(w, SHORT(uc))
			ELSIF uc # 0FEFFX THEN w.WriteChar(uc)
			END
		END;
		v := TextViews.dir.New(t);
		s := v
	END ImportUnicode;
	
	PROCEDURE ImportDosText* (f: Files.File; OUT s: Stores.Store);
		VAR r: Stores.Reader; t: TextModels.Model; wr: TextModels.Writer; ch, nch: SHORTCHAR;
		PROCEDURE ConvertChar (wr: TextModels.Writer; ch: SHORTCHAR);
		(* PC Code Page Mappings M4 (Latin) to Unicode Encoding *)
		(* Reference: The Unicode Standard, Version 1.0, Vol 1, Addison Wesley, p. 536 *)
		BEGIN		
			CASE ch OF
			| CR, TAB, " "..7EX: wr.WriteChar(ch)
			| LF:
			| 80X..0AFX: 
				wr.WriteChar(i21sysCharacters.UnicodeOf(CHR(ORD(ch) + 64)))
			| 0E0X..0EFX:
				wr.WriteChar(i21sysCharacters.UnicodeOf(CHR(ORD(ch) + 16)))
			| 00F0X:
				wr.WriteChar(i21sysCharacters.UnicodeOf(0A8X))
			| 00F1X:
				wr.WriteChar(i21sysCharacters.UnicodeOf(0B8X))
(*			| 080X: wr.WriteChar(0C7X)
			| 081X: wr.WriteChar(0FCX)
			| 082X: wr.WriteChar(0E9X)
			| 083X: wr.WriteChar(0E2X)
			| 084X: wr.WriteChar(0E4X)
			| 085X: wr.WriteChar(0E0X)
			| 086X: wr.WriteChar(0E5X)
			| 087X: wr.WriteChar(0E7X)
			| 088X: wr.WriteChar(0EAX)
			| 089X: wr.WriteChar(0EBX)
			| 08AX: wr.WriteChar(0E8X)
			| 08BX: wr.WriteChar(0EFX)
			| 08CX: wr.WriteChar(0EEX)
			| 08DX: wr.WriteChar(0ECX)
			| 08EX: wr.WriteChar(0C4X)
			| 08FX: wr.WriteChar(0C5X)
			| 090X: wr.WriteChar(0C9X)
			| 091X: wr.WriteChar(0E6X)
			| 092X: wr.WriteChar(0C6X)
			| 093X: wr.WriteChar(0F4X)
			| 094X: wr.WriteChar(0F6X)
			| 095X: wr.WriteChar(0F2X)
			| 096X: wr.WriteChar(0FBX)
			| 097X: wr.WriteChar(0F9X)
			| 098X: wr.WriteChar(0FFX)
			| 099X: wr.WriteChar(0D6X)
			| 09AX: wr.WriteChar(0DCX)
			| 09BX: wr.WriteChar(0F8X)
			| 09CX: wr.WriteChar(0A3X)
			| 09DX: wr.WriteChar(0D8X)
			| 09EX: wr.WriteChar(0D7X)
			| 09FX: wr.WriteChar(0192X)
			| 0A0X: wr.WriteChar(0E1X)
			| 0A1X: wr.WriteChar(0EDX)
			| 0A2X: wr.WriteChar(0F3X)
			| 0A3X: wr.WriteChar(0FAX)
			| 0A4X: wr.WriteChar(0F1X)
			| 0A5X: wr.WriteChar(0D1X)
			| 0A6X: wr.WriteChar(0AAX)
			| 0A7X: wr.WriteChar(0BAX)
			| 0A8X: wr.WriteChar(0BFX)
			| 0A9X: wr.WriteChar(0AEX)
			| 0AAX: wr.WriteChar(0ACX)
			| 0ABX: wr.WriteChar(0BDX)
			| 0ACX: wr.WriteChar(0BCX)
			| 0ADX: wr.WriteChar(0A1X)
			| 0AEX: wr.WriteChar(0ABX)
			| 0AFX: wr.WriteChar(0BBX) *)
			| 0B5X: wr.WriteChar(0C1X)
			| 0B6X: wr.WriteChar(0C2X)
			| 0B7X: wr.WriteChar(0C0X)
			| 0B8X: wr.WriteChar(0A9X)
			| 0BDX: wr.WriteChar(0A2X)
			| 0BEX: wr.WriteChar(0A5X)
			| 0C6X: wr.WriteChar(0E3X)
			| 0C7X: wr.WriteChar(0C3X)
			| 0CFX: wr.WriteChar(0A4X)
			| 0D0X: wr.WriteChar(0F0X)
			| 0D1X: wr.WriteChar(0D0X)
			| 0D2X: wr.WriteChar(0CAX)
			| 0D3X: wr.WriteChar(0CBX)
			| 0D4X: wr.WriteChar(0C8X)
			| 0D5X: wr.WriteChar(0131X)
			| 0D6X: wr.WriteChar(0CDX)
			| 0D7X: wr.WriteChar(0CEX)
			| 0D8X: wr.WriteChar(0CFX)
			| 0DDX: wr.WriteChar(0A6X)
			| 0DEX: wr.WriteChar(0CCX)
(*			| 0E0X: wr.WriteChar(0D3X)
			| 0E1X: wr.WriteChar(0DFX)
			| 0E2X: wr.WriteChar(0D4X)
			| 0E3X: wr.WriteChar(0D2X)
			| 0E4X: wr.WriteChar(0F5X)
			| 0E5X: wr.WriteChar(0D5X)
			| 0E6X: wr.WriteChar(0B5X)
			| 0E7X: wr.WriteChar(0FEX)
			| 0E8X: wr.WriteChar(0DEX)
			| 0E9X: wr.WriteChar(0DAX)
			| 0EAX: wr.WriteChar(0DBX)
			| 0EBX: wr.WriteChar(0D9X)
			| 0ECX: wr.WriteChar(0FDX)
			| 0EDX: wr.WriteChar(0DDX)
			| 0EEX: wr.WriteChar(0AFX)
			| 0EFX: wr.WriteChar(0B4X)
			| 0F0X: wr.WriteChar(0ADX)
			| 0F1X: wr.WriteChar(0B1X) *)
			| 0F2X: wr.WriteChar(02017X)
			| 0F3X: wr.WriteChar(0BEX)
			| 0F4X: wr.WriteChar(0B6X)
			| 0F5X: wr.WriteChar(0A7X)
			| 0F6X: wr.WriteChar(0F7X)
			| 0F7X: wr.WriteChar(0B8X)
			| 0F8X: wr.WriteChar(0B0X)
			| 0F9X: wr.WriteChar(0A8X)
			| 0FAX: wr.WriteChar(0B7X)
			| 0FBX: wr.WriteChar(0B9X)
			| 0FCX: wr.WriteChar(0B3X)
			| 0FDX: wr.WriteChar(0B2X)
			| 0X..8X, 0BX, 0CX, 0EX..1FX, 7FX,
			  0B0X..0B4X, 0B9X..0BCX, 0BFX..0C5X, 0C8X..0CEX, 0D9X..0DCX, 0DFX, 0FEX, 0FFX:
				wr.WriteChar(CHR(0EF00H + ORD(ch)))
			END
		END ConvertChar;
		
	BEGIN
		ASSERT(f # NIL, 20);
		r.ConnectTo(f); r.SetPos(0);
		t := TextModels.dir.New(); wr := t.NewWriter(NIL);
		r.ReadSChar(ch);
		WHILE ~r.rider.eof DO
			r.ReadSChar(nch);
			IF (ch = CR) & (nch = LF) THEN r.ReadSChar(nch)
			ELSIF ch = LF THEN ch := CR
			END;
			ConvertChar(wr, ch); ch := nch
		END;
		s := TextViews.dir.New(t)
	END ImportDosText;

	PROCEDURE TextView(s: Stores.Store): Stores.Store;
	BEGIN
		IF s IS Views.View THEN RETURN Properties.ThisType(s(Views.View), "TextViews.View")
		ELSE RETURN NIL
		END
	END TextView;

	PROCEDURE ExportText* (s: Stores.Store; f: Files.File);
		VAR w: Stores.Writer; t: TextModels.Model; r: TextModels.Reader; ch: CHAR; wch: SHORTCHAR;
	BEGIN
		ASSERT(s # NIL, 20); ASSERT(f # NIL, 21);
		(* IF s IS TextViews.View THEN *)
		s := TextView(s);
		IF s # NIL THEN
			w.ConnectTo(f); w.SetPos(0);
			t := s(TextViews.View).ThisModel();
			IF t # NIL THEN
				r := t.NewReader(NIL);
				r.ReadChar(ch);
				WHILE ~r.eot DO
					IF (ch # TextModels.viewcode) & (ch # TextModels.para) THEN
						wch := ThisWndChar(ch);
						w.WriteSChar(wch);
						IF wch = CR THEN w.WriteSChar(LF) END
					END;
					r.ReadChar(ch)
				END
			END
		END
	END ExportText;
	
	PROCEDURE ExportTabText* (s: Stores.Store; f: Files.File);
		VAR w: Stores.Writer; t: TextModels.Model; r: TextModels.Reader; ch: CHAR; wch: SHORTCHAR;
	BEGIN
		ASSERT(s # NIL, 20); ASSERT(f # NIL, 21);
		s := TextView(s);
		IF s # NIL THEN
			w.ConnectTo(f); w.SetPos(0);
			t := s(TextViews.View).ThisModel();
			IF t # NIL THEN
				r := t.NewReader(NIL);
				r.ReadChar(ch);
				WHILE ~r.eot DO
					IF (ch # TextModels.viewcode) & (ch # TextModels.para) THEN
						wch := ThisWndChar(ch);
						IF wch = CR THEN w.WriteSChar(CR); w.WriteSChar(LF)
						ELSIF wch = TAB THEN w.WriteSChar(" "); w.WriteSChar(" ")
						ELSE w.WriteSChar(wch)
						END
					END;
					r.ReadChar(ch)
				END
			END
		END
	END ExportTabText;
	
	PROCEDURE ExportRichText* (s: Stores.Store; f: Files.File);
		VAR t: TextModels.Model; r: TextModels.Reader; ch: CHAR; w: Stores.Writer;
	BEGIN
		ASSERT(s # NIL, 20); ASSERT(f # NIL, 21);
		WITH s: TextViews.View DO
			ConvertToRichText(s, 0, MAX(INTEGER), t);
			w.ConnectTo(f); w.SetPos(0);
			r := t.NewReader(NIL); r.ReadChar(ch);
			WHILE ~r.eot DO
				w.WriteSChar(SHORT(ch)); r.ReadChar(ch)
			END;
(*
			w.WriteSChar(0X)
*)
		ELSE
		END
	END ExportRichText;
	
	PROCEDURE ExportUnicode* (s: Stores.Store; f: Files.File);
		VAR w: Stores.Writer; t: TextModels.Model; r: TextModels.Reader; ch: CHAR; tab2sp: INTEGER;
	BEGIN
		ASSERT(s # NIL, 20); ASSERT(f # NIL, 21);
		s := TextView(s);
		IF s # NIL THEN
			w.ConnectTo(f); w.SetPos(0);
			w.WriteChar(0FEFFX);	(* little endian *)
			t := s(TextViews.View).ThisModel();
			IF t # NIL THEN
				r := t.NewReader(NIL);
				r.ReadChar(ch);
				WHILE ~r.eot DO
					ch := i21sysCharacters.UnicodeOf(ch);
					IF ch = CR THEN
						w.WriteChar(CR); w.WriteChar(LF)
					ELSIF (ch >= " ") OR (ch = TAB) THEN
						IF (ch >= 0EF00X) & (ch <= 0EFFFX) THEN ch := CHR(ORD(ch) - 0EF00H) END;
						w.WriteChar(ch)
					END;
					r.ReadChar(ch)
				END
			END
		END
	END ExportUnicode;
	
	PROCEDURE ImportHex* (f: Files.File; OUT s: Stores.Store);
		VAR r: Stores.Reader; t: TextModels.Model; w: TextMappers.Formatter; ch: SHORTCHAR; a: INTEGER;
			i: INTEGER; str: ARRAY 17 OF CHAR;
	BEGIN
		ASSERT(f # NIL, 20);
		r.ConnectTo(f); r.SetPos(0);
		t := TextModels.dir.New();
		w.ConnectTo(t); w.SetPos(0);
		r.ReadSChar(ch); a := 0;
		WHILE ~r.rider.eof DO
			IF a MOD 16 = 0 THEN
				w.WriteChar("[");
				w.WriteIntForm(a, TextMappers.hexadecimal, 8, "0", FALSE);
				w.WriteSString("]ЏЏЏ")
			END;
			w.WriteIntForm(ORD(ch), TextMappers.hexadecimal, 2, "0", FALSE);
			IF ch > 20X THEN str[a MOD 16] := ch ELSE str[a MOD 16] := "Џ" END;
			INC(a);
			IF a MOD 16 = 0 THEN
				str[16] := 0X; w.WriteString("ЏЏЏЏ"); w.WriteString(str);
				w.WriteLn
			ELSIF a MOD 4 = 0 THEN
				w.WriteString("ЏЏ")
			ELSE
				w.WriteChar("Џ")
			END;
			r.ReadSChar(ch)
		END;
		IF a MOD 16 # 0 THEN
			str[a MOD 16] := 0X;
			i := (16 - a MOD 16) * 3 + (3 - a MOD 16 DIV 4) + 3;
			WHILE i # 0 DO w.WriteChar("Џ"); DEC(i) END;
			w.WriteString(str)
		END;
		s := TextViews.dir.New(t)
	END ImportHex;

END HostTextConv.