Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to record tree #11

Merged
merged 11 commits into from
Oct 10, 2020
11 changes: 9 additions & 2 deletions Sources/HwpKit/HwpFile.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import Foundation
import OLEKit

public class HwpFile {
public struct HwpFile: HwpPrimitive {
public let fileHeader: HwpFileHeader
public let docInfo: HwpDocInfo
public let previewText: HwpPreviewText
public let sectionArray: [HwpSection]
public let previewText: HwpPreviewText

public init() {
fileHeader = HwpFileHeader()
docInfo = HwpDocInfo()
previewText = HwpPreviewText()
sectionArray = [HwpSection]()
}

public init(filePath: String) throws {
let ole: OLEFile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

/**
호환 문서

Tag ID : HWPTAG_COMPATIBLE_DOCUMENT
*/
public struct HwpCompatibleDocument: HwpFromRecord {
/**대상 프로그램*/
public let targetDocument: UInt32
public var layoutCompatibility: HwpLayoutCompatibility?

init() {
targetDocument = 0
}

init(_ record: HwpRecord) throws {
var reader = DataReader(record.payload)
defer {
precondition(reader.isEOF())
}
targetDocument = reader.read(UInt32.self)

if let layoutCompatibility = record.children
.first(where: {$0.tagId == HwpDocInfoTag.layoutCompatibility}) {
self.layoutCompatibility = try HwpLayoutCompatibility(layoutCompatibility.payload)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

/**
레이아웃 호환성

Tag ID : HWPTAG_LAYOUT_COMPATIBILITY
*/
public struct HwpLayoutCompatibility: HwpFromData {
/**글자 단위 서식*/
public let char: UInt32
/**문단 단위 서식*/
public let paragraph: UInt32
/**구역 단위 서식*/
public let section: UInt32
/**개체 단위 서식*/
public let object: UInt32
/**필드 단위 서식*/
public let field: UInt32

init() {
char = 0
paragraph = 0
section = 0
object = 0
field = 0
}

init(_ data: Data) throws {
var reader = DataReader(data)
defer {
precondition(reader.isEOF())
}
char = reader.read(UInt32.self)
paragraph = reader.read(UInt32.self)
section = reader.read(UInt32.self)
object = reader.read(UInt32.self)
field = reader.read(UInt32.self)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import Foundation

public struct HwpCaratLocation: HwpData {
public let listId: UInt32
public let paragraphId: UInt32
public let charIndex: UInt32
/**
문서 내 캐럿의 위치 정보
*/
public struct HwpCaratLocation: HwpFromData {
/**리스트 아이디*/
public var listId: UInt32
/**문단 아이디*/
public var paragraphId: UInt32
/**문단 내에서의 글자 단위 위치*/
public var charIndex: UInt32

init() {
listId = 0
paragraphId = 0
charIndex = 0
}

init(_ data: Data) {
var reader = DataReader(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import Foundation

public struct HwpDocumentProperties: HwpData {
public let sectionSize: UInt16
/**
문서 속성

Tag ID : HWPTAG_DOCUMENT_PROPERTIES
*/
public struct HwpDocumentProperties: HwpFromData {
/**구역 개수*/
public var sectionSize: UInt16
public let startingIndex: HwpStartingIndex
public let caratLocation: HwpCaratLocation

init(_ data: Data) {
init() {
sectionSize = 0
startingIndex = HwpStartingIndex()
caratLocation = HwpCaratLocation()
}

init(_ data: Data) {
var reader = DataReader(data)
sectionSize = reader.read(UInt16.self)
startingIndex = HwpStartingIndex(reader.readBytes(12))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import Foundation

public struct HwpStartingIndex: HwpData {
public struct HwpStartingIndex: HwpFromData {
public let page: UInt16
public let footnote: UInt16
public let endnote: UInt16
public let picture: UInt16
public let table: UInt16
public let equation: UInt16

init() {
page = 0
footnote = 0
endnote = 0
picture = 0
table = 0
equation = 0
}

init(_ data: Data) {
var reader = DataReader(data)
page = reader.read(UInt16.self)
Expand Down
123 changes: 123 additions & 0 deletions Sources/HwpKit/Model/DocInfo/HwpIdMappings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import Foundation

/**
아이디 매핑 헤더

Tag ID : HWPTAG_ID_MAPPINGS
*/
public struct HwpIdMappings: HwpFromRecordWithVersion {
/**바이너리 데이터*/
private let binaryDataCount: Int32
/**영어 글꼴*/
private let englishFaceCount: Int32
/**한자 글꼴*/
private let chineseFaceCount: Int32
/**일어 글꼴*/
private let japaneseFaceCount: Int32
/**기타 글꼴*/
private let etcFaceCount: Int32
/**기호 글꼴*/
private let symbolFaceCount: Int32
/**사용자 글꼴*/
private let userFaceCount: Int32
/**테두리/배경*/
private let borderFillCount: Int32
/**글자 모양*/
private let charShapeCount: Int32
/**탭 정의*/
private let tabDefCount: Int32
/**문단 번호*/
private let paraNumberingCount: Int32
/**글머리표*/
private let paraHeadCount: Int32
/**문단 모양*/
private let paraShapeCount: Int32
/**스타일*/
private let styleCount: Int32
/**메모 모양 (5.0.2.1 이상)*/
private var memoShapeCount: Int32?
/**변경추적 (5.0.3.2 이상)*/
private var changeTraceCount: Int32?
/**변경추적 사용자 (5.0.3.2 이상)*/
private var changeTraceUserCount: Int32?

public var binDataArray: [HwpBinData]
public var faceNameArray: [HwpFaceName]
public var borderFillArray: [HwpBorderFill]
public var charShapeArray: [HwpCharShape]
// TODO HWPTAG_TAB_DEF
// TODO HWPTAG_NUMBERING
// TODO HWPTAG_BULLET
public var paraShapeArray: [HwpParaShape]

init() {
binaryDataCount = 0
englishFaceCount = 0
chineseFaceCount = 0
japaneseFaceCount = 0
etcFaceCount = 0
symbolFaceCount = 0
userFaceCount = 0
borderFillCount = 0
charShapeCount = 0
tabDefCount = 0
paraNumberingCount = 0
paraHeadCount = 0
paraShapeCount = 0
styleCount = 0

binDataArray = [HwpBinData]()
faceNameArray = [HwpFaceName]()
borderFillArray = [HwpBorderFill]()
charShapeArray = [HwpCharShape]()
paraShapeArray = [HwpParaShape]()
}

init(_ record: HwpRecord, _ version: HwpVersion) throws {
var reader = DataReader(record.payload)
defer {
// precondition(reader.isEOF())
}
binaryDataCount = reader.read(Int32.self)
englishFaceCount = reader.read(Int32.self)
chineseFaceCount = reader.read(Int32.self)
japaneseFaceCount = reader.read(Int32.self)
etcFaceCount = reader.read(Int32.self)
symbolFaceCount = reader.read(Int32.self)
userFaceCount = reader.read(Int32.self)
borderFillCount = reader.read(Int32.self)
charShapeCount = reader.read(Int32.self)
tabDefCount = reader.read(Int32.self)
paraNumberingCount = reader.read(Int32.self)
paraHeadCount = reader.read(Int32.self)
paraShapeCount = reader.read(Int32.self)
styleCount = reader.read(Int32.self)
if version >= HwpVersion(5, 0, 2, 1) {
sboh1214 marked this conversation as resolved.
Show resolved Hide resolved
memoShapeCount = reader.read(Int32.self)
}
if version >= HwpVersion(5, 0, 3, 2) {
sboh1214 marked this conversation as resolved.
Show resolved Hide resolved
changeTraceCount = reader.read(Int32.self)
changeTraceUserCount = reader.read(Int32.self)
}

binDataArray = try record.children
sboh1214 marked this conversation as resolved.
Show resolved Hide resolved
.filter {$0.tagId == HwpDocInfoTag.binData}
.map {try HwpBinData($0.payload)}

faceNameArray = try record.children
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 7 locations. Consider refactoring.

.filter {$0.tagId == HwpDocInfoTag.faceName}
.map {try HwpFaceName($0.payload)}

borderFillArray = try record.children
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 7 locations. Consider refactoring.

.filter {$0.tagId == HwpDocInfoTag.borderFill}
.map {try HwpBorderFill($0.payload)}

charShapeArray = try record.children
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

.filter {$0.tagId == HwpDocInfoTag.charShape}
.map {try HwpCharShape($0.payload, version)}

paraShapeArray = try record.children
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

.filter {$0.tagId == HwpDocInfoTag.paraShape}
.map {try HwpParaShape($0.payload, version)}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation

Tag ID : HWPTAG_BIN_DATA
*/
public struct HwpBinData: HwpData {
public struct HwpBinData: HwpFromData {
/**속성 -> type*/
public let type: HwpBinDataType

Expand All @@ -32,6 +32,17 @@ public struct HwpBinData: HwpData {
*/
public var extensionName: [WCHAR]?

init() {
type = .embedding
absolutePathLength = nil
absolutePath = nil
relativePathLength = nil
relativePath = nil
streamId = nil
extensionLength = nil
extensionName = nil
}

init(_ data: Data) throws {
var reader = DataReader(data)
let property = reader.read(UInt16.self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Foundation

public struct HwpBorderFill: HwpData {
/**
테두리/배경

Tag ID : HWPTAG_BORDER_FILL
*/
public struct HwpBorderFill: HwpFromData {
public let property: UInt16
public let borderType: [UInt8]
public let borderThickness: [UInt8]
Expand All @@ -10,6 +15,17 @@ public struct HwpBorderFill: HwpData {
public let diagonalColor: HwpColor
public let fillInfo: [BYTE]

init() {
property = 0
borderType = [0, 0, 0, 0]
borderThickness = [0, 0, 0, 0]
borderColor = Array(repeating: HwpColor(), count: 4)
diagonalType = 0
diagonalThickness = 0
diagonalColor = HwpColor()
fillInfo = [BYTE]()
}

init(_ data: Data) throws {
var reader = DataReader(data)
property = reader.read(UInt16.self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct HwpCharShape: HwpDataWithVersion {
public struct HwpCharShape: HwpFromDataWithVersion {
/**언어별 글꼴 ID(FaceID) 참조 값*/
public let faceId: [WORD]
/**언어별 장평, 50%~200%(*/
Expand Down Expand Up @@ -32,6 +32,24 @@ public struct HwpCharShape: HwpDataWithVersion {
/**취소선 색 (5.0.3.0 이상)*/
public var strikethroughColor: HwpColor?

init() {
faceId = [0]
faceScaleX = [0]
faceSpacing = [0]
faceRelativeSize = [0]
faceLocation = [0]
baseSize = 0
property = 0
shadowInterval = 0
shadowInterval2 = 0
faceColor = HwpColor()
underlineColor = HwpColor()
shadeColor = HwpColor()
shadowColor = HwpColor()
borderFillId = nil
strikethroughColor = nil
}

init(_ data: Data, _ version: HwpVersion) throws {
var reader = DataReader(data)
defer {
Expand Down
Loading