forked from therox/rtf-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpictures.go
123 lines (102 loc) · 2.42 KB
/
pictures.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
package rtfdoc
import (
"bytes"
"encoding/hex"
"fmt"
"image"
"log"
_ "image/jpeg"
_ "image/png"
)
// AddPicture adds picture
func (par *Paragraph) AddPicture(source []byte, format string) *Picture {
var pic = Picture{
paragraphWidth: par.maxWidth,
}
var err error
formatFound := false
for _, i := range []string{ImageFormatJpeg, ImageFormatPng} {
if format == i {
formatFound = true
break
}
}
if !formatFound {
log.Println("Unknown format")
return &pic
}
pic.updateMaxWidth()
pic.src = source
pic.format = format
pic.scaleX = 100
pic.scaleY = 100
//Calculating dimensions
pic.height, pic.width, err = getImageDimensions(pic.src)
if err != nil {
pic.height = 100
pic.width = 100
}
if pic.width > getPixelsFromTwips(pic.maxWidth) {
newWidth := getPixelsFromTwips(pic.maxWidth)
pic.height = int(float64(pic.height) / (float64(pic.width) / float64(newWidth)))
pic.width = newWidth
}
par.content = append(par.content, &pic)
return &pic
}
func (pic *Picture) updateMaxWidth() *Picture {
pic.maxWidth = pic.paragraphWidth
return pic
}
func (pic *Picture) SetWidth(width int) *Picture {
if getTwipsFromPixels(width) > pic.maxWidth {
pic.width = getPixelsFromTwips(pic.maxWidth)
return pic
}
pic.width = width
return pic
}
func (pic *Picture) SetHeight(height int) *Picture {
pic.height = height
return pic
}
func (pic *Picture) SetScaleX(scaleX int) *Picture {
pic.scaleX = scaleX
return pic
}
func (pic *Picture) SetScaleY(scaleY int) *Picture {
pic.scaleY = scaleY
return pic
}
func (pic *Picture) SetCropLeft(cropL int) *Picture {
pic.cropL = cropL
return pic
}
func (pic *Picture) SetCropRight(cropR int) *Picture {
pic.cropR = cropR
return pic
}
func (pic *Picture) SetCropTop(cropT int) *Picture {
pic.cropT = cropT
return pic
}
func (pic *Picture) SetCropBottom(cropB int) *Picture {
pic.cropB = cropB
return pic
}
func (pic *Picture) compose() string {
res := fmt.Sprintf("\n{\\*\\shppict{ \\pict\\picscalex%d\\picscaley%d\\piccropl%d\\piccropr%d\\piccropt%d\\piccropb%d\\picw%d\\pich%d\\picwgoal%d\\pichgoal%d\\%sblip",
pic.scaleX, pic.scaleY,
pic.cropL, pic.cropR, pic.cropT, pic.cropB,
pic.width, pic.height,
pic.width*15, pic.height*15,
pic.format,
)
res += "\n" + hex.EncodeToString(pic.src)
res += "\n}}"
return res
}
func getImageDimensions(img []byte) (int, int, error) {
i, _, err := image.DecodeConfig(bytes.NewReader(img))
return i.Height, i.Width, err
}