Menu

#2 Collapsing of spaces

open
nobody
None
5
2005-11-29
2005-11-29
Arjuna
No

I am having this problem of Collapsing of spaces. If
I try to put more than one space in between two
words, it collapses to one. I would appreciate it if
you could me show a way to fix this problem

Discussion

  • Gustavo Salatino

    Logged In: YES
    user_id=1194940
    Originator: NO

    I know that this post comes a little late :-p, but is that I discovered sharpPDF recently (an excellent component).

    So, I've to fix this problem too. Here is the code:

    1. In Fonts/pdfPredefinedFont.cs source file, to avoid NullReferenceException when a char haven't metrics (i.e.: tab char):

    /// <summary>
    /// Method that returns the width of a string
    /// </summary>
    /// <param name="strWord">Text</param>
    /// <param name="fontSize">Font's size</param>
    /// <returns></returns>
    public override int getWordWidth(string strWord, int fontSize)
    {
    double returnWeight = 0;
    foreach(char myChar in strWord.ToCharArray()) {
    if (((Convert.ToInt32(myChar) >= 0) && (Convert.ToInt32(myChar) <= 255)) || (GlyphConverter.pdfCodeFromUnicode(Convert.ToInt32(myChar)) != "")) {
    if (_fontDefinition.fontMetrics[(int)myChar] != null) // HACK: GS. Para evitar la excepción NullReferenceException cuando encuentra un caracter que no tiene métrica definida (por ejemplo: el caracter tab con código 9).
    returnWeight += (int)_fontDefinition.fontMetrics[(int)myChar];
    }
    }
    return Convert.ToInt32(Math.Round(returnWeight * fontSize /1000));
    }

    2. In textAdapter.cs I rewrite the public method formatParagraph to solve the collapsing of spaces:

    /// <summary>
    /// Static method thats format a paragraph
    /// </summary>
    /// <param name="strText">Input Text</param>
    /// <param name="fontSize">Font's size</param>
    /// <param name="fontType">Font's type</param>
    /// <param name="parWidth">Paragrapfh's width</param>
    /// <param name="lineHeight">Line's height</param>
    /// <param name="parAlign">Paragraph's Alignment</param>
    /// <param name="maxLines">Number of maximum lines of the paragraph</param>
    /// <returns>IEnumerable interface that cointains paragraphLine objects</returns>
    public static paragraphLineList formatParagraph(
    ref string strText, int fontSize, pdfAbstractFont fontType,
    int parWidth, int maxLines, int lineHeight, predefinedAlignment parAlign)
    {
    string[] lines = strText.Replace("\r", "").Split(new char[1] {(char)10}); // Separa el texto en líneas a evaluar.
    int spaceWidth = fontType.getWordWidth(" ", fontSize);
    bool maxLinesExcedeed = false;

    paragraphLineList resultParagraph = new paragraphLineList();
    string line = string.Empty;
    int lineWidth = 0;

    for (int lineIndex = 0; lineIndex < lines.Length && !maxLinesExcedeed; lineIndex++)
    {
    string[] words = lines[lineIndex].Split(' '); // Separa la línea en palabras a evaluar.

    for (int wordIndex = 0; wordIndex < words.Length && !maxLinesExcedeed; wordIndex++)
    {
    // Quita los caracteres no reconocidos de la palabra evaluada.
    string word = fontType.cleanText(words[wordIndex]).Trim();

    // Calcula el tamaño de la palabra evaluada.
    int wordWidth = fontType.getWordWidth(word, fontSize);

    // Comprueba si línea actual más la palabra evaluada supera el tamaño máximo del párrafo.
    if (lineWidth + spaceWidth + wordWidth > parWidth)
    {
    if (lineWidth == 0)
    // Agrega una línea al párrafo, recortando la palabra evaluada para que quepa según el tamaño máximo.
    resultParagraph.Add(
    textAdapter.createNewLine(
    fontType.cropWord(word, parWidth, fontSize), parAlign, parWidth, parWidth, lineHeight, fontType));
    else
    {
    // Agrega una línea al párrafo sin incluir la palabra evaluada, porque supera ancho máximo.
    resultParagraph.Add(textAdapter.createNewLine(line, parAlign, parWidth, lineWidth, lineHeight, fontType));
    line = word;
    lineWidth = fontType.getWordWidth(line, fontSize);
    } // if

    // Comprueba si se ha excedido el máximo de líneas del párrafo.
    maxLinesExcedeed = (maxLines > 0 && resultParagraph.Count >= maxLines);
    }
    else
    {
    // Suma la palabra evaluada a la línea actual.
    if (lineWidth > 0)
    line += " ";

    line += word;
    lineWidth += spaceWidth + wordWidth;
    } // if
    } // foreach
    } // foreach

    // Y agrega la única línea que no excede el tamaño máximo del párrafo.
    if (!maxLinesExcedeed && lineWidth > 0)
    resultParagraph.Add(textAdapter.createNewLine(line, parAlign, parWidth, lineWidth, lineHeight, fontType));

    return resultParagraph;
    }

    GS

     
  • Gustavo Salatino

    Logged In: YES
    user_id=1194940
    Originator: NO

    A little change in public method formatParagraph:

    // Suma la palabra evaluada a la línea actual.
    if (lineWidth > 0)
    {
    line += " ";
    lineWidth += spaceWidth;
    } // if

     
  • Gustavo Salatino

    Logged In: YES
    user_id=1194940
    Originator: NO

    A little change in public method formatParagraph:

    // Comprueba si se ha excedido el máximo de líneas del párrafo.
    maxLinesExcedeed = (maxLines > 0 && resultParagraph.Count >= maxLines);
    }
    else
    {
    // Suma la palabra evaluada a la línea actual.
    if (lineWidth > 0)
    {
    line += " ";
    lineWidth += spaceWidth;
    } // if

    line += word;
    lineWidth += wordWidth;
    } // if

     
  • Gustavo Salatino

    Logged In: YES
    user_id=1194940
    Originator: NO

    A little change in public method formatParagraph:

    // Comprueba si se ha excedido el máximo de líneas del párrafo.
    maxLinesExcedeed = (maxLines > 0 && resultParagraph.Count >= maxLines);
    }
    else
    {
    // Suma la palabra evaluada a la línea actual.
    if (lineWidth > 0)
    {
    line += " ";
    lineWidth += spaceWidth;
    } // if

    line += word;
    lineWidth += wordWidth;
    } // if

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.