fontAnalyzerTest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var should = require('chai').should();
  2. var fontAnalyzer = require('../../lib/tools/redownload/fontAnalyzer');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('fontAnalyzer', function() {
  6. it('should extract metrics from a font', function(done) {
  7. this.timeout(10000);
  8. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/SourceSansPro/SourceSansPro-Regular.woff'));
  9. var fileSize = fileContent.length;
  10. var entry = {
  11. method: 'GET',
  12. url: 'http://localhost:8388/SourceSansPro/SourceSansPro-Regular.woff',
  13. requestHeaders: {
  14. 'User-Agent': 'something',
  15. Referer: 'http://www.google.fr/',
  16. Accept: '*/*',
  17. 'Accept-Encoding': 'gzip, deflate'
  18. },
  19. status: 200,
  20. isWebFont: true,
  21. type: 'webfont',
  22. contentType: 'image/jpeg',
  23. contentLength: 999,
  24. weightCheck: {
  25. bodyBuffer: fileContent,
  26. totalWeight: fileSize + 200,
  27. headersSize: 200,
  28. bodySize: fileSize,
  29. isCompressed: false,
  30. uncompressedSize: fileSize
  31. }
  32. };
  33. fontAnalyzer.getMetricsFromFont(entry, 'ABCD')
  34. .then(function(metrics) {
  35. metrics.should.be.an('Object');
  36. metrics.should.have.a.property('name').that.equals('Source Sans Pro');
  37. metrics.should.have.a.property('numGlyphs').that.equals(1944);
  38. metrics.should.have.a.property('averageGlyphComplexity').that.equals(26.6);
  39. metrics.should.have.a.property('compressedWeight').that.equals(fileSize);
  40. metrics.should.have.a.property('unicodeRanges').that.is.an('Object');
  41. metrics.unicodeRanges.should.have.a.property('Basic Latin');
  42. metrics.unicodeRanges['Basic Latin'].should.have.a.property('charset').that.is.a('String');
  43. metrics.unicodeRanges['Basic Latin'].charset.length.should.equal(95);
  44. metrics.unicodeRanges['Basic Latin'].name.should.equal('Basic Latin');
  45. metrics.unicodeRanges['Basic Latin'].rangeStart.should.equal(0x0020);
  46. metrics.unicodeRanges['Basic Latin'].rangeEnd.should.equal(0x007F);
  47. metrics.unicodeRanges['Basic Latin'].coverage.should.equal(95 / 96);
  48. metrics.unicodeRanges['Basic Latin'].numGlyphsInCommonWithPageContent.should.equal(4);
  49. metrics.unicodeRanges.Cyrillic.numGlyphsInCommonWithPageContent.should.equal(0);
  50. metrics.should.have.a.property('numGlyphsInCommonWithPageContent').that.equals(4);
  51. should.equal(metrics.unicodeRanges.Others.coverage, undefined);
  52. done();
  53. })
  54. .fail(function(err) {
  55. done(err);
  56. });
  57. });
  58. it('should sort glyphes by unicode ranges', function() {
  59. var ranges = fontAnalyzer.readUnicodeRanges([0x0041, 0x0042, 0x0043, 0x0044, 0x0416], '0123AMZ');
  60. ranges.should.deep.equal({
  61. 'Basic Latin': {
  62. name: 'Basic Latin',
  63. rangeStart: 32,
  64. rangeEnd: 127,
  65. charset: 'ABCD',
  66. coverage: 0.041666666666666664,
  67. numGlyphsInCommonWithPageContent: 1
  68. },
  69. 'Cyrillic': {
  70. name: 'Cyrillic',
  71. rangeStart: 1024,
  72. rangeEnd: 1327,
  73. charset: 'Ж',
  74. coverage: 0.003289473684210526,
  75. numGlyphsInCommonWithPageContent: 0
  76. }
  77. });
  78. });
  79. it('should transform an array of char codes into a string', function() {
  80. var str = fontAnalyzer.getCharacterSetAsString([0x0041, 0x0042, 0x0043, 0x0044, 0x0416]);
  81. str.should.equal('ABCDЖ');
  82. });
  83. it('should find the right unicode range for a char', function() {
  84. fontAnalyzer.getUnicodeRangeFromChar(0x0020).should.deep.equal({
  85. name: 'Basic Latin',
  86. rangeStart: 0x0020,
  87. rangeEnd: 0x007F
  88. });
  89. fontAnalyzer.getUnicodeRangeFromChar(0x0021).name.should.equal('Basic Latin');
  90. fontAnalyzer.getUnicodeRangeFromChar(0x007F).name.should.equal('Basic Latin');
  91. fontAnalyzer.getUnicodeRangeFromChar(0x007E).name.should.equal('Basic Latin');
  92. fontAnalyzer.getUnicodeRangeFromChar(0x0000).name.should.equal('Others');
  93. fontAnalyzer.getUnicodeRangeFromChar(0xFFFFFFFFF).name.should.equal('Others');
  94. });
  95. });