-
Notifications
You must be signed in to change notification settings - Fork 0
/
metafile.go
128 lines (97 loc) · 2.66 KB
/
metafile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"math"
"syscall"
"unsafe"
"github.com/lxn/win"
)
const milimeterPerMeter float64 = 1000.0
type Metafile struct {
hdc win.HDC
hemf win.HENHMETAFILE
size Size // in native pixels
dpi Size
}
func NewMetafile(referenceCanvas *Canvas) (*Metafile, error) {
hdc := win.CreateEnhMetaFile(referenceCanvas.hdc, nil, nil, nil)
if hdc == 0 {
return nil, newError("CreateEnhMetaFile failed")
}
return &Metafile{hdc: hdc}, nil
}
func NewMetafileFromFile(filePath string) (*Metafile, error) {
hemf := win.GetEnhMetaFile(syscall.StringToUTF16Ptr(filePath))
if hemf == 0 {
return nil, newError("GetEnhMetaFile failed")
}
mf := &Metafile{hemf: hemf}
err := mf.readSizeFromHeader()
if err != nil {
return nil, err
}
return mf, nil
}
func (mf *Metafile) Dispose() {
mf.ensureFinished()
if mf.hemf != 0 {
win.DeleteEnhMetaFile(mf.hemf)
mf.hemf = 0
}
}
func (mf *Metafile) Save(filePath string) error {
hemf := win.CopyEnhMetaFile(mf.hemf, syscall.StringToUTF16Ptr(filePath))
if hemf == 0 {
return newError("CopyEnhMetaFile failed")
}
win.DeleteEnhMetaFile(hemf)
return nil
}
func (mf *Metafile) readSizeFromHeader() error {
var hdr win.ENHMETAHEADER
if win.GetEnhMetaFileHeader(mf.hemf, uint32(unsafe.Sizeof(hdr)), &hdr) == 0 {
return newError("GetEnhMetaFileHeader failed")
}
mf.size = sizeFromRECT(hdr.RclBounds)
scale := milimeterPerMeter / inchesPerMeter
mf.dpi = Size{
int(math.Round(float64(hdr.SzlDevice.CX) / float64(hdr.SzlMillimeters.CX) * scale)),
int(math.Round(float64(hdr.SzlDevice.CY) / float64(hdr.SzlMillimeters.CY) * scale)),
}
return nil
}
func (mf *Metafile) ensureFinished() error {
if mf.hdc == 0 {
if mf.hemf == 0 {
return newError("already disposed")
} else {
return nil
}
}
mf.hemf = win.CloseEnhMetaFile(mf.hdc)
if mf.hemf == 0 {
return newError("CloseEnhMetaFile failed")
}
mf.hdc = 0
return mf.readSizeFromHeader()
}
// Size returns image size in 1/96" units.
func (mf *Metafile) Size() Size {
return Size{
Width: scaleInt(mf.size.Width, 96.0/float64(mf.dpi.Width)),
Height: scaleInt(mf.size.Height, 96.0/float64(mf.dpi.Height)),
}
}
func (mf *Metafile) draw(hdc win.HDC, location Point) error {
return mf.drawStretched(hdc, Rectangle{location.X, location.Y, mf.size.Width, mf.size.Height})
}
func (mf *Metafile) drawStretched(hdc win.HDC, bounds Rectangle) error {
rc := bounds.toRECT()
if !win.PlayEnhMetaFile(hdc, mf.hemf, &rc) {
return newError("PlayEnhMetaFile failed")
}
return nil
}