Permalink
Browse files

Changed the way the variation is parsed to properly handle every vari…

…ation.
  • Loading branch information...
1 parent 3b94452 commit 0c85b4af8cfc506fb197bb11d59c8e34b7ca1047 @jeremiele jeremiele committed Sep 7, 2012
Showing with 84 additions and 33 deletions.
  1. +14 −2 src-test/google/fontapiparsertest.js
  2. +70 −31 src/google/fontapiparser.js
@@ -3,17 +3,19 @@ var FontApiParserTest = TestCase('FontApiParserTest');
FontApiParserTest.prototype.testParsedValuesAreCoherent = function() {
var fontFamilies = [ 'Tangerine', 'Droid Serif:bi',
'Yanone Kaffeesatz:200,300,400,700',
- 'Cantarell:italic,b' ];
+ 'Cantarell:italic,b', 'Exo:100italic', 'Lobster:200n'];
var fontApiParser = new webfont.FontApiParser(fontFamilies);
fontApiParser.parse();
var parsedFontFamilies = fontApiParser.getFontFamilies();
- assertEquals(4, parsedFontFamilies.length);
+ assertEquals(6, parsedFontFamilies.length);
assertEquals('Tangerine', parsedFontFamilies[0]);
assertEquals('Droid Serif', parsedFontFamilies[1]);
assertEquals('Yanone Kaffeesatz', parsedFontFamilies[2]);
assertEquals('Cantarell', parsedFontFamilies[3]);
+ assertEquals('Exo', parsedFontFamilies[4]);
+ assertEquals('Lobster', parsedFontFamilies[5]);
var variations = fontApiParser.getVariations();
var tangerine = variations['Tangerine'];
@@ -39,6 +41,16 @@ FontApiParserTest.prototype.testParsedValuesAreCoherent = function() {
assertEquals(2, cantarell.length);
assertEquals('i4', cantarell[0]);
assertEquals('n7', cantarell[1]);
+
+ var exo = variations['Exo'];
+ assertNotNull(exo);
+ assertEquals(1, exo.length);
+ assertEquals('i1', exo[0]);
+
+ var lobster = variations['Lobster'];
+ assertNotNull(lobster);
+ assertEquals(1, lobster.length);
+ assertEquals('n2', lobster[0]);
};
FontApiParserTest.prototype.testMixOfNumericWeightAndStyle = function() {
View
@@ -9,20 +9,6 @@ webfont.FontApiParser = function(fontFamilies) {
this.fvd_ = new webfont.FontVariationDescription();
};
-webfont.FontApiParser.VARIATIONS = {
- 'ultralight': 'n2',
- 'light': 'n3',
- 'regular': 'n4',
- 'bold': 'n7',
- 'italic': 'i4',
- 'bolditalic': 'i7',
- 'ul': 'n2',
- 'l': 'n3',
- 'r': 'n4',
- 'b': 'n7',
- 'i': 'i4',
- 'bi': 'i7'
-};
webfont.FontApiParser.INT_FONTS = {
'latin': webfont.FontWatchRunner.DEFAULT_TEST_STRING,
@@ -32,6 +18,44 @@ webfont.FontApiParser.INT_FONTS = {
'Hanuman': 'កខគ' // For backward compatibility
};
+webfont.FontApiParser.WEIGHTS = {
+ 'thin': '1',
+ 'extralight': '2',
+ 'extra-light': '2',
+ 'ultralight': '2',
+ 'ultra-light': '2',
+ 'light': '3',
+ 'regular': '4',
+ 'book': '4',
+ 'medium': '5',
+ 'semi-bold': '6',
+ 'semibold': '6',
+ 'demi-bold': '6',
+ 'demibold': '6',
+ 'bold': '7',
+ 'extra-bold': '8',
+ 'extrabold': '8',
+ 'ultra-bold': '8',
+ 'ultrabold': '8',
+ 'black': '9',
+ 'heavy': '9',
+ 'l': '3',
+ 'r': '4',
+ 'b': '7'
+};
+
+webfont.FontApiParser.STYLES = {
+ 'i': 'i',
+ 'italic': 'i',
+ 'n': 'n',
+ 'normal': 'n'
+};
+
+webfont.FontApiParser.VARIATION_MATCH =
+ new RegExp("^(thin|(?:(?:extra|ultra)-?)?light|regular|book|medium|" +
+ "(?:(?:semi|demi|extra|ultra)-?)?bold|black|heavy|l|r|b|[1-9]00)?(n|i" +
+ "|normal|italic)?$");
+
webfont.FontApiParser.prototype.parse = function() {
var length = this.fontFamilies_.length;
@@ -71,29 +95,44 @@ webfont.FontApiParser.prototype.parse = function() {
};
webfont.FontApiParser.prototype.generateFontVariationDescription_ = function(variation) {
- if (!variation.match(/^[\w ]+$/)) {
+ if (!variation.match(/^[\w]+$/)) {
+ return '';
+ }
+ var normalizedVariation = variation.toLowerCase();
+ var groups = webfont.FontApiParser.VARIATION_MATCH.exec(normalizedVariation);
+ if (groups == null) {
return '';
}
+ var styleMatch = this.normalizeStyle_(groups[2]);
+ var weightMatch = this.normalizeWeight_(groups[1]);
+ var css = this.fvd_.expand([styleMatch, weightMatch].join(''));
+ return css ? this.fvd_.compact(css) : null;
+};
+
+
+webfont.FontApiParser.prototype.normalizeStyle_ = function(parsedStyle) {
+ if (parsedStyle == null) {
+ return 'n';
+ }
+ return webfont.FontApiParser.STYLES[parsedStyle];
+};
- var fvd = webfont.FontApiParser.VARIATIONS[variation];
-
- if (fvd) {
- return fvd;
- } else {
- var groups = variation.match(/^(\d*)(\w*)$/);
- var numericMatch = groups[1];
- var styleMatch = groups[2];
- var s = styleMatch ? styleMatch : 'n';
- var w = numericMatch ? numericMatch.substr(0, 1) : '4';
- var css = this.fvd_.expand([s, w].join(''));
- if (css) {
- return this.fvd_.compact(css);
- } else {
- return null;
- }
+
+webfont.FontApiParser.prototype.normalizeWeight_ = function(parsedWeight) {
+ if (parsedWeight == null) {
+ return '4';
+ }
+ var weight = webfont.FontApiParser.WEIGHTS[parsedWeight];
+ if (weight) {
+ return weight;
}
+ if (isNaN(parsedWeight)) {
+ return '4';
+ }
+ return parsedWeight.substr(0, 1);
};
+
webfont.FontApiParser.prototype.parseVariations_ = function(variations) {
var finalVariations = [];

0 comments on commit 0c85b4a

Please sign in to comment.