# This module contains assorted text-handling routines for JC's HTML viewer. # We use a couple of arrays to keep track of white space and newlines: # wantNL($w) is number of newlines to be added to $w before the next text. # SP($w) is white space that is to be added to $w before the next text. # LC is the last char that we added to any text widget. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Produce a "display" version of the string s. This routine is used to build # readable versions of strings with newlines converted to \n etc. proc DSP s { global DSPtbl foreach c [array names DSPtbl] { set d $DSPtbl($c) regsub -all $c $s $d s } return $s } global DSPtbl if [info exists DSPtbl] {unset DSPtbl} array set DSPtbl { "\\\\" {\\} "\a" {\a} "\b" {\b} "\f" {\f} "\r" {\r} "\n" {\n} "\t" {\t} "\v" {\v} "\x01" {\x01} "\x02" {\x02} "\x03" {\x03} "\x07" {\x07} "\x11" {\x11} "\x13" {\x13} } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Produce n newlines. The first one is conditional, depending on whether the # global wantNL variable says we just produced one. If n > 1, we produce extra # newlines, as needed by the
tag for instance. proc Hnl {n} { global V me wantNL curD if {$V>1} {set id "$me/Hnl"} if {$V>3} {puts "$id: $curD n=$n"} if ![info exists wantNL($curD)] {set wantNL($curD) 0} if {$V>3} {puts "$id: Change wantNL($curD) from $wantNL($curD) to $n."} set wantNL($curD) $n if {$V>3} {puts "$id: $curD n=$n wantNL($curD)=$wantNL($curD)"} } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # proc tagHR {w a} { global C V me N in LC wantNL Src spat if {$V>1} {set id "$me/tagHR"} incr N(HR) if {$V>4} {puts "$id: HR $N(HR) attributes=\"$a\""} if {$V>4} {puts "$id: $w insert unmarked wantNL at end."} $w insert end \n if {$V>4} {puts "$id: $w insert marked wantNL at end, before HR."} $w insert end \n HR if [regexp -nocase {size=([0-9]+)} $a {} h] { set b [expr (1+$h)/2] } else { set h 5 set b 3 } if {$V>4} {puts "$id: Height is $h border is $b."} if [winfo exists $w.hr$N(HR)] { $w.hr$N(HR) config -bd $b -relief raised if {$V>4} {puts "$id: $w.hr$N(HR) config -bd $b -relief raised"} } else { frame $w.hr$N(HR) -bd $b -relief raised if {$V>4} {puts "$id: frame $w.hr$N(HR) -bd $b -relief raised"} } if {$V>4} {puts "$id: $w window create insert -window \"$w.hr$N(HR)\""} $w window create insert -window $w.hr$N(HR) if {$V>4} {puts "$id: $w HR $N(HR) inserted."} if {$in(PRE) < 1} { if ![info exists wantNL($w)] {set wantNL($w) 0} if {$V>3} {puts "$id: Change wantNL($w) from $wantNL($w) to 0."} set wantNL($w) 0 } if [regsub $spat $Src {} Src] { if {$V>3} {puts "$id: Stripped initial spaces from Src."} } if {$V>3} {puts "$me/HSP: Change SP($w) to {}."} HSP $w {} if {$V>4} {puts "$id: LC=\"$LC\" (end)"} } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Hypertext spacing routine. The arg s should be one or more white # # chars of some sort. We remember them, and the widget that they are # # intended for, and the next call of Htxt will produce them. This is # # basically a kludge to minimize such spacing, and to suppress it # # entirely at the end of a text widget. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # proc HSP {w s} { global V me SP in if {$in(TITLE)} {set w TITLE} set SP($w) $s if {$V>3} {puts "$me/HSP: SP($w) is now \"[DSP $s]\""} } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Put a chunk of HTMLized text out to a text widget. If the global # # curD widget is defined, we use it as the target instead, so that # # the caller can redirect all of the output of subprocesses. We look # # for titles, and copy the text to our title bar. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # proc Htxt t { global V me curD LC Lnum Ltag Ftag in Mtab wantNL title TxtLen TxtMinw TxtMaxw set id "$me/Htxt" if {$in(TITLE)} {set w TITLE} else {set w $curD} if {$V>4} {puts "$id: $w t=\"$t\""} DoSP if {$in(TITLE)} { if {$V>1} {puts "$id: TITLE title=\"$title\" t=\"$t\""} append title $t if {$V>1} {puts "$id: title=\"$title\""} wm title . $title } if {$V>2} {puts "$id: $w contains $TxtLen($w) chars."} if {$t != {}} { if {$w != {TITLE}} { if {$V>4} {puts "$id: $w insert end \"$t\" \"$Mtab F$Ftag $Ltag $Lnum\""} $w insert end $t "$Mtab F$Ftag $Ltag $Lnum" $w tag raise $Lnum } incr TxtLen($w) [string length $t] if {$V>2} {puts "$id: $w TxtLen=$TxtLen($w) chars."} set pxls [font measure [FontName $w] $t] if {$V>1} {puts "$id: Add $pxls pixels for \"$t\""} if ![info exists TxtMinw($w)] {set TxtMinw($w) 0} if ![info exists TxtMaxw($w)] {set TxtMaxw($w) 0} incr TxtMaxw($w) $pxls if {$V>1} {puts "$id: $w now contains $TxtLen($w) chars \[$TxtMinw($w),$TxtMaxw($w)\] pixels."} set LC [string range $t end end] } if {$V>2} {puts "$id: $w now contains TxtLen=$TxtLen($w) chars LC={$LC} wantNL($curD)=$wantNL($curD)"} } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Here's the routine that puts out spaces. This gets a bit tricky. # # proc DoSP {} { global V me LC wantNL PicMinw SP curD TxtLen in title TxtMinw TxtMaxw set id "$me/DoSP" if {$in(TITLE)} {set w TITLE} else {set w $curD} if ![info exists SP($w)] {set SP($w) {}} if ![info exists TxtLen($w)] {set TxtLen($w) 0} if ![info exists PicMinw($w)] {set PicMinw($w) 0} if {$V>4} {puts "$id: $w SP=\"$SP($w)\" wantNL=$wantNL($w) TxtLen=$TxtLen($w) PicMinw=$PicMinw($w)."} if {$TxtLen($w) < 1 && $PicMinw($w) < 1} { if {$V>4} { if {$TxtLen($w)<1} {puts "$id: $w suppressing initial white space because TxtLen($w)=$TxtLen($w)."} if {$PicMinw($w)<1} {puts "$id: $w suppressing initial white space because PicMinw($w)=$PicMinw($w)."} } if {$V>3} {puts "$id: Change SP($w) from \"[DSP $SP($w)]\" to {}."} HSP $w {} if ![info exists wantNL($w)] {set wantNL($w) 0} if {$V>3} {puts "$id: Change wantNL($w) from $wantNL($w) to 0."} set wantNL($w) 0 } while {$wantNL($w) > 0} { if {!$in(TITLE)} { if {$V>4} {puts "$id: $w insert end newline $wantNL($w)."} $w insert end [set LC "\n"] } incr wantNL($w) -1 if {$V>3} {puts "$id: Change SP($w) from \"[DSP $SP($w)]\" to \"\"."} HSP $w {} } if {$V>4} {puts "$id: $w SP=\"$SP($w)\"."} if {$SP($w) != {}} { if {$V>4} {puts "$id: $w needs a space \"$SP($w)\"."} if {$in(TITLE)} { append title { } } else { set s $SP($w) if {$V>4} {puts "$id: $w insert end \"$s\""} $w insert end $s set pxls [font measure [FontName $w] $s] if {$V>1} {puts "$id: Add $pxls pixels for \"$s\""} incr TxtMaxw($w) $pxls if {$V>1} {puts "$id: $w now contains $TxtLen($w) chars \[$TxtMinw($w),$TxtMaxw($w)\] pixels."} } if {$V>3} {puts "$id: Change SP($w) from \"[DSP $SP($w)]\" to {}."} HSP $w {} if ![info exists wantNL($w)] {set wantNL($w) 0} if {$V>3} {puts "$id: Change wantNL($w) from $wantNL($w) to 0."} set wantNL($w) 0 } }