Esempio n. 1
0
 int ComputeSpacing(FontSpacing lhs, FontSpacing rhs)
 {
     if (lhs == null)
     {
         return(0);
     }
     return(Math.Min(lhs.BottomRight + rhs.BottomLeft, Math.Min(lhs.CenterRight + rhs.CenterLeft, lhs.TopRight + rhs.TopLeft)));
 }
Esempio n. 2
0
        int MeasureWidth(string text, double size)
        {
            double      factor      = size / fontsize;
            double      width       = -fontspacing * factor;
            FontSpacing lastspacing = null;

            foreach (char character in text)
            {
                if (glyphs.ContainsKey(character))
                {
                    Glyph glyph = glyphs[character];
                    width      -= ComputeSpacing(lastspacing, glyph.Spacing);
                    width      += fontspacing * factor;
                    width      += glyph.Width * factor;
                    lastspacing = glyph.Spacing;
                }
            }

            return((int)Math.Max(0, width));
        }
Esempio n. 3
0
        /// <summary>
        /// draws a text to an image
        /// </summary>
        /// <param name="text">text to draw</param>
        /// <param name="size">font size</param>
        /// <param name="target">image to draw to</param>
        /// <param name="blend">blend color</param>
        /// <param name="x">target x</param>
        /// <param name="y">target y</param>
        public void DrawText(string text, float size, Image target, Color blend, float x = 0.0f, float y = 0.0f)
        {
            if (uppercase)
            {
                text = text.ToUpper();
            }

            ColorMatrix colormatrix = new ColorMatrix(new[] {
                new[] { blend.R / 255.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                new[] { 0.0f, blend.G / 255.0f, 0.0f, 0.0f, 0.0f },
                new[] { 0.0f, 0.0f, blend.B / 255.0f, 0.0f, 0.0f },
                new[] { 0.0f, 0.0f, 0.0f, blend.A / 255.0f, 0.0f },
                new[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
            });

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            float       factor      = size / fontsize;
            FontSpacing lastspacing = null;

            using (Graphics g = Graphics.FromImage(target)) {
                foreach (char character in text)
                {
                    if (glyphs.ContainsKey(character))
                    {
                        Glyph glyph = glyphs[character];
                        x -= ComputeSpacing(lastspacing, glyph.Spacing);
                        g.DrawImage(atlas,
                                    new Rectangle((int)x, (int)(y + size - glyph.Height * factor), (int)(glyph.Width * factor), (int)(glyph.Height * factor)),
                                    glyph.X, glyph.Y, glyph.Width, (float)glyph.Height,
                                    GraphicsUnit.Pixel, attributes);
                        x          += (glyph.Width + fontspacing) * factor;
                        lastspacing = glyph.Spacing;
                    }
                }
            }
        }