繪圖引擎¶
DrawingEngine 提供完整的 PDF 向量繪圖能力,從簡單的矩形線條到複雜的貝茲曲線路徑,以及線性/放射狀漸層填充。所有繪圖操作直接生成最優化的 PDF 圖形操作符,無中間抽象層。
基本形狀¶
use NextPDF\Graphics\DrawingEngine;
use NextPDF\Graphics\StrokeStyle;
use NextPDF\Graphics\FillStyle;
use NextPDF\ValueObjects\Rectangle;
use NextPDF\ValueObjects\Position;
$draw = $document->draw();
// 矩形(填充 + 描邊)
$draw->rectangle(
rect: Rectangle::fromXY(x: 20.0, y: 30.0, width: 80.0, height: 40.0),
fill: FillStyle::solidColor([30, 58, 138]), // #1E3A8A Core blue
stroke: StrokeStyle::solid(color: [0, 0, 0], width: 0.5),
);
// 圓角矩形
$draw->roundedRectangle(
rect: Rectangle::fromXY(x: 20.0, y: 80.0, width: 80.0, height: 40.0),
radius: 5.0,
fill: FillStyle::solidColor([255, 255, 255]),
stroke: StrokeStyle::solid(color: [200, 200, 200], width: 0.3),
);
// 橢圓形
$draw->ellipse(
center: Position::at(x: 60.0, y: 140.0),
radiusX: 30.0,
radiusY: 20.0,
fill: FillStyle::solidColor([217, 119, 6]), // #D97706 Pro amber
);
// 直線
$draw->line(
from: Position::at(x: 20.0, y: 180.0),
to: Position::at(x: 170.0, y: 180.0),
stroke: StrokeStyle::dashed(
color: [100, 100, 100],
width: 0.5,
dashPattern: [3.0, 1.5],
),
);
貝茲曲線與自訂路徑¶
use NextPDF\Graphics\PathBuilder;
// 使用路徑建構器繪製自訂形狀
$path = PathBuilder::create()
->moveTo(x: 50.0, y: 100.0)
->lineTo(x: 100.0, y: 50.0)
->cubicBezierTo(
control1: Position::at(x: 130.0, y: 50.0),
control2: Position::at(x: 150.0, y: 80.0),
end: Position::at(x: 150.0, y: 100.0),
)
->quadraticBezierTo(
control: Position::at(x: 150.0, y: 140.0),
end: Position::at(x: 100.0, y: 150.0),
)
->close();
$draw->path(
path: $path->build(),
fill: FillStyle::solidColor([30, 58, 138]),
stroke: StrokeStyle::none(),
);
漸層填充¶
use NextPDF\Graphics\Gradient\LinearGradient;
use NextPDF\Graphics\Gradient\RadialGradient;
use NextPDF\Graphics\Gradient\ColorStop;
// 線性漸層
$gradient = LinearGradient::create(
from: Position::at(x: 0.0, y: 0.0),
to: Position::at(x: 100.0, y: 0.0),
stops: [
ColorStop::at(offset: 0.0, color: [30, 58, 138]),
ColorStop::at(offset: 1.0, color: [96, 165, 250]),
],
);
$draw->rectangle(
rect: Rectangle::fromXY(x: 20.0, y: 30.0, width: 100.0, height: 50.0),
fill: FillStyle::gradient($gradient),
);
// 放射狀漸層
$radial = RadialGradient::create(
center: Position::at(x: 60.0, y: 60.0),
radius: 40.0,
stops: [
ColorStop::at(offset: 0.0, color: [255, 255, 255]),
ColorStop::at(offset: 1.0, color: [30, 58, 138]),
],
);
線條樣式¶
use NextPDF\Graphics\LineCap;
use NextPDF\Graphics\LineJoin;
$draw->line(
from: Position::at(x: 20.0, y: 50.0),
to: Position::at(x: 170.0, y: 50.0),
stroke: StrokeStyle::create(
color: [0, 0, 0],
width: 2.0,
lineCap: LineCap::Round,
lineJoin: LineJoin::Round,
miterLimit: 10.0,
dashPattern: [5.0, 3.0, 1.0, 3.0], // 長虛線-點-虛線
dashOffset: 0.0,
),
);
圖形狀態儲存與還原¶
// 儲存當前圖形狀態
$draw->save();
// 修改圖形狀態(旋轉、縮放等)
$draw->transform()->rotate(angle: 45.0, center: Position::at(x: 100.0, y: 100.0));
// 繪製旋轉後的形狀
$draw->rectangle(/* ... */);
// 還原圖形狀態
$draw->restore();