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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//! ASCII DXF writer
use super::stream_writer::DxfStreamWriter;
use crate::error::Result;
use crate::types::Handle;
use std::io::Write;
/// ASCII DXF stream writer.
///
/// Uses CR/LF (`\r\n`) line endings as required by the DXF text format
/// specification.
pub struct DxfTextWriter<W: Write> {
writer: W,
/// Reusable stack buffer for formatting numbers without heap allocation.
fmt_buf: [u8; 64],
}
impl<W: Write> DxfTextWriter<W> {
/// Create a new ASCII DXF writer
pub fn new(writer: W) -> Self {
Self {
writer,
fmt_buf: [0u8; 64],
}
}
/// Write a DXF group code (right-aligned in 3-character field) followed by CRLF.
/// Uses itoa for zero-allocation integer formatting.
#[inline]
fn write_code(&mut self, code: i32) -> Result<()> {
let mut ibuf = itoa::Buffer::new();
let s = ibuf.format(code);
// Right-align in 3-char field: pad with spaces
let pad = if s.len() < 3 { 3 - s.len() } else { 0 };
// Build the line in fmt_buf: spaces + digits + \r\n
let len = pad + s.len() + 2;
self.fmt_buf[..pad].fill(b' ');
self.fmt_buf[pad..pad + s.len()].copy_from_slice(s.as_bytes());
self.fmt_buf[pad + s.len()] = b'\r';
self.fmt_buf[pad + s.len() + 1] = b'\n';
self.writer.write_all(&self.fmt_buf[..len])?;
Ok(())
}
/// Write a value string followed by CRLF.
#[inline]
fn write_value_crlf(&mut self, value: &[u8]) -> Result<()> {
self.writer.write_all(value)?;
self.writer.write_all(b"\r\n")?;
Ok(())
}
/// Format an i16/i32 right-aligned in a 6-character field into fmt_buf.
/// Returns the slice length.
#[inline]
fn format_right6(&mut self, value: i32) -> usize {
let mut ibuf = itoa::Buffer::new();
let s = ibuf.format(value);
let slen = s.len();
let pad = if slen < 6 { 6 - slen } else { 0 };
let total = pad + slen + 2; // +2 for \r\n
self.fmt_buf[..pad].fill(b' ');
self.fmt_buf[pad..pad + slen].copy_from_slice(s.as_bytes());
self.fmt_buf[pad + slen] = b'\r';
self.fmt_buf[pad + slen + 1] = b'\n';
total
}
/// Format f64 with 16 decimal places, trimming trailing zeros (keeping at
/// least one digit after the decimal point). Written directly into fmt_buf
/// to avoid heap allocation.
#[inline]
fn format_double(&mut self, value: f64) -> usize {
use std::io::Cursor;
// For extreme values (|v| >= 1e15), {:.16} may produce strings longer
// than the stack buffer. Fall back to 0.0 for non-finite values and
// use a heap-allocated string for very large magnitudes.
if !value.is_finite() {
// NaN / Infinity → write 0.0
self.fmt_buf[0] = b'0';
self.fmt_buf[1] = b'.';
self.fmt_buf[2] = b'0';
self.fmt_buf[3] = b'\r';
self.fmt_buf[4] = b'\n';
return 5;
}
let abs = value.abs();
if abs > 0.0 && abs < 1e-15 {
// Very small magnitude: {:.16} would lose all significance.
// Use scientific notation with full precision instead.
let s = format!("{:.16e}", value);
let bytes = s.as_bytes();
let total = bytes.len() + 2; // +CRLF
if total <= self.fmt_buf.len() {
self.fmt_buf[..bytes.len()].copy_from_slice(bytes);
self.fmt_buf[bytes.len()] = b'\r';
self.fmt_buf[bytes.len() + 1] = b'\n';
return total;
}
}
if abs >= 1e15 && abs != 0.0 {
// Scientific notation stays compact for every finite f64. Sixteen
// fractional digits provide the 17 significant decimal digits
// needed to round-trip an arbitrary binary64 value.
let s = format!("{:.16e}", value);
let bytes = s.as_bytes();
self.fmt_buf[..bytes.len()].copy_from_slice(bytes);
self.fmt_buf[bytes.len()] = b'\r';
self.fmt_buf[bytes.len() + 1] = b'\n';
return bytes.len() + 2;
}
let mut cursor = Cursor::new(&mut self.fmt_buf[..]);
// Rust's `{}` Display for f64 emits the shortest decimal string that
// round-trips back to the same value, and never uses exponent notation
// for magnitudes in this branch's range (1e-15 ..= 1e15). This matches
// AutoCAD's compact output (e.g. "943.920153") instead of dumping the
// full 16-digit binary expansion of the float ("943.9201530000000275"),
// which trimming trailing zeros cannot clean up because the tail digits
// are rounding noise, not zeros. write! into a Cursor<&mut [u8]> does
// not allocate.
let _ = write!(cursor, "{}", value);
let mut len = cursor.position() as usize;
// `{}` prints whole numbers without a fractional part ("15" not "15.0");
// DXF real values conventionally carry a decimal point, so add ".0".
if !self.fmt_buf[..len].contains(&b'.') {
self.fmt_buf[len] = b'.';
self.fmt_buf[len + 1] = b'0';
len += 2;
}
// Append CRLF
self.fmt_buf[len] = b'\r';
self.fmt_buf[len + 1] = b'\n';
len + 2
}
/// Get the inner writer
pub fn into_inner(self) -> W {
self.writer
}
}
impl<W: Write> DxfStreamWriter for DxfTextWriter<W> {
#[inline]
fn write_string(&mut self, code: i32, value: &str) -> Result<()> {
self.write_code(code)?;
// DXF text format is line-based: literal newlines in string values
// would corrupt the file. Replace them with the MText paragraph
// marker \P which is the standard convention in DXF/DWG ecosystems.
if value.contains('\n') || value.contains('\r') {
let sanitized = value
.replace("\r\n", "\\P")
.replace('\r', "\\P")
.replace('\n', "\\P");
self.write_value_crlf(sanitized.as_bytes())?;
} else {
self.write_value_crlf(value.as_bytes())?;
}
Ok(())
}
fn write_xrecord_string(&mut self, code: i32, value: &str) -> Result<()> {
self.write_code(code)?;
let escaped = value
.replace('^', "^ ")
.replace("\r\n", "^M^J")
.replace('\r', "^M")
.replace('\n', "^J")
.replace('\t', "^I");
self.write_value_crlf(escaped.as_bytes())?;
Ok(())
}
#[inline]
fn write_byte(&mut self, code: i32, value: u8) -> Result<()> {
self.write_code(code)?;
let mut ibuf = itoa::Buffer::new();
let s = ibuf.format(value);
self.write_value_crlf(s.as_bytes())?;
Ok(())
}
#[inline]
fn write_i16(&mut self, code: i32, value: i16) -> Result<()> {
self.write_code(code)?;
let len = self.format_right6(value as i32);
self.writer.write_all(&self.fmt_buf[..len])?;
Ok(())
}
#[inline]
fn write_i32(&mut self, code: i32, value: i32) -> Result<()> {
self.write_code(code)?;
let len = self.format_right6(value);
self.writer.write_all(&self.fmt_buf[..len])?;
Ok(())
}
#[inline]
fn write_i64(&mut self, code: i32, value: i64) -> Result<()> {
self.write_code(code)?;
let mut ibuf = itoa::Buffer::new();
let s = ibuf.format(value);
self.write_value_crlf(s.as_bytes())?;
Ok(())
}
#[inline]
fn write_double(&mut self, code: i32, value: f64) -> Result<()> {
self.write_code(code)?;
let len = self.format_double(value);
self.writer.write_all(&self.fmt_buf[..len])?;
Ok(())
}
#[inline]
fn write_bool(&mut self, code: i32, value: bool) -> Result<()> {
self.write_code(code)?;
// " 0\r\n" or " 1\r\n" — always 8 bytes
if value {
self.writer.write_all(b" 1\r\n")?;
} else {
self.writer.write_all(b" 0\r\n")?;
}
Ok(())
}
fn write_handle(&mut self, code: i32, handle: Handle) -> Result<()> {
self.write_code(code)?;
// Format handle value as uppercase hex directly into fmt_buf
let val = handle.value();
if val == 0 {
self.write_value_crlf(b"0")?;
} else {
// Upper-case hex: max u64 = 16 hex digits
let mut pos = 16usize; // start from end of a 16-byte region
let mut v = val;
while v > 0 {
pos -= 1;
let digit = (v & 0xF) as u8;
self.fmt_buf[pos] = if digit < 10 {
b'0' + digit
} else {
b'A' + digit - 10
};
v >>= 4;
}
let hex_len = 16 - pos;
// Shift to start of buffer and append CRLF
self.fmt_buf.copy_within(pos..16, 0);
self.fmt_buf[hex_len] = b'\r';
self.fmt_buf[hex_len + 1] = b'\n';
self.writer.write_all(&self.fmt_buf[..hex_len + 2])?;
}
Ok(())
}
fn write_binary(&mut self, code: i32, data: &[u8]) -> Result<()> {
self.write_code(code)?;
const HEX_CHARS: &[u8; 16] = b"0123456789ABCDEF";
// Write in chunks using fmt_buf to reduce write calls
let chunk_bytes = self.fmt_buf.len() / 2; // 2 hex chars per byte
for chunk in data.chunks(chunk_bytes) {
let mut pos = 0;
for &byte in chunk {
self.fmt_buf[pos] = HEX_CHARS[(byte >> 4) as usize];
self.fmt_buf[pos + 1] = HEX_CHARS[(byte & 0x0F) as usize];
pos += 2;
}
self.writer.write_all(&self.fmt_buf[..pos])?;
}
self.writer.write_all(b"\r\n")?;
Ok(())
}
fn flush(&mut self) -> Result<()> {
self.writer.flush()?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::super::stream_writer::DxfStreamWriterExt;
use super::*;
use crate::types::Vector3;
#[test]
fn test_write_string() {
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer.write_string(0, "LINE").unwrap();
}
let output = String::from_utf8(buf).unwrap();
assert_eq!(output, " 0\r\nLINE\r\n");
}
#[test]
fn test_write_code_formatting() {
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer.write_i16(5, 100).unwrap();
writer.write_i16(62, 7).unwrap();
writer.write_i16(100, 1).unwrap();
}
let output = String::from_utf8(buf).unwrap();
// Codes should be right-aligned in 3-character field
assert!(output.starts_with(" 5\r\n"));
assert!(output.contains(" 62\r\n"));
assert!(output.contains("100\r\n"));
}
#[test]
fn test_write_point3d() {
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer
.write_point3d(10, Vector3::new(1.0, 2.0, 3.0))
.unwrap();
}
let output = String::from_utf8(buf).unwrap();
assert!(output.contains(" 10\r\n"));
assert!(output.contains("1.0\r\n"));
assert!(output.contains(" 20\r\n"));
assert!(output.contains("2.0\r\n"));
assert!(output.contains(" 30\r\n"));
assert!(output.contains("3.0\r\n"));
}
#[test]
fn test_write_handle() {
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer.write_handle(5, Handle::new(255)).unwrap();
}
let output = String::from_utf8(buf).unwrap();
assert!(output.contains("FF\r\n"));
}
#[test]
fn test_write_double_compact() {
// Regression: the writer used to emit the full 16-decimal expansion of
// the f64 (e.g. "943.9201530000000275"), because trimming trailing
// zeros cannot remove binary rounding noise. It must emit the shortest
// decimal that round-trips to the same value instead.
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer.write_double(10, 943.920153).unwrap();
}
let output = String::from_utf8(buf).unwrap();
assert_eq!(output, " 10\r\n943.920153\r\n");
}
#[test]
fn test_write_double_integer_keeps_point() {
// Whole values must still carry a decimal point ("15.0", not "15"),
// since `{}` prints integers without a fractional part.
let mut buf = Vec::new();
{
let mut writer = DxfTextWriter::new(&mut buf);
writer.write_double(40, 15.0).unwrap();
}
let output = String::from_utf8(buf).unwrap();
assert_eq!(output, " 40\r\n15.0\r\n");
}
}