forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxHtmlTextShape.java
More file actions
134 lines (118 loc) · 3.27 KB
/
mxHtmlTextShape.java
File metadata and controls
134 lines (118 loc) · 3.27 KB
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
/**
* Copyright (c) 2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.shape;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Map;
import javax.swing.CellRendererPane;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxLightweightLabel;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
/**
* To set global CSS for all HTML labels, use the following code:
*
* <pre>
* mxGraphics2DCanvas.putTextShape(mxGraphics2DCanvas.TEXT_SHAPE_HTML,
* new mxHtmlTextShape()
* {
* protected String createHtmlDocument(Map<String, Object> style, String text)
* {
* return mxUtils.createHtmlDocument(style, text, 1, 0,
* "<style type=\"text/css\">.selectRef { " +
* "font-size:9px;font-weight:normal; }</style>");
* }
* }
* );
* </pre>
*/
public class mxHtmlTextShape implements mxITextShape
{
/**
* Specifies if linefeeds should be replaced with breaks in HTML markup.
* Default is true.
*/
protected boolean replaceHtmlLinefeeds = true;
/**
* Returns replaceHtmlLinefeeds
*/
public boolean isReplaceHtmlLinefeeds()
{
return replaceHtmlLinefeeds;
}
/**
* Returns replaceHtmlLinefeeds
*/
public void setReplaceHtmlLinefeeds(boolean value)
{
replaceHtmlLinefeeds = value;
}
/**
*
*/
protected String createHtmlDocument(Map<String, Object> style, String text,
int w, int h)
{
String overflow = mxUtils.getString(style, mxConstants.STYLE_OVERFLOW, "");
if (overflow.equals("fill"))
{
return mxUtils.createHtmlDocument(style, text, 1, w, null, "height:" + h + "pt;");
}
else if (overflow.equals("width"))
{
return mxUtils.createHtmlDocument(style, text, 1, w);
}
else
{
return mxUtils.createHtmlDocument(style, text);
}
}
/**
*
*/
public void paintShape(mxGraphics2DCanvas canvas, String text,
mxCellState state, Map<String, Object> style)
{
mxLightweightLabel textRenderer = mxLightweightLabel
.getSharedInstance();
CellRendererPane rendererPane = canvas.getRendererPane();
Rectangle rect = state.getLabelBounds().getRectangle();
Graphics2D g = canvas.getGraphics();
if (textRenderer != null
&& rendererPane != null
&& (g.getClipBounds() == null || g.getClipBounds().intersects(
rect)))
{
double scale = canvas.getScale();
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
if (!mxUtils.isTrue(style, mxConstants.STYLE_HORIZONTAL, true))
{
g.rotate(-Math.PI / 2, x + w / 2, y + h / 2);
g.translate(w / 2 - h / 2, h / 2 - w / 2);
int tmp = w;
w = h;
h = tmp;
}
// Replaces the linefeeds with BR tags
if (isReplaceHtmlLinefeeds())
{
text = text.replaceAll("\n", "<br>");
}
// Renders the scaled text
textRenderer.setText(createHtmlDocument(style, text,
(int) Math.round(w / state.getView().getScale()),
(int) Math.round(h / state.getView().getScale())));
textRenderer.setFont(mxUtils.getFont(style, canvas.getScale()));
g.scale(scale, scale);
rendererPane.paintComponent(g, textRenderer, rendererPane,
(int) (x / scale) + mxConstants.LABEL_INSET,
(int) (y / scale) + mxConstants.LABEL_INSET,
(int) (w / scale), (int) (h / scale), true);
}
}
}