unicode_spec.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. var _unicode = require("../../core/unicode.js");
  24. var _glyphlist = require("../../core/glyphlist.js");
  25. describe("unicode", function () {
  26. describe("mapSpecialUnicodeValues", function () {
  27. it("should not re-map normal Unicode values", function () {
  28. expect((0, _unicode.mapSpecialUnicodeValues)(0x0041)).toEqual(0x0041);
  29. expect((0, _unicode.mapSpecialUnicodeValues)(0xfb01)).toEqual(0xfb01);
  30. });
  31. it("should re-map special Unicode values", function () {
  32. expect((0, _unicode.mapSpecialUnicodeValues)(0xf8e9)).toEqual(0x00a9);
  33. expect((0, _unicode.mapSpecialUnicodeValues)(0xffff)).toEqual(0);
  34. });
  35. });
  36. describe("getCharUnicodeCategory", function () {
  37. it("should correctly determine the character category", function () {
  38. const tests = {
  39. " ": {
  40. isZeroWidthDiacritic: false,
  41. isInvisibleFormatMark: false,
  42. isWhitespace: true
  43. },
  44. "\t": {
  45. isZeroWidthDiacritic: false,
  46. isInvisibleFormatMark: false,
  47. isWhitespace: true
  48. },
  49. "\u2001": {
  50. isZeroWidthDiacritic: false,
  51. isInvisibleFormatMark: false,
  52. isWhitespace: true
  53. },
  54. "\uFEFF": {
  55. isZeroWidthDiacritic: false,
  56. isInvisibleFormatMark: false,
  57. isWhitespace: true
  58. },
  59. "\u0302": {
  60. isZeroWidthDiacritic: true,
  61. isInvisibleFormatMark: false,
  62. isWhitespace: false
  63. },
  64. "\u0344": {
  65. isZeroWidthDiacritic: true,
  66. isInvisibleFormatMark: false,
  67. isWhitespace: false
  68. },
  69. "\u0361": {
  70. isZeroWidthDiacritic: true,
  71. isInvisibleFormatMark: false,
  72. isWhitespace: false
  73. },
  74. "\u200B": {
  75. isZeroWidthDiacritic: false,
  76. isInvisibleFormatMark: true,
  77. isWhitespace: false
  78. },
  79. "\u200D": {
  80. isZeroWidthDiacritic: false,
  81. isInvisibleFormatMark: true,
  82. isWhitespace: false
  83. },
  84. a: {
  85. isZeroWidthDiacritic: false,
  86. isInvisibleFormatMark: false,
  87. isWhitespace: false
  88. },
  89. 1: {
  90. isZeroWidthDiacritic: false,
  91. isInvisibleFormatMark: false,
  92. isWhitespace: false
  93. }
  94. };
  95. for (const [character, expectation] of Object.entries(tests)) {
  96. expect((0, _unicode.getCharUnicodeCategory)(character)).toEqual(expectation);
  97. }
  98. });
  99. });
  100. describe("getUnicodeForGlyph", function () {
  101. let standardMap, dingbatsMap;
  102. beforeAll(function () {
  103. standardMap = (0, _glyphlist.getGlyphsUnicode)();
  104. dingbatsMap = (0, _glyphlist.getDingbatsGlyphsUnicode)();
  105. });
  106. afterAll(function () {
  107. standardMap = dingbatsMap = null;
  108. });
  109. it("should get Unicode values for valid glyph names", function () {
  110. expect((0, _unicode.getUnicodeForGlyph)("A", standardMap)).toEqual(0x0041);
  111. expect((0, _unicode.getUnicodeForGlyph)("a1", dingbatsMap)).toEqual(0x2701);
  112. });
  113. it("should recover Unicode values from uniXXXX/uXXXX{XX} glyph names", function () {
  114. expect((0, _unicode.getUnicodeForGlyph)("uni0041", standardMap)).toEqual(0x0041);
  115. expect((0, _unicode.getUnicodeForGlyph)("u0041", standardMap)).toEqual(0x0041);
  116. expect((0, _unicode.getUnicodeForGlyph)("uni2701", dingbatsMap)).toEqual(0x2701);
  117. expect((0, _unicode.getUnicodeForGlyph)("u2701", dingbatsMap)).toEqual(0x2701);
  118. });
  119. it("should not get Unicode values for invalid glyph names", function () {
  120. expect((0, _unicode.getUnicodeForGlyph)("Qwerty", standardMap)).toEqual(-1);
  121. expect((0, _unicode.getUnicodeForGlyph)("Qwerty", dingbatsMap)).toEqual(-1);
  122. });
  123. });
  124. describe("getUnicodeRangeFor", function () {
  125. it("should get correct Unicode range", function () {
  126. expect((0, _unicode.getUnicodeRangeFor)(0x0041)).toEqual(0);
  127. expect((0, _unicode.getUnicodeRangeFor)(0xfb01)).toEqual(62);
  128. });
  129. it("should not get a Unicode range", function () {
  130. expect((0, _unicode.getUnicodeRangeFor)(0x05ff)).toEqual(-1);
  131. });
  132. });
  133. describe("getNormalizedUnicodes", function () {
  134. let NormalizedUnicodes;
  135. beforeAll(function () {
  136. NormalizedUnicodes = (0, _unicode.getNormalizedUnicodes)();
  137. });
  138. afterAll(function () {
  139. NormalizedUnicodes = null;
  140. });
  141. it("should get normalized Unicode values for ligatures", function () {
  142. expect(NormalizedUnicodes["\uFB01"]).toEqual("fi");
  143. expect(NormalizedUnicodes["\u0675"]).toEqual("\u0627\u0674");
  144. });
  145. it("should not normalize standard characters", function () {
  146. expect(NormalizedUnicodes.A).toEqual(undefined);
  147. });
  148. });
  149. describe("reverseIfRtl", function () {
  150. let NormalizedUnicodes;
  151. function getGlyphUnicode(char) {
  152. if (NormalizedUnicodes[char] !== undefined) {
  153. return NormalizedUnicodes[char];
  154. }
  155. return char;
  156. }
  157. beforeAll(function () {
  158. NormalizedUnicodes = (0, _unicode.getNormalizedUnicodes)();
  159. });
  160. afterAll(function () {
  161. NormalizedUnicodes = null;
  162. });
  163. it("should not reverse LTR characters", function () {
  164. const A = getGlyphUnicode("A");
  165. expect((0, _unicode.reverseIfRtl)(A)).toEqual("A");
  166. const fi = getGlyphUnicode("\uFB01");
  167. expect((0, _unicode.reverseIfRtl)(fi)).toEqual("fi");
  168. });
  169. it("should reverse RTL characters", function () {
  170. const heAlef = getGlyphUnicode("\u05D0");
  171. expect((0, _unicode.reverseIfRtl)(heAlef)).toEqual("\u05D0");
  172. const arAlef = getGlyphUnicode("\u0675");
  173. expect((0, _unicode.reverseIfRtl)(arAlef)).toEqual("\u0674\u0627");
  174. });
  175. });
  176. });