mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 14:53:44 +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 != "",
|
||||
}
|
||||
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 {
|
||||
_, err = io.Copy(io.Discard, file)
|
||||
file.Close()
|
||||
if err == nil {
|
||||
r.Reader.fontFilesChecked[name] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
info.Status = FontStatusEmbedded
|
||||
|
||||
@@ -31,8 +31,10 @@ type Reader struct {
|
||||
OFD *OFD
|
||||
RootDir string
|
||||
ResMap map[string]string
|
||||
resourcesRead map[string]bool
|
||||
fontCache map[string]*Font
|
||||
fontResourcesRead bool
|
||||
fontFilesChecked map[string]bool
|
||||
colorSpaceCache map[string]*ColorSpace
|
||||
drawParamCache map[string]*DrawParam
|
||||
compositeGraphicUnitCache map[string]*CompositeGraphicUnit
|
||||
@@ -76,7 +78,9 @@ func (r *Reader) initRoot() error {
|
||||
}
|
||||
r.OFD = &ofd
|
||||
r.ResMap = make(map[string]string)
|
||||
r.resourcesRead = make(map[string]bool)
|
||||
r.fontCache = make(map[string]*Font)
|
||||
r.fontFilesChecked = make(map[string]bool)
|
||||
r.colorSpaceCache = make(map[string]*ColorSpace)
|
||||
r.drawParamCache = make(map[string]*DrawParam)
|
||||
r.compositeGraphicUnitCache = make(map[string]*CompositeGraphicUnit)
|
||||
@@ -182,6 +186,9 @@ func (r *Reader) loadRes(resPath string) {
|
||||
return
|
||||
}
|
||||
fullPath := r.ResPath(resPath)
|
||||
if r.resourcesRead[fullPath] {
|
||||
return
|
||||
}
|
||||
data, err := r.readFile(fullPath)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -217,6 +224,7 @@ func (r *Reader) loadRes(resPath string) {
|
||||
cgu := &res.CompositeGraphicUnits.CompositeGraphicUnit[i]
|
||||
r.compositeGraphicUnitCache[cgu.ID] = cgu
|
||||
}
|
||||
r.resourcesRead[fullPath] = true
|
||||
}
|
||||
|
||||
// resolveResourcePath 解析资源文件路径
|
||||
|
||||
@@ -26,16 +26,37 @@ import (
|
||||
"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 渲染为光栅图
|
||||
// 入参: page 页面内容
|
||||
// 返回: 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 {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user