Skip to content

Commit 112df58

Browse files
committed
改造了数组类型的构造语法,使用可省略参数和序列化接口组合
完成CT_Path测试
1 parent 5d11048 commit 112df58

File tree

11 files changed

+196
-46
lines changed

11 files changed

+196
-46
lines changed

ofdrw-core/src/main/java/org/ofdrw/core/basicType/STBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ public OFDSimpleTypeElement getElement(String name) {
2929
* @return 没有小数点的整数字符串
3030
*/
3131
public static String fmt(double d) {
32-
if (d == (long) d)
32+
if (d == (long) d) {
3333
return String.format("%d", (long) d);
34-
else
34+
} else {
3535
return String.format("%s", d);
36+
}
3637
}
3738
}

ofdrw-core/src/main/java/org/ofdrw/core/basicType/ST_Array.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ofdrw.core.basicType;
22

3+
import java.io.Serializable;
34
import java.util.ArrayList;
45
import java.util.List;
56

@@ -36,20 +37,23 @@ public static ST_Array getInstance(String arrStr) {
3637
return new ST_Array(arrStr.trim().split(" "));
3738
}
3839

39-
public ST_Array(String[] arr) {
40-
array = new ArrayList<>(arr.length);
41-
for (String item : arr) {
42-
if (item == null || item.length() == 0) {
43-
throw new IllegalArgumentException("数组元素为空");
44-
}
45-
array.add(item);
46-
}
47-
}
40+
public ST_Array(Serializable... arr) {
4841

49-
public ST_Array(double[] arr) {
42+
if (arr == null) {
43+
throw new IllegalArgumentException("参数不能为空");
44+
}
5045
array = new ArrayList<>(arr.length);
51-
for (double item : arr) {
52-
array.add(STBase.fmt(item));
46+
47+
for (Serializable item : arr) {
48+
if (item instanceof String || item == null) {
49+
String str = (String) item;
50+
if (item == null || str.trim().length() == 0) {
51+
throw new IllegalArgumentException("数组元素为空");
52+
} else {
53+
item = str;
54+
}
55+
}
56+
array.add(item.toString());
5357
}
5458
}
5559

@@ -63,6 +67,28 @@ public List<String> getArray() {
6367
return array;
6468
}
6569

70+
/**
71+
* 获取浮点数组
72+
*
73+
* @return 浮点数组
74+
*/
75+
public Double[] toDouble() {
76+
return this.array.stream()
77+
.map(Double::parseDouble)
78+
.toArray(Double[]::new);
79+
}
80+
81+
/**
82+
* 获取整形数组
83+
*
84+
* @return 整形数组
85+
*/
86+
public Integer[] toInt() {
87+
return this.array.stream()
88+
.map(Integer::parseInt)
89+
.toArray(Integer[]::new);
90+
}
91+
6692

6793
public ST_Array setArray(List<String> array) {
6894
this.array = array;

ofdrw-core/src/main/java/org/ofdrw/core/basicType/ST_Box.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.ofdrw.core.basicType;
22

3+
import java.io.Serializable;
4+
35
/**
46
* 矩形区域,以空格分割,前两个值代表了该矩形的
57
* 左上角的坐标,后两个值依次表示该矩形的宽和高,
@@ -53,6 +55,31 @@ public ST_Box(double topLeftX, double topLeftY, double width, double height) {
5355
this.height = height;
5456
}
5557

58+
/**
59+
* 通用构造
60+
*
61+
* @param arr 任意类型可序列化参数
62+
*/
63+
public ST_Box(Serializable... arr) {
64+
if (arr == null) {
65+
throw new IllegalArgumentException("参数不能为空");
66+
}
67+
if (arr.length == 4) {
68+
throw new IllegalArgumentException("Box 必须元素个数等4");
69+
}
70+
71+
this.topLeftX = Double.parseDouble(arr[0].toString());
72+
this.topLeftY = Double.parseDouble(arr[1].toString());
73+
if (width <= 0) {
74+
throw new IllegalArgumentException("width 应大于0");
75+
}
76+
this.width = Double.parseDouble(arr[2].toString());
77+
if (height <= 0) {
78+
throw new IllegalArgumentException("height 应大于0");
79+
}
80+
this.height = Double.parseDouble(arr[3].toString());
81+
}
82+
5683

5784
/**
5885
* 获取 ST_Box 实例如果参数非法则返还null

ofdrw-core/src/main/java/org/ofdrw/core/graph/pathObj/AbbreviatedData.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public static LinkedList<String[]> parse(String txt) {
4646
return null;
4747
}
4848

49+
/**
50+
* 刷新元素
51+
*
52+
* 默认情况下,每次调用C都将会刷新元素内容
53+
*/
54+
public void flush(){
55+
this.setText(this.toString());
56+
}
57+
4958
/**
5059
* 定义自绘制图形边线的起始点坐标 (x,y)
5160
*
@@ -243,6 +252,8 @@ public AbbreviatedData A(double rx, double ry,
243252
*/
244253
public AbbreviatedData close() {
245254
dataQueue.add(new String[]{"C"});
255+
// 重新设置元素内容
256+
this.flush();
246257
return this;
247258
}
248259

ofdrw-core/src/main/java/org/ofdrw/core/graph/pathObj/CT_Path.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* @author 权观宇
1515
* @since 2019-10-16 08:21:58
1616
*/
17-
public class CT_Path extends CT_GraphicUnit {
18-
// TODO 2019-10-16 22:25:14 CT_Path 测试 依赖于 CT_GraphicUnit 测试
17+
public class CT_Path extends CT_GraphicUnit<CT_Path> {
1918

2019
public CT_Path(Element proxy) {
2120
super(proxy);
2221
}
2322

24-
public CT_Path(String name) {
23+
public CT_Path() {
2524
super("Path");
2625
}
2726

ofdrw-core/src/main/java/org/ofdrw/core/pageDescription/CT_GraphicUnit.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @author 权观宇
2121
* @since 2019-10-14 07:32:38
2222
*/
23-
public class CT_GraphicUnit extends OFDElement {
23+
public abstract class CT_GraphicUnit<T extends CT_GraphicUnit> extends OFDElement {
2424
public CT_GraphicUnit(Element proxy) {
2525
super(proxy);
2626
}
@@ -39,12 +39,12 @@ public CT_GraphicUnit(String name) {
3939
* @param boundary 外接矩形
4040
* @return this
4141
*/
42-
public CT_GraphicUnit setBoundary(ST_Box boundary) {
42+
public T setBoundary(ST_Box boundary) {
4343
if (boundary == null) {
4444
throw new IllegalArgumentException("外接矩形不能为空");
4545
}
4646
this.addAttribute("Boundary", boundary.toString());
47-
return this;
47+
return (T) this;
4848
}
4949

5050
/**
@@ -67,12 +67,12 @@ public ST_Box getBoundary() {
6767
* @param name 图元对象的名字
6868
* @return this
6969
*/
70-
public CT_GraphicUnit setGraphicName(String name) {
70+
public T setGraphicName(String name) {
7171
if (name == null || name.trim().length() == 0) {
72-
return this;
72+
return (T) this;
7373
}
7474
this.addAttribute("Name", name);
75-
return this;
75+
return (T) this;
7676
}
7777

7878
/**
@@ -92,9 +92,9 @@ public String getGraphicName() {
9292
* @param visible true - 可见;false - 不见
9393
* @return this
9494
*/
95-
public CT_GraphicUnit setVisible(Boolean visible) {
95+
public T setVisible(Boolean visible) {
9696
this.addAttribute("Visible", visible.toString());
97-
return this;
97+
return (T) this;
9898
}
9999

100100
/**
@@ -121,9 +121,9 @@ public Boolean getVisible() {
121121
* @param lineWidth 绘制路径时使用的线宽
122122
* @return this
123123
*/
124-
public CT_GraphicUnit setLineWidth(Double lineWidth) {
124+
public T setLineWidth(Double lineWidth) {
125125
this.addAttribute("LineWidth", lineWidth.toString());
126-
return this;
126+
return (T) this;
127127
}
128128

129129
/**
@@ -153,9 +153,9 @@ public Double getLineWidth() {
153153
* @param cap 线端点样式
154154
* @return this
155155
*/
156-
public CT_GraphicUnit setCap(LineCapType cap) {
156+
public T setCap(LineCapType cap) {
157157
this.addAttribute("Cap", cap.toString());
158-
return this;
158+
return (T) this;
159159
}
160160

161161
/**
@@ -183,9 +183,9 @@ public LineCapType getCap() {
183183
* @param join 线条连接样式
184184
* @return this
185185
*/
186-
public CT_GraphicUnit setJoin(LineJoinType join) {
186+
public T setJoin(LineJoinType join) {
187187
this.addAttribute("Join", join.toString());
188-
return this;
188+
return (T) this;
189189
}
190190

191191
/**
@@ -216,9 +216,9 @@ public LineJoinType getJoin() {
216216
* @param miterLimit Join的截断值长度
217217
* @return this
218218
*/
219-
public CT_GraphicUnit setMiterLimit(Double miterLimit) {
219+
public T setMiterLimit(Double miterLimit) {
220220
this.addAttribute("MiterLimit", miterLimit.toString());
221-
return this;
221+
return (T) this;
222222
}
223223

224224
/**
@@ -258,9 +258,9 @@ public Double getMiterLimit() {
258258
* @param dashOffset 线条虚线开始位置
259259
* @return this
260260
*/
261-
public CT_GraphicUnit setDashOffset(Double dashOffset) {
261+
public T setDashOffset(Double dashOffset) {
262262
this.addAttribute("DashOffset", dashOffset.toString());
263-
return this;
263+
return (T) this;
264264
}
265265

266266
/**
@@ -303,9 +303,9 @@ public Double getDashOffset() {
303303
* @param dashPattern 线条虚线的重复样式的数组中共含两个值,第一个值代表虚线的线段的长度,第二个值代表虚线间隔的长度。
304304
* @return this
305305
*/
306-
public CT_GraphicUnit setDashPattern(ST_Array dashPattern) {
306+
public T setDashPattern(ST_Array dashPattern) {
307307
this.addAttribute("DashPattern", dashPattern.toString());
308-
return this;
308+
return (T) this;
309309
}
310310

311311
/**
@@ -340,15 +340,15 @@ public ST_Array getDashPattern() {
340340
* @param alpha 图元对象透明度,取值区间为 [0,255]
341341
* @return this
342342
*/
343-
public CT_GraphicUnit setAlpha(Integer alpha) {
343+
public T setAlpha(Integer alpha) {
344344
if (alpha == null || alpha < 0) {
345345
alpha = 0;
346346
}
347347
if (alpha > 255) {
348348
alpha = 255;
349349
}
350350
this.addAttribute("Alpha", alpha.toString());
351-
return this;
351+
return (T) this;
352352
}
353353

354354
/**
@@ -379,9 +379,9 @@ public Integer getAlpha() {
379379
* @param actions 图元对象的动作序列
380380
* @return this
381381
*/
382-
public CT_GraphicUnit setActions(Actions actions) {
382+
public T setActions(Actions actions) {
383383
this.add(actions);
384-
return this;
384+
return (T) this;
385385
}
386386

387387
/**

ofdrw-core/src/main/java/org/ofdrw/core/pageDescription/color/color/CT_Color.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ public CT_Color() {
3838
super("Color");
3939
}
4040

41+
/**
42+
* RGB颜色值
43+
* <p>
44+
* 其中颜色空间(CT_ColorSpace)的通道使用位数(BitsPerComponent)为 8
45+
* <p>
46+
* 采用10进制表示方式
47+
*
48+
* @param r 红色 0~255
49+
* @param g 绿色 0~255
50+
* @param b 蓝色 0~255
51+
* @return RGB 颜色
52+
*/
53+
public static CT_Color rgb(int r, int g, int b) {
54+
return new CT_Color()
55+
.setValue(new ST_Array(r,g,b))
56+
.setAlpha(255);
57+
}
58+
4159
/**
4260
* 【可选 属性】
4361
* 设置 颜色值
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.ofdrw.core.basicType;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ST_ArrayTest {
8+
9+
@Test
10+
void toInt() {
11+
ST_Array arr = new ST_Array("7", "7", "7", "7", "7", "7", "7");
12+
assertArrayEquals(arr.toInt(), new Integer[]{7, 7, 7, 7, 7, 7, 7});
13+
System.out.println(arr);
14+
15+
16+
arr = ST_Array.getInstance("7 7 7 7 7 7 7");
17+
assertArrayEquals(arr.toInt(), new Integer[]{7, 7, 7, 7, 7, 7, 7});
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.ofdrw.core.basicType;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ST_BoxTest {
8+
9+
10+
@Test
11+
public void con() throws Exception {
12+
ST_Box box = new ST_Box(0, 0, 255, 255);
13+
assertTrue("0 0 255 255".equals(box.toString()));
14+
}
15+
}

0 commit comments

Comments
 (0)