Filter:   InfoImg
download Font.cs
Language: C#
License: GPL
Copyright: (C) 2003 Southern Storm Software, Pty Ltd.
LOC: 537
Project Info
DotGNU Portable.NET(dotgnu-pnet)
Server: Savannah GNU
Type: cvs
...net\pnetlib\System.Drawing\
   .cvsignore
   Bitmap.cs
   Brush.cs
   Brushes.cs
   CharacterRange.cs
   Color.cs
   ColorConverter.cs
   ColorTranslator.cs
   ContentAlignment.cs
   Font.cs
   FontConverter.cs
   FontFamily.cs
   FontStyle.cs
   Graphics.cs
   GraphicsUnit.cs
   Icon.cs
   IconConverter.cs
   Image.cs
   ImageAnimator.cs
   ImageConverter.cs
   ImageFormatConverter.cs
   KnownColor.cs
   Makefile.am
   Pen.cs
   Pens.cs
   Point.cs
   PointConverter.cs
   PointF.cs
   Rectangle.cs
   RectangleConverter.cs
   RectangleF.cs
   Region.cs
   RotateFlipType.cs
   S.cs
   Size.cs
   SizeConverter.cs
   SizeF.cs
   SolidBrush.cs
   StringAlignment.cs
   StringDigitSubstitute.cs
   StringFormat.cs
   StringFormatFlags.cs
   StringTrimming.cs
   StringUnit.cs
   System.Drawing.build
   SystemBrushes.cs
   SystemColors.cs
   SystemIcons.cs
   SystemPens.cs
   TextureBrush.cs
   TODOAttribute.cs
   ToolboxBitmapAttribute.cs
   XorBrush.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * Font.cs - Implementation of the "System.Drawing.Font" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Drawing
{

using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Drawing.Toolkit;
using System.ComponentModel;
using System.Drawing.Design;

#if CONFIG_COMPONENT_MODEL_DESIGN
[Editor("System.Drawing.Design.FontEditor, System.Drawing.Design",
		typeof(UITypeEditor))]
[TypeConverter(typeof(FontConverter))]
#endif
#if !ECMA_COMPAT
[Serializable]
[ComVisible(true)]
#endif
public sealed class Font
	: MarshalByRefObject, ICloneable, IDisposable
#if CONFIG_SERIALIZATION
	, ISerializable
#endif
{
	// Internal state.
	private IToolkit toolkit;
	private IToolkitFont toolkitFont;
	private FontFamily fontFamily;
	private FontStyle fontStyle;
	private byte gdiCharSet;
	private bool gdiVerticalFont;
	private float size;
	private GraphicsUnit unit;
	// Cached height of the font in pixels.
	private int pixelHeight;

	// Constructors.
	public Font(Font prototype, FontStyle newStyle)
			: this(prototype.FontFamily, prototype.Size, newStyle,
				   prototype.Unit, prototype.GdiCharSet,
				   prototype.GdiVerticalFont) {}
	public Font(FontFamily family, float emSize)
			: this(family, emSize, FontStyle.Regular,
			       GraphicsUnit.Point, 1, false) {}
	public Font(FontFamily family, float emSize, FontStyle style)
			: this(family, emSize, style, GraphicsUnit.Point, 1, false) {}
	public Font(FontFamily family, float emSize, GraphicsUnit unit)
			: this(family, emSize, FontStyle.Regular, unit, 1, false) {}
	public Font(FontFamily family, float emSize, FontStyle style,
				GraphicsUnit unit)
			: this(family, emSize, style, unit, 1, false) {}
	public Font(FontFamily family, float emSize, FontStyle style,
				GraphicsUnit unit, byte gdiCharSet)
			: this(family, emSize, style, unit, gdiCharSet, false) {}
	public Font(String familyName, float emSize)
			: this(new FontFamily(familyName), emSize,
			       FontStyle.Regular, GraphicsUnit.Point, 1, false) {}
	public Font(String familyName, float emSize, FontStyle style)
			: this(new FontFamily(familyName), emSize,
			       style, GraphicsUnit.Point, 1, false) {}
	public Font(String familyName, float emSize, GraphicsUnit unit)
			: this(new FontFamily(familyName), emSize,
			       FontStyle.Regular, unit, 1, false) {}
	public Font(String familyName, float emSize, FontStyle style,
				GraphicsUnit unit)
			: this(new FontFamily(familyName), emSize,
			       style, unit, 1, false) {}
	public Font(String familyName, float emSize, FontStyle style,
				GraphicsUnit unit, byte gdiCharSet)
			: this(new FontFamily(familyName), emSize,
			       style, unit, gdiCharSet, false) {}
	public Font(String familyName, float emSize, FontStyle style,
				GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)
			: this(new FontFamily(familyName), emSize,
			       style, unit, gdiCharSet, gdiVerticalFont) {}
	public Font(FontFamily family, float emSize, FontStyle style,
				GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)
			{
				if(family == null)
				{
					throw new ArgumentNullException("family");
				}
				this.fontFamily = family;
				this.size = emSize;
				this.fontStyle = style;
				this.unit = unit;
				this.gdiCharSet = gdiCharSet;
				this.gdiVerticalFont = gdiVerticalFont;
			}
#if CONFIG_SERIALIZATION
	internal Font(SerializationInfo info, StreamingContext context)
			{
				if(info == null)
				{
					throw new ArgumentNullException("info");
				}
				fontFamily = new FontFamily(info.GetString("Name"));
				size = info.GetSingle("Size");
				fontStyle = (FontStyle)info.GetValue
					("Style", typeof(FontStyle));
				unit = (GraphicsUnit)info.GetValue
					("Unit", typeof(GraphicsUnit));
				gdiCharSet = 1;
				gdiVerticalFont = false;
			}
#endif
	private Font(IToolkitFont font)
			{
				this.toolkit = toolkit;
				this.toolkitFont = font;
				// TODO: load the font information from the IToolkitFont
			}

	// Destructor.
	~Font()
			{
				Dispose();
			}

	// Font properties.
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public bool Bold
			{
				get
				{
					return ((fontStyle & FontStyle.Bold) != 0);
				}
			}
#if CONFIG_COMPONENT_MODEL
	[Browsable(false)]
#endif
	public FontFamily FontFamily
			{
				get
				{
					return fontFamily;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public byte GdiCharSet
			{
				get
				{
					return gdiCharSet;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public bool GdiVerticalFont
			{
				get
				{
					return gdiVerticalFont;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[Browsable(false)]
#endif
	public int Height
			{
				get
				{
				#if CONFIG_EXTENDED_NUMERICS
					return (int)(Math.Ceiling(GetHeight()));
				#else
					return (int)(GetHeight() + 0.99f);
				#endif
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public bool Italic
			{
				get
				{
					return ((fontStyle & FontStyle.Italic) != 0);
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
	[TypeConverter(typeof(FontConverter.FontNameConverter))]
#endif
#if CONFIG_COMPONENT_MODEL_DESIGN
	[Editor("System.Drawing.Design.FontNameEditor, System.Drawing.Design",
			typeof(UITypeEditor))]
#endif
	public String Name
			{
				get
				{
					return fontFamily.Name;
				}
			}
	public float Size
			{
				get
				{
					return size;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[Browsable(false)]
#endif
	public float SizeInPoints
			{
				get
				{
					float adjust;
					switch(unit)
					{
						case GraphicsUnit.World:
						case GraphicsUnit.Pixel:
						default:
						{
							adjust = 72.0f / ToolkitManager.Toolkit.GetDefaultGraphics().DpiY;
						}
						break;

						case GraphicsUnit.Display:
						{
							adjust = 72.0f / 75.0f;
						}
						break;

						case GraphicsUnit.Point:
						{
							return size;
						}
						// Not reached.

						case GraphicsUnit.Inch:
						{
							adjust = 72.0f;
						}
						break;

						case GraphicsUnit.Document:
						{
							adjust = 72.0f / 300.0f;
						}
						break;

						case GraphicsUnit.Millimeter:
						{
							adjust = 72.0f / (75.0f / 25.4f);
						}
						break;
					}
					return size * adjust;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public bool Strikeout
			{
				get
				{
					return ((fontStyle & FontStyle.Strikeout) != 0);
				}
			}
#if CONFIG_COMPONENT_MODEL
	[Browsable(false)]
#endif
	public FontStyle Style
			{
				get
				{
					return fontStyle;
				}
			}
#if CONFIG_COMPONENT_MODEL
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
	public bool Underline
			{
				get
				{
					return ((fontStyle & FontStyle.Underline) != 0);
				}
			}
#if CONFIG_COMPONENT_MODEL
	[TypeConverter(typeof(FontConverter.FontUnitConverter))]
#endif
	public GraphicsUnit Unit
			{
				get
				{
					return unit;
				}
			}

	// Clone this object.
	public Object Clone()
			{
				lock(this)
				{
					Font font = (Font)(MemberwiseClone());
					font.toolkit = null;
					font.toolkitFont = null;
					return font;
				}
			}

	// Dispose of this object.
	public void Dispose()
			{
				lock(this)
				{
					if(toolkitFont != null)
					{
						toolkitFont.Dispose();
						toolkitFont = null;
					}
					toolkit = null;
				}
			}

	// Determine if two objects are equal.
	public override bool Equals(Object obj)
			{
				Font other = (obj as Font);
				if(other != null)
				{
					return (other.Name == Name &&
							other.Size == Size &&
							other.Style == Style &&
							other.GdiCharSet == GdiCharSet &&
							other.GdiVerticalFont == GdiVerticalFont);
				}
				else
				{
					return false;
				}
			}

	// Get a hash code for this object.
	public override int GetHashCode()
			{
				return Name.GetHashCode() + (((int)size) << 4) +
				       (int)fontStyle;
			}

	// Extract the active font from a native device context.
	public static Font FromHdc(IntPtr hdc)
			{
				IToolkitFont font;
				font = ToolkitManager.Toolkit.GetFontFromHdc(hdc);
				if(font != null)
				{
					return new Font(font);
				}
				else
				{
					return null;
				}
			}

	// Convert a native font handle into a font object.
	public static Font FromHfont(IntPtr hfont)
			{
				IToolkitFont font;
				font = ToolkitManager.Toolkit.GetFontFromHfont(hfont);
				if(font != null)
				{
					return new Font(font);
				}
				else
				{
					return null;
				}
			}

	// Convert a native logical font descriptor into a font object.
	public static Font FromLogFont(Object lf)
			{
				return FromLogFont(lf, IntPtr.Zero);
			}
	public static Font FromLogFont(Object lf, IntPtr hdc)
			{
				IToolkitFont font;
				font = ToolkitManager.Toolkit.GetFontFromLogFont(lf, hdc);
				if(font != null)
				{
					return new Font(font);
				}
				else
				{
					return null;
				}
			}

	// Get the height of this font.
	public float GetHeight()
			{
				return GetHeight(Graphics.DefaultGraphics);
			}
	public float GetHeight(Graphics graphics)
			{
				if(graphics == null)
				{
					throw new ArgumentNullException("graphics");
				}
				
				// Get the font size in raw pixels.
				if (pixelHeight == 0)
					pixelHeight = graphics.GetLineSpacing(this);

				// If the graphics object uses pixels (the most common case),
				// then return the pixel value directly.
				if(graphics.IsPixelUnits())
				{
					return (float)pixelHeight;
				}

				// Get the dpi of the screen for the y axis.
				float dpiY = graphics.DpiY;

				// Convert the pixel value back into points.
				float points = ((float)pixelHeight) / (dpiY / 72.0f);

				// Convert the points into the graphics object's unit.
				switch(graphics.PageUnit)
				{
					case GraphicsUnit.World:
					case GraphicsUnit.Pixel:
					{
						points *= (dpiY / 72.0f);
					}
					break;

					case GraphicsUnit.Display:
					{
						points *= (75.0f / 72.0f);
					}
					break;

					case GraphicsUnit.Point: break;

					case GraphicsUnit.Inch:
					{
						points /= 72.0f;
					}
					break;

					case GraphicsUnit.Document:
					{
						points *= (300.0f / 72.0f);
					}
					break;

					case GraphicsUnit.Millimeter:
					{
						points *= (25.4f / 72.0f);
					}
					break;
				}
				return points;
			}
	public float GetHeight(float dpi)
			{
				if(unit == GraphicsUnit.World || unit == GraphicsUnit.Pixel)
				{
					return GetHeight(Graphics.DefaultGraphics);
				}
				else
				{
					return fontFamily.GetLineSpacing(fontStyle) *
							(size / fontFamily.GetEmHeight(fontStyle)) * dpi / 72f;
				}
			}

	// Convert this object into a native font handle.
	public IntPtr ToHfont()
			{
				if(toolkitFont == null)
				{
					IToolkit toolkit = ToolkitManager.Toolkit;
					float dpiY = toolkit.GetDefaultGraphics().DpiY;
					GetFont(toolkit, dpiY);
				}
				return toolkitFont.GetHfont();
			}

	// Fill in a native font information structure with info about this font.
	public void ToLogFont(Object lf)
			{
				ToLogFont(lf, null);
			}
	public void ToLogFont(Object lf, Graphics graphics)
			{
				IToolkitGraphics g;
				if(graphics != null)
				{
					g = graphics.ToolkitGraphics;
				}
				else
				{
					g = null;
				}
				lock(this)
				{
					if(toolkitFont == null)
					{
						if(g != null)
						{
							GetFont(g.Toolkit, graphics.DpiY);
						}
						else
						{
							IToolkit toolkit = ToolkitManager.Toolkit;
							float dpiY = toolkit.GetDefaultGraphics().DpiY;
							GetFont(toolkit, dpiY);
						}
					}
					toolkitFont.ToLogFont(lf, g);
				}
			}

	// Convert this object into a string.
	public override String ToString()
			{
				StringBuilder builder = new StringBuilder();
				builder.Append("[Font: Name=");
				builder.Append(Name);
				builder.Append(" Size=");
			#if CONFIG_EXTENDED_NUMERICS
				builder.Append(Size);
			#else
				builder.Append((int)Size);
			#endif
				builder.Append(" Units=");
				builder.Append((int)Unit);
				builder.Append(" GdiCharSet=");
				builder.Append(GdiCharSet);
				builder.Append(" GdiVerticalFont=");
				builder.Append(GdiVerticalFont);
				if(Style != FontStyle.Regular)
				{
					builder.Append(" Style=");
					builder.Append(Style);
				}
				builder.Append("]");
				return builder.ToString();
			}

#if CONFIG_SERIALIZATION

	// Get the serialization information for this object.
	void ISerializable.GetObjectData(SerializationInfo info,
									 StreamingContext context)
			{
				if(info == null)
				{
					throw new ArgumentNullException("info");
				}
				info.AddValue("Name", Name);
				info.AddValue("Size", Size);
				info.AddValue("Style", Style);
				info.AddValue("Unit", Unit);
			}

#endif

	// Get the toolkit version of this font for a specific toolkit.
	internal IToolkitFont GetFont(IToolkit toolkit, float dpi)
			{
				lock(this)
				{
					if(this.toolkitFont == null)
					{
						// We don't yet have a toolkit font yet.
						this.toolkitFont = toolkit.CreateFont(this, dpi);
						this.toolkit = toolkit;
						return this.toolkitFont;
					}
					else if(this.toolkit == toolkit)
					{
						// Same toolkit - return the cached pen information.
						return this.toolkitFont;
					}
					else
					{
						// We have a font object for another toolkit,
						// so dispose it and create for this toolkit.
						// We null out "toolkitFont" before calling
						// "CreateFont()" just in case an exception
						// is thrown while creating the toolkit font.
						this.toolkitFont.Dispose();
						this.toolkitFont = null;
						this.toolkitFont = toolkit.CreateFont(this, dpi);
						this.toolkit = toolkit;
						return this.toolkitFont;
					}
				}
			}

}; // class Font

}; // namespace System.Drawing