From 813404e63b4e6c6c9c99bf774ec8312ca4d9bcd3 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sun, 28 Jan 2018 07:53:23 -0800 Subject: [PATCH] Getting a font selector in tools --- ApplicationView/ApplicationController.cs | 28 + DesignTools/MeshObjects.cs | 56 +- DesignTools/PubicPropertyEditor.cs | 45 +- StaticData/Fonts/Damion-Regular.svg | 1185 ++++++++++++++++++++++ Submodules/agg-sharp | 2 +- 5 files changed, 1288 insertions(+), 28 deletions(-) create mode 100644 StaticData/Fonts/Damion-Regular.svg diff --git a/ApplicationView/ApplicationController.cs b/ApplicationView/ApplicationController.cs index 58bc9e277..75e16dc48 100644 --- a/ApplicationView/ApplicationController.cs +++ b/ApplicationView/ApplicationController.cs @@ -668,6 +668,34 @@ namespace MatterHackers.MatterControl } } + private static TypeFace titilliumTypeFace = null; + public static TypeFace TitilliumTypeFace + { + get + { + if (titilliumTypeFace == null) + { + titilliumTypeFace = TypeFace.LoadFrom(AggContext.StaticData.ReadAllText(Path.Combine("Fonts", "TitilliumWeb-Black.svg"))); + } + + return titilliumTypeFace; + } + } + + private static TypeFace damionTypeFace = null; + public static TypeFace DamionTypeFace + { + get + { + if (damionTypeFace == null) + { + damionTypeFace = TypeFace.LoadFrom(AggContext.StaticData.ReadAllText(Path.Combine("Fonts", "Damion-Regular.svg"))); + } + + return damionTypeFace; + } + } + public static Task LoadCacheableAsync(string cacheKey, string cacheScope, string staticDataFallbackPath = null) where T : class { string cachePath = CacheablePath(cacheScope, cacheKey); diff --git a/DesignTools/MeshObjects.cs b/DesignTools/MeshObjects.cs index e962a95aa..3a042b22f 100644 --- a/DesignTools/MeshObjects.cs +++ b/DesignTools/MeshObjects.cs @@ -42,6 +42,7 @@ using MatterHackers.MatterControl.PartPreviewWindow.View3D; using MatterHackers.PolygonMesh; using MatterHackers.RenderOpenGl; using MatterHackers.VectorMath; +using Newtonsoft.Json; namespace MatterHackers.MatterControl.DesignTools { @@ -386,16 +387,41 @@ public class ChairFoot2 : MatterCadObject3D } } + public enum NamedTypeFace { Liberation_Sans, Liberation_Sans_Bold, Liberation_Mono, Titillium, Damion }; + + public static class NamedTypeFaceCache + { + public static TypeFace GetTypeFace(NamedTypeFace Name) + { + switch (Name) + { + case NamedTypeFace.Liberation_Sans: + return LiberationSansFont.Instance; + + case NamedTypeFace.Liberation_Sans_Bold: + return LiberationSansBoldFont.Instance; + + case NamedTypeFace.Liberation_Mono: + return ApplicationController.MonoSpacedTypeFace; + + case NamedTypeFace.Titillium: + return ApplicationController.TitilliumTypeFace; + + case NamedTypeFace.Damion: + return ApplicationController.DamionTypeFace; + + default: + return LiberationSansFont.Instance; + } + } + } + public class TextPrimitive : Object3D, IRebuildable { [DisplayName("Name")] public string NameToWrite { get; set; } = "Text"; - public enum Style { Sans, SansBold, Mono }; - public Style FontStyle = Style.Sans; - - public bool Bold { get; set; } = false; - public bool Mono { get; set; } = false; + public NamedTypeFace Font { get; set; } = new NamedTypeFace(); public double PointSize { get; set; } = 24; @@ -410,14 +436,7 @@ public class ChairFoot2 : MatterCadObject3D public void Rebuild() { - - var letterPrinter = new TypeFacePrinter(NameToWrite, PointSize * 0.352778, bold: Bold); - - if (Mono) - { - letterPrinter = new TypeFacePrinter(NameToWrite, new StyledTypeFace(ApplicationController.MonoSpacedTypeFace, PointSize * 0.352778)); - } - + var letterPrinter = new TypeFacePrinter(NameToWrite, new StyledTypeFace(NamedTypeFaceCache.GetTypeFace(Font), PointSize * 0.352778)); IObject3D nameMesh = new Object3D() { @@ -750,8 +769,6 @@ public class ChairFoot2 : MatterCadObject3D { public override string ActiveEditor => "PublicPropertyEditor"; - private static TypeFace typeFace = null; - public RibonWithName() { Rebuild(); @@ -760,18 +777,15 @@ public class ChairFoot2 : MatterCadObject3D [DisplayName("Name")] public string NameToWrite { get; set; } = "MatterHackers"; + public NamedTypeFace Font { get; set; } = new NamedTypeFace(); + public void Rebuild() { IObject3D cancerRibonStl = Object3D.Load("Cancer_Ribbon.stl", CancellationToken.None); cancerRibonStl = new Rotate(cancerRibonStl, MathHelper.DegreesToRadians(90)); - if (typeFace == null) - { - typeFace = TypeFace.LoadFrom(AggContext.StaticData.ReadAllText(Path.Combine("Fonts", "TitilliumWeb-Black.svg"))); - } - - var letterPrinter = new TypeFacePrinter(NameToWrite.ToUpper(), new StyledTypeFace(typeFace, 12)); + var letterPrinter = new TypeFacePrinter(NameToWrite.ToUpper(), new StyledTypeFace(NamedTypeFaceCache.GetTypeFace(Font), 12)); IObject3D nameMesh = new Object3D() { diff --git a/DesignTools/PubicPropertyEditor.cs b/DesignTools/PubicPropertyEditor.cs index b2854af5d..b37a1eac2 100644 --- a/DesignTools/PubicPropertyEditor.cs +++ b/DesignTools/PubicPropertyEditor.cs @@ -55,6 +55,17 @@ namespace MatterHackers.MatterControl.DesignTools public bool Unlocked { get; } = true; + + static Type[] allowedTypes = + { + typeof(double), typeof(int), typeof(string), typeof(bool), + typeof(NamedTypeFace) + }; + + static BindingFlags ownedPropertiesOnly = BindingFlags.Public + | System.Reflection.BindingFlags.Instance + | System.Reflection.BindingFlags.DeclaredOnly; + public GuiWidget Create(IObject3D item, View3DWidget view3DWidget, ThemeConfig theme) { this.view3DWidget = view3DWidget; @@ -105,12 +116,6 @@ namespace MatterHackers.MatterControl.DesignTools { var rebuildable = item as IRebuildable; - var allowedTypes = new Type[] { typeof(double), typeof(int), typeof(string), typeof(bool) }; - - var ownedPropertiesOnly = System.Reflection.BindingFlags.Public - | System.Reflection.BindingFlags.Instance - | System.Reflection.BindingFlags.DeclaredOnly; - var editableProperties = this.item.GetType().GetProperties(ownedPropertiesOnly) .Where(pi => allowedTypes.Contains(pi.PropertyType) && pi.GetGetMethod() != null @@ -198,6 +203,34 @@ namespace MatterHackers.MatterControl.DesignTools rowContainer.AddChild(textEditWidget); tabContainer.AddChild(rowContainer); } + // create a NamedTypeFace editor + else if (property.Value is NamedTypeFace namedTypeFace) + { + FlowLayoutWidget rowContainer = CreateSettingsRow(property.DisplayName.Localize()); + var availableFonts = new Dictionary(); + foreach (NamedTypeFace name in Enum.GetValues(typeof(NamedTypeFace))) + { + availableFonts.Add(name.ToString(), name.ToString().Replace('_', ' ')); + } + + var dropDownList = new DropDownList("Name".Localize(), theme.Colors.PrimaryTextColor, Direction.Down, pointSize: theme.DefaultFontSize); + + foreach (var fontName in availableFonts.OrderBy((n) => n.Value)) + { + MenuItem newItem = dropDownList.AddItem(fontName.Value); + + var localFontName = fontName; + newItem.Selected += (sender, e) => + { + NamedTypeFace key = (NamedTypeFace)Enum.Parse(typeof(NamedTypeFace), localFontName.Key); + property.PropertyInfo.GetSetMethod().Invoke(this.item, new Object[] { key }); + rebuildable?.Rebuild(); + }; + } + dropDownList.SelectedValue = namedTypeFace.ToString().Replace('_', ' '); + rowContainer.AddChild(dropDownList); + tabContainer.AddChild(rowContainer); + } } var updateButton = theme.ButtonFactory.Generate("Update".Localize()); diff --git a/StaticData/Fonts/Damion-Regular.svg b/StaticData/Fonts/Damion-Regular.svg new file mode 100644 index 000000000..fd4eee013 --- /dev/null +++ b/StaticData/Fonts/Damion-Regular.svg @@ -0,0 +1,1185 @@ + + + + +Created by FontForge 20161003 at Sat Jan 27 23:26:55 2018 + By www-data +Copyright (c) 2011 by vernon adams. All rights reserved. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 75bcac66c..84ded0e23 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 75bcac66c7f8a96e81b3ebd3ae47bce89d2c6342 +Subproject commit 84ded0e232d644f5ae96f556885cfff628503312