390 lines
8.9 KiB
Plaintext
390 lines
8.9 KiB
Plaintext
Uses TSPdfEnumerations;
|
|
{$IFDEF LINUX}
|
|
separator := "/";
|
|
{$ELSE}
|
|
separator := "\\";
|
|
{$ENDIF}
|
|
|
|
alias := "";
|
|
path := "";
|
|
InitCmdParams(alias, path);
|
|
output_file := "text_demo.pdf";
|
|
output_file := path + separator + output_file;
|
|
|
|
page_title := "Text Demo";
|
|
pdf := new PdfFile();
|
|
|
|
// set compression mode
|
|
pdf.SetCompressionMode(TSPdfEnumerations.COMP_ALL);
|
|
|
|
// create default-font
|
|
font := pdf.GetFont("Helvetica", "");
|
|
|
|
// add a new page object.
|
|
page := pdf.AddPage();
|
|
|
|
// draw grid to the page
|
|
PrintGrid(pdf, page);
|
|
|
|
width := page.GetWidth();
|
|
height := page.GetHeight();
|
|
|
|
// print the title of the page (with positioning center).
|
|
page.SetFontAndSize(font, 24);
|
|
tw := page.TextWidth(page_title);
|
|
page.BeginText();
|
|
page.TextOut((width - tw) / 2, height - 50, page_title);
|
|
page.EndText();
|
|
|
|
page.BeginText();
|
|
page.MoveTextPos(60, height - 60);
|
|
|
|
// font size
|
|
fsize := 8;
|
|
samp_text := "abcdefgABCDEFG123!#$%&+-@?";
|
|
samp_text2 := "The quick brown fox jumps over the lazy dog.";
|
|
while (fsize < 60) do
|
|
begin
|
|
// set style and size of font.
|
|
page.SetFontAndSize(font, fsize);
|
|
|
|
// set the position of the text.
|
|
page.MoveTextPos(0, -5 - fsize);
|
|
|
|
// measure the number of characters which included in the page.
|
|
len := page.MeasureText(samp_text, width - 120, false, "");
|
|
buf := samp_text[:len];
|
|
page.ShowText(buf);
|
|
|
|
// print the description.
|
|
page.MoveTextPos(0, -10);
|
|
page.SetFontAndSize(font, 8);
|
|
page.ShowText("Fontsize=" $ fsize);
|
|
|
|
fsize *= 1.5;
|
|
end
|
|
|
|
// font color
|
|
page.SetFontAndSize(font, 8);
|
|
page.MoveTextPos(0, -30);
|
|
page.ShowText("Font color");
|
|
|
|
page.SetFontAndSize(font, 18);
|
|
page.MoveTextPos(0, -20);
|
|
len := length(samp_text);
|
|
for i:=0 to len - 1 do
|
|
begin
|
|
r := i / len;
|
|
g := 1 - i / len;
|
|
buf := samp_text[integer(i + 1)];
|
|
page.SetRGBFill(r, g, 0.0);
|
|
page.ShowText(buf);
|
|
end
|
|
page.MoveTextPos(0, -25);
|
|
|
|
for i:=0 to len - 1 do
|
|
begin
|
|
r := i / len;
|
|
b := 1 - i / len;
|
|
buf := samp_text[integer(i + 1)];
|
|
|
|
page.SetRGBFill(r, 0.0, b);
|
|
page.ShowText(buf);
|
|
end
|
|
page.MoveTextPos(0, -25);
|
|
|
|
for i:=0 to len - 1 do
|
|
begin
|
|
b := i / len;
|
|
g := 1 - i / len;
|
|
buf := samp_text[integer(i + 1)];
|
|
|
|
page.SetRGBFill(0.0, g, b);
|
|
page.ShowText(buf);
|
|
end
|
|
page.EndText();
|
|
|
|
ypos := 450;
|
|
// Font rendering mode
|
|
page.SetFontAndSize(font, 32);
|
|
page.SetRGBFill(0.5, 0.5, 0.0);
|
|
page.SetLineWidth(1.5);
|
|
|
|
// PDF_FILL
|
|
ShowDescription(page, 60, ypos, "RenderingMode=PDF_FILL");
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos, "ABCabc123");
|
|
page.EndText();
|
|
|
|
// PDF_STROKE
|
|
ShowDescription(page, 60, ypos - 50, "RenderingMode=PDF_STROKE");
|
|
page.SetTextRenderingMode(TSPdfEnumerations.STROKE);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos - 50, "ABCabc123");
|
|
page.EndText();
|
|
|
|
// PDF_FILL_THEN_STROKE
|
|
ShowDescription(page, 60, ypos - 100, "RenderingMode=PDF_FILL_THEN_STROKE");
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL_THEN_STROKE);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos - 100, "ABCabc123");
|
|
page.EndText();
|
|
|
|
// PDF_FILL_CLIPPING
|
|
ShowDescription(page, 60, ypos - 150, "RenderingMode=PDF_FILL_CLIPPING");
|
|
page.GSave();
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL_CLIPPING);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos - 150, "ABCabc123");
|
|
page.EndText();
|
|
ShowStripePattern(page, 60, ypos - 150);
|
|
page.GRestore();
|
|
|
|
// PDF_STROKE_CLIPPING
|
|
ShowDescription(page, 60, ypos - 200, "RenderingMode=PDF_STROKE_CLIPPING");
|
|
page.GSave();
|
|
page.SetTextRenderingMode(TSPdfEnumerations.STROKE_CLIPPING);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos - 200, "ABCabc123");
|
|
page.EndText();
|
|
ShowStripePattern(page, 60, ypos - 200);
|
|
page.GRestore();
|
|
|
|
// PDF_FILL_STROKE_CLIPPING
|
|
ShowDescription(page, 60, ypos - 250,
|
|
"RenderingMode=PDF_FILL_STROKE_CLIPPING");
|
|
page.GSave();
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL_STROKE_CLIPPING);
|
|
page.BeginText();
|
|
page.TextOut(60, ypos - 250, "ABCabc123");
|
|
page.EndText();
|
|
ShowStripePattern(page, 60, ypos - 250);
|
|
page.GRestore();
|
|
|
|
// Reset text attributes
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL);
|
|
page.SetRGBFill(0, 0, 0);
|
|
page.SetFontAndSize(font, 30);
|
|
|
|
|
|
// Rotating text
|
|
|
|
angle1 := 30; // A rotation of 30 degrees.
|
|
rad1 := angle1 / 180 / 3.141592; // Calcurate the radian value.
|
|
|
|
ShowDescription(page, 320, ypos - 60, "Rotating text");
|
|
page.BeginText();
|
|
page.SetTextMatrix(cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 330, ypos - 60);
|
|
page.ShowText("ABCabc123");
|
|
page.EndText();
|
|
|
|
|
|
// Skewing text.
|
|
ShowDescription(page, 320, ypos - 120, "Skewing text");
|
|
page.BeginText();
|
|
|
|
angle1 := 10;
|
|
angle2 := 20;
|
|
rad1 := angle1 / 180 / 3.141592;
|
|
rad2 := angle2 / 180 / 3.141592;
|
|
|
|
page.SetTextMatrix(1, tan(rad1), tan(rad2), 1, 320, ypos - 120);
|
|
page.ShowText("ABCabc123");
|
|
page.EndText();
|
|
|
|
|
|
// scaling text (X direction)
|
|
ShowDescription(page, 320, ypos - 175, "Scaling text (X direction)");
|
|
page.BeginText();
|
|
page.SetTextMatrix(1.5, 0, 0, 1, 320, ypos - 175);
|
|
page.ShowText("ABCabc12");
|
|
page.EndText();
|
|
|
|
|
|
// scaling text (Y direction)
|
|
|
|
ShowDescription(page, 320, ypos - 250, "Scaling text (Y direction)");
|
|
page.BeginText();
|
|
page.SetTextMatrix(1, 0, 0, 2, 320, ypos - 250);
|
|
page.ShowText("ABCabc123");
|
|
page.EndText();
|
|
|
|
|
|
// char spacing, word spacing
|
|
ShowDescription(page, 60, 140, "char-spacing 0");
|
|
ShowDescription(page, 60, 100, "char-spacing 1.5");
|
|
ShowDescription(page, 60, 60, "char-spacing 1.5, word-spacing 2.5");
|
|
|
|
page.SetFontAndSize(font, 20);
|
|
page.SetRGBFill(0.1, 0.3, 0.1);
|
|
|
|
// char-spacing 0
|
|
page.BeginText();
|
|
page.TextOut(60, 140, samp_text2);
|
|
page.EndText();
|
|
|
|
// char-spacing 1.5
|
|
page.SetCharSpace(1.5);
|
|
|
|
page.BeginText();
|
|
page.TextOut(60, 100, samp_text2);
|
|
page.EndText();
|
|
|
|
// char-spacing 1.5, word-spacing 3.5
|
|
page.SetWordSpace(2.5);
|
|
|
|
page.BeginText();
|
|
page.TextOut(60, 60, samp_text2);
|
|
page.EndText();
|
|
|
|
// save the document to a file
|
|
err := pdf.SaveToFile(alias, output_file);
|
|
echo "SaveToFile::\t", "err := ", format("%x", err), "\toutput_file := ", output_file, "\n";
|
|
return;
|
|
|
|
Function ShowStripePattern(page, x, y)
|
|
Begin
|
|
iy := 0;
|
|
text_width := page.TextWidth("ABCabc123");
|
|
while iy < 50 do
|
|
begin
|
|
page.SetRGBStroke(0.0, 0.0, 0.5);
|
|
page.SetLineWidth(1);
|
|
page.MoveTo(x, y + iy);
|
|
page.LineTo(x + text_width, y + iy);
|
|
page.Stroke();
|
|
iy += 3;
|
|
end
|
|
page.SetLineWidth(2.5);
|
|
End;
|
|
|
|
|
|
Function ShowDescription(page, x, y, text)
|
|
Begin
|
|
fsize := page.GetCurrentFontSize();
|
|
font := page.GetCurrentFont();
|
|
rgb := page.GetRGBFill();
|
|
|
|
page.BeginText();
|
|
page.SetRGBFill(0, 0, 0);
|
|
page.SetTextRenderingMode(TSPdfEnumerations.FILL);
|
|
page.SetFontAndSize(font, 10);
|
|
page.TextOut(x, y - 12, text);
|
|
page.EndText();
|
|
|
|
page.SetFontAndSize(font, fsize);
|
|
[r, g, b] := rgb;
|
|
page.SetRGBFill(r, g, b);
|
|
End;
|
|
|
|
Function PrintGrid(pdf, page)
|
|
Begin
|
|
height := page.GetHeight();
|
|
width := page.GetWidth();
|
|
font := pdf.GetFont("Helvetica", "");
|
|
|
|
page.SetFontAndSize(font, 5);
|
|
page.SetGrayFill(0.5);
|
|
page.SetGrayStroke(0.8);
|
|
|
|
// Draw horizontal lines
|
|
y := 0;
|
|
while y < height do
|
|
begin
|
|
if y % 10 = 0 then
|
|
page.SetLineWidth(0.5);
|
|
else begin
|
|
wid := page.GetWidth();
|
|
if wid <> 0.25 then page.SetLineWidth(0.25);
|
|
end
|
|
|
|
page.MoveTo(0, y);
|
|
page.LineTo(width, y);
|
|
page.Stroke();
|
|
|
|
if y % 10 = 0 and y > 0 then
|
|
begin
|
|
page.SetGrayStroke(0.5);
|
|
|
|
page.MoveTo(0, y);
|
|
page.LineTo(5, y);
|
|
page.Stroke();
|
|
page.SetGrayStroke(0.8);
|
|
end
|
|
|
|
y += 5;
|
|
end
|
|
|
|
|
|
// Draw vertical lines
|
|
x := 0;
|
|
while x < width do
|
|
begin
|
|
if x % 10 = 0 then
|
|
page.SetLineWidth(0.5);
|
|
else
|
|
if page.GetWidth() <> 0.25 then page.SetLineWidth(0.25);
|
|
|
|
page.MoveTo(x, 0);
|
|
page.LineTo(x, height);
|
|
page.Stroke();
|
|
|
|
if x % 50 = 0 and x > 0 then
|
|
begin
|
|
page.SetGrayStroke(0.5);
|
|
|
|
page.MoveTo(x, 0);
|
|
page.LineTo(x, 5);
|
|
page.Stroke();
|
|
|
|
page.MoveTo(x, height);
|
|
page.LineTo(x, height - 5);
|
|
page.Stroke();
|
|
|
|
page.SetGrayStroke(0.8);
|
|
end
|
|
|
|
x += 5;
|
|
end
|
|
|
|
// Draw horizontal text
|
|
y := 0;
|
|
while y < height do
|
|
begin
|
|
if y % 10 = 0 and y > 0 then
|
|
begin
|
|
page.BeginText();
|
|
page.MoveTextPos(5, y - 2);
|
|
page.ShowText(tostring(y));
|
|
page.EndText();
|
|
end
|
|
y += 5;
|
|
end
|
|
|
|
|
|
// Draw vertical text
|
|
x := 0;
|
|
while x < width do
|
|
begin
|
|
if x % 50 = 0 and x > 0 then
|
|
begin
|
|
buf := tostring(x);
|
|
page.BeginText();
|
|
page.MoveTextPos(x, 5);
|
|
page.ShowText(buf);
|
|
page.EndText();
|
|
|
|
page.BeginText();
|
|
page.MoveTextPos(x, height - 10);
|
|
page.ShowText(buf);
|
|
page.EndText();
|
|
end
|
|
|
|
x += 5;
|
|
end
|
|
|
|
page.SetGrayFill(0);
|
|
page.SetGrayStroke(0);
|
|
End;
|