Hi,

what you need is an instance of FontInfo, from there you can retrieve the "Font" you need and call getCharWidth().

If you are using FOP version 1.1 or older use the following code snippet:

import org.apache.fop.apps.FOPException;
import org.apache.fop.fonts.EmbedFontInfo;
import org.apache.fop.fonts.FontCollection;
import org.apache.fop.fonts.FontEventListener;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontInfoConfigurator;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontManagerConfigurator;
import org.apache.fop.fonts.FontResolver;
import org.apache.fop.fonts.FontSetup;

FontInfo fontInfo = new FontInfo();

FontResolver fontResolver = FontManager.createMinimalFontResolver();

FontManager fontManager = new FontManager();

final FontEventListener listener = null;
final boolean strict = false;

FontManagerConfigurator fm = new FontManagerConfigurator(this.getConfiguration());

fm.configure(fontManager, true);

FontInfoConfigurator fontInfoConfigurator
= new FontInfoConfigurator(this.getConfiguration(), fontManager, fontResolver, listener, strict);
List<EmbedFontInfo> fontInfoList = new ArrayList<EmbedFontInfo>();
fontInfoConfigurator.configure(fontInfoList);

if (fontManager.useCache()) {
    fontManager.getFontCache().save();
}

FontSetup.setup(fontInfo, fontInfoList, fontResolver);

fontManager.setup(fontInfo, new FontCollection[]{});

For the FOP trunk version the following code snippet has to be used:

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopConfParser;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryBuilder;
import org.apache.fop.apps.io.InternalResourceResolver;
import org.apache.fop.apps.io.Resource;
import org.apache.fop.apps.io.ResourceResolver;
import org.apache.fop.apps.io.ResourceResolverFactory;

import org.apache.fop.fonts.DefaultFontConfig;
import org.apache.fop.fonts.DefaultFontConfigurator;
import org.apache.fop.fonts.EmbedFontInfo;
import org.apache.fop.fonts.FontCacheManagerFactory;
import org.apache.fop.fonts.FontCollection;
import org.apache.fop.fonts.FontDetectorFactory;
import org.apache.fop.fonts.FontEventListener;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontManagerConfigurator;
import org.apache.fop.fonts.FontSetup;

FontInfo fontInfo = new FontInfo();

final FontEventListener listener = null;
final boolean strict = false;

Configuration cfg = this.getConfiguration();

ResourceResolver resResolver = ResourceResolverFactory.createDefaultResourceResolver();

InternalResourceResolver resourceResolver
= ResourceResolverFactory.createDefaultInternalResourceResolver(this.getBaseURI());

FontManager fontManager = new FontManager(resourceResolver, FontDetectorFactory.createDefault(),
        FontCacheManagerFactory.createDefault());

FontManagerConfigurator fm = new FontManagerConfigurator(cfg, this.getBaseURI(), resResolver);

fm.configure(fontManager, true);

DefaultFontConfig.DefaultFontConfigParser parser
        = new DefaultFontConfig.DefaultFontConfigParser();
DefaultFontConfig fontInfoConfig = parser.parse(cfg, strict);
DefaultFontConfigurator fontInfoConfigurator
        = new DefaultFontConfigurator(fontManager, listener, strict);
List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fontInfoConfig);
fontManager.saveCache();

FontSetup.setup(fontInfo, fontInfoList, resourceResolver, false);

fontManager.setup(fontInfo, new FontCollection[]{});

return fontInfo;

Best regards,
Matthias

On 16.11.2012 15:23, cdellasanta wrote:
Hi all,

I'm trying to auto size a font to not exceed the block dimensions, giving a
default max size and downgrading to predefined steps until it fits or a
minimum size is reached.

I can't use the SVG "trick" because I want to fit to a max defined width,
and and as the size is reduced more text will fit in a single line.

My idea is to build an external processor step that calculates the optimum
font size to be used for the block. But to do this I need the exact font
width (in millipoints) of each character (or word) of the selected font
triplet on each font size I intend to use.

Browsing the fop project  source I've found the
org.apache.fop.fonts.Font.getCharWidth(), but It's for me a bit tricky to
instantiate correctly this Class.

I assume the the best way to get this is to start from the
org.apache.fop.tools.fontlist.FontListMain that does already the fop
configuration and loading of the font. But as a xsl-fo end-user, I've no
knowledge on the fop internals (structure/architecture) to go from A to B.

Can you please give me some hints or practical examples?
Wold it make sense to extend the org.apache.fop.tools to provide this
functionality?
Or am I on the wrong way?

Thanks in advance for any hint or feedback you can provide.

Regards.

p.s. I'm generating documents using a custom font, that I've imported using
org.apache.fop.fonts.apps.TTFReader and imported using the specific
configuration file.




--
View this message in context: 
http://apache-fop.1065347.n5.nabble.com/Extract-font-width-using-FOP-tp37360.html
Sent from the FOP - Users mailing list archive at Nabble.com.

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



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

Reply via email to