PDFlib - 控制文本颜色、文本背景和文本描边的背景和不透明度

PDFlib - controlling background and opacity of text color, text background and text stroke

我正在尝试分别设置文本颜色、文本背景和文本描边(轮廓)的背景和不透明度值。

下面的代码

$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=1 opacitystroke=1')); // Both fill and stroke are opaque
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();

结果:

黄色背景、蓝色字母描边和绿色字母填充不透明 - 正如预期的那样。

将填充和描边的不透明度添加到 gstate 为:

$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=0.3 opacitystroke=0.3'));
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();

所有背景、填充和描边使用相同的不透明度:

问题:

如何分别控制文本背景不透明度(黄色)、字母描边不透明度(蓝色)和字母填充不透明度(绿色)?

这是预期的结果,因为您为 所有 填充和描边内容指定了图形状态。

您应该只为文本设置不透明度 gstate,为火柴盒设置纯色 gstate。

$p->save();
$gstate_solid = $p->create_gstate('opacityfill=1 opacitystroke=1');
$gstate = $p->create_gstate('opacityfill=0.3 opacitystroke=0.3');
$p->fit_textline('QfjIL', 30, 30, 
   'fontname=NotoSerif-Regular encoding=unicode fontsize=240 ' . 
   'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0} gstate=' . $gstate_solid . '} '
   'charspacing=0 textrendering=2 strokewidth=10 position={left top} gstate=' . $gstate . 'fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->restore();

这给出了以下结果,我想这是预期的结果。

您可以在 PDFlib 9.2 API 参考第 6.2 章 "Matchboxes" 中找到有关匹配框选项的所有详细信息。