为什么X库报告错误的字符串长度值?

时间:2012-12-07 14:34:09

标签: haskell x11 xlib xorg

我试图使用XTextWidth()函数获取字符串的宽度,但由于某种原因,返回的值总是大于实际显示的字符串长度。在下面的示例中,打印出的值为196,而如果我在屏幕上测量宽度,则它大约为168像素(以下是可编辑和可运行的示例):

import Control.Concurrent
import qualified Graphics.X11.Xlib as X
import qualified Graphics.X11.Xlib.Types as Xt
import qualified Graphics.X11.Types as Xt

main = do
  display <- X.openDisplay ""
  let dflt = X.defaultScreen display
      border = X.blackPixel display dflt
      background = X.whitePixel display dflt
  rootw <- X.rootWindow display dflt
  win <- X.createSimpleWindow display rootw 0 0 500 300 1 border background
  X.mapWindow display win
  X.moveWindow display win 0 0

  updateScreen display win

updateScreen display win = do
  gc <- X.createGC display win
  bgcolor <- initColor display "white"
  fgcolor <- initColor display "black"
  X.setBackground display gc bgcolor
  X.setForeground display gc fgcolor

  font <- X.loadQueryFont display "-misc-fixed-*-*-*-*-14-*-*-*-*-*-*"
  let str = "Some reasonably long string."
      len = X.textWidth font str
  putStrLn $ show len
  X.drawImageString display win gc 0 50 str

  X.freeFont display font
  X.freeGC display gc
  threadDelay 100000
  updateScreen display win

initColor :: X.Display -> String -> IO X.Pixel
initColor dpy color = do
  let colormap = X.defaultColormap dpy (X.defaultScreen dpy)
  (apros,_) <- X.allocNamedColor dpy colormap color
  return $ X.color_pixel apros

我该如何解决?

1 个答案:

答案 0 :(得分:4)

您没有使用所选字体显示。试试这个:

X.setFont display gc $ X.fontFromFontStruct font