Sha256: ea11d36b5a1a32cad306bf3d4f27e3b5efec7175851af32cd1c5c8f2c04d0820

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

/**
 * @constructor
 */
webfont.FontApiUrlBuilder = function(apiUrl, protocol, text) {
  if (apiUrl) {
    this.apiUrl_ = apiUrl;
  } else {
    this.apiUrl_ = protocol + webfont.FontApiUrlBuilder.DEFAULT_API_URL;
  }
  this.fontFamilies_ = [];
  this.subsets_ = [];
  this.text_ = text || '';
};


webfont.FontApiUrlBuilder.DEFAULT_API_URL = '//fonts.googleapis.com/css';


webfont.FontApiUrlBuilder.prototype.setFontFamilies = function(fontFamilies) {
  this.parseFontFamilies_(fontFamilies);
};


webfont.FontApiUrlBuilder.prototype.parseFontFamilies_ =
    function(fontFamilies) {
  var length = fontFamilies.length;

  for (var i = 0; i < length; i++) {
    var elements = fontFamilies[i].split(':');

    if (elements.length == 3) {
      this.subsets_.push(elements.pop());
    }
    var joinCharacter = '';
    if (elements.length == 2 && elements[1] != ''){
      joinCharacter = ':';
    }
    this.fontFamilies_.push(elements.join(joinCharacter));
  }
};


webfont.FontApiUrlBuilder.prototype.webSafe = function(string) {
  return string.replace(/ /g, '+');
};


webfont.FontApiUrlBuilder.prototype.build = function() {
  if (this.fontFamilies_.length == 0) {
    throw new Error('No fonts to load !');
  }
  if (this.apiUrl_.indexOf("kit=") != -1) {
    return this.apiUrl_;
  }
  var length = this.fontFamilies_.length;
  var sb = [];

  for (var i = 0; i < length; i++) {
    sb.push(this.webSafe(this.fontFamilies_[i]));
  }
  var url = this.apiUrl_ + '?family=' + sb.join('%7C'); // '|' escaped.

  if (this.subsets_.length > 0) {
    url += '&subset=' + this.subsets_.join(',');
  }

  if (this.text_.length > 0) {
    url += '&text=' + encodeURIComponent(this.text_);
  }

  return url;
};

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
webfontloader-1.3.0 src/google/fontapiurlbuilder.js
webfontloader-1.2.1 src/google/fontapiurlbuilder.js
webfontloader-1.2.0 src/google/fontapiurlbuilder.js
webfontloader-1.1.2 src/google/fontapiurlbuilder.js