Pdf/demo/image_demo.tsl

174 lines
3.5 KiB
Plaintext

Uses TSPdfEnumerations;
{$IFDEF LINUX}
separator := "/";
{$ELSE}
separator := "\\";
{$ENDIF}
alias := "";
path := "";
InitCmdParams(alias, path);
output_file := "image_demo.pdf";
output_file := path + separator + output_file;
pdf := new PdfFile();
pdf.SetCompressionMode(TSPdfEnumerations.COMP_ALL);
// create default-font
font := pdf.GetFont("Helvetica", "");
// add a new page object.
page := pdf.AddPage();
page.SetWidth(550);
page.SetHeight(500);
dst := page.CreateDestination();
dst.SetXYZ(0, page.GetHeight(), 1);
pdf.SetOpenAction(dst);
page.BeginText();
page.SetFontAndSize(font, 20);
page.MoveTextPos(220, page.GetHeight() - 70);
page.ShowText("Image Demo");
page.EndText();
// load image file.
png_path := path + separator + "pngsuite" + separator;
p1 := png_path + "basn3p02.png";
image := pdf.LoadPngImageFromFile("", p1);
// image1 is masked by image2.
image1 := pdf.LoadPngImageFromFile("", p1);
// image2 is a mask image.
p2 := png_path + "basn0g01.png";
image2 := pdf.LoadPngImageFromFile("", p2);
// image3 is a RGB-color image. we use this image for color-mask
p3 := png_path + "maskimage.png";
image3 := pdf.LoadPngImageFromFile("", p3);
iw := image.GetWidth();
ih := image.GetHeight();
page.SetLineWidth(0.5);
x := 100;
y := page.GetHeight() - 150;
// Draw image to the canvas.(normal-mode with actual size.)//
page.DrawImage(image, x, y, iw, ih);
ShowDescription(page, x, y, "Actual Size");
x += 150;
// Scalling image(X direction)
page.DrawImage(image, x, y, iw * 1.5, ih);
ShowDescription(page, x, y, "Scalling image(X direction)");
x += 150;
// Scalling image(Y direction).
page.DrawImage(image, x, y, iw, ih * 1.5);
ShowDescription(page, x, y, "Scalling image(Y direction)");
x := 100;
y -= 120;
// Skewing image.
angle1 := 10;
angle2 := 20;
rad1 := angle1 * 180 * 3.141592;
rad2 := angle2 * 180 * 3.141592;
page.GSave();
page.Concat(iw, tan(rad1) * iw, tan(rad2) * ih, ih, x, y);
page.ExecuteXObject(image);
page.GRestore();
ShowDescription(page, x, y, "Skewing image");
x += 150;
// Rotating image
angle := 30; // rotation of 30 degrees.
rad := angle * 180 * 3.141592; // Calcurate the radian value.
page.GSave();
page.Concat(iw * cos(rad), iw * sin(rad), ih * -sin(rad), ih * cos(rad), x, y);
page.ExecuteXObject(image);
page.GRestore();
ShowDescription(page, x, y, "Rotating image");
x += 150;
// draw masked image.
// Set image2 to the mask image of image1
image1.SetMaskImage(image2);
page.SetRGBFill(0, 0, 0);
page.BeginText();
page.MoveTextPos(x - 6, y + 14);
page.ShowText("MASKMASK");
page.EndText();
page.DrawImage(image1, x - 3, y - 3, iw + 6, ih + 6);
ShowDescription(page, x, y, "masked image");
x := 100;
y -= 120;
// color mask.
page.SetRGBFill(0, 0, 0);
page.BeginText();
page.MoveTextPos(x - 6, y + 14);
page.ShowText("MASKMASK");
page.EndText();
image3.SetColorMask(0, 255, 0, 0, 0, 255);
page.DrawImage(image3, x, y, iw, ih);
ShowDescription(page, x, y, "Color Mask");
err := pdf.SaveToFile(alias, output_file);
echo "SaveToFile::\t", "err := ", format("%x", err), "\toutput_file := ", output_file, "\n";
return;
Function ShowDescription(page, x, y, text)
Begin
page.MoveTo(x, y - 10);
page.LineTo(x, y + 10);
page.MoveTo(x - 10, y);
page.LineTo(x + 10, y);
page.Stroke();
page.SetFontAndSize(page.GetCurrentFont(), 8);
page.SetRGBFill(0, 0, 0);
page.BeginText();
buf := format("(x=%d,y=%d)", x, y);
page.MoveTextPos(x - page.TextWidth(buf) - 5, y - 10);
page.ShowText(buf);
page.EndText();
page.BeginText();
page.MoveTextPos(x - 20, y - 25);
page.ShowText(text);
page.EndText();
End;