fonts_spec.js 2.8 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 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, numChars = 0;
  31. for (var i = 0; i < numRanges; i += 2) {
  32. var limits = {
  33. lower: ProblematicCharRanges[i],
  34. upper: ProblematicCharRanges[i + 1]
  35. };
  36. if (!isInt(limits.lower) || !isInt(limits.upper)) {
  37. throw new Error('Range endpoints must be integers: ' + printRange(limits));
  38. }
  39. if (limits.lower < 0 || limits.upper < 0) {
  40. throw new Error('Range endpoints must be non-negative: ' + printRange(limits));
  41. }
  42. var range = limits.upper - limits.lower;
  43. if (range < 1) {
  44. throw new Error('Range must contain at least one element: ' + printRange(limits));
  45. }
  46. if (prevLimits) {
  47. if (limits.lower < prevLimits.lower) {
  48. throw new Error('Ranges must be sorted in ascending order: ' + printRange(limits) + ', ' + printRange(prevLimits));
  49. }
  50. if (limits.lower < prevLimits.upper) {
  51. throw new Error('Ranges must not overlap: ' + printRange(limits) + ', ' + printRange(prevLimits));
  52. }
  53. }
  54. prevLimits = {
  55. lower: limits.lower,
  56. upper: limits.upper
  57. };
  58. numChars += range;
  59. }
  60. var puaLength = PRIVATE_USE_OFFSET_END + 1 - PRIVATE_USE_OFFSET_START;
  61. if (numChars > puaLength) {
  62. throw new Error('Total number of chars must not exceed the PUA length.');
  63. }
  64. return {
  65. numChars: numChars,
  66. puaLength: puaLength,
  67. percentage: 100 * (numChars / puaLength)
  68. };
  69. };
  70. describe('Fonts', function () {
  71. it('checkProblematicCharRanges', function () {
  72. var EXPECTED_PERCENTAGE = 45;
  73. var result = checkProblematicCharRanges();
  74. expect(result.percentage).toBeLessThan(EXPECTED_PERCENTAGE);
  75. });
  76. });