PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

How to determine distance between characters with any font?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2768
Page 1 of 1

Author:  Delizald [ Tue Mar 18, 2014 5:01 pm ]
Post subject:  How to determine distance between characters with any font?

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?

Author:  Thomas Hoevel [ Tue Mar 18, 2014 5:05 pm ]
Post subject:  Re: How to determine distance between characters with any fo

Hi!

The trick used by the XTextFormatter class can be seen in the source code of this class and in this post:
viewtopic.php?p=5801#p5801

Author:  Delizald [ Tue Mar 18, 2014 5:46 pm ]
Post subject:  Re: How to determine distance between characters with any fo

Thank you so much! It works perfectly now :D

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/