diff --git a/src/Text.cs b/src/Text.cs index 2710fa0..7ed4274 100644 --- a/src/Text.cs +++ b/src/Text.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Windows; using System.Windows.Media; using Autodesk.DesignScript.Geometry; @@ -9,18 +11,29 @@ namespace DynamoText { public static class Text { - public static IEnumerable FromStringOriginAndScale(string text, Point origin, double scale) + public static IEnumerable FromStringOriginAndScale( + string text, + Point origin, + double scale, + string fontFamily = "Arial", + bool bold = false, + bool italic = false) { //http://msdn.microsoft.com/en-us/library/ms745816(v=vs.110).aspx var crvs = new List(); - var font = new System.Windows.Media.FontFamily("Arial"); - var fontStyle = FontStyles.Normal; - var fontWeight = FontWeights.Medium; + // Validate that the font family exists + if (!Fonts.SystemFontFamilies.Any(f => f.Source.Equals(fontFamily, StringComparison.OrdinalIgnoreCase))) + { + throw new ArgumentException( + $"Font family '{fontFamily}' not found. Use GetInstalledFontNames() to get a list of available fonts.", + nameof(fontFamily)); + } - //if (Bold == true) fontWeight = FontWeights.Bold; - //if (Italic == true) fontStyle = FontStyles.Italic; + var font = new System.Windows.Media.FontFamily(fontFamily); + var fontStyle = italic ? FontStyles.Italic : FontStyles.Normal; + var fontWeight = bold ? FontWeights.Bold : FontWeights.Medium; // Create the formatted text based on the properties set. var formattedText = new FormattedText( @@ -73,6 +86,14 @@ public static IEnumerable FromStringOriginAndScale(string text, Point ori return crvs; } + public static IList GetInstalledFontNames() + { + return Fonts.SystemFontFamilies + .Select(f => f.Source) + .OrderBy(name => name, StringComparer.OrdinalIgnoreCase) + .ToList(); + } + private static Line LineBetweenPoints(Point origin, double scale, System.Windows.Point a, System.Windows.Point b) { var pt1 = Point.ByCoordinates((a.X * scale) + origin.X, ((-a.Y + 1) * scale) + origin.Y, origin.Z);