ghcjs / diagrams-ghcjs

diagrams backend that renders directly to an HTML5 canvas
Other
23 stars 6 forks source link

Text support #3

Closed joelburget closed 10 years ago

joelburget commented 10 years ago

Preview. Oops, looks like the images are missing at the moment, but you should still be able to see the rendered text.

co-dan commented 10 years ago

That's brilliant, thanks a lot!

co-dan commented 10 years ago

This also requires adding the 'measureText' FFI to the ghcjs-canvas, right?

co-dan commented 10 years ago

Is this the implementation you had in mind?

measureText :: Text -> Context -> IO Double
measureText t ctx = js_measureText (toJSString t) ctx
                    >>= getProp "width"
                    >>= liftM fromJust . fromJSRef
{-# INLINE measureText #-}

foreign import javascript unsafe "$2.measureText($1)"
                                            js_measureText :: JSString  -> Context -> IO (JSRef a)                                                            
joelburget commented 10 years ago

Oops, forgot I made this change as well!

That's essentially what I had, though I had the width selection in Javascript rather than Haskell.

measureText :: Text -> Context -> IO Double
measureText t ctx = js_measureText (toJSString t) ctx

foreign import javascript unsafe "$2.measureText($1).width"
    js_measureText :: JSString -> Context -> IO Double