aboutsummaryrefslogtreecommitdiff
path: root/lib/Shupai.hs
blob: cca8f304c685216991681551febdd0198f11719d (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
module Shupai 
  ( shupai
  )
where

import Data.Bits ((.&.), complement)
import Data.Char (ord)
import qualified Data.Map as Map
import Data.Function ((&))
import qualified Data.Maybe as Maybe
import Data.List.Split (chunksOf)
import Data.List (transpose)


isWideChar :: Char -> Bool
isWideChar c = inRange $ ord c where
  inRange x
    | x >= 0x1100 && x <= 0x115f = True
    | x >= 0x115f && x <= 0xa4cf && x /= 0x303f
        && (x .&. complement 0x0011) /= 0x300a = True
    | x >= 0xac00 && x <= 0xd7a3 = True
    | x >= 0xf900 && x <= 0xfaff = True
    | x >= 0xfe30 && x <= 0xfe6f = True
    | x >= 0xff00 && x <= 0xff5f = True
    | x >= 0xffe0 && x <= 0xffe6 = True
    | x >= 0x20000 && x <= 0x2ffff = True
    | otherwise = False

textPreprocess :: String -> String
textPreprocess s = s & filter isValid & map toFullWidth where
  isValid c = isWideChar c || Map.member c fullWidthMap
  toFullWidth c = Maybe.fromMaybe c $ Map.lookup c fullWidthMap

verticalFormat :: String -> String
verticalFormat s 
  | length s < 25 = verticalCompose $ chunksOf 5 s
  | otherwise = verticalCompose $ chunksOf (length s `div` 5 + 1) s

verticalCompose :: [String] -> String
verticalCompose lst = lst
  & fillSpace
  & transpose
  & map (addSpace . reverse)
  & unlines

addSpace :: String -> String
addSpace [] = []
addSpace [x] = [x]
addSpace (x:xs) = x : ' ' : addSpace xs

fillSpace :: [String] -> [String]
fillSpace [] = []
fillSpace [x] = [x]
fillSpace ls = go ls $ length (head ls) where
  go :: [String] -> Int -> [String]
  go [] _ = []
  go [x] len = [x ++ replicate (len - length x) '']
  go (x:xs) len = x : go xs len
    

shupai :: String -> String
shupai = verticalFormat . textPreprocess

fullWidthMap :: Map.Map Char Char
fullWidthMap = Map.fromList
  [ ('!', '')
  , ('"', '"')
  , ('#', '#')
  , ('$', '$')
  , ('%', '%')
  , ('&', '&')
  , ('\'', ''')
  , ('(', '(')
  , (')', ')')
  , ('*', '*')
  , ('+', '+')
  , (',', ',')
  , ('-', '-')
  , ('.', '。')
  , ('/', '/')
  , ('0', '0')
  , ('1', '1')
  , ('2', '2')
  , ('3', '3')
  , ('4', '4')
  , ('5', '5')
  , ('6', '6')
  , ('7', '7')
  , ('8', '8')
  , ('9', '9')
  , (':', ':')
  , (';', ';')
  , ('<', '<')
  , ('=', '=')
  , ('>', '>')
  , ('?', '?')
  , ('@', '@')
  , ('A', 'A')
  , ('B', 'B')
  , ('C', 'C')
  , ('D', 'D')
  , ('E', 'E')
  , ('F', 'F')
  , ('G', 'G')
  , ('H', 'H')
  , ('I', 'I')
  , ('J', 'J')
  , ('K', 'K')
  , ('L', 'L')
  , ('M', 'M')
  , ('N', 'N')
  , ('O', 'O')
  , ('P', 'P')
  , ('Q', 'Q')
  , ('R', 'R')
  , ('S', 'S')
  , ('T', 'T')
  , ('U', 'U')
  , ('V', 'V')
  , ('W', 'W')
  , ('X', 'X')
  , ('Y', 'Y')
  , ('Z', 'Z')
  , ('[', '[')
  , ('\\', '\')
  , (']', ']')
  , ('^', '^')
  , ('_', '_')
  , ('`', '`')
  , ('a', 'a')
  , ('b', 'b')
  , ('c', 'c')
  , ('d', 'd')
  , ('e', 'e')
  , ('f', 'f')
  , ('g', 'g')
  , ('h', 'h')
  , ('i', 'i')
  , ('j', 'j')
  , ('k', 'k')
  , ('l', 'l')
  , ('m', 'm')
  , ('n', 'n')
  , ('o', 'o')
  , ('p', 'p')
  , ('q', 'q')
  , ('r', 'r')
  , ('s', 's')
  , ('t', 't')
  , ('u', 'u')
  , ('v', 'v')
  , ('w', 'w')
  , ('x', 'x')
  , ('y', 'y')
  , ('z', 'z')
  , ('{', '{')
  , ('|', '|')
  , ('}', '}')
  , ('~', '~')
  , (' ', '・')
  ]