-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode.rb
More file actions
115 lines (100 loc) · 2.45 KB
/
unicode.rb
File metadata and controls
115 lines (100 loc) · 2.45 KB
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
require_relative 'ext'
class String
class << self
def codepoints(*cp)
cp.pack('U*')
end
end
SUPERSCRIPT = Hash.mapping(
'a' => "\u1d43",
'b' => "\u1d47",
'c' => "\u1d9c",
'd' => "\u1d48",
'e' => "\u1d49",
'f' => "\u1da0",
'g' => "\u1d4d",
'h' => "\u02b0",
'i' => "\u2071",
'j' => "\u02b2",
'k' => "\u1d4f",
'l' => "\u02e1",
'm' => "\u1d50",
'n' => "\u207f",
'o' => "\u1d52",
'p' => "\u1d56",
# 'q' => "\u????",
'r' => "\u02b3",
's' => "\u02e2",
't' => "\u1d57",
'u' => "\u1d58",
'v' => "\u1d5b",
'w' => "\u02b7",
'x' => "\u02e3",
'y' => "\u02b8",
'z' => "\u1dbb",
'0' => "\u2070",
'1' => "\u00b9",
'2' => "\u00b2",
'3' => "\u00b3",
)
SUBSCRIPT = Hash.mapping(
'a' => "\u2090",
'e' => "\u2091",
'i' => "\u1d62",
'j' => "\u2c7c",
'o' => "\u2092",
'r' => "\u1d63",
'u' => "\u1d64",
'v' => "\u1d65",
'x' => "\u2093",
)
(4..9).each do |n|
SUPERSCRIPT[n.to_s] = String.codepoints(0x2070 + n)
end
(0..9).each do |n|
SUBSCRIPT[n.to_s] = String.codepoints(0x2080 + n)
end
%w{+ - = ( )}.each_with_index do |c, i|
SUPERSCRIPT[c] = String.codepoints(0x207a + i)
SUBSCRIPT[c] = String.codepoints(0x208a + i)
end
DOUBLE_STRIKE = Hash.mapping(
'C' => "\u2102",
'H' => "\u210d",
'N' => "\u2115",
'P' => "\u2119",
'Q' => "\u211a",
'R' => "\u211d",
'Z' => "\u2124",
)
('A'..'Z').each_with_index do |c, i|
DOUBLE_STRIKE[c] = String.codepoints(0x1d538 + i) unless DOUBLE_STRIKE.key?(c)
end
('a'..'z').each_with_index do |c, i|
DOUBLE_STRIKE[c] = String.codepoints(0x1d552 + i)
end
def to_superscript
if size == 1
SUPERSCRIPT[self]
else
chars.map(&:to_superscript).join
end
end
def to_subscript
if size == 1
SUBSCRIPT[self]
else
chars.map(&:to_subscript).join
end
end
def overline
gsub(/./){|c| "#{c}\u0305" }
end
def double_strike
if size == 1
DOUBLE_STRIKE[self]
else
chars.map(&:double_strike).join
end
end
end