]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/Text.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/Text.java
new file mode 100644 (file)
index 0000000..93777a7
--- /dev/null
@@ -0,0 +1,59 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.graphviz.internal.xdot;\r
+\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+public class Text implements DrawCommand {\r
+\r
+       double x;\r
+       double y;\r
+       double j;\r
+       double w;\r
+       String text;\r
+       \r
+       public Text(double x, double y, int j, double w, String text) {\r
+               this.x = x;\r
+               this.y = -y;\r
+               this.j = j;\r
+               this.w = w;\r
+               this.text = text;\r
+       }\r
+\r
+       @Override\r
+       public void draw(Graphics2D g) {\r
+           double lx;\r
+           \r
+           if(j == -1)\r
+            lx = x;\r
+        else if(j == 0)\r
+            lx = x - 0.5*w;\r
+        else if(j == 1)\r
+            lx = x - w;\r
+        else\r
+            lx = x;\r
+        //g.draw(new Line2D.Double(lx, y, lx+w, y));\r
+           g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, \r
+               RenderingHints.VALUE_FRACTIONALMETRICS_ON);\r
+           Rectangle2D rect = g.getFont().getStringBounds(text, g.getFontRenderContext());\r
+           AffineTransform oldTransform = g.getTransform();\r
+           g.translate(lx, y);\r
+           double scale = w / rect.getWidth(); \r
+           g.scale(scale, scale);\r
+               g.drawString(text, 0.0f, 0.0f);\r
+               g.setTransform(oldTransform);\r
+       }\r
+       \r
+}\r