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
|
/* 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 HwpDoc.HwpElement;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import HwpDoc.Exception.HwpParseException;
import HwpDoc.paragraph.Ctrl;
import HwpDoc.paragraph.Ctrl_AutoNumber;
import HwpDoc.paragraph.Ctrl_Character;
import HwpDoc.paragraph.Ctrl_Character.CtrlCharType;
import HwpDoc.paragraph.Ctrl_ColumnDef;
import HwpDoc.paragraph.Ctrl_EqEdit;
import HwpDoc.paragraph.Ctrl_GeneralShape;
import HwpDoc.paragraph.Ctrl_HeadFoot;
import HwpDoc.paragraph.Ctrl_NewNumber;
import HwpDoc.paragraph.Ctrl_Note;
import HwpDoc.paragraph.Ctrl_PageNumPos;
import HwpDoc.paragraph.Ctrl_SectionDef;
import HwpDoc.paragraph.Ctrl_Table;
import HwpDoc.paragraph.ParaText;
public class HwpRecord_ParaText extends HwpRecord {
private static final Logger log = Logger.getLogger(HwpRecord_ParaText.class.getName());
private static final String PATTERN_STRING = "[\\u0000\\u000a\\u000d\\u0018-\\u001f]|[\\u0001\\u0002-\\u0009\\u000b-\\u000c\\u000e-\\u0017].{6}[\\u0001\\u0002-\\u0009\\u000b-\\u000c\\u000e-\\u0017]";
private static final String PATTERN_8BYTES = "[\\u0001\\u0002-\\u0009\\u000b-\\u000c\\u000e-\\u0017].{6}[\\u0001\\u0002-\\u0009\\u000b-\\u000c\\u000e-\\u0017]";
public static Pattern pattern = Pattern.compile(PATTERN_STRING);
HwpRecord_ParaText(int tagNum, int level, int size) {
super(tagNum, level, size);
}
public static List<Ctrl> parse(int tagNum, int level, int size, byte[] buf, int off, int version) throws HwpParseException {
int offset = off;
ArrayList<Ctrl> paras = new ArrayList<>();
String text = new String(buf, offset, size, StandardCharsets.UTF_16LE);
offset += size;
Matcher m = pattern.matcher(text);
log.finer("paraText Length="+ text.length());
int prevIndex = 0;
while(m.find()) {
if (m.start()>prevIndex) {
// write text
int startIndex = prevIndex;
String content = text.substring(startIndex, m.start());
paras.add(new ParaText("____", content, startIndex));
}
if (m.start()+1==m.end()) {
// 문자컨드롤
byte controlByte = m.group().getBytes(StandardCharsets.UTF_16LE)[0];
switch(controlByte) {
case 0x0a: // 10 한 줄 끝 (line break);
paras.add(new Ctrl_Character(" _", CtrlCharType.LINE_BREAK));
break;
case 0x0d: // 13 문단 끝 (para break)
paras.add(new Ctrl_Character(" _", CtrlCharType.PARAGRAPH_BREAK));
break;
case 0x18: // 24 하이픈
paras.add(new Ctrl_Character(" _", CtrlCharType.HARD_HYPHEN));
break;
case 0x1e: // 30 묶음 빈칸
case 0x1f: // 31 고정폭 빈칸
paras.add(new Ctrl_Character(" _", CtrlCharType.HARD_SPACE));
break;
}
} else if (m.start()+8==m.end()) {
// 인라인 컨트롤, 확장컨트롤
byte controlByte = m.group().getBytes(StandardCharsets.UTF_16LE)[0];
String info = new String(m.group().getBytes(StandardCharsets.UTF_16LE), 2, 12, StandardCharsets.US_ASCII).replaceAll("[\\x00-\\x20]+$", "");
switch(controlByte) {
case 0x04: // 필드 끝
break;
case 0x08: // title mark
break;
case 0x09: // 탭
paras.add(new ParaText("____", "\t", 0));
break;
case 0x10: // 머리말/꼬리말
paras.add(new Ctrl_HeadFoot(info));
break;
case 0x12: // 자동번호
paras.add(new Ctrl_AutoNumber(info));
break;
case 0x15: // 페이지 컨트롤(감추기, 새번호로 시작 등)
{
switch(info) {
case "dhgp": // 감추기
break;
case "pngp": // 쪽 번호 위치
paras.add(new Ctrl_PageNumPos(info));
break;
case "onwn": // 새 번호 지정
paras.add(new Ctrl_NewNumber(info));
break;
}
}
break;
case 0x02: // 구역정의/단정의
{
switch(info) {
case "dces":
paras.add(new Ctrl_SectionDef(info));
break;
case "dloc":
paras.add(new Ctrl_ColumnDef(info));
break;
}
}
break;
case 0x03: // 필드 시작 (누름틀,하이퍼링크,블록책갈피,표계산식,문서 요약,사용자 정보,현재 날짜/시간,문서 날짜/시간,파일 경로,상호 참조,메일머지,메모,교정부호,개인정보
case 0x0e: // 예약
case 0x0f: // 숨은 설명
break;
case 0x11: // 각주/미주
paras.add(new Ctrl_Note(info));
break;
case 0x16: // 책갈피/찾아보기 표식
case 0x17: // 덧말/글자 겹침
break;
case 0x0b: // 그리기 개체/표
{
switch(info) {
case " osg":
paras.add(new Ctrl_GeneralShape(info));
break;
case " lbt":
paras.add(new Ctrl_Table(info));
break;
case "deqe":
paras.add(new Ctrl_EqEdit(info));
break;
case "mrof":
break;
}
}
break;
default:
break;
}
}
prevIndex = m.end();
}
if (prevIndex<text.length()) {
// write final text
int startIndex = prevIndex;
String content = text.substring(startIndex);
paras.add(new ParaText("____", content, startIndex));
}
String readable = toReadableString(text);
log.fine(" "+readable);
if (offset-off-size != 0) {
log.fine("[TAG]=" + tagNum + ", size=" + size + ", but currentSize=" + (offset-off));
dump(buf, off, size);
throw new HwpParseException();
}
return paras;
}
public static String getText(int tagNum, int level, int size, byte[] buf, int off, int version) throws HwpParseException {
int offset = off;
String text = new String(buf, offset, size, StandardCharsets.UTF_16LE);
offset += size;
String readable = toReadableString(text);
log.fine(" "+readable);
if (offset-off-size != 0) {
log.fine("[TAG]=" + tagNum + ", size=" + size + ", but currentSize=" + (offset-off));
dump(buf, off, size);
throw new HwpParseException();
}
return text;
}
private static String toReadableString(String text) {
StringBuffer sb = new StringBuffer();
char extendChar = 0;
char inlineChar = 0;
ByteBuffer bb = ByteBuffer.allocate(text.length()*2).order(ByteOrder.LITTLE_ENDIAN);
for(int i=0;i<text.length();i++) {
char c = text.charAt(i);
if (extendChar==0 && inlineChar==0 && c>31) {
sb.append(c);
} else if (extendChar!=0) {
if (c==extendChar) {
sb.append(new String(bb.array(), StandardCharsets.US_ASCII).trim()+"[/EXT("+(int)c+")]");
((Buffer)bb).clear(); // Java9 이후와 호환을 위해
extendChar=0;
} else {
bb.putChar(c);
}
} else if (inlineChar!=0) {
if (c==inlineChar) {
switch(c) {
case 9:
sb.append(HwpRecord.toHexString(bb.array())+"[/INL("+(int)c+")]");
break;
default:
sb.append(new String(bb.array(), StandardCharsets.US_ASCII).trim()+"[/INL("+(int)c+")]");
}
((Buffer)bb).clear(); // Java9 이후와 호환을 위해
inlineChar=0;
} else {
bb.putChar(c);
}
} else if (c <= 31) {
switch(c) {
case 13:
sb.append("[END]");
break;
case 0:
case 10:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
sb.append("[CHAR("+(int)c+")]");
break;
case 1:
case 2:
case 3:
case 11:
case 12:
case 14:
case 15:
case 16:
case 17:
case 18:
case 21:
case 22:
case 23:
sb.append("[EXT("+(int)c+")]");
((Buffer)bb).clear(); // Java9 이후와 호환을 위해
extendChar = c;
break;
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 19:
case 20:
sb.append("[INL("+(int)c+")]");
((Buffer)bb).clear(); // Java9 이후와 호환을 위해
inlineChar = c;
break;
default:
bb.putChar(c);
}
}
}
return sb.toString();
}
}
|