mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00

* Add packed predefined cmaps * Add cmap cid range parsing * Load base cmap for predefined cmaps * Refactor pdfFont to Unicode methods * Preserve CharcodeBytesToUnicode behavior * Add support for CID-keyed Type 0 fonts * Add method documentation for the cmap package * Refactor and document charcode to Unicode conversion code * Add more cmap parsing test cases * Add more method documentation in the cmap package. * Remove unused code from the bcmaps package * Improve cmap test case * Assume identity when encoder is missing on regenerating field appearance * Add missing encoder log message * Add inverse CMap mappings * Add CMap encoder * Address golint notices and small fix in the cmap package * Keep smaller charcodes when generating cmap inverse mappings * Update extractor test case * Keep latest supplement charcodes/CIDs when computing inverse mappings * Fix comment typo
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
/*
|
|
* This file is subject to the terms and conditions defined in
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
*/
|
|
|
|
package cmap
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/unidoc/unipdf/v3/internal/cmap/bcmaps"
|
|
)
|
|
|
|
func TestIsPredefinedCMap(t *testing.T) {
|
|
cmaps := bcmaps.AssetNames()
|
|
for _, cmap := range cmaps {
|
|
require.True(t, IsPredefinedCMap(cmap))
|
|
_, err := bcmaps.Asset(cmap)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func TestLoadPredefinedCMap(t *testing.T) {
|
|
// Load base charcode to CID CMap.
|
|
cmap, err := LoadPredefinedCMap("90ms-RKSJ-H")
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, cmap.name, "90ms-RKSJ-H")
|
|
require.Equal(t, cmap.ctype, 1)
|
|
require.Equal(t, cmap.nbits, 16)
|
|
require.Equal(t, cmap.usecmap, "")
|
|
require.Equal(t, cmap.systemInfo.String(), "Adobe-Japan1-002")
|
|
require.Equal(t, cmap.codespaces, []Codespace{
|
|
Codespace{NumBytes: 1, Low: 0x0, High: 0x80},
|
|
Codespace{NumBytes: 1, Low: 0xa0, High: 0xdf},
|
|
Codespace{NumBytes: 2, Low: 0x8140, High: 0x9ffc},
|
|
Codespace{NumBytes: 2, Low: 0xe040, High: 0xfcfc},
|
|
})
|
|
|
|
expCIDs := map[CharCode]CharCode{
|
|
32: 231,
|
|
125: 324,
|
|
37952: 3287,
|
|
38014: 3349,
|
|
38016: 3350,
|
|
38140: 3474,
|
|
160: 326,
|
|
223: 389,
|
|
64576: 8706,
|
|
64587: 8717,
|
|
34670: 8047,
|
|
33972: 7554,
|
|
}
|
|
|
|
for code, expCID := range expCIDs {
|
|
cid, ok := cmap.CharcodeToCID(code)
|
|
require.True(t, ok)
|
|
require.Equal(t, cid, expCID)
|
|
}
|
|
|
|
// Load charcode to CID CMap using base.
|
|
cmap, err = LoadPredefinedCMap("90ms-RKSJ-V")
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, cmap.name, "90ms-RKSJ-V")
|
|
require.Equal(t, cmap.ctype, 1)
|
|
require.Equal(t, cmap.nbits, 16)
|
|
require.Equal(t, cmap.usecmap, "90ms-RKSJ-H")
|
|
require.Equal(t, cmap.systemInfo.String(), "Adobe-Japan1-002")
|
|
require.Equal(t, cmap.codespaces, []Codespace{
|
|
Codespace{NumBytes: 1, Low: 0x0, High: 0x80},
|
|
Codespace{NumBytes: 1, Low: 0xa0, High: 0xdf},
|
|
Codespace{NumBytes: 2, Low: 0x8140, High: 0x9ffc},
|
|
Codespace{NumBytes: 2, Low: 0xe040, High: 0xfcfc},
|
|
})
|
|
|
|
// Test CID inheritance and CID override.
|
|
expCIDs[34670] = 8349
|
|
expCIDs[33972] = 7554
|
|
expCIDs[33089] = 7887
|
|
expCIDs[33090] = 7888
|
|
|
|
for code, expCID := range expCIDs {
|
|
cid, ok := cmap.CharcodeToCID(code)
|
|
require.True(t, ok)
|
|
require.Equal(t, cid, expCID)
|
|
}
|
|
}
|