001: /*
002: * Copyright 2004 Paulo Soares
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047:
048: package com.lowagie.text.html.simpleparser;
049:
050: import java.awt.Color;
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.Properties;
054: import java.util.StringTokenizer;
055:
056: import com.lowagie.text.Chunk;
057: import com.lowagie.text.Element;
058: import com.lowagie.text.Font;
059: import com.lowagie.text.FontFactory;
060: import com.lowagie.text.FontFactoryImp;
061: import com.lowagie.text.ListItem;
062: import com.lowagie.text.Paragraph;
063: import com.lowagie.text.html.Markup;
064: import com.lowagie.text.pdf.BaseFont;
065:
066: /**
067: *
068: * @author psoares
069: */
070: public class FactoryProperties {
071:
072: private FontFactoryImp fontImp = FontFactory.getFontImp();
073:
074: /** Creates a new instance of FactoryProperties */
075: public FactoryProperties() {
076: }
077:
078: public Chunk createChunk(String text, ChainedProperties props) {
079: Font font = getFont(props);
080: float size = font.getSize();
081: size /= 2;
082: Chunk ck = new Chunk(text, font);
083: if (props.hasProperty("sub"))
084: ck.setTextRise(-size);
085: else if (props.hasProperty("sup"))
086: ck.setTextRise(size);
087: return ck;
088: }
089:
090: private static void setParagraphLeading(Paragraph p, String leading) {
091: if (leading == null) {
092: p.setLeading(0, 1.5f);
093: return;
094: }
095: try {
096: StringTokenizer tk = new StringTokenizer(leading, " ,");
097: String v = tk.nextToken();
098: float v1 = Float.parseFloat(v);
099: if (!tk.hasMoreTokens()) {
100: p.setLeading(v1, 0);
101: return;
102: }
103: v = tk.nextToken();
104: float v2 = Float.parseFloat(v);
105: p.setLeading(v1, v2);
106: } catch (Exception e) {
107: p.setLeading(0, 1.5f);
108: }
109: }
110:
111: public static Paragraph createParagraph(HashMap props) {
112: Paragraph p = new Paragraph();
113: String value = (String) props.get("align");
114: if (value != null) {
115: if (value.equalsIgnoreCase("center"))
116: p.setAlignment(Element.ALIGN_CENTER);
117: else if (value.equalsIgnoreCase("right"))
118: p.setAlignment(Element.ALIGN_RIGHT);
119: else if (value.equalsIgnoreCase("justify"))
120: p.setAlignment(Element.ALIGN_JUSTIFIED);
121: }
122: setParagraphLeading(p, (String) props.get("leading"));
123: return p;
124: }
125:
126: public static void createParagraph(Paragraph p,
127: ChainedProperties props) {
128: String value = props.getProperty("align");
129: if (value != null) {
130: if (value.equalsIgnoreCase("center"))
131: p.setAlignment(Element.ALIGN_CENTER);
132: else if (value.equalsIgnoreCase("right"))
133: p.setAlignment(Element.ALIGN_RIGHT);
134: else if (value.equalsIgnoreCase("justify"))
135: p.setAlignment(Element.ALIGN_JUSTIFIED);
136: }
137: setParagraphLeading(p, props.getProperty("leading"));
138: value = props.getProperty("before");
139: if (value != null) {
140: try {
141: p.setSpacingBefore(Float.parseFloat(value));
142: } catch (Exception e) {
143: }
144: }
145: value = props.getProperty("after");
146: if (value != null) {
147: try {
148: p.setSpacingAfter(Float.parseFloat(value));
149: } catch (Exception e) {
150: }
151: }
152: value = props.getProperty("extraparaspace");
153: if (value != null) {
154: try {
155: p.setExtraParagraphSpace(Float.parseFloat(value));
156: } catch (Exception e) {
157: }
158: }
159: }
160:
161: public static Paragraph createParagraph(ChainedProperties props) {
162: Paragraph p = new Paragraph();
163: createParagraph(p, props);
164: return p;
165: }
166:
167: public static ListItem createListItem(ChainedProperties props) {
168: ListItem p = new ListItem();
169: createParagraph(p, props);
170: return p;
171: }
172:
173: public Font getFont(ChainedProperties props) {
174: String face = props.getProperty("face");
175: if (face != null) {
176: StringTokenizer tok = new StringTokenizer(face, ",");
177: while (tok.hasMoreTokens()) {
178: face = tok.nextToken().trim();
179: if (face.startsWith("\""))
180: face = face.substring(1);
181: if (face.endsWith("\""))
182: face = face.substring(0, face.length() - 1);
183: if (fontImp.isRegistered(face))
184: break;
185: }
186: }
187: int style = 0;
188: if (props.hasProperty("i"))
189: style |= Font.ITALIC;
190: if (props.hasProperty("b"))
191: style |= Font.BOLD;
192: if (props.hasProperty("u"))
193: style |= Font.UNDERLINE;
194: if (props.hasProperty("s"))
195: style |= Font.STRIKETHRU;
196: String value = props.getProperty("size");
197: float size = 12;
198: if (value != null)
199: size = Float.parseFloat(value);
200: Color color = Markup.decodeColor(props.getProperty("color"));
201: String encoding = props.getProperty("encoding");
202: if (encoding == null)
203: encoding = BaseFont.WINANSI;
204: return fontImp
205: .getFont(face, encoding, true, size, style, color);
206: }
207:
208: public static void insertStyle(HashMap h) {
209: String style = (String) h.get("style");
210: if (style == null)
211: return;
212: Properties prop = Markup.parseAttributes(style);
213: for (Iterator it = prop.keySet().iterator(); it.hasNext();) {
214: String key = (String) it.next();
215: if (key.equals(Markup.CSS_KEY_FONTFAMILY)) {
216: h.put("face", prop.getProperty(key));
217: } else if (key.equals(Markup.CSS_KEY_FONTSIZE)) {
218: h.put("size", Float.toString(Markup.parseLength(prop
219: .getProperty(key)))
220: + "px");
221: } else if (key.equals(Markup.CSS_KEY_FONTSTYLE)) {
222: String ss = prop.getProperty(key).trim().toLowerCase();
223: if (ss.equals("italic") || ss.equals("oblique"))
224: h.put("i", null);
225: } else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) {
226: String ss = prop.getProperty(key).trim().toLowerCase();
227: if (ss.equals("bold") || ss.equals("700")
228: || ss.equals("800") || ss.equals("900"))
229: h.put("b", null);
230: } else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) {
231: String ss = prop.getProperty(key).trim().toLowerCase();
232: if (ss.equals("underline"))
233: h.put("u", null);
234: } else if (key.equals(Markup.CSS_KEY_COLOR)) {
235: Color c = Markup.decodeColor(prop.getProperty(key));
236: if (c != null) {
237: int hh = c.getRGB();
238: String hs = Integer.toHexString(hh);
239: hs = "000000" + hs;
240: hs = "#" + hs.substring(hs.length() - 6);
241: h.put("color", hs);
242: }
243: } else if (key.equals(Markup.CSS_KEY_LINEHEIGHT)) {
244: String ss = prop.getProperty(key).trim();
245: float v = Markup.parseLength(prop.getProperty(key));
246: if (ss.endsWith("%")) {
247: h.put("leading", "0," + (v / 100));
248: } else {
249: h.put("leading", v + ",0");
250: }
251: } else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) {
252: String ss = prop.getProperty(key).trim().toLowerCase();
253: h.put("align", ss);
254: }
255: }
256: }
257:
258: public FontFactoryImp getFontImp() {
259: return fontImp;
260: }
261:
262: public void setFontImp(FontFactoryImp fontImp) {
263: this .fontImp = fontImp;
264: }
265:
266: public static HashMap followTags = new HashMap();
267: static {
268: followTags.put("i", "i");
269: followTags.put("b", "b");
270: followTags.put("u", "u");
271: followTags.put("sub", "sub");
272: followTags.put("sup", "sup");
273: followTags.put("em", "i");
274: followTags.put("strong", "b");
275: followTags.put("s", "s");
276: followTags.put("strike", "s");
277: }
278: }
|