forked from ixmilia/dxf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodePairWriter.go
325 lines (270 loc) · 6.79 KB
/
codePairWriter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package dxf
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"strings"
)
type codePairWriter interface {
init() error
writeCodePair(codePair CodePair) error
}
// code pairs
type directCodePairWriter struct {
CodePairs []CodePair
}
func newDirectCodePairWriter() directCodePairWriter {
return directCodePairWriter{
CodePairs: make([]CodePair, 0),
}
}
func (d *directCodePairWriter) init() error {
// noop
return nil
}
func (d *directCodePairWriter) writeCodePair(codePair CodePair) error {
d.CodePairs = append(d.CodePairs, codePair)
return nil
}
// text
type textCodePairWriter struct {
writer io.Writer
version AcadVersion
}
func newTextCodePairWriter(writer io.Writer, version AcadVersion) codePairWriter {
return &textCodePairWriter{
writer: writer,
version: version,
}
}
func formatShortText(val int16) string {
return fmt.Sprintf("%6d", val)
}
func formatIntText(val int) string {
return fmt.Sprintf("%9d", val)
}
func formatLongText(val int64) string {
return fmt.Sprintf("%d", val)
}
func formatBoolText(val bool) string {
short := 1
if !val {
short = 0
}
return formatShortText(int16(short))
}
func formatFloat64Text(val float64) string {
// trim trailing zeros
display := strings.TrimRight(fmt.Sprintf("%.12f", val), "0")
// ensure it doesn't end with a decimal
if strings.HasSuffix(display, ".") {
display += "0"
}
return display
}
func formatStringText(val string, version AcadVersion) string {
if version <= R2004 {
// escape unicode characters
var builder strings.Builder
for _, r := range val {
u := uint(r)
if u >= 128 {
builder.WriteString(fmt.Sprintf("\\U+%04X", u))
} else {
builder.WriteRune(r)
}
}
val = builder.String()
}
return val
}
func (a *textCodePairWriter) writeBoolean(val bool) error {
return a.writeString(formatBoolText(val))
}
func (a *textCodePairWriter) writeDouble(val float64) error {
return a.writeString(formatFloat64Text(val))
}
func (a *textCodePairWriter) writeInt(val int) error {
return a.writeString(formatIntText(val))
}
func (a *textCodePairWriter) writeLong(val int64) error {
return a.writeString(formatLongText(val))
}
func (a *textCodePairWriter) writeShort(val int16) error {
return a.writeString(formatShortText(val))
}
func (a *textCodePairWriter) writeString(val string) error {
formatted := formatStringText(val, a.version)
bytes := []byte(fmt.Sprintf("%s\r\n", formatted))
_, err := a.writer.Write(bytes)
return err
}
func (a *textCodePairWriter) init() error {
// noop
return nil
}
func (a *textCodePairWriter) writeCodePair(codePair CodePair) error {
err := a.writeString(fmt.Sprintf("%3d", codePair.Code))
if err != nil {
return nil
}
switch t := codePair.Value.(type) {
case BoolCodePairValue:
return a.writeBoolean(t.Value)
case DoubleCodePairValue:
return a.writeDouble(t.Value)
case IntCodePairValue:
return a.writeInt(t.Value)
case LongCodePairValue:
return a.writeLong(t.Value)
case ShortCodePairValue:
return a.writeShort(t.Value)
case StringCodePairValue:
return a.writeString(t.Value)
default:
return fmt.Errorf("unsupported code pair value type %T", t)
}
}
// binary
type binaryCodePairWriter struct {
writer io.Writer
version AcadVersion
}
func newBinaryCodePairWriter(writer io.Writer, version AcadVersion) codePairWriter {
return &binaryCodePairWriter{
writer: writer,
version: version,
}
}
func formatShortBinary(val int16) []byte {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, uint16(val))
return buf
}
func formatIntBinary(val int) []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(val))
return buf
}
func formatLongBinary(val int64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(val))
return buf
}
func formatBoolBinary(val bool, version AcadVersion) []byte {
short := 1
if !val {
short = 0
}
// after R13 bools are a single byte
if version >= R13 {
return []byte{byte(short)}
}
return formatShortBinary(int16(short))
}
func formatFloat64Binary(val float64) []byte {
buf := make([]byte, 8)
ui := math.Float64bits(val)
binary.LittleEndian.PutUint64(buf, ui)
return buf
}
func formatStringBinary(val string) []byte {
buf := []byte(val)
buf = append(buf, 0x00)
return buf
}
func (b *binaryCodePairWriter) init() error {
sentinel := []byte("AutoCAD Binary DXF\r\n")
sentinel = append(sentinel, []byte{0x1A, 0x00}...)
n, err := b.writer.Write(sentinel)
if err != nil {
return err
}
if n != len(sentinel) {
return errors.New("unable to write binary sentinel")
}
return nil
}
func (b *binaryCodePairWriter) writeCodePair(codePair CodePair) error {
var err error
if b.version >= R13 {
// after R13 codes are always 2 bytes
err = b.writeShort(int16(codePair.Code))
if err != nil {
return err
}
} else if codePair.Code >= 255 {
// before R13 codes were 1 or 3 bytes
err = b.writeByte(255)
if err != nil {
return err
}
err = b.writeShort(int16(codePair.Code))
} else {
err = b.writeByte(byte(codePair.Code))
}
if err != nil {
return err
}
switch t := codePair.Value.(type) {
case BoolCodePairValue:
return b.writeBoolean(t.Value, b.version)
case DoubleCodePairValue:
return b.writeDouble(t.Value)
case IntCodePairValue:
return b.writeInt(t.Value)
case LongCodePairValue:
return b.writeLong(t.Value)
case ShortCodePairValue:
return b.writeShort(t.Value)
case StringCodePairValue:
return b.writeString(t.Value)
default:
return fmt.Errorf("unsupported code pair value type %T", t)
}
}
func (b *binaryCodePairWriter) writeBytes(buf []byte) error {
n, err := b.writer.Write(buf)
if err != nil {
return err
}
if n != len(buf) {
return errors.New("unable to write bytes")
}
return nil
}
func (b *binaryCodePairWriter) writeByte(bt byte) error {
return b.writeBytes([]byte{bt})
}
func (b *binaryCodePairWriter) writeShort(val int16) error {
return b.writeBytes(formatShortBinary(val))
}
func (b *binaryCodePairWriter) writeBoolean(val bool, version AcadVersion) error {
return b.writeBytes(formatBoolBinary(val, version))
}
func (b *binaryCodePairWriter) writeInt(val int) error {
return b.writeBytes(formatIntBinary(val))
}
func (b *binaryCodePairWriter) writeLong(val int64) error {
return b.writeBytes(formatLongBinary(val))
}
func (b *binaryCodePairWriter) writeDouble(val float64) error {
return b.writeBytes(formatFloat64Binary(val))
}
func (b *binaryCodePairWriter) writeString(val string) error {
return b.writeBytes(formatStringBinary(val))
}
func writeSectionStart(writer codePairWriter, sectionName string) (error error) {
error = writer.writeCodePair(NewStringCodePair(0, "SECTION"))
if error != nil {
return
}
error = writer.writeCodePair(NewStringCodePair(2, sectionName))
return
}
func writeSectionEnd(writer codePairWriter) (error error) {
error = writer.writeCodePair(NewStringCodePair(0, "ENDSEC"))
return
}