2

My PDF file contains following commands:

1.0 0 0 -1.0 0 810.0 cm
1.0 0 0 1.0 0 0 cm
1.0 0 0 1.0 9.0 9.0 cm
-9.0 -9.0 m 621.0 -9.0 l 621.0 801.0 l -9.0 801.0 l h
q
    1.0 1.0 1.0 rg f
Q
q
    1.0 0 0 1.0 0 0 cm
    /Div <</MCID 0>> BDC
    q
        /GS_0-0-0-0x0 gs
        q
            q
                q
                    1.0 0 0 1.0 -25.98 -17.82 cm
                Q
                q
                    1.0 0 0 1.0 -25.98 -17.82 cm
                    0 0 m 666.0 0 l 666.0 144.2 l 0 144.2 l h
                    q
                        .2392 .2784 .3215 rg f
                    Q
                Q
            Q
        Q
    Q

A rectangle is drawn at lines 4 and 20. There is a fill command "f" at lines 6 and 22.

Calling "f" at line 6 clears the current vector path, however, "Q" on a line 7 should return it back. So the line 22 should paint two rectangles, but the PDF viewer draws only one rectangle. My question is, which command exactly clears the first rectangle before the line 22?

Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
  • 1
    PDF isn't PostScript! The q and Q operators in PDF **specifically** do not save the path, because the PDF path is not part of the graphics state. See section 4.4.1 of the PDF 1.7 Reference Manual (top of page 226 in my edition). In PostScript, however, the current path **is** part of the graphics state, and subject to gsave/grestore. As to your question, the fill clears the path. The Q does not restore it because the current path is not subject to gsave and grestore. – KenS May 13 '22 at 13:04
  • Thanks a lot! I overlooked this part and thought it works the same as in PostScript! – Ivan Kuckir May 13 '22 at 15:16

1 Answers1

2

On one hand you can see that q does not save the current path by looking into the specification as KenS has proposed in his comment. If you use the ISO norm (either ISO 32000-1 or ISO 32000-2), you'll find the tables for "Device-Independent Graphics State Parameters" and "Device-Dependent Graphics State Parameters" in section 8.4.1. You'll see that the only path stored there is the clipping path.

But you can also see that q does not save the current path by considering when a q instruction is allowed: You'll find in particular that after defining a path the next instruction must be either a path painting instruction or or a clipping path instruction immediately followed by a path painting instruction. Thus, a path is already used (and, therefore, discarded) before the next q instruction! So q has no chance to save a current path. (For a normative reference have a look at Figure 9 – Graphics Objects – in ISO 32000-1 or ISO 32000-2).


As an aside, the second paragraph above tells you that your examples are invalid, neither the q nor the rg instructions you put between path definition and path painting are allowed.

So concerning your question:

My question is, which command exactly clears the first rectangle before the line 22?

As your example instruction sequence is invalid, the result is undefined. But as there is no element for the current path in the graphics state, you in particular should not expect the path to re-appear after Q.

mkl
  • 90,588
  • 15
  • 125
  • 265