-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage.go
More file actions
63 lines (51 loc) · 1.87 KB
/
Copy pathimage.go
File metadata and controls
63 lines (51 loc) · 1.87 KB
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
// Copyright (c) 2024-2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package typst
import (
"bytes"
"fmt"
"image"
"image/png"
"strconv"
)
// Image can be used to encode any image.Image into a Typst image.
//
// For this, just wrap any image.Image with this type before passing it to MarshalValue or a ValueEncoder:
//
// typstImage := typst.Image{img}
// typst.InjectValues(&r, map[string]any{"TestImage": typstImage})
type Image struct{ image.Image }
func (i Image) MarshalTypstValue() ([]byte, error) {
var buffer bytes.Buffer
if err := png.Encode(&buffer, i); err != nil {
return nil, fmt.Errorf("failed to encode image as PNG: %w", err)
}
// TODO: Make image encoding more efficient: Use reader/writer, baseXX encoding
// TODO: Consider using raw pixel encoding instead of PNG
var buf bytes.Buffer
buf.WriteString("image.decode(bytes((") // TODO: Pass bytes directly to image once Typst 0.12.0 is not supported anymore
for _, b := range buffer.Bytes() {
buf.WriteString(strconv.FormatUint(uint64(b), 10) + ",")
}
buf.WriteString(")))")
return buf.Bytes(), nil
}
// ImageRaw can be used to pass the raw data of any image to Typst.
// This will pass the raw byte values of a PNG, JPEG or any other image format that is supported by Typst.
//
// For this, just wrap any byte slice with this type before passing it to MarshalValue or a ValueEncoder:
//
// typstImage := typst.ImageRaw(bufferPNG)
// typst.InjectValues(&r, map[string]any{"TestImage": typstImage})
type ImageRaw []byte
func (i ImageRaw) MarshalTypstValue() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("image.decode(bytes((") // TODO: Pass bytes directly to image once Typst 0.12.0 is not supported anymore
for _, b := range i {
buf.WriteString(strconv.FormatUint(uint64(b), 10) + ",")
}
buf.WriteString(")))")
return buf.Bytes(), nil
}