Skip to content

Add TextBaseline #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Graphics/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@ exports.setTextAlignImpl = function(ctx) {
}
};

exports.textBaselineImpl = function (ctx) {
return function () {
return ctx.textBaseline;
}
};

exports.setTextBaselineImpl = function (ctx) {
return function (textBaseline) {
return function () {
ctx.textBaseline = textBaseline;
}
}
};

exports.font = function(ctx) {
return function() {
return ctx.font;
Expand Down
50 changes: 50 additions & 0 deletions src/Graphics/Canvas.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Graphics.Canvas
, Transform
, TranslateTransform
, TextAlign(..)
, TextBaseline(..)
, CanvasPattern
, PatternRepeat(..)
, CanvasGradient
Expand Down Expand Up @@ -75,6 +76,8 @@ module Graphics.Canvas

, textAlign
, setTextAlign
, textBaseline
, setTextBaseline
, font
, setFont
, fillText
Expand Down Expand Up @@ -521,6 +524,53 @@ setTextAlign ctx textalign =
toString AlignStart = "start"
toString AlignEnd = "end"

-- | Enumerates types of text baseline.
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 baseline.
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 baseline.
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"

-- | Text metrics:
-- |
-- | - The text width in pixels.
Expand Down