public void Union(GlyphText gt) { Rect res = TextMetrics.BoundingBox; res.Union(gt.TextMetrics.TotalBoundingBox); TextMetrics.TotalBoundingBox = res; }
//draws the main atom symbol, or an ellipse if necessary private AtomTextMetrics DrawSelf(DrawingContext drawingContext, bool measureOnly = false) { if (AtomSymbol != "") { var symbolText = new GlyphText(AtomSymbol, SymbolTypeface, SymbolSize, PixelsPerDip()); symbolText.Fill = Fill; symbolText.MeasureAtCenter(Position); if (!measureOnly) { symbolText.DrawAtBottomLeft(symbolText.TextMetrics.BoundingBox.BottomLeft, drawingContext); } return(symbolText.TextMetrics); } else { //so draw a circle double radiusX = SymbolSize / 3; Rect boundingBox = new Rect(new Point(Position.X - radiusX, Position.Y - radiusX), new Point(Position.X + radiusX, Position.Y + radiusX)); return(new AtomTextMetrics { BoundingBox = boundingBox, Geocenter = Position, TotalBoundingBox = boundingBox, FlattenedPath = new List <Point> { boundingBox.BottomLeft, boundingBox.TopLeft, boundingBox.TopRight, boundingBox.BottomRight } }); } }
/// <summary> /// Measures the dimensions of the atom prior to rendering /// </summary> /// <param name="parentMetrics">Metrics of the parent atom</param> /// <param name="direction">Orientation of the group relative to the parent atom, i.e. NESW</param> /// <returns>AtomTextMetrics object describing placement</returns> public AtomTextMetrics Measure(AtomTextMetrics parentMetrics, CompassPoints direction, float pixelsPerDip) { _subText = null; List <Point> mainOutline; //first, get some initial size measurements _mainText = new GlyphText(Text, SymbolTypeface, _fontSize, pixelsPerDip); _mainText.Premeasure(); //measure up the subscript (if we have one) string subscriptText = AtomHelpers.GetSubText(Count); if (subscriptText != "") { _subText = new SubLabelText(subscriptText, pixelsPerDip); _subText.Premeasure(); } //calculate the center of the H Atom depending on the direction var groupCenter = GetAdjunctCenter(parentMetrics, direction, _mainText.GlyphInfo, _subText?.GlyphInfo); //remeasure the main text _mainText.MeasureAtCenter(groupCenter); mainOutline = _mainText.FlattenedPath; if (_subText != null) //get the offset for the subscript { Vector subscriptOffset = new Vector(_mainText.TextMetrics.TotalBoundingBox.Width + _mainText.TrailingBearing + _subText.LeadingBearing, _subText.TextMetrics.BoundingBox.Height / 2); Point subBottomLeft = _mainText.TextMetrics.TotalBoundingBox.BottomLeft + subscriptOffset; _subText.MeasureAtBottomLeft(subBottomLeft, pixelsPerDip); //merge the total bounding boxes _mainText.Union(_subText); mainOutline.AddRange(_subText.FlattenedPath); } //return the placement metrics for the subscripted atom. AtomTextMetrics result = new AtomTextMetrics { Geocenter = groupCenter, BoundingBox = _mainText.TextMetrics.BoundingBox, TotalBoundingBox = _mainText.TextMetrics.TotalBoundingBox, FlattenedPath = mainOutline }; return(result); }
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); } }
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 }