Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Add DrawString Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kjpou1 committed Dec 4, 2013
1 parent d0ee783 commit 10b79ad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 287 deletions.
350 changes: 64 additions & 286 deletions Samples/MonoMac/DrawStringTest/DrawingView.cs
Expand Up @@ -86,6 +86,30 @@ public Font Font
}
#endregion


Font anyKeyFont = new Font("Chalkduster", 18, FontStyle.Bold);
Font clipFont = new Font(FontFamily.GenericSansSerif,12, FontStyle.Bold);


int currentView = 0;
int totalViews = 4;

public override bool AcceptsFirstResponder ()
{
return true;
}

public override void KeyDown (NSEvent theEvent)
{
currentView++;
currentView %= totalViews;
//Console.WriteLine("Current View: {0}", currentView);
this.NeedsDisplay = true;
}

string title = string.Empty;


public override void DrawRect (System.Drawing.RectangleF dirtyRect)
{

Expand Down Expand Up @@ -128,310 +152,64 @@ public override void DrawRect (System.Drawing.RectangleF dirtyRect)
// g.DrawRectangle(Pens.Red, rect);
// g.DrawString("Test2 Far", this.Font, Brushes.Blue, rect, format);
//
switch (currentView)
{
case 0:
DrawStringPointF (g);
break;

//AvailableFonts ();
//PrivateFonts ();
//CreatePrivateFontCollection (g);
ObtainFontMetrics (g);
g.Dispose();
}



void ObtainFontMetrics(Graphics g)
{
string infoString = ""; // enough space for one line of output
int ascent; // font family ascent in design units
float ascentPixel; // ascent converted to pixels
int descent; // font family descent in design units
float descentPixel; // descent converted to pixels
int lineSpacing; // font family line spacing in design units
float lineSpacingPixel; // line spacing converted to pixels

FontStyle fontStyle = FontStyle.Regular;
//fontStyle = FontStyle.Italic | FontStyle.Bold;
FontFamily fontFamily = new FontFamily("arial");
//fontFamily = FontFamily.GenericSansSerif;

Font font = new Font(
fontFamily,
16, fontStyle,
GraphicsUnit.Pixel);
PointF pointF = new PointF(10, 10);
SolidBrush solidBrush = new SolidBrush(Color.Black);

// Display the font size in pixels.
infoString = "font family : " + font.FontFamily.Name + " " + fontStyle + ".";
g.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the font size in pixels.
infoString = "font.Size returns " + font.Size + ".";
g.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " +
fontFamily.GetEmHeight(fontStyle) + ".";
g.DrawString(infoString, font, solidBrush, pointF);

// Move down two lines.
pointF.Y += 2 * font.Height;

// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(fontStyle);

// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
font.Size * ascent / fontFamily.GetEmHeight(fontStyle);
infoString = "The ascent is " + ascent + " design units, " + ascentPixel +
" pixels.";
g.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(fontStyle);

// 3.390625 = 16.0 * 434 / 2048
descentPixel =
font.Size * descent / fontFamily.GetEmHeight(fontStyle);
infoString = "The descent is " + descent + " design units, " +
descentPixel + " pixels.";
g.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(fontStyle);

// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(fontStyle);
infoString = "The line spacing is " + lineSpacing + " design units, " +
lineSpacingPixel + " pixels.";
g.DrawString(infoString, font, solidBrush, pointF);
}
}

g.ResetTransform ();
Brush sBrush = Brushes.Black;

private void AvailableFonts()
{
var installedFonts = new InstalledFontCollection ();

foreach ( FontFamily ff in installedFonts.Families )
if (!g.IsClipEmpty)
{
Console.WriteLine(ff.ToString());
var clipPoint = PointF.Empty;
var clipString = string.Format("Clip-{0}", g.ClipBounds);
g.ResetClip ();
var clipSize = g.MeasureString(clipString, clipFont);
clipPoint.X = (ClientRectangle.Width / 2) - (clipSize.Width / 2);
clipPoint.Y = 5;
g.DrawString(clipString, clipFont, sBrush, clipPoint );

foreach (var style in Enum.GetValues(typeof(FontStyle)) )
{
if (ff.IsStyleAvailable((FontStyle)style))
Console.WriteLine(ff.ToString() + " - " + (FontStyle)style);
}
}

}

private void PrivateFonts()
{
var privateFonts = new PrivateFontCollection ();
var anyKeyPoint = PointF.Empty;
var anyKey = "Press any key to continue.";
var anyKeySize = g.MeasureString(anyKey, anyKeyFont);
anyKeyPoint.X = (ClientRectangle.Width / 2) - (anyKeySize.Width / 2);
anyKeyPoint.Y = ClientRectangle.Height - (anyKeySize.Height + 10);
g.DrawString(anyKey, anyKeyFont, sBrush, anyKeyPoint );

privateFonts.AddFontFile ("A Damn Mess.ttf");
privateFonts.AddFontFile ("Abberancy.ttf");
privateFonts.AddFontFile ("Abduction.ttf");
privateFonts.AddFontFile ("American Typewriter.ttf");
privateFonts.AddFontFile ("Paint Boy.ttf");
anyKeySize = g.MeasureString(title, anyKeyFont);
anyKeyPoint.X = (ClientRectangle.Width / 2) - (anyKeySize.Width / 2);
anyKeyPoint.Y -= anyKeySize.Height;
g.DrawString(title, anyKeyFont, sBrush, anyKeyPoint );

foreach ( FontFamily ff in privateFonts.Families )
{
Console.WriteLine(ff.ToString());
foreach (var style in Enum.GetValues(typeof(FontStyle)) )
{
if (ff.IsStyleAvailable((FontStyle)style))
Console.WriteLine(ff.ToString() + " - " + (FontStyle)style);
}
}

g.Dispose();
}


void CreatePrivateFontCollection(Graphics g)
public void DrawStringPointF(Graphics g)
{

PointF pointF = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);
// Create string to draw.
String drawString = "Sample Text";

int count = 0;
string familyName = "";
string familyNameAndStyle;
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);

// Add three font files to the private collection.
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(150.0F, 150.0F);

// var path = Environment.ExpandEnvironmentVariables("%SystemRoot%\\Fonts\\");
//
// privateFontCollection.AddFontFile(System.IO.Path.Combine(path,"Arial.ttf"));
// privateFontCollection.AddFontFile(System.IO.Path.Combine(path,"CourBI.ttf"));
// //privateFontCollection.AddFontFile(System.IO.Path.Combine(path, "Courier New.ttf"));
// privateFontCollection.AddFontFile(System.IO.Path.Combine(path, "TimesBD.ttf"));
privateFontCollection.AddFontFile ("A Damn Mess.ttf");
privateFontCollection.AddFontFile ("Abberancy.ttf");
privateFontCollection.AddFontFile ("Abduction.ttf");
privateFontCollection.AddFontFile ("American Typewriter.ttf");
privateFontCollection.AddFontFile ("Paint Boy.ttf");


// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;

// How many objects in the fontFamilies array?
count = fontFamilies.Length;

// Display the name of each font family in the private collection
// along with the available styles for that font family.
for (int j = 0; j < count; ++j)
{
// Get the font family name.
familyName = fontFamilies[j].Name;

// Is the regular style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + " Regular";

Font regFont = new Font(
familyName,
26,
FontStyle.Regular,
GraphicsUnit.Pixel);

g.DrawString(
familyNameAndStyle,
regFont,
solidBrush,
pointF);

pointF.Y += regFont.Height;
}

// Is the bold style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + " Bold";

Font boldFont = new Font(
familyName,
26,
FontStyle.Bold,
GraphicsUnit.Pixel);

g.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF);

pointF.Y += boldFont.Height;
}
// Is the italic style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + " Italic";

Font italicFont = new Font(
familyName,
26,
FontStyle.Italic,
GraphicsUnit.Pixel);

g.DrawString(
familyNameAndStyle,
italicFont,
solidBrush,
pointF);

pointF.Y += italicFont.Height;
}

// Is the bold italic style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) &&
fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + "BoldItalic";

Font italicFont = new Font(
familyName,
26,
FontStyle.Italic | FontStyle.Bold,
GraphicsUnit.Pixel);

g.DrawString(
familyNameAndStyle,
italicFont,
solidBrush,
pointF);

pointF.Y += italicFont.Height;
}
// Is the underline style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + " Underline";

Font underlineFont = new Font(
familyName,
26,
FontStyle.Underline,
GraphicsUnit.Pixel);

g.DrawString(
familyNameAndStyle,
underlineFont,
solidBrush,
pointF);

pointF.Y += underlineFont.Height;
}

// Is the strikeout style available?
if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout))
{
familyNameAndStyle = "";
familyNameAndStyle = familyNameAndStyle + familyName;
familyNameAndStyle = familyNameAndStyle + " Strikeout";

Font strikeFont = new Font(
familyName,
26,
FontStyle.Strikeout,
GraphicsUnit.Pixel);

g.DrawString(
familyNameAndStyle,
strikeFont,
solidBrush,
pointF);

pointF.Y += strikeFont.Height;
}

// Separate the families with white space.
pointF.Y += 10;

} // for
}
// Draw string to screen.
g.DrawString(drawString, drawFont, drawBrush, drawPoint);

title = "DrawStringPointF";
}

// public override bool IsFlipped {
// get {
Expand Down
2 changes: 1 addition & 1 deletion sysdrawing-coregraphics.sln
Expand Up @@ -1253,7 +1253,7 @@ Global
{192C5B9E-5FF5-4C68-AAF6-B571695DA0EA} = {9F1E821E-5BAC-40C6-9FBE-4C81C1AD4E6A}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Samples\MonoTouch\MTFontTest\MTFontTest.csproj
StartupItem = Samples\MonoMac\DrawStringTest\DrawStringTest.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = Mono
Expand Down

0 comments on commit 10b79ad

Please sign in to comment.