From f243311e6436a70c22da7879fac4ec5f4b256618 Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Thu, 29 Apr 2021 12:01:45 +0200 Subject: [PATCH] adjust space handling to avoid a gap on the right ...which made the images not centred propertly --- laantas-script/GlyphsBase.hs | 15 ++++++++++----- laantas-script/Svg.hs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/laantas-script/GlyphsBase.hs b/laantas-script/GlyphsBase.hs index 2018941..0ac2421 100644 --- a/laantas-script/GlyphsBase.hs +++ b/laantas-script/GlyphsBase.hs @@ -70,7 +70,8 @@ run m e@(E {size}) = where margin' = runReader margin e ascHeight = size * 3 - s = S {x = margin', y = margin' + ascHeight, textWidth = 0, textHeight = 0} + s = S {x = margin', y = margin' + ascHeight, + textWidth = 0, textHeight = 0, firstOnLine = True} type EGlyph = (Glyph, [Segs]) @@ -98,12 +99,15 @@ placeWord :: Word -> M Element placeWord w = do wwidth <- wordWidth w margin' <- margin - S {x} <- get + S {x, firstOnLine} <- get E {width} <- ask - if x > margin' && x + wwidth > width then + let space' = if firstOnLine then pure () else space + e <- if x > margin' && x + wwidth > width then do newline *> placeWord w else do - mconcat <$> traverse placeGlyph w <* space + mconcat <$> (space' *> traverse placeGlyph w) + modify \s -> s {firstOnLine = False} + pure e placeGlyph :: EGlyph -> M Element placeGlyph g@(G {path = path1}, segss) = do @@ -119,7 +123,8 @@ newline = do modify \s@(S {x, y, textWidth, textHeight}) -> s {x = m, y = y + lh, textWidth = textWidth `max` (x + m), - textHeight = textHeight + lh} + textHeight = textHeight + lh, + firstOnLine = True} toPx :: Double -> Text toPx x = pack (showFFloat (Just 4) x "px") diff --git a/laantas-script/Svg.hs b/laantas-script/Svg.hs index 99121d2..6f53a8c 100644 --- a/laantas-script/Svg.hs +++ b/laantas-script/Svg.hs @@ -14,7 +14,7 @@ import Data.Text (Text, pack) data Env = E {width, size, stroke :: !Double, color :: !Text} -data St = S {x, y, textWidth, textHeight :: !Double} +data St = S {x, y, textWidth, textHeight :: !Double, firstOnLine :: Bool} -- nb textHeight is one lineheight less than the actual height -- unless ending with a 'newline'