1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
{-# LANGUAGE CPP #-}
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.X11.CairoDraw
-- Copyright: (c) 2022 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: unportable
-- Created: Fri Sep 09, 2022 02:03
--
-- Drawing the xmobar contents using Cairo and Pango
--
--
------------------------------------------------------------------------------
module Xmobar.X11.CairoDraw (drawInPixmap) where
import qualified Data.Map as M
import qualified Data.Colour.SRGB as SRGB
import qualified Data.Colour.Names as CNames
import Control.Monad.IO.Class (liftIO)
import Control.Monad (foldM, when)
import Control.Monad.Reader (ask)
import qualified Graphics.X11.Xlib as X11
import qualified Graphics.Rendering.Cairo as Cairo
import qualified Graphics.Rendering.Pango as Pango
import Graphics.Rendering.Cairo.Types(Surface)
import qualified Xmobar.Config.Types as C
import qualified Xmobar.Config.Parse as ConfigParse
import qualified Xmobar.Run.Parsers as P
import qualified Xmobar.Text.Pango as TextPango
import qualified Xmobar.X11.Boxes as Boxes
import qualified Xmobar.X11.Bitmap as B
import qualified Xmobar.X11.Types as X
import Xmobar.X11.CairoSurface (withXlibSurface)
#ifdef XRENDER
import qualified Xmobar.X11.XRender as XRender
#endif
type Renderinfo = (P.Segment, Surface -> Double -> Double -> IO (), Double)
type BitmapDrawer = Double -> Double -> String -> IO ()
type Actions = [X.ActionPos]
data DrawContext = DC { dcBitmapDrawer :: BitmapDrawer
, dcBitmapLookup :: String -> Maybe B.Bitmap
, dcConfig :: C.Config
, dcWidth :: Double
, dcHeight :: Double
, dcSegments :: [[P.Segment]]
}
drawInPixmap :: X11.GC -> X11.Pixmap -> [[P.Segment]] -> X.X Actions
drawInPixmap gc p s = do
xconf <- ask
let disp = X.display xconf
vis = X11.defaultVisualOfScreen (X11.defaultScreenOfDisplay disp)
(X11.Rectangle _ _ w h) = X.rect xconf
dw = fromIntegral w
dh = fromIntegral h
conf = X.config xconf
dc = DC (drawXBitmap xconf gc p) (lookupXBitmap xconf) conf dw dh s
render = drawSegments dc
#ifdef XRENDER
color = C.bgColor conf
alph = C.alpha conf
liftIO $ XRender.drawBackground disp p color alph (X11.Rectangle 0 0 w h)
#endif
liftIO $ withXlibSurface disp p vis (fromIntegral w) (fromIntegral h) render
drawXBitmap :: X.XConf -> X11.GC -> X11.Pixmap -> BitmapDrawer
drawXBitmap xconf gc p h v path = do
let disp = X.display xconf
conf = X.config xconf
fc = C.fgColor conf
bc = C.bgColor conf
case lookupXBitmap xconf path of
Just bm -> liftIO $ B.drawBitmap disp p gc fc bc (round h) (round v) bm
Nothing -> return ()
lookupXBitmap :: X.XConf -> String -> Maybe B.Bitmap
lookupXBitmap xconf path = M.lookup path (X.iconCache xconf)
readColourName :: String -> (SRGB.Colour Double, Double)
readColourName str =
case CNames.readColourName str of
Just c -> (c, 1.0)
Nothing -> case SRGB.sRGB24reads str of
[(c, "")] -> (c, 1.0)
[(c,d)] -> (c, read ("0x" ++ d))
_ -> (CNames.white, 1.0)
setSourceColor :: (SRGB.Colour Double, Double) -> Cairo.Render ()
setSourceColor (colour, alph) =
if alph < 1 then Cairo.setSourceRGBA r g b alph else Cairo.setSourceRGB r g b
where rgb = SRGB.toSRGB colour
r = SRGB.channelRed rgb
g = SRGB.channelGreen rgb
b = SRGB.channelBlue rgb
renderLines :: String -> Double -> [Boxes.Line] -> Cairo.Render ()
renderLines color wd lns = do
setSourceColor (readColourName color)
Cairo.setLineWidth wd
mapM_ (\(x0, y0, x1, y1) ->
Cairo.moveTo x0 y0 >> Cairo.lineTo x1 y1 >> Cairo.stroke) lns
segmentMarkup :: C.Config -> P.Segment -> String
segmentMarkup conf (P.Text txt, info, idx, _actions) =
let fnt = TextPango.fixXft $ ConfigParse.indexedFont conf idx
(fg, bg) = P.colorComponents conf (P.tColorsString info)
attrs = [Pango.FontDescr fnt, Pango.FontForeground fg]
attrs' = if bg == C.bgColor conf
then attrs
else Pango.FontBackground bg:attrs
in Pango.markSpan attrs' $ Pango.escapeMarkup txt
segmentMarkup _ _ = ""
withRenderinfo :: Pango.PangoContext -> DrawContext -> P.Segment -> IO Renderinfo
withRenderinfo ctx dctx seg@(P.Text _, inf, idx, a) = do
let conf = dcConfig dctx
lyt <- Pango.layoutEmpty ctx
mk <- Pango.layoutSetMarkup lyt (segmentMarkup conf seg) :: IO String
(_, Pango.PangoRectangle o u w h) <- Pango.layoutGetExtents lyt
let voff' = fromIntegral $ ConfigParse.indexedOffset conf idx
voff = voff' + (dcHeight dctx - h + u) / 2.0
wd = w - o
slyt s off mx = do
when (off + w > mx) $ do
Pango.layoutSetEllipsize lyt Pango.EllipsizeEnd
Pango.layoutSetWidth lyt (Just $ mx - off)
Cairo.renderWith s $ Cairo.moveTo off voff >> Pango.showLayout lyt
return ((P.Text mk, inf, idx, a), slyt, wd)
withRenderinfo _ _ seg@(P.Hspace w, _, _, _) =
return (seg, \_ _ _ -> return (), fromIntegral w)
withRenderinfo _ dctx seg@(P.Icon p, _, _, _) = do
let bm = dcBitmapLookup dctx p
wd = maybe 0 (fromIntegral . B.width) bm
ioff = C.iconOffset (dcConfig dctx)
vpos = dcHeight dctx / 2 + fromIntegral ioff
draw _ off mx = when (off + wd <= mx) $ dcBitmapDrawer dctx off vpos p
return (seg, draw, wd)
drawBox :: DrawContext -> Surface -> Double -> Double -> P.Box -> IO ()
drawBox dctx surf x0 x1 box@(P.Box _ _ w color _) =
Cairo.renderWith surf $
renderLines color (fromIntegral w) (Boxes.boxLines box (dcHeight dctx) x0 x1)
drawSegmentBackground ::
DrawContext -> Surface -> P.TextRenderInfo -> Double -> Double -> IO ()
drawSegmentBackground dctx surf info x0 x1 =
when (bg /= C.bgColor conf && (top >= 0 || bot >= 0)) $
Cairo.renderWith surf $ do
setSourceColor (readColourName bg)
Cairo.rectangle x0 top (x1 - x0) (dcHeight dctx - bot - top)
Cairo.fillPreserve
where conf = dcConfig dctx
(_, bg) = P.colorComponents conf (P.tColorsString info)
top = fromIntegral $ P.tBgTopOffset info
bot = fromIntegral $ P.tBgBottomOffset info
type BoundedBox = (Double, Double, [P.Box])
type Acc = (Double, Actions, [BoundedBox])
drawSegment :: DrawContext -> Surface -> Double -> Acc -> Renderinfo -> IO Acc
drawSegment dctx surface maxoff (off, acts, boxs) (segment, render, lwidth) = do
let end = min maxoff (off + lwidth)
(_, info, _, a) = segment
acts' = case a of Just as -> (as, round off, round end):acts; _ -> acts
bs = P.tBoxes info
boxs' = if null bs then boxs else (off, end, bs):boxs
drawSegmentBackground dctx surface info off end
render surface off maxoff
return (off + lwidth, acts', boxs')
renderOuterBorder :: C.Config -> Double -> Double -> Cairo.Render ()
renderOuterBorder conf mw mh = do
let (x0, y0, w, h) = Boxes.borderRect (C.border conf) mw mh
setSourceColor (readColourName (C.borderColor conf))
Cairo.setLineWidth (fromIntegral (C.borderWidth conf))
Cairo.rectangle x0 y0 w h
Cairo.stroke
drawBorder :: C.Config -> Double -> Double -> Surface -> IO ()
drawBorder conf w h surf =
case C.border conf of
C.NoBorder -> return ()
_ -> Cairo.renderWith surf (renderOuterBorder conf w h)
drawBBox :: DrawContext -> Surface -> BoundedBox -> IO ()
drawBBox dctx surf (from, to, bs) = mapM_ (drawBox dctx surf from to) bs
drawBoxes :: DrawContext -> Surface -> [BoundedBox] -> IO ()
drawBoxes dctx surf ((from, to, b):(from', to', b'):bxs) =
if to < from' || b /= b'
then do drawBBox dctx surf (from, to, b)
drawBoxes dctx surf $ (from', to', b'):bxs
else drawBoxes dctx surf $ (from, to', b'):bxs
drawBoxes dctx surf [bi] = drawBBox dctx surf bi
drawBoxes _ _ [] = return ()
#ifndef XRENDER
drawCairoBackground :: DrawContext -> Surface -> IO ()
drawCairoBackground dctx surf = do
let (c, _) = readColourName (bgColor (dcConfig dctx))
Cairo.renderWith surf $ setSourceColor (c, 1.0) >> Cairo.paint
#endif
drawSegments :: DrawContext -> Surface -> IO Actions
drawSegments dctx surf = do
let [left, center, right] = take 3 $ dcSegments dctx
dh = dcHeight dctx
dw = dcWidth dctx
conf = dcConfig dctx
sWidth = foldl (\a (_,_,w) -> a + w) 0
ctx <- Pango.cairoCreateContext Nothing
llyts <- mapM (withRenderinfo ctx dctx) left
rlyts <- mapM (withRenderinfo ctx dctx) right
clyts <- mapM (withRenderinfo ctx dctx) center
#ifndef XRENDER
drawCairoBackground dctx surf
#endif
(lend, as, bx) <- foldM (drawSegment dctx surf dw) (0, [], []) llyts
let rw = sWidth rlyts
rstart = max (lend + 1) (dw - rw - 1)
cmax = rstart - 1
cw = sWidth clyts
cstart = lend + 1 + max 0 (dw - rw - lend - cw) / 2.0
(_, as', bx') <- foldM (drawSegment dctx surf cmax) (cstart, as, bx) clyts
(_, as'', bx'') <- foldM (drawSegment dctx surf dw) (rstart, as', bx') rlyts
drawBoxes dctx surf (reverse bx'')
when (C.borderWidth conf > 0) (drawBorder conf dw dh surf)
return as''
|