Skip to content

Instantly share code, notes, and snippets.

@schell
Created October 11, 2018 20:07
Show Gist options
  • Save schell/412ad1ada6a7fe2e3e50a1c37457023f to your computer and use it in GitHub Desktop.
Save schell/412ad1ada6a7fe2e3e50a1c37457023f to your computer and use it in GitHub Desktop.
things to do with typograffiti
-- It would be nice to introduce more layout options. Something like annotated text
-- in iOS/UIKit.
-- | This is how Sandy uses cairo/pango.
-- https://github.com/isovector/sequoia/blob/master/src/Game/Sequoia.hs#L262-L284
renderElement :: Engine -> Element -> Cairo.Render ()
renderElement _ (TextElement (Text { textColor = (Color r g b a), .. })) = do
Cairo.save
layout <- Pango.createLayout textUTF8
Cairo.liftIO $ Pango.layoutSetAttributes layout
[ Pango.AttrFamily { paStart = i, paEnd = j, paFamily = textTypeface }
, Pango.AttrWeight { paStart = i, paEnd = j, paWeight = mapFontWeight textWeight }
, Pango.AttrStyle { paStart = i, paEnd = j, paStyle = mapFontStyle textStyle }
, Pango.AttrSize { paStart = i, paEnd = j, paSize = textHeight }
]
Pango.PangoRectangle x y w h <- fmap snd $ Cairo.liftIO $ Pango.layoutGetExtents layout
Cairo.translate ((-w / 2) -x) ((-h / 2) - y)
for_ textStroke $ \ls -> do
Pango.layoutPath layout
setLineStyle ls
Cairo.setSourceRGBA r g b a
Pango.showLayout layout
Cairo.restore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment