Skip to content

Commit

Permalink
fix: shape 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnlee committed May 18, 2024
1 parent 4573422 commit f467b66
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
56 changes: 19 additions & 37 deletions packages/parser/src/models/char-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,32 @@ import type { HWPRecord } from './record.js'
import { SectionTagID } from '../constants/tag-id.js'
import { ByteReader } from '../utils/byte-reader.js'

export class CharList {
constructor(private chars: HWPChar[]) {}

static empty() {
return new CharList([new CharControl(CharControls.ParaBreak)])
export function parseChars(record: HWPRecord, count: number) {
if (record.id !== SectionTagID.HWPTAG_PARA_TEXT) {
throw new Error('BodyText: CharList: Record has wrong ID')
}

static fromRecord(record: HWPRecord, count: number) {
if (record.id !== SectionTagID.HWPTAG_PARA_TEXT) {
throw new Error('BodyText: CharList: Record has wrong ID')
}

const reader = new ByteReader(record.data)

const chars: HWPChar[] = []
let i = 0
while (i < count) {
const char = readChar(reader)
i += char.bytes
chars.push(char)
}
const reader = new ByteReader(record.data)

if (!reader.isEOF()) {
throw new Error('BodyText: CharList: Reader is not EOF')
}

return new CharList(chars)
const chars: HWPChar[] = []
let i = 0
while (i < count) {
const char = readChar(reader)
i += char.bytes
chars.push(char)
}

*[Symbol.iterator]() {
for (const char of this.chars) {
yield char
}
if (!reader.isEOF()) {
throw new Error('BodyText: CharList: Reader is not EOF')
}

get length() {
return this.chars.length
}
return chars
}

extendedControls() {
return this.chars.filter((char) => char instanceof ExtendedControl)
}
export function createEmptyCharList() {
return [new CharControl(CharControls.ParaBreak)]
}

toString() {
return this.chars.map((char) => char.toString()).join('')
}
export function extendedControls(chars: HWPChar[]) {
return chars.filter((char) => char instanceof ExtendedControl)
}
41 changes: 34 additions & 7 deletions packages/parser/src/models/paragraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ import { HWPRecord } from './record.js'
import { HWPVersion } from './version.js'
import { SectionTagID } from '../constants/tag-id.js'
import { ByteReader } from '../utils/byte-reader.js'
import { CharList } from './char-list.js'
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'
import type { HWPChar } from './char.js'
import {
createEmptyCharList,
extendedControls,
parseChars,
} from './char-list.js'

export class Paragraph {
constructor(
public header: ParagraphHeader,
public chars: CharList,
public chars: HWPChar[],
public charShapes: CharShape[],
public lineSegments: LineSegment[],
public rangeTags: RangeTag[],
Expand All @@ -50,8 +55,8 @@ export class Paragraph {
// NOTE: (@hahnlee) 문서와 달리 header.chars가 0보다 커도 없을 수 있다.
const chars =
iterator.peek().id === SectionTagID.HWPTAG_PARA_TEXT
? CharList.fromRecord(iterator.next(), header.chars)
: CharList.empty()
? parseChars(iterator.next(), header.chars)
: createEmptyCharList()

const charShapes: CharShape[] = []
if (header.charShapes > 0) {
Expand Down Expand Up @@ -99,9 +104,9 @@ export class Paragraph {
}
}

const controls: Control[] = chars
.extendedControls()
.map(() => parseControl(iterator, version, options))
const controls = extendedControls(chars).map(() =>
parseControl(iterator, version, options),
)

const unknown = collectChildren(iterator, current.level)

Expand All @@ -119,6 +124,28 @@ export class Paragraph {
)
}

*shapes() {
let index = 0
for (let i = 0; i < this.charShapes.length - 1; i++) {
const start = this.charShapes[i].startPosition
const end = this.charShapes[i + 1].startPosition
const chars = []
let counts = end - start
while (counts > 0) {
const char = this.chars[index]
chars.push(char)
counts -= char.bytes
index++
}
yield [this.charShapes[i], chars] as const
}

yield [
this.charShapes[this.charShapes.length - 1],
this.chars.slice(index),
] as const
}

toString() {
return this.chars.toString()
}
Expand Down

0 comments on commit f467b66

Please sign in to comment.