2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Drawing / Samples / System.Drawing / FontDrawing.cs
1 //
2 // 
3 // Simple font handeling
4 //
5 // Author:
6 //   Jordi Mas i Hernandez <jordi@ximian.com>
7 // 
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System;
33 using System.Drawing;
34 using System.Drawing.Text;
35 using System.Drawing.Imaging;
36
37 namespace Font1Sample {
38         public class FontDrawing {
39                 
40                 public static void listFonts()
41                 {               
42                         FontCollection ifc = new InstalledFontCollection();
43                         foreach( FontFamily ffm in ifc.Families) {                              
44                                 try
45                                 {
46                                         Font f = new Font(ffm.Name,12); 
47                                         Console.WriteLine("Family Name:" + ffm.Name + "," + f.Name);
48                                 }                       
49                                 catch (Exception ex)    {}
50                         }
51                 }
52                 
53                 public static void checkFontProperties()
54                 {
55                         Font f = new Font("Arial",12);  
56                         Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
57                         
58                         f = new Font("Verdana", 12);    
59                         Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
60                         
61                         f = new Font("Courier New", 12);        
62                         Console.WriteLine("Font:" + f.Name + " size:" + f.Size);
63                                         
64                 }
65                 
66                 public static void Main( ) 
67                 {
68                         Console.WriteLine("Fonts--------------------");                 
69                         listFonts();                    
70                 
71                         Console.WriteLine("Propierties--------------------");                                           
72                         checkFontProperties();            
73                         
74                         Console.WriteLine("Draw--------------------");
75                         float width = 800.0F;
76                         float height = 650.0F;
77                         string str = "";
78                         
79                         Font f1 = new Font("Arial",12);                 
80                         Font f2 = new Font("Verdana", 12, FontStyle.Bold);      
81                         Font f3 = new Font("Courier New", 12, FontStyle.Italic);
82                         
83                         Font f4  = new Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular, GraphicsUnit.Millimeter);
84                         Console.WriteLine("Font:" + f4.Name + " size:" + f4.Size + "Points: " + f4.SizeInPoints);
85                         
86                         Font f5  = new Font(FontFamily.GenericSerif, 15, FontStyle.Regular, GraphicsUnit.Point);
87                         Console.WriteLine("Font:" + f5.Name + " size:" + f5.Size + "Points: " + f5.SizeInPoints);
88                         
89                         Font f6  = new Font("Arial", 40, FontStyle.Regular, GraphicsUnit.Pixel);
90                         Console.WriteLine("Font:" + f6.Name + " size:" + f6.Size + "Points: " + f6.SizeInPoints);
91                         
92                         Font f7  = new Font("Courier New", 19, FontStyle.Regular, GraphicsUnit.World);
93                         Console.WriteLine("Font:" + f7.Name + " size:" + f7.Size + "Points: " + f7.SizeInPoints);                                                       
94
95                         Font f8  = new Font("Courier New", 19, FontStyle.Bold |  FontStyle.Underline, GraphicsUnit.World);
96                         Console.WriteLine("Font:" + f8.Name + " size:" + f8.Size + "Points: " + f8.SizeInPoints);
97
98                         Font f9  = new Font("Courier New", 19, FontStyle.Bold |  FontStyle.Underline|  FontStyle.Italic, GraphicsUnit.World);
99                         Console.WriteLine("Font:" + f9.Name + " size:" + f9.Size + "Points: " + f9.SizeInPoints);
100
101                         Font f10  = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Strikeout, GraphicsUnit.Millimeter);
102                         Console.WriteLine("Font:" + f10.Name + " size:" + f10.Size + "Points: " + f10.SizeInPoints);
103                         
104                         
105                         Bitmap bmp = new Bitmap((int)width, (int)height);
106                         Graphics gr = Graphics.FromImage(bmp);
107                         SolidBrush br = new SolidBrush(Color.White);
108                         SolidBrush colorRed = new SolidBrush(Color.Red);
109
110                         gr.FillRectangle(br, 0.0F, 0.0F, width, height);
111                         
112                         br = new SolidBrush(Color.Black);
113
114                         str = "This an " +  f1.Name + " test string size: "+ f1.Height;                        
115                         gr.DrawString (str, f1, br, 10, 10);
116
117                         str = "This a " +  f2.Name + " bold test string size: "+ f2.Height;
118                         gr.DrawString( str, f2, colorRed, 10, 50);
119
120                         str = "This a " +  f3.Name + " italic test string size: "+ f3.Height;
121                         gr.DrawString( str, f3, br, 10, 100);
122
123                         str = "This an " +  f4.Name + " test string size: "+ f4.Height;
124                         gr.DrawString (str, f4, br, 10, 150);
125
126                         str = "This a " +  f5.Name + " test string size: "+ f5.Height;
127                         gr.DrawString( str, f5, colorRed, 10, 250);
128
129                         str = "This a " +  f6.Name + " test string size: "+ f6.Height;
130                         gr.DrawString( str, f6, br, new Rectangle(10,300,0,0));
131                         
132                         str = "This a " +  f7.Name + " test string size: "+ f7.Height;
133                         gr.DrawString( str, f7, br, 10,350);
134
135                         str = "This a " +  f8.Name + " test (Underline/Bold) string size: "+ f8.Height;
136                         gr.DrawString( str, f8, br, 10,400);
137
138                         str = "This a " +  f9.Name + " test (Underline/Bold/Italic) string size: "+ f9.Height;
139                         gr.DrawString( str, f9, br, 10,450);
140
141                         str = "This a " +  f10.Name + " test (Strikeout) string size: "+ f10.Height;
142                         gr.DrawString( str, f10, br, 10,500);
143
144                         
145                         bmp.Save("FontDrawing.bmp", ImageFormat.Bmp);                   
146                 }
147         }
148 }