Skip to main content

folio_content/
ops.rs

1//! PDF content stream operator definitions.
2//!
3//! All ~70 PDF content stream operators are represented as enum variants.
4
5use folio_core::Matrix2D;
6use folio_cos::PdfObject;
7
8/// A parsed content stream operator with its operands.
9#[derive(Debug, Clone)]
10pub enum ContentOp {
11    // --- Graphics State ---
12    /// `q` — Save graphics state
13    SaveState,
14    /// `Q` — Restore graphics state
15    RestoreState,
16    /// `cm` — Concatenate matrix to CTM
17    ConcatMatrix(Matrix2D),
18    /// `w` — Set line width
19    SetLineWidth(f64),
20    /// `J` — Set line cap style (0=butt, 1=round, 2=square)
21    SetLineCap(i32),
22    /// `j` — Set line join style (0=miter, 1=round, 2=bevel)
23    SetLineJoin(i32),
24    /// `M` — Set miter limit
25    SetMiterLimit(f64),
26    /// `d` — Set dash pattern [array, phase]
27    SetDashPattern(Vec<f64>, f64),
28    /// `ri` — Set rendering intent
29    SetRenderingIntent(Vec<u8>),
30    /// `i` — Set flatness tolerance
31    SetFlatness(f64),
32    /// `gs` — Set parameters from graphics state parameter dict
33    SetExtGState(Vec<u8>),
34
35    // --- Path Construction ---
36    /// `m` — Move to (x, y)
37    MoveTo(f64, f64),
38    /// `l` — Line to (x, y)
39    LineTo(f64, f64),
40    /// `c` — Cubic Bézier curve (x1, y1, x2, y2, x3, y3)
41    CurveTo(f64, f64, f64, f64, f64, f64),
42    /// `v` — Cubic Bézier with first control point = current point
43    CurveToInitial(f64, f64, f64, f64),
44    /// `y` — Cubic Bézier with last control point = final point
45    CurveToFinal(f64, f64, f64, f64),
46    /// `h` — Close subpath
47    ClosePath,
48    /// `re` — Rectangle (x, y, w, h)
49    Rectangle(f64, f64, f64, f64),
50
51    // --- Path Painting ---
52    /// `S` — Stroke path
53    Stroke,
54    /// `s` — Close and stroke path
55    CloseAndStroke,
56    /// `f` or `F` — Fill path (non-zero winding)
57    Fill,
58    /// `f*` — Fill path (even-odd rule)
59    FillEvenOdd,
60    /// `B` — Fill and stroke (non-zero winding)
61    FillAndStroke,
62    /// `B*` — Fill and stroke (even-odd rule)
63    FillAndStrokeEvenOdd,
64    /// `b` — Close, fill, and stroke (non-zero winding)
65    CloseFillAndStroke,
66    /// `b*` — Close, fill, and stroke (even-odd rule)
67    CloseFillAndStrokeEvenOdd,
68    /// `n` — End path without filling or stroking (used for clipping)
69    EndPath,
70
71    // --- Clipping ---
72    /// `W` — Set clipping path (non-zero winding)
73    Clip,
74    /// `W*` — Set clipping path (even-odd rule)
75    ClipEvenOdd,
76
77    // --- Text Objects ---
78    /// `BT` — Begin text object
79    BeginText,
80    /// `ET` — End text object
81    EndText,
82
83    // --- Text State ---
84    /// `Tc` — Set character spacing
85    SetCharSpacing(f64),
86    /// `Tw` — Set word spacing
87    SetWordSpacing(f64),
88    /// `Tz` — Set horizontal scaling (percent)
89    SetHorizScaling(f64),
90    /// `TL` — Set text leading
91    SetTextLeading(f64),
92    /// `Tf` — Set font and size (font_name, size)
93    SetFont(Vec<u8>, f64),
94    /// `Tr` — Set text rendering mode
95    SetTextRenderMode(i32),
96    /// `Ts` — Set text rise
97    SetTextRise(f64),
98
99    // --- Text Positioning ---
100    /// `Td` — Move text position (tx, ty)
101    MoveTextPos(f64, f64),
102    /// `TD` — Move text position and set leading (tx, ty)
103    MoveTextPosSetLeading(f64, f64),
104    /// `Tm` — Set text matrix
105    SetTextMatrix(Matrix2D),
106    /// `T*` — Move to start of next line
107    NextLine,
108
109    // --- Text Showing ---
110    /// `Tj` — Show text string
111    ShowText(Vec<u8>),
112    /// `TJ` — Show text with positioning adjustments [(string, adjustment), ...]
113    ShowTextAdjusted(Vec<TextOp>),
114    /// `'` — Move to next line and show text
115    NextLineShowText(Vec<u8>),
116    /// `"` — Set word/char spacing, move to next line, show text
117    SetSpacingNextLineShowText(f64, f64, Vec<u8>),
118
119    // --- Color ---
120    /// `CS` — Set stroke color space
121    SetStrokeColorSpace(Vec<u8>),
122    /// `cs` — Set fill color space
123    SetFillColorSpace(Vec<u8>),
124    /// `SC` or `SCN` — Set stroke color
125    SetStrokeColor(Vec<f64>),
126    /// `sc` or `scn` — Set fill color
127    SetFillColor(Vec<f64>),
128    /// `G` — Set stroke gray
129    SetStrokeGray(f64),
130    /// `g` — Set fill gray
131    SetFillGray(f64),
132    /// `RG` — Set stroke RGB
133    SetStrokeRGB(f64, f64, f64),
134    /// `rg` — Set fill RGB
135    SetFillRGB(f64, f64, f64),
136    /// `K` — Set stroke CMYK
137    SetStrokeCMYK(f64, f64, f64, f64),
138    /// `k` — Set fill CMYK
139    SetFillCMYK(f64, f64, f64, f64),
140
141    // --- XObject ---
142    /// `Do` — Paint XObject (name)
143    PaintXObject(Vec<u8>),
144
145    // --- Shading ---
146    /// `sh` — Paint shading pattern
147    PaintShading(Vec<u8>),
148
149    // --- Inline Image ---
150    /// `BI`...`ID`...`EI` — Inline image
151    InlineImage {
152        dict: Vec<(Vec<u8>, PdfObject)>,
153        data: Vec<u8>,
154    },
155
156    // --- Marked Content ---
157    /// `MP` — Marked content point (tag)
158    MarkedContentPoint(Vec<u8>),
159    /// `DP` — Marked content point with properties (tag, properties)
160    MarkedContentPointProperties(Vec<u8>, PdfObject),
161    /// `BMC` — Begin marked content (tag)
162    BeginMarkedContent(Vec<u8>),
163    /// `BDC` — Begin marked content with properties (tag, properties)
164    BeginMarkedContentProperties(Vec<u8>, PdfObject),
165    /// `EMC` — End marked content
166    EndMarkedContent,
167
168    // --- Compatibility ---
169    /// `BX` — Begin compatibility section
170    BeginCompat,
171    /// `EX` — End compatibility section
172    EndCompat,
173
174    /// Unknown operator
175    Unknown(Vec<u8>, Vec<PdfObject>),
176}
177
178/// A text operation within a TJ array.
179#[derive(Debug, Clone)]
180pub enum TextOp {
181    /// A text string to show.
182    Text(Vec<u8>),
183    /// A positioning adjustment (negative = move right, positive = move left).
184    Adjustment(f64),
185}
186
187/// Path segment types for path data.
188#[derive(Debug, Clone, Copy, PartialEq)]
189pub enum PathSegment {
190    MoveTo,
191    LineTo,
192    CurveTo,
193    ClosePath,
194    Rectangle,
195}