API 0.9.5
AppKit/CPFontManager.j
Go to the documentation of this file.
00001 /*
00002  * CPFontManager.j
00003  * AppKit
00004  *
00005  * Created by Tom Robinson.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 
00026 var CPSharedFontManager     = nil,
00027     CPFontManagerFactory    = Nil;
00028 
00032 @implementation CPFontManager : CPObject
00033 {
00034     CPArray _availableFonts;
00035 }
00036 
00037 // Getting the Shared Font Manager
00042 + (CPFontManager)sharedFontManager
00043 {
00044     if (!CPSharedFontManager)
00045         CPSharedFontManager = [[CPFontManagerFactory alloc] init];
00046 
00047     return CPSharedFontManager;
00048 }
00049 
00050 // Changing the Default Font Conversion Classes
00055 + (void)setFontManagerFactory:(Class)aClass
00056 {
00057     CPFontManagerFactory = aClass;
00058 }
00059 
00063 - (CPArray)availableFonts
00064 {
00065     if (!_availableFonts)
00066     {
00067         _CPFontDetectSpan = document.createElement("span");
00068         _CPFontDetectSpan.fontSize = "24px";
00069         _CPFontDetectSpan.appendChild(document.createTextNode("mmmmmmmmmml"));
00070         var div = document.createElement("div");
00071         div.style.position = "absolute";
00072         div.style.top = "-1000px";
00073         div.appendChild(_CPFontDetectSpan);
00074         document.getElementsByTagName("body")[0].appendChild(div);
00075 
00076         _CPFontDetectReferenceFonts = _CPFontDetectPickTwoDifferentFonts(["monospace", "serif", "sans-serif", "cursive"]);
00077 
00078         _availableFonts = [];
00079         for (var i = 0; i < _CPFontDetectAllFonts.length; i++)
00080         {
00081             var available = _CPFontDetectFontAvailable(_CPFontDetectAllFonts[i]);
00082             if (available)
00083                 _availableFonts.push(_CPFontDetectAllFonts[i]);
00084         }
00085     }
00086     return _availableFonts;
00087 }
00088 
00093 - (CPArray)fontWithNameIsAvailable:(CPString)aFontName
00094 {
00095     return _CPFontDetectFontAvailable(aFontName);
00096 }
00097 
00098 @end
00099 
00100 var _CPFontDetectSpan,
00101     _CPFontDetectReferenceFonts,
00102     _CPFontDetectAllFonts = /* "04b_21","A Charming Font","Abadi MT Condensed","Abadi MT Condensed Extra Bold","Abadi MT Condensed Light","Academy Engraved LET","Agency FB","Alba","Alba Matter","Alba Super","Algerian",*//* "Andale Mono","Andale Mono IPA","Andy", *//* "Avant Garde","Avantgarde","Baby Kruffy","Base 02","Baskerville","Baskerville Old Face","Bauhaus 93","Beesknees ITC","Bell MT","Berlin Sans FB","Berlin Sans FB Demi","Bernard MT Condensed","Bickley Script",*//* "Blackadder ITC","Blackletter686 BT","Bodoni MT","Bodoni MT Black","Bodoni MT Condensed","Bodoni MT Poster Compressed","Book Antiqua","Bookman","Bookman Old Style","Bradley Hand ITC","Braggadocio","Britannic Bold","Broadway","Broadway BT",*//* "BudHand","CAMPBELL","Calibri","Californian FB","Calisto MT","Calligraph421 BT",*//* "Candara","Capitals",*//* "Champignon","Charcoal","Charter","Charter BT","Chicago","Chick","Chiller","ClearlyU","Colonna MT",*//* "Croobie","Curlz MT","Desdemona","Didot","DomBold BT","Edwardian Script ITC","Engravers MT","Eras Bold ITC","Eras Demi ITC","Eras Light ITC","Eras Medium ITC","Eurostile","FIRSTHOME","Fat","Felix Titling","Fine Hand","Fixed","Footlight MT Light","Forte","Franklin Gothic Book","Franklin Gothic Demi","Franklin Gothic Demi Cond","Franklin Gothic Heavy","Franklin Gothic Medium","Franklin Gothic Medium Cond","Freestyle Script","French Script MT","Freshbot","Frosty",*//* "GENUINE","Gadget","Garamond",*//* "GlooGun","Gloucester MT Extra Condensed","Goudy Old Style","Goudy Stout","Haettenschweiler","Harlow Solid Italic","Harrington",*//* "Informal Roman","Jenkins v2.0","John Handy LET","Jokerman","Jokerman LET","Jokewood","Juice ITC","Kabel Ult BT","Kartika","Kino MT","Kristen ITC","Kunstler Script","La Bamba LET", *//* "Luxi Mono","Luxi Sans","Luxi Serif","MARKETPRO","MS Reference Sans Serif","MS Reference Serif","Magneto","Maiandra GD", *//* "Matisse ITC","Matura MT Script Capitals","Mead Bold","Mekanik LET","Mercurius Script MT Bold", *//* "Niagara Engraved","Niagara Solid","Nimbus Mono L","Nimbus Roman No9 L","OCR A Extended","OCRB","Odessa LET","Old English Text MT","OldDreadfulNo7 BT","One Stroke Script LET","Onyx","Optima","Orange LET","Palace Script MT","Palatino","Palatino Linotype", *//* "ParkAvenue BT","Pepita MT","Perpetua","Perpetua Titling MT","Placard Condensed","Playbill","Poornut","Pristina","Pump Demi Bold LET","Pussycat","Quixley LET","Rage Italic","Rage Italic LET","Ravie","Rockwell","Rockwell Condensed","Rockwell Extra Bold","Ruach LET","Runic MT Condensed","Sand","Script MT Bold","Scruff LET","Segoe UI","Showcard Gothic","Skia","Smudger LET","Snap ITC","Square721 BT","Staccato222 BT","Stencil","Sylfaen", *//* "Tw Cen MT","Tw Cen MT Condensed","Tw Cen MT Condensed Extra Bold","URW Antiqua T","URW Bookman L","URW Chancery L","URW Gothic L","URW Palladio L","Univers","University Roman LET","Utopia", *//* "Victorian LET","Viner Hand ITC","Vivaldi","Vladimir Script","Vrinda","Weltron Urban","Westwood LET","Wide Latin","Zapf Chancery", */[
00103         
00104         "American Typewriter",
00105         
00106         "Apple Chancery","Arial","Arial Black","Arial Narrow","Arial Rounded MT Bold","Arial Unicode MS",
00107         
00108         "Big Caslon","Bitstream Vera Sans","Bitstream Vera Sans Mono","Bitstream Vera Serif",
00109         
00110         "Brush Script MT",
00111         
00112         "Cambria",
00113         
00114         "Caslon","Castellar","Cataneo BT","Centaur","Century Gothic","Century Schoolbook","Century Schoolbook L",
00115         
00116         "Comic Sans", "Comic Sans MS","Consolas","Constantia","Cooper Black","Copperplate","Copperplate Gothic Bold","Copperplate Gothic Light","Corbel","Courier","Courier New",
00117         
00118         "Futura",
00119         
00120         "Geneva","Georgia","Georgia Ref", "Geeza Pro", "Gigi","Gill Sans","Gill Sans MT","Gill Sans MT Condensed","Gill Sans MT Ext Condensed Bold","Gill Sans Ultra Bold","Gill Sans Ultra Bold Condensed",
00121         
00122         "Helvetica","Helvetica Narrow","Helvetica Neue","Herculanum","High Tower Text","Highlight LET","Hoefler Text","Impact","Imprint MT Shadow",
00123         
00124         "Lucida","Lucida Bright","Lucida Calligraphy","Lucida Console","Lucida Fax","Lucida Grande","Lucida Handwriting","Lucida Sans","Lucida Sans Typewriter","Lucida Sans Unicode",
00125         
00126         "Marker Felt",
00127         
00128         "Microsoft Sans Serif","Milano LET","Minion Web","MisterEarl BT","Mistral","Monaco","Monotype Corsiva","Monotype.com","New Century Schoolbook","New York","News Gothic MT",
00129         
00130         "Papyrus",
00131         
00132         "Tahoma","Techno","Tempus Sans ITC","Terminal","Textile","Times","Times New Roman","Tiranti Solid LET","Trebuchet MS",
00133         
00134         "Verdana","Verdana Ref", 
00135         "Zapfino"];
00136 
00137 // Compare against the reference fonts. Return true if it produces a different size than at least one of them.
00138 var _CPFontDetectFontAvailable = function(font)
00139 {
00140     for (var i = 0; i < _CPFontDetectReferenceFonts.length; i++)
00141         if (_CPFontDetectCompareFonts(_CPFontDetectReferenceFonts[i], font))
00142             return true;
00143     return false;
00144 }
00145 
00146 var _CPFontDetectCache = {};
00147 
00148 // Compares two given fonts. Returns true if they produce different sizes (i.e. fontA didn't fallback to fontB)
00149 var _CPFontDetectCompareFonts = function(fontA, fontB)
00150 {
00151     var a;
00152     if (_CPFontDetectCache[fontA])
00153         a = _CPFontDetectCache[fontA];
00154 
00155     else
00156     {
00157         _CPFontDetectSpan.style.fontFamily = '"' + fontA + '"';
00158         _CPFontDetectCache[fontA] = a = { w: _CPFontDetectSpan.offsetWidth, h: _CPFontDetectSpan.offsetHeight };
00159     }
00160 
00161     _CPFontDetectSpan.style.fontFamily = '"' + fontB + '", "' + fontA + '"';
00162     var bWidth = _CPFontDetectSpan.offsetWidth,
00163         bHeight = _CPFontDetectSpan.offsetHeight;
00164 
00165     return (a.w != bWidth || a.h != bHeight);
00166 }
00167 
00168 // Test the candidate fonts pairwise until we find two that are different. Otherwise return the first.
00169 var _CPFontDetectPickTwoDifferentFonts = function(candidates)
00170 {
00171     for (var i = 0; i < candidates.length; i++)
00172         for (var j = 0; j < i; j++)
00173             if (_CPFontDetectCompareFonts(candidates[i], candidates[j]))
00174                 return [candidates[i], candidates[j]];
00175     return [candidates[0]];
00176 }
00177 
00178 [CPFontManager setFontManagerFactory:[CPFontManager class]];
 All Classes Files Functions Variables Defines