PDFlib - 如何清除由 set_graphics_option() 设置的属性

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

代码:

$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();

这将呈现两个矩形,均带有红色轮廓(描边)并填充绿色。

问题是第二个矩形仍然 "remembers" 填充之前 set_graphics_option() 调用设置的颜色 - 虽然最近的调用没有定义 fillcolor.

问题:

  1. 是否有类似 set_graphics_option('fillcolor=none') 的方法来仅绘制第二个矩形作为轮廓?
  2. 是否有与 setcolor() 相反的命令 - 会取消设置当前颜色?

重要: 我想使用 fill_stroke() 来渲染两个矩形。我知道我可以使用 fill()stroke().

你应该用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();

当您在一个单个 选项列表中多次设置相同的选项时,最后一个获胜。在这个示例中,您先设置 linewidth=1,然后设置 linewidth=2,因此当前线宽为 2。

一个选项在您重置它或相关范围停止之前一直有效。

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

当您不想填充矩形时,请改用 stroke() 。当你填充它时,你总是得到当前的颜色。 none 不是有效的颜色。