hwp.js
repository·main·Indexed 23 days ago
https://github.com/hahnlee/hwp.jsAn open-source library for viewing and parsing Hangul Word Processor (.hwp) files using web technologies. Built based on the official Hancom HWP file structure 5.0 documentation, it provides tools to parse HWP documents into structured programmatic models, including support for the FileHeader, document metadata, tables, shapes, and font classification via the Panose model.
What's inside hwp.js
- hwp.js is an open-source library powered by web technology designed to view and parse Hangul Word Processor (.hwp) files. It is developed based on the official Hancom HWP file structure 5.0 documentation.
Interpret HWP file versions
mainThe file version is a
DWORDin the format0xMMnnPPrr(e.g.,5.0.3.0). The components determine compatibility:MM: Major format version. If this differs, the file is incompatible with older versions.nn: Major structure version. If this differs, the file is incompatible with older versions.PP: Record version. If this differs, the file is compatible with older versions (indicates new or incompatible records added).rr: Revision version. If this differs, the file is compatible with older versions (indicates information added to records).
Calculate Dimensions using HWPUNIT and SHWPUNIT
mainTo represent sizes for characters, images, or page margins, HWP 5.0 uses
HWPUNITandSHWPUNIT. These units are based on 1/7200 of an inch to ensure consistent sizing regardless of device resolution.Conversion Example: To represent an image that is 2 inches wide by 1 inch high:
- Width: $2 \text{ inches} \times 7200 = 14400$
HWPUNIT - Height: $1 \text{ inch} \times 7200 = 7200$
HWPUNIT
- Width: $2 \text{ inches} \times 7200 = 14400$
Understand HWP 5.0 Data Types and Endianness
mainIn HWP 5.0 files, information is stored using specific data types. All multi-byte data types use Little-endian byte ordering, meaning the least significant byte is stored first. One byte is defined as 8 bits.
When data is represented as an array, it is denoted as
DataType array[count]. For example, awordarray with 10 elements is written asword array[10].Install hwp.js via npm or yarn
mainYou can install the
hwp.jslibrary using either npm or yarn to use its HWP (Hangul Word Processor) viewer and parser capabilities in your web-based projects.npm install hwp.js yarn add hwp.jsUnderstand the HWP 5.0 FileHeader structure
mainThe
FileHeaderstream is used to identify a file as an HWP document. It is a fixed-length structure of 256 bytes containing the signature, versioning, attributes, and encryption information.| 자료형 | 길이(바이트) | 설명 | | --------------- |:---------:| -- | | BYTE array[32] | 32 | signature | | DWORD | 4 | 파일 버전 | | DWORD | 4 | 속성1 | | DWORD | 4 | 속성2 | | DWORD | 4 | EncryptVersion | | BYTE | 1 | 공공누리(KOGL) 라이선스 지원 국가 | | BYTE array[207] | 207 | 예약 | | 전체 길이 | 256 |Identify HWP files using the signature
mainTo verify if a file is a valid HWP document, check if the first 32 bytes match the signature string:HWP Document File.HWP Document FileReference HWP 5.0 Data Type Specifications
mainThe following table defines the data types used in HWP 5.0 files. Note that
WCHARis used for Unicode-based characters (including Hangul, English, and Hanja), and all characters occupy exactly 2 bytes.| 자료형 | 길이 | 부호 | 설명 | | ------------- |:---:|:---:| --- | | BYTE | 1 | | 부호 없는 한 바이트(0~255) | | WORD | 2 | | 16비트 컴파일러에서 `unsigned int`에 해당 | | DWORD | 4 | | 16비트 컴파일러에서 `unsigned long`에 해당 | | WCHAR | 2 | | 한글의 기본 코드로 유니코드 기반 문자 | | HWPUNIT | 4 | | 1/7200인치로 표현된 한글 내부 단위 | | SHWPUNIT | 4 | √ | 1/7200인치로 표현된 한글 내부 단위 | | UINT8 | 1 | | `unsigned __int8` 에 해당 | | UINT16 | 2 | | `unsigned __int16` 에 해당 | | UINT32(=UINT) | 4 | | `unsigned __int32` 에 해당 | | INT8 | 1 | √ | `signed __int8` 에 해당 | | INT16 | 2 | √ | `signed __int16` 에 해당 | | INT32 | 4 | √ | `signed __int32` 에 해당 | | HWPUNIT16 | 2 | √ | INT16 과 같다. | | COLORREF | 4 | | RGB값(0x00bbggrr)을 십진수로 표시 (rr : red 1 byte, gg : green 1 byte, bb : blue 1 byte) | | BYTE stream | | | 일련의 BYTE로 구성됨. 본문 내에서 다른 구조를 참조할 경우에 사용됨 |Identify encryption versions (EncryptVersion)
mainThe
EncryptVersionfield indicates the encryption method used by the document:0: None1: HWP version 2.5 or lower2: HWP 3.0 Enhanced3: HWP 3.0 Old4: HWP 7.0 or later
Parse Attribute 2 (속성2) bitmask
mainThe
속성2field is aDWORDbitmask for licensing and reproduction constraints:Bit Description 0 CCL, KOGL (공공누리) license information 1 Reproduction restriction status 2 Permission to reproduce under same conditions (ignored if bit 1 is set) 3-31 Reserved Parse Attribute 1 (속성1) bitmask
mainThe
속성1field is aDWORDbitmask used to identify various document properties. Use the following bit mapping to parse the flags:Bit Description 0 Compression status 1 Encryption status 2 Distribution document status 3 Script storage status 4 DRM security document status 5 XMLTemplate storage existence 6 Document history management existence 7 Electronic signature information existence 8 Public certificate encryption 9 Electronic signature preliminary storage 10 Public certificate DRM security document 11 CCL document status 12 Mobile optimization status 13 Personal information security document status 14 Change tracking document status 15 KOGL (공공누리) copyright document 16 Video control inclusion 17 Table of contents field control inclusion 18-31 Reserved Identify KOGL supported countries
mainThe KOGL (공공누리) license support country is stored as a single
BYTE:6: KOR (Korea)15: US (United States)