1 29 30 package nextapp.echo2.webcontainer.propertyrender; 31 32 import nextapp.echo2.app.Component; 33 import nextapp.echo2.app.Font; 34 import nextapp.echo2.webrender.output.CssStyle; 35 36 40 public class FontRender { 41 42 49 public static String renderFontFamilyCssAttributeValue(Font.Typeface typeface) { 50 StringBuffer out = new StringBuffer (typeface.getName()); 51 typeface = typeface.getAlternate(); 52 while (typeface != null) { 53 out.append(","); 54 out.append(typeface.getName()); 55 typeface = typeface.getAlternate(); 56 } 57 return out.toString(); 58 } 59 60 67 public static String renderFontStyleCssAttributeValue(Font font) { 68 return font.isItalic() ? "italic" : "normal"; 69 } 70 71 78 public static String renderFontWeightCssAttributeValue(Font font) { 79 return font.isBold() ? "bold" : "normal"; 80 } 81 82 89 public static String renderTextDecorationCssAttributeValue(Font font) { 90 StringBuffer out = new StringBuffer (); 91 if (font.isUnderline()) { 92 out.append("underline"); 93 } 94 if (font.isOverline()) { 95 if (out.length() > 0) { 96 out.append(" "); 97 } 98 out.append("overline"); 99 } 100 if (font.isLineThrough()) { 101 if (out.length() > 0) { 102 out.append(" "); 103 } 104 out.append("line-through"); 105 } 106 if (out.length() == 0) { 107 return "none"; 108 } else { 109 return out.toString(); 110 } 111 } 112 113 120 public static void renderToStyle(CssStyle cssStyle, Component component) { 121 renderToStyle(cssStyle, (Font) component.getRenderProperty(Component.PROPERTY_FONT)); 122 } 123 124 131 public static void renderToStyle(CssStyle cssStyle, Font font) { 132 if (font == null) { 133 return; 134 } 135 Font.Typeface typeface = font.getTypeface(); 136 if (typeface != null) { 137 cssStyle.setAttribute("font-family", renderFontFamilyCssAttributeValue(typeface)); 138 } 139 if (font.getSize() != null) { 140 cssStyle.setAttribute("font-size", ExtentRender.renderCssAttributeValue(font.getSize())); 141 } 142 if (!font.isPlain()) { 143 if (font.isBold()) { 144 cssStyle.setAttribute("font-weight", "bold"); 145 } 146 if (font.isItalic()) { 147 cssStyle.setAttribute("font-style", "italic"); 148 } 149 if (font.isUnderline() || font.isOverline() || font.isLineThrough()) { 150 StringBuffer out = new StringBuffer (); 151 if (font.isUnderline()) { 152 out.append("underline"); 153 } 154 if (font.isOverline()) { 155 if (out.length() > 0) { 156 out.append(" "); 157 } 158 out.append("overline"); 159 } 160 if (font.isLineThrough()) { 161 if (out.length() > 0) { 162 out.append(" "); 163 } 164 out.append("line-through"); 165 } 166 cssStyle.setAttribute("text-decoration", out.toString()); 167 } 168 } 169 } 170 171 172 private FontRender() { } 173 } 174 | Popular Tags |