fonts_spec.js 2.7 KB

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