webfontloader 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
 - data/lib/webfontloader/demo/public/fontwatchrunner-default-fonts.html +86 -0
 - data/lib/webfontloader/demo/public/index.html +1 -0
 - data/lib/webfontloader/demo/public/jquery.min.js +166 -0
 - data/lib/webfontloader/demo/server.rb +9 -0
 - data/lib/webfontloader.rb +1 -1
 - data/src/core/fontwatcher.js +30 -88
 - data/src/core/fontwatchrunner.js +136 -0
 - data/src/core/namespace.js +1 -1
 - data/src/modules.yml +1 -0
 - data/src-test/core/fontwatchertest.js +185 -496
 - data/src-test/core/fontwatchrunnertest.js +190 -0
 - data/webfontloader.gemspec +6 -2
 - metadata +19 -4
 
| 
         @@ -0,0 +1,136 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            /**
         
     | 
| 
      
 2 
     | 
    
         
            +
             * @constructor
         
     | 
| 
      
 3 
     | 
    
         
            +
             * @param {function(string, string)} activeCallback
         
     | 
| 
      
 4 
     | 
    
         
            +
             * @param {function(string, string)} inactiveCallback
         
     | 
| 
      
 5 
     | 
    
         
            +
             * @param {webfont.DomHelper} domHelper
         
     | 
| 
      
 6 
     | 
    
         
            +
             * @param {Object.<string, function(Object): number>} fontSizer
         
     | 
| 
      
 7 
     | 
    
         
            +
             * @param {function(function(), number=)} asyncCall
         
     | 
| 
      
 8 
     | 
    
         
            +
             * @param {function(): number} getTime
         
     | 
| 
      
 9 
     | 
    
         
            +
             * @param {string} fontFamily
         
     | 
| 
      
 10 
     | 
    
         
            +
             * @param {string} fontDescription
         
     | 
| 
      
 11 
     | 
    
         
            +
             * @param {string=} opt_fontTestString
         
     | 
| 
      
 12 
     | 
    
         
            +
             */
         
     | 
| 
      
 13 
     | 
    
         
            +
            webfont.FontWatchRunner = function(activeCallback, inactiveCallback, domHelper,
         
     | 
| 
      
 14 
     | 
    
         
            +
                fontSizer, asyncCall, getTime, fontFamily, fontDescription, opt_fontTestString) {
         
     | 
| 
      
 15 
     | 
    
         
            +
              this.activeCallback_ = activeCallback;
         
     | 
| 
      
 16 
     | 
    
         
            +
              this.inactiveCallback_ = inactiveCallback;
         
     | 
| 
      
 17 
     | 
    
         
            +
              this.domHelper_ = domHelper;
         
     | 
| 
      
 18 
     | 
    
         
            +
              this.fontSizer_ = fontSizer;
         
     | 
| 
      
 19 
     | 
    
         
            +
              this.asyncCall_ = asyncCall;
         
     | 
| 
      
 20 
     | 
    
         
            +
              this.getTime_ = getTime;
         
     | 
| 
      
 21 
     | 
    
         
            +
              this.nameHelper_ = new webfont.CssFontFamilyName();
         
     | 
| 
      
 22 
     | 
    
         
            +
              this.fvd_ = new webfont.FontVariationDescription();
         
     | 
| 
      
 23 
     | 
    
         
            +
              this.fontFamily_ = fontFamily;
         
     | 
| 
      
 24 
     | 
    
         
            +
              this.fontDescription_ = fontDescription;
         
     | 
| 
      
 25 
     | 
    
         
            +
              this.fontTestString_ = opt_fontTestString || webfont.FontWatchRunner.DEFAULT_TEST_STRING;
         
     | 
| 
      
 26 
     | 
    
         
            +
              this.originalSizeA_ = this.getDefaultFontSize_(
         
     | 
| 
      
 27 
     | 
    
         
            +
                  webfont.FontWatchRunner.DEFAULT_FONTS_A);
         
     | 
| 
      
 28 
     | 
    
         
            +
              this.originalSizeB_ = this.getDefaultFontSize_(
         
     | 
| 
      
 29 
     | 
    
         
            +
                  webfont.FontWatchRunner.DEFAULT_FONTS_B);
         
     | 
| 
      
 30 
     | 
    
         
            +
              this.requestedFontA_ = this.createHiddenElementWithFont_(
         
     | 
| 
      
 31 
     | 
    
         
            +
                  webfont.FontWatchRunner.DEFAULT_FONTS_A);
         
     | 
| 
      
 32 
     | 
    
         
            +
              this.requestedFontB_ = this.createHiddenElementWithFont_(
         
     | 
| 
      
 33 
     | 
    
         
            +
                  webfont.FontWatchRunner.DEFAULT_FONTS_B);
         
     | 
| 
      
 34 
     | 
    
         
            +
              this.started_ = getTime();
         
     | 
| 
      
 35 
     | 
    
         
            +
              this.check_();
         
     | 
| 
      
 36 
     | 
    
         
            +
            };
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            /**
         
     | 
| 
      
 39 
     | 
    
         
            +
             * A set of sans-serif fonts and a generic family that cover most platforms:
         
     | 
| 
      
 40 
     | 
    
         
            +
             * Windows - arial - 99.71%
         
     | 
| 
      
 41 
     | 
    
         
            +
             * Mac - arial - 97.67%
         
     | 
| 
      
 42 
     | 
    
         
            +
             * Linux - 97.67%
         
     | 
| 
      
 43 
     | 
    
         
            +
             * (Based on http://www.codestyle.org/css/font-family/sampler-CombinedResults.shtml)
         
     | 
| 
      
 44 
     | 
    
         
            +
             * @type {string}
         
     | 
| 
      
 45 
     | 
    
         
            +
             * @const
         
     | 
| 
      
 46 
     | 
    
         
            +
             */
         
     | 
| 
      
 47 
     | 
    
         
            +
            webfont.FontWatchRunner.DEFAULT_FONTS_A = "arial,'URW Gothic L',sans-serif";
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
            /**
         
     | 
| 
      
 50 
     | 
    
         
            +
             * A set of serif fonts and a generic family that cover most platforms. We
         
     | 
| 
      
 51 
     | 
    
         
            +
             * want each of these fonts to have a different width when rendering the test
         
     | 
| 
      
 52 
     | 
    
         
            +
             * string than each of the fonts in DEFAULT_FONTS_A:
         
     | 
| 
      
 53 
     | 
    
         
            +
             * Windows - Georgia - 98.98%
         
     | 
| 
      
 54 
     | 
    
         
            +
             * Mac - Georgia - 95.60%
         
     | 
| 
      
 55 
     | 
    
         
            +
             * Linux - Century Schoolbook L - 97.97%
         
     | 
| 
      
 56 
     | 
    
         
            +
             * (Based on http://www.codestyle.org/css/font-family/sampler-CombinedResults.shtml)
         
     | 
| 
      
 57 
     | 
    
         
            +
             * @type {string}
         
     | 
| 
      
 58 
     | 
    
         
            +
             * @const
         
     | 
| 
      
 59 
     | 
    
         
            +
             */
         
     | 
| 
      
 60 
     | 
    
         
            +
            webfont.FontWatchRunner.DEFAULT_FONTS_B = "Georgia,'Century Schoolbook L',serif";
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
            /**
         
     | 
| 
      
 63 
     | 
    
         
            +
             * Default test string. Characters are chosen so that their widths vary a lot
         
     | 
| 
      
 64 
     | 
    
         
            +
             * between the fonts in the default stacks. We want each fallback stack
         
     | 
| 
      
 65 
     | 
    
         
            +
             * to always start out at a different width than the other.
         
     | 
| 
      
 66 
     | 
    
         
            +
             * @type {string}
         
     | 
| 
      
 67 
     | 
    
         
            +
             * @const
         
     | 
| 
      
 68 
     | 
    
         
            +
             */
         
     | 
| 
      
 69 
     | 
    
         
            +
            webfont.FontWatchRunner.DEFAULT_TEST_STRING = 'BESs';
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
            /**
         
     | 
| 
      
 72 
     | 
    
         
            +
             * @private
         
     | 
| 
      
 73 
     | 
    
         
            +
             */
         
     | 
| 
      
 74 
     | 
    
         
            +
            webfont.FontWatchRunner.prototype.check_ = function() {
         
     | 
| 
      
 75 
     | 
    
         
            +
              var sizeA = this.fontSizer_.getWidth(this.requestedFontA_);
         
     | 
| 
      
 76 
     | 
    
         
            +
              var sizeB = this.fontSizer_.getWidth(this.requestedFontB_);
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
              if (this.originalSizeA_ != sizeA || this.originalSizeB_ != sizeB) {
         
     | 
| 
      
 79 
     | 
    
         
            +
                this.finish_(this.activeCallback_);
         
     | 
| 
      
 80 
     | 
    
         
            +
              } else if (this.getTime_() - this.started_ < 5000) {
         
     | 
| 
      
 81 
     | 
    
         
            +
                this.asyncCheck_();
         
     | 
| 
      
 82 
     | 
    
         
            +
              } else {
         
     | 
| 
      
 83 
     | 
    
         
            +
                this.finish_(this.inactiveCallback_);
         
     | 
| 
      
 84 
     | 
    
         
            +
              }
         
     | 
| 
      
 85 
     | 
    
         
            +
            };
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
            /**
         
     | 
| 
      
 88 
     | 
    
         
            +
             * @private
         
     | 
| 
      
 89 
     | 
    
         
            +
             */
         
     | 
| 
      
 90 
     | 
    
         
            +
            webfont.FontWatchRunner.prototype.asyncCheck_ = function() {
         
     | 
| 
      
 91 
     | 
    
         
            +
              this.asyncCall_(function(context, func) {
         
     | 
| 
      
 92 
     | 
    
         
            +
                return function() {
         
     | 
| 
      
 93 
     | 
    
         
            +
                  func.call(context);
         
     | 
| 
      
 94 
     | 
    
         
            +
                }
         
     | 
| 
      
 95 
     | 
    
         
            +
              }(this, this.check_), 50);
         
     | 
| 
      
 96 
     | 
    
         
            +
            };
         
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
            /**
         
     | 
| 
      
 99 
     | 
    
         
            +
             * @private
         
     | 
| 
      
 100 
     | 
    
         
            +
             * @param {function(string, string)} callback
         
     | 
| 
      
 101 
     | 
    
         
            +
             */
         
     | 
| 
      
 102 
     | 
    
         
            +
            webfont.FontWatchRunner.prototype.finish_ = function(callback) {
         
     | 
| 
      
 103 
     | 
    
         
            +
              this.domHelper_.removeElement(this.requestedFontA_);
         
     | 
| 
      
 104 
     | 
    
         
            +
              this.domHelper_.removeElement(this.requestedFontB_);
         
     | 
| 
      
 105 
     | 
    
         
            +
              callback(this.fontFamily_, this.fontDescription_);
         
     | 
| 
      
 106 
     | 
    
         
            +
            };
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
            /**
         
     | 
| 
      
 109 
     | 
    
         
            +
             * @private
         
     | 
| 
      
 110 
     | 
    
         
            +
             * @param {string} defaultFonts
         
     | 
| 
      
 111 
     | 
    
         
            +
             */
         
     | 
| 
      
 112 
     | 
    
         
            +
            webfont.FontWatchRunner.prototype.getDefaultFontSize_ = function(defaultFonts) {
         
     | 
| 
      
 113 
     | 
    
         
            +
              var defaultFont = this.createHiddenElementWithFont_(defaultFonts, true);
         
     | 
| 
      
 114 
     | 
    
         
            +
              var size = this.fontSizer_.getWidth(defaultFont);
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
              this.domHelper_.removeElement(defaultFont);
         
     | 
| 
      
 117 
     | 
    
         
            +
              return size;
         
     | 
| 
      
 118 
     | 
    
         
            +
            };
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
            /**
         
     | 
| 
      
 121 
     | 
    
         
            +
             * @private
         
     | 
| 
      
 122 
     | 
    
         
            +
             * @param {string} defaultFonts
         
     | 
| 
      
 123 
     | 
    
         
            +
             * @param {boolean=} opt_withoutFontFamily
         
     | 
| 
      
 124 
     | 
    
         
            +
             */
         
     | 
| 
      
 125 
     | 
    
         
            +
            webfont.FontWatchRunner.prototype.createHiddenElementWithFont_ = function(
         
     | 
| 
      
 126 
     | 
    
         
            +
                defaultFonts, opt_withoutFontFamily) {
         
     | 
| 
      
 127 
     | 
    
         
            +
              var variationCss = this.fvd_.expand(this.fontDescription_);
         
     | 
| 
      
 128 
     | 
    
         
            +
              var styleString = "position:absolute;top:-999px;font-size:300px;font-family:" +
         
     | 
| 
      
 129 
     | 
    
         
            +
                  (opt_withoutFontFamily ? "" : this.nameHelper_.quote(this.fontFamily_) + ",") +
         
     | 
| 
      
 130 
     | 
    
         
            +
                  defaultFonts + ";" + variationCss;
         
     | 
| 
      
 131 
     | 
    
         
            +
              var span = this.domHelper_.createElement('span', { 'style': styleString },
         
     | 
| 
      
 132 
     | 
    
         
            +
                  this.fontTestString_);
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
              this.domHelper_.insertInto('body', span);
         
     | 
| 
      
 135 
     | 
    
         
            +
              return span;
         
     | 
| 
      
 136 
     | 
    
         
            +
            };
         
     | 
    
        data/src/core/namespace.js
    CHANGED