Translate characters for a target

The implementation allows to set translators for characters. Those tanslators will be called before the paragraph text if formatted. Translators can be for characters only, for HTML entities, or for complete targets

Target LaTeX

The first example uses a translator for the target LaTeX. This means that all characters in the text that require special translation for being used in a LaTeX document will be translated into that representation (depending on the capabilities of the translator of course).

The code below creates a paragraph with some text that is not suitable to be used in LaTeX documents. It then prints the default output (no translation), followed by setting the LaTeX target translator, printing the output again.

1
2
3
4
5
6
7
8
9
10
11
12
13
AP_Context ctx = new AP_Context();
ctx.setAlignment(TextAlignment.LEFT);

AsciiParagraph ap = new AsciiParagraph(ctx);
ap.addText("A sentence with some normal text, not specific to LaTeX.");
ap.addText("Now for some characters that require conversion: # % &.");
ap.addText("And some more: © § ¤.");
ap.addText("And even more: È É Ê Ë.");
ap.addText("And some arrows as well: ← ↑ → ↓ ↔");
System.out.println(ap.render(35));

ctx.setTargetTranslator(new Text2Latex());
System.out.println(ap.render(35));

The resulting output for text without translation to LaTeX:

A sentence with some normal text,
not specific to LaTeX. Now for some
characters that require conversion:
# % &. And some more: © § ¤. And
even more: È É Ê Ë. And some arrows
as well: ← ↑ → ↓ ↔

The resulting output for text with translation to LaTeX:

A sentence with some normal text,
not specific to LaTeX. Now for some
characters that require conversion:
\# \% \&. And some more:
{\copyright} {\S} \currency. And
even more: \`{E} \'{E} \^{E} \"{E}.
And some arrows as well:
\(\leftarrow{}\) \(\uparrow\)
\(\rightarrow{}\) \(\downarrow{}\)
\(\leftrightarrow{}\)

Target HTML

The second example uses a translator for the target HTML. This means that all characters in the text that require special translation for being used in a HTML document will be translated into that representation (depending on the capabilities of the translator of course).

The code below creates a paragraph with some text that is not suitable to be used in LaTeX documents. It then prints the default output (no translation), followed by setting the HTML target translator, printing the output again.

1
2
3
4
5
6
7
8
9
10
11
12
13
AP_Context ctx = new AP_Context();
ctx.setAlignment(TextAlignment.LEFT);

AsciiParagraph ap = new AsciiParagraph(ctx);
ap.addText("A sentence with some normal text, not specific to HTML.");
ap.addText("Now for some characters that require conversion: # % & < >.");
ap.addText("And some more: © § ¤.");
ap.addText("And even more: Ē ē Ĕ ĕ Ė ė Ę ę Ě ě.");
ap.addText("And some arrows as well: ← ↑ → ↓ ↔ ↕");
System.out.println(ap.render(36));

ctx.setTargetTranslator(new Text2Html());
System.out.println(ap.render(36));

The resulting output for text without translation to HTML:

A sentence with some normal text,
not specific to HTML. Now for some
characters that require conversion:
# % & < >. And some more: © § ¤. And
even more: Ē ē Ĕ ĕ Ė ė Ę ę Ě ě. And
some arrows as well: ← ↑ → ↓ ↔ ↕

The resulting output for text with translation to HTML:

A sentence with some normal text,
not specific to HTML. Now for some
characters that require conversion:
&#803; &#37; &amp; &lt; &gt;. And
some more: &copy; &sect; &curren;.
And even more: &#274; &#275; &#276;
&#277; &#278; &#279; &#280; &#281;
&#282; &#283;. And some arrows as
well: &larr; &uarr; &rarr; &darr;
&harr; &#8597;