Вопрос

I use PPLA language to print labels directly to an Argox OS-214.

How can I word wrap long texts?

Do I have to make the calculations or is there any function that does that for me?

If I have to calculate, do you hav any tips on how to do it?

Это было полезно?

Решение

In the PPLA language manual there are no functions to word wrap long text lines, so you have to calculate yourself where to insert the line breaks before sending the commands to the printer.
How to do it depends by the programming language that you are using; for example in the PHP language there is a wordwrap function that can do it for you (the C source code of the wordwrap function is here, at line 803).
A simple description of a word wrapping algorithm can be found on the wikipedia:

SpaceLeft := LineWidth
for each Word in Text
    if (Width(Word) + SpaceWidth) > SpaceLeft
        insert line break before Word in Text
        SpaceLeft := LineWidth - Width(Word)
    else
        SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)

First of all, you have to split the text in words, but I suppose that this is not a problem for you, so I won't elaborate further.
LineWidth should be calculated as the label width minus the left and right margins width.
Assuming that for your text you're using a single fixed-width (non proportional) font, you can convert the LineWidth to a number of characters by simply dividing it by the character width (or by simply printing a long line of text and then counting the number of characters that gets printed); this way each word length corresponds to the number of its characters.
Otherwise, if you're using a proportional font, calculating each word width is more difficult, unless you know each character width.
An idea could be using an image with the same pixel size of the label, just to simulate the printing and for calculating where to insert the line breaks, but this requires to have a function to calculate the pixel width of each word (for example in PHP there is the imagettfbbox function), and also to use exactly the same font on the printer and the PC.
Be aware that you could also have to handle some words that are longer than the label width; in this case you must simply split the word at LineWidth and continue on the next line.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top