在下面的程序中,我正在绘制一些带有字母的小盒子cairo-0.12.2
.不幸的是,当我使用该translate
函数移动用户空间原点时,矩形被翻译但文本不是.
import Graphics.Rendering.Cairo
main = withSVGSurface "test.svg" 600 600
(`renderWith` draw)
draw = do
color white
rectangle 0 0 600 600
fill
newPath
color black
translate 300 300
drawSortBox
translate 200 200
drawSortBox
stroke
drawSortBox = do
showText "V ?"
a <- textExtents "V ?"
rectangle (textExtentsXbearing a - 2) (textExtentsYbearing a - 2) (textExtentsWidth a / 2 + 2) (textExtentsHeight a + 4)
rectangle (textExtentsXbearing a - 2) (textExtentsYbearing a - 2) (textExtentsWidth a + 4) (textExtentsHeight a + 4)
color (a,b,c) = setSourceRGB a b c
white = (255,255,255)
black =(0,0,0)
Run Code Online (Sandbox Code Playgroud)
根据文档,showText
从当前位置开始绘制文本.translate
移动原点,但不移动当前位置.您必须使用moveTo
而不是translate
选择文本的位置.(translate
碰巧第一次调用的事实与newPath
删除当前位置的事实有关.)