/
TextBaseline.purs
57 lines (47 loc) 路 1.89 KB
/
TextBaseline.purs
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
module Nemo.Patch.TextBaseline where
-- | Enumerates types of text alignment.
import Prelude
import Effect (Effect)
import Effect.Exception.Unsafe (unsafeThrow)
import Graphics.Canvas (Context2D)
-- | Enumerates types of text alignment.
data TextBaseline
= BaselineTop
| BaselineHanging
| BaselineMiddle
| BaselineAlphabetic
| BaselineIdeographic
| BaselineBottom
instance showTextBaseline :: Show TextBaseline where
show BaselineTop = "BaselineTop"
show BaselineHanging = "BaselineHanging"
show BaselineMiddle = "BaselineMiddle"
show BaselineAlphabetic = "BaselineAlphabetic"
show BaselineIdeographic = "BaselineIdeographic"
show BaselineBottom = "BaselineBottom"
foreign import textBaselineImpl :: Context2D -> Effect String
-- | Get the current text alignment.
textBaseline :: Context2D -> Effect TextBaseline
textBaseline ctx = unsafeParseTextBaseline <$> textBaselineImpl ctx
where
unsafeParseTextBaseline :: String -> TextBaseline
unsafeParseTextBaseline "top" = BaselineTop
unsafeParseTextBaseline "hanging" = BaselineHanging
unsafeParseTextBaseline "middle" = BaselineMiddle
unsafeParseTextBaseline "alphabetic" = BaselineAlphabetic
unsafeParseTextBaseline "ideographic" = BaselineIdeographic
unsafeParseTextBaseline "bottom" = BaselineBottom
unsafeParseTextBaseline align = unsafeThrow $ "invalid TextBaseline: " <> align
-- ^ dummy to silence compiler warnings
foreign import setTextBaselineImpl :: Context2D -> String -> Effect Unit
-- | Set the current text alignment.
setTextBaseline :: Context2D -> TextBaseline -> Effect Unit
setTextBaseline ctx textbaseline =
setTextBaselineImpl ctx (toString textbaseline)
where
toString BaselineTop = "top"
toString BaselineHanging = "hanging"
toString BaselineMiddle = "middle"
toString BaselineAlphabetic = "alphabetic"
toString BaselineIdeographic = "ideographic"
toString BaselineBottom = "bottom"