1// Copyright 2015 The TCell Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use file except in compliance with the License.
5// You may obtain a copy of the license at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package encoding
16
17import (
18	"github.com/gdamore/encoding"
19	"github.com/gdamore/tcell/v2"
20
21	"golang.org/x/text/encoding/charmap"
22	"golang.org/x/text/encoding/japanese"
23	"golang.org/x/text/encoding/korean"
24	"golang.org/x/text/encoding/simplifiedchinese"
25	"golang.org/x/text/encoding/traditionalchinese"
26)
27
28// Register registers all known encodings.  This is a short-cut to
29// add full character set support to your program.  Note that this can
30// add several megabytes to your program's size, because some of the encodings
31// are rather large (particularly those from East Asia.)
32func Register() {
33	// We supply latin1 and latin5, because Go doesn't
34	tcell.RegisterEncoding("ISO8859-1", encoding.ISO8859_1)
35	tcell.RegisterEncoding("ISO8859-9", encoding.ISO8859_9)
36
37	tcell.RegisterEncoding("ISO8859-10", charmap.ISO8859_10)
38	tcell.RegisterEncoding("ISO8859-13", charmap.ISO8859_13)
39	tcell.RegisterEncoding("ISO8859-14", charmap.ISO8859_14)
40	tcell.RegisterEncoding("ISO8859-15", charmap.ISO8859_15)
41	tcell.RegisterEncoding("ISO8859-16", charmap.ISO8859_16)
42	tcell.RegisterEncoding("ISO8859-2", charmap.ISO8859_2)
43	tcell.RegisterEncoding("ISO8859-3", charmap.ISO8859_3)
44	tcell.RegisterEncoding("ISO8859-4", charmap.ISO8859_4)
45	tcell.RegisterEncoding("ISO8859-5", charmap.ISO8859_5)
46	tcell.RegisterEncoding("ISO8859-6", charmap.ISO8859_6)
47	tcell.RegisterEncoding("ISO8859-7", charmap.ISO8859_7)
48	tcell.RegisterEncoding("ISO8859-8", charmap.ISO8859_8)
49	tcell.RegisterEncoding("KOI8-R", charmap.KOI8R)
50	tcell.RegisterEncoding("KOI8-U", charmap.KOI8U)
51
52	// Asian stuff
53	tcell.RegisterEncoding("EUC-JP", japanese.EUCJP)
54	tcell.RegisterEncoding("SHIFT_JIS", japanese.ShiftJIS)
55	tcell.RegisterEncoding("ISO2022JP", japanese.ISO2022JP)
56
57	tcell.RegisterEncoding("EUC-KR", korean.EUCKR)
58
59	tcell.RegisterEncoding("GB18030", simplifiedchinese.GB18030)
60	tcell.RegisterEncoding("GB2312", simplifiedchinese.HZGB2312)
61	tcell.RegisterEncoding("GBK", simplifiedchinese.GBK)
62
63	tcell.RegisterEncoding("Big5", traditionalchinese.Big5)
64
65	// Common aliaess
66	aliases := map[string]string{
67		"8859-1":      "ISO8859-1",
68		"ISO-8859-1":  "ISO8859-1",
69		"8859-13":     "ISO8859-13",
70		"ISO-8859-13": "ISO8859-13",
71		"8859-14":     "ISO8859-14",
72		"ISO-8859-14": "ISO8859-14",
73		"8859-15":     "ISO8859-15",
74		"ISO-8859-15": "ISO8859-15",
75		"8859-16":     "ISO8859-16",
76		"ISO-8859-16": "ISO8859-16",
77		"8859-2":      "ISO8859-2",
78		"ISO-8859-2":  "ISO8859-2",
79		"8859-3":      "ISO8859-3",
80		"ISO-8859-3":  "ISO8859-3",
81		"8859-4":      "ISO8859-4",
82		"ISO-8859-4":  "ISO8859-4",
83		"8859-5":      "ISO8859-5",
84		"ISO-8859-5":  "ISO8859-5",
85		"8859-6":      "ISO8859-6",
86		"ISO-8859-6":  "ISO8859-6",
87		"8859-7":      "ISO8859-7",
88		"ISO-8859-7":  "ISO8859-7",
89		"8859-8":      "ISO8859-8",
90		"ISO-8859-8":  "ISO8859-8",
91		"8859-9":      "ISO8859-9",
92		"ISO-8859-9":  "ISO8859-9",
93
94		"SJIS":        "Shift_JIS",
95		"EUCJP":       "EUC-JP",
96		"2022-JP":     "ISO2022JP",
97		"ISO-2022-JP": "ISO2022JP",
98
99		"EUCKR": "EUC-KR",
100
101		// ISO646 isn't quite exactly ASCII, but the 1991 IRV
102		// (international reference version) is so.  This helps
103		// some older systems that may use "646" for POSIX locales.
104		"646":    "US-ASCII",
105		"ISO646": "US-ASCII",
106
107		// Other names for UTF-8
108		"UTF8": "UTF-8",
109	}
110	for n, v := range aliases {
111		if enc := tcell.GetEncoding(v); enc != nil {
112			tcell.RegisterEncoding(n, enc)
113		}
114	}
115}
116