mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 01:27:26 +08:00
This commit is contained in:
1 parent
3c15a18546
commit
3b2950a9c1
3 files changed
+40
-3
No files matched your search
+9
-1
@@ -168,10 +168,18 @@ func (r *Renderer) fontInfo(font Font) FontInfo {
|
|||||||
Embedded: font.FontFile != "",
|
Embedded: font.FontFile != "",
|
||||||
}
|
}
|
||||||
if info.Embedded {
|
if info.Embedded {
|
||||||
file, err := r.Reader.openFile(r.Reader.ResPath(font.FontFile))
|
name := r.Reader.ResPath(font.FontFile)
|
||||||
|
var err error
|
||||||
|
if !r.Reader.fontFilesChecked[name] {
|
||||||
|
var file io.ReadCloser
|
||||||
|
file, err = r.Reader.openFile(name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_, err = io.Copy(io.Discard, file)
|
_, err = io.Copy(io.Discard, file)
|
||||||
file.Close()
|
file.Close()
|
||||||
|
if err == nil {
|
||||||
|
r.Reader.fontFilesChecked[name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
info.Status = FontStatusEmbedded
|
info.Status = FontStatusEmbedded
|
||||||
|
|||||||
@@ -31,8 +31,10 @@ type Reader struct {
|
|||||||
OFD *OFD
|
OFD *OFD
|
||||||
RootDir string
|
RootDir string
|
||||||
ResMap map[string]string
|
ResMap map[string]string
|
||||||
|
resourcesRead map[string]bool
|
||||||
fontCache map[string]*Font
|
fontCache map[string]*Font
|
||||||
fontResourcesRead bool
|
fontResourcesRead bool
|
||||||
|
fontFilesChecked map[string]bool
|
||||||
colorSpaceCache map[string]*ColorSpace
|
colorSpaceCache map[string]*ColorSpace
|
||||||
drawParamCache map[string]*DrawParam
|
drawParamCache map[string]*DrawParam
|
||||||
compositeGraphicUnitCache map[string]*CompositeGraphicUnit
|
compositeGraphicUnitCache map[string]*CompositeGraphicUnit
|
||||||
@@ -76,7 +78,9 @@ func (r *Reader) initRoot() error {
|
|||||||
}
|
}
|
||||||
r.OFD = &ofd
|
r.OFD = &ofd
|
||||||
r.ResMap = make(map[string]string)
|
r.ResMap = make(map[string]string)
|
||||||
|
r.resourcesRead = make(map[string]bool)
|
||||||
r.fontCache = make(map[string]*Font)
|
r.fontCache = make(map[string]*Font)
|
||||||
|
r.fontFilesChecked = make(map[string]bool)
|
||||||
r.colorSpaceCache = make(map[string]*ColorSpace)
|
r.colorSpaceCache = make(map[string]*ColorSpace)
|
||||||
r.drawParamCache = make(map[string]*DrawParam)
|
r.drawParamCache = make(map[string]*DrawParam)
|
||||||
r.compositeGraphicUnitCache = make(map[string]*CompositeGraphicUnit)
|
r.compositeGraphicUnitCache = make(map[string]*CompositeGraphicUnit)
|
||||||
@@ -182,6 +186,9 @@ func (r *Reader) loadRes(resPath string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fullPath := r.ResPath(resPath)
|
fullPath := r.ResPath(resPath)
|
||||||
|
if r.resourcesRead[fullPath] {
|
||||||
|
return
|
||||||
|
}
|
||||||
data, err := r.readFile(fullPath)
|
data, err := r.readFile(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -217,6 +224,7 @@ func (r *Reader) loadRes(resPath string) {
|
|||||||
cgu := &res.CompositeGraphicUnits.CompositeGraphicUnit[i]
|
cgu := &res.CompositeGraphicUnits.CompositeGraphicUnit[i]
|
||||||
r.compositeGraphicUnitCache[cgu.ID] = cgu
|
r.compositeGraphicUnitCache[cgu.ID] = cgu
|
||||||
}
|
}
|
||||||
|
r.resourcesRead[fullPath] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveResourcePath 解析资源文件路径
|
// resolveResourcePath 解析资源文件路径
|
||||||
|
|||||||
@@ -26,16 +26,37 @@ import (
|
|||||||
"github.com/tdewolff/canvas/renderers/rasterizer"
|
"github.com/tdewolff/canvas/renderers/rasterizer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// rasterRenderer 保留页面物理尺寸的光栅渲染器
|
||||||
|
type rasterRenderer struct {
|
||||||
|
*rasterizer.Rasterizer
|
||||||
|
width, height float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size 返回页面物理尺寸
|
||||||
|
// 返回: float64 宽度, float64 高度
|
||||||
|
func (r *rasterRenderer) Size() (float64, float64) {
|
||||||
|
return r.width, r.height
|
||||||
|
}
|
||||||
|
|
||||||
// RenderToImage 渲染为光栅图
|
// RenderToImage 渲染为光栅图
|
||||||
// 入参: page 页面内容
|
// 入参: page 页面内容
|
||||||
// 返回: image.Image 图像对象, error 错误信息
|
// 返回: image.Image 图像对象, error 错误信息
|
||||||
func (r *Renderer) RenderToImage(page *PageContent) (image.Image, error) {
|
func (r *Renderer) RenderToImage(page *PageContent) (image.Image, error) {
|
||||||
c, err := r.RenderPage(page)
|
box, err := r.GetPageBox(page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dpmm := r.DPI / 25.4
|
dpmm := r.DPI / 25.4
|
||||||
return rasterizer.Draw(c, canvas.DPMM(dpmm), canvas.DefaultColorSpace), nil
|
raster := &rasterRenderer{
|
||||||
|
Rasterizer: rasterizer.New(box.W, box.H, canvas.DPMM(dpmm), canvas.DefaultColorSpace),
|
||||||
|
width: box.W,
|
||||||
|
height: box.H,
|
||||||
|
}
|
||||||
|
if err := r.RenderPageToContext(canvas.NewContext(raster), page); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
raster.Close()
|
||||||
|
return raster.Image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderToSVG 渲染为SVG
|
// RenderToSVG 渲染为SVG
|
||||||
|
|||||||
Reference in new issue
Block a user