unicode_spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this 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. */
  15. 'use strict';
  16. var coreGlyphList = require('../../core/glyphlist.js');
  17. var coreUnicode = require('../../core/unicode.js');
  18. var getGlyphsUnicode = coreGlyphList.getGlyphsUnicode;
  19. var getDingbatsGlyphsUnicode = coreGlyphList.getDingbatsGlyphsUnicode;
  20. var mapSpecialUnicodeValues = coreUnicode.mapSpecialUnicodeValues;
  21. var getUnicodeForGlyph = coreUnicode.getUnicodeForGlyph;
  22. var getUnicodeRangeFor = coreUnicode.getUnicodeRangeFor;
  23. var getNormalizedUnicodes = coreUnicode.getNormalizedUnicodes;
  24. var reverseIfRtl = coreUnicode.reverseIfRtl;
  25. describe('unicode', function () {
  26. describe('mapSpecialUnicodeValues', function () {
  27. it('should not re-map normal Unicode values', function () {
  28. expect(mapSpecialUnicodeValues(0x0041)).toEqual(0x0041);
  29. expect(mapSpecialUnicodeValues(0xFB01)).toEqual(0xFB01);
  30. });
  31. it('should re-map special Unicode values', function () {
  32. expect(mapSpecialUnicodeValues(0xF8E9)).toEqual(0x00A9);
  33. expect(mapSpecialUnicodeValues(0xFFFF)).toEqual(0);
  34. });
  35. });
  36. describe('getUnicodeForGlyph', function () {
  37. var standardMap, dingbatsMap;
  38. beforeAll(function (done) {
  39. standardMap = getGlyphsUnicode();
  40. dingbatsMap = getDingbatsGlyphsUnicode();
  41. done();
  42. });
  43. afterAll(function () {
  44. standardMap = dingbatsMap = null;
  45. });
  46. it('should get Unicode values for valid glyph names', function () {
  47. expect(getUnicodeForGlyph('A', standardMap)).toEqual(0x0041);
  48. expect(getUnicodeForGlyph('a1', dingbatsMap)).toEqual(0x2701);
  49. });
  50. it('should recover Unicode values from uniXXXX/uXXXX{XX} glyph names', function () {
  51. expect(getUnicodeForGlyph('uni0041', standardMap)).toEqual(0x0041);
  52. expect(getUnicodeForGlyph('u0041', standardMap)).toEqual(0x0041);
  53. expect(getUnicodeForGlyph('uni2701', dingbatsMap)).toEqual(0x2701);
  54. expect(getUnicodeForGlyph('u2701', dingbatsMap)).toEqual(0x2701);
  55. });
  56. it('should not get Unicode values for invalid glyph names', function () {
  57. expect(getUnicodeForGlyph('Qwerty', standardMap)).toEqual(-1);
  58. expect(getUnicodeForGlyph('Qwerty', dingbatsMap)).toEqual(-1);
  59. });
  60. });
  61. describe('getUnicodeRangeFor', function () {
  62. it('should get correct Unicode range', function () {
  63. expect(getUnicodeRangeFor(0x0041)).toEqual(0);
  64. expect(getUnicodeRangeFor(0xFB01)).toEqual(62);
  65. });
  66. it('should not get a Unicode range', function () {
  67. expect(getUnicodeRangeFor(0x05FF)).toEqual(-1);
  68. });
  69. });
  70. describe('getNormalizedUnicodes', function () {
  71. var NormalizedUnicodes;
  72. beforeAll(function (done) {
  73. NormalizedUnicodes = getNormalizedUnicodes();
  74. done();
  75. });
  76. afterAll(function () {
  77. NormalizedUnicodes = null;
  78. });
  79. it('should get normalized Unicode values for ligatures', function () {
  80. expect(NormalizedUnicodes['\uFB01']).toEqual('fi');
  81. expect(NormalizedUnicodes['\u0675']).toEqual('\u0627\u0674');
  82. });
  83. it('should not normalize standard characters', function () {
  84. expect(NormalizedUnicodes['A']).toEqual(undefined);
  85. });
  86. });
  87. describe('reverseIfRtl', function () {
  88. var NormalizedUnicodes;
  89. function getGlyphUnicode(char) {
  90. if (NormalizedUnicodes[char] !== undefined) {
  91. return NormalizedUnicodes[char];
  92. }
  93. return char;
  94. }
  95. beforeAll(function (done) {
  96. NormalizedUnicodes = getNormalizedUnicodes();
  97. done();
  98. });
  99. afterAll(function () {
  100. NormalizedUnicodes = null;
  101. });
  102. it('should not reverse LTR characters', function () {
  103. var A = getGlyphUnicode('A');
  104. expect(reverseIfRtl(A)).toEqual('A');
  105. var fi = getGlyphUnicode('\uFB01');
  106. expect(reverseIfRtl(fi)).toEqual('fi');
  107. });
  108. it('should reverse RTL characters', function () {
  109. var heAlef = getGlyphUnicode('\u05D0');
  110. expect(reverseIfRtl(heAlef)).toEqual('\u05D0');
  111. var arAlef = getGlyphUnicode('\u0675');
  112. expect(reverseIfRtl(arAlef)).toEqual('\u0674\u0627');
  113. });
  114. });
  115. });