Example #1
0
        /// <summary>
        /// Get cached font if it exists in cache or null if it is not.
        /// </summary>
        PixelFarm.Drawing.FontInfo TryGetFont(string family, float size, System.Drawing.FontStyle style)
        {
            PixelFarm.Drawing.FontInfo fontInfo = null;
            FontKey fontKey = new FontKey(family.ToLower(), size, style);

            _fontInfoCacheByFontKey.TryGetValue(fontKey, out fontInfo);
            return(fontInfo);
        }
Example #2
0
        PixelFarm.Drawing.FontInfo RegisterFont(System.Drawing.Font newFont, FontKey fontKey)
        {
            //from ...
            //1. http://msdn.microsoft.com/en-us/library/xwf9s90b%28v=vs.100%29.aspx
            //3.  Font metrics from http://msdn.microsoft.com/en-us/library/xwf9s90b(VS.71).aspx

            //2. http://stackoverflow.com/questions/1006069/how-do-i-get-the-position-of-the-text-baseline-in-a-label-and-a-numericupdown
            //cellHeight = font's ascent + descent
            //linespacing = cellHeight + external leading
            //em height (size)= cellHeight - internal leading

            //-------------------
            //evaluate this font, collect font matrix in pixel mode
            PixelFarm.Drawing.FontInfo fontInfo;
            if (!_fontInfoCache.TryGetValue(newFont, out fontInfo))
            {
                System.Drawing.FontFamily ff = newFont.FontFamily;

                int lineSpacing = ff.GetLineSpacing(newFont.Style);
                //font height is expensive call ****
                int fontHeight = newFont.Height;

                //convert descent

                float fontSize     = newFont.Size;
                int   fontEmHeight = newFont.FontFamily.GetEmHeight(newFont.Style);
                int   fontAscent   = newFont.FontFamily.GetCellAscent(newFont.Style);
                float descent      = newFont.FontFamily.GetCellDescent(newFont.Style);

                var myFont = new PixelFarm.Drawing.WinGdi.MyFont(newFont);
                fontInfo = new PixelFarm.Drawing.WinGdi.MyFontInfo(
                    myFont,
                    fontHeight,
                    (fontAscent * fontSize / fontEmHeight),
                    (descent * fontSize / fontEmHeight),
                    fontHeight * fontAscent / lineSpacing,
                    gdiFontHelper);
                myFont.SetFontInfo(fontInfo);
                //lineSpacing * newFont.FontFamily.GetCellAscent(newFont.Style) / linespace);


                //1. info
                _fontInfoCache.Add(newFont, fontInfo);
                _fontInfoCacheByFontKey.Add(fontKey, fontInfo);

                _fontsUnmanagedCache[newFont] = newFont.ToHfont();
                //2. line cache
                _fontHeightCache[newFont] = fontHeight;

                return(fontInfo);
            }
            return(fontInfo);
        }
Example #3
0
        public static WinGdiFont GetWinGdiFont(RequestFont f)
        {
            if (f == null)
            {
                throw new NotSupportedException();
            }
            if (f == latestFont)
            {
                return(latestWinFont);
            }
            WinGdiFont actualFontInside = WinGdiFont.GetCacheFontAsWinGdiFont(f);

            if (actualFontInside != null)
            {
                return(actualFontInside);
            }
            //-----
            //need to create a new one
            //get register font or create the new one
            FontKey    key = f.FontKey;
            WinGdiFont found;

            if (!registerFonts.TryGetValue(key, out found))
            {
                //create the new one and register
                //create fontface
                FontFaceKey    fontfaceKey = new FontFaceKey(key);
                WinGdiFontFace fontface;
                if (!winGdiFonFaces.TryGetValue(fontfaceKey, out fontface))
                {
                    //create new
                    fontface = new WinGdiFontFace(f);
                    winGdiFonFaces.Add(fontfaceKey, fontface);
                }

                found = (WinGdiFont)fontface.GetFontAtPointSize(f.SizeInPoints);
                registerFonts.Add(key, found);//cache here
            }
            latestFont = f;
            found.AssignToRequestFont(f);
            return(latestWinFont = found);
        }
Example #4
0
 public FontFaceKey(FontKey fontKey)
 {
     this.FontNameIndex = fontKey.FontNameIndex;
     this.FontStyle     = fontKey.FontStyle;
 }