I'm trying to use a TrueType font in AFP with Apache FOP 2.2.
I believe I configured things correctly, but I'm getting the following exception:
java.lang.ClassCastException: org.apache.fop.afp.modca.MapDataResource cannot be cast to org.apache.fop.afp.modca.MapCodedFont
at org.apache.fop.afp.modca.ActiveEnvironmentGroup.getCurrentMapCodedFont(ActiveEnvironmentGroup.java:286)
at org.apache.fop.afp.modca.ActiveEnvironmentGroup.createFont(ActiveEnvironmentGroup.java:201)
at org.apache.fop.afp.modca.AbstractPageObject.createFont(AbstractPageObject.java:149)
at org.apache.fop.afp.DataStream.createFont(DataStream.java:331)
at org.apache.fop.afp.DataStream.addFontsToCurrentPage(DataStream.java:313)
at org.apache.fop.render.afp.AFPDocumentHandler.endPage(AFPDocumentHandler.java:310)
at org.apache.fop.render.intermediate.util.IFDocumentHandlerProxy.endPage(IFDocumentHandlerProxy.java:167)
at org.apache.fop.render.intermediate.EventProducingFilter.endPage(EventProducingFilter.java:48)
at org.apache.fop.render.intermediate.IFRenderer.renderPage(IFRenderer.java:603)
at org.apache.fop.area.RenderPagesModel.renderPage(RenderPagesModel.java:193)
at org.apache.fop.area.RenderPagesModel.checkPreparedPages(RenderPagesModel.java:174)
at org.apache.fop.area.RenderPagesModel.addPage(RenderPagesModel.java:146)
at org.apache.fop.layoutmgr.AbstractPageSequenceLayoutManager.finishPage(AbstractPageSequenceLayoutManager.java:316)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.finishPage(PageSequenceLayoutManager.java:226)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.activateLayout(PageSequenceLayoutManager.java:145)
at org.apache.fop.area.AreaTreeHandler.endPageSequence(AreaTreeHandler.java:267)
at org.apache.fop.fo.pagination.PageSequence.endOfNode(PageSequence.java:130)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:360)
at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:190)
Below is a snippet of my conf.xml configuration:
<renderer mime="application/x-afp">
<fonts>
<font name="Script MT Bold" kerning="yes" embed-url="/fonts/SCRIPTBL.TTF">
<font-triplet name="Script MT Bold" style="normal" weight="normal"/>
</font>
</fonts>
</renderer>
Do I require additional configurations?
Additionally, must I embed the TrueType font in AFP or can I simply include a reference and rely on the mainframe printer to resolve it?
I also faced the same exception. It seems like while creating the fonts for rendering the current page FOP populates a list of AbstractStructuredObject.
While loading the True Type font FOP creates a MapDataResource for it and populates the same in the list of AbstractStructuredObject.
However if the page has some other font such as a raster font then before loading this raster font it gets the most recent AbstractStructuredObject from the list and typecasts it to MapCodedFont. Since, the recent most object was of of type MapDataResource we get ClassCastException.
As a workaround I am currently using only True Type fonts in my AFP document and have added the default font-triplet to one of the font configuration.
<font name="Script MT Bold" kerning="yes" embed-url="/fonts/SCRIPTBL.TTF">
<font-triplet name="Script MT Bold" style="normal" weight="normal"/>
<font-triplet name="any" style="normal" weight="normal"/>
</font>