diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-16 20:35:39 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-16 20:35:39 +0000 |
commit | 5f0351bb2a7f20b72b6a001e8891263d751c247a (patch) | |
tree | f064c91ebad9958a4b1c772f3d7b1b7c4a51099b /server | |
parent | e9c92d233ba470d060f1ee0276120bc746b806ac (diff) | |
download | thermoprint-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')
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Generic.hs | 55 |
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 | ||
141 | breakLine :: Doc -> Doc | 141 | breakLine :: Doc -> Doc |
142 | breakLine doc@(Doc{..}) = doc { remainingSpace = space, lines = lines |> currentLine, currentLine = return () } | 142 | breakLine doc@(Doc{..}) = doc { remainingSpace = space |
143 | , lines = lines |> currentLine | ||
144 | , currentLine = return () | ||
145 | } | ||
143 | 146 | ||
144 | renderDoc :: Doc -> Put | 147 | renderDoc :: Doc -> Put |
145 | renderDoc Doc{..} = intersperse' (newls' 1) id lines >> currentLine | 148 | renderDoc Doc{..} = intersperse' (newls' 1) id lines' |
149 | where | ||
150 | lines' | ||
151 | | remainingSpace == space = lines | ||
152 | | otherwise = lines |> currentLine | ||
146 | 153 | ||
147 | renderBlock :: Block -> State Doc () | 154 | renderBlock :: Block -> State Doc () |
148 | renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine | 155 | renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine |
@@ -153,19 +160,35 @@ renderLine :: Line -> State Doc () | |||
153 | renderLine (HSpace n) = modify' insertSpace | 160 | renderLine (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 } | ||
159 | renderLine (JuxtaPos xs) = mapM_ renderLine xs | 168 | renderLine (JuxtaPos xs) = mapM_ renderLine xs |
160 | renderLine word = modify' $ insertWord | 169 | renderLine (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 | |||