adjust space handling to avoid a gap on the right

...which made the images not centred propertly
This commit is contained in:
Rhiannon Morris 2021-04-29 12:01:45 +02:00
parent 2607e26119
commit f243311e64
2 changed files with 11 additions and 6 deletions

View File

@ -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")

View File

@ -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'