-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JeffryTian
committed
May 15, 2020
0 parents
commit 6529e92
Showing
13 changed files
with
731 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fyne.syso | ||
*.exe | ||
*.so | ||
/release | ||
pkg | ||
debug | ||
*.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// 使用 IntelliSense 了解相关属性。 | ||
// 悬停以查看现有属性的描述。 | ||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}", | ||
"env": {}, | ||
"args": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"type": "shell", | ||
"echoCommand": true, | ||
"options": { | ||
"cwd": "${workspaceFolder}", | ||
}, | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "go build", | ||
"args": [ | ||
"-x" | ||
], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
}, | ||
{ | ||
"label": "test", | ||
"command": "go test", | ||
"args": [], | ||
"group": "test" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "fyne package", | ||
"args": [ | ||
"-release", | ||
"-name", | ||
"${workspaceFolderBasename}" | ||
], | ||
"group": "build", | ||
"problemMatcher": [ | ||
"$go" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# About | ||
A fyne project, can convert pdf to image. | ||
# How To Build | ||
- Install [fyne](https://fyne.io) by instructions | ||
- Download [Pdfium binaries](https://github.com/bblanchon/pdfium-binaries) and config the pkg-config | ||
# References | ||
[1] [go-pdfium-render](https://github.com/brunsgaard/go-pdfium-render) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module github.com/slory7/gtoolui | ||
|
||
go 1.14 | ||
|
||
require ( | ||
fyne.io/fyne v1.2.1-0.20200508113208-04d654b06855 | ||
github.com/containerd/containerd v1.3.4 | ||
github.com/fsnotify/fsnotify v1.4.9 // indirect | ||
github.com/fyne-io/mobile v0.0.2-0.20200511203347-825e44fe5a95 // indirect | ||
github.com/go-gl/glfw v0.0.0-20200420212212-258d9bec320e // indirect | ||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200420212212-258d9bec320e // indirect | ||
github.com/godbus/dbus/v5 v5.0.3 // indirect | ||
github.com/google/martian v2.1.0+incompatible | ||
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca // indirect | ||
github.com/lucor/goinfo v0.0.0-20200401173949-526b5363a13a // indirect | ||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect | ||
github.com/nuveo/log v0.0.0-20190430190217-44d02db6bdf8 | ||
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect | ||
github.com/opencontainers/image-spec v1.0.1 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/rjeczalik/pkgconfig v0.0.0-20190903131546-94d388dab445 // indirect | ||
github.com/sirupsen/logrus v1.6.0 // indirect | ||
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect | ||
github.com/stretchr/testify v1.5.1 // indirect | ||
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect | ||
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect | ||
golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25 // indirect | ||
golang.org/x/text v0.3.2 // indirect | ||
golang.org/x/tools v0.0.0-20200512001501-aaeff5de670a // indirect | ||
google.golang.org/grpc v1.29.1 // indirect | ||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect | ||
gopkg.in/yaml.v2 v2.2.8 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/app" | ||
"fyne.io/fyne/theme" | ||
"github.com/slory7/gtoolui/screens/home" | ||
) | ||
|
||
func init() { | ||
if runtime.GOOS == "windows" { | ||
windir := os.Getenv("windir") | ||
os.Setenv("FYNE_FONT", windir+"\\fonts\\msyh.ttc") | ||
} | ||
} | ||
|
||
func main() { | ||
a := app.NewWithID("GToolUI") | ||
a.SetIcon(theme.FyneLogo()) | ||
a.Settings().SetTheme(theme.LightTheme()) | ||
|
||
w := a.NewWindow("工具包") | ||
h := home.NewHome() | ||
w.SetContent(h.UILayout(w)) | ||
|
||
w.Resize(fyne.NewSize(850, 650)) | ||
w.SetFixedSize(true) | ||
w.CenterOnScreen() | ||
w.SetMaster() | ||
w.ShowAndRun() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Package home is the home screen of this app | ||
package home | ||
|
||
import ( | ||
"fmt" | ||
"image/jpeg" | ||
"image/png" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"fyne.io/fyne" | ||
"github.com/slory7/gtoolui/third-parties/pdfium" | ||
"github.com/slory7/gtoolui/utils/ui" | ||
) | ||
|
||
// Home page | ||
type Home struct { | ||
win fyne.Window | ||
pdfFile, pdfFolder string | ||
pdfFileNameChanged chan string | ||
pageRange, imageFormat string | ||
imageScale int | ||
pdfIsConverting chan bool | ||
} | ||
|
||
// NewHome is used to new it | ||
func NewHome() Home { | ||
p := fyne.CurrentApp().Preferences() | ||
imgf := p.StringWithFallback("imageFormat", "png") | ||
imgs := p.IntWithFallback("imageScale", 200) | ||
return Home{ | ||
pdfFileNameChanged: make(chan string), | ||
pdfIsConverting: make(chan bool), | ||
imageFormat: imgf, | ||
imageScale: imgs, | ||
} | ||
} | ||
|
||
func (p *Home) fileInputChanged(s string) { | ||
ltrChar := "\u202a" | ||
s = strings.TrimLeft(s, ltrChar) | ||
if len(s) > 0 && strings.HasSuffix(s, ".pdf") { | ||
if dir, file := filepath.Split(s); dir != "" { | ||
p.pdfFolder = dir | ||
p.pdfFile = s | ||
p.pdfFileNameChanged <- file | ||
} else if p.pdfFolder != "" { | ||
p.pdfFile = filepath.Join(p.pdfFolder, s) | ||
} | ||
p.win.SetTitle(p.pdfFile) | ||
} | ||
} | ||
|
||
func (p *Home) showPDFFileOpen() { | ||
ui.ShowFileOpen(p.win, func(fname, fpath string, err error) { | ||
if err != nil { | ||
ui.ShowError(err, p.win) | ||
} else if fname != "" { | ||
p.pdfFileNameChanged <- fname | ||
p.pdfFile = fpath | ||
p.pdfFolder = filepath.Dir(p.pdfFile) | ||
p.win.SetTitle(p.pdfFile) | ||
} | ||
}) | ||
} | ||
|
||
func (p *Home) convertPDF() { | ||
if p.pdfFile != "" { | ||
bytesPDF, err := ioutil.ReadFile(p.pdfFile) | ||
if err != nil { | ||
ui.ShowError(err, p.win) | ||
} else { | ||
p.pdfIsConverting <- true | ||
|
||
pdfium.InitLibrary() | ||
d, err := pdfium.NewDocument(&bytesPDF) | ||
if err != nil { | ||
ui.ShowError(err, p.win) | ||
} else { | ||
ext := filepath.Ext(p.pdfFile) | ||
imgfileFormat := strings.TrimRight(p.pdfFile, ext) + "_%d." + p.imageFormat | ||
pagecount := d.GetPageCount() | ||
convertedCount := 0 | ||
if p.pageRange != "" { | ||
if strings.Contains(p.pageRange, ",") { | ||
for _, pg := range strings.Split(p.pageRange, ",") { | ||
if pgnum, _ := strconv.Atoi(pg); pgnum > 0 { | ||
if pgnum > pagecount { | ||
pgnum = pagecount | ||
} | ||
renderPDFPageToImageFile(d, fmt.Sprintf(imgfileFormat, pgnum), pgnum-1, p.imageScale) | ||
convertedCount++ | ||
} | ||
} | ||
} else if strings.Contains(p.pageRange, "-") { | ||
arr := strings.Split(p.pageRange, "-") | ||
from, _ := strconv.Atoi(arr[0]) | ||
to, _ := strconv.Atoi(arr[1]) | ||
if to == 0 { | ||
to = pagecount | ||
} | ||
if from <= to { | ||
if from > pagecount { | ||
from = pagecount | ||
} | ||
if to > pagecount { | ||
to = pagecount | ||
} | ||
for n := from; n <= to; n++ { | ||
renderPDFPageToImageFile(d, fmt.Sprintf(imgfileFormat, n), n-1, p.imageScale) | ||
convertedCount++ | ||
} | ||
} | ||
} else { | ||
if pgnum, _ := strconv.Atoi(p.pageRange); pgnum > 0 { | ||
if pgnum > pagecount { | ||
pgnum = pagecount | ||
} | ||
renderPDFPageToImageFile(d, fmt.Sprintf(imgfileFormat, pgnum), pgnum-1, p.imageScale) | ||
convertedCount++ | ||
} | ||
} | ||
} else { | ||
for n := 0; n < pagecount; n++ { | ||
renderPDFPageToImageFile(d, fmt.Sprintf(imgfileFormat, n+1), n, p.imageScale) | ||
convertedCount++ | ||
} | ||
} | ||
d.Close() | ||
ui.ShowInformation("操作完成", fmt.Sprintf("转换了%d页", convertedCount), p.win) | ||
} | ||
pdfium.DestroyLibrary() | ||
|
||
p.pdfIsConverting <- false | ||
} | ||
} | ||
} | ||
|
||
func renderPDFPageToImageFile(d *pdfium.Document, imageFile string, pageIndex int, scale int) (err error) { | ||
img := d.RenderPage(pageIndex, float64(scale)/100) | ||
f, err := os.Create(imageFile) | ||
if err != nil { | ||
return | ||
} | ||
if strings.HasSuffix(imageFile, ".jpg") { | ||
err = jpeg.Encode(f, img, &jpeg.Options{Quality: 80}) | ||
} else if strings.HasSuffix(imageFile, ".png") { | ||
err = png.Encode(f, img) | ||
} | ||
defer f.Close() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package home | ||
|
||
import ( | ||
"fmt" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/layout" | ||
"fyne.io/fyne/theme" | ||
"fyne.io/fyne/widget" | ||
) | ||
|
||
// UILayout is the layout ui func | ||
func (p *Home) UILayout(win fyne.Window) *fyne.Container { | ||
p.win = win | ||
ltitle := widget.NewLabel("PDF转图片:") | ||
ltitle.Alignment = fyne.TextAlignLeading | ||
txtFile := widget.NewEntry() | ||
txtFile.SetPlaceHolder("请输入PDF文件路径") | ||
txtFile.OnChanged = p.fileInputChanged | ||
btnBrowse := widget.NewButtonWithIcon("浏览", theme.FolderOpenIcon(), p.showPDFFileOpen) | ||
rowFile := widget.NewHBox(txtFile, btnBrowse) | ||
|
||
lPageRange := widget.NewLabel("页码范围") | ||
txtPageRange := widget.NewEntry() | ||
txtPageRange.SetPlaceHolder("1,3,5 或 2-10 或 3-") | ||
txtPageRange.OnChanged = func(s string) { p.pageRange = s } | ||
rowPageRange := widget.NewHBox(lPageRange, txtPageRange) | ||
|
||
lselectFormat := widget.NewLabel("图片格式") | ||
selectFormat := widget.NewSelect([]string{"png", "jpg"}, func(s string) { | ||
p.imageFormat = s | ||
fyne.CurrentApp().Preferences().SetString("imageFormat", p.imageFormat) | ||
}) | ||
selectFormat.Selected = p.imageFormat | ||
rowFormat := widget.NewHBox(lselectFormat, selectFormat) | ||
ssacle := "缩放比例 %d%%" | ||
lsliderScale := widget.NewLabel(fmt.Sprintf(ssacle, p.imageScale)) | ||
sliderScale := widget.NewSlider(1, 400) | ||
sliderScale.Value = float64(p.imageScale) | ||
sliderScale.OnChanged = func(v float64) { | ||
p.imageScale = int(v) | ||
fyne.CurrentApp().Preferences().SetInt("imageScale", p.imageScale) | ||
lsliderScale.Text = fmt.Sprintf(ssacle, p.imageScale) | ||
lsliderScale.Refresh() | ||
} | ||
//sliderScale.Resize(fyne.NewSize(200, 50)) | ||
//rowScale := widget.NewHBox(lsliderScale, sliderScale) | ||
|
||
btnConvert := widget.NewButtonWithIcon("转换", theme.SearchReplaceIcon(), p.convertPDF) | ||
|
||
l := fyne.NewContainerWithLayout(layout.NewVBoxLayout(), | ||
ltitle, | ||
rowFile, | ||
rowPageRange, | ||
rowFormat, | ||
lsliderScale, | ||
sliderScale, | ||
layout.NewSpacer(), | ||
btnConvert, | ||
) | ||
|
||
go func() { | ||
for { | ||
txtFile.Text = <-p.pdfFileNameChanged | ||
txtFile.Refresh() | ||
} | ||
}() | ||
go func() { | ||
for b := range p.pdfIsConverting { | ||
if b { | ||
btnConvert.SetText("转换中。。。") | ||
btnConvert.Disable() | ||
} else { | ||
btnConvert.SetText("转换") | ||
btnConvert.Enable() | ||
} | ||
} | ||
}() | ||
|
||
return l | ||
} |
Oops, something went wrong.