예제 #1
0
        /// <summary>
        ///     Draws a text
        /// </summary>
        /// <param name="text">The text to be drawn</param>
        /// <param name="font">The texts font</param>
        /// <param name="x">The texts x-position</param>
        /// <param name="y">The texts y-position</param>
        /// <param name="z">The texts z-position</param>
        /// <param name="color">The text color</param>
        public static void DrawText(string text, CFont font, float x, float y, float z, SColorF color, bool allMonitors = true)
        {
            if (font.Height <= 0f || text == "")
            {
                return;
            }

            CFontStyle fontStyle = _GetFontStyle(font);

            float dx = x;

            foreach (char chr in text)
            {
                fontStyle.DrawGlyph(chr, font.Height, dx, y, z, color, allMonitors);
                dx += fontStyle.GetWidth(chr, font.Height);
            }
        }
예제 #2
0
        public static void DrawTextReflection(string text, CFont font, float x, float y, float z, SColorF color, float rspace, float rheight)
        {
            if (font.Height <= 0f || text == "")
            {
                return;
            }

            CFontStyle fontStyle = _GetFontStyle(font);

            float dx = x;

            foreach (char chr in text)
            {
                fontStyle.DrawGlyphReflection(chr, font.Height, dx, y, z, color, rspace, rheight);
                dx += fontStyle.GetWidth(chr, font.Height);
            }
        }
예제 #3
0
        public static void DrawText(string text, CFont font, float x, float y, float z, SColorF color, float begin, float end)
        {
            if (font.Height <= 0f || text == "")
            {
                return;
            }

            float w = GetTextWidth(text, font);

            if (w <= 0f)
            {
                return;
            }

            float xStart = x + w * begin;
            float xEnd   = x + w * end;
            float xCur   = x;

            CFontStyle fontStyle = _GetFontStyle(font);

            foreach (char chr in text)
            {
                float w2 = fontStyle.GetWidth(chr, font.Height);
                float b  = (xStart - xCur) / w2;

                if (b < 1f)
                {
                    if (b < 0f)
                    {
                        b = 0f;
                    }
                    float e = (xEnd - xCur) / w2;
                    if (e > 0f)
                    {
                        if (e > 1f)
                        {
                            e = 1f;
                        }
                        fontStyle.DrawGlyph(chr, font.Height, xCur, y, z, color, b, e);
                    }
                }
                xCur += w2;
            }
        }
예제 #4
0
        public static float GetTextHeight(string text, CFont font)
        {
            CFontStyle fontStyle = _GetFontStyle(font);

            return(text == "" ? 0 : text.Select(chr => fontStyle.GetHeight(chr, font.Height)).Max());
        }
예제 #5
0
        public static float GetTextWidth(string text, CFont font)
        {
            CFontStyle fontStyle = _GetFontStyle(font);

            return(text.Sum(chr => fontStyle.GetWidth(chr, font.Height)));
        }
예제 #6
0
        public CGlyph(char chr, CFontStyle fontStyle, float maxHeight)
        {
            MaxHeight = maxHeight;
            float  outlineSize = fontStyle.Outline * maxHeight;
            string chrString   = chr.ToString();

            Font  fo = fontStyle.GetSystemFont(maxHeight);
            SizeF fullSize;
            Size  bmpSize;

            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                fullSize = g.MeasureString(chrString, fo);
                if (chr != ' ')
                {
                    //Gets exact height and width for drawing more than 1 char. But width is to small to draw char on bitmap as e.g. italic chars will get cropped
                    //See https://stackoverflow.com/questions/11708621/how-to-measure-width-of-a-string-precisely
                    StringFormat     format = StringFormat.GenericTypographic;
                    RectangleF       rect   = new RectangleF(0, 0, 1000, 1000);
                    CharacterRange[] ranges = { new CharacterRange(0, chrString.Length) };
                    format.SetMeasurableCharacterRanges(ranges);
                    _BoundingBox = g.MeasureCharacterRanges(chrString, fo, rect, format)[0].GetBounds(g).Size;

                    // ReSharper disable CompareOfFloatsByEqualityOperator
                    if (_BoundingBox.Height == 0)
                    {
                        // ReSharper restore CompareOfFloatsByEqualityOperator
                        _BoundingBox.Height = fullSize.Height;
                    }
                    _BoundingBox.Width  += outlineSize / 2;
                    _BoundingBox.Height += outlineSize;
                    fullSize.Width      += outlineSize;
                    bmpSize              = new Size((int)fullSize.Width, (int)Math.Round(_BoundingBox.Height));
                }
                else
                {
                    _BoundingBox         = fullSize;
                    _BoundingBox.Height += outlineSize;
                    bmpSize              = new Size(1, 1);
                }
            }
            using (var bmp = new Bitmap(bmpSize.Width, bmpSize.Height, PixelFormat.Format32bppArgb))
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.Transparent);

                    if (chr == ' ')
                    {
                        _Texture      = CDraw.AddTexture(bmp);
                        _DrawBounding = new RectangleF(0, 0, 0, 0);
                    }
                    else
                    {
                        g.SmoothingMode     = SmoothingMode.AntiAlias;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                        var point = new PointF(outlineSize / 2, outlineSize / 4);

                        using (var path = new GraphicsPath())
                        {
                            //Have to use size in em not pixels!
                            float emSize = fo.Size * fo.FontFamily.GetCellAscent(fo.Style) / fo.FontFamily.GetEmHeight(fo.Style);
                            path.AddString(chrString, fo.FontFamily, (int)fo.Style, emSize, point, new StringFormat());

                            using (var pen = new Pen(fontStyle.OutlineColor.AsColor(), outlineSize))
                            {
                                pen.LineJoin = LineJoin.Round;
                                g.DrawPath(pen, path);
                                g.FillPath(Brushes.White, path);
                            }
                        }
                        _DrawBounding = _GetRealBounds(bmp);
                        using (Bitmap bmpCropped = bmp.Clone(_DrawBounding, PixelFormat.Format32bppArgb))
                        {
                            float dx = (fullSize.Width - _BoundingBox.Width - 1) / 2;
                            _DrawBounding.X -= dx;
                            _Texture         = CDraw.AddTexture(bmpCropped);

                            /*_DrawBounding.X *= _Texture.Width / _DrawBounding.Width;
                             * _DrawBounding.Y *= _Texture.Width / _DrawBounding.Width;
                             * _DrawBounding.Width = _Texture.Width;
                             * _DrawBounding.Height = _Texture.Height;*/
#if FONT_DEBUG_OUTPUT
                            if (Char.IsLetterOrDigit(chr))
                            {
                                if (outline > 0)
                                {
                                    bmpCropped.Save("font_" + chr + "o" + CFonts.Style + "2.png", ImageFormat.Png);
                                }
                                else
                                {
                                    bmpCropped.Save("font_" + chr + CFonts.Style + "2.png", ImageFormat.Png);
                                }
                            }
#endif
                        }
                    }
                }
        }