using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents;
using colorSpacesit.stefanochizzolini.clown.documents.contents.colorSpaces;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.documents.contents.objects;
using it.stefanochizzolini.clown.files;
using it.stefanochizzolini.clown.objects;
using System;
using System.Drawing;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample shows the effects of the manipulation of the CTM (Current Transformation
Matrix), that is the logical device which affects the page coordinate system used to place graphics
contents onto the canvas.</summary>
*/
public class PageCoordinatesSample
: ISample
{
#region static
#region fields
private static readonly PdfName ResourceName_DefaultFont = new PdfName("default");
#endregion
#endregion
#region dynamic
#region interface
#region public
#region ISample
public void Run(
SampleLoader loader
)
{
// 1. Instantiate a new PDF file!
/* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */
File file = new File();
try
{
// 2. Get its corresponding document!
/* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */
Document document = file.Document;
// 3. Set the document properties and resources!
Build_Initialize(document);
// 4. Insert the contents into the document!
Build_Content(document);
}
catch(Exception e)
{throw new Exception("Unhandled exception",e);}
// 5. Serialize the PDF file!
loader.Serialize(file,this.GetType().Name,false);
}
#endregion
#endregion
#region private
/**
Populates a PDF file with contents.
*/
private void Build_Content(
Document document
)
{
// Add the page to the document!
Page page = new Page(document); // Instantiates the page inside the document context.
document.Pages.Add(page); // Puts the page in the pages collection.
SizeF pageSize = page.Size.Value;
// Create a content builder for the page content stream!
PrimitiveFilter builder = new PrimitiveFilter(page);
string[] steps = new string[5];
colorSpaces::Color[] fillColors = new colorSpaces::Color[5];
Build_Content_Steps(
builder,
steps,
fillColors,
pageSize
);
Build_Content_Legend(
builder,
steps,
fillColors,
pageSize
);
builder.Flush();
}
private string Build_Content_GetStepNote(
PrimitiveFilter builder,
string comment
)
{
// Get the CTM!
double[] ctm = builder.Scanner.State.CTM;
return "CTM (" + comment + "): " + ctm[0] + ", " + ctm[1] + ", " + ctm[2] + ", " + ctm[3] + ", " + ctm[4] + ", " + ctm[5];
}
private void Build_Content_Legend(
PrimitiveFilter builder,
string[] steps,
colorSpaces::Color[] fillColors,
SizeF pageSize
)
{
double[] finalCTM = builder.Scanner.State.CTM;
BlockFilter blockFilter = new BlockFilter(builder);
builder.BeginLocalState();
builder.SetFillColor(
new colorSpaces::DeviceRGBColor(115f/255,164f/255,232f/255)
);
RectangleF frame = new RectangleF(
18,
18,
pageSize.Width * .5f,
pageSize.Height * .5f
);
blockFilter.Begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top);
builder.SetFont(ResourceName_DefaultFont,24);
blockFilter.ShowText("Page coordinates sample");
SizeF breakSize = new SizeF(0,8);
blockFilter.ShowBreak(breakSize);
builder.SetFont(ResourceName_DefaultFont,8);
blockFilter.ShowText("This sample shows the effects of the manipulation of the CTM (Current Transformation Matrix), that is the device which affects the page coordinate system used to place graphics contents onto the canvas.");
blockFilter.ShowBreak(breakSize);
blockFilter.ShowText("The following steps represent the operations applied to this page's CTM in order to alter it. Each step writes the tag \"Step\" at the current coordinates origin and draws the page frame:");
breakSize = new SizeF(8,4);
blockFilter.ShowBreak(breakSize);
for(int i = 0; i < steps.Length; i++)
{
builder.SetFillColor(
fillColors[i]
);
blockFilter.ShowText("Step " + i + ")");
builder.SetFillColor(
new colorSpaces::DeviceRGBColor(115f/255,164f/255,232f/255)
);
blockFilter.ShowText(" " + steps[i]);
blockFilter.ShowBreak(breakSize);
}
blockFilter.ShowText("Note that the (negligible: " + Math.Max(
Math.Max(
Math.Max(
Math.Abs(finalCTM[1]%1),
Math.Abs(finalCTM[2]%1)
),
Math.Abs(finalCTM[4]%1)
),
Math.Abs(finalCTM[5]%1)
) + ") approximation of the CTM components at step 4 is due to floating point precision limits; their exact values should be 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.");
blockFilter.End();
builder.End();
}
private void Build_Content_Steps(
PrimitiveFilter builder,
string[] steps,
colorSpaces::Color[] fillColors,
SizeF pageSize
)
{
builder.SetFont(ResourceName_DefaultFont,32);
RectangleF frame = new RectangleF(
0,
0,
pageSize.Width,
pageSize.Height
);
// Step 0.
{
fillColors[0] = new colorSpaces::DeviceRGBColor(30f/255, 10f/255, 0);
builder.SetFillColor(
fillColors[0]
);
builder.SetStrokeColor(
new colorSpaces::DeviceRGBColor(0, 0, 0)
);
// Draw the page frame!
builder.DrawRectangle(frame);
builder.Stroke();
builder.ShowText(
"Step 0",
new PointF(0,pageSize.Height),
AlignmentXEnum.Left,
AlignmentYEnum.Bottom,
0
);
steps[0] = Build_Content_GetStepNote(builder,"default");
}
// Step 1.
{
fillColors[1] = new colorSpaces::DeviceRGBColor(80f/255, 25f/255, 0);
builder.SetFillColor(
fillColors[1]
);
builder.SetStrokeColor(
new colorSpaces::DeviceRGBColor(50f/255, 15f/255, 0)
);
builder.Translate(72,72);
// Draw the page frame!
builder.DrawRectangle(frame);
builder.Stroke();
builder.ShowText(
"Step 1",
new PointF(0,pageSize.Height),
AlignmentXEnum.Left,
AlignmentYEnum.Bottom,
0
);
steps[1] = Build_Content_GetStepNote(builder,"after translate(72,72)");
}
// Step 2.
{
fillColors[2] = new colorSpaces::DeviceRGBColor(130f/255, 45f/255, 0);
builder.SetFillColor(
fillColors[2]
);
builder.SetStrokeColor(
new colorSpaces::DeviceRGBColor(100f/255, 30f/255, 0)
);
builder.Rotate(-20);
// Draw the page frame!
builder.DrawRectangle(frame);
builder.Stroke();
builder.ShowText("Origin 2");
builder.ShowText(
"Step 2",
new PointF(0,pageSize.Height),
AlignmentXEnum.Left,
AlignmentYEnum.Bottom,
0
);
steps[2] = Build_Content_GetStepNote(builder,"after rotate(20)");
}
// Step 3.
{
fillColors[3] = new colorSpaces::DeviceRGBColor(180f/255, 60f/255, 0);
builder.SetFillColor(
fillColors[3]
);
builder.SetStrokeColor(
new colorSpaces::DeviceRGBColor(150f/255, 50f/255, 0)
);
builder.Translate(0,72);
builder.Scale(.5,.5);
// Draw the page frame!
builder.DrawRectangle(frame);
builder.Stroke();
builder.ShowText(
"Step 3",
new PointF(0,pageSize.Height),
AlignmentXEnum.Left,
AlignmentYEnum.Bottom,
0
);
steps[3] = Build_Content_GetStepNote(builder,"after translate(0,72) and scale(.5,.5)");
}
// Step 4.
{
fillColors[4] = new colorSpaces::DeviceRGBColor(230f/255, 75f/255, 0);
builder.SetFillColor(
fillColors[4]
);
builder.SetStrokeColor(
new colorSpaces::DeviceRGBColor(200f/255, 65f/255, 0)
);
builder.Add(
ModifyCTM.GetResetCTM(
builder.Scanner.State.CTM
)
);
// Draw the page frame!
builder.DrawRectangle(frame);
builder.Stroke();
builder.ShowText(
"Step 4",
new PointF(0,pageSize.Height),
AlignmentXEnum.Left,
AlignmentYEnum.Bottom,
0
);
steps[4] = Build_Content_GetStepNote(builder,"after resetting CTM");
}
}
/**
Prepares the basic settings for populating a PDF file.
*/
private void Build_Initialize(
Document document
)
{
// 1. Set default page size (A4)!
document.PageSize = PageFormat.GetSize();
// 2. Setting the document resources...
// 2.1. Resources collection.
Resources resources = new Resources(document); // Instantiates the resources collection inside the document context.
document.Resources = resources; // Puts the resources collection in the common resources role.
// 2.2. Fonts collection.
FontResources fonts = new FontResources(document); // Instantiates the fonts collection inside the document context.
resources.Fonts = fonts; // Puts the fonts collection in the common resources role.
// Add a font to the fonts collection!
fonts[ResourceName_DefaultFont] = new StandardType1Font(
document,
StandardType1Font.FamilyNameEnum.Courier,
true,
false
);
}
#endregion
#endregion
#endregion
}
}
|