using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.files;
using System;
using System.Drawing;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample demonstrates the use of standard Type 1 fonts, which are the 14 built-in fonts
prescribed by the PDF specification to be shipped along with any conformant PDF viewer.</summary>
<remarks>In particular, this sample displays the complete glyphset of each standard font,
iterating through character codes and glyph styles (regular, italic, bold).</remarks>
*/
public class StandardFontSample
: ISample
{
#region static
#region fields
private static readonly int FontBaseSize = 20;
private static readonly int Margin = 50;
#endregion
#endregion
#region dynamic
#region interface
#region public
#region ISample
public void Run(
SampleLoader loader
)
{
// 1. PDF file instantiation.
File file = new File();
Document document = file.Document;
// 2. Content creation.
Populate(document);
// (boilerplate metadata insertion -- ignore it)
loader.BuildAccessories(document,this.GetType(),"Standard Type 1 fonts","applying standard Type 1 fonts");
// 3. Serialize the PDF file (again, boilerplate code -- see the SampleLoader class source code)!
loader.Serialize(file,this.GetType().Name,false);
}
#endregion
#endregion
#region private
private void Populate(
Document document
)
{
Page page = new Page(document);
document.Pages.Add(page);
SizeF pageSize = page.Size.Value;
PrimitiveFilter builder = new PrimitiveFilter(page);
int x = Margin, y = Margin;
StandardType1Font titleFont = new StandardType1Font(
document,
StandardType1Font.FamilyNameEnum.Times,
true,
true
);
StandardType1Font font = null;
// Iterating through the standard Type 1 fonts...
foreach(StandardType1Font.FamilyNameEnum fontFamilyName
in (StandardType1Font.FamilyNameEnum[])Enum.GetValues(typeof(StandardType1Font.FamilyNameEnum)))
{
// Iterating through the font styles...
for(int styleIndex = 0; styleIndex < 4; styleIndex++)
{
/*
NOTE: Symbol and Zapf Dingbats are available just as regular fonts (no italic or bold variant).
*/
if(styleIndex > 0
&& (fontFamilyName == StandardType1Font.FamilyNameEnum.Symbol
|| fontFamilyName == StandardType1Font.FamilyNameEnum.ZapfDingbats))
break;
bool bold, italic;
switch(styleIndex)
{
case 0: // Regular.
bold = false;
italic = false;
break;
case 1: // Bold.
bold = true;
italic = false;
break;
case 2: // Italic.
bold = false;
italic = true;
break;
case 3: // Bold italic.
bold = true;
italic = true;
break;
default:
throw new Exception("styleIndex " + styleIndex + " not supported.");
}
// Define the font used to show its character set!
font = new StandardType1Font(
document,
fontFamilyName,
bold,
italic
);
if(y > pageSize.Height - Margin)
{
builder.Flush();
page = new Page(document);
document.Pages.Add(page);
pageSize = page.Size.Value;
builder = new PrimitiveFilter(page);
x = Margin; y = Margin;
}
if(styleIndex == 0)
{
builder.DrawLine(
new PointF(x,y),
new PointF(pageSize.Width - Margin,y)
);
builder.Stroke();
y += 5;
}
builder.SetFont(
titleFont,
FontBaseSize * (styleIndex == 0 ? 1.5 : 1)
);
builder.ShowText(
fontFamilyName.ToString() + (bold ? " bold" : "") + (italic ? " italic" : ""),
new PointF(x,y)
);
y += 40;
// Set the font used to show its character set!
builder.SetFont(font,FontBaseSize);
// Iterating through the font characters...
for(int charCode = 32; charCode < 256; charCode++)
{
if(y > pageSize.Height - Margin)
{
builder.Flush();
page = new Page(document);
document.Pages.Add(page);
pageSize = page.Size.Value;
builder = new PrimitiveFilter(page);
x = Margin; y = Margin;
builder.SetFont(titleFont,FontBaseSize);
builder.ShowText(
fontFamilyName.ToString() + " (continued)",
new PointF(pageSize.Width - Margin, y),
AlignmentXEnum.Right,
AlignmentYEnum.Top,
0
);
builder.SetFont(font,FontBaseSize);
y += FontBaseSize * 2;
}
try
{
// Show the current character (using the current standard Type 1 font)!
builder.ShowText(
new String(new char[]{(char)charCode}),
new PointF(x,y)
);
x += FontBaseSize;
if(x > pageSize.Width - Margin)
{x = Margin; y += 30;}
}
catch(Exception e)
{ /* NOOP. */ }
}
x = Margin; y += Margin;
}
}
builder.Flush();
}
#endregion
#endregion
#endregion
}
}
|