THotPDF.RegisterLatticeFormGouraudShading Method

 

THotPDF.RegisterLatticeFormGouraudShading

THotPDF

 

Top

Declares a PDF 1.3+ lattice-form Gouraud-shaded triangle mesh shading (ISO 32000-1 8.7.4.5.5, /ShadingType 5) and wraps it in an inline Pattern Type 2 entry on the page Resources/Pattern dictionary. The renderer auto-triangulates each pair of adjacent rows into a triangle strip, so the caller supplies only the vertex grid - useful when the source data already lives on a regular sample grid (terrain, FEA results, scientific heatmaps). Use RegisterFreeFormGouraudShading instead when triangulation is irregular and per-triangle flags are needed.

 

Delphi syntax:

function RegisterLatticeFormGouraudShading(XMin, YMin, XMax, YMax: Single; NumComponents: Integer; VerticesPerRow: Integer; const Vertices: array of Extended): AnsiString;

 

C++ syntax:

AnsiString RegisterLatticeFormGouraudShading(float XMin, float YMin, float XMax, float YMax, int NumComponents, int VerticesPerRow, const Extended* Vertices, int VertexElementCount);

 

Description

The shading is emitted as an indirect stream with /ShadingType 5, /BitsPerCoordinate 16, /BitsPerComponent 8 and /VerticesPerRow N. Unlike Type 4, there is no per-vertex flag byte and /BitsPerFlag is absent from the shading dictionary; each vertex occupies 4 + NumComponents bytes (2 big-endian X + 2 big-endian Y + N 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 ordered row-major: the first VerticesPerRow vertices form row 0, the next VerticesPerRow form row 1, and so on. The renderer connects each pair of adjacent rows (row i and row i+1) into a triangle strip - no flags, no explicit triangle list. The number of Extended values you pass in Vertices must be a multiple of (2 + NumComponents) * VerticesPerRow and the resulting row count must be at least 2.

 

XMin, YMin, XMax, YMax - the user-space rectangle that bounds the lattice. These four values become the first four entries of /Decode. 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.

VerticesPerRow - number of vertices N per lattice row, N >= 2. The dictionary entry /VerticesPerRow takes this value verbatim. The number of rows M is inferred from the array length as VertexCount / VerticesPerRow and must be at least 2.

Vertices - flat array of Extended values in (X, Y, c1...cN) order per vertex, repeated row-major (row 0 first, then row 1, ...). Coordinates are clamped to [XMin, XMax] x [YMin, YMax] and quantised to 16-bit integers; color components are clamped to [0, 1] and quantised to 8-bit integers.

 

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

// 3x3 lattice over a 200x200 quad. The renderer auto-triangulates
// adjacent rows into a triangle strip, giving 4 cells x 2 triangles
// = 8 triangles. Colour layout (visual):
//
//   row 0 (top, Y=YMax): red     yellow  green
//   row 1 (mid):         magenta gray    cyan
//   row 2 (bot, Y=YMin): blue    purple  orange
var
  Doc: THotPDF;
  PatName: AnsiString;
  Verts: array[0..44] of Extended;   // 9 vertices x 5 floats each
  H, XMin, YMin, XMax, YMax, XMid, YMid: 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;
    XMid := (XMin + XMax) / 2;
    YMid := (YMin + YMax) / 2;
    // row 0 (Y=YMax): red - yellow - green
    Verts[ 0] := XMin; Verts[ 1] := YMax; Verts[ 2] := 1.0; Verts[ 3] := 0.0; Verts[ 4] := 0.0;
    Verts[ 5] := XMid; Verts[ 6] := YMax; Verts[ 7] := 1.0; Verts[ 8] := 1.0; Verts[ 9] := 0.0;
    Verts[10] := XMax; Verts[11] := YMax; Verts[12] := 0.0; Verts[13] := 1.0; Verts[14] := 0.0;
    // row 1 (Y mid): magenta - gray - cyan
    Verts[15] := XMin; Verts[16] := YMid; Verts[17] := 1.0; Verts[18] := 0.0; Verts[19] := 1.0;
    Verts[20] := XMid; Verts[21] := YMid; Verts[22] := 0.5; Verts[23] := 0.5; Verts[24] := 0.5;
    Verts[25] := XMax; Verts[26] := YMid; Verts[27] := 0.0; Verts[28] := 1.0; Verts[29] := 1.0;
    // row 2 (Y=YMin): blue - purple - orange
    Verts[30] := XMin; Verts[31] := YMin; Verts[32] := 0.0; Verts[33] := 0.0; Verts[34] := 1.0;
    Verts[35] := XMid; Verts[36] := YMin; Verts[37] := 0.5; Verts[38] := 0.0; Verts[39] := 0.5;
    Verts[40] := XMax; Verts[41] := YMin; Verts[42] := 1.0; Verts[43] := 0.5; Verts[44] := 0.0;
    PatName := Doc.RegisterLatticeFormGouraudShading(
      XMin, YMin, XMax, YMax, 3, 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

RegisterFreeFormGouraudShading, RegisterDeviceN, RegisterSeparation, RegisterLabColorSpace, Version, PDF Filter Support