Page 1 of 1

Fallback fonts

PostPosted: Fri Mar 05, 2010 2:53 am
by Animal
I'm setting up paragraph styles correctly according to a font name using

Code: Select all
        if (isNotBlank(font)) {
            RFonts rf = new RFonts();
            rf.setAscii(font);
            rPr.setRFonts(rf);
        }


How do I allow it to fall back if a font is not available?

In HTML and XSL-FO, I can specify "arial,tahoma,sans-serif" in order of preference. That does not work in Word - I've tried it.

Is such a thing possible?

Re: Fallback fonts

PostPosted: Fri Mar 05, 2010 8:03 am
by jason
Just to clarify, your scenario is creation of a Word document (as opposed to PDF/HTML output)?

And are you running on a server, where you control what fonts are present, or on end user machines, where you don't?

Word's font stuff is in the Font part and the Theme part. Maybe the spec has something further to say (its a while since I've had to look at the fonts stuff, I'm happy to say), but off hand, there are a couple of things in docx4j which may be relevant. One is the font mappers (discussed a bit in the forum a year or so ago), another is the ability (per the spec) to embed a font in the document, so that it is available in Word when the document is opened.

If you are creating Word documents on end user machines, you might re-use some of the code in the font mappers to do font discovery, and create documents which only use available fonts. See for example http://dev.plutext.org/trac/docx4j/brow ... apper.java
The font mapper stuff is used in HTML/PDF output, and can be used in editing applications (eg docx4all).

But if you are sending Word documents to end user machines, it may be the latter you are after http://dev.plutext.org/trac/docx4j/brow ... tPart.java

Re: Fallback fonts

PostPosted: Sat Mar 06, 2010 12:52 am
by Animal
I'm creating Word (or PDF or whatever) documents on a server. I'll have no idea what fonts are available on the client.

I don't think what I want can be done. Word will not fall back.

I tried to create a cascade of "basedOn" styles in response to the element

Code: Select all
<page font="my-special-font,arial">


The styles part ended up like this:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 1.0.8.0 (http://www.liquid-technologies.com) -->
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" xmlns:ns7="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
   <w:docDefaults>
      <w:rPrDefault>
         <w:rPr>
            <w:rFonts w:ascii="arial" />
            <w:color w:val="CornflowerBlue" />
            <w:sz w:val="16" />
         </w:rPr>
      </w:rPrDefault>
      <w:pPrDefault>
         <w:pPr />
      </w:pPrDefault>
   </w:docDefaults>
   <w:style w:type="paragraph" w:styleId="paragraphmy-special-font2" w:customStyle="false">
      <w:name w:val="paragraphmy-special-font2" />
      <w:pPr />
      <w:rPr>
         <w:rFonts w:ascii="my-special-font" />
      </w:rPr>
   </w:style>
   <w:style w:type="paragraph" w:styleId="paragraphparagraphmy-special-font2red1" w:customStyle="false">
      <w:name w:val="paragraphparagraphmy-special-font2red1" />
      <w:basedOn w:val="paragraphmy-special-font2" />
      <w:pPr />
      <w:rPr>
         <w:color w:val="red" />
      </w:rPr>
   </w:style>
   <w:style w:type="paragraph" w:styleId="paragraphparagraphparagraphmy-special-font2red1RIGHT8" w:customStyle="false">
      <w:name w:val="paragraphparagraphparagraphmy-special-font2red1RIGHT8" />
      <w:basedOn w:val="paragraphparagraphmy-special-font2red1" />
      <w:pPr>
         <w:jc w:val="right" />
      </w:pPr>
      <w:rPr />
   </w:style>
</w:styles>


You see that the docDefaults has "arial", and the lowest level style upon which all others build specifies "my-special-font"

But Word does not fall back to a value from an ancestor style if the named font does not exist. It actually attempts to use the nonexistent font, but displays Times New Roman.

I will not be sending any embedded font definitions!

I'll have to specify that document template authors use fonts which will exist upon the client machines likely to be downloading documents.