最近有国内新客户抱怨我们产品显示的原理图太不专业了,在原理图上使用宋体GB2312设计好中文图表,经过几次缩放时,表格内的文字居然会跑到表格外边,更要命的是打印出来的文档也存在同样的问题。 我研究了一下,原来又是Windows GDI Text APIs的一个大坑!
问题详细描述
用Tcl/tk script可以很容易地重现这个问题。如下图,第一行文字使用的Arial字体rendering by GDI APIs, 第二行文字使用的是OF Helvetica字体rendering by Freetype APIs,两行文字都用和字体无关的方式加了边框。
- 
刚运行脚本时,两行文字都是8pt,虽然显示效果不一样,但看着是一样大。 
 
- 
放大几次后,第一行的GDI文字明显跑到框外了。 
 
再看一下这时的log: 
font:-family Arial -size 20.118148965022 -weight normal -slant roman -underline 0 -overstrike 0 -type truetype 
font:-family {OF Helvetica} -size 19.53125 -weight normal -slant roman -underline 0 -overstrike 0 -type outline 
zoom fact:2.44140625
缩放比例2.44140625,outline字体的大小是19.53125ot,正好等于 8 * 2.44140625,没有一点精度损失;而truetype字体的大小是20.118148965022pt,明显偏大了。
原因分析
Tcl tk在Windows上使用GDI font engine来渲染显示Truetype字体。GDI Text APIs 的font size必须是int类型,在代码调用过程中,需要把界面指定的phsicial font size(pt) 转换为 logical font size,从而导致了精度损失;作为对比,如果使用我们产品内置的Outline字体就没有类似的问题,因为Outline字体是我们过去为了支持跨平台,自己移植的Freetype字体,对应使用的是Freetype font engine, Freetype APIs的font size都是float类型的,从phsicial font size 转换为 logical font size时不会产生精度损失。为了支持中文,用户只能使用基于GDI render 的truetype字体,因为Freetype字体库中还没有支持中文的字体,这次看来必须要对Tcl/tk的font engine做个大手术,用DirectWrite float-based APIs 更换老旧的GDI APIs了。
tcl/tk测试代码如下
- proc zoominit {c {zfact {1.1}}}{
- # save zoom state in a global variable with the same name as the canvas handle
- upvar #0 $c data
- set data(zdepth)1.0
- }
- proc zoom {c fact}{
- upvar #0 $c data
- # zoom at the current mouse position
- set x [$c canvasx [expr {[winfo pointerx $c]-[winfo rootx $c]}]]
- set y [$c canvasy [expr {[winfo pointery $c]-[winfo rooty $c]}]]
- $c scale all $x $y $fact $fact
- # save new zoom depth
- set data(zdepth)[expr {$data(zdepth)* $fact}]
- # zoom text since the "canvas scale all" command doesn't account for text items
- zoomtext $c
- puts "zoom fact:$data(zdepth)"
- }
- proc zoomtext {c}{
- upvar #0 $c data
- # adjust fonts
- foreach{i}[$c find all]{
- if{![string equal [$c type $i] text]}{continue}
- set fontsize 0
- # get original fontsize and text from tags if they were previously recorded
- foreach{tag}[$c gettags $i]{
- scan $tag {_f%f} fontsize
- scan $tag "_t%\[^\0\]" text
- }
- # if not, then record current fontsize and text and use them
- set font [$c itemcget $i -font]
- if{!$fontsize}{
- set text [$c itemcget $i -text]
- set fontsize [font actual $font -size]
- $c addtag _f$fontsize withtag $i
- $c addtag _t$text withtag $i
- }
- # scale font
- set newsize [expr {$fontsize * $data(zdepth)}]
- set index [lsearch -exact $font -size]
- incr index
- set font [lreplace $font $index $index $newsize]
- $c itemconfigure $i -font $font -text $text -anchor w
- puts "font:$font"
- }
- # update canvas scrollregion
- set bbox [$c bbox all]
- if{[llength $bbox]}{
- $c configure -scrollregion $bbox
- }{
- $c configure -scrollregion [list -4-4 \
- [expr {[winfo width $c]-4}] \
- [expr {[winfo height $c]-4}]]
- }
- # move srollbar to topright in order to easily observe whether the zoomed text
- # goes outside of the rectangle
- $c xview moveto [winfo width $c]
- $c yview moveto 0
- }
- # test code
- set c [canvas .c -width 600-height 500 \
- -xscrollcommand ".shoriz set" \
- -yscrollcommand ".svert set"]
- # add x/y scrollbar for the canvas
- scrollbar .svert -orient v -command "$c yview"
- scrollbar .shoriz -orient h -command "$c xview"
- grid .c -row 0-column 0-columnspan 3-sticky news
- grid .svert -row 0-column 3-columnspan 1-sticky ns
- grid .shoriz -row 1-column 0-columnspan 3-sticky ew
- grid columnconfigure .0-weight 1
- grid columnconfigure .1-weight 1
- grid columnconfigure .2-weight 1
- grid rowconfigure .0-weight 1
- zoominit .c
- button .zoomin -text "Zoom In"-command "zoom $c 1.25"
- button .zoomout -text "Zoom Out"-command "zoom $c 0.8"
- grid .zoomin .zoomout
- set text "Hello, World!Here's a simple procedure called zoom that might help to get you started."
- # tfont is a truetype font rendering by Windows GDI APIs (GDI font engine)
- set tfont [font actual "-family Arial -size 8"]
- # ofont is a outline font rendering by freetype font engine transplanted by ourself
- set ofont [font actual "-family {OF Helvetica} -size 8"]
- .c create text 450-text $text -font $tfont -anchor w -tag tfont
- # create a rectangle with the same size of the bbox of the text. zoom in/out the canvas and we
- # can observe whether the text is zoomed in the same proportion as the rectangle
- .c create rect [.c bbox tfont]
- .c create text 4100-text $text -font $ofont -anchor w -tag ofont
- .c create rect [.c bbox ofont]
代码参考资源链接: 
Canvas zooming using mousewheel
 
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号