Skip to content

Commit f007c41

Browse files
authored
Add TextBaseline (#66)
1 parent ecaf945 commit f007c41

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Graphics/Canvas.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,20 @@ exports.setTextAlignImpl = function(ctx) {
321321
}
322322
};
323323

324+
exports.textBaselineImpl = function (ctx) {
325+
return function () {
326+
return ctx.textBaseline;
327+
}
328+
};
329+
330+
exports.setTextBaselineImpl = function (ctx) {
331+
return function (textBaseline) {
332+
return function () {
333+
ctx.textBaseline = textBaseline;
334+
}
335+
}
336+
};
337+
324338
exports.font = function(ctx) {
325339
return function() {
326340
return ctx.font;

src/Graphics/Canvas.purs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)