From 7a0d5cba3fba1858b8b70f05ce017b0e46cb4bdc Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 21 May 2015 12:33:27 +0100 Subject: Scale images down if they can't fit in the text area --- tkread | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tkread b/tkread index 12d4908..e6c7c87 100755 --- a/tkread +++ b/tkread @@ -327,6 +327,40 @@ proc changeFontSize {change} { .t tag configure prefix_$x -font "{$fontfamily} [expr $fontsize + $fmtsizemod] $fmtstring" } } + scaleImages +} + +proc reduceImageIfNeeded {img maxwidth} { + set curwidth [image width $img] + if {$curwidth > $maxwidth} { + set t [image create photo] + $t copy $img + $img blank + set zoom [expr round($curwidth.0 / $maxwidth)] + $img copy $t -shrink -subsample $zoom + image delete $t + } +} + +proc scaleImages {} { + global fontfamily + global fontsize + + foreach img [.t image names] { + # note width is 64 * (width of '0' char) + set charwidth [font measure "{$fontfamily} $fontsize" "0"] + set maxwidth [expr $charwidth * 64] + set curwidth [image width $img] + if {$curwidth < $maxwidth} { + # re-load the image as it may have been shrunk + # TODO: could make this more efficient by keeping track of which images actually have been shrunk + # TODO: also should keep copies of the original image in memory + puts "$curwidth < $maxwidth" + set f [$img cget -file] + $img read $f + } + reduceImageIfNeeded $img $maxwidth + } } proc invertColours {} { @@ -378,6 +412,7 @@ if { $domarkdown } { markup .t } .t configure -state disabled ;# disable text insertion & cursor +scaleImages bind . {scroll -1 unit} bind . {scroll -1 unit} -- cgit v1.2.3