Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="GraphicRef"></param>
        /// <param name="GraphicString"></param>
        /// <param name="clipRectangle"></param>
        /// <param name="fontResourceName">Either Font Family Name or the full path to the Font Resourse (RESX)</param>
        /// <param name="MaxFontSize"></param>
        /// <param name="MinFontSize"></param>
        /// <param name="SmallestOnFail"></param>
        /// <returns></returns>
        static public Font GetAdjustedFont(System.Drawing.Graphics GraphicRef, string GraphicString, System.Drawing.Rectangle clipRectangle, string fontResourceName, int MaxFontSize = 100, int MinFontSize = 10, bool SmallestOnFail = true)
        {
            bool privateFont  = fontResourceName.ToLower().EndsWith("ttf");
            Font OriginalFont = null;

            try {
                if (privateFont)
                {
                    OriginalFont = GetFontFromResx(fontResourceName);
                }
                else
                {
                    OriginalFont = new Font(fontResourceName, 100);
                }

                // We utilize MeasureString which we get via a control instance
                for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--)
                {
                    Font TestFont;

                    if (privateFont)
                    {
                        TestFont = new Font(private_fonts.Families[0], AdjustedSize);
                    }
                    else
                    {
                        TestFont = new Font(fontResourceName, AdjustedSize);
                    }

                    // Test the string with the new size
                    SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont);

                    if (clipRectangle.Width - 4 > Convert.ToInt32(AdjustedSizeNew.Width) && clipRectangle.Height - 4 > Convert.ToInt32(AdjustedSizeNew.Height))
                    {
                        // Good font, return it
                        return(TestFont);
                    }
                    else
                    {
                        TestFont.Dispose();
                    }
                }

                // If you get here there was no fontsize that worked
                // return MinimumSize or Original?
                if (SmallestOnFail)
                {
                    return(new Font(OriginalFont.Name, MinFontSize, OriginalFont.Style));
                }
                else
                {
                    return(OriginalFont);
                }
            } finally {
                if (OriginalFont != null)
                {
                    OriginalFont.Dispose();
                }
            }
        }
Exemple #2
0
        public void TestGlyphBoundsWithoutM()
        {
            string hello    = "Hello";
            var    font     = new TestFont(10);
            var    provider = TestGlyphBoundsProvider.Instance;
            var    glyphRun = new AttributedGlyphRun <TestFont, TGlyph>(hello, hello, font);
            var    width    = provider.GetTypographicWidth(font, glyphRun);

            Approximately.Equal(width, 25, 0.01);
        }
Exemple #3
0
        public void TestGlyphBoundsWithM()
        {
            string america  = "America";
            var    font     = new TestFont(10);
            var    provider = TestGlyphBoundsProvider.Instance;
            var    glyphRun = new AttributedGlyphRun <TestFont, TGlyph>(america, america, font);
            var    width    = provider.GetTypographicWidth(font, glyphRun);

            Approximately.Equal(width, 40, 0.01);
        }
Exemple #4
0
        public void TestGlyphBoundsWithoutM()
        {
            string hello    = "Hello";
            var    font     = new TestFont(10);
            var    provider = TestGlyphBoundsProvider.Instance;
            var    glyphRun = new AttributedGlyphRun <TestFont, TGlyph>(hello, hello.EnumerateRunes(), font);

            Assert.All(glyphRun.GlyphInfos, glyphInfo => Assert.Null(glyphInfo.Foreground));
            var width = provider.GetTypographicWidth(font, glyphRun);

            Approximately.Equal(width, 25, 0.01);
        }