runewidth.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package runewidth
  2. var EastAsianWidth = IsEastAsian()
  3. var DefaultCondition = &Condition{EastAsianWidth}
  4. type interval struct {
  5. first rune
  6. last rune
  7. }
  8. var combining = []interval{
  9. {0x0300, 0x036F}, {0x0483, 0x0486}, {0x0488, 0x0489},
  10. {0x0591, 0x05BD}, {0x05BF, 0x05BF}, {0x05C1, 0x05C2},
  11. {0x05C4, 0x05C5}, {0x05C7, 0x05C7}, {0x0600, 0x0603},
  12. {0x0610, 0x0615}, {0x064B, 0x065E}, {0x0670, 0x0670},
  13. {0x06D6, 0x06E4}, {0x06E7, 0x06E8}, {0x06EA, 0x06ED},
  14. {0x070F, 0x070F}, {0x0711, 0x0711}, {0x0730, 0x074A},
  15. {0x07A6, 0x07B0}, {0x07EB, 0x07F3}, {0x0901, 0x0902},
  16. {0x093C, 0x093C}, {0x0941, 0x0948}, {0x094D, 0x094D},
  17. {0x0951, 0x0954}, {0x0962, 0x0963}, {0x0981, 0x0981},
  18. {0x09BC, 0x09BC}, {0x09C1, 0x09C4}, {0x09CD, 0x09CD},
  19. {0x09E2, 0x09E3}, {0x0A01, 0x0A02}, {0x0A3C, 0x0A3C},
  20. {0x0A41, 0x0A42}, {0x0A47, 0x0A48}, {0x0A4B, 0x0A4D},
  21. {0x0A70, 0x0A71}, {0x0A81, 0x0A82}, {0x0ABC, 0x0ABC},
  22. {0x0AC1, 0x0AC5}, {0x0AC7, 0x0AC8}, {0x0ACD, 0x0ACD},
  23. {0x0AE2, 0x0AE3}, {0x0B01, 0x0B01}, {0x0B3C, 0x0B3C},
  24. {0x0B3F, 0x0B3F}, {0x0B41, 0x0B43}, {0x0B4D, 0x0B4D},
  25. {0x0B56, 0x0B56}, {0x0B82, 0x0B82}, {0x0BC0, 0x0BC0},
  26. {0x0BCD, 0x0BCD}, {0x0C3E, 0x0C40}, {0x0C46, 0x0C48},
  27. {0x0C4A, 0x0C4D}, {0x0C55, 0x0C56}, {0x0CBC, 0x0CBC},
  28. {0x0CBF, 0x0CBF}, {0x0CC6, 0x0CC6}, {0x0CCC, 0x0CCD},
  29. {0x0CE2, 0x0CE3}, {0x0D41, 0x0D43}, {0x0D4D, 0x0D4D},
  30. {0x0DCA, 0x0DCA}, {0x0DD2, 0x0DD4}, {0x0DD6, 0x0DD6},
  31. {0x0E31, 0x0E31}, {0x0E34, 0x0E3A}, {0x0E47, 0x0E4E},
  32. {0x0EB1, 0x0EB1}, {0x0EB4, 0x0EB9}, {0x0EBB, 0x0EBC},
  33. {0x0EC8, 0x0ECD}, {0x0F18, 0x0F19}, {0x0F35, 0x0F35},
  34. {0x0F37, 0x0F37}, {0x0F39, 0x0F39}, {0x0F71, 0x0F7E},
  35. {0x0F80, 0x0F84}, {0x0F86, 0x0F87}, {0x0F90, 0x0F97},
  36. {0x0F99, 0x0FBC}, {0x0FC6, 0x0FC6}, {0x102D, 0x1030},
  37. {0x1032, 0x1032}, {0x1036, 0x1037}, {0x1039, 0x1039},
  38. {0x1058, 0x1059}, {0x1160, 0x11FF}, {0x135F, 0x135F},
  39. {0x1712, 0x1714}, {0x1732, 0x1734}, {0x1752, 0x1753},
  40. {0x1772, 0x1773}, {0x17B4, 0x17B5}, {0x17B7, 0x17BD},
  41. {0x17C6, 0x17C6}, {0x17C9, 0x17D3}, {0x17DD, 0x17DD},
  42. {0x180B, 0x180D}, {0x18A9, 0x18A9}, {0x1920, 0x1922},
  43. {0x1927, 0x1928}, {0x1932, 0x1932}, {0x1939, 0x193B},
  44. {0x1A17, 0x1A18}, {0x1B00, 0x1B03}, {0x1B34, 0x1B34},
  45. {0x1B36, 0x1B3A}, {0x1B3C, 0x1B3C}, {0x1B42, 0x1B42},
  46. {0x1B6B, 0x1B73}, {0x1DC0, 0x1DCA}, {0x1DFE, 0x1DFF},
  47. {0x200B, 0x200F}, {0x202A, 0x202E}, {0x2060, 0x2063},
  48. {0x206A, 0x206F}, {0x20D0, 0x20EF}, {0x302A, 0x302F},
  49. {0x3099, 0x309A}, {0xA806, 0xA806}, {0xA80B, 0xA80B},
  50. {0xA825, 0xA826}, {0xFB1E, 0xFB1E}, {0xFE00, 0xFE0F},
  51. {0xFE20, 0xFE23}, {0xFEFF, 0xFEFF}, {0xFFF9, 0xFFFB},
  52. {0x10A01, 0x10A03}, {0x10A05, 0x10A06}, {0x10A0C, 0x10A0F},
  53. {0x10A38, 0x10A3A}, {0x10A3F, 0x10A3F}, {0x1D167, 0x1D169},
  54. {0x1D173, 0x1D182}, {0x1D185, 0x1D18B}, {0x1D1AA, 0x1D1AD},
  55. {0x1D242, 0x1D244}, {0xE0001, 0xE0001}, {0xE0020, 0xE007F},
  56. {0xE0100, 0xE01EF},
  57. }
  58. type ctype int
  59. const (
  60. narrow ctype = iota
  61. ambiguous
  62. wide
  63. halfwidth
  64. fullwidth
  65. neutral
  66. )
  67. type intervalType struct {
  68. first rune
  69. last rune
  70. ctype ctype
  71. }
  72. var ctypes = []intervalType{
  73. {0x0020, 0x007E, narrow},
  74. {0x00A1, 0x00A1, ambiguous},
  75. {0x00A2, 0x00A3, narrow},
  76. {0x00A4, 0x00A4, ambiguous},
  77. {0x00A5, 0x00A6, narrow},
  78. {0x00A7, 0x00A8, ambiguous},
  79. {0x00AA, 0x00AA, ambiguous},
  80. {0x00AC, 0x00AC, narrow},
  81. {0x00AD, 0x00AE, ambiguous},
  82. {0x00AF, 0x00AF, narrow},
  83. {0x00B0, 0x00B4, ambiguous},
  84. {0x00B6, 0x00BA, ambiguous},
  85. {0x00BC, 0x00BF, ambiguous},
  86. {0x00C6, 0x00C6, ambiguous},
  87. {0x00D0, 0x00D0, ambiguous},
  88. {0x00D7, 0x00D8, ambiguous},
  89. {0x00DE, 0x00E1, ambiguous},
  90. {0x00E6, 0x00E6, ambiguous},
  91. {0x00E8, 0x00EA, ambiguous},
  92. {0x00EC, 0x00ED, ambiguous},
  93. {0x00F0, 0x00F0, ambiguous},
  94. {0x00F2, 0x00F3, ambiguous},
  95. {0x00F7, 0x00FA, ambiguous},
  96. {0x00FC, 0x00FC, ambiguous},
  97. {0x00FE, 0x00FE, ambiguous},
  98. {0x0101, 0x0101, ambiguous},
  99. {0x0111, 0x0111, ambiguous},
  100. {0x0113, 0x0113, ambiguous},
  101. {0x011B, 0x011B, ambiguous},
  102. {0x0126, 0x0127, ambiguous},
  103. {0x012B, 0x012B, ambiguous},
  104. {0x0131, 0x0133, ambiguous},
  105. {0x0138, 0x0138, ambiguous},
  106. {0x013F, 0x0142, ambiguous},
  107. {0x0144, 0x0144, ambiguous},
  108. {0x0148, 0x014B, ambiguous},
  109. {0x014D, 0x014D, ambiguous},
  110. {0x0152, 0x0153, ambiguous},
  111. {0x0166, 0x0167, ambiguous},
  112. {0x016B, 0x016B, ambiguous},
  113. {0x01CE, 0x01CE, ambiguous},
  114. {0x01D0, 0x01D0, ambiguous},
  115. {0x01D2, 0x01D2, ambiguous},
  116. {0x01D4, 0x01D4, ambiguous},
  117. {0x01D6, 0x01D6, ambiguous},
  118. {0x01D8, 0x01D8, ambiguous},
  119. {0x01DA, 0x01DA, ambiguous},
  120. {0x01DC, 0x01DC, ambiguous},
  121. {0x0251, 0x0251, ambiguous},
  122. {0x0261, 0x0261, ambiguous},
  123. {0x02C4, 0x02C4, ambiguous},
  124. {0x02C7, 0x02C7, ambiguous},
  125. {0x02C9, 0x02CB, ambiguous},
  126. {0x02CD, 0x02CD, ambiguous},
  127. {0x02D0, 0x02D0, ambiguous},
  128. {0x02D8, 0x02DB, ambiguous},
  129. {0x02DD, 0x02DD, ambiguous},
  130. {0x02DF, 0x02DF, ambiguous},
  131. {0x0300, 0x036F, ambiguous},
  132. {0x0391, 0x03A2, ambiguous},
  133. {0x03A3, 0x03A9, ambiguous},
  134. {0x03B1, 0x03C1, ambiguous},
  135. {0x03C3, 0x03C9, ambiguous},
  136. {0x0401, 0x0401, ambiguous},
  137. {0x0410, 0x044F, ambiguous},
  138. {0x0451, 0x0451, ambiguous},
  139. {0x1100, 0x115F, wide},
  140. {0x2010, 0x2010, ambiguous},
  141. {0x2013, 0x2016, ambiguous},
  142. {0x2018, 0x2019, ambiguous},
  143. {0x201C, 0x201D, ambiguous},
  144. {0x2020, 0x2022, ambiguous},
  145. {0x2024, 0x2027, ambiguous},
  146. {0x2030, 0x2030, ambiguous},
  147. {0x2032, 0x2033, ambiguous},
  148. {0x2035, 0x2035, ambiguous},
  149. {0x203B, 0x203B, ambiguous},
  150. {0x203E, 0x203E, ambiguous},
  151. {0x2074, 0x2074, ambiguous},
  152. {0x207F, 0x207F, ambiguous},
  153. {0x2081, 0x2084, ambiguous},
  154. {0x20A9, 0x20A9, halfwidth},
  155. {0x20AC, 0x20AC, ambiguous},
  156. {0x2103, 0x2103, ambiguous},
  157. {0x2105, 0x2105, ambiguous},
  158. {0x2109, 0x2109, ambiguous},
  159. {0x2113, 0x2113, ambiguous},
  160. {0x2116, 0x2116, ambiguous},
  161. {0x2121, 0x2122, ambiguous},
  162. {0x2126, 0x2126, ambiguous},
  163. {0x212B, 0x212B, ambiguous},
  164. {0x2153, 0x2154, ambiguous},
  165. {0x215B, 0x215E, ambiguous},
  166. {0x2160, 0x216B, ambiguous},
  167. {0x2170, 0x2179, ambiguous},
  168. {0x2189, 0x218A, ambiguous},
  169. {0x2190, 0x2199, ambiguous},
  170. {0x21B8, 0x21B9, ambiguous},
  171. {0x21D2, 0x21D2, ambiguous},
  172. {0x21D4, 0x21D4, ambiguous},
  173. {0x21E7, 0x21E7, ambiguous},
  174. {0x2200, 0x2200, ambiguous},
  175. {0x2202, 0x2203, ambiguous},
  176. {0x2207, 0x2208, ambiguous},
  177. {0x220B, 0x220B, ambiguous},
  178. {0x220F, 0x220F, ambiguous},
  179. {0x2211, 0x2211, ambiguous},
  180. {0x2215, 0x2215, ambiguous},
  181. {0x221A, 0x221A, ambiguous},
  182. {0x221D, 0x2220, ambiguous},
  183. {0x2223, 0x2223, ambiguous},
  184. {0x2225, 0x2225, ambiguous},
  185. {0x2227, 0x222C, ambiguous},
  186. {0x222E, 0x222E, ambiguous},
  187. {0x2234, 0x2237, ambiguous},
  188. {0x223C, 0x223D, ambiguous},
  189. {0x2248, 0x2248, ambiguous},
  190. {0x224C, 0x224C, ambiguous},
  191. {0x2252, 0x2252, ambiguous},
  192. {0x2260, 0x2261, ambiguous},
  193. {0x2264, 0x2267, ambiguous},
  194. {0x226A, 0x226B, ambiguous},
  195. {0x226E, 0x226F, ambiguous},
  196. {0x2282, 0x2283, ambiguous},
  197. {0x2286, 0x2287, ambiguous},
  198. {0x2295, 0x2295, ambiguous},
  199. {0x2299, 0x2299, ambiguous},
  200. {0x22A5, 0x22A5, ambiguous},
  201. {0x22BF, 0x22BF, ambiguous},
  202. {0x2312, 0x2312, ambiguous},
  203. {0x2329, 0x232A, wide},
  204. {0x2460, 0x24E9, ambiguous},
  205. {0x24EB, 0x254B, ambiguous},
  206. {0x2550, 0x2573, ambiguous},
  207. {0x2580, 0x258F, ambiguous},
  208. {0x2592, 0x2595, ambiguous},
  209. {0x25A0, 0x25A1, ambiguous},
  210. {0x25A3, 0x25A9, ambiguous},
  211. {0x25B2, 0x25B3, ambiguous},
  212. {0x25B6, 0x25B7, ambiguous},
  213. {0x25BC, 0x25BD, ambiguous},
  214. {0x25C0, 0x25C1, ambiguous},
  215. {0x25C6, 0x25C8, ambiguous},
  216. {0x25CB, 0x25CB, ambiguous},
  217. {0x25CE, 0x25D1, ambiguous},
  218. {0x25E2, 0x25E5, ambiguous},
  219. {0x25EF, 0x25EF, ambiguous},
  220. {0x2605, 0x2606, ambiguous},
  221. {0x2609, 0x2609, ambiguous},
  222. {0x260E, 0x260F, ambiguous},
  223. {0x2614, 0x2615, ambiguous},
  224. {0x261C, 0x261C, ambiguous},
  225. {0x261E, 0x261E, ambiguous},
  226. {0x2640, 0x2640, ambiguous},
  227. {0x2642, 0x2642, ambiguous},
  228. {0x2660, 0x2661, ambiguous},
  229. {0x2663, 0x2665, ambiguous},
  230. {0x2667, 0x266A, ambiguous},
  231. {0x266C, 0x266D, ambiguous},
  232. {0x266F, 0x266F, ambiguous},
  233. {0x269E, 0x269F, ambiguous},
  234. {0x26BE, 0x26BF, ambiguous},
  235. {0x26C4, 0x26CD, ambiguous},
  236. {0x26CF, 0x26E1, ambiguous},
  237. {0x26E3, 0x26E3, ambiguous},
  238. {0x26E8, 0x26FF, ambiguous},
  239. {0x273D, 0x273D, ambiguous},
  240. {0x2757, 0x2757, ambiguous},
  241. {0x2776, 0x277F, ambiguous},
  242. {0x27E6, 0x27ED, narrow},
  243. {0x2985, 0x2986, narrow},
  244. {0x2B55, 0x2B59, ambiguous},
  245. {0x2E80, 0x2E9A, wide},
  246. {0x2E9B, 0x2EF4, wide},
  247. {0x2F00, 0x2FD6, wide},
  248. {0x2FF0, 0x2FFC, wide},
  249. {0x3000, 0x3000, fullwidth},
  250. {0x3001, 0x303E, wide},
  251. {0x3041, 0x3097, wide},
  252. {0x3099, 0x3100, wide},
  253. {0x3105, 0x312E, wide},
  254. {0x3131, 0x318F, wide},
  255. {0x3190, 0x31BB, wide},
  256. {0x31C0, 0x31E4, wide},
  257. {0x31F0, 0x321F, wide},
  258. {0x3220, 0x3247, wide},
  259. {0x3248, 0x324F, ambiguous},
  260. {0x3250, 0x32FF, wide},
  261. {0x3300, 0x4DBF, wide},
  262. {0x4E00, 0xA48D, wide},
  263. {0xA490, 0xA4C7, wide},
  264. {0xA960, 0xA97D, wide},
  265. {0xAC00, 0xD7A4, wide},
  266. {0xE000, 0xF8FF, ambiguous},
  267. {0xF900, 0xFAFF, wide},
  268. {0xFE00, 0xFE0F, ambiguous},
  269. {0xFE10, 0xFE1A, wide},
  270. {0xFE30, 0xFE53, wide},
  271. {0xFE54, 0xFE67, wide},
  272. {0xFE68, 0xFE6C, wide},
  273. {0xFF01, 0xFF60, fullwidth},
  274. {0xFF61, 0xFFBF, halfwidth},
  275. {0xFFC2, 0xFFC8, halfwidth},
  276. {0xFFCA, 0xFFD0, halfwidth},
  277. {0xFFD2, 0xFFD8, halfwidth},
  278. {0xFFDA, 0xFFDD, halfwidth},
  279. {0xFFE0, 0xFFE7, fullwidth},
  280. {0xFFE8, 0xFFEF, halfwidth},
  281. {0xFFFD, 0xFFFE, ambiguous},
  282. {0x1B000, 0x1B002, wide},
  283. {0x1F100, 0x1F10A, ambiguous},
  284. {0x1F110, 0x1F12D, ambiguous},
  285. {0x1F130, 0x1F169, ambiguous},
  286. {0x1F170, 0x1F19B, ambiguous},
  287. {0x1F200, 0x1F203, wide},
  288. {0x1F210, 0x1F23B, wide},
  289. {0x1F240, 0x1F249, wide},
  290. {0x1F250, 0x1F252, wide},
  291. {0x20000, 0x2FFFE, wide},
  292. {0x30000, 0x3FFFE, wide},
  293. {0xE0100, 0xE01F0, ambiguous},
  294. {0xF0000, 0xFFFFD, ambiguous},
  295. {0x100000, 0x10FFFE, ambiguous},
  296. }
  297. type Condition struct {
  298. EastAsianWidth bool
  299. }
  300. func NewCondition() *Condition {
  301. return &Condition{EastAsianWidth}
  302. }
  303. // RuneWidth returns the number of cells in r.
  304. // See http://www.unicode.org/reports/tr11/
  305. func (c *Condition) RuneWidth(r rune) int {
  306. if r == 0 {
  307. return 0
  308. }
  309. if r < 32 || (r >= 0x7f && r < 0xa0) {
  310. return 1
  311. }
  312. for _, iv := range combining {
  313. if iv.first <= r && r <= iv.last {
  314. return 0
  315. }
  316. }
  317. if c.EastAsianWidth && IsAmbiguousWidth(r) {
  318. return 2
  319. }
  320. if r >= 0x1100 &&
  321. (r <= 0x115f || r == 0x2329 || r == 0x232a ||
  322. (r >= 0x2e80 && r <= 0xa4cf && r != 0x303f) ||
  323. (r >= 0xac00 && r <= 0xd7a3) ||
  324. (r >= 0xf900 && r <= 0xfaff) ||
  325. (r >= 0xfe30 && r <= 0xfe6f) ||
  326. (r >= 0xff00 && r <= 0xff60) ||
  327. (r >= 0xffe0 && r <= 0xffe6) ||
  328. (r >= 0x20000 && r <= 0x2fffd) ||
  329. (r >= 0x30000 && r <= 0x3fffd)) {
  330. return 2
  331. }
  332. return 1
  333. }
  334. func (c *Condition) StringWidth(s string) (width int) {
  335. for _, r := range []rune(s) {
  336. width += c.RuneWidth(r)
  337. }
  338. return width
  339. }
  340. func (c *Condition) Truncate(s string, w int, tail string) string {
  341. if c.StringWidth(s) <= w {
  342. return s
  343. }
  344. r := []rune(s)
  345. tw := c.StringWidth(tail)
  346. w -= tw
  347. width := 0
  348. i := 0
  349. for ; i < len(r); i++ {
  350. cw := c.RuneWidth(r[i])
  351. if width+cw > w {
  352. break
  353. }
  354. width += cw
  355. }
  356. return string(r[0:i]) + tail
  357. }
  358. func (c *Condition) Wrap(s string, w int) string {
  359. width := 0
  360. out := ""
  361. for _, r := range []rune(s) {
  362. cw := RuneWidth(r)
  363. if r == '\n' {
  364. out += string(r)
  365. width = 0
  366. continue
  367. } else if width+cw > w {
  368. out += "\n"
  369. width = 0
  370. out += string(r)
  371. width += cw
  372. continue
  373. }
  374. out += string(r)
  375. width += cw
  376. }
  377. return out
  378. }
  379. func (c *Condition) FillLeft(s string, w int) string {
  380. width := c.StringWidth(s)
  381. count := w - width
  382. if count > 0 {
  383. b := make([]byte, count)
  384. for i := range b {
  385. b[i] = ' '
  386. }
  387. return string(b) + s
  388. }
  389. return s
  390. }
  391. func (c *Condition) FillRight(s string, w int) string {
  392. width := c.StringWidth(s)
  393. count := w - width
  394. if count > 0 {
  395. b := make([]byte, count)
  396. for i := range b {
  397. b[i] = ' '
  398. }
  399. return s + string(b)
  400. }
  401. return s
  402. }
  403. // RuneWidth returns the number of cells in r.
  404. // See http://www.unicode.org/reports/tr11/
  405. func RuneWidth(r rune) int {
  406. return DefaultCondition.RuneWidth(r)
  407. }
  408. func ct(r rune) ctype {
  409. for _, iv := range ctypes {
  410. if iv.first <= r && r <= iv.last {
  411. return iv.ctype
  412. }
  413. }
  414. return neutral
  415. }
  416. // IsAmbiguousWidth returns whether is ambiguous width or not.
  417. func IsAmbiguousWidth(r rune) bool {
  418. return ct(r) == ambiguous
  419. }
  420. // IsAmbiguousWidth returns whether is ambiguous width or not.
  421. func IsNeutralWidth(r rune) bool {
  422. return ct(r) == neutral
  423. }
  424. func StringWidth(s string) (width int) {
  425. return DefaultCondition.StringWidth(s)
  426. }
  427. func Truncate(s string, w int, tail string) string {
  428. return DefaultCondition.Truncate(s, w, tail)
  429. }
  430. func Wrap(s string, w int) string {
  431. return DefaultCondition.Wrap(s, w)
  432. }
  433. func FillLeft(s string, w int) string {
  434. return DefaultCondition.FillLeft(s, w)
  435. }
  436. func FillRight(s string, w int) string {
  437. return DefaultCondition.FillRight(s, w)
  438. }