aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-16 20:35:39 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-16 20:35:39 +0000
commit5f0351bb2a7f20b72b6a001e8891263d751c247a (patch)
treef064c91ebad9958a4b1c772f3d7b1b7c4a51099b /server/src/Thermoprint
parente9c92d233ba470d060f1ee0276120bc746b806ac (diff)
downloadthermoprint-5f0351bb2a7f20b72b6a001e8891263d751c247a.tar
thermoprint-5f0351bb2a7f20b72b6a001e8891263d751c247a.tar.gz
thermoprint-5f0351bb2a7f20b72b6a001e8891263d751c247a.tar.bz2
thermoprint-5f0351bb2a7f20b72b6a001e8891263d751c247a.tar.xz
thermoprint-5f0351bb2a7f20b72b6a001e8891263d751c247a.zip
Fixed bugs in linewrapping
Diffstat (limited to 'server/src/Thermoprint')
-rw-r--r--server/src/Thermoprint/Server/Printer/Generic.hs55
1 files changed, 39 insertions, 16 deletions
diff --git a/server/src/Thermoprint/Server/Printer/Generic.hs b/server/src/Thermoprint/Server/Printer/Generic.hs
index 2945dd0..24150cf 100644
--- a/server/src/Thermoprint/Server/Printer/Generic.hs
+++ b/server/src/Thermoprint/Server/Printer/Generic.hs
@@ -139,10 +139,17 @@ initDoc space = Doc { lines = Seq.empty
139 } 139 }
140 140
141breakLine :: Doc -> Doc 141breakLine :: Doc -> Doc
142breakLine doc@(Doc{..}) = doc { remainingSpace = space, lines = lines |> currentLine, currentLine = return () } 142breakLine doc@(Doc{..}) = doc { remainingSpace = space
143 , lines = lines |> currentLine
144 , currentLine = return ()
145 }
143 146
144renderDoc :: Doc -> Put 147renderDoc :: Doc -> Put
145renderDoc Doc{..} = intersperse' (newls' 1) id lines >> currentLine 148renderDoc Doc{..} = intersperse' (newls' 1) id lines'
149 where
150 lines'
151 | remainingSpace == space = lines
152 | otherwise = lines |> currentLine
146 153
147renderBlock :: Block -> State Doc () 154renderBlock :: Block -> State Doc ()
148renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine 155renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine
@@ -153,19 +160,35 @@ renderLine :: Line -> State Doc ()
153renderLine (HSpace n) = modify' insertSpace 160renderLine (HSpace n) = modify' insertSpace
154 where 161 where
155 insertSpace doc@(Doc{..}) 162 insertSpace doc@(Doc{..})
156 | remainingSpace > n = doc { remainingSpace = remainingSpace - n, currentLine = currentLine >> (sequence_ $ genericReplicate n $ encode' " ") } 163 | remainingSpace > n = doc { remainingSpace = remainingSpace - n
157 | remainingSpace == n = doc { remainingSpace = space, currentLine = return (), lines = lines |> currentLine } 164 , currentLine = currentLine >> (sequence_ $ genericReplicate n $ encode' " ")
158 | otherwise = doc { remainingSpace = space, lines = lines |> currentLine, currentLine = return (), overflows = overflows + 1 } 165 }
166 | remainingSpace == n = breakLine doc
167 | otherwise = breakLine $ doc { overflows = overflows + 1 }
159renderLine (JuxtaPos xs) = mapM_ renderLine xs 168renderLine (JuxtaPos xs) = mapM_ renderLine xs
160renderLine word = modify' $ insertWord 169renderLine (cotext . Line -> word) = modify' $ insertWord
161 where 170 where
162 insertWord doc@(Doc{..}) 171 insertWord doc
163 | remainingSpace > length = doc { remainingSpace = remainingSpace - (length + 1), currentLine = currentLine >> word'' } 172 | TL.null wordTail = checkBreak $ doc'
164 | remainingSpace == length = doc { remainingSpace = space, lines = lines |> (currentLine >> word''), currentLine = return () } 173 | otherwise = checkBreak $ doc' { remainingSpace = space doc' - (wordTail' `mod` space doc')
165 | space >= length = doc { remainingSpace = space - length, lines = lines |> currentLine, currentLine = word'' } 174 , lines = (lines doc' |> currentLine doc') <> cs
166 | length `div` space == 0 = doc { remainingSpace = space, lines = (lines |> currentLine) <> (cs |> c), currentLine = return (), overflows = overflows + (toEnum $ Seq.length cs) } 175 , currentLine = c
167 | otherwise = doc { remainingSpace = space - (length `div` space), lines = (lines |> currentLine) <> cs, currentLine = c, overflows = overflows + (toEnum $ Seq.length cs) } 176 , overflows = overflows doc' + 1 + (toInteger $ Seq.length cs)
168 word' = cotext (Line word) 177 }
169 word'' = encode' $ TL.unpack $ word' 178 where
170 length = toInteger $ TL.length word' 179 (wordInit, wordTail) = TL.splitAt (fromInteger $ remainingSpace doc) word
171 (cs :> c) = viewr . Seq.fromList . map (encode' . TL.unpack) $ TL.chunksOf (fromInteger length) word' 180 wordInit' = toInteger $ TL.length wordInit
181 wordTail' = toInteger $ TL.length wordTail
182 doc' = insertInit doc
183 insertInit doc@(Doc{..}) = doc { remainingSpace = remainingSpace - wordInit'
184 , currentLine = currentLine >> encode'' wordInit
185 }
186 (cs :> c) = viewr . Seq.fromList . map encode'' $ TL.chunksOf (fromInteger $ space doc) wordTail
187 checkBreak doc@(Doc{..})
188 | remainingSpace == 0 = doc { remainingSpace = space
189 , lines = lines |> currentLine
190 , currentLine = return ()
191 }
192 | otherwise = doc
193 encode'' = encode' . TL.unpack
194