2012-12-07 63 views
1

我試圖獲得字符串的寬度,使用XTextWidth()函數,但由於某種原因,返回值總是大於實際顯示的字符串長度。在下面的例子中,打印出來的值是196,而如果我測量屏幕上的寬度,它的周圍168像素(以下是編譯和運行的例子)的地方:爲什麼X庫報告錯誤的字符串長度值?

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

您沒有使用所選字體顯示。試試這個:

X.setFont display gc $ X.fontFromFontStruct font 
相關問題