1

我试图获取字符串的宽度,使用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

我该如何解决?

4

1 回答 1

4

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

X.setFont display gc $ X.fontFromFontStruct font
于 2012-12-07T15:56:11.950 回答