Menu

XTextFormatter and box height

Anonymous
2012-07-13
2013-04-23
  • Anonymous

    Anonymous - 2012-07-13

    Would love to be able to get the height of the box.  Currently the program counts the number of lines to be printed, and uses that to calculate the height of the printed text.  Pretty cumbersome tho.

     
  • J-B Herbert

    J-B Herbert - 2012-08-16

    Here is a function I added to the class XTextFormatter to calculate the size of the printed text :

            /// <summary>
            /// Get bounding box for the text drawn with the last call of this instance's DrawString().
            /// </summary>
            public XRect GetLastBoundingBox()
            {
                double dx = layoutRectangle.Location.x;
                double dy = layoutRectangle.Location.y;
                double max_x = 0, max_y = 0, min_x = 0, min_y = 0;

                foreach (var block in this.blocks)
                {
                    if (block.Location.x < min_x)
                    {
                        min_x = block.Location.x;
                    }

                    if (block.Location.y < min_y)
                    {
                        min_y = block.Location.y;
                    }

                    if ((block.Location.x + block.Width) > max_x)
                    {
                        max_x = block.Location.x + block.Width;
                    }

                    if ((block.Location.y + lineSpace) > max_y)
                    {
                        max_y = block.Location.y + lineSpace;
                    }
                }

                return (new XRect(min_x + dx, min_y + dy, max_x - min_x, max_y - min_y));
            }

     

Log in to post a comment.