diff options
author | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2017-01-23 16:09:05 +0100 |
---|---|---|
committer | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2017-01-23 16:09:05 +0100 |
commit | 64b6ead0d1e157701f8569743eda496bc71b8351 (patch) | |
tree | f7ae09e9ec950e91cacb1923aa54b96d59c9a8f3 /server/src | |
parent | e95dac748371afcad3ffddf5c98e5fcb0a8302b7 (diff) | |
download | thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.gz thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.bz2 thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.xz thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.zip |
Add support for ESC/POS text decoration
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Generic.hs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/server/src/Thermoprint/Server/Printer/Generic.hs b/server/src/Thermoprint/Server/Printer/Generic.hs index f431e4f..ce818ee 100644 --- a/server/src/Thermoprint/Server/Printer/Generic.hs +++ b/server/src/Thermoprint/Server/Printer/Generic.hs | |||
@@ -6,6 +6,7 @@ | |||
6 | 6 | ||
7 | module Thermoprint.Server.Printer.Generic | 7 | module Thermoprint.Server.Printer.Generic |
8 | ( genericPrint | 8 | ( genericPrint |
9 | , mkPrintout | ||
9 | ) where | 10 | ) where |
10 | 11 | ||
11 | import Thermoprint.Printout | 12 | import Thermoprint.Printout |
@@ -31,6 +32,7 @@ import Data.Encoding | |||
31 | import Data.Encoding.CP437 | 32 | import Data.Encoding.CP437 |
32 | import Data.Binary.Put | 33 | import Data.Binary.Put |
33 | import Data.Word | 34 | import Data.Word |
35 | import Data.Bits | ||
34 | 36 | ||
35 | import Control.Monad | 37 | import Control.Monad |
36 | import Control.Monad.Reader | 38 | import Control.Monad.Reader |
@@ -47,6 +49,8 @@ import Control.Monad.Catch | |||
47 | import Data.Foldable | 49 | import Data.Foldable |
48 | 50 | ||
49 | import Data.List (genericReplicate, genericLength, intercalate) | 51 | import Data.List (genericReplicate, genericLength, intercalate) |
52 | import Data.Set (Set) | ||
53 | import qualified Data.Set as Set | ||
50 | 54 | ||
51 | import Data.Monoid | 55 | import Data.Monoid |
52 | 56 | ||
@@ -69,7 +73,10 @@ genericPrint' path = flip catches handlers . withFile path . print | |||
69 | ] | 73 | ] |
70 | print printout handle = $(logDebug) (T.pack $ show printout') >> liftIO (slowPut handle printout' >> return Nothing) | 74 | print printout handle = $(logDebug) (T.pack $ show printout') >> liftIO (slowPut handle printout' >> return Nothing) |
71 | where | 75 | where |
72 | printout' = runPut $ initialize >> render printout >> finalize | 76 | printout' = mkPrintout printout |
77 | |||
78 | mkPrintout :: Printout -> Lazy.ByteString | ||
79 | mkPrintout printout = runPut $ initialize >> render printout >> finalize | ||
73 | 80 | ||
74 | slowPut :: Handle -> Lazy.ByteString -> IO () | 81 | slowPut :: Handle -> Lazy.ByteString -> IO () |
75 | slowPut h = slowPut' . LBS.split (LBS.last newl) | 82 | slowPut h = slowPut' . LBS.split (LBS.last newl) |
@@ -128,6 +135,7 @@ data Doc = Doc | |||
128 | , space :: Integer | 135 | , space :: Integer |
129 | , remainingSpace :: Integer | 136 | , remainingSpace :: Integer |
130 | , overflows :: Integer | 137 | , overflows :: Integer |
138 | , currentMarkup :: Set MarkupMode | ||
131 | } | 139 | } |
132 | 140 | ||
133 | initDoc :: Integer -> Doc | 141 | initDoc :: Integer -> Doc |
@@ -136,6 +144,7 @@ initDoc space = Doc { lines = Seq.empty | |||
136 | , space = space | 144 | , space = space |
137 | , remainingSpace = space | 145 | , remainingSpace = space |
138 | , overflows = 0 | 146 | , overflows = 0 |
147 | , currentMarkup = Set.empty | ||
139 | } | 148 | } |
140 | 149 | ||
141 | breakLine :: Doc -> Doc | 150 | breakLine :: Doc -> Doc |
@@ -151,6 +160,19 @@ renderDoc Doc{..} = intersperse' (newls' 1) id lines' | |||
151 | | remainingSpace == space = lines | 160 | | remainingSpace == space = lines |
152 | | otherwise = lines |> currentLine | 161 | | otherwise = lines |> currentLine |
153 | 162 | ||
163 | escSequence' :: [Word8] -> State Doc () | ||
164 | escSequence' s = modify (\(doc@(Doc{..})) -> doc { currentLine = currentLine >> escSequence s }) | ||
165 | |||
166 | setMarkup :: Set MarkupMode -> State Doc () | ||
167 | setMarkup m = escSequence' [33, byte m] >> modify (\doc -> doc { currentMarkup = m }) | ||
168 | where | ||
169 | byte = foldr (.|.) zeroBits . Set.map bitMask | ||
170 | bitMask Bold = 8 | ||
171 | bitMask DoubleHeight = 16 | ||
172 | bitMask DoubleWidth = 32 | ||
173 | bitMask Underline = 128 | ||
174 | bitMask _ = zeroBits | ||
175 | |||
154 | renderBlock :: Block -> State Doc () | 176 | renderBlock :: Block -> State Doc () |
155 | renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine | 177 | renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine |
156 | renderBlock (NewlSep xs) = intersperse' (modify' breakLine) renderBlock xs | 178 | renderBlock (NewlSep xs) = intersperse' (modify' breakLine) renderBlock xs |
@@ -165,6 +187,11 @@ renderLine (HSpace n) = modify' insertSpace | |||
165 | } | 187 | } |
166 | | remainingSpace == n = breakLine doc | 188 | | remainingSpace == n = breakLine doc |
167 | | otherwise = breakLine $ doc { overflows = overflows + 1 } | 189 | | otherwise = breakLine $ doc { overflows = overflows + 1 } |
190 | renderLine (Markup ms l) | ||
191 | | Set.null ms = renderLine l | ||
192 | | otherwise = do | ||
193 | prevMarkup <- gets currentMarkup | ||
194 | setMarkup (prevMarkup <> ms) >> renderLine l >> setMarkup prevMarkup | ||
168 | renderLine (JuxtaPos xs) = mapM_ renderLine xs | 195 | renderLine (JuxtaPos xs) = mapM_ renderLine xs |
169 | renderLine (cotext . Line -> word) = modify' $ insertWord | 196 | renderLine (cotext . Line -> word) = modify' $ insertWord |
170 | where | 197 | where |
@@ -192,4 +219,3 @@ renderLine (cotext . Line -> word) = modify' $ insertWord | |||
192 | } | 219 | } |
193 | | otherwise = doc | 220 | | otherwise = doc |
194 | encode'' = encode' . TL.unpack | 221 | encode'' = encode' . TL.unpack |
195 | |||