UNPKG

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