预定义行上的文本

时间:2012-11-01 14:44:46

标签: c# algorithm graphics gdi+

我有一行(PointF[]),一些stringGraphics对象。现在我想在我的线上绘制这个字符串。

以下是一个例子:

enter image description here

有没有算法以最简单的方式做到这一点?

[编辑] 好的,我已经尝试了@ endofzero的代码并对其进行了一些修改。这是整个解决方案(使用角度和距离计算):

private static void DrawBrokenString(Graphics g, IList<PointF> line, string text, Font font)
{
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.TextRenderingHint = TextRenderingHint.AntiAlias;
    Pen pen = new Pen(Brushes.Black);

    for (int index = 0; index < line.Count - 1; index++)
    {
        float distance = GetDistance(line[index], line[index + 1]);

        if (text.Length > 0)
        {
            for (int cIndex = text.Length; cIndex >= 0; cIndex--)
            {
                SizeF textSize = g.MeasureString(text.Substring(0, cIndex).Trim(), font);
                if (textSize.Width <= distance)
                {
                    float rotation = GetAngle(line[index], line[index + 1]);

                    g.TranslateTransform(line[index].X, line[index].Y);
                    g.RotateTransform(rotation);

                    if (index != line.Count - 2)
                    {
                        g.DrawString(text.Substring(0, cIndex).Trim(), font, new SolidBrush(Color.Black),
                                 new PointF(0, -textSize.Height));
                    }
                    else
                    {
                        g.DrawString(text.Trim(), font, new SolidBrush(Color.Black),
                                 new PointF(0, -textSize.Height));
                    }

                    g.RotateTransform(-rotation);
                    g.TranslateTransform(-line[index].X, -line[index].Y);

                    text = text.Remove(0, cIndex);
                    break;
                }
            }    
        }
        else
        {
            break;
        }

        g.DrawLine(pen, line[index].X, line[index].Y, line[index + 1].X, line[index + 1].Y);
    }
}

private static float GetDistance(PointF p1, PointF p2)
{
    return (float) Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
}

private static float GetAngle(PointF p1, PointF p2)
{
    float c = (float) Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));

    if (c == 0)
        return 0;

    if (p1.X > p2.X)
        return (float) (Math.Asin((p1.Y - p2.Y)/c)*180/Math.PI - 180);

    return (float) (Math.Asin((p2.Y - p1.Y)/c)*180/Math.PI);
}

现在我只需要一件事来完成我的问题。我不希望字符串相互重叠。有任何想法吗?啊,当我们不能在路径上画一个字符串时(由于断点太多,它应该画在线上方(中间顶部)。

以下是不需要的重叠示例:

enter image description here

1 个答案:

答案 0 :(得分:1)

以下是如何按虚线绘制文本的简单示例。

    private static void DrawBrokenLine(PointF[] line, string text, Graphics g, Font font)
    {
        for (int index = 0; index < line.Length - 1; index++)
        {
            float distance = distanceBetween(line[index], line[index + 1]);

            if (text.Length > 0)
            {
                for (int cIndex = text.Length; cIndex >= 0; cIndex--)
                {
                    SizeF textSize = g.MeasureString(text.Substring(0, cIndex).Trim(), font);
                    if (textSize.Width <= distance)
                    {
                        float rotation = angleBetween(line[index], line[index + 1]);

                        g.TranslateTransform(line[index].X, line[index].Y);
                        g.RotateTransform(rotation);

                        g.DrawString(text.Substring(0, cIndex).Trim(), font, new SolidBrush(Color.Black),
                            new PointF(0, -textSize.Height));

                        g.RotateTransform(-rotation);
                        g.TranslateTransform(-line[index].X, -line[index].Y);

                        text = text.Remove(0, cIndex);
                        break;
                    }
                }
            }
            else
                break;
        }
    }

如果您希望它只在单词之间的空格处断开,则必须更改它在测量时缩短字符串的方式。

修改

为了修复角上的文字重叠,我为它添加了一个计算:

    private static void DrawBrokenLine(PointF[] line, string text, Graphics g, Font font)
    {
        float lastOverlap = 0;

        for (int index = 0; index < line.Length - 1; index++)
        {
            float distance = distanceBetween(line[index], line[index + 1]);

            float angleOfLines = 180;
            if (index < line.Length - 2)
            {
                angleOfLines = angleBetweenLines(line[index], line[index + 1], line[index + 2]);
            }

            if (text.Length > 0)
            {
                SizeF textSize = g.MeasureString(text, font);
                float overlap = 0;
                if (angleOfLines < 180)
                {
                    if (angleOfLines <= 90)
                    {
                        overlap = (textSize.Height / 1.5f) / (
                            Convert.ToSingle(Math.Tan((angleOfLines / 2) * Math.PI / 180))
                            );
                    }
                    else
                    {
                        overlap = Convert.ToSingle(
                            Math.Sin(angleOfLines * Math.PI / 180) * (textSize.Height / 1.5f));
                    }
                }

                for (int cIndex = text.Length; cIndex >= 0; cIndex--)
                {
                    textSize = g.MeasureString(text.Substring(0, cIndex).Trim(), font);

                    if (textSize.Width <= distance - overlap - lastOverlap)   //notice the subtraction of overlap
                    {
                        float rotation = angleBetween(line[index], line[index + 1]);

                        g.TranslateTransform(line[index].X, line[index].Y);
                        g.RotateTransform(rotation);

                        g.DrawString(text.Substring(0, cIndex).Trim(), font, new SolidBrush(Color.Black),
                            new PointF(lastOverlap, -textSize.Height));

                        g.RotateTransform(-rotation);
                        g.TranslateTransform(-line[index].X, -line[index].Y);

                        text = text.Remove(0, cIndex);
                        break;
                    }
                }

                lastOverlap = overlap;
            }
            else
                break;
        }
    }

    private static float angleBetweenLines(PointF A, PointF B, PointF C)
    {
        float angle1 = 360 - angleBetween(A, B);
        float angle2 = 360 - angleBetween(B, C);

        if (angle1 < 0)
            angle1 += 360;
        if (angle2 < 0)
            angle2 += 360;

        float delta = 180 + angle1 - angle2;

        if (delta < 0)
            delta += 360;
        if (delta > 360)
            delta -= 360;

        return delta;
    }

enter image description here

我的例子有一个警告:如果你注意到,我将文字高度除以特殊数字。我在MeasureString没有为文本返回准确的高度时遇到问题,因此我尝试手动纠正此问题。

关于在线上方绘制一个字符串(因为中断太多),这取决于你想要检测那种情况的方式(构成太多,文本放在哪里,它应该是一个角度?)。也许如果您能提供这种情况的示例图像,请记住这有助于澄清。