1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
14 import java.awt.Color;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Point2D;
20 import java.awt.geom.Rectangle2D;
21 import java.io.IOException;
23 import org.simantics.scenegraph.ExportableWidget.OutputWidget;
24 import org.simantics.scenegraph.g2d.G2DNode;
25 import org.simantics.scenegraph.g2d.G2DPDFRenderingHints;
27 import com.lowagie.text.DocumentException;
28 import com.lowagie.text.Element;
29 import com.lowagie.text.Rectangle;
30 import com.lowagie.text.pdf.FontMapper;
31 import com.lowagie.text.pdf.PdfFormField;
32 import com.lowagie.text.pdf.PdfWriter;
33 import com.lowagie.text.pdf.TextField;
36 public class TextNode extends G2DNode {
38 private static final long serialVersionUID = 8508750881358776559L;
40 protected Rectangle2D bounds = null;
41 protected String text = null;
42 protected Font font = null;
43 protected Color color = Color.BLACK;
46 public void setBounds(Rectangle2D bounds) {
51 public void setFont(Font font) {
56 public void setText(String text) {
60 public String getText() {
65 public void setColor(Color color) {
70 public void render(Graphics2D g) {
71 if (text == null || bounds == null || font == null || color == null)
74 // PdfWriter writer = (PdfWriter) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER);
75 // if ( writer == null ) {
76 // Write to Graphics2D
80 FontMetrics fm = g.getFontMetrics(font);
81 Rectangle2D rect = fm.getStringBounds(text, g);
82 float x = (float) ((bounds.getWidth() - rect.getWidth())/2 + bounds.getMinX());
83 float y = (float) ((bounds.getHeight() - rect.getHeight())/2 + bounds.getMinY());
85 AffineTransform at = g.getTransform();
86 g.transform(getTransform());
87 g.drawString(text, x, (float)(y+rect.getHeight()));
93 FontMapper mapper = (FontMapper) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_FONTMAPPER);
94 //text.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
95 AffineTransform at = g.getTransform();
96 FontMetrics fm = g.getFontMetrics(font);
97 Rectangle2D rect = fm.getStringBounds(text, g);
98 float x = (float) ((bounds.getWidth() - rect.getWidth())/2 + bounds.getMinX());
99 float y = (float) ((bounds.getHeight() - rect.getHeight())/2 + bounds.getMinY());
101 Point2D pt1 = at.transform(new Point2D.Float(x, y+(float)rect.getHeight()), null);
102 Point2D pt2 = at.transform(new Point2D.Float(x+(float)rect.getWidth(), y), null);
103 Rectangle rectangle = new Rectangle(
108 TextField text = new TextField(writer, rectangle, "approvedBy");
109 text.setText(this.text);
110 text.setFont(mapper.awtToPdf(font));
112 text.setAlignment(Element.ALIGN_LEFT);
113 text.setRotation(90);
114 text.setOptions(TextField.READ_ONLY);
115 PdfFormField field = text.getTextField();
116 writer.addAnnotation(field);
117 } catch (IOException e) {
119 } catch (DocumentException e) {
127 // public Rectangle2D getBoundsInLocal() {
132 public Rectangle2D getBoundsInLocal() {
137 public Rectangle2D getBounds() {