001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.util;
009:
010: import net.mygwt.ui.client.MyDOM;
011:
012: import com.google.gwt.user.client.DOM;
013: import com.google.gwt.user.client.Element;
014:
015: /**
016: * Provides precise pixel measurements for blocks of text so that you can
017: * determine exactly how high and wide, in pixels, a given block of text will
018: * be.
019: */
020: public class TextMetrics {
021:
022: private static TextMetrics instance;
023:
024: static {
025: instance = new TextMetrics();
026: }
027:
028: /**
029: * Returns the singleton instance.
030: *
031: * @return the text metrics instance
032: */
033: public static TextMetrics get() {
034: return instance;
035: }
036:
037: private Element elem;
038:
039: private TextMetrics() {
040: elem = DOM.createDiv();
041: DOM.appendChild(MyDOM.getBody(), elem);
042: DOM.setStyleAttribute(elem, "position", "absolute");
043: MyDOM.setLeftTop(elem, -10000, -1000);
044: MyDOM.setVisibility(elem, false);
045: }
046:
047: /**
048: * Binds this TextMetrics instance to an element from which to copy existing
049: * CSS styles that can affect the size of the rendered text.
050: *
051: * @param el the element
052: */
053: public void bind(Element el) {
054: DOM.setStyleAttribute(elem, "fontSize", DOM.getStyleAttribute(
055: el, "fontSize"));
056: DOM.setStyleAttribute(elem, "fontStyle", DOM.getStyleAttribute(
057: el, "fontStyle"));
058: DOM.setStyleAttribute(elem, "fontWeight", DOM
059: .getStyleAttribute(el, "fontWeight"));
060: }
061:
062: /**
063: * Returns the measured height of the specified text. For multiline text, be
064: * sure to call {@link #setFixedWidth} if necessary.
065: *
066: * @param text the text to be measured
067: * @return the height in pixels
068: */
069: public int getHeight(String text) {
070: return getSize(text).height;
071: }
072:
073: /**
074: * Returns the size of the specified text based on the internal element's
075: * style and width properties.
076: *
077: * @param text the text to measure
078: * @return the size
079: */
080: public Size getSize(String text) {
081: MyDOM.setInnerHTML(elem, text);
082: Size size = MyDOM.getSize(elem);
083: MyDOM.setInnerHTML(elem, "");
084: return size;
085: }
086:
087: /**
088: * Returns the measured width of the specified text.
089: *
090: * @param text the text to measure
091: * @return the width in pixels
092: */
093: public int getWidth(String text) {
094: DOM.setStyleAttribute(elem, "width", "auto");
095: return getSize(text).width;
096: }
097:
098: /**
099: * Sets a fixed width on the internal measurement element. If the text will be
100: * multiline, you have to set a fixed width in order to accurately measure the
101: * text height.
102: *
103: * @param width the width to set on the element
104: */
105: public void setFixedWidth(int width) {
106: DOM.setIntStyleAttribute(elem, "width", width);
107: }
108:
109: }
|