From 6e7ae4a4a3a2b6d367b945d15ddbb065dae41b0d Mon Sep 17 00:00:00 2001 From: kvii <13721952202@163.com> Date: Wed, 8 Oct 2025 10:45:22 +0800 Subject: [PATCH 1/3] feat: Add support for "UniGB-UCS2-H" encoding. #55 --- page.go | 12 ++++++++++++ page_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 page_test.go diff --git a/page.go b/page.go index 31db8ab9..3f691599 100644 --- a/page.go +++ b/page.go @@ -6,11 +6,13 @@ package pdf import ( "bytes" + "encoding/binary" "errors" "fmt" "io" "sort" "strings" + "unicode/utf16" ) // A Page represent a single page in a PDF file. @@ -210,6 +212,8 @@ func (f Font) getEncoder() TextEncoding { return &byteEncoder{&macRomanEncoding} case "Identity-H": return f.charmapEncoding() + case "UniGB-UCS2-H": + return &ucs2Encoder{} default: if DebugOn { println("unknown encoding", enc.Name()) @@ -287,6 +291,14 @@ func (e *nopEncoder) Decode(raw string) (text string) { return raw } +type ucs2Encoder struct{} + +func (e *ucs2Encoder) Decode(raw string) (text string) { + u16s := make([]uint16, len(raw)/2) + _ = binary.Read(strings.NewReader(raw), binary.BigEndian, &u16s) + return string(utf16.Decode(u16s)) +} + type byteEncoder struct { table *[256]rune } diff --git a/page_test.go b/page_test.go new file mode 100644 index 00000000..04da5c65 --- /dev/null +++ b/page_test.go @@ -0,0 +1,37 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package pdf + +import ( + "bytes" + "testing" +) + +func TestUcs2Encoder(t *testing.T) { + // Extract form the raw file. 0x5c is an escape character in pdf. + data := []byte{ + 0x6C, 0x5F, // 江 + 0x82, 0xCF, // 苏 + 0x94, 0xF6, // 银 + 0x88, 0x4C, // 行 + 0x00, 0x5C, 0x28, // ( + 0x4E, 0xA4, // 交 + 0x66, 0x13, // 易 + 0x62, 0x63, // 扣 + 0x6B, 0x3E, // 款 + 0x56, 0xDE, // 回 + 0x53, 0x55, // 单 + 0x00, 0x5C, 0x29, // ) + } + data = bytes.ReplaceAll(data, []byte{0x5C}, []byte{}) + + var e ucs2Encoder + text := e.Decode(string(data)) + + const want = "江苏银行(交易扣款回单)" + if text != want { + t.Errorf("got %q, want %q", text, want) + } +} From 5f555d437550afc222d98e960ea3e32a71da04f3 Mon Sep 17 00:00:00 2001 From: kvii <13721952202@163.com> Date: Sat, 5 Sep 2026 19:00:54 +0800 Subject: [PATCH 2/3] feat: reuse utf16Decode; only test the encoder; remove copyright. --- page.go | 6 +----- page_test.go | 16 +++++++--------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/page.go b/page.go index 3f691599..93cd746e 100644 --- a/page.go +++ b/page.go @@ -6,13 +6,11 @@ package pdf import ( "bytes" - "encoding/binary" "errors" "fmt" "io" "sort" "strings" - "unicode/utf16" ) // A Page represent a single page in a PDF file. @@ -294,9 +292,7 @@ func (e *nopEncoder) Decode(raw string) (text string) { type ucs2Encoder struct{} func (e *ucs2Encoder) Decode(raw string) (text string) { - u16s := make([]uint16, len(raw)/2) - _ = binary.Read(strings.NewReader(raw), binary.BigEndian, &u16s) - return string(utf16.Decode(u16s)) + return utf16Decode(raw) } type byteEncoder struct { diff --git a/page_test.go b/page_test.go index 04da5c65..4e87bcd9 100644 --- a/page_test.go +++ b/page_test.go @@ -1,31 +1,29 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - package pdf import ( - "bytes" "testing" ) func TestUcs2Encoder(t *testing.T) { - // Extract form the raw file. 0x5c is an escape character in pdf. + // raw content in PDF (hexed for pretty) + // 6c5f82cf94f6884c005c284ea4661362636b3e56de5355005c29 + + // raw bytes in showEncodedText (hexed for pretty) + // 6c5f82cf94f6884c00284ea4661362636b3e56de53550029 data := []byte{ 0x6C, 0x5F, // 江 0x82, 0xCF, // 苏 0x94, 0xF6, // 银 0x88, 0x4C, // 行 - 0x00, 0x5C, 0x28, // ( + 0x00, 0x28, // ( 0x4E, 0xA4, // 交 0x66, 0x13, // 易 0x62, 0x63, // 扣 0x6B, 0x3E, // 款 0x56, 0xDE, // 回 0x53, 0x55, // 单 - 0x00, 0x5C, 0x29, // ) + 0x00, 0x29, // ) } - data = bytes.ReplaceAll(data, []byte{0x5C}, []byte{}) var e ucs2Encoder text := e.Decode(string(data)) From 57ce244ec9daa5ad7ca55d2b9bf793e9f5c6c03e Mon Sep 17 00:00:00 2001 From: kvii <13721952202@163.com> Date: Mon, 7 Sep 2026 20:03:27 +0800 Subject: [PATCH 3/3] feat: fix test code --- page_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/page_test.go b/page_test.go index 9a569226..00c03849 100644 --- a/page_test.go +++ b/page_test.go @@ -1,6 +1,8 @@ package pdf import ( + "reflect" + "sort" "testing" ) @@ -31,10 +33,8 @@ func TestUcs2Encoder(t *testing.T) { const want = "江苏银行(交易扣款回单)" if text != want { t.Errorf("got %q, want %q", text, want) - "reflect" - "sort" - "testing" -) + } +} func TestNopEncoder(t *testing.T) { e := &nopEncoder{}