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
00031 @implementation CPFontManager : CPObject
00032 {
00033 CPArray _availableFonts;
00034 }
00035
00036
00041 + (CPFontManager)sharedFontManager
00042 {
00043 if (!CPSharedFontManager)
00044 CPSharedFontManager = [[CPFontManagerFactory alloc] init];
00045
00046 return CPSharedFontManager;
00047 }
00048
00049
00054 + (void)setFontManagerFactory:(Class)aClass
00055 {
00056 CPFontManagerFactory = aClass;
00057 }
00058
00062 - (CPArray)availableFonts
00063 {
00064 if (!_availableFonts)
00065 {
00066 _CPFontDetectSpan = document.createElement("span");
00067 _CPFontDetectSpan.fontSize = "24px";
00068 _CPFontDetectSpan.appendChild(document.createTextNode("mmmmmmmmmml"));
00069 var div = document.createElement("div");
00070 div.style.position = "absolute";
00071 div.style.top = "-1000px";
00072 div.appendChild(_CPFontDetectSpan);
00073 document.getElementsByTagName("body")[0].appendChild(div);
00074
00075 _CPFontDetectReferenceFonts = _CPFontDetectPickTwoDifferentFonts(["monospace", "serif", "sans-serif", "cursive"]);
00076
00077 _availableFonts = [];
00078 for (var i = 0; i < _CPFontDetectAllFonts.length; i++) {
00079 var available = _CPFontDetectFontAvailable(_CPFontDetectAllFonts[i]);
00080 if (available)
00081 _availableFonts.push(_CPFontDetectAllFonts[i]);
00082 }
00083 }
00084 return _availableFonts;
00085 }
00086
00091 - (CPArray)fontWithNameIsAvailable:(CPString)aFontName
00092 {
00093 return _CPFontDetectFontAvailable(aFontName);
00094 }
00095
00096 @end
00097
00098 var _CPFontDetectSpan,
00099 _CPFontDetectReferenceFonts,
00100 _CPFontDetectAllFonts = [
00101
00102 "American Typewriter",
00103
00104 "Apple Chancery","Arial","Arial Black","Arial Narrow","Arial Rounded MT Bold","Arial Unicode MS",
00105
00106 "Big Caslon","Bitstream Vera Sans","Bitstream Vera Sans Mono","Bitstream Vera Serif",
00107
00108 "Brush Script MT",
00109
00110 "Cambria",
00111
00112 "Caslon","Castellar","Cataneo BT","Centaur","Century Gothic","Century Schoolbook","Century Schoolbook L",
00113
00114 "Comic Sans", "Comic Sans MS","Consolas","Constantia","Cooper Black","Copperplate","Copperplate Gothic Bold","Copperplate Gothic Light","Corbel","Courier","Courier New",
00115
00116 "Futura",
00117
00118 "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",
00119
00120 "Helvetica","Helvetica Narrow","Helvetica Neue","Herculanum","High Tower Text","Highlight LET","Hoefler Text","Impact","Imprint MT Shadow",
00121
00122 "Lucida","Lucida Bright","Lucida Calligraphy","Lucida Console","Lucida Fax","Lucida Grande","Lucida Handwriting","Lucida Sans","Lucida Sans Typewriter","Lucida Sans Unicode",
00123
00124 "Marker Felt",
00125
00126 "Microsoft Sans Serif","Milano LET","Minion Web","MisterEarl BT","Mistral","Monaco","Monotype Corsiva","Monotype.com","New Century Schoolbook","New York","News Gothic MT",
00127
00128 "Papyrus",
00129
00130 "Tahoma","Techno","Tempus Sans ITC","Terminal","Textile","Times","Times New Roman","Tiranti Solid LET","Trebuchet MS",
00131
00132 "Verdana","Verdana Ref",
00133 "Zapfino"];
00134
00135
00136 var _CPFontDetectFontAvailable = function(font) {
00137 for (var i = 0; i < _CPFontDetectReferenceFonts.length; i++)
00138 if (_CPFontDetectCompareFonts(_CPFontDetectReferenceFonts[i], font))
00139 return true;
00140 return false;
00141 }
00142
00143 var _CPFontDetectCache = {};
00144
00145
00146 var _CPFontDetectCompareFonts = function(fontA, fontB) {
00147 var a;
00148 if (_CPFontDetectCache[fontA]) {
00149 a = _CPFontDetectCache[fontA];
00150 } else {
00151 _CPFontDetectSpan.style.fontFamily = '"' + fontA + '"';
00152 _CPFontDetectCache[fontA] = a = { w: _CPFontDetectSpan.offsetWidth, h: _CPFontDetectSpan.offsetHeight };
00153 }
00154
00155 _CPFontDetectSpan.style.fontFamily= '"' + fontB + '", "' + fontA + '"';
00156 var bWidth = _CPFontDetectSpan.offsetWidth;
00157 var bHeight = _CPFontDetectSpan.offsetHeight;
00158
00159 return (a.w != bWidth || a.h != bHeight);
00160 }
00161
00162
00163 var _CPFontDetectPickTwoDifferentFonts = function(candidates) {
00164 for (var i = 0; i < candidates.length; i++)
00165 for (var j = 0; j < i; j++)
00166 if (_CPFontDetectCompareFonts(candidates[i], candidates[j]))
00167 return [candidates[i], candidates[j]];
00168 return [candidates[0]];
00169 }
00170
00171 [CPFontManager setFontManagerFactory:[CPFontManager class]];