'PDF commands - does the q - Q save the path?

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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkl