Esempio n. 1
0
 public void DrawText(DrawTextInfo dtInfo, Color color)
 {
     for (int i = 0; i < dtInfo.location.Count; i++)
     {
         DrawText(dtInfo.location[i], dtInfo.text[i], color);
     }
 }
Esempio n. 2
0
        public DrawTextInfo CalculateDrawTextInfo(Rectangle rectangle, string text)
        {
            DrawTextInfo dtInfo = new DrawTextInfo();

            Point position  = new Point(0, 0);
            int   altoLetra = tamaniosLetras[(int)' '].Height;

            if (position.Y + altoLetra <= rectangle.Height)
            {
                int ancho = 0;

                text = text.Replace("\n", " \n ");
                String[] palabras = text.Split(' ');
                int      nPalabra = 0;

                StringBuilder sb       = new StringBuilder(text.Length);
                string        texto    = "";
                bool          imprimir = false;

                while (nPalabra < palabras.Length)
                {
                    String palabra = palabras[nPalabra];

                    int anchoPalabra = 0;

                    if (palabra != "\n")
                    {
                        foreach (Char c in palabra)
                        {
                            if (c >= 256)
                            {
                                anchoPalabra += tamaniosLetras[' '].Width;
                            }
                            else
                            {
                                anchoPalabra += tamaniosLetras[c].Width;
                            }
                        }

                        if (anchoPalabra + ancho > rectangle.Width)
                        {
                            if (ancho == 0)
                            {
                                texto = palabra;
                                nPalabra++;
                            }
                            else
                            {
                                texto = sb.ToString();
                            }

                            imprimir = true;
                        }
                        else
                        {
                            sb.Append(palabra);
                            sb.Append(' ');
                            ancho += anchoPalabra + tamaniosLetras[' '].Width;
                            nPalabra++;
                        }
                    }
                    else
                    {
                        imprimir = true;
                        texto    = sb.ToString();
                        nPalabra++;
                    }

                    if (!imprimir && nPalabra == palabras.Length)
                    {
                        texto    = sb.ToString();
                        imprimir = true;
                    }

                    if (imprimir)
                    {
                        dtInfo.location.Add(new Point(position.X + rectangle.X, position.Y + rectangle.Y));
                        dtInfo.text.Add(texto);

                        position.Y += altoLetra;

                        if (position.Y + altoLetra > rectangle.Height)
                        {
                            break;
                        }

                        ancho     = 0;
                        sb.Length = 0;
                        imprimir  = false;
                    }
                }
            }

            return(dtInfo);
        }