001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.propertyrender;
031:
032: import nextapp.echo2.app.Component;
033: import nextapp.echo2.app.Font;
034: import nextapp.echo2.webrender.output.CssStyle;
035:
036: /**
037: * Utility class for rendering <code>nextapp.echo2.app.Font</code>
038: * properties to CSS.
039: */
040: public class FontRender {
041:
042: /**
043: * Renders a 'font-family' CSS attribute value based on the specified
044: * <code>Font.Typeface</code>.
045: *
046: * @param typeface the typeface
047: * @return the CSS attribute value
048: */
049: public static String renderFontFamilyCssAttributeValue(
050: Font.Typeface typeface) {
051: StringBuffer out = new StringBuffer(typeface.getName());
052: typeface = typeface.getAlternate();
053: while (typeface != null) {
054: out.append(",");
055: out.append(typeface.getName());
056: typeface = typeface.getAlternate();
057: }
058: return out.toString();
059: }
060:
061: /**
062: * Renders a 'font-style' CSS attribute value for the specified
063: * <code>Font</code>.
064: *
065: * @param font the font
066: * @return the CSS attribute value
067: */
068: public static String renderFontStyleCssAttributeValue(Font font) {
069: return font.isItalic() ? "italic" : "normal";
070: }
071:
072: /**
073: * Renders a 'font-weight' CSS attribute value for the specified
074: * <code>Font</code>.
075: *
076: * @param font the font
077: * @return the CSS attribute value
078: */
079: public static String renderFontWeightCssAttributeValue(Font font) {
080: return font.isBold() ? "bold" : "normal";
081: }
082:
083: /**
084: * Renders a 'text-decoration' CSS attribute value based on the specified
085: * <code>Font</code>
086: *
087: * @param font the font
088: * @return the CSS attribute value
089: */
090: public static String renderTextDecorationCssAttributeValue(Font font) {
091: StringBuffer out = new StringBuffer();
092: if (font.isUnderline()) {
093: out.append("underline");
094: }
095: if (font.isOverline()) {
096: if (out.length() > 0) {
097: out.append(" ");
098: }
099: out.append("overline");
100: }
101: if (font.isLineThrough()) {
102: if (out.length() > 0) {
103: out.append(" ");
104: }
105: out.append("line-through");
106: }
107: if (out.length() == 0) {
108: return "none";
109: } else {
110: return out.toString();
111: }
112: }
113:
114: /**
115: * Renders the <code>Font</code> properties of the provided
116: * <code>Component</code> to a CSS style.
117: *
118: * @param cssStyle the target <code>CssStyle</code>
119: * @param component the component
120: */
121: public static void renderToStyle(CssStyle cssStyle,
122: Component component) {
123: renderToStyle(cssStyle, (Font) component
124: .getRenderProperty(Component.PROPERTY_FONT));
125: }
126:
127: /**
128: * Renders a <code>Font</code> property to the given CSS style.
129: * Null property values are ignored.
130: *
131: * @param cssStyle the target <code>CssStyle</code>
132: * @param font the property value
133: */
134: public static void renderToStyle(CssStyle cssStyle, Font font) {
135: if (font == null) {
136: return;
137: }
138: Font.Typeface typeface = font.getTypeface();
139: if (typeface != null) {
140: cssStyle.setAttribute("font-family",
141: renderFontFamilyCssAttributeValue(typeface));
142: }
143: if (font.getSize() != null) {
144: cssStyle.setAttribute("font-size", ExtentRender
145: .renderCssAttributeValue(font.getSize()));
146: }
147: if (!font.isPlain()) {
148: if (font.isBold()) {
149: cssStyle.setAttribute("font-weight", "bold");
150: }
151: if (font.isItalic()) {
152: cssStyle.setAttribute("font-style", "italic");
153: }
154: if (font.isUnderline() || font.isOverline()
155: || font.isLineThrough()) {
156: StringBuffer out = new StringBuffer();
157: if (font.isUnderline()) {
158: out.append("underline");
159: }
160: if (font.isOverline()) {
161: if (out.length() > 0) {
162: out.append(" ");
163: }
164: out.append("overline");
165: }
166: if (font.isLineThrough()) {
167: if (out.length() > 0) {
168: out.append(" ");
169: }
170: out.append("line-through");
171: }
172: cssStyle
173: .setAttribute("text-decoration", out.toString());
174: }
175: }
176: }
177:
178: /** Non-instantiable class. */
179: private FontRender() {
180: }
181: }
|