【问题标题】:Why is X library reporting wrong string length values?为什么 X 库报告错误的字符串长度值?
【发布时间】:2012-12-07 14:34:08
【问题描述】:

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

我该如何解决?

【问题讨论】:

    标签: haskell x11 xlib xorg


    【解决方案1】:

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

    X.setFont display gc $ X.fontFromFontStruct font
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多