コード例 #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, Color fontColor, Color?bgColor, PortableFontDesc typeface, string text)
        {
            var font = GetFont(typeface);

            return(bmp.DrawString(x0, y0, new IntRect(new IntPoint(0, 0), new IntSize(bmp.PixelWidth, bmp.PixelHeight)), fontColor, bgColor, font, text));
        }
コード例 #3
0
        public static int DrawString(this WriteableBitmap bmp, int x0, int y0, IntRect cliprect, Color fontColor, PortableFontDesc typeface, string text)
        {
            var font = GetFont(typeface);

            return(bmp.DrawString(x0, y0, cliprect, fontColor, font, text));
        }
コード例 #4
0
        public static int DrawStringVertical(this WriteableBitmap bmp, int x0, int y0, Color fontColor, PortableFontDesc typeface, string text, Boolean topToBottom)
        {
            IntRect cliprect = new IntRect(new IntPoint(0, 0), new IntSize(bmp.PixelWidth, bmp.PixelHeight));

            var font = GetFont(typeface);

            if (text == null)
            {
                return(0);
            }
            int dx     = 0;
            int dy     = 0;
            int textwi = 0;

            using (var context = bmp.GetBitmapContext())
            {
                int dyDirection = 1;
                if (topToBottom == false)
                {
                    dyDirection = -1;
                }

                foreach (char ch in text)
                {
                    if (ch == '\n')
                    {
                        if (dy > textwi)
                        {
                            textwi = dy;
                        }
                        dy  = 0;
                        dx += font.TextHeight * dyDirection;
                    }

                    if (y0 + dy <= cliprect.Bottom)
                    {
                        var letter = font.GetGrayScaleLetter(ch);
                        if (letter == null)
                        {
                            continue;
                        }
                        context.DrawLetterVertical(x0 + dx, y0 + dy, cliprect, fontColor, letter, topToBottom);
                        dy += letter.Width * dyDirection;
                    }
                }
            }

            if (dx > textwi)
            {
                textwi = dx;
            }
            return(textwi);
        }