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
|
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Text.Emoji.Types
import Text.Emoji.DataFiles.EmojiTest
import Control.Applicative ((<|>))
import Control.Exception
import Control.Monad (join)
import Data.Attoparsec.Text (parse, feed, IResult (..))
import Data.Maybe (fromJust)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Ratio
import Data.Word
import Numeric (showHex, fromRat, showFFloat)
import Conduit
import qualified Data.XML.Types as XT
import qualified Text.XML.Stream.Parse as XML
import qualified Text.XML.Stream.Render as XML
import qualified Options.Applicative as O
import System.Environment
import System.Exit
import System.Directory (doesFileExist)
import System.FilePath ((</>))
import System.IO
flattenFilter :: EmojiTestEntry -> [EmojiTestEntry]
flattenFilter x@(Entry _ _ _ _) = [x]
flattenFilter (Group _ _ gs) = concatMap flattenFilter gs
flattenFilter (Comment _) = []
data PosterException
= ParseError String
deriving Show
instance Exception PosterException where
displayException (ParseError e) = "Parse Error: " ++ e
parseEmojiTest :: MonadThrow m => ConduitT T.Text EmojiTestEntry m ()
parseEmojiTest = do
parseResult <- (flip feed) T.empty <$> -- send end of input
foldlC updateParser (Partial (parse emojiTestFile))
case parseResult of
Partial _ -> throwM $ ParseError "Not enough input"
Fail _ _ m -> throwM $ ParseError m
Done _ r -> yieldMany r
where updateParser r t =
case r of
Partial _ -> feed r t
_ -> r
svgPath :: Config -> EmojiTestEntry -> FilePath
svgPath cfg (Entry codes _ _ _) = cfgSvgPath cfg </> filename
where filename = prefix ++ codes_string ++ ".svg"
(prefix, con) = case cfgFontType cfg of
Twemoji -> ("", '-')
Noto -> ("emoji_u", '_')
codes_string = tail . foldr (\f acc -> con:(f acc)) "" $
map showHex codes
svgPath _ _ = error "svgPath should receive only entries"
badElement :: XT.Name -> Bool
badElement n = XT.nameLocalName n == "image"
filterXMLEvent :: XT.Event -> Bool
filterXMLEvent ev =
case ev of
XT.EventBeginDocument -> False
XT.EventEndDocument -> False
XT.EventBeginDoctype _ _ -> False
XT.EventEndDoctype -> False
XT.EventInstruction _ -> False
XT.EventComment _ -> False
-- no image inclusions (only 4 times in noto or something)
XT.EventBeginElement n _ -> not (badElement n)
XT.EventEndElement n -> not (badElement n)
_ -> True
data SVGState
= SVGState
{ svgEmojiWidth :: Rational -- ^ width in cm
, svgEmojiHeight :: Rational -- ^ height in cm
, svgXEmojiCount :: Integer
, svgXIndex :: Integer
, svgYIndex :: Integer
}
advance :: SVGState -> SVGState
advance st =
if svgXEmojiCount st == svgXIndex st + 1
then st { svgXIndex = 0
, svgYIndex = svgYIndex st + 1 }
else st { svgXIndex = svgXIndex st + 1 }
type XMLAttrs = [(XT.Name, [XT.Content])]
setAttribute :: XT.Name -> XT.Content -> XMLAttrs -> XMLAttrs
setAttribute n c [] = [(n, [c])]
setAttribute n c ((name,content):xs) =
if name == n
then (n, [c]) : xs
else (name, content) : setAttribute n c xs
ratC :: Rational -> XT.Content
ratC r = XT.ContentText .
(<> "cm") . T.pack $ (showFFloat (Just 2) . fromRat) r ""
svgPosition :: SVGState -> XT.Event -> XT.Event
svgPosition st ev =
case ev of
XT.EventBeginElement n attrs ->
let w = svgEmojiWidth st
h = svgEmojiHeight st
x = w * fromIntegral (svgXIndex st)
y = h * fromIntegral (svgYIndex st)
in if XT.nameLocalName n == "svg"
then XT.EventBeginElement n
$ setAttribute "width" (ratC w)
. setAttribute "height" (ratC h)
. setAttribute "x" (ratC x)
. setAttribute "y" (ratC y)
$ attrs
else ev
_ -> ev
concatXMLEvs :: Monad m => Config -> ConduitT [XT.Event] XT.Event m ()
concatXMLEvs cfg =
let a0Width = 841 % 10
a0Height = 1189 % 10
emojiPerRow = cfgEmojiPerRow cfg
emojiSide = a0Width / fromIntegral emojiPerRow
initialState = SVGState
{ svgEmojiWidth = emojiSide
, svgEmojiHeight = emojiSide
, svgXEmojiCount = emojiPerRow
, svgXIndex = 0
, svgYIndex = 0
}
addSVG :: [XT.Event] -> SVGState -> (SVGState, [XT.Event])
addSVG els st = (advance st, map (svgPosition st) els)
in do
yield XT.EventBeginDocument
yield $ XT.EventBeginElement "{http://www.w3.org/2000/svg}svg"
[ ("width", [ ratC a0Width ])
, ("height", [ ratC a0Height ]) ]
concatMapAccumC addSVG initialState
yield $ XT.EventEndElement "{http://www.w3.org/2000/svg}svg"
yield XT.EventEndDocument
buildSVG :: Config -> IO ()
buildSVG cfg = withSourceFile (cfgEmojiTest cfg) $ \source -> do
runConduit
$ source
.| decodeUtf8C
.| parseEmojiTest
.| concatMapC flattenFilter
.| mapC (svgPath cfg)
.| filterMC doesFileExist
.| mapMC (\f -> withSourceFile f $ \xml -> runConduit $ xml
.| XML.detectUtf
.| XML.parseText XML.def
.| filterC filterXMLEvent
.| sinkList)
.| concatXMLEvs cfg
.| XML.renderBytes XML.def
.| stdoutC
data FontType = Twemoji | Noto
data Config
= Config
{ cfgSvgPath :: FilePath
, cfgEmojiTest :: FilePath
, cfgEmojiPerRow :: Integer
, cfgFontType :: FontType
}
config :: O.Parser Config
config = Config
<$> O.strOption
(O.long "svg-path"
<> O.metavar "PATH"
<> O.help "Directory containing the font's svgs")
<*> O.strOption
(O.long "emoji-test"
<> O.metavar "PATH"
<> O.help "Path to emoji-test.txt")
<*> O.option O.auto
(O.long "per-row"
<> O.metavar "INT"
<> O.value 100
<> O.help "how many emojis per row")
<*> (O.flag' Twemoji (O.long "twemoji" <> O.help "SVGs are from twemoji") <|>
O.flag' Noto (O.long "noto" <> O.help "SVGs are from noto-emoji"))
main :: IO ()
main = O.execParser opts >>= buildSVG
where opts = O.info config O.fullDesc
|