Tcl/tk缩放Truetype字体时的精度问题
最近有国内新客户抱怨我们产品显示的原理图太不专业了,在原理图上使用宋体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 handleupvar #0 $c dataset data(zdepth)1.0}proc zoom {c fact}{upvar #0 $c data# zoom at the current mouse positionset 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 depthset data(zdepth)[expr {$data(zdepth)* $fact}]# zoom text since the "canvas scale all" command doesn't account for text itemszoomtext $cputs "zoom fact:$data(zdepth)"}proc zoomtext {c}{upvar #0 $c data# adjust fontsforeach{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 recordedforeach{tag}[$c gettags $i]{scan $tag {_f%f} fontsizescan $tag "_t%\[^\0\]" text}# if not, then record current fontsize and text and use themset 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 fontset newsize [expr {$fontsize * $data(zdepth)}]set index [lsearch -exact $font -size]incr indexset font [lreplace $font $index $index $newsize]$c itemconfigure $i -font $font -text $text -anchor wputs "font:$font"}# update canvas scrollregionset 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 codeset c [canvas .c -width 600-height 500 \-xscrollcommand ".shoriz set" \-yscrollcommand ".svert set"]# add x/y scrollbar for the canvasscrollbar .svert -orient v -command "$c yview"scrollbar .shoriz -orient h -command "$c xview"grid .c -row 0-column 0-columnspan 3-sticky newsgrid .svert -row 0-column 3-columnspan 1-sticky nsgrid .shoriz -row 1-column 0-columnspan 3-sticky ewgrid columnconfigure .0-weight 1grid columnconfigure .1-weight 1grid columnconfigure .2-weight 1grid rowconfigure .0-weight 1zoominit .cbutton .zoomin -text "Zoom In"-command "zoom $c 1.25"button .zoomout -text "Zoom Out"-command "zoom $c 0.8"grid .zoomin .zoomoutset 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 ourselfset 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
Tcl/tk缩放Truetype字体时的精度问题的更多相关文章
- SpriteBuilder中使用TrueType字体的一些障碍
在实践中,有一些小的陷阱和障碍可能阻止你使用一般的TrueType字体. 第一个,必须要有一个有效的字体文件.在Finder中双击该.ttf文件,应该会打开Font Book app,显示一个象形符号 ...
- [转]TrueType字体结构
TrueType字体通常包含在单个TrueType字体文件中,其文件后缀为.TTF. OpenType字体是以类似于TrueType字体的格式编码的POSTSCRIPT字体.OPENTYPE字体使用. ...
- Tcl Tk Introduction
Tcl Tk Introduction eryar@163.com 摘要Abstract:Tcl/Tck脚本可以很容易实现用户自定义的命令,方便的创建图形化的用户界面GUI,所以Tcl和Tk的应用领域 ...
- freewrap——将tcl/tk脚本转变为可执行文件
FreeWrap可以把TCL/TK的脚本和二进制文件打包成应用程序,FreeWrap将所有的文件组合成一个单独的可执行文件. FreeWrap的原理是把脚本和tcl/tk解释器和库文件都打包 ...
- 用Tcl/Tk脚本计算圆周率
读了阮一峰的蒙特卡罗方法入门,用概率统计的方式求解棘手的数学问题还挺有意思的,尤其是利用正方形和它的内切圆之间的面积关系来建模求解圆周率的方法精巧又简单,比投针实验好理解也好实现多了.建模可不是M ...
- linux下ruby使用tcl/tk编程环境设置
正常情况下最新的ruby都是不带tcl/tk选项编译的,所以我们在运行tcl/tk代码时都会发生找不到tk库的错误.解决办法很简单只要以tcl/tk选项编译ruby即可. 这里以ubuntu 15.0 ...
- Mac OS X下让ruby支持tcl/tk
我记得在老早在OS X10.8下使用ruby1.9.x的时候只要到下载安装ActiveTcl8.5,没怎么配置就运行tk好好的.但是近日想重新执行下tk代码,发现在require 'tk'的时候就报错 ...
- OpenType字体与TrueType字体的区别
TrueType采用几何学中二次B样条曲线及直线来描述字体的外形轮廓,其特点是:TrueType既可以作打印字体,又可以用作屏幕显示:由于它是由指令对字形进行描述,因此它与分辨率无关,输出时总是按照打 ...
- CVE-2012-0774:Adobe Reader TrueType 字体整数溢出漏洞调试分析
0x01 TrueType 字体 TTF 字体是 Apple 和 Microsoft 两家公司共同推出的字体格式,现在已经广泛的运用于 Windows 操作系统,其中 PDF 文档也可以嵌入 TTF ...
随机推荐
- CocoaPods 更新慢&swift版本适配
一.更新慢的问题 使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的 ...
- 解析xml的问题未解决
工作上需要解析xml,目前的逻辑是:解析xml到对象中,然后对象再插入数据库.但这存在内存溢出的风险. 今天做的另外一件事是将循环用到工作上,暂时还没有测试,是否能保证程序的重启.有待观察 ##### ...
- 使用Python脚本操作MongoDB的教程
Reference: http://www.jb51.net/article/64225.htm
- TSP问题[动态规划]
分析: 有用的量:城市集合V={a,b,c,d,--} 所以我们用 T(i,V) 表示从 城市 i 出发遍历集合 V 中的城市一遍且仅一遍后回到 i 所用的最少费用(这里可能表达不好,底下会看到,但是 ...
- 关于String的相关常见方法
package Stirng类; /** * String 常见的相关方法摘要 * @author Administrator * */ public class DemoStringMethod { ...
- linux Cron 执行Django 任务计划
用shell 脚本调用python 脚本如下 #!/bin/bash export FLAVOR=liveexport PYTHONPATH=$PYTHONPATH:/home/alex/Django ...
- XML 特殊字符处理和 CDATA
在处理XML数据时,特殊字符要特殊处理,不能和节点字符混淆. 所有 XML 文档中的文本均会被解析器解析. 只有 CDATA 区段(CDATA section)中的文本会被解析器忽略. PCDATA ...
- C# Unity游戏开发——Excel中的数据是如何到游戏中的 (三)
本帖是延续的:C# Unity游戏开发——Excel中的数据是如何到游戏中的 (二) 前几天有点事情所以没有继续更新,今天我们接着说.上个帖子中我们看到已经把Excel数据生成了.bin的文件,不过其 ...
- redhat+11g+rac 安装数据库软件时只有一个节点可选
在安装数据库软件时,只能检测到一个节点 650) this.width=650;" title="捕获.JPG" src="http://s3.51cto.co ...
- saiku的源码包Bulid常见问题和jar包
最近在做kylin+mondrian+saiku的二次开发的时候,Bulid saiku的源码出现了很多问题,基本上一大部分问题jar找不到问题,很多jar国内网站都找不到.这时候只有手动下载然后注册 ...