fonts_spec.js 2.9 KB

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