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 00026 var CPStringReferenceElement = nil; 00027 00028 @implementation CPString (CPStringDrawing) 00029 00033 - (CPString)cssString 00034 { 00035 return self; 00036 } 00037 00038 - (CGSize)sizeWithFont:(CPFont)aFont 00039 { 00040 if (!CPStringReferenceElement) 00041 { 00042 CPStringReferenceElement = document.createElement("span"); 00043 00044 var style = CPStringReferenceElement.style; 00045 00046 style.position = "absolute"; 00047 style.whiteSpace = "pre"; 00048 style.visibility = "visible"; 00049 style.padding = "0px"; 00050 style.margin = "0px"; 00051 00052 style.left = "-100000px"; 00053 style.top = "-100000px"; 00054 style.zIndex = "10000"; 00055 style.background = "red"; 00056 00057 document.getElementsByTagName("body")[0].appendChild(CPStringReferenceElement); 00058 } 00059 00060 CPStringReferenceElement.style.font = [aFont ? aFont : [CPFont systemFontOfSize:12.0] cssString]; 00061 00062 if (CPFeatureIsCompatible(CPJavascriptInnerTextFeature)) 00063 CPStringReferenceElement.innerText = self; 00064 else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature)) 00065 CPStringReferenceElement.textContent = self; 00066 00067 return CGSizeMake(CPStringReferenceElement.clientWidth, CPStringReferenceElement.clientHeight); 00068 } 00069 00070 @end