Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #233 from mdumic/ligatures

Support for Basic Latin Ligatures (\uFB00 to \uFB06). Partially resolves #38
  • Loading branch information...
commit 94db085164a74a1cc8c2d22906d23c416e9f2dbd 2 parents 79ea413 + 91a7c02
@sorccu authored
Showing with 51 additions and 4 deletions.
  1. +51 −4 js/cufon.js
View
55 js/cufon.js
@@ -344,7 +344,7 @@ var Cufon = (function() {
function Font(data) {
- var face = this.face = data.face, wordSeparators = {
+ var face = this.face = data.face, ligatureCache = [], wordSeparators = {
'\u0020': 1,
'\u00a0': 1,
'\u3000': 1
@@ -416,6 +416,43 @@ var Cufon = (function() {
return jumps;
};
+ this.applyLigatures = function(text, ligatures) {
+ // find cached ligature configuration for this font
+ for (var i=0, ligatureConfig; i<ligatureCache.length && !ligatureConfig; i++)
+ if (ligatureCache[i].ligatures === ligatures)
+ ligatureConfig = ligatureCache[i];
+
+ // if there is none, it needs to be created and cached
+ if (!ligatureConfig) {
+ // identify letter groups to prepare regular expression that matches these
+ var letterGroups = [];
+ for (var letterGroup in ligatures) {
+ if (this.glyphs[ligatures[letterGroup]]) {
+ letterGroups.push(letterGroup);
+ }
+ }
+
+ // sort by longer groups first, then alphabetically (to aid caching by this key)
+ var regexpText = letterGroups.sort(function(a, b) {
+ return b.length - a.length || a > b;
+ }).join('|');
+
+ ligatureCache.push(ligatureConfig = {
+ ligatures: ligatures,
+ // create regular expression for matching desired ligatures that are present in the font
+ regexp: regexpText.length > 0
+ ? regexpCache[regexpText] || (regexpCache[regexpText] = new RegExp(regexpText, 'g'))
+ : null
+ });
+ }
+
+ // return applied ligatures or original text if none exist for given configuration
+ return ligatureConfig.regexp
+ ? text.replace(ligatureConfig.regexp, function(match) {
+ return ligatures[match] || match;
+ })
+ : text;
+ };
}
function FontFamily() {
@@ -813,6 +850,7 @@ var Cufon = (function() {
var C_SHY_DISABLED = 'cufon-shy-disabled';
var C_VIEWPORT_RESIZING = 'cufon-viewport-resizing';
+ var regexpCache = {};
var sharedStorage = new Storage();
var hoverHandler = new HoverHandler();
var replaceHistory = new ReplaceHistory();
@@ -878,7 +916,16 @@ var Cufon = (function() {
ul: 1
},
textShadow: 'none',
- trim: 'advanced'
+ trim: 'advanced',
+ ligatures: {
+ 'ff': '\ufb00',
+ 'fi': '\ufb01',
+ 'fl': '\ufb02',
+ 'ffi': '\ufb03',
+ 'ffl': '\ufb04',
+ '\u017ft': '\ufb05',
+ 'st': '\ufb06'
+ }
};
var separators = {
@@ -1098,7 +1145,7 @@ Cufon.registerEngine('vml', (function() {
wStyle.height = size.convert(font.height) + 'px';
var color = style.get('color');
- var chars = Cufon.CSS.textTransform(text, style).split('');
+ var chars = Cufon.CSS.textTransform(options.ligatures ? font.applyLigatures(text, options.ligatures) : text, style).split('');
var jumps = font.spacing(chars,
getSpacingValue(el, style, size, 'letterSpacing'),
@@ -1317,7 +1364,7 @@ Cufon.registerEngine('canvas', (function() {
}
}
- var chars = Cufon.CSS.textTransform(text, style).split('');
+ var chars = Cufon.CSS.textTransform(options.ligatures ? font.applyLigatures(text, options.ligatures) : text, style).split('');
var jumps = font.spacing(chars,
~~size.convertFrom(parseFloat(style.get('letterSpacing')) || 0),
Please sign in to comment.
Something went wrong with that request. Please try again.