Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

OpenOffice document with all possible fonts

by oakbox (Chaplain)
on Jul 20, 2005 at 07:44 UTC ( #476398=sourcecode: print w/replies, xml ) Need Help??
Category: Text Processing
Author/Contact Info Richard Still
Description: I use OpenOffice a lot in my work. Generating XML files is, if not a snap, at least not too difficult, and from those XML files I get a whole universe of file types to export to.

This bit of code is useful for two reasons: 1) It does something useful. 2) If you have not fiddled around with OO, this is a chance to see how easy it is.

One issue I run into regularly is "What fonts can I use here?" You see, while OpenOffice might render something beautifully to the screen, it's pretty much a crap shoot on how those fonts will look in a PDF export, or how MS Word will render things on screen. For the *most* part, everything works just fine, but errors do creep in.

So, with this in mind, I wrote a little test script. This program:

  1. Looks in your OpenOffice settings (we have to assume you have OO installed)
  2. Opens up the pspfontcache file, this file holds all of the fonts that OO has found on your system
  3. Parses pspfontcache, grab the names of the fonts.
  4. Generate a simple content.xml file showing each font in action.
  5. zip's the results into a .sxw file called 'fonttest.sxw'

You can open fonttest.sxw in OpenOffice and print, exporttoPDF, email a copy of it to your designer and say "You can only use these fonts", etc.

This code has been tested only in Linux.

UPDATE: add comments, add code that searches for most recent version of OO.


# OpenOffice Font Printer
# Every time OpenOffice runs, it looks through paths on your
# system for possible fonts.  The found fonts are stored (with
# formatting information) to the pspfontcache file in your
# home directory.
# This script parses the pspfontcache file and creates a very
# basic oowriter file (.sxw) that displays all of the fonts
# in a single document.
# See for
# helpful information about font handling in OO.
# Usage: (in Linux)
# Run from command line under your own account
# perl
# Written by Richard Still
# Discuss on
# (c) Perl Artistic License

my $homedir = $ENV{'HOME'};
my $outfile = "fonttest.sxw";
my $psp =".openoffice"; # this is the standard location

# find the user directory, or the highest OO version
# user directory
my $seek_user = $homedir . "/" . $psp;
   opendir DIR, $seek_user || die $!;
my @directories = readdir DIR;
my $pspfonts;

my @possibilities;
foreach my $listing (@directories){
  if($listing eq "." || $listing eq ".."){ next; }
  if($listing eq "user"){ $pspfonts = "$psp/user/psprint/pspfontcache"
+; last;}
  push(@possibilities, $listing);

if($pspfonts eq ""){
  my @sorted = reverse sort @possibilities;
  $pspfonts = "$psp/$sorted[0]/user/psprint/pspfontcache";

# Attempt to open pspfontcache file
open(READ,"<$homedir/$pspfonts") || die "$homedir/$pspfonts $!\n";
my @READ = <READ>;

print "Using configuration file $pspfonts\n";

# Parse pspfontcache file
my $fontlist;

  my $var = shift @READ;
  chomp $var;
  # many simple regex's
  if($var =~ /File/  ){ next; }
  if($var =~ /Font/  ){ next; }
  if($var =~ /Empty/ ){ next; }
  if($var =~ /[0-3]\;/ ){ next; }
  if($var eq "" ){ next; }
  $fontlist->{$var} = 1;

# This is the opening stuff in content.xml
my $data = q|<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE office:document-content PUBLIC "-// Offic
+eDocument 1.0//EN" "office.dtd">
office:class="text" office:version="1.0">

# fonts are defined here.  I'm lying to OO, 
# I don't know what 'font-family-generic' *really* should be
# and the 'font-pitch' setting is iffy.  Well, it's not 
# perfect, but you have to make some compromises

foreach my $fontname (sort keys %{$fontlist}){
   $data .= qq|<style:font-decl style:name="$fontname" 
style:font-family-generic="swiss" style:font-pitch="variable"/>|;

$data .= qq|</office:font-decls>

# Make a definition for different paragraph types.  
# Each paragraph style gets a different font.

my $pnum;
foreach my $fontname (sort keys %{$fontlist}){
   $fontlist->{$fontname} = $pnum;
   $data .= qq|<style:style style:name="P$pnum" style:family="paragrap
+h" style:parent-style-name="Standard">
<style:properties style:font-name="$fontname"/>

   $data .= qq|</office:automatic-styles>
<text:sequence-decl text:display-outline-level="0" text:name="Illustra
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"

# Actually insert the text with the different 
# definitions driving the formatting
foreach my $fontname (sort keys %{$fontlist}){
   $data .= qq|<text:p text:style-name="P$fontlist->{$fontname}">$font
<text:p text:style-name="P$fontlist->{$fontname}" />

# close it all up
   $data .= qq|

print WRT $data;
close (WRT);

# Yes, you can have a complete .sxw file using
# ONLY the content.xml file.  Cool!

my @args = ("zip", "$outfile", "content.xml");
   system(@args) == 0
        or die "Boom!  Zip operation failed : $?";

print "'oowriter $outfile' should bring up the file\n";

Replies are listed 'Best First'.
Re: OpenOffice document with all possible fonts
by bfdi533 (Friar) on Jul 20, 2005 at 16:23 UTC

    This works great!

    ... except for one thing.

    The path to the file, at least on my system, is ~/.openoffice/x.y.z/user/psprint/pspfontcache, where x.y.z is the version of OpenOffice. For me I have 1.0.2, 1.1.0 and 1.1.4 meaning, I presume, that I actually used each of these versions on my system and a user profile was saved. The code should parse this out to get, presumably, the most current version of the code to create the font list from.

    Then it would be perfect!

      "Then it would be perfect!"

      Then I guess it's perfect now :) Updated the code to look for most recent version.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://476398]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-10-27 13:26 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (256 votes). Check out past polls.