/// <summary> /// Function to add a node as a child to this node. /// </summary> /// <param name="dimensions">Dimensions that the node will occupy.</param> public GlyphNode AddNode(Size dimensions) { if (!IsLeaf) { GlyphNode node = Left.AddNode(dimensions); // If we can't add the node to the left, then try the right. return(node ?? Right.AddNode(dimensions)); } // Do nothing if there's no more room. if (_noMoreRoom) { return(null); } // Ensure we can fit this node. if ((dimensions.Width > Region.Width) || (dimensions.Height > Region.Height)) { return(null); } // We have an exact fit, so there will be no more room for other nodes. if ((dimensions.Width == Region.Width) && (dimensions.Height == Region.Height)) { _noMoreRoom = true; return(this); } Left = new GlyphNode(this); Right = new GlyphNode(this); // Subdivide. var delta = new Size(Region.Width - dimensions.Width, Region.Height - dimensions.Height); if (delta.Width > delta.Height) { Left.Region = new Rectangle(Region.Left, Region.Top, dimensions.Width, Region.Height); Right.Region = new Rectangle(Region.Left + dimensions.Width, Region.Top, delta.Width, Region.Height); } else { Left.Region = new Rectangle(Region.Left, Region.Top, Region.Width, dimensions.Height); Right.Region = new Rectangle(Region.Left, Region.Top + dimensions.Height, Region.Width, delta.Height); } return(Left.AddNode(dimensions)); }
/// <summary> /// Function to add a node to the /// </summary> /// <param name="dimensions">The glyph dimensions.</param> /// <returns>A rectangle for the area on the image that the glyph will be located at, or <b>null</b> if there's no room.</returns> public static Rectangle? Add(Size dimensions) { if ((dimensions.Width > Root.Region.Width) || (dimensions.Height > Root.Region.Height)) { throw new ArgumentOutOfRangeException(nameof(dimensions), Resources.GORGFX_ERR_FONT_GLYPH_NODE_TOO_LARGE); } // Do nothing here. if ((dimensions.Width == 0) || (dimensions.Height == 0)) { return Rectangle.Empty; } GlyphNode newNode = Root.AddNode(dimensions); return newNode?.Region; }
/// <summary> /// Initializes a new instance of the <see cref="GlyphNode"/> class. /// </summary> /// <param name="parentNode">The parent node.</param> public GlyphNode(GlyphNode parentNode) { Region = Rectangle.Empty; Parent = parentNode; }
/// <summary> /// Function to create the root node. /// </summary> /// <param name="textureWidth">The width of the texture.</param> /// <param name="textureHeight">The height of the texture.</param> public static void CreateRoot(int textureWidth, int textureHeight) => Root = new GlyphNode(null) { Region = new Rectangle(0, 0, textureWidth, textureHeight) };