]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/Text.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / xdot / Text.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphviz.internal.xdot;\r
13 \r
14 import java.awt.Graphics2D;\r
15 import java.awt.RenderingHints;\r
16 import java.awt.geom.AffineTransform;\r
17 import java.awt.geom.Rectangle2D;\r
18 \r
19 public class Text implements DrawCommand {\r
20 \r
21         double x;\r
22         double y;\r
23         double j;\r
24         double w;\r
25         String text;\r
26         \r
27         public Text(double x, double y, int j, double w, String text) {\r
28                 this.x = x;\r
29                 this.y = -y;\r
30                 this.j = j;\r
31                 this.w = w;\r
32                 this.text = text;\r
33         }\r
34 \r
35         @Override\r
36         public void draw(Graphics2D g) {\r
37             double lx;\r
38             \r
39             if(j == -1)\r
40             lx = x;\r
41         else if(j == 0)\r
42             lx = x - 0.5*w;\r
43         else if(j == 1)\r
44             lx = x - w;\r
45         else\r
46             lx = x;\r
47         //g.draw(new Line2D.Double(lx, y, lx+w, y));\r
48             g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, \r
49                 RenderingHints.VALUE_FRACTIONALMETRICS_ON);\r
50             Rectangle2D rect = g.getFont().getStringBounds(text, g.getFontRenderContext());\r
51             AffineTransform oldTransform = g.getTransform();\r
52             g.translate(lx, y);\r
53             double scale = w / rect.getWidth(); \r
54             g.scale(scale, scale);\r
55                 g.drawString(text, 0.0f, 0.0f);\r
56                 g.setTransform(oldTransform);\r
57         }\r
58         \r
59 }\r