|
声明一个 PDF 1.3+ 的格点形式 Gouraud 着色三角形网格 shading(ISO 32000-1 8.7.4.5.5,/ShadingType 5),并把它包装成页面 Resources/Pattern 字典中的内联 Pattern Type 2 条目。渲染器会把每对相邻的行自动三角化为三角形带,因此调用方只需提供顶点网格;源数据本来就在规则采样网格上时(地形、有限元分析结果、科学热力图),用它很合适。三角化不规则、需要逐三角形 flag 时,请改用 RegisterFreeFormGouraudShading
Delphi 语法:
function RegisterLatticeFormGouraudShading(XMin, YMin, XMax, YMax: Single; NumComponents: Integer; VerticesPerRow: Integer; const Vertices: array of Extended): AnsiString;
C++ 语法:
AnsiString RegisterLatticeFormGouraudShading(float XMin, float YMin, float XMax, float YMax, int NumComponents, int VerticesPerRow, const Extended* Vertices, int VertexElementCount);
说明
shading 作为带 /ShadingType 5、/BitsPerCoordinate 16、/BitsPerComponent 8 与 /VerticesPerRow N 的间接流输出。不同于 Type 4,没有逐顶点 flag 字节,shading 字典中也没有 /BitsPerFlag;每个顶点占 4 + NumComponents 字节(2 字节大端 X + 2 字节大端 Y + N 个颜色分量)。/Decode 数组把 16 位坐标映射回给定的 [XMin, XMax] x [YMin, YMax] 用户空间矩形,把每个颜色分量映射回 [0, 1]。
顶点按行优先排序:前 VerticesPerRow 个顶点构成 row 0,接下来 VerticesPerRow个构成 row 1,依此类推。渲染器把每对相邻行(row i 与 row i+1)连接成 triangle strip——无 flag,无显式三角形列表。传入的Extended 值数量(在 Vertices 中传入)必须是(2 + NumComponents) * VerticesPerRow 的倍数,且行数至少为 2。
XMin, YMin, XMax, YMax - 限定 lattice 的用户空间矩形。这四个值成为 /Decode。请放在将要绘制的同一用户空间坐标中——对 HotPDF 中位于Rectangle 的屏幕坐标 (X, Y, W, H) Rectangle,对应的 PDF 用户空间矩形是(X, PageHeight - Y - H) 到 (X + W, PageHeight - Y)。
NumComponents - 每个顶点的颜色分量数量。DeviceGray 传 1,DeviceRGB 传 3,DeviceCMYK 传 4。其他值抛出异常。
VerticesPerRow - 每个 lattice 行的顶点数 N,N >= 2。字典条目 /VerticesPerRow 原样使用该值。行数 M 按数组长度推断为 VertexCount / VerticesPerRow,且必须至少为 2。
Vertices - 平面数组,包含Extended 平面数组,按每顶点 (X, Y, c1...cN) 顺序行优先重复(先 row 0,再 row 1……)。坐标钳制到[XMin, XMax] x [YMin, YMax] 并量化为 16 位整数;颜色分量钳制到 [0, 1] 并量化为 8 位整数。
返回值: 自动生成的 pattern 名称(Sh1、Sh2 等),注册在当前页面的 Resources/Pattern 字典。StrictVersionLock 开启且当前 Version 低于 PDF 1.3 时返回空字符串(否则文档版本自动提升到 1.3)。
代码示例
// 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;
另请参阅
RegisterFreeFormGouraudShading, RegisterDeviceN, RegisterSeparation, RegisterLabColorSpace, Version, PDF 过滤器支持
|