/// <summary> /// Get the bounding box internal text padding to be used when drawing text. /// </summary> public static User32.DRAWTEXTPARAMS GetTextMargins( this FontCache.Scope font, TextPaddingOptions padding = default) { // DrawText(Ex) adds a small space at the beginning of the text bounding box but not at the end, // this is more noticeable when the font has the italic style. We compensate with this factor. int leftMargin = 0; int rightMargin = 0; float overhangPadding; switch (padding) { case TextPaddingOptions.GlyphOverhangPadding: // [overhang padding][Text][overhang padding][italic padding] overhangPadding = font.Data.Height / 6f; leftMargin = (int)Math.Ceiling(overhangPadding); rightMargin = (int)Math.Ceiling(overhangPadding * (1 + ItalicPaddingFactor)); break; case TextPaddingOptions.LeftAndRightPadding: // [2 * overhang padding][Text][2 * overhang padding][italic padding] overhangPadding = font.Data.Height / 6f; leftMargin = (int)Math.Ceiling(2 * overhangPadding); rightMargin = (int)Math.Ceiling(overhangPadding * (2 + ItalicPaddingFactor)); break; case TextPaddingOptions.NoPadding: default: break; } return(new User32.DRAWTEXTPARAMS { iLeftMargin = leftMargin, iRightMargin = rightMargin }); }