font and format drawing samples
authorJordi Mas i Hernandez <jordi@mono-cvs.ximian.com>
Fri, 13 Feb 2004 10:40:15 +0000 (10:40 -0000)
committerJordi Mas i Hernandez <jordi@mono-cvs.ximian.com>
Fri, 13 Feb 2004 10:40:15 +0000 (10:40 -0000)
svn path=/trunk/mcs/; revision=23055

mcs/class/System.Drawing/Test/System.Drawing/fontDrawing.cs [new file with mode: 0644]
mcs/class/System.Drawing/Test/System.Drawing/fontDrawingAdv.cs [new file with mode: 0644]

diff --git a/mcs/class/System.Drawing/Test/System.Drawing/fontDrawing.cs b/mcs/class/System.Drawing/Test/System.Drawing/fontDrawing.cs
new file mode 100644 (file)
index 0000000..de3e4aa
--- /dev/null
@@ -0,0 +1,93 @@
+//
+// 
+// Simple font handeling
+//
+// Author:
+//   Jordi Mas i Hernandez <jordi@ximian.com>
+// 
+//
+using System;
+using System.Drawing;
+using System.Drawing.Text;
+using System.Drawing.Imaging;
+
+namespace Font1Sample {
+       public class FontDrawing {
+               
+               public static void listFonts()
+               {               
+                       FontCollection ifc = new InstalledFontCollection();
+                       foreach( FontFamily ffm in ifc.Families) {                              
+                               try
+                               {
+                                       Font f = new Font(ffm.Name,12); 
+                                       Console.WriteLine("Family Name:" + ffm.Name + "," + f.Name);
+                               }                       
+                               catch (Exception ex)    {}
+                       }
+               }
+               
+               public static void checkFontProperties()
+               {
+                       Font f = new Font("Arial",12);  
+                       Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
+                       
+                       f = new Font("Verdana", 12);    
+                       Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
+                       
+                       f = new Font("Courier New", 12);        
+                       Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
+                                       
+               }
+               
+               public static void Main( ) 
+               {
+                       Console.WriteLine("Fonts--------------------");                 
+               listFonts();                    
+               
+                       Console.WriteLine("Propierties--------------------");                                           
+                       checkFontProperties();            
+                       
+                       Console.WriteLine("Draw--------------------");
+                       float width = 400.0F;
+                       float height = 400.0F;
+            string str = "";
+                       
+                       Font f1 = new Font("Arial",12);                 
+                       Font f2 = new Font("Verdana", 12, FontStyle.Bold);      
+                       Font f3 = new Font("Courier New", 12, FontStyle.Italic);
+                       Font f4 = new Font("Arial",12);
+                       
+                       Console.WriteLine("Name: {1}", f1.Name, f1.Height);
+                       
+                       Bitmap bmp = new Bitmap((int)width, (int)height);
+                       Graphics gr = Graphics.FromImage(bmp);
+                       SolidBrush br = new SolidBrush(Color.White);
+                       SolidBrush colorRed = new SolidBrush(Color.Red);
+
+                       gr.FillRectangle(br, 0.0F, 0.0F, width, height);
+                       
+                       br = new SolidBrush(Color.Black);
+
+            str = "This an " +  f1.Name + " test string size: "+ f1.Height;                        
+                       gr.DrawString (str, f1, br, 10, 10);
+
+            str = "This a " +  f2.Name + " bold test string size: "+ f2.Height;
+                       gr.DrawString( str, f2, colorRed, 10, 50);
+
+            str = "This a " +  f3.Name + " italic test string size: "+ f3.Height;
+                       gr.DrawString( str, f3, br, 10, 100);
+
+                        str = "This an " +  f1.Name + " test string size: "+ f1.Height;
+                       gr.DrawString (str, f1, br, 10, 10);
+
+            str = "This a " +  f2.Name + " bold test string size: "+ f2.Height;
+                       gr.DrawString( str, f2, colorRed, 10, 50);
+
+            str = "This a " +  f3.Name + " italic test string size: "+ f3.Height;
+                       gr.DrawString( str, f3, br, 10, 100);
+                       
+                       bmp.Save("fontdrawing.bmp", ImageFormat.Bmp);                   
+               }
+       }
+}
diff --git a/mcs/class/System.Drawing/Test/System.Drawing/fontDrawingAdv.cs b/mcs/class/System.Drawing/Test/System.Drawing/fontDrawingAdv.cs
new file mode 100644 (file)
index 0000000..029fc2d
--- /dev/null
@@ -0,0 +1,169 @@
+//
+// 
+// Advanced text drawing and formatting sample
+//
+// Author:
+//   Jordi Mas i Hernandez <jordi@ximian.com>
+// 
+//
+using System;
+using System.Drawing;
+using System.Drawing.Text;
+using System.Drawing.Imaging;
+
+namespace Font1Sample {
+       public class FontDrawing {              
+               
+               static string flagProcessing(StringFormat format)
+               {
+                       string str = "";
+                       
+                       switch (format.Alignment) {
+                       case StringAlignment.Far:
+                               str = "halign: Far - ";
+                               break;
+                       case StringAlignment.Near:
+                               str = "halign: Near - ";
+                               break;
+                       case StringAlignment.Center:
+                               str = "halign: Center - ";
+                               break;
+                       default:
+                               break;                          
+                       }
+                       
+                       switch (format.LineAlignment) {
+                       case StringAlignment.Far:
+                               str += "valign: Far - ";
+                               break;
+                       case StringAlignment.Near:
+                               str += "valign: Near - ";
+                               break;
+                       case StringAlignment.Center:
+                               str += "valign: Center - ";
+                               break;
+                       default:
+                               break;                          
+                       }
+                       
+                       str+="fmt: ";
+                       
+                       if (format.FormatFlags==StringFormatFlags.NoWrap) str+="NoWarp ";
+                       if (format.FormatFlags==StringFormatFlags.DirectionVertical) str+="DirVer ";
+                       
+                       return str;     
+               }
+               
+               static Rectangle calcRect(Rectangle rect)
+               {
+                       return new Rectangle (rect.X, rect.Y+rect.Height+10, rect.Width,200);                                           
+               }
+               
+               public static void Main( ) 
+               {                                               
+                       float width = 700.0F;
+                       float height = 600.0F;
+            
+                       Font f1 = new Font("Arial",12);                         
+                       Font f2  = new Font("Verdana", 8);                      
+                       Font f3  = new Font("Courier New", 14);
+                       Font f4  = new Font(FontFamily.GenericSansSerif, 14);
+                       Font f5  = new Font(FontFamily.GenericMonospace, 14);
+                       Font f6  = new Font(FontFamily.GenericSerif, 16);
+                       Font fonttxt= new Font("Verdana", 8);
+                       SolidBrush brushtxt = new SolidBrush(Color.Pink);
+                                       
+                       StringFormat strfmt1 = new StringFormat();
+                       StringFormat strfmt2 = new StringFormat();
+                       StringFormat strfmt3 = new StringFormat();
+                       StringFormat strfmt4 = new StringFormat();
+                       StringFormat strfmt5 = new StringFormat();
+                       StringFormat strfmt6 = new StringFormat();                      
+                       StringFormat strfmttxt = new StringFormat();                    
+                       
+                       Bitmap bmp = new Bitmap((int)width, (int)height);
+                       Graphics gr = Graphics.FromImage(bmp);
+                       SolidBrush br = new SolidBrush(Color.White);
+                       SolidBrush colorRed = new SolidBrush(Color.Red);
+                       
+                       strfmttxt.Alignment = StringAlignment.Near;
+                       strfmttxt.LineAlignment = StringAlignment.Near;
+                       strfmttxt.Trimming = StringTrimming.Character;
+                       
+                       strfmt1.Alignment = StringAlignment.Center;
+                       strfmt1.LineAlignment = StringAlignment.Near;
+                       strfmt1.Trimming = StringTrimming.Character;
+                       strfmt1.HotkeyPrefix = HotkeyPrefix.Show;
+                       
+                       strfmt2.Alignment = StringAlignment.Far;
+                       strfmt2.LineAlignment = StringAlignment.Center;
+                       strfmt2.Trimming = StringTrimming.Character;
+                       
+                       strfmt3.Alignment = StringAlignment.Far;
+                       strfmt3.LineAlignment = StringAlignment.Near;
+                       strfmt3.Trimming = StringTrimming.None;
+                       
+                       strfmt4.Alignment = StringAlignment.Far;
+                       strfmt4.LineAlignment = StringAlignment.Far;
+                       strfmt4.Trimming = StringTrimming.EllipsisCharacter;
+                       
+                       strfmt5.Alignment = StringAlignment.Far;
+                       strfmt5.LineAlignment = StringAlignment.Near;
+                       strfmt5.Trimming = StringTrimming.None;
+                       strfmt5.FormatFlags = StringFormatFlags.DirectionVertical;
+                       
+                       strfmt6.Alignment = StringAlignment.Far;
+                       strfmt6.LineAlignment = StringAlignment.Far;
+                       strfmt6.Trimming = StringTrimming.EllipsisCharacter;
+                       strfmt6.FormatFlags = StringFormatFlags.NoWrap;
+                       
+                       
+                       Rectangle rect1 = new Rectangle (10,50,100,150);
+                       Rectangle rect2 = new Rectangle (10,300,150,150);
+                       
+                       Rectangle rect3 = new Rectangle (200,50,175,175);
+                       Rectangle rect4 = new Rectangle (200,300,150,150);
+                       
+                       Rectangle rect5 = new Rectangle (400,50,175,175);
+                       Rectangle rect6 = new Rectangle (400,300,150,150);                      
+                       
+                       gr.DrawRectangle( new Pen(Color.Yellow), rect3);                        
+                       gr.DrawRectangle( new Pen(Color.Blue), rect4);                  
+                       
+                       gr.DrawRectangle( new Pen(Color.Yellow), rect5);                        
+                       gr.DrawRectangle( new Pen(Color.Blue), rect6);                          
+                       
+                       gr.DrawString("Samples of text with different fonts and formatting", 
+                               new Font("Verdana",16), new SolidBrush(Color.White), new Rectangle (5,5,600,100), strfmttxt);                                                                                   
+                       
+                       gr.FillEllipse(new SolidBrush(Color.Blue), rect1);
+                       gr.DrawRectangle( new Pen(Color.Green), rect2);                 
+                       gr.DrawString("Ara que tinc &vint anys, ara que encara tinc força,que no tinc l'ànima morta, i em sento bullir la sang. (" + f1.Name + ")", 
+                               f1, new SolidBrush(Color.White), rect1, strfmt1);                                               
+                       gr.DrawString(flagProcessing(strfmt1), fonttxt, brushtxt, calcRect(rect1), strfmttxt);                  
+                               
+                       gr.DrawString("Ara que em sento capaç de cantar si un altre canta. Avui que encara tinc veu i encara puc creure en déus (" + f2.Name + ")", 
+                               f2, new SolidBrush(Color.Red),rect2, strfmt2);                                                                                                          
+                       gr.DrawString(flagProcessing(strfmt2), fonttxt, brushtxt, calcRect(rect2), strfmttxt);                                          
+                       
+                       gr.DrawString("Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f3.Name + ")", 
+                               f3, new SolidBrush(Color.White), rect3, strfmt3);                               
+                       gr.DrawString(flagProcessing(strfmt3), fonttxt, brushtxt, calcRect(rect3), strfmttxt);                  
+                                                       
+                       gr.DrawString("A la nit, al cel i a aquet mar tan nostre i al vent que al matí ve a besar-me el rostre (" + f4.Name + ")", 
+                               f4, new SolidBrush(Color.Red),rect4, strfmt4);
+                       gr.DrawString(flagProcessing(strfmt4), fonttxt, brushtxt, calcRect(rect4), strfmttxt);                  
+                       
+                       gr.DrawString("Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f5.Name + ")", 
+                               f5, new SolidBrush(Color.White), rect5, strfmt5);
+                       gr.DrawString(flagProcessing(strfmt5), fonttxt, brushtxt, calcRect(rect5), strfmttxt);                  
+                               
+                       gr.DrawString("Ve a besar-me el rostre (" + f6.Name + ")", 
+                               f6, new SolidBrush(Color.Red),rect6, strfmt6);
+                       gr.DrawString(flagProcessing(strfmt6), fonttxt, brushtxt, calcRect(rect6), strfmttxt);                                          
+                       
+                       bmp.Save("fontDrawingAdv.bmp", ImageFormat.Bmp);
+                       
+               }
+       }
+}