Search code examples
pdflib

PDFlib - how can I clear attribute that was set by set_graphics_option()


The code:

$p->set_graphics_option('fillcolor={rgb 0 1 0} strokecolor={rgb 1 0 0} linewidth=2');
$p->rect(100, 300, 300, 200);
$p->fill_stroke();

$p->set_graphics_option('linewidth=1 strokecolor={rgb 1 0 0} linewidth=2');
$p->rect(100, 600, 100, 100);
$p->fill_stroke();

This will render two rectangles, both with red outline (stroke) and filled with green color.

The problem is that second rectangle still "remembers" fill color set by previous set_graphics_option() call - although most recent call does not define fillcolor.

Questions:

  1. Is there something similar to set_graphics_option('fillcolor=none') to draw second rectangle as outline only?
  2. Is there a command that works as opposite of setcolor() - that would unset current color?

Important: I would like to use fill_stroke() to render both rectangles. I am aware that I can use either fill() or stroke().


Solution

  • you should encapsulate it with save restore:

    $p->save();
      $p->set_graphics_option('fillcolor={rgb 0 1 0} strokecolor={rgb 1 0 0} linewidth=2');
      $p->rect(100, 300, 300, 200);
      $p->fill_stroke();
    $p->restore();
    
    $p->save();
      $p->set_graphics_option('linewidth=1 strokecolor={rgb 1 0 0} linewidth=2');
      $p->rect(100, 600, 100, 100);
      $p->fill_stroke();
    $p->restore();
    

    when you set the same option multiple times within a single option list, the last one wins. In this sample, you set first linewidth=1 and then linewidth=2, so the current line width is 2.

    A option is always valid until you reset it, or the related scope stops.

    Is there something similar to set_graphics_option('fillcolor=none') to draw second rectangle as outline only?

    When you don't want a fill the rectangle, please use stroke() instead. When you fill it, you get always the current color. And none is not a valid color.