fonts_spec.js 2.7 KB

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