Skip to content

Commit cba8c6f

Browse files
committed
Track q nesting depth
Pdf-writer doesn't enforce it, but it's quite useful for downstream users that want to enforce it
1 parent 4546ae6 commit cba8c6f

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/content.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::*;
33
/// A builder for a content stream.
44
pub struct Content {
55
buf: Vec<u8>,
6+
q_depth: usize,
67
}
78

89
/// Core methods.
@@ -16,7 +17,7 @@ impl Content {
1617

1718
/// Create a new content stream with the specified initial buffer capacity.
1819
pub fn with_capacity(capacity: usize) -> Self {
19-
Self { buf: Vec::with_capacity(capacity) }
20+
Self { buf: Vec::with_capacity(capacity), q_depth: 0 }
2021
}
2122

2223
/// Start writing an arbitrary operation.
@@ -243,16 +244,24 @@ impl Content {
243244
#[inline]
244245
pub fn save_state(&mut self) -> &mut Self {
245246
self.op("q");
247+
self.q_depth = self.q_depth.saturating_add(1);
246248
self
247249
}
248250

249251
/// `Q`: Restore the graphics state from the stack.
250252
#[inline]
251253
pub fn restore_state(&mut self) -> &mut Self {
252254
self.op("Q");
255+
self.q_depth = self.q_depth.saturating_sub(1);
253256
self
254257
}
255258

259+
/// The current `q` nesting depth.
260+
#[inline]
261+
pub fn state_nesting_depth(&self) -> usize {
262+
self.q_depth
263+
}
264+
256265
/// `cm`: Pre-concatenate the `matrix` with the current transformation
257266
/// matrix.
258267
#[inline]

0 commit comments

Comments
 (0)