private List <Polygon> RenderString(TextToolOptions options, Vector offset) { var fontfamily = new FontFamily(options.Font.FontFamily.Name); var typeface = FindTypeface(options, fontfamily, EmptySynonymMap) ?? FindTypeface(options, fontfamily, SynonymMap) ?? fontfamily.GetTypefaces().First(); var decorations = new TextDecorationCollection(); if (options.Font.Strikeout) { decorations.Add(TextDecorations.Strikethrough); } if (options.Font.Underline) { decorations.Add(TextDecorations.Underline); } const double sizeFactor = 0.1; var formattedText = new FormattedText(options.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, options.Font.SizeInPoints * sizeFactor, Brushes.Black, 1) { LineHeight = options.LineHeight * options.Font.SizeInPoints * sizeFactor }; formattedText.SetTextDecorations(decorations); var smoothness = options.Smoothness; var geom = formattedText.BuildGeometry(new Point(0, 0)); List <Polygon> polys; try { (polys, smoothness) = BuildPolygons(geom, offset, smoothness); } catch (PolygonException) { var opt = TextToolOptions.Default; opt.Text = "Unable to render\nthis font without\ntopology errors."; return(RenderString(opt, offset)); } try { FinalizePolygons(polys); return(polys); } catch (TopologyException) { var opt = options; opt.Smoothness = smoothness; return(RenderString(opt, offset)); } }
public void MouseDown(MouseEventArgs mouseData) { switch (mouseData.Button) { case MouseButtons.Left: _writing = true; _currentTextPolygons = new List <Polygon>(); LevEditor.RedrawScene(); var result = TextToolForm.ShowDefault(_currentOptions, HandleChange); _writing = false; if (result.HasValue) { _currentOptions = result.Value; MarkAllAs(VectorMark.None); var rendered = RenderString(_currentOptions, CurrentPos); Lev.Polygons.AddRange(rendered); if (rendered.Count > 0) { LevEditor.Modified = true; } LevEditor.UpdateSelectionInfo(); } LevEditor.RedrawScene(); break; case MouseButtons.None: break; case MouseButtons.Right: break; case MouseButtons.Middle: break; case MouseButtons.XButton1: break; case MouseButtons.XButton2: break; default: throw new ArgumentOutOfRangeException(); } }
private static Typeface FindTypeface(TextToolOptions options, FontFamily fontfamily, Dictionary <string, string> synonymMap) { Typeface typeface = null; foreach (var familyTypeface in fontfamily.GetTypefaces()) { var faceName = familyTypeface.FaceNames[XmlLanguage.GetLanguage("en-US")]; var styleName = options.Font.Style.ToString() .Replace(",", "") .Replace("Underline", "") .Replace("Strikeout", "") .Trim(); var fixedStyleName = familyTypeface.Weight + " " + familyTypeface.Style; if (familyTypeface.Style.ToString() == styleName) { typeface = familyTypeface; break; } if (fixedStyleName == options.FontStyleName) { typeface = familyTypeface; break; } if (fixedStyleName == options.FontStyleName.Replace(faceName, "").Trim()) { typeface = familyTypeface; break; } if (synonymMap.Any(v => faceName == styleName.Replace(v.Key, v.Value))) { typeface = familyTypeface; break; } } return(typeface); }
private void HandleChange(TextToolOptions obj) { _currentTextPolygons = RenderString(obj, CurrentPos); LevEditor.RedrawScene(); }