Files
pdfcraft/crates/prc/src/entity.rs
T
KenM76andClaude Opus 5.5 c571b13469 M12: PRC reader: product tree and styles (#1297)
Walk the PRC product tree into placed, coloured tessellations.

- PrcFile::placements / placements_with(StyleAlpha, EntityOverrides):
  every drawable tessellation reached from the model file's roots, placed
  by its occurrence path's locations and then the representation item's
  local coordinate systems (a 4x4 Matrix) [WD 7.3.10.1, 7.6.3.2].
- Colours: styles and materials from the file structure globals, the
  father/son inheritance bits [WD 7.2.4.2], style transparency combined
  with material alpha by StyleAlpha, per-face styles and topology
  references via Placement::triangle_colours. A prototype's part is
  coloured by its own structure's styles. A colour index counts the colour
  array's doubles (pdf-issues #816).
- Occurrences' entity references: colour, hide or relocate the entity
  they name, in the occurrence's subtree, everywhere, or not at all.
- PrcFile::model_tree: the assembly tree as a model-tree panel lists it,
  with name sources, visibility and each node's placement range.
- TriangleMesh::transformed applies a Matrix: positions, cofactor-mapped
  normals, winding flipped under a mirror.

Never panics: occurrence and prototype cycles are Malformed; depth 64,
2^20 visits, MAX_PLACEMENTS and a face-override ceiling bound the walk;
counts are checked against the remaining data before allocating.
Markups, views, scene lights and clipping planes are Unsupported;
textures are read past.

Field orders the working draft leaves open follow prc-rs (MIT); each is
marked `// prc-rs:`, as are the step-1 container, Double and schema
choices that did.

Adapted from pdfcer (MIT, same author), crates/pdfcer-3d/src/tree and
vec3.rs at 0c9742b5936232af89cd707138ef149dc1f4a660.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SqmHYx8FiWM5PcsoiErgp9
2026-11-02 12:26:44 -05:00

127 lines
4.4 KiB
Rust

//! What every section reader shares: the cursor, the schema, the authoring version, and the
//! base fields every PRC entity starts with [WD 7.2.3, 7.4.3, 8.6].
use crate::PrcError;
use crate::bits::BitReader;
use crate::schema::Schema;
use crate::tree::Graphics;
const ATTRIBUTE: u32 = 201;
/// A [`PrcError::Malformed`] naming `what`.
pub(crate) fn malformed(what: String) -> PrcError {
PrcError::Malformed(what)
}
/// A section reader.
pub(crate) struct Ctx<'a, 's> {
pub(crate) r: BitReader<'a>,
pub(crate) schema: &'s Schema,
/// The file structure's authoring version; gates version-added fields.
pub(crate) version: u32,
/// The current name: what a `same_name` entity reuses; every entity's own name replaces
/// it [WD 7.2.3.4]. Each section starts with none.
pub(crate) name: Option<String>,
/// The PRC unique id of the last `ContentPRCRefBase` read [WD 7.2.3.2].
pub(crate) uid: u32,
/// The current graphics: what a `same_graphics` entity reuses [WD 7.2.4].
pub(crate) graphics: Graphics,
}
impl<'a, 's> Ctx<'a, 's> {
pub(crate) fn new(r: BitReader<'a>, schema: &'s Schema, version: u32) -> Self {
Ctx { r, schema, version, name: None, uid: 0, graphics: Graphics::default() }
}
/// Reads an entity type code; any value but `want` is malformed.
pub(crate) fn expect_type(&mut self, want: u32) -> Result<(), PrcError> {
let t = self.r.unsigned_integer()?;
if t != want {
return Err(malformed(format!("entity type {t} where {want} belongs")));
}
Ok(())
}
/// A count that must fit in the remaining data at `min_bits` per item, which bounds any
/// allocation sized by it.
pub(crate) fn count(&mut self, min_bits: usize, what: &'static str) -> Result<usize, PrcError> {
let n = self.r.unsigned_integer()? as usize;
if n.saturating_mul(min_bits) > self.r.remaining() {
return Err(PrcError::Truncated(what));
}
Ok(n)
}
/// A counted array of `UnsignedInteger`s.
pub(crate) fn uints(&mut self, what: &'static str) -> Result<Vec<u32>, PrcError> {
let n = self.count(1, what)?;
(0..n).map(|_| self.r.unsigned_integer()).collect()
}
/// A counted array of `Double`s.
pub(crate) fn doubles(&mut self, what: &'static str) -> Result<Vec<f64>, PrcError> {
let n = self.count(2, what)?;
(0..n).map(|_| self.r.double()).collect()
}
/// `AttributeEntry` [WD 7.4.3]: a predefined title code or a string.
fn attribute_entry(&mut self) -> Result<(), PrcError> {
if self.r.bit()? {
self.r.unsigned_integer()?;
} else {
self.r.string()?;
}
Ok(())
}
/// One attribute value [WD 7.4.3], read past.
fn attribute_value(&mut self) -> Result<(), PrcError> {
match self.r.unsigned_integer()? {
0 => {}
1 | 3 => {
self.r.integer()?;
}
2 => {
self.r.double()?;
}
4 => {
self.r.string()?;
}
// prc-rs: a 64-bit integer is its high part, then its low part.
5 => {
self.r.integer()?;
self.r.unsigned_integer()?;
}
k => return Err(malformed(format!("attribute value kind {k}"))),
}
Ok(())
}
/// `ContentPRCBase` [WD 7.2.3]: attributes, then the name; returns the entity's name,
/// which is also the current name afterwards.
pub(crate) fn content_prc_base(&mut self) -> Result<Option<String>, PrcError> {
let n = self.count(1, "attributes")?;
for _ in 0..n {
self.expect_type(ATTRIBUTE)?;
self.attribute_entry()?;
let pairs = self.count(2, "attribute pairs")?;
for _ in 0..pairs {
self.attribute_entry()?;
self.attribute_value()?;
}
self.schema.skip_added_fields(ATTRIBUTE, &mut self.r)?;
}
// `same_name` TRUE reuses the current name [WD 7.2.3.4].
if !self.r.bit()? {
self.name = self.r.string()?;
}
Ok(self.name.clone())
}
/// `UserData` [WD 8.6]: a bit count, then that many opaque bits.
pub(crate) fn user_data(&mut self) -> Result<(), PrcError> {
let n = self.r.unsigned_integer()? as usize;
self.r.skip_bits(n)
}
}