File: Transform.java

package info (click to toggle)
h2orestart 0.7.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,168 kB
  • sloc: java: 21,845; makefile: 36; xml: 19
file content (384 lines) | stat: -rw-r--r-- 11,976 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright (C) 2023 ebandal
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */
/* 본 제품은 한글과컴퓨터의 ᄒᆞᆫ글 문서 파일(.hwp) 공개 문서를 참고하여 개발하였습니다.
 * 개방형 워드프로세서 마크업 언어(OWPML) 문서 구조 KS X 6101:2018 문서를 참고하였습니다.
 * 작성자 : 반희수 ebandal@gmail.com  
 * 작성일 : 2022.10
 */
package soffice;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

import com.sun.star.beans.PropertyValue;
import com.sun.star.drawing.TextVerticalAdjust;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.style.ParagraphAdjust;
import com.sun.star.table.BorderLine;
import com.sun.star.table.BorderLine2;
import com.sun.star.table.BorderLineStyle;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;

import HwpDoc.HwpElement.HwpRecordTypes;
import HwpDoc.HwpElement.HwpRecord_BorderFill;
import HwpDoc.HwpElement.HwpRecordTypes.LineStyle2;
import HwpDoc.paragraph.Ctrl_GeneralShape;
import HwpDoc.paragraph.Ctrl_ShapePic;

public class Transform {
    
	/*
	 * change value in HWP to value in OpenOffice
	 * A4의 가로 길이는 Hwp에서는 59529(1/7000 inch), OpenOffice에서는 210(mm)
	 */
	public static int translateHwp2Office(int hwpValue) {
		int officeValue = (int)(hwpValue*((double)21000/59529));
		return officeValue;
	}

	public static BorderLine toBorderLine(HwpRecord_BorderFill.Border border) {
		BorderLine line = new BorderLine();
		if (border==null) {
			line.Color= 0x000000;
			// line.LineStyle = BorderLineStyle.NONE;
			line.InnerLineWidth = 0;
			line.OuterLineWidth = 0;
			line.LineDistance = 0;
		} else {
			line.Color= border.color;
			// line.LineStyle= Transform.toBorderLineStyle(border.type);
			line.InnerLineWidth = 0;
			line.OuterLineWidth = border.style==LineStyle2.NONE?0:Transform.toLineWidth(border.width);
			line.LineDistance = 0;
		}
		return line;
	}

	public static BorderLine2 toBorderLine2(HwpRecord_BorderFill.Border border) {
		BorderLine2 line = new BorderLine2();
		if (border==null || border.style == LineStyle2.NONE) {
			line.Color= 0x000000;
			line.LineStyle = BorderLineStyle.NONE;
			line.InnerLineWidth = 0;
			line.OuterLineWidth = 0;
			line.LineDistance = 0;
			line.LineWidth = 0;
		} else {
			line.LineStyle= Transform.toBorderLineStyle(border.style);
			line.Color= border.color;
			
			switch(line.LineStyle) {
			case BorderLineStyle.DOUBLE:
			case BorderLineStyle.DOUBLE_THIN:
				line.LineWidth = Transform.toLineWidth(border.width);
				line.InnerLineWidth = (short)(line.LineWidth/3);
				line.OuterLineWidth = (short)(line.LineWidth/3);
				line.LineDistance = (short)(line.LineWidth);
				break;
			case BorderLineStyle.THICKTHIN_LARGEGAP:
			case BorderLineStyle.THICKTHIN_MEDIUMGAP:
			case BorderLineStyle.THICKTHIN_SMALLGAP:
			case BorderLineStyle.THINTHICK_LARGEGAP:
			case BorderLineStyle.THINTHICK_MEDIUMGAP:
			case BorderLineStyle.THINTHICK_SMALLGAP:
				line.LineWidth = Transform.toLineWidth(border.width);
				line.InnerLineWidth = (short)(line.LineWidth/3);
				line.OuterLineWidth = (short)(line.LineWidth/3);
				line.LineDistance = (short)(line.LineWidth);
				break;
			case BorderLineStyle.SOLID:
			case BorderLineStyle.DASHED:
			case BorderLineStyle.FINE_DASHED:
			case BorderLineStyle.DOTTED:
			case BorderLineStyle.DASH_DOT:
			case BorderLineStyle.DASH_DOT_DOT:
			case BorderLineStyle.EMBOSSED:
			case BorderLineStyle.ENGRAVED:
			case BorderLineStyle.INSET:
			case BorderLineStyle.OUTSET:
			default:
				line.LineWidth = Transform.toLineWidth(border.width);
				line.InnerLineWidth = 0;
				line.OuterLineWidth = (short)line.LineWidth;
				line.LineDistance = 0;
			}
		}
		return line;
	}

	public static BorderLine2 toBorderLineDefault(HwpRecord_BorderFill.Border border) {
		BorderLine2 line = new BorderLine2();
		if (border==null || border.style == LineStyle2.NONE) {
			line.Color= 0x000000;
			line.LineStyle = BorderLineStyle.NONE;
			line.InnerLineWidth = 0;
			line.OuterLineWidth = 0;
			line.LineDistance = 0;
			line.LineWidth = 0;
		} else {
			line.LineStyle= BorderLineStyle.DOUBLE;
			line.LineWidth = 90;
			line.Color= 0x000000;
			line.InnerLineWidth = 30;
			line.OuterLineWidth = 30;
			line.LineDistance = 30;
		}
		return line;
	}

	public static BorderLine2 toBorderLine2(Ctrl_GeneralShape shape) {
		BorderLine2 line = new BorderLine2();
		if (shape==null) {
			line.Color= 0x000000;
			line.LineStyle = BorderLineStyle.NONE;
			line.InnerLineWidth = 0;
			line.OuterLineWidth = 0;
			line.LineDistance = 0;
			line.LineWidth = 0;
		} else {
			if (shape instanceof Ctrl_ShapePic) {
				line.Color= ((Ctrl_ShapePic)shape).borderColor;
				line.LineStyle= BorderLineStyle.SOLID;
				line.InnerLineWidth = 0;
				line.LineWidth = Transform.translateHwp2Office(((Ctrl_ShapePic)shape).borderThick);
			} else {
				line.Color= shape.lineColor;
				line.LineStyle= Transform.toBorderLineStyle(shape.lineStyle);
				line.InnerLineWidth = 0;
				line.OuterLineWidth = shape.lineStyle==LineStyle2.NONE?0:Transform.toLineWidth((short)shape.lineThick);
				line.LineDistance = (short)(shape.lineStyle==LineStyle2.NONE?0:100);
				line.LineWidth = shape.lineStyle==LineStyle2.NONE?0:Transform.translateHwp2Office(shape.lineThick);
				if (line.LineStyle!=BorderLineStyle.NONE && line.LineWidth==0) {
					line.LineWidth = 1;
				}
			}
		}
		return line;
	}

	public static ParagraphAdjust toHorzAlign(short align ) {
		ParagraphAdjust adjust = com.sun.star.style.ParagraphAdjust.BLOCK;
		
		switch(align) {
		case 0:		// 0:양쪽정렬
			adjust = com.sun.star.style.ParagraphAdjust.STRETCH;
			break;
		case 1:		// 1:왼쪽정렬
			adjust = com.sun.star.style.ParagraphAdjust.LEFT;
			break;
		case 2:		// 2:오른쪽정렬
			adjust = com.sun.star.style.ParagraphAdjust.RIGHT;
			break;
		case 3:		// 3:가운데정렬
			adjust = com.sun.star.style.ParagraphAdjust.CENTER;
			break;
		case 4:		// 4:배분정렬
		case 5:		// 5:나눔 정렬
			adjust = com.sun.star.style.ParagraphAdjust.BLOCK;
			break;
		}
		return adjust;
	}

	public static TextVerticalAdjust toTextVertAlign(int align) {
		TextVerticalAdjust orient = TextVerticalAdjust.BLOCK;
		
		switch(align) {
		case 0:		// 0:Top
			orient = TextVerticalAdjust.TOP;
			break;
		case 1:		// 1:Center
			orient = TextVerticalAdjust.CENTER;
			break;
		case 2:		// 2:Bottom
			orient = TextVerticalAdjust.BOTTOM;
			break;
		}
		return orient;
	}

	public static short toVertAlign(int align) {
		short orient = com.sun.star.text.VertOrientation.NONE;
		
		switch(align) {
		case 0:		// 0:Top
			orient = com.sun.star.text.VertOrientation.TOP;
			break;
		case 1:		// 1:Center
			orient = com.sun.star.text.VertOrientation.CENTER;
			break;
		case 2:		// 2:Bottom
			orient = com.sun.star.text.VertOrientation.BOTTOM;
			break;
		}
		return orient;
	}
	
	public static short toLineWidth(short hwpThick) {
		short lineWidth = 0;
		switch(hwpThick) {
		case 0:		// 0.1mm
			lineWidth = 10; break;
		case 1:		// 0.12mm
			lineWidth = 12; break;
		case 2:		// 0.15mm
			lineWidth = 15; break;
		case 3:		// 0.2mm
			lineWidth = 20; break;
		case 4:		// 0.25mm
			lineWidth = 25; break;
		case 5:		// 0.3mm
			lineWidth = 30; break;
		case 6:		// 0.4mm
			lineWidth = 40; break;
		case 7:		// 0.5mm
			lineWidth = 50; break;
		case 8:		// 0.6mm
			lineWidth = 60; break;
		case 9:		// 0.7mm
			lineWidth = 70; break;
		case 10:	// 1.0mm
			lineWidth = 100; break;
		case 11:	// 1.5mm
			lineWidth = 150; break;
		case 12:	// 2.0mm
			lineWidth = 200; break;
		case 13:	// 3.0mm
			lineWidth = 300; break;
		case 14:	// 4.0mm
			lineWidth = 400; break;
		case 15:	// 5.0mm
			lineWidth = 500; break;
		}
		return lineWidth;
	}
	
	public static short toBorderLineStyle(HwpRecordTypes.LineStyle2 line) {
		short lineStyle = BorderLineStyle.NONE;
		if (line==null) return lineStyle;
		
		switch(line) {
		case NONE:
			lineStyle =  BorderLineStyle.NONE;
			break;
		case SOLID:
			lineStyle =  BorderLineStyle.SOLID;
			break;
		case DASH:
		case LONG_DASH:
			lineStyle =  BorderLineStyle.DASHED;
			break;
		case DOT:
			lineStyle =  BorderLineStyle.DOTTED;
			break;
		case DASH_DOT:
			lineStyle =  BorderLineStyle.DASH_DOT;
			break;
		case DASH_DOT_DOT:
			lineStyle =  BorderLineStyle.DASH_DOT_DOT;
			break;
		case DOUBLE_SLIM:
			lineStyle =  BorderLineStyle.THICKTHIN_MEDIUMGAP;
			break;
		case SLIM_THICK:
			lineStyle =  BorderLineStyle.THINTHICK_MEDIUMGAP;
			// lineStyle =  BorderLineStyle.DOUBLE;
			break;
		case THICK_SLIM:
			lineStyle =  BorderLineStyle.THICKTHIN_MEDIUMGAP;
			// lineStyle =  BorderLineStyle.DOUBLE;
			break;
		case SLIM_THICK_SLIM:
			lineStyle =  BorderLineStyle.THINTHICK_MEDIUMGAP;
			// lineStyle =  BorderLineStyle.DOUBLE;
			break;
		case CIRCLE:
		default:
			lineStyle =  BorderLineStyle.DASHED;
			break;
		}
		
		return lineStyle;
	}
	
	public static void save2Pdf(XTextDocument myDoc, String fileURI) throws IOException {
        if (fileURI==null || fileURI.isEmpty()) {
        	return;
        }
        fileURI = fileURI.replace('\\',  '/');
        // save as a PDF
        PropertyValue[] propertyValues = new PropertyValue[2];
        propertyValues[0] = new PropertyValue();
        propertyValues[0].Name = "Overwrite";
        propertyValues[0].Value = true;
        propertyValues[1] = new PropertyValue();
        propertyValues[1].Name = "FilterName";
        propertyValues[1].Value = "writer_pdf_Export";
        
        XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, myDoc);
		xStorable.storeToURL("file:///" + fileURI, propertyValues);
	}
	
	public static void save2Odf(XTextDocument myDoc, String fileURI) throws IOException {
        if (fileURI==null || fileURI.isEmpty()) {
        	return;
        }
        fileURI = fileURI.replace('\\',  '/');
        // save as ODF
        PropertyValue[] propertyValues = new PropertyValue[2];
        propertyValues[0] = new PropertyValue();
        propertyValues[0].Name = "Overwrite";
        propertyValues[0].Value = true;
        propertyValues[1] = new PropertyValue();
        propertyValues[1].Name = "FilterName";
        propertyValues[1].Value = "writer8";

        XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, myDoc);
       	xStorable.storeToURL("file:///" + fileURI, propertyValues);
	}

    public static Point2D rotateValue(int rotat, Point2D ptSrc) {
        Point2D ptDst = null;
        
        if (rotat%90 == 0) { // 4 quadrant
            if (rotat%180 == 0) {
                ptDst = (Point2D) ptSrc.clone();
            } else {
                ptDst = new Point2D.Double(ptSrc.getY(), ptSrc.getX());
            }
        } else {
            AffineTransform at = new AffineTransform();
            at.translate(0, 0);
            double angle = rotat%180==90? Math.PI/2 : Math.toRadians(rotat%180);
            at.rotate(angle);
            ptDst = at.transform(ptSrc, null);
        }
        return ptDst;
    }

    public static Point2D rotateValue(double angle, Point2D ptSrc) {
        Point2D ptDst = null;
        
        AffineTransform at = new AffineTransform();
        at.translate(0, 0);
        at.rotate(angle);
        ptDst = at.transform(ptSrc, null);
        return ptDst;
    }

}