/// <summary> /// /// </summary> /// <param name="drawingContext"></param> /// <param name="mainAtomMetrics"></param> /// <param name="hMetrics"></param> /// <param name="isoMetrics"></param> /// <param name="defaultHOrientation"></param> /// <returns></returns> private LabelMetrics DrawCharges(DrawingContext drawingContext, AtomTextMetrics mainAtomMetrics, AtomTextMetrics hMetrics, LabelMetrics isoMetrics, CompassPoints defaultHOrientation) { var chargeString = AtomHelpers.GetChargeString(Charge); var chargeText = DrawChargeOrRadical(drawingContext, mainAtomMetrics, hMetrics, isoMetrics, chargeString, Fill, defaultHOrientation); chargeText.TextMetrics.FlattenedPath = chargeText.TextRun.GetOutline(); return(chargeText.TextMetrics); }
private void RenderAtom(DrawingContext drawingContext) { //renders the atom complete with charges, hydrogens and labels. //this code is *complex* - alter it at your own risk! List <Point> symbolPoints = new List <Point>(); List <Point> hydrogenPoints = new List <Point>(); //private variables used to keep track of onscreen visuals AtomTextMetrics hydrogenMetrics = null; LabelMetrics isoMetrics = null; SubscriptedGroup subscriptedGroup = null; Hull = new List <Point>(); //stage 1: measure up the main atom symbol in position //we need the metrics first if (AtomSymbol != "") { var symbolText = new GlyphText(AtomSymbol, SymbolTypeface, GlyphText.SymbolSize, PixelsPerDip()); symbolText.MeasureAtCenter(Position); //grab the hull for later if (symbolText.FlattenedPath != null) { Hull.AddRange(symbolText.FlattenedPath); } } //stage 2. grab the main atom metrics br drawing it var mainAtomMetrics = DrawSelf(drawingContext); //if it's a vertex atom we need the hull if (AtomSymbol == "") { Hull.AddRange(mainAtomMetrics.FlattenedPath); } //stage 3: measure up the hydrogens //if we have implicit hydrogens and we have an explicit label, draw them if (ImplicitHydrogenCount > 0 && AtomSymbol != "") { var defaultHOrientation = ParentAtom.GetDefaultHOrientation(); subscriptedGroup = new SubscriptedGroup(ImplicitHydrogenCount, "H", GlyphText.SymbolSize); hydrogenMetrics = subscriptedGroup.Measure(mainAtomMetrics, defaultHOrientation, PixelsPerDip()); subscriptedGroup.DrawSelf(drawingContext, hydrogenMetrics, PixelsPerDip(), Fill); hydrogenPoints = hydrogenMetrics.FlattenedPath; Hull.AddRange(hydrogenPoints); } //stage 6: draw an isotope label if needed if (Isotope != null) { isoMetrics = DrawIsotopeLabel(drawingContext, mainAtomMetrics, hydrogenMetrics); Hull.AddRange(isoMetrics.Corners); } //stage7: draw any charges if ((Charge ?? 0) != 0) { LabelMetrics cMetrics = DrawCharges(drawingContext, mainAtomMetrics, hydrogenMetrics, isoMetrics, ParentAtom.GetDefaultHOrientation()); Hull.AddRange(cMetrics.FlattenedPath); } //stage 8: recalculate the hull if (Hull.Any()) { //sort the points properly before doing a hull calculation var sortedHull = (from Point p in Hull orderby p.X, p.Y descending select p).ToList(); Hull = Geometry <Point> .GetHull(sortedHull, p => p); // Diag: Show the Hull #if DEBUG #if SHOWHULLS ShowHull(Hull, drawingContext); #endif #endif // End Diag } // Diag: Show the Atom Point #if DEBUG #if SHOWATOMCENTRES drawingContext.DrawEllipse(Brushes.Red, null, ParentAtom.Position, 5, 5); #endif #endif // End Diag }
private static void RotateUntilClear(AtomTextMetrics mainAtomMetrics, AtomTextMetrics hMetrics, LabelMetrics isoMetrics, Vector labelOffset, GlyphText labelText, out Point labelCenter, CompassPoints defHOrientation) { Matrix rotator = new Matrix(); double angle = Globals.ClockDirections.II.ToDegrees(); rotator.Rotate(angle); labelOffset = labelOffset * rotator; Rect bb = new Rect(); Rect bb2 = new Rect(); if (hMetrics != null) { bb = hMetrics.TotalBoundingBox; } if (isoMetrics != null) { bb2 = isoMetrics.BoundingBox; } labelCenter = mainAtomMetrics.Geocenter + labelOffset; labelText.MeasureAtCenter(labelCenter); double increment; if (defHOrientation == CompassPoints.East) { increment = -10; } else { increment = 10; } while (labelText.CollidesWith(mainAtomMetrics.TotalBoundingBox, bb, bb2) & Math.Abs(angle - 30) > 0.001) { rotator = new Matrix(); angle += increment; rotator.Rotate(increment); labelOffset = labelOffset * rotator; labelCenter = mainAtomMetrics.Geocenter + labelOffset; labelText.MeasureAtCenter(labelCenter); } }
/// <param name="drawingContext"></param> /// <param name="mainAtomMetrics"> /// </param> /// <param name="hMetrics"> /// </param> /// <param name="isoMetrics"> /// </param> /// <param name="chargeString"></param> /// <param name="fill"></param> /// <param name="defaultHOrientation"></param> /// <returns></returns> /// <summary> /// Draws a charge or radical label at the given point /// </summary> /// <returns></returns> private ChargeLabelText DrawChargeOrRadical(DrawingContext drawingContext, AtomTextMetrics mainAtomMetrics, AtomTextMetrics hMetrics, LabelMetrics isoMetrics, string chargeString, Brush fill, CompassPoints defaultHOrientation) { ChargeLabelText chargeText = new ChargeLabelText(chargeString, PixelsPerDip()); //try to place the charge at 2 o clock to the atom Vector chargeOffset = BasicGeometry.ScreenNorth * GlyphText.SymbolSize * 0.9; RotateUntilClear(mainAtomMetrics, hMetrics, isoMetrics, chargeOffset, chargeText, out var chargeCenter, defaultHOrientation); chargeText.MeasureAtCenter(chargeCenter); chargeText.Fill = fill; chargeText.DrawAtBottomLeft(chargeText.TextMetrics.BoundingBox.BottomLeft, drawingContext); return(chargeText); }