Permalink
Browse files

Refactor Font to use partial classes for the native implementation of…

… the Font class.
  • Loading branch information...
1 parent b646772 commit c52e1a4c2ee13bb7556e7316e776d6fc8ea1cc41 @kjpou1 kjpou1 committed Nov 28, 2013
View
94 System.Drawing/Font-CoreText.cs
@@ -0,0 +1,94 @@
+using System;
+
+#if MONOMAC
+using MonoMac.CoreGraphics;
+using MonoMac.CoreText;
+#else
+using MonoTouch.CoreGraphics;
+using MonoTouch.CoreText;
+#endif
+
+namespace System.Drawing
+{
+ public partial class Font
+ {
+ internal CTFont nativeFont;
+ bool bold = false;
+ bool italic = false;
+
+
+ private void CreateNativeFont (FontFamily familyName, float emSize, FontStyle style,
+ GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont )
+ {
+ // convert to 96 Dpi to be consistent with Windows
+ var dpiSize = emSize * dpiScale;
+
+ try
+ {
+ nativeFont = new CTFont(familyName.NativeDescriptor,dpiSize);
+ }
+ catch
+ {
+ nativeFont = new CTFont("Helvetica",dpiSize);
+ }
+
+ CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;
+
+ if ((style & FontStyle.Bold) == FontStyle.Bold)
+ tMask |= CTFontSymbolicTraits.Bold;
+ if ((style & FontStyle.Italic) == FontStyle.Italic)
+ tMask |= CTFontSymbolicTraits.Italic;
+ strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
+ underLine = (style & FontStyle.Underline) == FontStyle.Underline;
+
+ var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize,tMask,tMask);
+
+ if (nativeFont2 != null)
+ nativeFont = nativeFont2;
+
+ bold = (nativeFont.SymbolicTraits & ~CTFontSymbolicTraits.Bold) == CTFontSymbolicTraits.Bold;
+ italic = (nativeFont.SymbolicTraits & ~CTFontSymbolicTraits.Italic) == CTFontSymbolicTraits.Italic;
+ sizeInPoints = emSize;
+ this.unit = unit;
+
+ // FIXME
+ // I do not like the hard coded 72 but am just trying to boot strap the Font class right now
+ size = ConversionHelpers.GraphicsUnitConversion(GraphicsUnit.Point, unit, 72.0f, sizeInPoints);
+
+ }
+
+ /**
+ *
+ * Returns: The line spacing, in pixels, of this font.
+ *
+ * The line spacing of a Font is the vertical distance between the base lines of
+ * two consecutive lines of text. Thus, the line spacing includes the blank space
+ * between lines along with the height of the character itself.
+ *
+ * If the Unit property of the font is set to anything other than GraphicsUnit.Pixel,
+ * the height (in pixels) is calculated using the vertical resolution of the
+ * screen display. For example, suppose the font unit is inches and the font size
+ * is 0.3. Also suppose that for the corresponding font family, the em-height
+ * is 2048 and the line spacing is 2355. For a screen display that has a vertical
+ * resolution of 96 dots per inch, you can calculate the height as follows:
+ *
+ * 2355*(0.3/2048)*96 = 33.11719
+ *
+ **/
+ private float GetNativeheight()
+ {
+ // Documentation for Accessing Font Metrics
+ // http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html
+ float lineHeight = 0;
+ lineHeight += nativeFont.AscentMetric;
+ lineHeight += nativeFont.DescentMetric;
+ lineHeight += nativeFont.LeadingMetric;
+
+
+ // Still have not figured this out yet!!!!
+ return lineHeight;
+
+ }
+ }
+}
+
View
96 System.Drawing/Font.cs
@@ -1,27 +1,29 @@
using System;
using System.Runtime.Serialization;
-#if MONOMAC
-using MonoMac.CoreGraphics;
-using MonoMac.CoreText;
-#else
-using MonoTouch.CoreGraphics;
-using MonoTouch.CoreText;
-#endif
+//#if MONOMAC
+//using MonoMac.CoreGraphics;
+//using MonoMac.CoreText;
+//#else
+//using MonoTouch.CoreGraphics;
+//using MonoTouch.CoreText;
+//#endif
namespace System.Drawing
{
- public sealed class Font : MarshalByRefObject, ISerializable, ICloneable, IDisposable {
+ public sealed partial class Font : MarshalByRefObject, ISerializable, ICloneable, IDisposable
+ {
+
const byte DefaultCharSet = 1;
- internal CTFont nativeFont;
float sizeInPoints = 0;
GraphicsUnit unit = GraphicsUnit.Point;
float size;
bool underLine = false;
bool strikeThrough = false;
FontFamily fontFamily;
+ FontStyle fontStyle;
static float dpiScale = 96f / 72f;
@@ -86,43 +88,12 @@ public Font (string familyName, float emSize, FontStyle style, GraphicsUnit unit
{
fontFamily = familyName;
+ fontStyle = style;
if (emSize <= 0)
throw new ArgumentException("emSize is less than or equal to 0, evaluates to infinity, or is not a valid number.","emSize");
-
- // convert to 96 Dpi to be consistent with Windows
- var dpiSize = emSize * dpiScale;
-
- try {
- nativeFont = new CTFont(familyName.NativeDescriptor,dpiSize);
- }
- catch
- {
- //nativeFont = new CTFont("Lucida Grande",emSize);
- nativeFont = new CTFont("Helvetica",dpiSize);
- }
-
- CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;
-
- if ((style & FontStyle.Bold) == FontStyle.Bold)
- tMask |= CTFontSymbolicTraits.Bold;
- if ((style & FontStyle.Italic) == FontStyle.Italic)
- tMask |= CTFontSymbolicTraits.Italic;
- strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
- underLine = (style & FontStyle.Underline) == FontStyle.Underline;
-
- var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize,tMask,tMask);
-
- if (nativeFont2 != null)
- nativeFont = nativeFont2;
-
- sizeInPoints = emSize;
- this.unit = unit;
-
- // FIXME
- // I do not like the hard coded 72 but am just trying to boot strap the Font class right now
- size = ConversionHelpers.GraphicsUnitConversion(GraphicsUnit.Point, unit, 72.0f, sizeInPoints);
+ CreateNativeFont (familyName, emSize, style, unit, gdiCharSet, gdiVerticalFont);
}
#region ISerializable implementation
@@ -183,12 +154,12 @@ public float Size
public bool Bold
{
- get { return (nativeFont.SymbolicTraits & ~CTFontSymbolicTraits.Bold) == CTFontSymbolicTraits.Bold; }
+ get { return bold; }
}
public bool Italic
{
- get { return (nativeFont.SymbolicTraits & ~CTFontSymbolicTraits.Italic) == CTFontSymbolicTraits.Italic; }
+ get { return italic; }
}
public bool Underline
@@ -209,38 +180,15 @@ public FontFamily FontFamily
{
get { return fontFamily; }
}
- /**
- *
- * Returns: The line spacing, in pixels, of this font.
- *
- * The line spacing of a Font is the vertical distance between the base lines of
- * two consecutive lines of text. Thus, the line spacing includes the blank space
- * between lines along with the height of the character itself.
- *
- * If the Unit property of the font is set to anything other than GraphicsUnit.Pixel,
- * the height (in pixels) is calculated using the vertical resolution of the
- * screen display. For example, suppose the font unit is inches and the font size
- * is 0.3. Also suppose that for the corresponding font family, the em-height
- * is 2048 and the line spacing is 2355. For a screen display that has a vertical
- * resolution of 96 dots per inch, you can calculate the height as follows:
- *
- * 2355*(0.3/2048)*96 = 33.11719
- *
- **/
- public float GetHeight()
- {
-
-
- // Documentation for Accessing Font Metrics
- // http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html
- float lineHeight = 0;
- lineHeight += nativeFont.AscentMetric;
- lineHeight += nativeFont.DescentMetric;
- lineHeight += nativeFont.LeadingMetric;
+ public FontStyle Style
+ {
+ get { return fontStyle; }
+ }
- // Still have not figured this out yet!!!!
- return lineHeight;
+ public float GetHeight()
+ {
+ return GetNativeheight ();
}
}
}
View
9 System.Drawing/FontFamily.cs
@@ -61,13 +61,8 @@ public FontFamily(GenericFontFamilies genericFamily)
}
public FontFamily (string name)
- {
- if (string.IsNullOrEmpty (name))
- throw new ArgumentException ("name can not be null or empty");
-
- CreateNativeFontFamily (name, false);
-
- }
+ : this (name, false)
+ { }
internal FontFamily (string name, bool createDefaultIfNotExists)
{
View
3 sysdrawing-coregraphics-Mac/sysdrawing-coregraphics-Mac.csproj
@@ -324,6 +324,9 @@
<Compile Include="..\System.Drawing.Text\PrivateFontCollection-CoreText.cs">
<Link>System.Drawing.Text\PrivateFontCollection-CoreText.cs</Link>
</Compile>
+ <Compile Include="..\System.Drawing\Font-CoreText.cs">
+ <Link>System.Drawing\Font-CoreText.cs</Link>
+ </Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
View
1 sysdrawing-coregraphics.csproj
@@ -251,6 +251,7 @@
<Compile Include="System.Drawing.Text\FontCollection-CoreText.cs" />
<Compile Include="System.Drawing.Text\PrivateFontCollection.cs" />
<Compile Include="System.Drawing.Text\PrivateFontCollection-CoreText.cs" />
+ <Compile Include="System.Drawing\Font-CoreText.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="External\" />

0 comments on commit c52e1a4

Please sign in to comment.