You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Eric Douglas <ed...@blockhouse.com> on 2011/09/02 19:40:27 UTC
How to translate characters?
I pass a character into my XML/FO as □ and it shows on the PDF as
a square.
I'm trying to figure out what Java has for interpreting such code, and
it appears FOP just string searches and pulls out the number, in the
class org.apache.xmlgraphics.fonts.Glyphs.
Is this all it is, to search text for "&#" and ";" and find the
character value between, or is there an actual Java class/method for
translating such values?
RE: How to translate characters?
Posted by Eric Douglas <ed...@blockhouse.com>.
Aha!
Your code was almost right.
Now, I can't put a square into the XML or I get this error.
SystemId Unknown; Line #188; Column #5; An invalid XML character
(Unicode: 0x0) was found in the element content of the document.
So I stick with putting the string value "□" into my text object
I'm using to create various outputs.
For a PDF I get the text value, write it to XML, run it through the
transform to get FO, run it through FOP to get a PDF, then load it into
pdfbox to print.
The XML file contains that string. The FO file contains the square
character. I'm guessing the Transformer itself knows nothing about this
but something in the SAX handler converts it.
For a print preview just to display a single unicode value the simplest
way is to hardcode it.
Your String declaration was on the right track except you need a value
container.
drawText = displayText.getText();
String CHARCHECKBOX = new String("□");
String CHARSMILEY = new String("☺");
String UNICODECHARCHECKBOX = new String(new int[]{0x25a1}, 0, 1 );
String UNICODECHARSMILEY = new String(new int[]{0x263A}, 0, 1 );
drawText = drawText.replaceAll(CHARCHECKBOX, UNICODECHARCHECKBOX);
drawText = drawText.replaceAll(CHARSMILEY, UNICODECHARSMILEY);
I don't know an easy way to find and replace all possible such unicode
strings aside from creating a custom SAX handler and wrapping the text
in some default XML to recreate what the Transformer is doing.
-----Original Message-----
From: Christopher R. Maden [mailto:crism@maden.org]
Sent: Monday, September 05, 2011 9:32 PM
To: fop-dev@xmlgraphics.apache.org
Subject: Re: How to translate characters?
On 09/05/2011 06:31 PM, Eric Douglas wrote:
> So, I'm confused. I put that exact string into an fo:inline.
> I'm running embedded code and I separated the XML-FO transform step
from the FOP step.
> I'm just running a straight Oracle transform.
> My input is an XML file containing the string □ My transformer
> is created using an XSL file.
> My output is an FO file with that string converted to a square.
> I put that string into a Java String and get the value. It comes out
the same as it went in.
> How do I get the value of the String as a square?
Java is not XML. Java has no native facility for interpreting an XML
numeric character reference, so you are getting exactly the expected
results.
I don't speak Java well, but try something like this:
String square = new String( [ 0x25a1 ], 0, 1 );
Then pass that string, square, to FOP.
If that doesn't help, I'm done; I can read Java, but don't really write
it well.
~Chris
--
Chris Maden, text nerd <URL: http://crism.maden.org/ > "The present
tendency and drift towards the Police State gives all free Americans
pause." - Alabama Supreme Court, 1955 (Pike v. Southern Bell Tel. &
Telegraph, 81 So.2d 254)
Re: How to translate characters?
Posted by "Christopher R. Maden" <cr...@maden.org>.
On 09/05/2011 06:31 PM, Eric Douglas wrote:
> So, I'm confused. I put that exact string into an fo:inline.
> I'm running embedded code and I separated the XML-FO transform step from the FOP step.
> I'm just running a straight Oracle transform.
> My input is an XML file containing the string □
> My transformer is created using an XSL file.
> My output is an FO file with that string converted to a square.
> I put that string into a Java String and get the value. It comes out the same as it went in.
> How do I get the value of the String as a square?
Java is not XML. Java has no native facility for interpreting an XML
numeric character reference, so you are getting exactly the expected
results.
I don’t speak Java well, but try something like this:
String square = new String( [ 0x25a1 ], 0, 1 );
Then pass that string, square, to FOP.
If that doesn’t help, I’m done; I can read Java, but don’t really write
it well.
~Chris
--
Chris Maden, text nerd <URL: http://crism.maden.org/ >
“The present tendency and drift towards the Police State gives all
free Americans pause.” — Alabama Supreme Court, 1955
(Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)
RE: How to translate characters?
Posted by Eric Douglas <ed...@blockhouse.com>.
So, I'm confused. I put that exact string into an fo:inline.
I'm running embedded code and I separated the XML-FO transform step from the FOP step.
I'm just running a straight Oracle transform.
My input is an XML file containing the string □
My transformer is created using an XSL file.
My output is an FO file with that string converted to a square.
I put that string into a Java String and get the value. It comes out the same as it went in.
How do I get the value of the String as a square?
-----Original Message-----
From: Christopher R. Maden [mailto:crism@maden.org]
Sent: Fri 9/2/2011 4:09 PM
To: fop-dev@xmlgraphics.apache.org
Subject: Re: How to translate characters?
I overlooked a couple things here... so by way of explanation:
On 09/02/2011 02:44 PM, Eric Douglas wrote:
> I see. As I said I saw something in the FOP classes which appeared
> to be looking for such a code and translating it, but I just pulled
> up a .FO file and I see it's already translated to be
> <fo:inline>?</fo:inline> so there must be something in the Oracle
> Transformer.
There is nothing in the Oracle Transformer doing this. When you pass
XML+XSLT or raw FO XML to FOP, the first thing that happens is the XML
is parsed. XML is a markup language (obviously): the information
represented by:
<fo:inline>?</fo:inline>
<fo:inline>□</fo:inline>
<fo:inline>□</fo:inline>
<fo:inline>?</fo:inline>
is not only identical in effect, but literally indistinguishable after
parsing. Operating on the parsed document, you cannot tell these things
apart. They each represent a single XML element (named inline in the FO
namespace) with a single character of content, U+25A1 WHITE SQUARE.
The Oracle Transformer is operating on that data: an element with
content that is one character long. No lookup or mapping is done.
> So it seems FOP just gets the actual square and still has to know
> what character it maps to in the font.
FOP gets the square which *is* a character. It knows which character it
maps to because it *is* that character. What FOP doesn't know (at
first) is which fonts have useful glyphs for that character. It looks
through the fonts currently in scope (as specified in the list in the
font-family property) to find the first one that does specify a useful
glyph for that character.[*]
~Chris
[*] Strictly, as I understand it, it looks for the font that best
matches an entire string, as character-by-character font selection isn't
implemented. But for a single-character string, it's the same thing.
--
Chris Maden, text nerd <URL: http://crism.maden.org/ >
"The present tendency and drift towards the Police State gives all
free Americans pause." - Alabama Supreme Court, 1955
(Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)
Re: How to translate characters?
Posted by "Christopher R. Maden" <cr...@maden.org>.
I overlooked a couple things here... so by way of explanation:
On 09/02/2011 02:44 PM, Eric Douglas wrote:
> I see. As I said I saw something in the FOP classes which appeared
> to be looking for such a code and translating it, but I just pulled
> up a .FO file and I see it's already translated to be
> <fo:inline>□</fo:inline> so there must be something in the Oracle
> Transformer.
There is nothing in the Oracle Transformer doing this. When you pass
XML+XSLT or raw FO XML to FOP, the first thing that happens is the XML
is parsed. XML is a markup language (obviously): the information
represented by:
<fo:inline>□</fo:inline>
<fo:inline>□</fo:inline>
<fo:inline>□</fo:inline>
<fo:inline>□</fo:inline>
is not only identical in effect, but literally indistinguishable after
parsing. Operating on the parsed document, you cannot tell these things
apart. They each represent a single XML element (named inline in the FO
namespace) with a single character of content, U+25A1 WHITE SQUARE.
The Oracle Transformer is operating on that data: an element with
content that is one character long. No lookup or mapping is done.
> So it seems FOP just gets the actual square and still has to know
> what character it maps to in the font.
FOP gets the square which *is* a character. It knows which character it
maps to because it *is* that character. What FOP doesn’t know (at
first) is which fonts have useful glyphs for that character. It looks
through the fonts currently in scope (as specified in the list in the
font-family property) to find the first one that does specify a useful
glyph for that character.[*]
~Chris
[*] Strictly, as I understand it, it looks for the font that best
matches an entire string, as character-by-character font selection isn’t
implemented. But for a single-character string, it’s the same thing.
--
Chris Maden, text nerd <URL: http://crism.maden.org/ >
“The present tendency and drift towards the Police State gives all
free Americans pause.” — Alabama Supreme Court, 1955
(Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)
Re: How to translate characters?
Posted by "Christopher R. Maden" <cr...@maden.org>.
On 09/02/2011 02:44 PM, Eric Douglas wrote:
> I see. As I said I saw something in the FOP classes which appeared
> to be looking for such a code and translating it, but I just pulled
> up a .FO file and I see it's already translated to be
> <fo:inline>□</fo:inline> so there must be something in the Oracle
> Transformer.
> So it seems FOP just gets the actual square and still has to know
> what character it maps to in the font.
>
> So I'm trying to pass in this text ("□") and get it to display
> with Java's Graphics2D.drawText().
> So I'm wondering if I translate that the same way I do to create this
> FO, with an XSL file and a Transformer or if there's a simpler
> method.
> So I'm looking at the class java.nio.charset.Charset trying to figure
> out if or how that connects to a custom font file.
Passing that string, you are passing a series of characters: ampersand,
number sign, lower-case Latin letter x, Arabic numeral two, etc. You
need to pass the text “□” — a single character, white square.
If all you have is the character code (U+25A1), you can create a new
String using the code; one of the String constructors takes an array of
integer code points as an argument.
~Chris
--
Chris Maden, text nerd <URL: http://crism.maden.org/ >
“The present tendency and drift towards the Police State gives all
free Americans pause.” — Alabama Supreme Court, 1955
(Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)
RE: How to translate characters?
Posted by Eric Douglas <ed...@blockhouse.com>.
I see. As I said I saw something in the FOP classes which appeared to be looking for such a code and translating it, but I just pulled up a .FO file and I see it's already translated to be <fo:inline>□</fo:inline> so there must be something in the Oracle Transformer.
So it seems FOP just gets the actual square and still has to know what character it maps to in the font.
So I'm trying to pass in this text ("□") and get it to display with Java's Graphics2D.drawText().
So I'm wondering if I translate that the same way I do to create this FO, with an XSL file and a Transformer or if there's a simpler method.
So I'm looking at the class java.nio.charset.Charset trying to figure out if or how that connects to a custom font file.
-----Original Message-----
From: Christopher R. Maden [mailto:crism@maden.org]
Sent: Friday, September 02, 2011 1:56 PM
To: fop-dev@xmlgraphics.apache.org
Subject: Re: How to translate characters?
On 09/02/2011 01:40 PM, Eric Douglas wrote:
> I pass a character into my XML/FO as □ and it shows on the PDF
> as a square.
> I'm trying to figure out what Java has for interpreting such code, and
> it appears FOP just string searches and pulls out the number, in the
> class org.apache.xmlgraphics.fonts.Glyphs.
> Is this all it is, to search text for "&#" and ";" and find the
> character value between, or is there an actual Java class/method for
> translating such values?
By the time FOP gets this information, the XML has been parsed.
□ is just a convenient way of entering the single character with Unicode value 25A1, □, WHITE SQUARE. Equivalents would be □ or (with common ISO entity declarations) □ - in all cases, FOP just receives a single character, □.
FOP then attempts to find which of the specified fonts actually has a glyph for that character, and does so using the numeric Unicode value of the character, but that is independent of how the input XML (which FOP does not see) specified the character. If you’re looking at altering or overriding the code, you need to operate in the character domain, not the XML markup domain.
(By analogy, FOP receives elements, root-in-the-FO-namespace etc., not the string “<fo:root ...”. This is the same kind of thing.)
~Chris
--
Chris Maden, text nerd <URL: http://crism.maden.org/ > “The present tendency and drift towards the Police State gives all free Americans pause.” - Alabama Supreme Court, 1955 (Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)
Re: How to translate characters?
Posted by "Christopher R. Maden" <cr...@maden.org>.
On 09/02/2011 01:40 PM, Eric Douglas wrote:
> I pass a character into my XML/FO as □ and it shows on the PDF
> as a square.
> I'm trying to figure out what Java has for interpreting such code,
> and it appears FOP just string searches and pulls out the number, in
> the class org.apache.xmlgraphics.fonts.Glyphs.
> Is this all it is, to search text for "&#" and ";" and find the
> character value between, or is there an actual Java class/method for
> translating such values?
By the time FOP gets this information, the XML has been parsed.
□ is just a convenient way of entering the single character with
Unicode value 25A1, □, WHITE SQUARE. Equivalents would be □ or
(with common ISO entity declarations) □ — in all cases, FOP just
receives a single character, □.
FOP then attempts to find which of the specified fonts actually has a
glyph for that character, and does so using the numeric Unicode value of
the character, but that is independent of how the input XML (which FOP
does not see) specified the character. If you’re looking at altering or
overriding the code, you need to operate in the character domain, not
the XML markup domain.
(By analogy, FOP receives elements, root-in-the-FO-namespace etc., not
the string “<fo:root ...”. This is the same kind of thing.)
~Chris
--
Chris Maden, text nerd <URL: http://crism.maden.org/ >
“The present tendency and drift towards the Police State gives all
free Americans pause.” — Alabama Supreme Court, 1955
(Pike v. Southern Bell Tel. & Telegraph, 81 So.2d 254)