]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/Text.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / xdot / Text.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz.internal.xdot;
13
14 import java.awt.Graphics2D;
15 import java.awt.RenderingHints;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18
19 public class Text implements DrawCommand {
20
21         double x;
22         double y;
23         double j;
24         double w;
25         String text;
26         
27         public Text(double x, double y, int j, double w, String text) {
28                 this.x = x;
29                 this.y = -y;
30                 this.j = j;
31                 this.w = w;
32                 this.text = text;
33         }
34
35         @Override
36         public void draw(Graphics2D g) {
37             double lx;
38             
39             if(j == -1)
40             lx = x;
41         else if(j == 0)
42             lx = x - 0.5*w;
43         else if(j == 1)
44             lx = x - w;
45         else
46             lx = x;
47         //g.draw(new Line2D.Double(lx, y, lx+w, y));
48             g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, 
49                 RenderingHints.VALUE_FRACTIONALMETRICS_ON);
50             Rectangle2D rect = g.getFont().getStringBounds(text, g.getFontRenderContext());
51             AffineTransform oldTransform = g.getTransform();
52             g.translate(lx, y);
53             double scale = w / rect.getWidth(); 
54             g.scale(scale, scale);
55                 g.drawString(text, 0.0f, 0.0f);
56                 g.setTransform(oldTransform);
57         }
58         
59 }