GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mxgraph/java/src/com/mxgraph/shape/mxDefaultTextShape.java at master · devgis/mxgraph

Expand file tree

/

mxDefaultTextShape.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
140 lines (121 loc) · 3.9 KB
/

mxDefaultTextShape.java

Copy path

File metadata and controls

140 lines (121 loc) · 3.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.shape;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Map;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
public class mxDefaultTextShape implements mxITextShape
{
/**
*
*/
public void paintShape(mxGraphics2DCanvas canvas, String text,
mxCellState state, Map<String, Object> style)
{
Rectangle rect = state.getLabelBounds().getRectangle();
Graphics2D g = canvas.getGraphics();
if (g.getClipBounds() == null || g.getClipBounds().intersects(rect))
{
boolean horizontal = mxUtils.isTrue(style,
mxConstants.STYLE_HORIZONTAL, true);
double scale = canvas.getScale();
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
if (!horizontal)
{
g.rotate(-Math.PI / 2, x + w / 2, y + h / 2);
g.translate(w / 2 - h / 2, h / 2 - w / 2);
}
Color fontColor = mxUtils.getColor(style,
mxConstants.STYLE_FONTCOLOR, Color.black);
g.setColor(fontColor);
// Shifts the y-coordinate down by the ascent plus a workaround
// for the line not starting at the exact vertical location
Font scaledFont = mxUtils.getFont(style, scale);
g.setFont(scaledFont);
int fontSize = mxUtils.getInt(style, mxConstants.STYLE_FONTSIZE,
mxConstants.DEFAULT_FONTSIZE);
FontMetrics fm = g.getFontMetrics();
int scaledFontSize = scaledFont.getSize();
double fontScaleFactor = ((double) scaledFontSize)
/ ((double) fontSize);
// This factor is the amount by which the font is smaller/
// larger than we expect for the given scale. 1 means it's
// correct, 0.8 means the font is 0.8 the size we expected
// when scaled, etc.
double fontScaleRatio = fontScaleFactor / scale;
// The y position has to be moved by (1 - ratio) * height / 2
y += 2 * fm.getMaxAscent() - fm.getHeight()
+ mxConstants.LABEL_INSET * scale;
Object vertAlign = mxUtils.getString(style,
mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_MIDDLE);
double vertAlignProportion = 0.5;
if (vertAlign.equals(mxConstants.ALIGN_TOP))
{
vertAlignProportion = 0;
}
else if (vertAlign.equals(mxConstants.ALIGN_BOTTOM))
{
vertAlignProportion = 1.0;
}
y += (1.0 - fontScaleRatio) * h * vertAlignProportion;
// Gets the alignment settings
Object align = mxUtils.getString(style, mxConstants.STYLE_ALIGN,
mxConstants.ALIGN_CENTER);
if (align.equals(mxConstants.ALIGN_LEFT))
{
x += mxConstants.LABEL_INSET * scale;
}
else if (align.equals(mxConstants.ALIGN_RIGHT))
{
x -= mxConstants.LABEL_INSET * scale;
}
// Draws the text line by line
String[] lines = text.split("\n");
for (int i = 0; i < lines.length; i++)
{
int dx = 0;
if (align.equals(mxConstants.ALIGN_CENTER))
{
int sw = fm.stringWidth(lines[i]);
if (horizontal)
{
dx = (w - sw) / 2;
}
else
{
dx = (h - sw) / 2;
}
}
else if (align.equals(mxConstants.ALIGN_RIGHT))
{
int sw = fm.stringWidth(lines[i]);
dx = ((horizontal) ? w : h) - sw;
}
g.drawString(lines[i], x + dx, y);
postProcessLine(text, lines[i], fm, canvas, x + dx, y);
y += fm.getHeight() + mxConstants.LINESPACING;
}
}
}
/**
* Hook to add functionality after a line has been drawn
* @param text the entire label text
* @param line the line at the specified location
* @param fm the text font metrics
* @param canvas the canvas object currently being painted to
* @param x the x co-ord of the baseline of the text line
* @param y the y co-ord of the baseline of the text line
*/
protected void postProcessLine(String text, String line, FontMetrics fm, mxGraphics2DCanvas canvas, int x, int y){}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.