Exemple #1
0
        internal override Size GetPreferredSizeCore(Size proposedConstraints)
        {
            Size textExtent;
            Size bordersAndPadding = this.GetBordersAndPadding();

            proposedConstraints -= bordersAndPadding;
            proposedConstraints  = LayoutUtils.UnionSizes(proposedConstraints, Size.Empty);
            if (string.IsNullOrEmpty(this.Text))
            {
                using (WindowsFont font = WindowsFont.FromFont(this.Font))
                {
                    textExtent       = WindowsGraphicsCacheManager.MeasurementGraphics.GetTextExtent("0", font);
                    textExtent.Width = 0;
                    goto Label_0113;
                }
            }
            if (this.UseGDIMeasuring())
            {
                TextFormatFlags flags = (this.FlatStyle == System.Windows.Forms.FlatStyle.System) ? TextFormatFlags.Default : this.CreateTextFormatFlags(proposedConstraints);
                textExtent = this.MeasureTextCache.GetTextSize(this.Text, this.Font, proposedConstraints, flags);
            }
            else
            {
                using (Graphics graphics = WindowsFormsUtils.CreateMeasurementGraphics())
                {
                    using (StringFormat format = this.CreateStringFormat())
                    {
                        SizeF layoutArea = (proposedConstraints.Width == 1) ? new SizeF(0f, (float)proposedConstraints.Height) : new SizeF((float)proposedConstraints.Width, (float)proposedConstraints.Height);
                        textExtent = Size.Ceiling(graphics.MeasureString(this.Text, this.Font, layoutArea, format));
                    }
                }
            }
Label_0113:
            return(textExtent + bordersAndPadding);
        }
Exemple #2
0
        public void Font_GetCharSet(string family, float size, byte charset)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            Assert.Equal(charset, windowsFont.CharSet);
        }
Exemple #3
0
        public void Font_GetOverhangPadding(string family, float size, float expected)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;

            Assert.Equal(expected, graphics.GetOverhangPadding(windowsFont));
        }
Exemple #4
0
        public void Font_MeasureText(string family, float size, Size proposedSize, uint dt, Size expected)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;
            Size            measure  = graphics.MeasureText("Windows Foundation Classes", windowsFont, proposedSize, (User32.DT)dt);

            Assert.Equal(expected, measure);
        }
Exemple #5
0
        public void Font_GetTextMargins(string family, float size, int left, int right)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;

            User32.DRAWTEXTPARAMS margins = graphics.GetTextMargins(windowsFont);
            Assert.Equal(left, margins.iLeftMargin);
            Assert.Equal(right, margins.iRightMargin);
        }
Exemple #6
0
        public void Font_GetTextExtent(string family, float size, int width, int height)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;
            Size            extent   = graphics.GetTextExtent("Whizzo Butter", windowsFont);

            Assert.Equal(width, extent.Width);
            Assert.Equal(height, extent.Height);
        }
Exemple #7
0
        public void Font_AdjustForVerticalAlignment(string family, float size, Rectangle bounds, uint dt, Rectangle expected)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;

            graphics.DeviceContext.SelectFont(windowsFont);
            User32.DRAWTEXTPARAMS param  = default;
            Rectangle             result = WindowsGraphics.AdjustForVerticalAlignment(
                graphics,
                "Windows Foundation Classes",
                bounds,
                (User32.DT)dt,
                ref param);

            Assert.Equal(expected, result);
        }
        public static WindowsFont GetWindowsFont(Font font, WindowsFontQuality fontQuality)
        {
            if (font == null)
            {
                return(null);
            }

            // First check if font is in the cache.

            int count = 0;
            int index = currentIndex;

            // Search by index of most recently added object.
            while (count < WindowsFontCache.Count)
            {
                if (WindowsFontCache[index].Key.Equals(font))  // don't do shallow comparison, we could miss cloned fonts.
                {
                    // We got a Font in the cache, let's see if we have a WindowsFont with the same quality as required by the caller.

                    // WARNING: It is not expected that the WindowsFont is disposed externally since it is created by this class.
                    Debug.Assert(WindowsFontCache[index].Value.Hfont != IntPtr.Zero, "Cached WindowsFont was disposed, enable GDI_FINALIZATION_WATCH to track who did it!");

                    WindowsFont wf = WindowsFontCache[index].Value;
                    if (wf.Quality == fontQuality)
                    {
                        return(wf);
                    }
                }

                index--;
                count++;

                if (index < 0)
                {
                    index = CacheSize - 1;
                }
            }

            // Font is not in the cache, let's add it.

            WindowsFont winFont = WindowsFont.FromFont(font, fontQuality);
            KeyValuePair <Font, WindowsFont> newEntry = new KeyValuePair <Font, WindowsFont>(font, winFont);

            currentIndex++;

            if (currentIndex == CacheSize)
            {
                currentIndex = 0;
            }

            if (WindowsFontCache.Count == CacheSize)  // No more room, update current index.
            {
                WindowsFont wfont = null;

                // Go through the existing fonts in the cache, and see if any
                // are not in use by a DC.  If one isn't, replace that.  If
                // all are in use, new up a new font and do not cache it.

                bool finished   = false;
                int  startIndex = currentIndex;
                int  loopIndex  = startIndex + 1;
                while (!finished)
                {
                    if (loopIndex >= CacheSize)
                    {
                        loopIndex = 0;
                    }

                    if (loopIndex == startIndex)
                    {
                        finished = true;
                    }

                    wfont = WindowsFontCache[loopIndex].Value;
                    if (!DeviceContexts.IsFontInUse(wfont))
                    {
                        currentIndex = loopIndex;
                        finished     = true;
                        break;
                    }
                    else
                    {
                        loopIndex++;
                        wfont = null;
                    }
                }

                if (wfont != null)
                {
                    WindowsFontCache[currentIndex] = newEntry;
                    winFont.OwnedByCacheManager    = true;


#if GDI_FONT_CACHE_TRACK
                    Debug.WriteLine("Removing from cache: " + wfont);
                    Debug.WriteLine("Adding to cache: " + winFont);
#endif


                    wfont.OwnedByCacheManager = false;
                    wfont.Dispose();
                }
                else
                {
                    // do not cache font - caller is ALWAYS responsible for
                    // disposing now.  If it is owned  by the CM, it will not
                    // disposed.

                    winFont.OwnedByCacheManager = false;


#if GDI_FONT_CACHE_TRACK
                    Debug.WriteLine("Creating uncached font: " + winFont);
#endif
                }
            }
            else
            {
                winFont.OwnedByCacheManager = true;
                WindowsFontCache.Add(newEntry);


#if GDI_FONT_CACHE_TRACK
                Debug.WriteLine("Adding to cache: " + winFont);
#endif
            }
            return(winFont);
        }