00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024
00025 @import <AppKit/CPFont.j>
00026
00027
00028 var CPSharedFontManager = nil,
00029 CPFontManagerFactory = Nil;
00030
00034 @implementation CPFontManager : CPObject
00035 {
00036 CPArray _availableFonts;
00037 }
00038
00039
00044 + (CPFontManager)sharedFontManager
00045 {
00046 if (!CPSharedFontManager)
00047 CPSharedFontManager = [[CPFontManagerFactory alloc] init];
00048
00049 return CPSharedFontManager;
00050 }
00051
00052
00057 + (void)setFontManagerFactory:(Class)aClass
00058 {
00059 CPFontManagerFactory = aClass;
00060 }
00061
00065 - (CPArray)availableFonts
00066 {
00067 if (!_availableFonts)
00068 {
00069 _CPFontDetectSpan = document.createElement("span");
00070 _CPFontDetectSpan.fontSize = "24px";
00071 _CPFontDetectSpan.appendChild(document.createTextNode("mmmmmmmmmml"));
00072 var div = document.createElement("div");
00073 div.style.position = "absolute";
00074 div.style.top = "-1000px";
00075 div.appendChild(_CPFontDetectSpan);
00076 document.getElementsByTagName("body")[0].appendChild(div);
00077
00078 _CPFontDetectReferenceFonts = _CPFontDetectPickTwoDifferentFonts(["monospace", "serif", "sans-serif", "cursive"]);
00079
00080 _availableFonts = [];
00081 for (var i = 0; i < _CPFontDetectAllFonts.length; i++) {
00082 var available = _CPFontDetectFontAvailable(_CPFontDetectAllFonts[i]);
00083 if (available)
00084 _availableFonts.push(_CPFontDetectAllFonts[i]);
00085 }
00086 }
00087 return _availableFonts;
00088 }
00089
00094 - (CPArray)fontWithNameIsAvailable:(CPString)aFontName
00095 {
00096 return _CPFontDetectFontAvailable(aFontName);
00097 }
00098
00099 @end
00100
00101 var _CPFontDetectSpan,
00102 _CPFontDetectReferenceFonts,
00103 _CPFontDetectAllFonts = [
00104
00105 "American Typewriter",
00106
00107 "Apple Chancery","Arial","Arial Black","Arial Narrow","Arial Rounded MT Bold","Arial Unicode MS",
00108
00109 "Big Caslon","Bitstream Vera Sans","Bitstream Vera Sans Mono","Bitstream Vera Serif",
00110
00111 "Brush Script MT",
00112
00113 "Cambria",
00114
00115 "Caslon","Castellar","Cataneo BT","Centaur","Century Gothic","Century Schoolbook","Century Schoolbook L",
00116
00117 "Comic Sans", "Comic Sans MS","Consolas","Constantia","Cooper Black","Copperplate","Copperplate Gothic Bold","Copperplate Gothic Light","Corbel","Courier","Courier New",
00118
00119 "Futura",
00120
00121 "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",
00122
00123 "Helvetica","Helvetica Narrow","Helvetica Neue","Herculanum","High Tower Text","Highlight LET","Hoefler Text","Impact","Imprint MT Shadow",
00124
00125 "Lucida","Lucida Bright","Lucida Calligraphy","Lucida Console","Lucida Fax","Lucida Grande","Lucida Handwriting","Lucida Sans","Lucida Sans Typewriter","Lucida Sans Unicode",
00126
00127 "Marker Felt",
00128
00129 "Microsoft Sans Serif","Milano LET","Minion Web","MisterEarl BT","Mistral","Monaco","Monotype Corsiva","Monotype.com","New Century Schoolbook","New York","News Gothic MT",
00130
00131 "Papyrus",
00132
00133 "Tahoma","Techno","Tempus Sans ITC","Terminal","Textile","Times","Times New Roman","Tiranti Solid LET","Trebuchet MS",
00134
00135 "Verdana","Verdana Ref",
00136 "Zapfino"];
00137
00138
00139 var _CPFontDetectFontAvailable = function(font) {
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
00149 var _CPFontDetectCompareFonts = function(fontA, fontB) {
00150 var a;
00151 if (_CPFontDetectCache[fontA]) {
00152 a = _CPFontDetectCache[fontA];
00153 } else {
00154 _CPFontDetectSpan.style.fontFamily = '"' + fontA + '"';
00155 _CPFontDetectCache[fontA] = a = { w: _CPFontDetectSpan.offsetWidth, h: _CPFontDetectSpan.offsetHeight };
00156 }
00157
00158 _CPFontDetectSpan.style.fontFamily= '"' + fontB + '", "' + fontA + '"';
00159 var bWidth = _CPFontDetectSpan.offsetWidth;
00160 var bHeight = _CPFontDetectSpan.offsetHeight;
00161
00162 return (a.w != bWidth || a.h != bHeight);
00163 }
00164
00165
00166 var _CPFontDetectPickTwoDifferentFonts = function(candidates) {
00167 for (var i = 0; i < candidates.length; i++)
00168 for (var j = 0; j < i; j++)
00169 if (_CPFontDetectCompareFonts(candidates[i], candidates[j]))
00170 return [candidates[i], candidates[j]];
00171 return [candidates[0]];
00172 }
00173
00174 [CPFontManager setFontManagerFactory:[CPFontManager class]];