From 4573422842d09eef80131933d972e0ba583b9cbf Mon Sep 17 00:00:00 2001 From: hahnlee Date: Sat, 18 May 2024 17:26:27 +0900 Subject: [PATCH] =?UTF-8?q?feat(parser):=20control=20=EC=83=81=EC=86=8D?= =?UTF-8?q?=EA=B4=80=EA=B3=84=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parser/src/models/controls/content.ts | 50 +++++++++++++------ .../parser/src/models/controls/control.ts | 19 +++++++ packages/parser/src/models/controls/index.ts | 31 +----------- .../parser/src/models/controls/section.ts | 10 +++- .../src/models/controls/shapes/content.ts | 2 +- .../src/models/controls/shapes/shape.ts | 10 +++- packages/parser/src/models/controls/table.ts | 18 +++++-- .../parser/src/models/controls/unknown.ts | 15 ++++-- packages/parser/src/models/paragraph.ts | 3 +- packages/parser/tsconfig.json | 3 +- 10 files changed, 103 insertions(+), 58 deletions(-) create mode 100644 packages/parser/src/models/controls/control.ts diff --git a/packages/parser/src/models/controls/content.ts b/packages/parser/src/models/controls/content.ts index 8dfc60a..29a03f8 100644 --- a/packages/parser/src/models/controls/content.ts +++ b/packages/parser/src/models/controls/content.ts @@ -15,38 +15,60 @@ */ import { CommonCtrlID, OtherCtrlID } from '../../constants/ctrl-id.js' +import { SectionTagID } from '../../constants/tag-id.js' import type { ParseOptions } from '../../types/parser.js' +import { ByteReader } from '../../utils/byte-reader.js' import type { PeekableIterator } from '../../utils/generator.js' import type { HWPRecord } from '../record.js' import { HWPVersion } from '../version.js' import { SectionControl } from './section.js' -import { PictureControl } from './shapes/picture.js' import { GenShapeObjectControl } from './shapes/shape.js' import { TableControl } from './table.js' import { UnknownControl } from './unknown.js' -export type ControlContent = - | TableControl - | SectionControl - | GenShapeObjectControl - | PictureControl - | UnknownControl - -export function parseControl( +function mapControl( ctrlId: number, current: HWPRecord, iterator: PeekableIterator, version: HWPVersion, options: ParseOptions, -): ControlContent { +) { switch (ctrlId) { case OtherCtrlID.Section: - return SectionControl.fromRecord(current, iterator, version) + return SectionControl.fromRecord(ctrlId, current, iterator, version) case CommonCtrlID.Table: - return TableControl.fromRecord(current, iterator, version, options) + return TableControl.fromRecord( + ctrlId, + current, + iterator, + version, + options, + ) case CommonCtrlID.GenShapeObject: - return GenShapeObjectControl.fromRecord(current, iterator, version, options) + return GenShapeObjectControl.fromRecord( + ctrlId, + current, + iterator, + version, + options, + ) default: - return UnknownControl.fromRecord(current, iterator) + return UnknownControl.fromRecord(ctrlId, current, iterator) } } + +export function parseControl( + iterator: PeekableIterator, + version: HWPVersion, + options: ParseOptions, +) { + const current = iterator.next() + if (current.id !== SectionTagID.HWPTAG_CTRL_HEADER) { + throw new Error('Control: Record has wrong ID') + } + + const reader = new ByteReader(current.data) + const id = reader.readUInt32() + + return mapControl(id, current, iterator, version, options) +} diff --git a/packages/parser/src/models/controls/control.ts b/packages/parser/src/models/controls/control.ts new file mode 100644 index 0000000..e61d0f1 --- /dev/null +++ b/packages/parser/src/models/controls/control.ts @@ -0,0 +1,19 @@ +/** + * Copyright Han Lee and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export class Control { + constructor(public id: number) {} +} diff --git a/packages/parser/src/models/controls/index.ts b/packages/parser/src/models/controls/index.ts index 7e24525..6c48ba6 100644 --- a/packages/parser/src/models/controls/index.ts +++ b/packages/parser/src/models/controls/index.ts @@ -14,32 +14,5 @@ * limitations under the License. */ -import { ByteReader } from '../../utils/byte-reader.js' -import type { PeekableIterator } from '../../utils/generator.js' -import type { HWPRecord } from '../record.js' -import { SectionTagID } from '../../constants/tag-id.js' -import { type ControlContent, parseControl } from './content.js' -import { HWPVersion } from '../version.js' -import type { ParseOptions } from '../../types/parser.js' - -export class Control { - constructor(public id: number, public content: ControlContent) {} - - static fromRecords( - iterator: PeekableIterator, - version: HWPVersion, - options: ParseOptions, - ) { - const current = iterator.next() - if (current.id !== SectionTagID.HWPTAG_CTRL_HEADER) { - throw new Error('Control: Record has wrong ID') - } - - const reader = new ByteReader(current.data) - const id = reader.readUInt32() - - const content = parseControl(id, current, iterator, version, options) - - return new Control(id, content) - } -} +export * from './control.js' +export * from './section.js' diff --git a/packages/parser/src/models/controls/section.ts b/packages/parser/src/models/controls/section.ts index 63145fe..4d9e1a7 100644 --- a/packages/parser/src/models/controls/section.ts +++ b/packages/parser/src/models/controls/section.ts @@ -21,10 +21,12 @@ import type { PeekableIterator } from '../../utils/generator.js' import { Border } from '../doc-info/border-fill.js' import type { HWPRecord } from '../record.js' import { HWPVersion } from '../version.js' +import { Control } from './control.js' import { PageDefinition } from './page-definition.js' -export class SectionControl { +export class SectionControl extends Control { constructor( + public id: number, /** 컨트롤 ID */ public ctrlId: number, /** 머리말을 감출지 여부 */ @@ -81,9 +83,12 @@ export class SectionControl { /** 미주 모양 정보 */ public endnoteShape: FootnoteEndnoteShape, public unknown: ArrayBuffer, - ) {} + ) { + super(id) + } static fromRecord( + id: number, record: HWPRecord, iterator: PeekableIterator, version: HWPVersion, @@ -146,6 +151,7 @@ export class SectionControl { // TODO: (@hahnlee) 바탕쪽 정보 관련된 파싱 추가하기 return new SectionControl( + id, ctrlId, hideHeader, hideFooter, diff --git a/packages/parser/src/models/controls/shapes/content.ts b/packages/parser/src/models/controls/shapes/content.ts index 24f7161..9b28735 100644 --- a/packages/parser/src/models/controls/shapes/content.ts +++ b/packages/parser/src/models/controls/shapes/content.ts @@ -32,6 +32,6 @@ export function parseContent( case CommonCtrlID.Picture: return PictureRecord.fromRecords(iterator) default: - return UnknownControl.fromRecord(current, iterator) + return UnknownControl.fromRecord(elementProperties.ctrlId, current, iterator) } } diff --git a/packages/parser/src/models/controls/shapes/shape.ts b/packages/parser/src/models/controls/shapes/shape.ts index 49297d9..2ea6cc9 100644 --- a/packages/parser/src/models/controls/shapes/shape.ts +++ b/packages/parser/src/models/controls/shapes/shape.ts @@ -20,6 +20,7 @@ import type { PeekableIterator } from '../../../utils/generator.js' import type { HWPRecord } from '../../record.js' import type { HWPVersion } from '../../version.js' import { CommonProperties } from '../common-properties.js' +import { Control } from '../control.js' import { DrawText } from '../draw-text.js' import { ElementProperties } from '../element-properties.js' import { type ShapeObjectContent, parseContent } from './content.js' @@ -27,8 +28,9 @@ import { type ShapeObjectContent, parseContent } from './content.js' /** * 그리기 객체 */ -export class GenShapeObjectControl { +export class GenShapeObjectControl extends Control { constructor( + public id: number, /** 개체 공통 속성 */ public commonProperties: CommonProperties, /** 개체 요소 속성 */ @@ -37,9 +39,12 @@ export class GenShapeObjectControl { public drawText: DrawText | null, /** 컨텐츠 */ public content: ShapeObjectContent, - ) {} + ) { + super(id) + } static fromRecord( + id: number, record: HWPRecord, iterator: PeekableIterator, version: HWPVersion, @@ -62,6 +67,7 @@ export class GenShapeObjectControl { const content = parseContent(elementProperties, record, iterator) return new GenShapeObjectControl( + id, commonProperties, elementProperties, drawText, diff --git a/packages/parser/src/models/controls/table.ts b/packages/parser/src/models/controls/table.ts index 835da37..fd072ee 100644 --- a/packages/parser/src/models/controls/table.ts +++ b/packages/parser/src/models/controls/table.ts @@ -23,16 +23,21 @@ import { CommonProperties } from './common-properties.js' import { SectionTagID } from '../../constants/tag-id.js' import { ParagraphList } from './paragraph-list.js' import type { ParseOptions } from '../../types/parser.js' +import { Control } from './control.js' -export class TableControl { +export class TableControl extends Control { constructor( + public id: number, /** 개체 공통 속성 */ public commonProperties: CommonProperties, public record: TableRecord, public cells: Cell[], - ) {} + ) { + super(id) + } static fromRecord( + id: number, record: HWPRecord, iterator: PeekableIterator, version: HWPVersion, @@ -60,7 +65,7 @@ export class TableControl { cells.push(Cell.fromRecords(iterator, version, options)) } - return new TableControl(commonProperties, tableRecord, cells) + return new TableControl(id, commonProperties, tableRecord, cells) } } @@ -220,7 +225,12 @@ export class Cell { } const reader = new ByteReader(record.data) - const paragraphs = ParagraphList.fromReader(reader, iterator, version, options) + const paragraphs = ParagraphList.fromReader( + reader, + iterator, + version, + options, + ) const column = reader.readUInt16() const row = reader.readUInt16() diff --git a/packages/parser/src/models/controls/unknown.ts b/packages/parser/src/models/controls/unknown.ts index f7be521..758b82e 100644 --- a/packages/parser/src/models/controls/unknown.ts +++ b/packages/parser/src/models/controls/unknown.ts @@ -17,11 +17,18 @@ import type { PeekableIterator } from '../../utils/generator.js' import { collectChildren } from '../../utils/record.js' import type { HWPRecord } from '../record.js' +import { Control } from './control.js' -export class UnknownControl { - constructor(public records: HWPRecord[]) {} +export class UnknownControl extends Control { + constructor(public id: number, public records: HWPRecord[]) { + super(id) + } - static fromRecord(current: HWPRecord, iterator: PeekableIterator) { - return new UnknownControl(collectChildren(iterator, current.level)) + static fromRecord( + id: number, + current: HWPRecord, + iterator: PeekableIterator, + ) { + return new UnknownControl(id, collectChildren(iterator, current.level)) } } diff --git a/packages/parser/src/models/paragraph.ts b/packages/parser/src/models/paragraph.ts index 892e826..5b66997 100644 --- a/packages/parser/src/models/paragraph.ts +++ b/packages/parser/src/models/paragraph.ts @@ -27,6 +27,7 @@ import { collectChildren } from '../utils/record.js' import { RangeTag } from './range-tag.js' import { CharShape } from './char-shape.js' import type { ParseOptions } from '../types/parser.js' +import { parseControl } from './controls/content.js' export class Paragraph { constructor( @@ -100,7 +101,7 @@ export class Paragraph { const controls: Control[] = chars .extendedControls() - .map(() => Control.fromRecords(iterator, version, options)) + .map(() => parseControl(iterator, version, options)) const unknown = collectChildren(iterator, current.level) diff --git a/packages/parser/tsconfig.json b/packages/parser/tsconfig.json index 8410a0f..07db92e 100644 --- a/packages/parser/tsconfig.json +++ b/packages/parser/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "NodeNext", "lib": ["esnext"], - "strict": true + "strict": true, + "skipLibCheck": true } }