-
Notifications
You must be signed in to change notification settings - Fork 1
/
metafile.go
137 lines (106 loc) · 2.88 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
129
130
131
132
133
134
135
136
137
// 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 winapi
import (
"math"
"syscall"
"unsafe"
"github.com/Gipcomp/win32/gdi32"
"github.com/Gipcomp/winapi/errs"
)
const milimeterPerMeter float64 = 1000.0
type Metafile struct {
hdc gdi32.HDC
hemf gdi32.HENHMETAFILE
size Size // in native pixels
dpi Size
}
func NewMetafile(referenceCanvas *Canvas) (*Metafile, error) {
hdc := gdi32.CreateEnhMetaFile(referenceCanvas.hdc, nil, nil, nil)
if hdc == 0 {
return nil, errs.NewError("CreateEnhMetaFile failed")
}
return &Metafile{hdc: hdc}, nil
}
func NewMetafileFromFile(filePath string) (*Metafile, error) {
strPtr, err := syscall.UTF16PtrFromString(filePath)
if err != nil {
return nil, err
}
hemf := gdi32.GetEnhMetaFile(strPtr)
if hemf == 0 {
return nil, errs.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 {
gdi32.DeleteEnhMetaFile(mf.hemf)
mf.hemf = 0
}
}
func (mf *Metafile) Save(filePath string) error {
strPtr, err := syscall.UTF16PtrFromString(filePath)
if err != nil {
return err
}
hemf := gdi32.CopyEnhMetaFile(mf.hemf, strPtr)
if hemf == 0 {
return errs.NewError("CopyEnhMetaFile failed")
}
gdi32.DeleteEnhMetaFile(hemf)
return nil
}
func (mf *Metafile) readSizeFromHeader() error {
var hdr gdi32.ENHMETAHEADER
if gdi32.GetEnhMetaFileHeader(mf.hemf, uint32(unsafe.Sizeof(hdr)), &hdr) == 0 {
return errs.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 errs.NewError("already disposed")
} else {
return nil
}
}
mf.hemf = gdi32.CloseEnhMetaFile(mf.hdc)
if mf.hemf == 0 {
return errs.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 gdi32.HDC, location Point) error {
return mf.drawStretched(hdc, Rectangle{location.X, location.Y, mf.size.Width, mf.size.Height})
}
func (mf *Metafile) drawStretched(hdc gdi32.HDC, bounds Rectangle) error {
rc := bounds.toRECT()
if !gdi32.PlayEnhMetaFile(hdc, mf.hemf, &rc) {
return errs.NewError("PlayEnhMetaFile failed")
}
return nil
}