mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
Follow-ups to #2697 from the review in #3423: - ATTRIBUTE_FILL is an AcGiFillType trait (1 always, 2 never) in force until the next one, for polygons and circles; a filled shape keeps its outline. Before, a 2 turned fill on, and only for the next polygon. - A circle or arc under a matrix that scales its plane unevenly is an ellipse or elliptical arc; 3-point chunks are fitted in their plane. - The size group is the first 92/160 after the AcDbProxyEntity marker. - Only what the 310 groups hold is reserved, not the size claimed.
921 lines
42 KiB
Rust
921 lines
42 KiB
Rust
//! The proxy graphics an ACAD_PROXY_ENTITY carries (DXF groups 92/160 and 310): the picture
|
||
//! the owning application left for programs that don't have it, drawn as AutoCAD draws it with
|
||
//! PROXYSHOW 1. The layout follows the Open Design Alliance's public DWG specification
|
||
//! ("Proxy Entity Graphics"): a header (total size, chunk count), then chunks of (size, type,
|
||
//! data), sizes in bytes including the 8-byte chunk header, all little-endian.
|
||
//!
|
||
//! The picture becomes ordinary, read-only entities: drawn and measured like the contents of a
|
||
//! block, with layer 0, ByBlock colour, linetype and lineweight standing for the proxy's own
|
||
//! until a chunk sets them. Chunks CADCraft doesn't decode (meshes, shells, lightweight
|
||
//! polylines, materials) are skipped.
|
||
|
||
use super::{Arc, Circle, Common, Ellipse, Entity, EntityKind, Handle, Hatch, HatchLoop, Line, Lineweight, LwPolyline, Text, Unknown};
|
||
use cadcraft_color::{Color, Rgb};
|
||
use cadcraft_geom::{PolyVertex, Vec2, Vec3};
|
||
|
||
/// Bytes of proxy graphics read at most from one entity.
|
||
const MAX_BYTES: usize = 16 << 20;
|
||
/// Entities made at most from one entity's proxy graphics.
|
||
const MAX_ENTITIES: usize = 100_000;
|
||
/// Vertices read at most from one polyline or polygon chunk.
|
||
const MAX_VERTICES: usize = 1_000_000;
|
||
/// Matrices pushed at most.
|
||
const MAX_MATRICES: usize = 64;
|
||
|
||
/// The table names proxy graphics refer to by index, in the order of the file's tables.
|
||
#[derive(Clone, Debug, Default)]
|
||
pub struct ProxyTables {
|
||
/// LAYER records in file order.
|
||
pub layers: Vec<String>,
|
||
/// LTYPE records in file order, without ByBlock and ByLayer (indices count only the others).
|
||
pub linetypes: Vec<String>,
|
||
/// Text styles (name, font file) in file order: a text chunk names its font, and the first
|
||
/// style with that font draws it.
|
||
pub styles: Vec<(String, String)>,
|
||
}
|
||
|
||
impl Unknown {
|
||
/// The proxy graphics bytes of an ACAD_PROXY_ENTITY: the 310 groups after its size group
|
||
/// (92, or 160 in newer files), cut to that size. The size group is the first after the
|
||
/// `AcDbProxyEntity` subclass marker (from the start in a record without one), so a 92 or
|
||
/// 160 among the entity's own groups before it isn't taken for it. Empty for other records,
|
||
/// for an entity without graphics and for one whose data isn't hex.
|
||
pub fn proxy_graphics_bytes(&self) -> Vec<u8> {
|
||
if !self.dxf_type.eq_ignore_ascii_case("ACAD_PROXY_ENTITY") {
|
||
return Vec::new();
|
||
}
|
||
let from = self.tags.iter().position(|t| t.code == 100 && t.value.trim() == "AcDbProxyEntity").map_or(0, |m| m + 1);
|
||
let Some(at) = self.tags.iter().skip(from).position(|t| t.code == 92 || t.code == 160).map(|i| from + i) else { return Vec::new() };
|
||
let size = self.tags.get(at).and_then(|t| t.value.trim().parse::<usize>().ok()).unwrap_or(0).min(MAX_BYTES);
|
||
// Reserve what the 310 groups hold, not what the size claims: a size far past the data
|
||
// would otherwise reserve up to MAX_BYTES for nothing.
|
||
let held: usize = self.tags.iter().skip(at + 1).take_while(|t| t.code == 310).map(|t| t.value.trim().len() / 2).sum();
|
||
let mut out = Vec::with_capacity(size.min(held));
|
||
for t in self.tags.iter().skip(at + 1).take_while(|t| t.code == 310) {
|
||
// Damaged data draws nothing rather than a picture cut at a guess.
|
||
if !push_hex(&mut out, &t.value) {
|
||
return Vec::new();
|
||
}
|
||
if out.len() >= size {
|
||
break;
|
||
}
|
||
}
|
||
out.truncate(size);
|
||
out
|
||
}
|
||
}
|
||
|
||
/// Append the bytes of hex string `s`. False on a character that isn't a hex digit.
|
||
fn push_hex(out: &mut Vec<u8>, s: &str) -> bool {
|
||
let digits: Vec<u8> = s.trim().bytes().collect();
|
||
for pair in digits.chunks(2) {
|
||
let (Some(&hi), Some(&lo)) = (pair.first(), pair.get(1)) else { return false };
|
||
let (Some(hi), Some(lo)) = ((hi as char).to_digit(16), (lo as char).to_digit(16)) else { return false };
|
||
out.push((hi * 16 + lo) as u8);
|
||
}
|
||
true
|
||
}
|
||
|
||
/// The entities the proxy graphics `bytes` draw, all with `handle` (the proxy's): selecting one
|
||
/// selects the proxy.
|
||
pub fn proxy_entities(bytes: &[u8], tables: &ProxyTables, handle: Handle) -> Vec<Entity> {
|
||
let mut r = Decoder {
|
||
tables,
|
||
handle,
|
||
pen: Common { layer: "0".into(), color: Color::ByBlock, linetype: "ByBlock".into(), lineweight: Lineweight::ByBlock, ..Common::default() },
|
||
fill: false,
|
||
matrices: Vec::new(),
|
||
out: Vec::new(),
|
||
};
|
||
let mut at = 8;
|
||
while let (Some(size), Some(kind)) = (u32_at(bytes, at), u32_at(bytes, at + 4)) {
|
||
let size = size as usize;
|
||
if size < 8 || r.out.len() >= MAX_ENTITIES {
|
||
break;
|
||
}
|
||
let Some(data) = bytes.get(at + 8..at.saturating_add(size)) else { break };
|
||
r.chunk(kind, data);
|
||
at += size;
|
||
}
|
||
r.out
|
||
}
|
||
|
||
struct Decoder<'a> {
|
||
tables: &'a ProxyTables,
|
||
handle: Handle,
|
||
/// Layer, colour, linetype and lineweight for the entities that follow.
|
||
pen: Common,
|
||
/// Whether polygons and circles that follow are filled (ATTRIBUTE_FILL).
|
||
fill: bool,
|
||
/// Model transforms pushed by PUSH_MATRIX, innermost last (row-major 3x4 of the 4x4).
|
||
matrices: Vec<[f64; 12]>,
|
||
out: Vec<Entity>,
|
||
}
|
||
|
||
impl Decoder<'_> {
|
||
/// Decode one chunk of type `kind`: draw it, or change the pen for what follows.
|
||
fn chunk(&mut self, kind: u32, data: &[u8]) {
|
||
let mut b = Bytes { data, at: 0 };
|
||
match kind {
|
||
// CIRCLE: centre, radius, normal.
|
||
2 => {
|
||
if let (Some(c), Some(r)) = (b.point(), b.f64()) {
|
||
self.circle(c, r);
|
||
}
|
||
}
|
||
// CIRCLE_3P: three points on it.
|
||
3 => {
|
||
if let (Some(p1), Some(p2), Some(p3)) = (b.point(), b.point(), b.point())
|
||
&& let Some((c, r)) = circle_through(p1.xy(), p2.xy(), p3.xy())
|
||
{
|
||
self.circle(c.to3(p1.z), r);
|
||
}
|
||
}
|
||
// CIRCULAR_ARC: centre, radius, normal, start vector, sweep angle (around the normal).
|
||
4 => {
|
||
if let (Some(c), Some(r), Some(n), Some(v), Some(sweep)) = (b.point(), b.f64(), b.point(), b.point(), b.f64()) {
|
||
self.arc(c, r, n, v, sweep);
|
||
}
|
||
}
|
||
// CIRCULAR_ARC_3P: start, a point on the arc, end.
|
||
5 => {
|
||
if let (Some(p1), Some(p2), Some(p3)) = (b.point(), b.point(), b.point()) {
|
||
self.arc_3p_local(p1, p2, p3);
|
||
}
|
||
}
|
||
// POLYLINE and POLYGON: a count, then the points.
|
||
6 | 7 => {
|
||
let Some(n) = b.u32() else { return };
|
||
let n = (n as usize).min(MAX_VERTICES);
|
||
let pts: Vec<Vec2> = (0..n).map_while(|_| b.point()).map(|p| self.xy(p)).collect();
|
||
self.polyline(pts, kind == 7);
|
||
}
|
||
// TEXT: position, normal, direction, height, width factor, oblique, string.
|
||
10 => {
|
||
if let (Some(p), Some(_), Some(dir), Some(h), Some(w), Some(o)) = (b.point(), b.point(), b.point(), b.f64(), b.f64(), b.f64()) {
|
||
let s = b.string();
|
||
self.text(TextChunk { p, dir, height: h, width: w, oblique: o, value: s, font: String::new() });
|
||
}
|
||
}
|
||
// TEXT2: position, normal, direction, string, length (the characters drawn), raw
|
||
// flag, height, width factor, oblique, tracking, five style flags, font, big font.
|
||
11 => {
|
||
if let (Some(p), Some(_), Some(dir)) = (b.point(), b.point(), b.point()) {
|
||
let s = b.string();
|
||
let s = first_chars(s, b.u32());
|
||
b.skip(4);
|
||
if let (Some(h), Some(w), Some(o)) = (b.f64(), b.f64(), b.f64()) {
|
||
b.skip(8 + 20);
|
||
let font = b.string();
|
||
self.text(TextChunk { p, dir, height: h, width: w, oblique: o, value: s, font });
|
||
}
|
||
}
|
||
}
|
||
// UNICODE_TEXT: as TEXT with a UTF-16 string.
|
||
36 => {
|
||
if let (Some(p), Some(_), Some(dir), Some(h), Some(w), Some(o)) = (b.point(), b.point(), b.point(), b.f64(), b.f64(), b.f64()) {
|
||
let s = b.unicode();
|
||
self.text(TextChunk { p, dir, height: h, width: w, oblique: o, value: s, font: String::new() });
|
||
}
|
||
}
|
||
// UNICODE_TEXT2: as TEXT2 with UTF-16 strings, and bold, italic, charset, pitch and
|
||
// the typeface before the font file.
|
||
38 => {
|
||
if let (Some(p), Some(_), Some(dir)) = (b.point(), b.point(), b.point()) {
|
||
let s = b.unicode();
|
||
let s = first_chars(s, b.u32());
|
||
b.skip(4);
|
||
if let (Some(h), Some(w), Some(o)) = (b.f64(), b.f64(), b.f64()) {
|
||
b.skip(8 + 20 + 16);
|
||
let _typeface = b.unicode();
|
||
let font = b.unicode();
|
||
self.text(TextChunk { p, dir, height: h, width: w, oblique: o, value: s, font });
|
||
}
|
||
}
|
||
}
|
||
// ATTRIBUTE_COLOR: ACI, 0 ByBlock, 256 ByLayer.
|
||
14 => {
|
||
if let Some(c) = b.u32() {
|
||
self.pen.color = Color::from_aci(i16::try_from(c).unwrap_or(256));
|
||
}
|
||
}
|
||
// ATTRIBUTE_LAYER: index into the layer table; an index past it leaves the layer.
|
||
16 => {
|
||
if let Some(name) = b.u32().and_then(|i| self.tables.layers.get(i as usize)) {
|
||
self.pen.layer = name.clone();
|
||
}
|
||
}
|
||
// ATTRIBUTE_LINETYPE: index into the linetypes other than ByBlock and ByLayer;
|
||
// 32766 is ByBlock, 32767 and anything past the table ByLayer.
|
||
18 => {
|
||
if let Some(i) = b.u32() {
|
||
self.pen.linetype = match i {
|
||
32766 => "ByBlock".into(),
|
||
_ => self.tables.linetypes.get(i as usize).cloned().unwrap_or_else(|| "ByLayer".into()),
|
||
};
|
||
}
|
||
}
|
||
// ATTRIBUTE_FILL: a trait like the colour, in force until the next one: 1 fills the
|
||
// polygons and circles that follow, 2 (and anything else) doesn't (AcGiFillType
|
||
// kAcGiFillAlways, kAcGiFillNever).
|
||
20 => {
|
||
if let Some(f) = b.u32() {
|
||
self.fill = f == 1;
|
||
}
|
||
}
|
||
// ATTRIBUTE_TRUE_COLOR: a colour with its method in the top byte.
|
||
22 => {
|
||
if let Some(v) = b.u32() {
|
||
self.pen.color = match v >> 24 {
|
||
0xC0 => Color::ByLayer,
|
||
0xC1 => Color::ByBlock,
|
||
0xC2 => Color::True(Rgb::from_u32(v & 0x00FF_FFFF)),
|
||
0xC3 => Color::from_aci((v & 0xFF) as i16),
|
||
_ => self.pen.color,
|
||
};
|
||
}
|
||
}
|
||
// ATTRIBUTE_LINEWEIGHT: hundredths of a millimetre, or -1 ByLayer, -2 ByBlock,
|
||
// -3 the default.
|
||
23 => {
|
||
if let Some(v) = b.u32() {
|
||
self.pen.lineweight = Lineweight::from_dxf(i16::try_from(v as i32).unwrap_or(-1));
|
||
}
|
||
}
|
||
// ATTRIBUTE_LTSCALE.
|
||
24 => {
|
||
if let Some(s) = b.f64().filter(|s| s.is_finite() && *s > 0.0) {
|
||
self.pen.ltscale = s;
|
||
}
|
||
}
|
||
// PUSH_MATRIX, PUSH_MATRIX2: a 4x4 model transform for what follows; POP_MATRIX.
|
||
29 | 30 => {
|
||
let m: Vec<f64> = (0..16).map_while(|_| b.f64()).collect();
|
||
if let Some(m) = m.get(..12).and_then(|m| <[f64; 12]>::try_from(m).ok())
|
||
&& m.iter().all(|x| x.is_finite())
|
||
&& self.matrices.len() < MAX_MATRICES
|
||
{
|
||
self.matrices.push(m);
|
||
}
|
||
}
|
||
31 => {
|
||
self.matrices.pop();
|
||
}
|
||
_ => {}
|
||
}
|
||
}
|
||
|
||
/// Add an entity drawn with the current pen (up to [`MAX_ENTITIES`]).
|
||
fn push(&mut self, kind: EntityKind) {
|
||
if self.out.len() < MAX_ENTITIES {
|
||
self.out.push(Entity { handle: self.handle, common: self.pen.clone(), kind });
|
||
}
|
||
}
|
||
|
||
/// `p` through the pushed matrices, innermost first, in the XY plane.
|
||
fn xy(&self, p: Vec3) -> Vec2 {
|
||
let mut p = p;
|
||
for m in self.matrices.iter().rev() {
|
||
p = Vec3::new(
|
||
m[0] * p.x + m[1] * p.y + m[2] * p.z + m[3],
|
||
m[4] * p.x + m[5] * p.y + m[6] * p.z + m[7],
|
||
m[8] * p.x + m[9] * p.y + m[10] * p.z + m[11],
|
||
);
|
||
}
|
||
p.xy()
|
||
}
|
||
|
||
/// How much the pushed matrices scale lengths (1 without any).
|
||
fn scale(&self) -> f64 {
|
||
self.matrices.iter().map(|m| (m[0] * m[5] - m[1] * m[4]).abs().sqrt()).product()
|
||
}
|
||
|
||
/// Where the pushed matrices take the unit vectors of the chunk's plane at `c`: the circle of
|
||
/// radius 1 around `c` is drawn as `c' + u·cos a + v·sin a`.
|
||
fn axes(&self, c: Vec3) -> (Vec2, Vec2, Vec2) {
|
||
let at = self.xy(c);
|
||
(at, self.xy(c + Vec3::new(1.0, 0.0, 0.0)) - at, self.xy(c + Vec3::new(0.0, 1.0, 0.0)) - at)
|
||
}
|
||
|
||
/// A full circle around `c`, through the pushed matrices: an ellipse when they scale its
|
||
/// plane unevenly or shear it. While ATTRIBUTE_FILL is on, a solid hatch under it: AutoCAD
|
||
/// draws a filled shape's outline too, with its lineweight.
|
||
fn circle(&mut self, c: Vec3, r: f64) {
|
||
let (at, u, v) = self.axes(c);
|
||
if !(r.is_finite() && r > 0.0 && at.is_finite()) {
|
||
return;
|
||
}
|
||
match round(u * r, v * r) {
|
||
Some(r) => {
|
||
if self.fill {
|
||
// Two half-circle arcs (bulge 1).
|
||
let side = |p: Vec2| PolyVertex { p, bulge: 1.0, start_width: 0.0, end_width: 0.0 };
|
||
self.solid(vec![side(at + Vec2::new(r, 0.0)), side(at - Vec2::new(r, 0.0))]);
|
||
}
|
||
self.push(EntityKind::Circle(Circle { center: at.to3(0.0), radius: r }));
|
||
}
|
||
None => {
|
||
if let Some(e) = ellipse(at, u * r, v * r, 0.0, std::f64::consts::TAU) {
|
||
self.push(EntityKind::Ellipse(e));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// An arc around `c` from the direction `start`, turned by `sweep` around `normal`.
|
||
fn arc(&mut self, c: Vec3, r: f64, normal: Vec3, start: Vec3, sweep: f64) {
|
||
if !(r.is_finite() && r > 0.0 && sweep.is_finite() && sweep != 0.0) || start.xy().len() < 1e-12 {
|
||
return;
|
||
}
|
||
// The start point and the end point (the start vector turned by the sweep around the
|
||
// normal: clockwise in plan for a normal pointing down), through the matrices.
|
||
let dir = if normal.z < 0.0 { -1.0 } else { 1.0 };
|
||
let a0 = start.y.atan2(start.x);
|
||
let a1 = a0 + dir * sweep;
|
||
if sweep.abs() >= std::f64::consts::TAU - 1e-9 {
|
||
return self.circle(c, r);
|
||
}
|
||
self.arc_between(c, r, a0, a1);
|
||
}
|
||
|
||
/// The arc of radius `r` around `c` (in the chunk's plane) from angle `a0` to `a1`, either
|
||
/// way round, through the pushed matrices: an elliptical arc when they don't keep it round.
|
||
fn arc_between(&mut self, c: Vec3, r: f64, a0: f64, a1: f64) {
|
||
let (at, u, v) = self.axes(c);
|
||
let on = |a: f64| at + u * (r * a.cos()) + v * (r * a.sin());
|
||
if round(u * r, v * r).is_some() {
|
||
return self.arc_3p(on(a0), on((a0 + a1) / 2.0), on(a1));
|
||
}
|
||
if let Some(e) = ellipse(at, u * r, v * r, a0.min(a1), a0.max(a1)) {
|
||
self.push(EntityKind::Ellipse(e));
|
||
}
|
||
}
|
||
|
||
/// The arc from `p1` through `p2` to `p3`, given in the chunk's plane (`z` is the plane's):
|
||
/// fitted there and drawn through the pushed matrices, which may make it elliptical.
|
||
fn arc_3p_local(&mut self, p1: Vec3, p2: Vec3, p3: Vec3) {
|
||
if self.matrices.is_empty() {
|
||
return self.arc_3p(p1.xy(), p2.xy(), p3.xy());
|
||
}
|
||
let Some((c, r)) = circle_through(p1.xy(), p2.xy(), p3.xy()) else {
|
||
return self.polyline(vec![self.xy(p1), self.xy(p3)], false);
|
||
};
|
||
let ang = |p: Vec3| (p.y - c.y).atan2(p.x - c.x);
|
||
let (a1, a2, a3) = (ang(p1), ang(p2), ang(p3));
|
||
let ccw = |from: f64, to: f64| (to - from).rem_euclid(std::f64::consts::TAU);
|
||
// Counter-clockwise from p1 to p3 when p2 lies on that way, else from p3 to p1.
|
||
let (from, sweep) = if ccw(a1, a2) <= ccw(a1, a3) { (a1, ccw(a1, a3)) } else { (a3, ccw(a3, a1)) };
|
||
self.arc_between(c.to3(p1.z), r, from, from + sweep);
|
||
}
|
||
|
||
/// The arc from `p1` through `p2` to `p3`.
|
||
fn arc_3p(&mut self, p1: Vec2, p2: Vec2, p3: Vec2) {
|
||
let Some((c, r)) = circle_through(p1, p2, p3) else {
|
||
return self.polyline(vec![p1, p3], false);
|
||
};
|
||
let ang = |p: Vec2| (p.y - c.y).atan2(p.x - c.x);
|
||
let (a1, a2, a3) = (ang(p1), ang(p2), ang(p3));
|
||
let ccw = |from: f64, to: f64| (to - from).rem_euclid(std::f64::consts::TAU);
|
||
// Counter-clockwise from p1 to p3 when p2 lies on that way, else from p3 to p1.
|
||
let (start, end) = if ccw(a1, a2) <= ccw(a1, a3) { (a1, a3) } else { (a3, a1) };
|
||
self.push(EntityKind::Arc(Arc { center: c.to3(0.0), radius: r, start, end }));
|
||
}
|
||
|
||
/// A polyline, or a closed polygon: on a solid hatch while ATTRIBUTE_FILL is on (the outline
|
||
/// is drawn too, as for a circle).
|
||
fn polyline(&mut self, pts: Vec<Vec2>, polygon: bool) {
|
||
if pts.iter().any(|p| !p.is_finite()) || pts.is_empty() {
|
||
return;
|
||
}
|
||
if polygon && self.fill && pts.len() >= 3 {
|
||
let vertices = pts.iter().map(|&p| PolyVertex { p, bulge: 0.0, start_width: 0.0, end_width: 0.0 }).collect();
|
||
self.solid(vertices);
|
||
}
|
||
match pts.as_slice() {
|
||
[a, b] if !polygon => self.push(EntityKind::Line(Line { a: a.to3(0.0), b: b.to3(0.0) })),
|
||
_ => {
|
||
let vertices = pts.iter().map(|&p| PolyVertex { p, bulge: 0.0, start_width: 0.0, end_width: 0.0 }).collect();
|
||
self.push(EntityKind::LwPolyline(LwPolyline { vertices, closed: polygon, const_width: 0.0, elevation: 0.0, plinegen: false }));
|
||
}
|
||
}
|
||
}
|
||
|
||
/// A solid hatch with the one boundary `vertices`.
|
||
fn solid(&mut self, vertices: Vec<PolyVertex>) {
|
||
self.push(EntityKind::Hatch(Hatch {
|
||
pattern: "SOLID".into(),
|
||
solid: true,
|
||
loops: vec![HatchLoop { vertices, outer: true, boundary_flags: None, sources: Vec::new() }],
|
||
scale: 1.0,
|
||
angle: 0.0,
|
||
associative: false,
|
||
style: 0,
|
||
elevation: 0.0,
|
||
gradient: None,
|
||
origin: Vec2::ZERO,
|
||
background: None,
|
||
pattern_lines: Vec::new(),
|
||
}));
|
||
}
|
||
|
||
/// A single-line text, its style the first one whose font file the chunk names.
|
||
fn text(&mut self, t: TextChunk) {
|
||
let TextChunk { p, dir, height, width, oblique, value, font } = t;
|
||
let font = font.as_str();
|
||
let height = height * self.scale();
|
||
if !(height.is_finite() && height > 0.0) || value.is_empty() {
|
||
return;
|
||
}
|
||
let at = self.xy(p);
|
||
if !at.is_finite() {
|
||
return;
|
||
}
|
||
let towards = self.xy(p + dir) - at;
|
||
let rotation = if towards.len() > 1e-12 { towards.y.atan2(towards.x) } else { 0.0 };
|
||
let stem = |f: &str| f.trim().rsplit(['/', '\\']).next().unwrap_or_default().split('.').next().unwrap_or_default().to_ascii_lowercase();
|
||
let style = self
|
||
.tables
|
||
.styles
|
||
.iter()
|
||
.find(|(_, f)| !font.is_empty() && stem(f) == stem(font))
|
||
.map(|(n, _)| n.clone())
|
||
.unwrap_or_else(|| "Standard".into());
|
||
self.push(EntityKind::Text(Text {
|
||
insert: at.to3(0.0),
|
||
align_pt: None,
|
||
height,
|
||
value,
|
||
rotation,
|
||
width_factor: if width.is_finite() && width > 0.0 { width } else { 1.0 },
|
||
oblique: if oblique.is_finite() { oblique } else { 0.0 },
|
||
style,
|
||
halign: Default::default(),
|
||
valign: Default::default(),
|
||
}));
|
||
}
|
||
}
|
||
|
||
/// The first `length` characters of `s`: a text chunk may carry more text than it draws (the
|
||
/// rest of a note, drawn by the chunks after it). No length, or one past the end, keeps it all.
|
||
fn first_chars(s: String, length: Option<u32>) -> String {
|
||
match length.map(|n| n as usize) {
|
||
Some(n) if n > 0 && n < s.chars().count() => s.chars().take(n).collect(),
|
||
_ => s,
|
||
}
|
||
}
|
||
|
||
/// A text chunk: position, direction, height, width factor, oblique, string and font file.
|
||
struct TextChunk {
|
||
p: Vec3,
|
||
dir: Vec3,
|
||
height: f64,
|
||
width: f64,
|
||
oblique: f64,
|
||
value: String,
|
||
font: String,
|
||
}
|
||
|
||
/// The radius of the circle with conjugate radii `u` and `v` when they are one: as long as each
|
||
/// other and at right angles (a matrix that turns, mirrors and scales evenly).
|
||
fn round(u: Vec2, v: Vec2) -> Option<f64> {
|
||
let (lu, lv) = (u.len(), v.len());
|
||
let tol = 1e-9 * lu.max(lv);
|
||
((lu - lv).abs() <= tol && u.dot(v).abs() <= tol * lu.max(lv) && lu.is_finite() && lu > 0.0).then_some(lu)
|
||
}
|
||
|
||
/// The ellipse `c + u·cos a + v·sin a` for `a` from `a0` to `a1` (counter-clockwise in the
|
||
/// parameter): its axes from the conjugate radii `u` and `v`, the arc carried over to the
|
||
/// ellipse's own parameter (counter-clockwise from its major axis).
|
||
fn ellipse(c: Vec2, u: Vec2, v: Vec2, a0: f64, a1: f64) -> Option<Ellipse> {
|
||
use std::f64::consts::{FRAC_PI_2, TAU};
|
||
// c + u·cos a + v·sin a = c + p·cos(a − t) + q·sin(a − t): p and q at right angles.
|
||
let t = 0.5 * (2.0 * u.dot(v)).atan2(u.len2() - v.len2());
|
||
let p = u * t.cos() + v * t.sin();
|
||
let q = v * t.cos() - u * t.sin();
|
||
// The ellipse turns counter-clockwise with its parameter only when q is p turned left;
|
||
// a mirroring matrix reverses it.
|
||
let mirrored = p.cross(q) < 0.0;
|
||
let q = if mirrored { -q } else { q };
|
||
let (mut s0, mut s1) = if mirrored { (t - a1, t - a0) } else { (a0 - t, a1 - t) };
|
||
// The major axis is the longer of p and q.
|
||
let (major, minor) = if q.len() > p.len() {
|
||
s0 -= FRAC_PI_2;
|
||
s1 -= FRAC_PI_2;
|
||
(q, p.len())
|
||
} else {
|
||
(p, q.len())
|
||
};
|
||
let ratio = minor / major.len();
|
||
if !(c.is_finite() && major.is_finite() && major.len() > 0.0 && ratio.is_finite() && ratio > 0.0) {
|
||
return None;
|
||
}
|
||
let full = (a1 - a0) >= TAU - 1e-9;
|
||
let (start, end) = if full { (0.0, TAU) } else { (s0.rem_euclid(TAU), s1.rem_euclid(TAU)) };
|
||
Some(Ellipse { center: c.to3(0.0), major: major.to3(0.0), ratio, start, end })
|
||
}
|
||
|
||
/// The circle through three points (centre, radius), if they aren't on one line.
|
||
fn circle_through(a: Vec2, b: Vec2, c: Vec2) -> Option<(Vec2, f64)> {
|
||
let d = 2.0 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
|
||
if !d.is_finite() || d.abs() < 1e-12 {
|
||
return None;
|
||
}
|
||
let (a2, b2, c2) = (a.x * a.x + a.y * a.y, b.x * b.x + b.y * b.y, c.x * c.x + c.y * c.y);
|
||
let center =
|
||
Vec2::new((a2 * (b.y - c.y) + b2 * (c.y - a.y) + c2 * (a.y - b.y)) / d, (a2 * (c.x - b.x) + b2 * (a.x - c.x) + c2 * (b.x - a.x)) / d);
|
||
let r = (a - center).len();
|
||
(center.is_finite() && r.is_finite() && r > 0.0).then_some((center, r))
|
||
}
|
||
|
||
/// The little-endian u32 at `at`, if `bytes` holds one there.
|
||
fn u32_at(bytes: &[u8], at: usize) -> Option<u32> {
|
||
bytes.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes)
|
||
}
|
||
|
||
/// A reader over one chunk's data. Strings are padded to four bytes.
|
||
struct Bytes<'a> {
|
||
data: &'a [u8],
|
||
at: usize,
|
||
}
|
||
|
||
impl Bytes<'_> {
|
||
/// The next `n` bytes, if there are that many.
|
||
fn take(&mut self, n: usize) -> Option<&[u8]> {
|
||
let s = self.data.get(self.at..self.at.checked_add(n)?)?;
|
||
self.at += n;
|
||
Some(s)
|
||
}
|
||
/// Pass over `n` bytes (reads after the end then find nothing).
|
||
fn skip(&mut self, n: usize) {
|
||
self.at = self.at.saturating_add(n);
|
||
}
|
||
/// A little-endian u32.
|
||
fn u32(&mut self) -> Option<u32> {
|
||
self.take(4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes)
|
||
}
|
||
/// A little-endian double.
|
||
fn f64(&mut self) -> Option<f64> {
|
||
self.take(8).and_then(|b| b.try_into().ok()).map(f64::from_le_bytes)
|
||
}
|
||
/// Three doubles, if all are finite.
|
||
fn point(&mut self) -> Option<Vec3> {
|
||
let p = Vec3::new(self.f64()?, self.f64()?, self.f64()?);
|
||
(p.x.is_finite() && p.y.is_finite() && p.z.is_finite()).then_some(p)
|
||
}
|
||
/// Move on to the next multiple of four bytes, where padded strings end.
|
||
fn align(&mut self) {
|
||
self.at = self.at.saturating_add((4 - self.at % 4) % 4);
|
||
}
|
||
/// A zero-terminated string in the drawing's code page; bytes above 0x7F as Latin-1.
|
||
fn string(&mut self) -> String {
|
||
let rest = self.data.get(self.at..).unwrap_or_default();
|
||
let end = rest.iter().position(|&c| c == 0).unwrap_or(rest.len());
|
||
let s: String = rest.iter().take(end).map(|&c| c as char).collect();
|
||
self.at += end + 1;
|
||
self.align();
|
||
s
|
||
}
|
||
/// A UTF-16 string ended by a zero code unit.
|
||
fn unicode(&mut self) -> String {
|
||
let rest = self.data.get(self.at..).unwrap_or_default();
|
||
let (pairs, _) = rest.as_chunks::<2>();
|
||
let units: Vec<u16> = pairs.iter().map(|&p| u16::from_le_bytes(p)).take_while(|&u| u != 0).collect();
|
||
self.at += units.len() * 2 + 2;
|
||
self.align();
|
||
String::from_utf16_lossy(&units)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::RawTag;
|
||
|
||
/// A chunk of type `kind`, its data padded to four bytes.
|
||
fn chunk(kind: u32, data: &[u8]) -> Vec<u8> {
|
||
let mut d = data.to_vec();
|
||
d.resize(d.len().div_ceil(4) * 4, 0);
|
||
let mut out = ((8 + d.len()) as u32).to_le_bytes().to_vec();
|
||
out.extend(kind.to_le_bytes());
|
||
out.extend(d);
|
||
out
|
||
}
|
||
/// Proxy graphics: the header (total size, chunk count), then the chunks.
|
||
fn graphics(chunks: &[Vec<u8>]) -> Vec<u8> {
|
||
let body: Vec<u8> = chunks.concat();
|
||
let mut out = ((8 + body.len()) as u32).to_le_bytes().to_vec();
|
||
out.extend((chunks.len() as u32).to_le_bytes());
|
||
out.extend(body);
|
||
out
|
||
}
|
||
/// A point in the XY plane as three doubles.
|
||
fn pt(x: f64, y: f64) -> Vec<u8> {
|
||
[x, y, 0.0].iter().flat_map(|v| v.to_le_bytes()).collect()
|
||
}
|
||
/// Two layers, two linetypes and two text styles.
|
||
fn tables() -> ProxyTables {
|
||
ProxyTables {
|
||
layers: vec!["0".into(), "BALLOONS".into()],
|
||
linetypes: vec!["Continuous".into(), "DASHED".into()],
|
||
styles: vec![("Standard".into(), "txt".into()), ("ISO".into(), "isocp.shx".into())],
|
||
}
|
||
}
|
||
|
||
/// A balloon as AutoCAD Mechanical stores one: layer, colour, a circle, the leader and the
|
||
/// number as TEXT2.
|
||
#[test]
|
||
fn a_balloon_becomes_circle_line_and_text() {
|
||
let mut text2 = pt(173.0, 112.0);
|
||
text2.extend(pt(0.0, 0.0).iter().take(16).chain(1.0f64.to_le_bytes().iter()));
|
||
text2.extend(pt(1.0, 0.0));
|
||
text2.extend(b"12\0\0");
|
||
text2.extend(2i32.to_le_bytes());
|
||
text2.extend(0i32.to_le_bytes());
|
||
for v in [6.0f64, 1.0, 0.0, 0.0] {
|
||
text2.extend(v.to_le_bytes());
|
||
}
|
||
text2.extend([0u8; 20]);
|
||
text2.extend(b"ISOCP.SHX\0\0\0\0\0");
|
||
let mut circle = pt(175.0, 115.0);
|
||
circle.extend(6.0f64.to_le_bytes());
|
||
circle.extend(pt(0.0, 0.0).iter().take(16).chain(1.0f64.to_le_bytes().iter()));
|
||
let mut leader = 2u32.to_le_bytes().to_vec();
|
||
leader.extend(pt(170.0, 111.0));
|
||
leader.extend(pt(150.0, 90.0));
|
||
let g = graphics(&[chunk(16, &1u32.to_le_bytes()), chunk(14, &1u32.to_le_bytes()), chunk(2, &circle), chunk(6, &leader), chunk(11, &text2)]);
|
||
let es = proxy_entities(&g, &tables(), Handle(0x35));
|
||
assert_eq!(es.len(), 3);
|
||
assert!(es.iter().all(|e| e.handle == Handle(0x35) && e.common.layer == "BALLOONS" && e.common.color == Color::Index(1)));
|
||
assert!(matches!(&es[0].kind, EntityKind::Circle(c) if c.center == Vec3::new(175.0, 115.0, 0.0) && c.radius == 6.0));
|
||
assert!(matches!(&es[1].kind, EntityKind::Line(l) if l.b == Vec3::new(150.0, 90.0, 0.0)));
|
||
assert!(matches!(&es[2].kind, EntityKind::Text(t) if t.value == "12" && t.height == 6.0 && t.style == "ISO" && t.rotation == 0.0));
|
||
}
|
||
|
||
/// A TEXT2 chunk draws only as many characters as its length says.
|
||
#[test]
|
||
fn text2_draws_its_length() {
|
||
let text2 = |value: &[u8], length: i32| {
|
||
let mut t = pt(0.0, 0.0);
|
||
t.extend([0.0f64, 0.0, 1.0].iter().flat_map(|v| v.to_le_bytes()));
|
||
t.extend(pt(1.0, 0.0));
|
||
t.extend(value);
|
||
t.extend(vec![0u8; 4 - value.len() % 4]);
|
||
t.extend(length.to_le_bytes());
|
||
t.extend(1i32.to_le_bytes());
|
||
for v in [2.5f64, 1.0, 0.0, 0.0] {
|
||
t.extend(v.to_le_bytes());
|
||
}
|
||
t.extend([0u8; 20]);
|
||
t.extend(b"txt\0");
|
||
let es = proxy_entities(&graphics(&[chunk(11, &t)]), &tables(), Handle(1));
|
||
match es.first().map(|e| &e.kind) {
|
||
Some(EntityKind::Text(t)) => t.value.clone(),
|
||
other => panic!("{other:?}"),
|
||
}
|
||
};
|
||
assert_eq!(text2(b"two lines\\Pof text", 9), "two lines");
|
||
assert_eq!(text2(b"whole", 0), "whole");
|
||
assert_eq!(text2(b"whole", 99), "whole");
|
||
}
|
||
|
||
/// Without layer and colour chunks the picture takes the proxy's own (layer 0, ByBlock).
|
||
#[test]
|
||
fn unset_traits_are_the_proxys() {
|
||
let mut leader = 3u32.to_le_bytes().to_vec();
|
||
for (x, y) in [(0.0, 0.0), (10.0, 0.0), (10.0, 5.0)] {
|
||
leader.extend(pt(x, y));
|
||
}
|
||
let es = proxy_entities(
|
||
&graphics(&[chunk(6, &leader), chunk(16, &u32::MAX.to_le_bytes()), chunk(18, &32766u32.to_le_bytes())]),
|
||
&tables(),
|
||
Handle(1),
|
||
);
|
||
let e = es.first().expect("polyline");
|
||
assert_eq!(
|
||
(e.common.layer.as_str(), e.common.color, e.common.linetype.as_str(), e.common.lineweight),
|
||
("0", Color::ByBlock, "ByBlock", Lineweight::ByBlock)
|
||
);
|
||
assert!(matches!(&e.kind, EntityKind::LwPolyline(p) if p.vertices.len() == 3 && !p.closed));
|
||
}
|
||
|
||
/// ATTRIBUTE_FILL is a trait like the colour: 1 (kAcGiFillAlways) fills every polygon and
|
||
/// circle until a 2 (kAcGiFillNever) ends it, whatever is drawn in between. AutoCAD
|
||
/// Mechanical writes 2 before its balloons' circles and 1 before their arrowheads and dots.
|
||
#[test]
|
||
fn fill_is_a_trait_until_turned_off() {
|
||
let mut tri = 3u32.to_le_bytes().to_vec();
|
||
for (x, y) in [(0.0, 0.0), (4.0, 0.0), (2.0, 3.0)] {
|
||
tri.extend(pt(x, y));
|
||
}
|
||
let mut line = 2u32.to_le_bytes().to_vec();
|
||
line.extend(pt(0.0, 0.0));
|
||
line.extend(pt(1.0, 0.0));
|
||
let mut dot = pt(5.0, 5.0);
|
||
dot.extend(0.5f64.to_le_bytes());
|
||
dot.extend(pt(0.0, 0.0));
|
||
let (on, off) = (chunk(20, &1u32.to_le_bytes()), chunk(20, &2u32.to_le_bytes()));
|
||
let es = proxy_entities(
|
||
&graphics(&[on, chunk(6, &line), chunk(7, &tri), chunk(2, &dot), chunk(7, &tri), off, chunk(7, &tri), chunk(2, &dot)]),
|
||
&tables(),
|
||
Handle(1),
|
||
);
|
||
let kinds: Vec<&str> = es.iter().map(|e| e.kind.type_name()).collect();
|
||
// A filled shape is a solid hatch with its outline on top.
|
||
assert_eq!(kinds, ["Line", "Hatch", "Polyline", "Hatch", "Circle", "Hatch", "Polyline", "Polyline", "Circle"]);
|
||
assert!(matches!(&es[1].kind, EntityKind::Hatch(h) if h.solid && h.loops[0].vertices.len() == 3));
|
||
assert!(matches!(&es[2].kind, EntityKind::LwPolyline(p) if p.closed));
|
||
// The filled circle: two half-circle arcs around its centre.
|
||
let disc: Vec<(Vec2, f64)> = match &es[3].kind {
|
||
EntityKind::Hatch(h) => h.loops[0].vertices.iter().map(|v| (v.p, v.bulge)).collect(),
|
||
other => panic!("{other:?}"),
|
||
};
|
||
assert_eq!(disc, [(Vec2::new(5.5, 5.0), 1.0), (Vec2::new(4.5, 5.0), 1.0)]);
|
||
assert!(matches!(&es[6].kind, EntityKind::LwPolyline(p) if p.closed));
|
||
}
|
||
|
||
/// A circle or arc under a matrix that scales its plane unevenly is an ellipse, measured
|
||
/// and snapped as one; one that scales evenly stays a circle or arc.
|
||
#[test]
|
||
fn circles_and_arcs_under_an_uneven_matrix_are_ellipses() {
|
||
use std::f64::consts::{FRAC_PI_2, PI, TAU};
|
||
let push = |sx: f64, sy: f64| {
|
||
let mut m = [0.0f64; 16];
|
||
(m[0], m[5], m[10], m[15], m[3]) = (sx, sy, 1.0, 1.0, 10.0);
|
||
chunk(29, &m.iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>())
|
||
};
|
||
let mut circle = pt(0.0, 0.0);
|
||
circle.extend(1.0f64.to_le_bytes());
|
||
circle.extend(pt(0.0, 0.0));
|
||
let quarter = {
|
||
let mut a = pt(0.0, 0.0);
|
||
a.extend(1.0f64.to_le_bytes());
|
||
a.extend([0.0f64, 0.0, 1.0].iter().flat_map(|v| v.to_le_bytes()));
|
||
a.extend(pt(1.0, 0.0));
|
||
a.extend(FRAC_PI_2.to_le_bytes());
|
||
a.extend(0u32.to_le_bytes());
|
||
a
|
||
};
|
||
// Three points of the same quarter arc, as CIRCULAR_ARC_3P gives them.
|
||
let h = std::f64::consts::FRAC_1_SQRT_2;
|
||
let quarter_3p: Vec<u8> = [pt(1.0, 0.0), pt(h, h), pt(0.0, 1.0)].concat();
|
||
let near = |a: f64, b: f64| (a - b).abs() < 1e-9;
|
||
let ell = |e: &Entity| match &e.kind {
|
||
EntityKind::Ellipse(e) => (e.center, e.major, e.ratio, e.start, e.end),
|
||
other => panic!("{other:?}"),
|
||
};
|
||
let es = proxy_entities(&graphics(&[push(2.0, 1.0), chunk(2, &circle), chunk(4, &quarter), chunk(5, &quarter_3p)]), &tables(), Handle(1));
|
||
let (c, major, ratio, start, end) = ell(&es[0]);
|
||
assert!(c == Vec3::new(10.0, 0.0, 0.0) && near(major.x.abs(), 2.0) && near(major.y, 0.0) && near(ratio, 0.5) && (start, end) == (0.0, TAU));
|
||
for e in &es[1..] {
|
||
let (_, major, ratio, start, end) = ell(e);
|
||
// The major axis may point either way along x; the arc runs from (12, 0) to (10, 1).
|
||
let at = |t: f64| Vec2::new(10.0, 0.0) + major.xy() * t.cos() + major.xy().perp() * (ratio * t.sin());
|
||
assert!((at(start) - Vec2::new(12.0, 0.0)).len() < 1e-9 && (at(end) - Vec2::new(10.0, 1.0)).len() < 1e-9, "{start} {end}");
|
||
assert!(near((end - start).rem_euclid(TAU), FRAC_PI_2));
|
||
}
|
||
// Mirrored along x and stretched: the quarter arc runs from (10, 1) to (8, 0).
|
||
let es = proxy_entities(&graphics(&[push(-2.0, 1.0), chunk(4, &quarter)]), &tables(), Handle(1));
|
||
let (_, major, ratio, start, end) = ell(&es[0]);
|
||
let at = |t: f64| Vec2::new(10.0, 0.0) + major.xy() * t.cos() + major.xy().perp() * (ratio * t.sin());
|
||
assert!((at(start) - Vec2::new(10.0, 1.0)).len() < 1e-9 && (at(end) - Vec2::new(8.0, 0.0)).len() < 1e-9, "{start} {end}");
|
||
// Even scaling, mirrored or not, keeps circles and arcs.
|
||
let es = proxy_entities(&graphics(&[push(-3.0, 3.0), chunk(2, &circle), chunk(4, &quarter), chunk(5, &quarter_3p)]), &tables(), Handle(1));
|
||
assert!(matches!(&es[0].kind, EntityKind::Circle(c) if near(c.radius, 3.0)));
|
||
for e in &es[1..] {
|
||
let angle = |a: f64, b: f64| near((a - b).rem_euclid(TAU).min((b - a).rem_euclid(TAU)), 0.0);
|
||
assert!(matches!(&e.kind, EntityKind::Arc(a) if near(a.radius, 3.0) && angle(a.start, FRAC_PI_2) && angle(a.end, PI)), "{e:?}");
|
||
}
|
||
}
|
||
|
||
/// A quarter arc counter-clockwise from 0°, and the same sweep around a downward normal,
|
||
/// which runs clockwise in plan (from 0° to -90°).
|
||
#[test]
|
||
fn arcs_follow_their_normal() {
|
||
let arc = |nz: f64| {
|
||
let mut a = pt(0.0, 0.0);
|
||
a.extend(2.0f64.to_le_bytes());
|
||
a.extend([0.0f64, 0.0, nz].iter().flat_map(|v| v.to_le_bytes()));
|
||
a.extend(pt(1.0, 0.0));
|
||
a.extend(std::f64::consts::FRAC_PI_2.to_le_bytes());
|
||
a.extend(0u32.to_le_bytes());
|
||
match proxy_entities(&graphics(&[chunk(4, &a)]), &tables(), Handle(1)).first().map(|e| e.kind.clone()) {
|
||
Some(EntityKind::Arc(a)) => (a.start, a.end, a.radius),
|
||
other => panic!("{other:?}"),
|
||
}
|
||
};
|
||
let (s, e, r) = arc(1.0);
|
||
assert!(s.abs() < 1e-9 && (e - std::f64::consts::FRAC_PI_2).abs() < 1e-9 && (r - 2.0).abs() < 1e-9);
|
||
let (s, e, _) = arc(-1.0);
|
||
assert!((s + std::f64::consts::FRAC_PI_2).abs() < 1e-9 && e.abs() < 1e-9, "{s} {e}");
|
||
}
|
||
|
||
#[test]
|
||
fn a_pushed_matrix_moves_what_follows_until_popped() {
|
||
let mut m = [0.0f64; 16];
|
||
(m[0], m[5], m[10], m[15], m[3], m[7]) = (2.0, 2.0, 1.0, 1.0, 100.0, 50.0);
|
||
let mut circle = pt(1.0, 1.0);
|
||
circle.extend(1.0f64.to_le_bytes());
|
||
circle.extend(pt(0.0, 0.0));
|
||
let push: Vec<u8> = m.iter().flat_map(|v| v.to_le_bytes()).collect();
|
||
let es = proxy_entities(&graphics(&[chunk(29, &push), chunk(2, &circle), chunk(31, &[]), chunk(2, &circle)]), &tables(), Handle(1));
|
||
assert!(matches!(&es[0].kind, EntityKind::Circle(c) if c.center == Vec3::new(102.0, 52.0, 0.0) && c.radius == 2.0));
|
||
assert!(matches!(&es[1].kind, EntityKind::Circle(c) if c.center == Vec3::new(1.0, 1.0, 0.0) && c.radius == 1.0));
|
||
}
|
||
|
||
/// A finite matrix can still move a point past the largest float: a circle or a text that
|
||
/// would land there is left out, as a polyline is, so no infinity reaches the extents.
|
||
#[test]
|
||
fn what_a_matrix_moves_out_of_range_is_left_out() {
|
||
let mut m = [0.0f64; 16];
|
||
(m[0], m[5], m[10], m[15], m[3]) = (1.0, 1.0, 1.0, 1.0, 1.7e308);
|
||
let push: Vec<u8> = m.iter().flat_map(|v| v.to_le_bytes()).collect();
|
||
let mut circle = pt(1e308, 0.0);
|
||
circle.extend(1.0f64.to_le_bytes());
|
||
circle.extend(pt(0.0, 0.0));
|
||
let mut text = pt(1e308, 0.0);
|
||
text.extend([0.0f64, 0.0, 1.0].iter().flat_map(|v| v.to_le_bytes()));
|
||
text.extend(pt(1.0, 0.0));
|
||
text.extend(b"far\0");
|
||
text.extend(0i32.to_le_bytes());
|
||
text.extend(1i32.to_le_bytes());
|
||
for v in [2.5f64, 1.0, 0.0, 0.0] {
|
||
text.extend(v.to_le_bytes());
|
||
}
|
||
text.extend([0u8; 20]);
|
||
text.extend(b"txt\0");
|
||
let es = proxy_entities(&graphics(&[chunk(29, &push), chunk(2, &circle), chunk(11, &text)]), &tables(), Handle(1));
|
||
assert!(es.is_empty(), "{es:?}");
|
||
// The same text without the matrix is drawn: the chunk itself is sound.
|
||
assert_eq!(proxy_entities(&graphics(&[chunk(11, &text)]), &tables(), Handle(1)).len(), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn the_bytes_come_from_the_310_groups_after_the_size() {
|
||
let tag = |code: i32, value: &str| RawTag { code, value: value.into() };
|
||
let u = Unknown {
|
||
dxf_type: "ACAD_PROXY_ENTITY".into(),
|
||
tags: vec![tag(90, "498"), tag(92, "5"), tag(310, "0102"), tag(310, "030405FF"), tag(93, "0")],
|
||
graphics: Vec::new(),
|
||
};
|
||
assert_eq!(u.proxy_graphics_bytes(), [1, 2, 3, 4, 5]);
|
||
let other = Unknown { dxf_type: "ACAD_PROXY_OBJECT".into(), ..u.clone() };
|
||
assert!(other.proxy_graphics_bytes().is_empty());
|
||
let bad = Unknown { tags: vec![tag(92, "4"), tag(310, "01zz")], ..u.clone() };
|
||
assert!(bad.proxy_graphics_bytes().is_empty());
|
||
// The size group is the first after the AcDbProxyEntity marker: a 160 or 92 before it
|
||
// belongs to the entity's common data.
|
||
let marked = Unknown {
|
||
tags: vec![
|
||
tag(100, "AcDbEntity"),
|
||
tag(160, "2"),
|
||
tag(310, "EEEE"),
|
||
tag(100, "AcDbProxyEntity"),
|
||
tag(90, "498"),
|
||
tag(92, "3"),
|
||
tag(310, "010203"),
|
||
],
|
||
..u.clone()
|
||
};
|
||
assert_eq!(marked.proxy_graphics_bytes(), [1, 2, 3]);
|
||
// A size far past the data gives the data there is.
|
||
let short = Unknown { tags: vec![tag(92, "16000000"), tag(310, "0102")], ..u };
|
||
assert_eq!(short.proxy_graphics_bytes(), [1, 2]);
|
||
}
|
||
|
||
/// Truncated chunks, sizes past the end, huge counts and garbage draw what they can and
|
||
/// never panic.
|
||
#[test]
|
||
fn hostile_graphics_do_not_panic() {
|
||
let mut huge = u32::MAX.to_le_bytes().to_vec();
|
||
huge.extend(pt(1.0, 1.0));
|
||
let mut nan = pt(f64::NAN, 0.0);
|
||
nan.extend(1.0f64.to_le_bytes());
|
||
let cases: Vec<Vec<u8>> = vec![
|
||
Vec::new(),
|
||
vec![0; 7],
|
||
graphics(&[chunk(6, &huge)]),
|
||
graphics(&[chunk(2, &nan)]),
|
||
graphics(&[chunk(11, &[0xFF; 30])]),
|
||
graphics(&[chunk(38, &[0x41; 77])]),
|
||
graphics(&[chunk(29, &[1; 40])]),
|
||
graphics(&[chunk(31, &[]), chunk(31, &[])]),
|
||
[graphics(&[chunk(2, &pt(0.0, 0.0))]), vec![0xFF; 9]].concat(),
|
||
{
|
||
let mut g = graphics(&[]);
|
||
g.extend(4u32.to_le_bytes());
|
||
g.extend(2u32.to_le_bytes());
|
||
g
|
||
},
|
||
{
|
||
let mut g = graphics(&[]);
|
||
g.extend(1_000u32.to_le_bytes());
|
||
g.extend(2u32.to_le_bytes());
|
||
g
|
||
},
|
||
];
|
||
for g in cases {
|
||
let _ = proxy_entities(&g, &tables(), Handle(1));
|
||
}
|
||
}
|
||
}
|