You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Karl Roberts <Ka...@macquarie.com> on 2006/02/21 03:41:01 UTC

Re how to embed a font programatically

Hi,
Did you ever manage to do this?

I need to do something similar because my embedded FOP is running in a
Webapp and the font metric file and the TTF file are hidden inside the
WEB-INF directory and so the metric-url and embed-url in my fop-config
is no good (even if they were accessible via a URL I don't know the
hostname or context path until runtime)

What would be really nice is someway to pass the fontMetric and TTF file
to the Renderer as InputSource (or InputStream). This way I can get the
font files from the hidden WEB-INF dir using

InputStream fontmetric =
serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/metric.x
ml");
InputStream embed =
serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/font.TTF
"); 
Renderer myRenderer = new PDFRenderer();
FontInfo myFontInfo = new FontInfo();

// method needed ...(ahem)
myFontInfo.addFont("FontName", "FontStyle", "FontWeight", fontmetric,
embed);
 
myRenderer.setupFontInfo(myFontInfo);
userAgent.setRendererOverride(myRenderer);

Am I on the right track? Has anyone done this (or better) and mind
sharing the code?

Cheers

Karl

On 2006-01-26 16:22:42 Jeremias Maerki wrote:
> Looking at the source code, you'd need to do the following:
> - Subclass the Renderer implementation (ex. PDFRenderer) so you gain
> access to the protected fontList member variable.
> - Fill the fontList variable much like
> FontSetup.buildFontListFromConfiguration() does. This code will need
to
> run before you start the rendering run or at least before
> PrintRenderer.setupFontInfo() is called.
> - Instantiate your subclassed Renderer yourself and set it on the
> FOUserAgent using setRendererOverride().
>
> Have fun!

> On 26.01.2006 16:59:17 Stefan Burkard wrote:
>> hi fop-users
>> 
>> i know i can use a custom font by building an xml-file with it's 
>> kerning-values and configuring a font definition in the
user-config-file.
>> 
>> but how can i include the font programatically? i don't like to have
a 
>> user-config-file at all. i found examples to set the font-directory
or 
>> base-directory programatically, but i didn't found an example for 
>> configuring fonts directly in java.
>> 
>> can anybody help me?
>> thanks
>> stefan


NOTICE
This e-mail and any attachments are confidential and may contain copyright material of Macquarie Bank or third parties. If you are not the intended recipient of this email you should not read, print, re-transmit, store or act in reliance on this e-mail or any attachments, and should destroy all copies of them. Macquarie Bank does not guarantee the integrity of any emails or any attached files. The views or opinions expressed are the author's own and may not reflect the views or opinions of Macquarie Bank.


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Re: Re how to embed a font programatically

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Have you seen my other post? I've implemented exactly what I proposed to
you and put it in FOP Trunk:
http://svn.apache.org/viewcvs?rev=379810&view=rev

More comments inline...

On 23.02.2006 07:02:33 Karl Roberts wrote:
> Hi I built a  URIResolver that reads URL's like:
> 
> servlet-context:/WEB-INF/metric.xml
> 
> And it works fine, however when it encounters a URL that doesn't match
> the servlet-context scheme I return null.

That's correct IMO. It's what I did.

> However I then get an NullPointerException 
> Caused by: javax.xml.transform.TransformerException:
> java.lang.NullPointerException
> 	at
> com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(T
> ransformerImpl.java:647)
> 	at
> com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(T
> ransformerImpl.java:279)
> 	....

Hmm, I don't get these with my implementation. TransformerException is
not the real exception. Look further down in the stack trace where you
should find the right place where the exception occurred. The above
doesn't really help. Setting an exception breakpoint for
NullPointerException will also be useful.

> I do return null from my URIResolver in the cases where the scheme
> doesn't match the one I'm after, and FOURIResolver kicks in, but it
> never seems to come back to my URIResolver.
> Should I instead delegate to FOURIResolver rather than return null?

No, I don't think so. There must be something else that's wrong. Can you
try my implementation instead?
http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/src/java/org/apache/fop/servlet/ServletContextURIResolver.java

<snip/>

Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Re: Re how to embed a font programatically

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Since I'm looking into URIResolvers anyway at the moment, I went ahead
and implemented that URIResolver I suggested. It's now available in FOP
Trunk: http://svn.apache.org/viewcvs?rev=379810&view=rev

Just in case you haven't got around to do it, yet. :-)

On 21.02.2006 22:29:11 Karl Roberts wrote:
> Hi Jeremias,
> 
> Cheers for that. My main issue is that I wanted to as little hacking of
> the underlying FOP as possible so that I can keep all my stuff in
> separate extended classes (less source control issues) and do it as fast
> as possible ;-)
> 
> Being quite new to FOP I am unaware of some of the design philosopies.
> Injecting my own URIResolver is a much nicer solution than what I was
> going to do.
> 
> So thanks again
> 
> Karl
> 
> -----Original Message-----
> From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch] 
> Sent: Tuesday, 21 February 2006 8:17 PM
> To: fop-users@xmlgraphics.apache.org
> Subject: Re: Re how to embed a font programatically
> 
> I haven't done this from outside FOP, yet, but I can give you a
> suggestion that I'd prefer in your case: Write a URIResolver
> implementation that returns StreamSource instances for specific URIs.
> You can take FOURIResolver as an example for how to do that. You can
> then set the URIResolver instance on the FOUserAgent using
> setURIResolver().
> 
> This allows you to specify the font URLs like this:
> servlet-context:/WEB-INF/metric.xml
> 
> In your URIResolver you simply check if the URI starts with
> "servlet-context:" and extract the path. You then simply construct a
> StreamSource giving it the InputStream returned by getResourceAsStream
> (). If a URI doesn't start with the above prefix just return null and
> the default URIResolver will kick in.
> 
> The clue about all this is that you have to do less "FOP-hacking" and
> instead use the normal configuration mechanism. It should only take 30
> minutes to hack.
> 
> I hope that helps.
> 
> On 21.02.2006 03:41:01 Karl Roberts wrote:
> > Hi,
> > Did you ever manage to do this?
> > 
> > I need to do something similar because my embedded FOP is running in a
> 
> > Webapp and the font metric file and the TTF file are hidden inside the
> 
> > WEB-INF directory and so the metric-url and embed-url in my fop-config
> 
> > is no good (even if they were accessible via a URL I don't know the 
> > hostname or context path until runtime)
> > 
> > What would be really nice is someway to pass the fontMetric and TTF 
> > file to the Renderer as InputSource (or InputStream). This way I can 
> > get the font files from the hidden WEB-INF dir using
> > 
> > InputStream fontmetric =
> > serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/metric
> > .x
> > ml");
> > InputStream embed =
> > serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/font.T
> > TF
> > ");
> > Renderer myRenderer = new PDFRenderer(); FontInfo myFontInfo = new 
> > FontInfo();
> > 
> > // method needed ...(ahem)
> > myFontInfo.addFont("FontName", "FontStyle", "FontWeight", fontmetric, 
> > embed);
> >  
> > myRenderer.setupFontInfo(myFontInfo);
> > userAgent.setRendererOverride(myRenderer);
> > 
> > Am I on the right track? Has anyone done this (or better) and mind 
> > sharing the code?
> > 
> > Cheers
> > 
> > Karl
> > 
> > On 2006-01-26 16:22:42 Jeremias Maerki wrote:
> > > Looking at the source code, you'd need to do the following:
> > > - Subclass the Renderer implementation (ex. PDFRenderer) so you gain
> 
> > > access to the protected fontList member variable.
> > > - Fill the fontList variable much like
> > > FontSetup.buildFontListFromConfiguration() does. This code will need
> > to
> > > run before you start the rendering run or at least before
> > > PrintRenderer.setupFontInfo() is called.
> > > - Instantiate your subclassed Renderer yourself and set it on the 
> > > FOUserAgent using setRendererOverride().
> > >
> > > Have fun!
> > 
> > > On 26.01.2006 16:59:17 Stefan Burkard wrote:
> > >> hi fop-users
> > >> 
> > >> i know i can use a custom font by building an xml-file with it's 
> > >> kerning-values and configuring a font definition in the
> > user-config-file.
> > >> 
> > >> but how can i include the font programatically? i don't like to 
> > >> have
> > a
> > >> user-config-file at all. i found examples to set the font-directory
> > or
> > >> base-directory programatically, but i didn't found an example for 
> > >> configuring fonts directly in java.
> > >> 
> > >> can anybody help me?
> > >> thanks
> > >> stefan


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Re: Re how to embed a font programatically

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
I haven't done this from outside FOP, yet, but I can give you a
suggestion that I'd prefer in your case: Write a URIResolver
implementation that returns StreamSource instances for specific URIs.
You can take FOURIResolver as an example for how to do that. You can
then set the URIResolver instance on the FOUserAgent using
setURIResolver().

This allows you to specify the font URLs like this:
servlet-context:/WEB-INF/metric.xml

In your URIResolver you simply check if the URI starts with
"servlet-context:" and extract the path. You then simply construct a
StreamSource giving it the InputStream returned by getResourceAsStream
(). If a URI doesn't start with the above prefix just return null and
the default URIResolver will kick in.

The clue about all this is that you have to do less "FOP-hacking" and
instead use the normal configuration mechanism. It should only take 30
minutes to hack.

I hope that helps.

On 21.02.2006 03:41:01 Karl Roberts wrote:
> Hi,
> Did you ever manage to do this?
> 
> I need to do something similar because my embedded FOP is running in a
> Webapp and the font metric file and the TTF file are hidden inside the
> WEB-INF directory and so the metric-url and embed-url in my fop-config
> is no good (even if they were accessible via a URL I don't know the
> hostname or context path until runtime)
> 
> What would be really nice is someway to pass the fontMetric and TTF file
> to the Renderer as InputSource (or InputStream). This way I can get the
> font files from the hidden WEB-INF dir using
> 
> InputStream fontmetric =
> serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/metric.x
> ml");
> InputStream embed =
> serve.servlet.getServletContext().getResourceAsStream("/WEB-INF/font.TTF
> "); 
> Renderer myRenderer = new PDFRenderer();
> FontInfo myFontInfo = new FontInfo();
> 
> // method needed ...(ahem)
> myFontInfo.addFont("FontName", "FontStyle", "FontWeight", fontmetric,
> embed);
>  
> myRenderer.setupFontInfo(myFontInfo);
> userAgent.setRendererOverride(myRenderer);
> 
> Am I on the right track? Has anyone done this (or better) and mind
> sharing the code?
> 
> Cheers
> 
> Karl
> 
> On 2006-01-26 16:22:42 Jeremias Maerki wrote:
> > Looking at the source code, you'd need to do the following:
> > - Subclass the Renderer implementation (ex. PDFRenderer) so you gain
> > access to the protected fontList member variable.
> > - Fill the fontList variable much like
> > FontSetup.buildFontListFromConfiguration() does. This code will need
> to
> > run before you start the rendering run or at least before
> > PrintRenderer.setupFontInfo() is called.
> > - Instantiate your subclassed Renderer yourself and set it on the
> > FOUserAgent using setRendererOverride().
> >
> > Have fun!
> 
> > On 26.01.2006 16:59:17 Stefan Burkard wrote:
> >> hi fop-users
> >> 
> >> i know i can use a custom font by building an xml-file with it's 
> >> kerning-values and configuring a font definition in the
> user-config-file.
> >> 
> >> but how can i include the font programatically? i don't like to have
> a 
> >> user-config-file at all. i found examples to set the font-directory
> or 
> >> base-directory programatically, but i didn't found an example for 
> >> configuring fonts directly in java.
> >> 
> >> can anybody help me?
> >> thanks
> >> stefan



Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org