|
Declares a PDF 1.3+ free-form Gouraud-shaded triangle mesh shading (ISO 32000-1 8.7.4.5.4, /ShadingType 4) and wraps it in an inline Pattern Type 2 entry on the page Resources/Pattern dictionary. Use the returned pattern name with THPDFPage.SetFillPattern or THPDFPage.SetStrokePattern to paint a vector gradient whose color varies smoothly across each triangle.
Delphi syntax:
function RegisterFreeFormGouraudShading(XMin, YMin, XMax, YMax: Single; NumComponents: Integer; const Vertices: array of Extended): AnsiString;
C++ syntax:
AnsiString RegisterFreeFormGouraudShading(float XMin, float YMin, float XMax, float YMax, int NumComponents, const Extended* Vertices, int VertexElementCount);
Description
The shading is emitted as a single indirect stream with /ShadingType 4, /BitsPerCoordinate 16, /BitsPerComponent 8, and /BitsPerFlag 8. Each vertex is packed in stream order as 8 + NumComponents − 3 bytes - a 1-byte flag (always 0 for independent triangles), 2 big-endian bytes for the X coordinate, 2 big-endian bytes for the Y coordinate, and NumComponents 8-bit color components. The /Decode array maps the 16-bit coordinates back to the supplied [XMin, XMax] x [YMin, YMax] user-space rectangle and each color component back to [0, 1].
Vertices are consumed three at a time as independent triangles. The number of Extended values you pass in Vertices must be exactly (2 + NumComponents) * 3 * TriangleCount; the helper raises an exception when the array length is not a multiple of the stride or when the vertex count is not a multiple of three.
XMin, YMin, XMax, YMax - the user-space rectangle that bounds the mesh. These four values become the first four entries of /Decode so the 16-bit-encoded vertex coordinates map back to PDF user space. Place them in the same user-space coordinates you will paint into - for HotPDF's screen-coordinate Rectangle at (X, Y, W, H) the matching PDF user-space rectangle is (X, PageHeight - Y - H) to (X + W, PageHeight - Y).
NumComponents - number of color components per vertex. Pass 1 for DeviceGray, 3 for DeviceRGB or 4 for DeviceCMYK. Any other value raises an exception. The /ColorSpace entry of the resulting shading is set accordingly.
Vertices - flat array of Extended values in (X, Y, c1...cN) order, repeated for every vertex. Coordinates are clamped to the [XMin, XMax] x [YMin, YMax] box and quantised to 16-bit integers; color components are clamped to [0, 1] and quantised to 8-bit integers. Adjacent triangles that need to share an edge must repeat the shared vertices verbatim because the registered shading uses a flag of 0 for every vertex (independent triangles).
Return value: an auto-generated pattern name (Sh1, Sh2, ...) registered on the current page's Resources/Pattern dictionary. Returns an empty string when StrictVersionLock is on and the active Version is below PDF 1.3 (otherwise the document version auto-bumps to 1.3).
Code Example
// Four-corner colour quad split into two independent triangles
// along the top-left -> bot-right diagonal. The colours blend
// smoothly across the mesh; the diagonal is the ridge where the
// two triangles meet.
//
// top-left red -- top-right green
// | |
// bot-left blue -- bot-right yellow
var
Doc: THotPDF;
PatName: AnsiString;
Verts: array[0..29] of Extended;
H, XMin, YMin, XMax, YMax: Single;
begin
Doc := THotPDF.Create(nil);
try
Doc.Version := pdf14;
Doc.BeginDoc;
H := Doc.CurrentPage.Height;
XMin := 0; YMin := H - 200; XMax := 200; YMax := H;
// triangle A: top-left red, top-right green, bot-right yellow
Verts[ 0] := XMin; Verts[ 1] := YMax; Verts[ 2] := 1.0; Verts[ 3] := 0.0; Verts[ 4] := 0.0;
Verts[ 5] := XMax; Verts[ 6] := YMax; Verts[ 7] := 0.0; Verts[ 8] := 1.0; Verts[ 9] := 0.0;
Verts[10] := XMax; Verts[11] := YMin; Verts[12] := 1.0; Verts[13] := 1.0; Verts[14] := 0.0;
// triangle B: top-left red, bot-right yellow, bot-left blue
Verts[15] := XMin; Verts[16] := YMax; Verts[17] := 1.0; Verts[18] := 0.0; Verts[19] := 0.0;
Verts[20] := XMax; Verts[21] := YMin; Verts[22] := 1.0; Verts[23] := 1.0; Verts[24] := 0.0;
Verts[25] := XMin; Verts[26] := YMin; Verts[27] := 0.0; Verts[28] := 0.0; Verts[29] := 1.0;
PatName := Doc.RegisterFreeFormGouraudShading(
XMin, YMin, XMax, YMax, 3, Verts);
Doc.CurrentPage.SetFillPattern(PatName);
Doc.CurrentPage.Rectangle(0, 0, 200, 200);
Doc.CurrentPage.Fill;
Doc.EndDoc;
finally
Doc.Free;
end;
end;
See Also
RegisterDeviceN, RegisterSeparation, RegisterLabColorSpace, Version, PDF Filter Support
|