@@ -17,6 +17,7 @@ module Graphics.Canvas
1717 , Transform
1818 , TranslateTransform
1919 , TextAlign (..)
20+ , TextBaseline (..)
2021 , CanvasPattern
2122 , PatternRepeat (..)
2223 , CanvasGradient
@@ -75,6 +76,8 @@ module Graphics.Canvas
7576
7677 , textAlign
7778 , setTextAlign
79+ , textBaseline
80+ , setTextBaseline
7881 , font
7982 , setFont
8083 , fillText
@@ -527,6 +530,53 @@ setTextAlign ctx textalign =
527530 toString AlignStart = " start"
528531 toString AlignEnd = " end"
529532
533+ -- | Enumerates types of text baseline.
534+ data TextBaseline
535+ = BaselineTop
536+ | BaselineHanging
537+ | BaselineMiddle
538+ | BaselineAlphabetic
539+ | BaselineIdeographic
540+ | BaselineBottom
541+
542+ instance showTextBaseline :: Show TextBaseline where
543+ show BaselineTop = " BaselineTop"
544+ show BaselineHanging = " BaselineHanging"
545+ show BaselineMiddle = " BaselineMiddle"
546+ show BaselineAlphabetic = " BaselineAlphabetic"
547+ show BaselineIdeographic = " BaselineIdeographic"
548+ show BaselineBottom = " BaselineBottom"
549+
550+ foreign import textBaselineImpl :: Context2D -> Effect String
551+
552+ -- | Get the current text baseline.
553+ textBaseline :: Context2D -> Effect TextBaseline
554+ textBaseline ctx = unsafeParseTextBaseline <$> textBaselineImpl ctx
555+ where
556+ unsafeParseTextBaseline :: String -> TextBaseline
557+ unsafeParseTextBaseline " top" = BaselineTop
558+ unsafeParseTextBaseline " hanging" = BaselineHanging
559+ unsafeParseTextBaseline " middle" = BaselineMiddle
560+ unsafeParseTextBaseline " alphabetic" = BaselineAlphabetic
561+ unsafeParseTextBaseline " ideographic" = BaselineIdeographic
562+ unsafeParseTextBaseline " bottom" = BaselineBottom
563+ unsafeParseTextBaseline align = unsafeThrow $ " invalid TextBaseline: " <> align
564+ -- ^ dummy to silence compiler warnings
565+
566+ foreign import setTextBaselineImpl :: Context2D -> String -> Effect Unit
567+
568+ -- | Set the current text baseline.
569+ setTextBaseline :: Context2D -> TextBaseline -> Effect Unit
570+ setTextBaseline ctx textbaseline =
571+ setTextBaselineImpl ctx (toString textbaseline)
572+ where
573+ toString BaselineTop = " top"
574+ toString BaselineHanging = " hanging"
575+ toString BaselineMiddle = " middle"
576+ toString BaselineAlphabetic = " alphabetic"
577+ toString BaselineIdeographic = " ideographic"
578+ toString BaselineBottom = " bottom"
579+
530580-- | Text metrics:
531581-- |
532582-- | - The text width in pixels.
0 commit comments