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

Update to pdfium 6281 and implement FPDFText_LoadCidType2Font #138

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go: [ "1.20", "1.21", "1.22" ]
pdfium: [ "4849", "6248" ]
pdfium: [ "4849", "6281" ]
env:
PDFIUM_EXPERIMENTAL_VERSION: "6248"
PDFIUM_EXPERIMENTAL_VERSION: "6281"
PDFIUM_EXPERIMENTAL_GO_VERSION: "1.22"
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pdfium-windows.pc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ includedir=D:/opt/pdfium/include

Name: PDFium
Description: PDFium
Version: 6248
Version: 6281
Requires:

Libs: -L${libdir} -lpdfium
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pdfium.pc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ includedir=/opt/pdfium/include

Name: PDFium
Description: PDFium
Version: 6248
Version: 6281
Requires:

Libs: -L${libdir} -lpdfium
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ includedir={path}/include

Name: PDFium
Description: PDFium
Version: 6248
Version: 6281
Requires:

Libs: -L${libdir} -lpdfium
Expand Down
29 changes: 29 additions & 0 deletions internal/commons/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/implementation_cgo/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,9 @@ func (p *PdfiumImplementation) FPDFText_SetCharcodes(request *requests.FPDFText_
}

// FPDFText_LoadFont returns a font object loaded from a stream of data. The font is loaded
// into the document.
// The loaded font can be closed using FPDFFont_Close.
// into the document. Various font data structures, such as the ToUnicode data, are auto-generated based
// on the inputs.
// The loaded font can be closed using FPDFFont_Close().
func (p *PdfiumImplementation) FPDFText_LoadFont(request *requests.FPDFText_LoadFont) (*responses.FPDFText_LoadFont, error) {
p.Lock()
defer p.Unlock()
Expand Down
31 changes: 30 additions & 1 deletion internal/implementation_cgo/fpdf_edit_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@
// FPDFText_LoadStandardFont loads one of the standard 14 fonts per PDF spec 1.7 page 416. The preferred
// way of using font style is using a dash to separate the name from the style,
// for example 'Helvetica-BoldItalic'.
// The loaded font can be closed using FPDFFont_Close.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
func (p *PdfiumImplementation) FPDFText_LoadStandardFont(request *requests.FPDFText_LoadStandardFont) (*responses.FPDFText_LoadStandardFont, error) {
p.Lock()
Expand All @@ -765,6 +765,35 @@
}, nil
}

// FPDFText_LoadCidType2Font returns a font object loaded from a stream of data for a type 2 CID font.
// The font is loaded into the document. Unlike FPDFText_LoadFont(), the ToUnicode data and the CIDToGIDMap
// data are caller provided, instead of auto-generated.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
func (p *PdfiumImplementation) FPDFText_LoadCidType2Font(request *requests.FPDFText_LoadCidType2Font) (*responses.FPDFText_LoadCidType2Font, error) {
p.Lock()
defer p.Unlock()

documentHandle, err := p.getDocumentHandle(request.Document)
if err != nil {
return nil, err
}

toUnicodeCmap := C.CString(request.ToUnicodeCmap)
defer C.free(unsafe.Pointer(toUnicodeCmap))

font := C.FPDFText_LoadCidType2Font(documentHandle.handle, (*C.uchar)(unsafe.Pointer(&request.FontData[0])), C.uint32_t(len(request.FontData)), toUnicodeCmap, (*C.uchar)(unsafe.Pointer(&request.CIDToGIDMapData[0])), C.uint32_t(len(request.CIDToGIDMapData)))
if font == nil {
return nil, errors.New("could not load CID Type2 font")
}

Check warning on line 788 in internal/implementation_cgo/fpdf_edit_experimental.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_cgo/fpdf_edit_experimental.go#L787-L788

Added lines #L787 - L788 were not covered by tests

fontHandle := p.registerFont(font)

return &responses.FPDFText_LoadCidType2Font{
Font: fontHandle.nativeRef,
}, nil
}

// FPDFTextObj_SetTextRenderMode sets the text rendering mode of a text object.
// Experimental API.
func (p *PdfiumImplementation) FPDFTextObj_SetTextRenderMode(request *requests.FPDFTextObj_SetTextRenderMode) (*responses.FPDFTextObj_SetTextRenderMode, error) {
Expand Down
11 changes: 10 additions & 1 deletion internal/implementation_cgo/fpdf_edit_no_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,21 @@ func (p *PdfiumImplementation) FPDFPageObj_SetDashArray(request *requests.FPDFPa
// FPDFText_LoadStandardFont loads one of the standard 14 fonts per PDF spec 1.7 page 416. The preferred
// way of using font style is using a dash to separate the name from the style,
// for example 'Helvetica-BoldItalic'.
// The loaded font can be closed using FPDFFont_Close.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
func (p *PdfiumImplementation) FPDFText_LoadStandardFont(request *requests.FPDFText_LoadStandardFont) (*responses.FPDFText_LoadStandardFont, error) {
return nil, pdfium_errors.ErrExperimentalUnsupported
}

// FPDFText_LoadCidType2Font returns a font object loaded from a stream of data for a type 2 CID font.
// The font is loaded into the document. Unlike FPDFText_LoadFont(), the ToUnicode data and the CIDToGIDMap
// data are caller provided, instead of auto-generated.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
func (p *PdfiumImplementation) FPDFText_LoadCidType2Font(request *requests.FPDFText_LoadCidType2Font) (*responses.FPDFText_LoadCidType2Font, error) {
return nil, pdfium_errors.ErrExperimentalUnsupported
}

// FPDFTextObj_SetTextRenderMode sets the text rendering mode of a text object.
// Experimental API.
func (p *PdfiumImplementation) FPDFTextObj_SetTextRenderMode(request *requests.FPDFTextObj_SetTextRenderMode) (*responses.FPDFTextObj_SetTextRenderMode, error) {
Expand Down
59 changes: 57 additions & 2 deletions internal/implementation_webassembly/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -1783,8 +1783,9 @@
}

// FPDFText_LoadFont returns a font object loaded from a stream of data. The font is loaded
// into the document.
// The loaded font can be closed using FPDFFont_Close.
// into the document. Various font data structures, such as the ToUnicode data, are auto-generated based
// on the inputs.
// The loaded font can be closed using FPDFFont_Close().
func (p *PdfiumImplementation) FPDFText_LoadFont(request *requests.FPDFText_LoadFont) (*responses.FPDFText_LoadFont, error) {
p.Lock()
defer p.Unlock()
Expand Down Expand Up @@ -3082,6 +3083,60 @@
}, nil
}

// FPDFText_LoadCidType2Font returns a font object loaded from a stream of data for a type 2 CID font.
// The font is loaded into the document. Unlike FPDFText_LoadFont(), the ToUnicode data and the CIDToGIDMap
// data are caller provided, instead of auto-generated.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
func (p *PdfiumImplementation) FPDFText_LoadCidType2Font(request *requests.FPDFText_LoadCidType2Font) (*responses.FPDFText_LoadCidType2Font, error) {
p.Lock()
defer p.Unlock()

documentHandle, err := p.getDocumentHandle(request.Document)
if err != nil {
return nil, err
}

fontDataLength := uint32(len(request.FontData))
fontData, err := p.ByteArrayPointer(uint64(len(request.FontData)), request.FontData)
if err != nil {
return nil, err
}

Check warning on line 3104 in internal/implementation_webassembly/fpdf_edit.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_webassembly/fpdf_edit.go#L3103-L3104

Added lines #L3103 - L3104 were not covered by tests

defer fontData.Free()

toUnicodeCmapPointer, err := p.CString(request.ToUnicodeCmap)
if err != nil {
return nil, err
}

Check warning on line 3111 in internal/implementation_webassembly/fpdf_edit.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_webassembly/fpdf_edit.go#L3110-L3111

Added lines #L3110 - L3111 were not covered by tests

defer toUnicodeCmapPointer.Free()

cidToGIDMapDataLength := uint32(len(request.CIDToGIDMapData))
cidToGIDMapData, err := p.ByteArrayPointer(uint64(len(request.CIDToGIDMapData)), request.CIDToGIDMapData)
if err != nil {
return nil, err
}

Check warning on line 3119 in internal/implementation_webassembly/fpdf_edit.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_webassembly/fpdf_edit.go#L3118-L3119

Added lines #L3118 - L3119 were not covered by tests

defer cidToGIDMapData.Free()

res, err := p.Module.ExportedFunction("FPDFText_LoadCidType2Font").Call(p.Context, *documentHandle.handle, fontData.Pointer, *(*uint64)(unsafe.Pointer(&fontDataLength)), toUnicodeCmapPointer.Pointer, cidToGIDMapData.Pointer, *(*uint64)(unsafe.Pointer(&cidToGIDMapDataLength)))
if err != nil {
return nil, err
}

Check warning on line 3126 in internal/implementation_webassembly/fpdf_edit.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_webassembly/fpdf_edit.go#L3125-L3126

Added lines #L3125 - L3126 were not covered by tests

font := res[0]
if font == 0 {
return nil, errors.New("could not load CID Type2 font")
}

Check warning on line 3131 in internal/implementation_webassembly/fpdf_edit.go

View check run for this annotation

Codecov / codecov/patch

internal/implementation_webassembly/fpdf_edit.go#L3130-L3131

Added lines #L3130 - L3131 were not covered by tests

fontHandle := p.registerFont(&font)

return &responses.FPDFText_LoadCidType2Font{
Font: fontHandle.nativeRef,
}, nil
}

// FPDFTextObj_SetTextRenderMode sets the text rendering mode of a text object.
// Experimental API.
func (p *PdfiumImplementation) FPDFTextObj_SetTextRenderMode(request *requests.FPDFTextObj_SetTextRenderMode) (*responses.FPDFTextObj_SetTextRenderMode, error) {
Expand Down
8 changes: 8 additions & 0 deletions multi_threaded/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions pdfium.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,25 @@ type Pdfium interface {
FPDFText_SetCharcodes(request *requests.FPDFText_SetCharcodes) (*responses.FPDFText_SetCharcodes, error)

// FPDFText_LoadFont returns a font object loaded from a stream of data. The font is loaded
// into the document.
// The loaded font can be closed using FPDFFont_Close.
// into the document. Various font data structures, such as the ToUnicode data, are auto-generated based
// on the inputs
// The loaded font can be closed using FPDFFont_Close().
FPDFText_LoadFont(request *requests.FPDFText_LoadFont) (*responses.FPDFText_LoadFont, error)

// FPDFText_LoadStandardFont loads one of the standard 14 fonts per PDF spec 1.7 page 416. The preferred
// way of using font style is using a dash to separate the name from the style,
// for example 'Helvetica-BoldItalic'.
// The loaded font can be closed using FPDFFont_Close.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
FPDFText_LoadStandardFont(request *requests.FPDFText_LoadStandardFont) (*responses.FPDFText_LoadStandardFont, error)

// FPDFText_LoadCidType2Font returns a font object loaded from a stream of data for a type 2 CID font.
// The font is loaded into the document. Unlike FPDFText_LoadFont(), the ToUnicode data and the CIDToGIDMap
// data are caller provided, instead of auto-generated.
// The loaded font can be closed using FPDFFont_Close().
// Experimental API.
FPDFText_LoadCidType2Font(request *requests.FPDFText_LoadCidType2Font) (*responses.FPDFText_LoadCidType2Font, error)

// FPDFTextObj_GetFontSize returns the font size of a text object.
FPDFTextObj_GetFontSize(request *requests.FPDFTextObj_GetFontSize) (*responses.FPDFTextObj_GetFontSize, error)

Expand Down
13 changes: 10 additions & 3 deletions requests/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,23 @@ type FPDFText_SetCharcodes struct {

type FPDFText_LoadFont struct {
Document references.FPDF_DOCUMENT
Data []byte
FontType enums.FPDF_FONT
CID bool // Whether the font is a CID font or not.
Data []byte // The stream of font data, which will be copied by the font object.
FontType enums.FPDF_FONT // FPDF_FONT_TYPE1 or FPDF_FONT_TRUETYPE depending on the font type.
CID bool // Whether the font is a CID font or not.
}

type FPDFText_LoadStandardFont struct {
Document references.FPDF_DOCUMENT
Font string
}

type FPDFText_LoadCidType2Font struct {
Document references.FPDF_DOCUMENT
FontData []byte // The stream of font data, which will be copied by the font object.
ToUnicodeCmap string // The ToUnicode data.
CIDToGIDMapData []byte // the stream of CIDToGIDMap data.
}

type FPDFTextObj_GetFontSize struct {
PageObject references.FPDF_PAGEOBJECT
}
Expand Down
4 changes: 4 additions & 0 deletions responses/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ type FPDFText_LoadStandardFont struct {
Font references.FPDF_FONT
}

type FPDFText_LoadCidType2Font struct {
Font references.FPDF_FONT
}

type FPDFTextObj_GetRenderedBitmap struct {
Bitmap references.FPDF_BITMAP
}
Expand Down
61 changes: 61 additions & 0 deletions shared_tests/fpdf_edit_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ var _ = Describe("fpdf_edit", func() {
Expect(FPDFText_LoadStandardFont).To(BeNil())
})

It("returns an error when calling FPDFText_LoadCidType2Font", func() {
FPDFText_LoadCidType2Font, err := PdfiumInstance.FPDFText_LoadCidType2Font(&requests.FPDFText_LoadCidType2Font{})
Expect(err).To(MatchError("document not given"))
Expect(FPDFText_LoadCidType2Font).To(BeNil())
})

It("returns an error when calling FPDFTextObj_GetRenderedBitmap", func() {
FPDFTextObj_GetRenderedBitmap, err := PdfiumInstance.FPDFTextObj_GetRenderedBitmap(&requests.FPDFTextObj_GetRenderedBitmap{})
Expect(err).To(MatchError("document not given"))
Expand Down Expand Up @@ -447,6 +453,61 @@ var _ = Describe("fpdf_edit", func() {
Expect(FPDFFont_Close).To(Equal(&responses.FPDFFont_Close{}))
})

It("allows a CID Type 2 font to be loaded, a text object to be created with it and to be closed", func() {
fontData, err := ioutil.ReadFile(TestDataPath + "/testdata/NotoSansSC-Regular.subset.otf")
Expect(err).To(BeNil())

FPDFText_LoadCidType2Font, err := PdfiumInstance.FPDFText_LoadCidType2Font(&requests.FPDFText_LoadCidType2Font{
Document: doc,
FontData: fontData,
ToUnicodeCmap: `(
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo <<
/Registry (Adobe)
/Ordering (Identity)
/Supplement 0
>> def
/CMapName /Adobe-Identity-H def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
5 beginbfrange
<0001> <0003> [<0020> <3002> <2F00>]
<0003> <0004> [<4E00> <2F06>]
<0004> <0005> [<4E8C> <53E5>]
<0005> <0008> [<F906> <662F> <7B2C> <884C>]
<0008> <0009> [<FA08> <8FD9>]
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end
end
)`,
CIDToGIDMapData: []byte{0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9},
})
Expect(err).To(BeNil())
Expect(FPDFText_LoadCidType2Font).To(Not(BeNil()))
Expect(FPDFText_LoadCidType2Font.Font).To(Not(BeEmpty()))

FPDFPageObj_CreateTextObj, err := PdfiumInstance.FPDFPageObj_CreateTextObj(&requests.FPDFPageObj_CreateTextObj{
Font: FPDFText_LoadCidType2Font.Font,
Document: doc,
FontSize: 20,
})
Expect(err).To(BeNil())
Expect(FPDFPageObj_CreateTextObj).To(Not(BeNil()))
Expect(FPDFPageObj_CreateTextObj.PageObject).To(Not(BeEmpty()))

FPDFFont_Close, err := PdfiumInstance.FPDFFont_Close(&requests.FPDFFont_Close{
Font: FPDFText_LoadCidType2Font.Font,
})
Expect(err).To(BeNil())
Expect(FPDFFont_Close).To(Equal(&responses.FPDFFont_Close{}))
})

It("gives an error when loading a non-existing standard font", func() {
FPDFText_LoadStandardFont, err := PdfiumInstance.FPDFText_LoadStandardFont(&requests.FPDFText_LoadStandardFont{
Document: doc,
Expand Down
Loading
Loading