示例#1
0
        public static GlyphFont GetFont(PortableFontDesc typeface)
        {
            lock (FontCache)
            {
                if (FontCache.ContainsKey(typeface))
                {
                    return(FontCache[typeface]);
                }
            }

            var fontFlags = System.Drawing.FontStyle.Regular;

            if (typeface.IsItalic)
            {
                fontFlags |= System.Drawing.FontStyle.Italic;
            }

            if (typeface.IsBold)
            {
                fontFlags |= System.Drawing.FontStyle.Bold;
            }

            var font = new GlyphFont
            {
                Typeface = new Typeface(new FontFamily(typeface.FontName),
                                        typeface.IsItalic ? FontStyles.Italic : FontStyles.Normal,
                                        typeface.IsBold ? FontWeights.Bold : FontWeights.Normal,
                                        FontStretches.Normal),
                EmSize      = typeface.EmSize,
                Font        = new Font(typeface.FontName, typeface.EmSize * 76.0f / 92.0f, fontFlags),
                IsClearType = typeface.IsClearType,
            };

            font.Typeface.TryGetGlyphTypeface(out font.GlyphTypeface);
            lock (FontCache)
            {
                FontCache[typeface] = font;
            }

            return(font);
        }
示例#2
0
        public static int DrawString(this WriteableBitmap bmp, int x0, int y0, IntRect cliprect, Color fontColor, Color?bgColor, GlyphFont font, string text)
        {
            if (text == null)
            {
                return(0);
            }
            int dx = 0, dy = 0;
            var textwi = 0;

            using (var context = bmp.GetBitmapContext())
            {
                foreach (var ch in text)
                {
                    if (ch == '\n')
                    {
                        if (dx > textwi)
                        {
                            textwi = dx;
                        }
                        dx  = 0;
                        dy += font.TextHeight;
                    }
                    if (x0 + dx <= cliprect.Right)
                    {
                        if (font.IsClearType)
                        {
                            if (!bgColor.HasValue)
                            {
                                throw new Exception("Clear type fonts must have background specified");
                            }
                            var letter = font.GetClearTypeLetter(ch, fontColor, bgColor.Value);
                            if (letter == null)
                            {
                                continue;
                            }
                            context.DrawLetter(x0 + dx, y0 + dy, cliprect, letter);
                            dx += letter.Width;
                        }
                        else
                        {
                            var letter = font.GetGrayScaleLetter(ch);
                            if (letter == null)
                            {
                                continue;
                            }
                            context.DrawLetter(x0 + dx, y0 + dy, cliprect, fontColor, letter);
                            dx += letter.Width;
                        }
                    }
                }
            }

            if (dx > textwi)
            {
                textwi = dx;
            }
            return(textwi);
        }
示例#3
0
 public static int DrawString(this WriteableBitmap bmp, int x0, int y0, IntRect cliprect, Color fontColor, GlyphFont font, string text)
 {
     return(DrawString(bmp, x0, y0, cliprect, fontColor, null, font, text));
 }
 public static int DrawString(this WriteableBitmap bmp, int x0, int y0, IntRect cliprect, Color fontColor, Color?bgColor, GlyphFont font, string text, bool vertical = false)
 {
     if (vertical)
     {
         return(DrawStringVertical(bmp, x0, y0, cliprect, fontColor, null, font, text));
     }
     else
     {
         return(DrawString(bmp, x0, y0, cliprect, fontColor, null, font, text));
     }
 }