Quantcast

JLatexMath and iText

classic Classic list List threaded Threaded
5 messages Options
Mark Taylor Mark Taylor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JLatexMath and iText

Hi JLatexMath people,

I would like to use JLatexMath to generate PDF output using the
iText library (http://itextpdf.com/).  I can get a Graphics2D from
iText and paint the TeXIcon onto it, and export the result as a PDF.
This apparently works, but the resulting PDF looks rubbish - the
fonts are not embedded in the document, so some default font is
used which means that symbols don't look anything like they are
supposed to.  I've attached the program which does this.

I have poked around in the source code a bit, and I *think* what's
required to get the fonts embedded is to implement a suitable
com.itextpdf.awt.FontMapper which can supply a PDF font
(com.itextpdf.text.pdf.BaseFont object) corresponding to a given
LaTeX font.  Since the JLatexMath jar file contains ttf files,
I think that should be doable, though I don't know quite how to
do it, in particular what BaseFont "encoding" these ttf files use.
But, maybe I'm barking up the wrong tree - perhaps there's some
easier way to do it, like generating PDF fonts automatically from the
AWT fonts, or perhaps it's simply impossible.

I realise I could render the TeXIcon to an image and paint that to
the PDF, but I want vector output so that's not a good option.
I also realise I could use FOP to generate PDF output, but it
seems rather a heavyweight solution.

Has anybody tried to get iText working with JLatexMath?  Can anyone
give me help/advice in getting them to talk to each other?

Thanks a lot,

Mark

PS JLatexMath is a very nice item - thank you to the developers.
   If I can get it generating vector output to PDFs I'll be a
   very happy man, and so will my users (astronomers).

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
[hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/

PdfLatex.java (1K) Download Attachment
Calixte DENIZET Calixte DENIZET
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JLatexMath and iText

Hi Mark,

Two possibilities:

i) you can pass true as fourth argument of PdfGraphics2D to draw glyphs as shapes
ii) you can register the ttf fonts in FontFactory (all the fonts are available on the JLM forge in a zip file http://forge.scilab.org/index.php/p/jlatexmath/downloads/345/ ) and you can override the method awtToPdf in DefaultFontMapper with something like:
public BaseFont awtToPdf(Font f) {
            if (f.getFontName().equals("jlm_cmex10")) {
                     return FontFactory.getFont("jlm_cmex10").getBaseFont();
            }
            return super.awtToPdf(f);
}

You should write code for all other fonts...
You could use  a Map<String, com.itextpdf.text.pdf.BaseFont> as a cache.

Thanks to you to be a jlm user.

Regards

Calixte

On 03/05/2012 16:23, Mark Taylor wrote:
Hi JLatexMath people,

I would like to use JLatexMath to generate PDF output using the
iText library (http://itextpdf.com/).  I can get a Graphics2D from
iText and paint the TeXIcon onto it, and export the result as a PDF.
This apparently works, but the resulting PDF looks rubbish - the
fonts are not embedded in the document, so some default font is
used which means that symbols don't look anything like they are
supposed to.  I've attached the program which does this.

I have poked around in the source code a bit, and I *think* what's
required to get the fonts embedded is to implement a suitable 
com.itextpdf.awt.FontMapper which can supply a PDF font 
(com.itextpdf.text.pdf.BaseFont object) corresponding to a given
LaTeX font.  Since the JLatexMath jar file contains ttf files,
I think that should be doable, though I don't know quite how to
do it, in particular what BaseFont "encoding" these ttf files use.
But, maybe I'm barking up the wrong tree - perhaps there's some
easier way to do it, like generating PDF fonts automatically from the
AWT fonts, or perhaps it's simply impossible.

I realise I could render the TeXIcon to an image and paint that to
the PDF, but I want vector output so that's not a good option.
I also realise I could use FOP to generate PDF output, but it
seems rather a heavyweight solution.

Has anybody tried to get iText working with JLatexMath?  Can anyone
give me help/advice in getting them to talk to each other?

Thanks a lot,

Mark

PS JLatexMath is a very nice item - thank you to the developers.
   If I can get it generating vector output to PDFs I'll be a
   very happy man, and so will my users (astronomers).

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
[hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/

Mark Taylor Mark Taylor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JLatexMath and iText

Dear Calixte,

fabulous!  Many thanks for the quick response.  If I'd guessed what
"onlyShapes" meant I could have saved myself a lot of effort...

A couple of supplementary questions:

1. The fonts already seem to be in the jlatexmath jar file;
   is there any reason I need to get the jlatexmath-fonts.zip file
   separately?

2. I presume there is a difference of file size/efficiency between
   the two approaches, but since I'm only likely to be using small
   amounts of latex text this is probably not an issue.
   But is there an issue of quality too?  Using the onlyShapes
   PdfGraphics2D the results look pretty good, but they're not
   perfect - at huge magnifications some of the characters look
   at bit wrong.  Will this go away if I take the approach of
   registering the fonts instead?

Thanks again,

Mark

On Thu, 3 May 2012, Calixte DENIZET wrote:

> Hi Mark,
>
> Two possibilities:
>
> i) you can pass true as fourth argument of PdfGraphics2D to draw glyphs as
> shapes
> ii) you can register the ttf fonts in FontFactory (all the fonts are available
> on the JLM forge in a zip file
> http://forge.scilab.org/index.php/p/jlatexmath/downloads/345/ ) and you can
> override the method awtToPdf in DefaultFontMapper with something like:
> public BaseFont awtToPdf(Font f) {
>             if (f.getFontName().equals("jlm_cmex10")) {
>                      return FontFactory.getFont("jlm_cmex10").getBaseFont();
>             }
>             return super.awtToPdf(f);
> }
>
> You should write code for all other fonts...
> You could use  a Map<String, com.itextpdf.text.pdf.BaseFont> as a cache.
>
> Thanks to you to be a jlm user.
>
> Regards
>
> Calixte
>
> On 03/05/2012 16:23, Mark Taylor wrote:
> > Hi JLatexMath people,
> >
> > I would like to use JLatexMath to generate PDF output using the
> > iText library (http://itextpdf.com/).  I can get a Graphics2D from
> > iText and paint the TeXIcon onto it, and export the result as a PDF.
> > This apparently works, but the resulting PDF looks rubbish - the
> > fonts are not embedded in the document, so some default font is
> > used which means that symbols don't look anything like they are
> > supposed to.  I've attached the program which does this.
> >
> > I have poked around in the source code a bit, and I *think* what's
> > required to get the fonts embedded is to implement a suitable
> > com.itextpdf.awt.FontMapper which can supply a PDF font
> > (com.itextpdf.text.pdf.BaseFont object) corresponding to a given
> > LaTeX font.  Since the JLatexMath jar file contains ttf files,
> > I think that should be doable, though I don't know quite how to
> > do it, in particular what BaseFont "encoding" these ttf files use.
> > But, maybe I'm barking up the wrong tree - perhaps there's some
> > easier way to do it, like generating PDF fonts automatically from the
> > AWT fonts, or perhaps it's simply impossible.
> >
> > I realise I could render the TeXIcon to an image and paint that to
> > the PDF, but I want vector output so that's not a good option.
> > I also realise I could use FOP to generate PDF output, but it
> > seems rather a heavyweight solution.
> >
> > Has anybody tried to get iText working with JLatexMath?  Can anyone
> > give me help/advice in getting them to talk to each other?
> >
> > Thanks a lot,
> >
> > Mark
> >
> > PS JLatexMath is a very nice item - thank you to the developers.
> >     If I can get it generating vector output to PDFs I'll be a
> >     very happy man, and so will my users (astronomers).
> >
> > --
> > Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
> > [hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/
>
>

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
[hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/
Calixte DENIZET Calixte DENIZET
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JLatexMath and iText

On 03/05/2012 18:36, Mark Taylor wrote:

> Dear Calixte,
>
> fabulous!  Many thanks for the quick response.  If I'd guessed what
> "onlyShapes" meant I could have saved myself a lot of effort...
>
> A couple of supplementary questions:
>
> 1. The fonts already seem to be in the jlatexmath jar file;
>     is there any reason I need to get the jlatexmath-fonts.zip file
>     separately?

It seems that is possible to give a jar URI to FontFactory.register():

FontFactory.register(TeXFormula.class.getResource("fonts/base/jlm_cmex10.ttf").toURI().toString());

So no need to use the fonts in the zip file.
> 2. I presume there is a difference of file size/efficiency between
>     the two approaches, but since I'm only likely to be using small
>     amounts of latex text this is probably not an issue.
>     But is there an issue of quality too?  Using the onlyShapes
>     PdfGraphics2D the results look pretty good, but they're not
>     perfect - at huge magnifications some of the characters look
>     at bit wrong.  Will this go away if I take the approach of
>     registering the fonts instead?

I already observed the glyph as shape rendering problem (in SVG and PDF
(with FOP))... but imho it is a Java problem (in fact the shape is got
with getGlyphOutline in GlyphVector class)

Calixte

>
> Thanks again,
>
> Mark
>
> On Thu, 3 May 2012, Calixte DENIZET wrote:
>
>> Hi Mark,
>>
>> Two possibilities:
>>
>> i) you can pass true as fourth argument of PdfGraphics2D to draw glyphs as
>> shapes
>> ii) you can register the ttf fonts in FontFactory (all the fonts are available
>> on the JLM forge in a zip file
>> http://forge.scilab.org/index.php/p/jlatexmath/downloads/345/ ) and you can
>> override the method awtToPdf in DefaultFontMapper with something like:
>> public BaseFont awtToPdf(Font f) {
>>              if (f.getFontName().equals("jlm_cmex10")) {
>>                       return FontFactory.getFont("jlm_cmex10").getBaseFont();
>>              }
>>              return super.awtToPdf(f);
>> }
>>
>> You should write code for all other fonts...
>> You could use  a Map<String, com.itextpdf.text.pdf.BaseFont>  as a cache.
>>
>> Thanks to you to be a jlm user.
>>
>> Regards
>>
>> Calixte
>>
>> On 03/05/2012 16:23, Mark Taylor wrote:
>>> Hi JLatexMath people,
>>>
>>> I would like to use JLatexMath to generate PDF output using the
>>> iText library (http://itextpdf.com/).  I can get a Graphics2D from
>>> iText and paint the TeXIcon onto it, and export the result as a PDF.
>>> This apparently works, but the resulting PDF looks rubbish - the
>>> fonts are not embedded in the document, so some default font is
>>> used which means that symbols don't look anything like they are
>>> supposed to.  I've attached the program which does this.
>>>
>>> I have poked around in the source code a bit, and I *think* what's
>>> required to get the fonts embedded is to implement a suitable
>>> com.itextpdf.awt.FontMapper which can supply a PDF font
>>> (com.itextpdf.text.pdf.BaseFont object) corresponding to a given
>>> LaTeX font.  Since the JLatexMath jar file contains ttf files,
>>> I think that should be doable, though I don't know quite how to
>>> do it, in particular what BaseFont "encoding" these ttf files use.
>>> But, maybe I'm barking up the wrong tree - perhaps there's some
>>> easier way to do it, like generating PDF fonts automatically from the
>>> AWT fonts, or perhaps it's simply impossible.
>>>
>>> I realise I could render the TeXIcon to an image and paint that to
>>> the PDF, but I want vector output so that's not a good option.
>>> I also realise I could use FOP to generate PDF output, but it
>>> seems rather a heavyweight solution.
>>>
>>> Has anybody tried to get iText working with JLatexMath?  Can anyone
>>> give me help/advice in getting them to talk to each other?
>>>
>>> Thanks a lot,
>>>
>>> Mark
>>>
>>> PS JLatexMath is a very nice item - thank you to the developers.
>>>      If I can get it generating vector output to PDFs I'll be a
>>>      very happy man, and so will my users (astronomers).
>>>
>>> --
>>> Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
>>> [hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/
>>
> --
> Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
> [hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/

Mark Taylor Mark Taylor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JLatexMath and iText

Following Calixte's advice, I've written a FontMapper implementation
which is suitable for getting iText to work with JLaTeXMath.
It doesn't do anything cleverer than what Calixte already said,
but it contains some utility methods for extracting lists of font
files from zip files (e.g. jlatexmath.jar) which should make it
painless to incoporate into existing code.

I attach it to this mail in case anyone would like to look at it or
use it.  If someone wants to add it to the JLaTeXMath distribution
that's fine by me.

Mark

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
[hidden email] +44-117-928-8776 http://www.star.bris.ac.uk/~mbt/

On Thu, 3 May 2012, Calixte DENIZET wrote:

> On 03/05/2012 18:36, Mark Taylor wrote:
> > Dear Calixte,
> >
> > fabulous!  Many thanks for the quick response.  If I'd guessed what
> > "onlyShapes" meant I could have saved myself a lot of effort...
> >
> > A couple of supplementary questions:
> >
> > 1. The fonts already seem to be in the jlatexmath jar file;
> >     is there any reason I need to get the jlatexmath-fonts.zip file
> >     separately?
>
> It seems that is possible to give a jar URI to FontFactory.register():
>
> FontFactory.register(TeXFormula.class.getResource("fonts/base/jlm_cmex10.ttf").toURI().toString());
>
> So no need to use the fonts in the zip file.
> > 2. I presume there is a difference of file size/efficiency between
> >     the two approaches, but since I'm only likely to be using small
> >     amounts of latex text this is probably not an issue.
> >     But is there an issue of quality too?  Using the onlyShapes
> >     PdfGraphics2D the results look pretty good, but they're not
> >     perfect - at huge magnifications some of the characters look
> >     at bit wrong.  Will this go away if I take the approach of
> >     registering the fonts instead?
>
> I already observed the glyph as shape rendering problem (in SVG and PDF (with
> FOP))... but imho it is a Java problem (in fact the shape is got with
> getGlyphOutline in GlyphVector class)
>
> Calixte

ExternalFontMapper.java (11K) Download Attachment
Loading...