001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.export.oasis;
029:
030: import java.awt.Color;
031: import java.awt.font.TextAttribute;
032: import java.io.IOException;
033: import java.io.Writer;
034: import java.util.Collection;
035: import java.util.HashMap;
036: import java.util.HashSet;
037: import java.util.Map;
038: import java.util.Set;
039:
040: import net.sf.jasperreports.engine.JRBox;
041: import net.sf.jasperreports.engine.JRPrintElement;
042: import net.sf.jasperreports.engine.JRPrintFrame;
043: import net.sf.jasperreports.engine.JRPrintGraphicElement;
044: import net.sf.jasperreports.engine.JRPrintImage;
045: import net.sf.jasperreports.engine.JRPrintText;
046: import net.sf.jasperreports.engine.base.JRBaseBox;
047: import net.sf.jasperreports.engine.util.JRColorUtil;
048:
049: /**
050: * @author Teodor Danciu (teodord@users.sourceforge.net)
051: * @version $Id: StyleCache.java 1664 2007-03-26 09:48:03Z lucianc $
052: */
053: public class StyleCache {
054: /**
055: *
056: */
057: private Writer styleWriter = null;
058: private Map fontMap = null;
059: private Set fontFaces = new HashSet();
060:
061: /**
062: *
063: */
064: private Map frameStyles = new HashMap();//FIXMEODT soft cache?
065: private int frameStylesCounter = 0;
066: private Map cellStyles = new HashMap();
067: private int cellStylesCounter = 0;
068: private Map graphicStyles = new HashMap();
069: private int graphicStylesCounter = 0;
070: private Map paragraphStyles = new HashMap();
071: private int paragraphStylesCounter = 0;
072: private Map textSpanStyles = new HashMap();
073: private int textSpanStylesCounter = 0;
074:
075: /**
076: *
077: */
078: public StyleCache(Writer styleWriter, Map fontMap) {
079: this .styleWriter = styleWriter;
080: this .fontMap = fontMap;
081: }
082:
083: /**
084: *
085: */
086: public Collection getFontFaces() {
087: return fontFaces;
088: }
089:
090: /**
091: *
092: */
093: public String getFrameStyle(JRPrintText text) throws IOException {
094: FrameStyle frameStyle = new FrameStyle(styleWriter, text);
095: frameStyle.setBox(text);
096:
097: String frameStyleId = frameStyle.getId();
098: String frameStyleName = (String) frameStyles.get(frameStyleId);
099:
100: if (frameStyleName == null) {
101: frameStyleName = "F" + frameStylesCounter++;
102: frameStyles.put(frameStyleId, frameStyleName);
103:
104: frameStyle.write(frameStyleName);
105: }
106:
107: return frameStyleName;
108: }
109:
110: /**
111: *
112: */
113: public String getFrameStyle(JRPrintElement element)
114: throws IOException {
115: FrameStyle frameStyle = new FrameStyle(styleWriter, element);
116:
117: String frameStyleId = frameStyle.getId();
118: String frameStyleName = (String) frameStyles.get(frameStyleId);
119:
120: if (frameStyleName == null) {
121: frameStyleName = "F" + frameStylesCounter++;
122: frameStyles.put(frameStyleId, frameStyleName);
123:
124: frameStyle.write(frameStyleName);
125: }
126:
127: return frameStyleName;
128: }
129:
130: /**
131: *
132: */
133: public String getGraphicStyle(JRPrintGraphicElement element)
134: throws IOException {
135: GraphicStyle graphicStyle = new GraphicStyle(styleWriter,
136: element);
137:
138: String graphicStyleId = graphicStyle.getId();
139: String graphicStyleName = (String) cellStyles
140: .get(graphicStyleId);
141:
142: if (graphicStyleName == null) {
143: graphicStyleName = "G" + graphicStylesCounter++;
144: graphicStyles.put(graphicStyleId, graphicStyleName);
145:
146: graphicStyle.write(graphicStyleName);
147: }
148:
149: return graphicStyleName;
150: }
151:
152: /**
153: *
154: */
155: public String getCellStyle(JRPrintFrame frame) throws IOException {
156: return getCellStyle(frame, frame);
157: }
158:
159: /**
160: *
161: */
162: public String getCellStyle(JRPrintText text) throws IOException {
163: return getCellStyle(text, text);
164: }
165:
166: /**
167: *
168: */
169: public String getCellStyle(JRPrintImage image) throws IOException {
170: return getCellStyle(image, image);
171: }
172:
173: /**
174: *
175: */
176: public String getCellStyle(JRPrintGraphicElement element)
177: throws IOException {
178: return getCellStyle(element, new JRBaseBox(element.getPen(),
179: element.getForecolor()));
180: }
181:
182: /**
183: *
184: */
185: public String getCellStyle(JRPrintElement element, JRBox box)
186: throws IOException {
187: CellStyle cellStyle = new CellStyle(styleWriter, element);
188: cellStyle.setBox(box);
189:
190: String cellStyleId = cellStyle.getId();
191: String cellStyleName = (String) cellStyles.get(cellStyleId);
192:
193: if (cellStyleName == null) {
194: cellStyleName = "C" + cellStylesCounter++;
195: cellStyles.put(cellStyleId, cellStyleName);
196:
197: cellStyle.write(cellStyleName);
198: }
199:
200: return cellStyleName;
201: }
202:
203: /**
204: *
205: */
206: public String getParagraphStyle(JRPrintText text)
207: throws IOException {
208: ParagraphStyle paragraphStyle = new ParagraphStyle(styleWriter,
209: text);
210:
211: String paragraphStyleId = paragraphStyle.getId();
212: String paragraphStyleName = (String) paragraphStyles
213: .get(paragraphStyleId);
214:
215: if (paragraphStyleName == null) {
216: paragraphStyleName = "P" + paragraphStylesCounter++;
217: paragraphStyles.put(paragraphStyleId, paragraphStyleName);
218:
219: paragraphStyle.write(paragraphStyleName);
220: }
221:
222: return paragraphStyleName;
223: }
224:
225: /**
226: *
227: */
228: public String getTextSpanStyle(Map attributes, String text)
229: throws IOException {
230: String fontFamily;
231: String fontFamilyAttr = (String) attributes
232: .get(TextAttribute.FAMILY);
233: if (fontMap != null && fontMap.containsKey(fontFamilyAttr)) {
234: fontFamily = (String) fontMap.get(fontFamilyAttr);
235: } else {
236: fontFamily = fontFamilyAttr;
237: }
238: fontFaces.add(fontFamily);
239:
240: StringBuffer textSpanStyleIdBuffer = new StringBuffer();
241: textSpanStyleIdBuffer.append(fontFamily);
242:
243: String forecolorHexa = null;
244: Color forecolor = (Color) attributes
245: .get(TextAttribute.FOREGROUND);
246: if (!Color.black.equals(forecolor)) {
247: forecolorHexa = JRColorUtil.getColorHexa(forecolor);
248: textSpanStyleIdBuffer.append(forecolorHexa);
249: }
250:
251: String backcolorHexa = null;
252: Color runBackcolor = (Color) attributes
253: .get(TextAttribute.BACKGROUND);
254: if (runBackcolor != null) {
255: backcolorHexa = JRColorUtil.getColorHexa(runBackcolor);
256: textSpanStyleIdBuffer.append(backcolorHexa);
257: }
258:
259: String size = String
260: .valueOf(attributes.get(TextAttribute.SIZE));
261: textSpanStyleIdBuffer.append(size);
262:
263: String weight = null;
264: if (TextAttribute.WEIGHT_BOLD.equals(attributes
265: .get(TextAttribute.WEIGHT))) {
266: weight = "bold";
267: textSpanStyleIdBuffer.append(weight);
268: }
269: String posture = null;
270: if (TextAttribute.POSTURE_OBLIQUE.equals(attributes
271: .get(TextAttribute.POSTURE))) {
272: posture = "italic";
273: textSpanStyleIdBuffer.append(posture);
274: }
275: String underline = null;
276: if (TextAttribute.UNDERLINE_ON.equals(attributes
277: .get(TextAttribute.UNDERLINE))) {
278: underline = "single";
279: textSpanStyleIdBuffer.append(underline);
280: }
281: String strikeThrough = null;
282: if (TextAttribute.STRIKETHROUGH_ON.equals(attributes
283: .get(TextAttribute.STRIKETHROUGH))) {
284: strikeThrough = "single";
285: textSpanStyleIdBuffer.append(strikeThrough);
286: }
287:
288: // if (TextAttribute.SUPERSCRIPT_SUPER.equals(attributes.get(TextAttribute.SUPERSCRIPT)))
289: // {
290: // textSpanStyleIdBuffer.append(" vertical-align: super;");
291: // }
292: // else if (TextAttribute.SUPERSCRIPT_SUB.equals(attributes.get(TextAttribute.SUPERSCRIPT)))
293: // {
294: // textSpanStyleIdBuffer.append(" vertical-align: sub;");
295: // }
296:
297: String textSpanStyleId = textSpanStyleIdBuffer.toString();
298: String textSpanStyleName = (String) textSpanStyles
299: .get(textSpanStyleId);
300:
301: if (textSpanStyleName == null) {
302: textSpanStyleName = "T" + textSpanStylesCounter++;
303: textSpanStyles.put(textSpanStyleId, textSpanStyleName);
304:
305: styleWriter.write("<style:style style:name=\""
306: + textSpanStyleName + "\"");
307: styleWriter.write(" style:family=\"text\">\n");
308: styleWriter.write("<style:text-properties");
309: if (forecolorHexa != null) {
310: styleWriter.write(" fo:color=\"#" + forecolorHexa
311: + "\"");
312: }
313: styleWriter
314: .write(" style:font-name=\"" + fontFamily + "\"");
315: styleWriter.write(" fo:font-size=\"" + size + "pt\"");
316: if (posture != null) {
317: styleWriter.write(" fo:font-style=\"" + posture + "\"");
318: }
319: if (weight != null) {
320: styleWriter.write(" fo:font-weight=\"" + weight + "\"");
321: }
322: if (backcolorHexa != null) {
323: styleWriter.write(" fo:background-color=\"#"
324: + backcolorHexa + "\"");
325: }
326: // styleWriter.write(" style:text-rotation-angle=\"" + textRotationAngle + "\"");
327: // styleWriter.write(" style:text-rotation-scale=\"" + textRotationScale + "\"");
328: if (underline != null) {
329: styleWriter.write(" style:text-underline-type=\""
330: + underline + "\"");
331: }
332: if (strikeThrough != null) {
333: styleWriter.write(" style:text-line-through-type=\""
334: + strikeThrough + "\"");
335: }
336: styleWriter.write(">\n");
337: styleWriter.write("</style:text-properties>\n");
338: styleWriter.write("</style:style>\n");
339: }
340:
341: return textSpanStyleName;
342: }
343:
344: }
|