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 where
margin' = runReader margin e margin' = runReader margin e
ascHeight = size * 3 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]) type EGlyph = (Glyph, [Segs])
@ -98,12 +99,15 @@ placeWord :: Word -> M Element
placeWord w = do placeWord w = do
wwidth <- wordWidth w wwidth <- wordWidth w
margin' <- margin margin' <- margin
S {x} <- get S {x, firstOnLine} <- get
E {width} <- ask 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 newline *> placeWord w
else do else do
mconcat <$> traverse placeGlyph w <* space mconcat <$> (space' *> traverse placeGlyph w)
modify \s -> s {firstOnLine = False}
pure e
placeGlyph :: EGlyph -> M Element placeGlyph :: EGlyph -> M Element
placeGlyph g@(G {path = path1}, segss) = do placeGlyph g@(G {path = path1}, segss) = do
@ -119,7 +123,8 @@ newline = do
modify \s@(S {x, y, textWidth, textHeight}) -> modify \s@(S {x, y, textWidth, textHeight}) ->
s {x = m, y = y + lh, s {x = m, y = y + lh,
textWidth = textWidth `max` (x + m), textWidth = textWidth `max` (x + m),
textHeight = textHeight + lh} textHeight = textHeight + lh,
firstOnLine = True}
toPx :: Double -> Text toPx :: Double -> Text
toPx x = pack (showFFloat (Just 4) x "px") 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 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 -- nb textHeight is one lineheight less than the actual height
-- unless ending with a 'newline' -- unless ending with a 'newline'