aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint/Server/Printer/Generic.hs
blob: ce818ee58699d939e6a3b99a5e75d504ada0b355 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}

module Thermoprint.Server.Printer.Generic
       ( genericPrint
       , mkPrintout
       ) 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 Data.Bits

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.Set (Set)
import qualified Data.Set as Set

import Data.Monoid

import Data.Int (Int64)

import Control.Concurrent (threadDelay)

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 (slowPut handle printout' >> return Nothing)
      where
        printout' = mkPrintout printout

mkPrintout :: Printout -> Lazy.ByteString
mkPrintout printout = runPut $ initialize >> render printout >> finalize

slowPut :: Handle -> Lazy.ByteString -> IO ()
slowPut h = slowPut' . LBS.split (LBS.last newl)
  where
    slowPut' []     = return ()
    slowPut' [t]    = LBS.hPutStr h t
    slowPut' (x:xs) = slowPut' [x] >> LBS.hPutStr h (LBS.singleton $ LBS.last newl) >> threadDelay (50 * 10^3) >> slowPut' xs

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 . getParagraphs

renderPar :: Paragraph -> Put
renderPar = mapM_ renderChunk . getChunks
  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
  , currentMarkup :: Set MarkupMode
  }

initDoc :: Integer -> Doc
initDoc space = Doc { lines          = Seq.empty
                    , currentLine    = return ()
                    , space          = space
                    , remainingSpace = space
                    , overflows      = 0
                    , currentMarkup  = Set.empty
                    }

breakLine :: Doc -> Doc
breakLine doc@(Doc{..}) = doc { remainingSpace = space
                              , lines = lines |> currentLine
                              , currentLine = return ()
                              }
  
renderDoc :: Doc -> Put
renderDoc Doc{..} = intersperse' (newls' 1) id lines'
  where
    lines'
      | remainingSpace == space = lines
      | otherwise               = lines |> currentLine

escSequence' :: [Word8] -> State Doc ()
escSequence' s = modify (\(doc@(Doc{..})) -> doc { currentLine = currentLine >> escSequence s })

setMarkup :: Set MarkupMode -> State Doc ()
setMarkup m = escSequence' [33, byte m] >> modify (\doc -> doc { currentMarkup = m })
  where
    byte = foldr (.|.) zeroBits . Set.map bitMask
    bitMask Bold = 8
    bitMask DoubleHeight = 16
    bitMask DoubleWidth  = 32
    bitMask Underline    = 128
    bitMask _            = zeroBits
 
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 = breakLine doc
      | otherwise = breakLine $ doc { overflows = overflows + 1 }
renderLine (Markup ms l)
  | Set.null ms = renderLine l
  | otherwise = do
      prevMarkup <- gets currentMarkup
      setMarkup (prevMarkup <> ms) >> renderLine l >> setMarkup prevMarkup
renderLine (JuxtaPos xs) = mapM_ renderLine xs
renderLine (cotext . Line -> word) = modify' $ insertWord
  where
    insertWord doc
      | TL.null wordTail = checkBreak $ doc'
      | otherwise = checkBreak $ doc { remainingSpace = space doc - (word' `mod` space doc)
                                     , lines = (lines doc |> currentLine doc) <> cs
                                     , currentLine = c
                                     , overflows = overflows doc + (toInteger $ Seq.length cs)
                                     }
      where
        (wordInit, wordTail) = TL.splitAt (fromInteger $ remainingSpace doc) word
        word' = toInteger $ TL.length word
        wordInit' = toInteger $ TL.length wordInit
        wordTail' = toInteger $ TL.length wordTail
        doc' = insertInit doc
        insertInit doc@(Doc{..}) = doc { remainingSpace = remainingSpace - wordInit'
                                       , currentLine = currentLine >> encode'' wordInit
                                       }
        (cs :> c) = viewr . Seq.fromList . map encode'' $ TL.chunksOf (fromInteger $ space doc) word
        checkBreak doc@(Doc{..})
          | remainingSpace == 0 = doc { remainingSpace = space
                                      , lines = lines |> currentLine
                                      , currentLine = return ()
                                      }
          | otherwise = doc
    encode'' = encode' . TL.unpack