fonts_spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 _fonts = require('../../core/fonts');
  24. var checkProblematicCharRanges = function checkProblematicCharRanges() {
  25. function printRange(limits) {
  26. return '[' + limits.lower.toString('16').toUpperCase() + ', ' + limits.upper.toString('16').toUpperCase() + ')';
  27. }
  28. var numRanges = _fonts.ProblematicCharRanges.length;
  29. if (numRanges % 2 !== 0) {
  30. throw new Error('Char ranges must contain an even number of elements.');
  31. }
  32. var prevLimits,
  33. numChars = 0;
  34. for (var i = 0; i < numRanges; i += 2) {
  35. var limits = {
  36. lower: _fonts.ProblematicCharRanges[i],
  37. upper: _fonts.ProblematicCharRanges[i + 1]
  38. };
  39. if (!Number.isInteger(limits.lower) || !Number.isInteger(limits.upper)) {
  40. throw new Error('Range endpoints must be integers: ' + printRange(limits));
  41. }
  42. if (limits.lower < 0 || limits.upper < 0) {
  43. throw new Error('Range endpoints must be non-negative: ' + printRange(limits));
  44. }
  45. var range = limits.upper - limits.lower;
  46. if (range < 1) {
  47. throw new Error('Range must contain at least one element: ' + printRange(limits));
  48. }
  49. if (prevLimits) {
  50. if (limits.lower < prevLimits.lower) {
  51. throw new Error('Ranges must be sorted in ascending order: ' + printRange(limits) + ', ' + printRange(prevLimits));
  52. }
  53. if (limits.lower < prevLimits.upper) {
  54. throw new Error('Ranges must not overlap: ' + printRange(limits) + ', ' + printRange(prevLimits));
  55. }
  56. }
  57. prevLimits = {
  58. lower: limits.lower,
  59. upper: limits.upper
  60. };
  61. numChars += range;
  62. }
  63. var puaLength = _fonts.PRIVATE_USE_OFFSET_END + 1 - _fonts.PRIVATE_USE_OFFSET_START;
  64. if (numChars > puaLength) {
  65. throw new Error('Total number of chars must not exceed the PUA length.');
  66. }
  67. return {
  68. numChars: numChars,
  69. puaLength: puaLength,
  70. percentage: 100 * (numChars / puaLength)
  71. };
  72. };
  73. describe('Fonts', function () {
  74. it('checkProblematicCharRanges', function () {
  75. var EXPECTED_PERCENTAGE = 100;
  76. var result = checkProblematicCharRanges();
  77. expect(result.percentage).toBeLessThan(EXPECTED_PERCENTAGE);
  78. });
  79. });