Skip to content

Commit

Permalink
Merge pull request #78 from klippa-app/feature/update-to-pdfium-5854
Browse files Browse the repository at this point in the history
Update to pdfium 5854
  • Loading branch information
jerbob92 authored Jun 28, 2023
2 parents ce92d6e + 79c9969 commit 02b4608
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 9 deletions.
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.18", "1.19", "1.20" ]
pdfium: [ "4849", "4874", "4929", "5038", "5079", "5254", "5378", "5664" ]
pdfium: [ "4849", "4874", "4929", "5038", "5079", "5254", "5378", "5664", "5854" ]
env:
PDFIUM_EXPERIMENTAL_VERSION: "5664"
PDFIUM_EXPERIMENTAL_VERSION: "5854"
PDFIUM_EXPERIMENTAL_GO_VERSION: "1.20"
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: 5664
Version: 5854
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: 5664
Version: 5854
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: 5664
Version: 5854
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.

25 changes: 25 additions & 0 deletions internal/implementation_cgo/fpdf_edit_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,3 +1162,28 @@ func (p *PdfiumImplementation) FPDFGlyphPath_GetGlyphPathSegment(request *reques
GlyphPathSegment: segmentHandle.nativeRef,
}, nil
}

// FPDFImageObj_GetImagePixelSize get the image size in pixels. Faster method to get only image size.
// Experimental API.
func (p *PdfiumImplementation) FPDFImageObj_GetImagePixelSize(request *requests.FPDFImageObj_GetImagePixelSize) (*responses.FPDFImageObj_GetImagePixelSize, error) {
p.Lock()
defer p.Unlock()

imageObjectHandle, err := p.getPageObjectHandle(request.ImageObject)
if err != nil {
return nil, err
}

width := C.uint(0)
height := C.uint(0)

result := C.FPDFImageObj_GetImagePixelSize(imageObjectHandle.handle, &width, &height)
if int(result) == 0 {
return nil, errors.New("could not get image pixel size")
}

return &responses.FPDFImageObj_GetImagePixelSize{
Width: uint(width),
Height: uint(height),
}, nil
}
6 changes: 6 additions & 0 deletions internal/implementation_cgo/fpdf_edit_no_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,9 @@ func (p *PdfiumImplementation) FPDFGlyphPath_CountGlyphSegments(request *request
func (p *PdfiumImplementation) FPDFGlyphPath_GetGlyphPathSegment(request *requests.FPDFGlyphPath_GetGlyphPathSegment) (*responses.FPDFGlyphPath_GetGlyphPathSegment, error) {
return nil, pdfium_errors.ErrExperimentalUnsupported
}

// FPDFImageObj_GetImagePixelSize get the image size in pixels. Faster method to get only image size.
// Experimental API.
func (p *PdfiumImplementation) FPDFImageObj_GetImagePixelSize(request *requests.FPDFImageObj_GetImagePixelSize) (*responses.FPDFImageObj_GetImagePixelSize, error) {
return nil, pdfium_errors.ErrExperimentalUnsupported
}
49 changes: 49 additions & 0 deletions internal/implementation_webassembly/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,55 @@ func (p *PdfiumImplementation) FPDFImageObj_GetImageMetadata(request *requests.F
}, nil
}

// FPDFImageObj_GetImagePixelSize get the image size in pixels. Faster method to get only image size.
// Experimental API.
func (p *PdfiumImplementation) FPDFImageObj_GetImagePixelSize(request *requests.FPDFImageObj_GetImagePixelSize) (*responses.FPDFImageObj_GetImagePixelSize, error) {
p.Lock()
defer p.Unlock()

imageObjectHandle, err := p.getPageObjectHandle(request.ImageObject)
if err != nil {
return nil, err
}

widthPointer, err := p.UIntPointer()
if err != nil {
return nil, err
}
defer widthPointer.Free()

heightPointer, err := p.UIntPointer()
if err != nil {
return nil, err
}
defer heightPointer.Free()

res, err := p.Module.ExportedFunction("FPDFImageObj_GetImagePixelSize").Call(p.Context, *imageObjectHandle.handle, widthPointer.Pointer, heightPointer.Pointer)
if err != nil {
return nil, err
}

success := *(*int32)(unsafe.Pointer(&res[0]))
if int(success) == 0 {
return nil, errors.New("could not get image pixel size")
}

widthValue, err := widthPointer.Value()
if err != nil {
return nil, err
}

heightValue, err := heightPointer.Value()
if err != nil {
return nil, err
}

return &responses.FPDFImageObj_GetImagePixelSize{
Width: widthValue,
Height: heightValue,
}, nil
}

// FPDFPageObj_CreateNewPath creates a new path object at an initial position.
func (p *PdfiumImplementation) FPDFPageObj_CreateNewPath(request *requests.FPDFPageObj_CreateNewPath) (*responses.FPDFPageObj_CreateNewPath, error) {
p.Lock()
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.

4 changes: 4 additions & 0 deletions pdfium.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ type Pdfium interface {
// failure to retrieve any specific parameter would result in its value being 0.
FPDFImageObj_GetImageMetadata(request *requests.FPDFImageObj_GetImageMetadata) (*responses.FPDFImageObj_GetImageMetadata, error)

// FPDFImageObj_GetImagePixelSize get the image size in pixels. Faster method to get only image size.
// Experimental API.
FPDFImageObj_GetImagePixelSize(request *requests.FPDFImageObj_GetImagePixelSize) (*responses.FPDFImageObj_GetImagePixelSize, error)

// FPDFPageObj_CreateNewPath creates a new path object at an initial position.
FPDFPageObj_CreateNewPath(request *requests.FPDFPageObj_CreateNewPath) (*responses.FPDFPageObj_CreateNewPath, error)

Expand Down
4 changes: 4 additions & 0 deletions requests/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ type FPDFImageObj_GetImageMetadata struct {
Page Page
}

type FPDFImageObj_GetImagePixelSize struct {
ImageObject references.FPDF_PAGEOBJECT
}

type FPDFPageObj_CreateNewPath struct {
X float32
Y float32
Expand Down
5 changes: 5 additions & 0 deletions responses/fpdf_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ type FPDFImageObj_GetImageMetadata struct {
ImageMetadata structs.FPDF_IMAGEOBJ_METADATA
}

type FPDFImageObj_GetImagePixelSize struct {
Width uint
Height uint
}

type FPDFPageObj_CreateNewPath struct {
PageObject references.FPDF_PAGEOBJECT
}
Expand Down
17 changes: 17 additions & 0 deletions shared_tests/fpdf_edit_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ var _ = Describe("fpdf_edit", func() {
Expect(err).To(MatchError("pageObject not given"))
Expect(FPDFPageObj_GetRotatedBounds).To(BeNil())
})

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

Expand Down Expand Up @@ -950,6 +956,17 @@ var _ = Describe("fpdf_edit", func() {
Expect(FPDFBitmap_Destroy).To(Equal(&responses.FPDFBitmap_Destroy{}))
})

It("returns the correct image pixel size", func() {
FPDFImageObj_GetImagePixelSize, err := PdfiumInstance.FPDFImageObj_GetImagePixelSize(&requests.FPDFImageObj_GetImagePixelSize{
ImageObject: imageObject,
})
Expect(err).To(BeNil())
Expect(FPDFImageObj_GetImagePixelSize).To(Equal(&responses.FPDFImageObj_GetImagePixelSize{
Width: 109,
Height: 88,
}))
})

It("gives an error when trying to remove an object without giving the page object", func() {
FPDFPage_RemoveObject, err := PdfiumInstance.FPDFPage_RemoveObject(&requests.FPDFPage_RemoveObject{
Page: requests.Page{
Expand Down
8 changes: 4 additions & 4 deletions shared_tests/fpdf_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = Describe("fpdf_save", func() {
Expect(err).To(BeNil())
Expect(FPDF_SaveAsCopy).To(Not(BeNil()))
Expect(FPDF_SaveAsCopy.FileBytes).To(Not(BeNil()))
Expect(FPDF_SaveAsCopy.FileBytes).To(PointTo(HaveLen(11375)))
Expect(FPDF_SaveAsCopy.FileBytes).To(SatisfyAny(PointTo(HaveLen(11375)), PointTo(HaveLen(11183)))) // 11375 < Pdfium 5854, 11183 >= Pdfium 5854
})
})

Expand All @@ -93,7 +93,7 @@ var _ = Describe("fpdf_save", func() {
Expect(err).To(BeNil())
Expect(FPDF_SaveAsCopy).To(Not(BeNil()))
Expect(FPDF_SaveAsCopy.FileBytes).To(BeNil())
Expect(fileStat.Size()).To(Equal(int64(11375)))
Expect(fileStat.Size()).To(SatisfyAny(Equal(int64(11375)), Equal(int64(11183)))) // 11375 < Pdfium 5854, 11183 >= Pdfium 5854
})
})

Expand Down Expand Up @@ -126,7 +126,7 @@ var _ = Describe("fpdf_save", func() {
Expect(err).To(BeNil())
Expect(FPDF_SaveAsCopy).To(Not(BeNil()))
Expect(FPDF_SaveAsCopy.FileBytes).To(BeNil())
Expect(buffer.Len()).To(Equal(11375))
Expect(buffer.Len()).To(SatisfyAny(Equal(11375), Equal(11183))) // 11375 < Pdfium 5854, 11183 >= Pdfium 5854
})
})

Expand All @@ -139,7 +139,7 @@ var _ = Describe("fpdf_save", func() {
Expect(err).To(BeNil())
Expect(FPDF_SaveWithVersion).To(Not(BeNil()))
Expect(FPDF_SaveWithVersion.FileBytes).To(Not(BeNil()))
Expect(FPDF_SaveWithVersion.FileBytes).To(PointTo(HaveLen(11375)))
Expect(FPDF_SaveWithVersion.FileBytes).To(SatisfyAny(PointTo(HaveLen(11375)), PointTo(HaveLen(11183)))) // 11375 < Pdfium 5854, 11183 >= Pdfium 5854

savedDoc, err := PdfiumInstance.FPDF_LoadMemDocument(&requests.FPDF_LoadMemDocument{
Data: FPDF_SaveWithVersion.FileBytes,
Expand Down
14 changes: 14 additions & 0 deletions shared_tests/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,20 @@ func compareRenderHash(renderedPage *responses.RenderPage, expectedPage *respons

hasher.Write(buf.Bytes())
currentHash := fmt.Sprintf("%x", hasher.Sum(nil))

// Overwrite hash because Pdfium 5854 renders things a littlebit different,
// causing the hash to have changed.

// Webassembly
if currentHash == "81c72474d4d2dbba72d23257716983ff46005b2d1eba54b6ba88642c0512282a" {
currentHash = "3abd50c3c34f37fdc3941c22ff118e82974df8621551e2b821649b05120b5c83"
}

// CGO
if currentHash == "13ab9fbbcf144199cdbf15a4b4fa01a0b78e52fe97b68b533ac5ef02696aeda5" {
currentHash = "5162bbc4c340ed3204fc348a4a8fe4454812419dbde77f506dd87d7a3f39b721"
}

Expect(string(existingFileHash)).To(Equal(currentHash))
}

Expand Down
14 changes: 14 additions & 0 deletions single_threaded/generated.go

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

14 changes: 14 additions & 0 deletions webassembly/generated.go

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

Binary file modified webassembly/pdfium.wasm
Binary file not shown.
7 changes: 7 additions & 0 deletions webassembly/webassembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ func Init(config Config) (pdfium.Pool, error) {
newWorker.Module = mod

malloc := mod.ExportedFunction("malloc")
if malloc == nil {
return nil, fmt.Errorf("could not find malloc in exported methods")
}

free := mod.ExportedFunction("free")
if malloc == nil {
return nil, fmt.Errorf("could not find free in exported methods")
}

newWorker.Functions = map[string]api.Function{
"malloc": malloc,
Expand Down

0 comments on commit 02b4608

Please sign in to comment.