On 9/3/2021 2:23 PM, Helmut Giese wrote:
Hello out there,
wanting to display text in a text widget which is partly italic and /
or bold I am wondering if there is a light weight conversion tool
which converts text like
This is normal text while this is in <i>italic</i>
(or with any other annotation) into the proper text widget 's
pathname tag configure ...
commands?
Any link or tip will be greatly appreciated
Helmut
This is what I use and it works for me.
# Return the text widget tag for html italic or bold text
#
proc tag {ib} {
switch -exact $ib {
i {
return tagCaptionItalic
}
b {
return tagCaptionBold
}
default {
return ""
}
}
}
# Replace any html character entities with the literal character
#
proc unHtml {text} {
regsub -all {&?} $text {\&} text
regsub -all {<?} $text {<} text
regsub -all {>?} $text {>} text
regsub -all {¢?} $text "\u00a2" text
regsub -all {"?} $text {"} text
regsub -all {'?} $text {'} text
regsub -all { ?} $text "\u00a0" text
return $text
}
# Scan a html text string and break it into {text tag} pairs for
insertion
# into the text widget for the caption. The only html tags
supported are
# <i>...</i> and <b>...</b> and they cannot be nested
#
proc tagify {string} {
set text [list ]
set ranges [regexp -all -indices -inline
{<{1,1}?([ib])\s*>(.*?)</\1\s*>} $string]
set cursor 0
foreach {whole tag content} $ranges {
lassign $whole s e
if { $s > $cursor } {
lappend text [unHtml [string range $string $cursor $s-1]] ""
}
lappend text [unHtml [string range $string {*}$content]] [tag
[string index $string [lindex $tag 0]]]
set cursor [expr {$e+1}]
}
if { $cursor < [string length $string] } {
lappend text [unHtml [string range $string $cursor end]] ""
}
return $text
}
#
# Derive and create fonts
#
array set Constants [list ]
set size [font configure TkCaptionFont -size]
# The fonts for the caption window
set Constants(captionFont) [eval \
font create [font configure TkCaptionFont] -size [incrFontSize
$size 2]]
set Constants(captionBoldFont) [eval \
font create [font configure TkCaptionFont] -size [incrFontSize
$size 2] \
-weight bold]
set Constants(captionItalicFont) [eval \
font create [font configure TkCaptionFont] -size [incrFontSize
$size 2] \
-slant italic]
set Constants(captionMeasure) [font measure $Constants(captionFont) 0]
unset size
proc whatever ... {
:
:
:
# Insert the caption after replacing any html character
entities and
# using an italic or bold font for <i> or <b> html tags
if { [string length $caption] > 0 } {
text $w.caption -font $Constants(captionFont) \
-borderwidth 0 \
-background white \
-height 1 \
-width [expr { ([image width image$id] - 50)
/ $Constants(captionMeasure) }] \
-relief flat \
-wrap word
$w.caption tag config tagJustify -justify center
$w.caption tag config tagCaptionItalic -font
$Constants(captionItalicFont)
$w.caption tag config tagCaptionBold -font
$Constants(captionBoldFont)
foreach {text tag} [tagify $caption] {
$w.caption insert end $text $tag
}
$w.caption tag add tagJustify 0.0 end
pack $w.caption -side top \
-pady {0 5}
:
:
:
}
--
computerjock AT mail DOT com
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)