fontAnalyzerTest.js 4.4 KB

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