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 | |
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')
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Generic.hs | 30 | ||||
-rw-r--r-- | server/test/Thermoprint/Server/Printer/GenericSpec.hs | 69 | ||||
-rw-r--r-- | server/thermoprint-server.cabal | 3 | ||||
-rw-r--r-- | server/thermoprint-server.nix | 8 |
4 files changed, 103 insertions, 7 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 | |||
diff --git a/server/test/Thermoprint/Server/Printer/GenericSpec.hs b/server/test/Thermoprint/Server/Printer/GenericSpec.hs new file mode 100644 index 0000000..80096b0 --- /dev/null +++ b/server/test/Thermoprint/Server/Printer/GenericSpec.hs | |||
@@ -0,0 +1,69 @@ | |||
1 | {-# LANGUAGE OverloadedStrings, OverloadedLists #-} | ||
2 | {-# LANGUAGE StandaloneDeriving #-} | ||
3 | {-# LANGUAGE ViewPatterns #-} | ||
4 | |||
5 | module Thermoprint.Server.Printer.GenericSpec (spec) where | ||
6 | |||
7 | import Test.Hspec | ||
8 | import Test.Hspec.QuickCheck (prop) | ||
9 | import Test.QuickCheck (Property, Discard(..), property) | ||
10 | import Test.QuickCheck.Instances | ||
11 | |||
12 | import Thermoprint.Server.Printer.Generic | ||
13 | import Thermoprint.Printout | ||
14 | |||
15 | import Data.Text (Text) | ||
16 | import qualified Data.Text.Lazy as TL (pack) | ||
17 | |||
18 | import qualified Data.ByteString.Lazy as L (ByteString) | ||
19 | import qualified Data.ByteString.Lazy as L.BS | ||
20 | |||
21 | import Data.String (IsString(..)) | ||
22 | |||
23 | import Control.Monad (zipWithM_, join) | ||
24 | import Data.Monoid ((<>)) | ||
25 | import Data.Function (on) | ||
26 | |||
27 | import Data.Word | ||
28 | |||
29 | import Data.Sequence (Seq) | ||
30 | import qualified Data.Sequence as Seq | ||
31 | |||
32 | import Debug.Trace | ||
33 | |||
34 | instance IsString Line where | ||
35 | fromString = (\(Right l) -> l) . text . TL.pack | ||
36 | |||
37 | spec :: Spec | ||
38 | spec = do | ||
39 | describe "example printouts" $ | ||
40 | zipWithM_ example [1..] examples | ||
41 | where | ||
42 | example n (s, ts) = let str = show s | ||
43 | in specify str $ traceDump (mkPrintout . Printout . fmap (Paragraph . Seq.singleton) . fmap Cooked $ s) == ts | ||
44 | |||
45 | final, initial :: L.ByteString | ||
46 | initial = esc [64] <> esc [64] | ||
47 | final = "\n\n\n \n" | ||
48 | |||
49 | esc :: [Word8] -> L.ByteString | ||
50 | esc = L.BS.pack . (27:) | ||
51 | |||
52 | p :: L.ByteString -> L.ByteString | ||
53 | p = (<> final) . (initial <>) | ||
54 | |||
55 | traceDump :: L.ByteString -> L.ByteString | ||
56 | traceDump bs = traceShow (map (\b -> (b, (toEnum $ fromEnum b) :: Char)) $ L.BS.unpack bs) bs | ||
57 | |||
58 | examples :: [(Seq Block, L.ByteString)] | ||
59 | examples = [ ( [Line (JuxtaPos ["Hello", HSpace 1, "World!"])] | ||
60 | , p "Hello World!") | ||
61 | , ( [Line (JuxtaPos ["Hello", HSpace 4, "World!"])] | ||
62 | , p "Hello World!") | ||
63 | , ( [Line ("Par1"), Line ("Par2"), Line (JuxtaPos ["Par3", HSpace 1, "Word2"])] | ||
64 | , p "Par1\n\nPar2\n\nPar3 Word2") | ||
65 | , ( [Line (JuxtaPos ["Par1", HSpace 4]), NewlSep [VSpace 2, Line ("Par2")], Line (JuxtaPos ["Par3", HSpace 1, "Word2"])] | ||
66 | , p "Par1 \n\n\n\n\nPar2\n\nPar3 Word2") | ||
67 | , ( [Line (JuxtaPos [Markup [Bold] (JuxtaPos ["B",HSpace 1,Markup [Underline] "BM"]),HSpace 1,Markup [Bold,Underline] "BM"])] | ||
68 | , p $ esc [33, 8] <> "B " <> esc [33, 128 + 8] <> "BM" <> esc [33, 8] <> esc [33, 0] <> " " <> esc [33, 128 + 8] <> "BM" <> esc [33, 0]) | ||
69 | ] | ||
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal index 259abf3..c80351b 100644 --- a/server/thermoprint-server.cabal +++ b/server/thermoprint-server.cabal | |||
@@ -50,7 +50,7 @@ library | |||
50 | , servant-server >=0.4.4 && <1 | 50 | , servant-server >=0.4.4 && <1 |
51 | , stm >=2.4.4 && <3 | 51 | , stm >=2.4.4 && <3 |
52 | , text >=1.2.1 && <2 | 52 | , text >=1.2.1 && <2 |
53 | , thermoprint-spec ==4.0.* | 53 | , thermoprint-spec ==5.0.* |
54 | , time >=1.5.0 && <2 | 54 | , time >=1.5.0 && <2 |
55 | , wai >=3.0.4 && <4 | 55 | , wai >=3.0.4 && <4 |
56 | , warp >=3.1.9 && <4 | 56 | , warp >=3.1.9 && <4 |
@@ -94,6 +94,7 @@ Test-Suite tests | |||
94 | , containers >=0.5.6 && <1 | 94 | , containers >=0.5.6 && <1 |
95 | , async >=2.1.0 && <3 | 95 | , async >=2.1.0 && <3 |
96 | , http-types >=0.9.1 && <1 | 96 | , http-types >=0.9.1 && <1 |
97 | , bytestring >=0.10.6 && <1 | ||
97 | 98 | ||
98 | executable thermoprint-server | 99 | executable thermoprint-server |
99 | main-is: Main.hs | 100 | main-is: Main.hs |
diff --git a/server/thermoprint-server.nix b/server/thermoprint-server.nix index a40dddb..71a4211 100644 --- a/server/thermoprint-server.nix +++ b/server/thermoprint-server.nix | |||
@@ -25,10 +25,10 @@ mkDerivation { | |||
25 | base monad-logger mtl persistent-sqlite resourcet | 25 | base monad-logger mtl persistent-sqlite resourcet |
26 | ]; | 26 | ]; |
27 | testHaskellDepends = [ | 27 | testHaskellDepends = [ |
28 | async base containers exceptions hspec http-types monad-logger mtl | 28 | async base bytestring containers exceptions hspec http-types |
29 | persistent-sqlite QuickCheck quickcheck-instances resourcet stm | 29 | monad-logger mtl persistent-sqlite QuickCheck quickcheck-instances |
30 | temporary text thermoprint-client thermoprint-spec transformers | 30 | resourcet stm temporary text thermoprint-client thermoprint-spec |
31 | warp | 31 | transformers warp |
32 | ]; | 32 | ]; |
33 | homepage = "http://dirty-haskell.org/tags/thermoprint.html"; | 33 | homepage = "http://dirty-haskell.org/tags/thermoprint.html"; |
34 | description = "Server for thermoprint-spec"; | 34 | description = "Server for thermoprint-spec"; |