1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Thermoprint.Server.Printer.Generic
( genericPrint
) where
import Thermoprint.Printout
import Thermoprint.API (PrintingError(..))
import Thermoprint.Server.Printer
import System.FileLock
import System.IO
import Data.Sequence (Seq, (|>), (<|), ViewL(..), viewl, ViewR(..), viewr)
import qualified Data.Sequence as Seq
import qualified Data.ByteString.Lazy as Lazy (ByteString)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as CLBS
import qualified Data.Text.Lazy as Lazy (Text)
import qualified Data.Text.Lazy as TL
import qualified Data.Text as T
import Data.Encoding
import Data.Encoding.CP437
import Data.Binary.Put
import Data.Word
import Control.Monad
import Control.Monad.Reader
import Control.Monad.Trans.State
import Control.Monad.Logger
import Control.Monad.IO.Class
import Control.Monad.Trans.Resource
import Data.Acquire
import Control.Exception.Base (IOException)
import Control.Monad.Catch
import Data.Foldable
import Data.List (genericReplicate, genericLength, intercalate)
import Data.Monoid
import Data.Int (Int64)
import Prelude hiding (mapM_, sequence_, lines)
genericPrint :: FilePath -> PrinterMethod
genericPrint path = PM $ genericPrint' path
genericPrint' :: (MonadIO m, MonadMask m, MonadLogger m) => FilePath -> Printout -> m (Maybe PrintingError)
genericPrint' path = flip catches handlers . withFile path . print
where
withFile path f = flip withEx f $ mkAcquire (openFile path WriteMode >>= (\h -> h <$ hSetBuffering h NoBuffering)) hClose
handlers = [ Handler $ return . Just . IOError . (show :: IOException -> String)
, Handler $ return . Just . EncError
, Handler $ return . Just
]
print printout handle = $(logDebug) (T.pack $ show printout') >> liftIO (LBS.hPutStr handle printout' >> return Nothing)
where
printout' = runPut $ initialize >> render printout >> finalize
encode' :: ByteSink m => String -> m ()
encode' = encode CP437
width :: Integer
width = 32
esc :: Word8
esc = 27
escSequence :: [Word8] -> Put
escSequence = mapM_ pushWord8 . (esc:)
initialize :: Put
initialize = replicateM_ 2 $ escSequence [64]
newl :: Lazy.ByteString
newl = encodeLazyByteString CP437 "\n"
newls :: Integer -> Lazy.ByteString
newls i = mconcat $ genericReplicate i newl
newls' :: Integer -> Put
newls' = mapM_ pushWord8 . LBS.unpack . newls
finalize :: Put
finalize = newls' 3 >> encode' " " >> newls' 1
intersperse :: b -> (a -> b) -> Seq a -> Seq b
intersperse _ _ (viewl -> EmptyL) = Seq.empty
intersperse _ f (viewl -> x :< (viewl -> EmptyL)) = Seq.singleton $ f x
intersperse b f (viewl -> x :< xs) = f x <| b <| intersperse b f xs
intersperse' :: Monad m => m b -> (a -> m b) -> Seq a -> m ()
intersperse' b f = sequence_ . intersperse b f
render :: Printout -> Put
render = intersperse' (newls' 2) renderPar
renderPar :: Paragraph -> Put
renderPar = mapM_ renderChunk
where
renderChunk (Raw bs) = mapM_ pushWord8 $ LBS.unpack bs
renderChunk (Cooked block) = renderDoc $ execState (renderBlock block) (initDoc width)
data Doc = Doc
{ lines :: Seq Put
, currentLine :: Put
, space :: Integer
, remainingSpace :: Integer
, overflows :: Integer
}
initDoc :: Integer -> Doc
initDoc space = Doc { lines = Seq.empty
, currentLine = return ()
, space = space
, remainingSpace = space
, overflows = 0
}
breakLine :: Doc -> Doc
breakLine doc@(Doc{..}) = doc { remainingSpace = space, lines = lines |> currentLine, currentLine = return () }
renderDoc :: Doc -> Put
renderDoc Doc{..} = intersperse' (newls' 1) id lines >> currentLine
renderBlock :: Block -> State Doc ()
renderBlock (VSpace n) = sequence_ . genericReplicate n $ modify' breakLine
renderBlock (NewlSep xs) = intersperse' (modify' breakLine) renderBlock xs
renderBlock (Line x) = renderLine x
renderLine :: Line -> State Doc ()
renderLine (HSpace n) = modify' insertSpace
where
insertSpace doc@(Doc{..})
| remainingSpace > n = doc { remainingSpace = remainingSpace - n, currentLine = currentLine >> (sequence_ $ genericReplicate n $ encode' " ") }
| remainingSpace == n = doc { remainingSpace = space, currentLine = return (), lines = lines |> currentLine }
| otherwise = doc { remainingSpace = space, lines = lines |> currentLine, currentLine = return (), overflows = overflows + 1 }
renderLine (JuxtaPos xs) = mapM_ renderLine xs
renderLine word = modify' $ insertWord
where
insertWord doc@(Doc{..})
| remainingSpace > length = doc { remainingSpace = remainingSpace - (length + 1), currentLine = currentLine >> word'' }
| remainingSpace == length = doc { remainingSpace = space, lines = lines |> (currentLine >> word''), currentLine = return () }
| space >= length = doc { remainingSpace = space - length, lines = lines |> currentLine, currentLine = word'' }
| length `div` space == 0 = doc { remainingSpace = space, lines = (lines |> currentLine) <> (cs |> c), currentLine = return (), overflows = overflows + (toEnum $ Seq.length cs) }
| otherwise = doc { remainingSpace = space - (length `div` space), lines = (lines |> currentLine) <> cs, currentLine = c, overflows = overflows + (toEnum $ Seq.length cs) }
word' = cotext (Line word)
word'' = encode' $ TL.unpack $ word'
length = toInteger $ TL.length word'
(cs :> c) = viewr . Seq.fromList . map (encode' . TL.unpack) $ TL.chunksOf (fromInteger length) word'
|