Archive for the 'Software' Category

OpenOffice .odt Opened Up – Part 2: Meta and Settings

Monday, January 15th, 2007

Overview

In my last article, OpenOffice .odt Opened Up – Part 1: Overview, I discussed the overall package scheme for ODT documents, and pointed out that OpenOffice uses the subdocument form. In this article, I will be taking a closer look at two of simpler top level subdocuments of the four included in the specification. Specifically, we will be taking a closer look at the office:document-meta and office:document-settings elements.

As before, my test cases where produced with the following software:

  • SuSE Linux 10.1
  • OpenOffice 2.0.2.7.1
  • zip 2.31 (March 8th 2005)

The original source document can be downloaded here oo_part1.odt, and in particular the two subdocuments under observation can be downloaded here meta.xml and settings.xml.

The office:document-meta element

The office:document-meta element provides metadata with respect to the document, such as, author, creation time and editing time, among other data. The metadata elements can be either pre-defined or user defined. Pre-defined elements should be respected and updated by the editing application. User defined elements provides a more generic way of storing and using metadata. Each user defined metadata element is compossed of a name, a type and a value. Supporting applications can access this information and display it to the user based on its type. Both pre-defined and user defined should be able to be referenced through appropriate document text fields.

The pre-defined metadata elements are largely based upon the metadata standards developed by the Dublin Core Metadata Initiative (http://www.dublincore.org), thus many of the elements use the dc namespace.

There are 18 pre-defined metadata elements, these are listed below:

  • meta:generator
  • dc:title
  • dc:description
  • dc:subject
  • dc:keyword – Can appear multiple times
  • meta:initial-creator
  • dc:creator – Last modifier
  • meta:printed-by
  • meta:creation-date – Format YYYY-MM-DDThh:mm:ss
  • dc:date – Last modification date, format YYYY-MM-DDThh:mm:ss
  • meta:print-date
  • meta:template
  • meta:auto-reload
  • meta-hyperlink-behaviour
  • dc:language – As defined by RFC3066, with ISO 639 language code and ISO 3166 country code
  • meta:editing-cycles
  • meta:editing-duration – Format PnYnMnDTnHnMnS
  • meta:document-statistic – Can appear multiple times, ODT attributes below
    • meta:page-count
    • meta:table-count
    • meta:draw-count
    • meta:image-count
    • meta:ole-object-count
    • meta:paragraph-count
    • meta:word-count
    • meta:character-count
    • meta:row-count
    • meta:frame-count
    • meta:sentence-count
    • meta:syllable-count
    • meta:non-whitespace-character-count

As I suggested regarding the thumbnail image in a prior article, this information could easily be extracted and displayed to users of popular search engines such as Google, Yahoo and Ask. Additionally these services could allow the user to narrow their search based on certain criteria found in the metadata.

Provided here, meta.pl, is an example written in perl using the XML::Simple package that extracts the last editor, modification date, and page and word count. If anyone would like to contribute ports of this to another language feel free. If there is significant interest, I will cover XML::Simple or other XML packages or utilities.

The office:document-settings element

Next we take a look at the office:document-settings element. This element contains application settings that may impact thedocument. It does not caontain a complete set of application settings. Being application settings, there are no particular entries that are defined ih the ODF Specification. A office:document-settings element will contain one or more config:config-item-set elements, these elements will in turn contain config:config-item, config:config-item-set, config:config-item-map-named or config:config-item-map-indexed. The discovery of how each of these elements works with a particular application, such as OpenOffice.org Writer, is left as an exersise to the reader since they do not directly affect our goals of understanding ODF as it relates to Microsoft’s .doc format. Suffice it to say, the office:document-settings element is of little interest to all but an application developer.

What’s up next?

Next up we will investigate the significantly more interesting office:document-styles element. We will also learn some optimization techniques that we can apply to this element and perhaps discover a little of how it relates to thee office:document-content element.

Until next time,

-3Monkeys

Popularity: 8% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller

OpenOffice .odt Opened Up – Part 1: Overview

Friday, January 12th, 2007

Overview

In the first article in this series, OpenOffice ODF/.odt compared to Microsoft Word .doc, I compared various file types for size efficiency. Of particular interest was the fact that OpenOffice Write stores .odts in a zip format, an implementation of PKZip to be exact. With this knowledge and the Open Document Format standard, we can investigate how certain elements of a document effect its size and overall efficiency.

My test cases where produced with the following software:

  • SuSE Linux 10.1
  • OpenOffice 2.0.2.7.1
  • zip 2.31 (March 8th 2005)

Starting Out

As we previously observed, .odt documents are stored in ZIP format. It is possible to store the document as a single XML file that conforms to the OpenOffice.org document type definition (DTD). It is also possible to store the document as several subdocuments, each with a different document root that represents a particular aspect of the document, such as, content or style.
Quoting the Open Document Format for Office Applications (OpenDocument) v1.0 (Second Edition), (ODF Specification):

The OpenDocument format supports the following two ways of document representation:

  • As a single XML document.
  • As a collection of several subdocuments within a package (see section 17), each of which stores part of the complete document. Each subdocument has a different document root and stores a particular aspect of the XML document. For example, one subdocument contains the style information and another subdocument contains the content of the document. All types of documents, for example, text and spreadsheet documents, use the same document and subdocuments definitions.

There are four types of subdocuments, each with different root elements. Additionally, the single XML document has its own root element, for a total of five different supported root elements. The root elements are summarized in the following table:

Root Element Subdocument Content Subdoc. Name in Package
office:document Complete office document in a single XML document. n/a
office:document-content Document content and automatic styles used in the content. content.xml
office:document-styles Styles used in the document content and automatic styles used in the styles themselves. styles.xml
office:document-meta Document meta information, such as the author or the time of the last save action. meta.xml
office:document-settings Application-specific settings, such as the window size or printer information. settings.xml

So, what is in our reference .odt? We will use the Linux produced document from a prior article (oo_part1.odt) with XML compression disabled. We’ve done this so that the XML is more human readable. After we unzip the file using the Linux utility unzip, we have the raw files as shown below.

.odt unzipped directory tree

As you can see all four subdocuments as specified in the specification are present as well as several other files. In particular META-INF/manifest.xml list the contents of the package, including information such as full path and type.

The file Thumbnails/thumbnail.png although part of the package, is not part of the document. The thumbnail image should conform to the Thumbnail Managing Standard (TMS) at www.freedesktop.org, and therefore should be24bit, non-interlaced PNG image with full alpha transparency. The required size for the thumbnails is 128×128 pixel.

Here is the thumbnail from our reference document.

thumbnail.png

Having the thumbnail available in the package, allows other applications such as file managers to preview the document to the user. With a little creative programming, sites such as Google, Yahoo or Ask, could extract this thumbnail and preview the document for users, with little difficulty.

Document Elements

The office:document may contain any of the document elements listed below.

  • office:document-attrs
  • office:document-common-attrs
  • office:meta
  • office:settings
  • office:scripts
  • office:font-face-decls
  • office:styles
  • office:automatic-styles
  • office:master-styles
  • office:body

When the subdocument method is used however, elements are restricted to certain subdocuments.

Elements in content.xml

  • office:document-content (subdocument root)
  • office:document-common-attrs
  • office:scripts
  • office:font-face-decls
  • office:automatic-styles
  • office:body

Elements in styles.xml

  • office:document-styles (subdocument root)
  • office:document-attrs
  • office:document-common-attrs
  • office:font-face-decls
  • office:styles
  • office:automatic-styles
  • office:master-styles

Elements in meta.xml

  • office:document-meta (subdocument root)
  • office:document-common-attrs
  • office:meta

Elements in settings.xml

  • office:document-settings (subdocument root)
  • office:document-common-attrs
  • office:settings

What’s Up Next?

At this point we have a clear understanding of the subdocument method that OpenOffice applies to its ODF implementation, and we know what top level elements are handled by each subdocument.

In the next article, we will ease into the subdocument elements by exploring the office:document-meta and office:document-settings elements. These two elements are rather simple and will not require as much review compared to office:document-content or office:document-styles.
Until next time.

-3Monkeys

Popularity: 23% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller

Open Document Tutorial part 2:

Tuesday, July 11th, 2006

This article is being archived here from its original publication in the 3Monkeyweb wiki.

When a document has been edited in Word and OpenOffice certain artifact are introduced. One of my current projects is to clean up a 800 plus page specification that has been translated between .doc and .odt many times. This has resulted in a bloated document, with several inconsistencies. In my last article, I showed some underlying differences in the two formats, both from direct inspection and inference. Now comes the task of tackling the .odt.

Getting started

I will be using oo_ms_oo.doc.odt, as a reference document for this tutorial. As shown previously, .odt’s are nothing more than .zip files. unzip the file in an empty directory as follows.

% unzip ../oo_ms_oo_doc.odt
Archive:  ../oo_ms_oo_doc.odt
extracting: mimetype
inflating: layout-cache
inflating: content.xml
inflating: styles.xml
extracting: meta.xml
inflating: Thumbnails/thumbnail.png
inflating: settings.xml
inflating: META-INF/manifest.xml

I am going to skip all of the preamble information regarding .odt ”packages” and jump right into the meat of the problem. The files we are most concerned with are ”content.xml” and ”styles.xml”. Open ”styles.xml” in your favorite XML editor (I prefer oXygen). Ignoring, the root element, ”<office:document-styles>”, we see the first major element of the document <office:font-face-decls>. This is the element we will attack first. I will use Relax-NG Schema notation for elements.

<office:font-face-decls>

<define name="office-font-face-decls">
  <optional>
    <element name="office:font-face-decls">
      <zeroOrMore>
        <ref name="style-font-face"/>
      </zeroOrMore>
    </element>
  </optional>
</define>

As you can see, it is pretty simple. I only contains style-font-face
refs. So let us take a look at that element.

<define name="style-font-face">
  <element name="style:font-face">
    <ref name="style-font-face-attlist"/>
    <optional>
      <ref name="svg-font-face-src"/>
    </optional>
    <optional>
      <ref name="svg-definition-src"/>
    </optional>
  </element>
</define>

For brevity (and simplicity), I’m going to choose to ignore the optional ”svg-font-face-src” and ”svg-definition-src” refs. As a side note, I have not encountered these in real world situations. We are left with an <office:font-face-decls> element that contains zero or more <style:font-face”> elements. We can infer that the ref ”style-font-face-attlist” is an attribute list and does not contain any elements. I have verified that that is indeed the case, but the complete definition is too lengthy to list here. Here is the complete schema.

Basic strategy

We will iterate through the fonts comparing certain attributes. When we find two fonts that are similar enough, we can replace one with the other and remove the duplicate. This will be accomplished in two steps.

  1. Identify potential substitutions
    Once all substitutions have been identified, a map file is written to disk. This file can then be edited to suit the particular interest of the user.
  2. Perform the substitutions
    Once the map file is ready the script is run a second time to make all of the replacements.

The code
I choose to program this in perl with the help of the package XML::Simple. It certainly could have been done with some XSL filters, but would have been much more complicated. The complete perl script font-face-decls.pl can be downloaded from the ODT Tools file repository. Remember this was not intended as a production script. Therefore, I did not worry a lot about bounds checking, errors, or plain just making it look pretty. If you would like to volunteer to help on this project and combine this and future tools in to a well rounded package please contact me.

First we need to load ”content.xml” and ”styles.xml” then extract the <office:document-styles> element. I simple read each file in as one big string by locally undef’ing $/, then use a regular expression to extract the <office:document-styles> element to a string. Finally, I use XMLin to convert the element to a perl data structure. I could have actually, extracted <office:font-face-decls>

I don’t want to work with two structures, so the first thing I do is combine the styles and content hashes into a single hash. We check to make sure any combined elements contain the same attributes, adding any extra attributes as well.

So what in the XML do we want to modify?
Let us compare <style:font-face> elements to determine where we might make some improvements

The XML (edited for brevity))

<style:font-face style:name="StarSymbol"
style:font-charset="x-symbol"/>

<style:font-face style:name="Wingdings"
style:font-pitch="variable"
style:font-charset="x-symbol"/>

<style:font-face style:name="Symbol"
style:font-family-generic="roman"
style:font-pitch="variable"
style:font-charset="x-symbol"/>

<style:font-face style:name="Albany AMT1"
style:font-pitch="variable"/>

<style:font-face style:name="Albany AMT"
style:font-pitch="variable"/>

<style:font-face style:name="Lucidasans"
style:font-pitch="variable"/>

<style:font-face style:name="Thorndale AMT"
style:font-family-generic="roman"
style:font-pitch="variable"/>

<style:font-face style:name="Thorndale AMT1"
style:font-family-generic="roman"
style:font-pitch="variable"/>

Notice that ”Albany AMY” and ”Thorndale AMT” appear to be duplicated. Our first rule will be to replace any fonts whose names only differ by an appended sequential number. Next, we see that there are three fonts with a ”x-symbol” font-charset. One symbol font is plenty, therefore we can replace all symbol fonts with a single symbol font. Finally, we notice that, neither ”Albany AMT” or ”Lucidasans” has a ”style:font-family-generic” attribute. These both happen to belong to the ”swiss” generic font family. Since we are attacking this in two steps, we will be able to modify the ”font-face-decl.map” file in order to substitute one of these for the other. But let us consider the case where these two style:font-faces where described as follows.

The Hypothetical XML

<style:font-face style:name="Albany AMT"
style:font-family-generic="swiss"
style:font-pitch="variable"/>

<style:font-face style:name="Lucidasans"
style:font-family-generic="swiss"
style:font-pitch="variable"/>

If this were the case, then we could add a third rule to replace members of the same style:font-family-generic with a single font. Perhaps, I will update the example data to show this operation, but for now just be aware that I have tested this rule on the 800 page gorilla, and it is included in the script.

Running the script

As I stated above, the script I wrote is not of my normal professional quality, so the run environment is pretty strict. Volunteers? You must run the script from the directory that contains your extracted .odt. To create the map file run the following command.

% font-face-decls.pl map

This will result in the output of the two files ”font-face-decls.rpt” and ”font-face-decls.map”, see them below.

font-face-decls.rpt

StarSymbol                             x-symbol
Wingdings                              x-symbol                 StarSymbol
Symbol                 roman           x-symbol                 StarSymbol
Albany AMT1                                                     Albany AMT
Albany AMT
Lucidasans
Thorndale AMT          roman
Thorndale AMT1         roman                                    Thorndale AMT

font-face-decls.map

{
  'StarSymbol' => '',
  'Wingdings' => 'StarSymbol',
  'Symbol' => 'StarSymbol',
  'Albany AMT1' => 'Albany AMT',
  'Albany AMT' => '',
  'Lucidasans' => '',
  'Thorndale AMT' => '',
  'Thorndale AMT1' => 'Thorndale AMT',
};

As suggested previously, we want to modify the map file in order to eliminate one of either, ”Albany AMT” or ”Lucidasans”. Since ”Albany AMT” is alreadybeing used as a replacement, we will replace ”Lucidasans” with it as well. Therefore edit the map file ”Lucidasans” line to read.’Lucidasans’ => ‘Albany AMT’,We are now ready to perform the substitutions in bulk. Run the script in ”replace” mode as follows.

% font-face-decls.pl replace

We end up with two files ”comment-new.xml” and ”styles-new.xml”. If we examine either of these files, we will find that the <office:font-face-decls> element is reduced to three <style:font-face> elements as seen here.

<office:font-face-decls> (Edited for brevity)

<style:font-face style:name="StarSymbol"
style:font-charset="x-symbol"/>

<style:font-face style:name="Albany AMT"
style:font-pitch="variable"/>

<style:font-face style:name="Thorndale AMT"
style:font-family-generic="roman"
style:font-pitch="variable"/>

Some interesting points

You may notice in the code that when replacing one font with another we do this by first removing the duplicate style, then we substitute the replacement font name globally for the duplicated font name. There is a potential bug in the global substitution. Suppose the actual content of the document contained the duplicate font name ”in quotes” such as ””Symbol””. This would be replace by ””StarSymbol””, which is an incorrect substitution. We should limit our substitution to the element <office:document-styles>. Volunteers?

Second, notice that we are replacing the duplicate font name in certain style. That means that one or more styles may now be duplicated. We will investigate removing duplicate styles in the next installment.

Another point of interest, is the fact that we wrap our string in the call to XMLin with a static string. This resolves namespace issues but could be implemented cleaner.

Until next time…

Popularity: 9% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller