Hello all! I'm working on a multiline function to determine the xPosition where a character is going to be drawn and then return the last Y position where the last character was drawn. This is because I'm working with a lots of dynamic strings with variable lengths.
Code:
public double fnEscribirMultilinea(string sLinea, int nCaracteres, double nSeparacion, double posX, double posY, XFont xFuente, ref XGraphics gfx)
{
// StringBuilder sb1 = new StringBuilder();
// StringBuilder sb2 = new StringBuilder();
sLinea = sLinea.Replace("\n", "");
sLinea = sLinea.Replace("\r", "");
double separacionLetra = xFuente.Metrics.Ascent;
int nCaracteresCadena = sLinea.Length;
double posXTemp = posX;
double xColumna = posX;
double yRenglon = posY;
if (nCaracteresCadena > nCaracteres)
{
for (int i = 0; i < nCaracteresCadena; i++)
{
char caracter = sLinea[i];
if (((i == nCaracteres) || i % nCaracteres == 0) & i != 0)
{
yRenglon += nSeparacion;
gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, posX, yRenglon, XStringFormats.TopLeft);
xColumna = posX + separacionLetra;
}
else if (i == 0)
{
gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
xColumna += separacionLetra;
}
else if (i < nCaracteres)
{
gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
xColumna += separacionLetra;
}
else
{
gfx.DrawString(caracter.ToString(), xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft);
xColumna += separacionLetra;
}
}
}
else { gfx.DrawString(sLinea, xFuente, XBrushes.Black, xColumna, yRenglon, XStringFormats.TopLeft); }
return yRenglon;
}
So far, the Y position separation works properly, but the X separation between characters needs to be adjusted according to the font that is being used.
Is there a way to determine the separation between characters according to the font without having to manually adjust this value?