00001 /* 00002 * CPStringDrawing.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 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 @import <Foundation/CPString.j> 00024 00025 #include "CoreGraphics/CGGeometry.h" 00026 #include "Platform/Platform.h" 00027 00028 00029 var CPStringReferenceElement = nil, 00030 CPStringDefaultFont = nil; 00031 00032 @implementation CPString (CPStringDrawing) 00033 00037 - (CPString)cssString 00038 { 00039 return self; 00040 } 00041 00042 - (CGSize)sizeWithFont:(CPFont)aFont 00043 { 00044 return [self sizeWithFont:aFont inWidth:NULL]; 00045 } 00046 00047 - (CGSize)sizeWithFont:(CPFont)aFont inWidth:(float)aWidth 00048 { 00049 #if PLATFORM(DOM) 00050 if (!CPStringReferenceElement) 00051 { 00052 CPStringReferenceElement = document.createElement("span"); 00053 00054 var style = CPStringReferenceElement.style; 00055 00056 style.position = "absolute"; 00057 style.whiteSpace = "pre"; 00058 style.visibility = "visible"; 00059 style.padding = "0px"; 00060 style.margin = "0px"; 00061 00062 style.left = "-100000px"; 00063 style.top = "-100000px"; 00064 style.zIndex = "10000"; 00065 style.background = "red"; 00066 00067 document.getElementsByTagName("body")[0].appendChild(CPStringReferenceElement); 00068 } 00069 00070 if (!aFont) 00071 { 00072 if (!CPStringDefaultFont) 00073 CPStringDefaultFont = [CPFont systemFontOfSize:12.0]; 00074 00075 aFont = CPStringDefaultFont; 00076 } 00077 00078 var style = CPStringReferenceElement.style; 00079 00080 if (aWidth === NULL) 00081 { 00082 style.width = ""; 00083 style.whiteSpace = "pre"; 00084 } 00085 00086 else 00087 { 00088 style.width = ROUND(aWidth) + "px"; 00089 00090 if (document.attachEvent) 00091 style.wordWrap = "break-word"; 00092 00093 else 00094 { 00095 style.whiteSpace = "-o-pre-wrap"; 00096 style.whiteSpace = "-pre-wrap"; 00097 style.whiteSpace = "-moz-pre-wrap"; 00098 style.whiteSpace = "pre-wrap"; 00099 } 00100 } 00101 00102 style.font = [aFont cssString]; 00103 00104 if (CPFeatureIsCompatible(CPJavascriptInnerTextFeature)) 00105 CPStringReferenceElement.innerText = self; 00106 00107 else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature)) 00108 CPStringReferenceElement.textContent = self; 00109 00110 return _CGSizeMake(CPStringReferenceElement.clientWidth, CPStringReferenceElement.clientHeight); 00111 #endif 00112 return _CGSizeMakeZero(); 00113 } 00114 00115 + (void)_resetSize 00116 { 00117 CPStringReferenceElement = nil; 00118 } 00119 00120 @end