GNU TeXmacsUser manualReferenceAboutEnglish
The Word and OpenDocument converters

The converters for the documents of Word (.docx) and the texts of OpenDocument (.odt) are written in Scheme, in

convert/office

. Both formats are zip archives of XML files with nearly the same model of a document, in two vocabularies: they are read into one tree, the office tree, and written from it, so that the conversion from and to TeXmacs is written once. The user-level description is in converters for Word and OpenDocument.

1.The formats and the converters

init-office.scm

defines the formats docx and odt, loaded by need. Since define-format makes the converters between a file and its -document as a string, the document of these formats is the archive itself, a string of bytes.

docx-document \(\rightarrow\) office-stree

parse-docx-document, in

docxin.scm

.

odt-document \(\rightarrow\) office-stree

parse-odt-document, in

odtin.scm

.

office-stree \(\rightarrow\) texmacs-stree

office->texmacs, in

officetm.scm

.

texmacs-stree \(\rightarrow\) office-stree

texmacs->office, in

tmoffice.scm

.

office-stree \(\rightarrow\) docx-document, odt-document

serialize-docx-document in

docxout.scm

and serialize-odt-document in

odtout.scm

.

2.Zip archives

System/Files/zip_files.cpp

reads and writes zip archives without a library, so that the converters also work where none is linked (the browser). It reads the entries which are stored or deflated, with its own inflate, and writes all the entries stored, in the order which is given: an OpenDocument text must start with its entry mimetype. The date of the entries is always the same, so that the same contents give the same archive. There is no encryption and no zip64.

(zip-archive? s)

(zip-unpack s)

(zip-pack names datas)

Whether the string s is a zip archive; the list (name data name data …) of its entries (the directories are left out); the archive of the entries with these names and data.

3.The office tree

The tree is described at the head of

office-tools.scm

, which also has the tools on XML trees (ox-attr, ox-child, ox-serialize…). It is (office block …), with its strings in UTF-8.

Blocks

(p …) with the attributes role (title, subtitle, author, date, abstract, heading, quote, code, caption, figure, term, definition, toc), level, numbered (no for a heading without number) and align (when it is not the one of the usual paragraphs); (list (item block …) …) with the attributes kind, format (1, a, A, i, I) and start; (table (row (cell block …) …) …); (pagebreak), (rule), (toc), (meta …) and (setup).

The page

The block (setup) has the page (its size, its orientation, its margins) and the usual paragraph of the document: the size and the family of its font, its alignment, the indentation of its first line, the spaces around it and between its lines. The readers take the usual paragraph from the style of most plain paragraphs (docx-setup, odt-setup), with the defaults of the document and the fonts of the theme; office->texmacs makes it the initial environment of the document (oftm-setup), and texmacs->office takes it from there (tmof-setup), for the section properties of Word and the first master page of OpenDocument.

The style office

The blocks (styles (style …) …) (the named styles which the text uses, with their look), (header block …) (attributes place and kind), (package) (the file itself, attribute data) and (columns block …); the attribute style of a paragraph and the inlines (span …), (field …) (attribute code, or xml for the element of OpenDocument), (comment block …), (ins …) and (del …) (attributes author and date); the attribute crop of an image. With the option office->texmacs:faithful, office->texmacs makes a document of the style office (

styles/office.ts

): its macros office-par, office-span, office-field, office-comment, office-ins, office-del and office-columns, the macros of the named styles (oftm-style-macros), the headers (oftm-headers) and the file (office-package) in its initial environment. texmacs->office takes them back (tmof-package), and the writers reuse the parts of the file which TeXmacs does not edit (dx-merged-styles, dx-merged-numbering, the relations which are kept; ot-merged-styles).

Tables

A table has the attributes align, width (a part of the paragraph, 0.5par) and columns (the parts of the columns in the table). A cell has borders (the letters of its sides which have one, among t, b, l and r, or none), background, colspan, rowspan, header and align. The grid is full: the cells which a wider or a higher one covers are there, with the attribute covered.

Text

Strings, the wrappers em, strong, underline, strike, sub, sup, code, smallcaps, mark and color, and link, ref, bookmark, note, br, tab, image (with the bytes of its file as the attribute data) and math.

Mathematics

A node math holds MathML: its text (a file of an OpenDocument text), or a tree with the attribute form sxml. The attribute display tells a formula on its own lines.

4.Reading

Both readers work in the same way. They first read the styles, each with its own properties and the name of the style it is based on; docx-style and odt-style give the properties of a style with those which it inherits. What a paragraph is comes from the names of its style and of its parents (docx-roles, odt-roles): in a Word file the name of a style (heading 1) is in English even where its identifier is in the language of the program which wrote the file. The runs of text with the same properties are then merged (office-merge).

Some of what the files of real programs have, and which the readers handle:

5.Mathematics

The formulas go through MathML and its converters (

convert/mathml

). OpenDocument has MathML as it is. The formulas of Word are in its own format, OMML, whose constructions are those of MathML under other names:

omml.scm

maps one to the other in both directions (omml->mathml, mathml->omml). The text of a run of OMML is cut into identifiers, numbers and operators, which MathML wants apart; in the other direction a big operator takes what follows it as its operand, and brackets what they enclose.

oftm-from-mathml gives the converter of MathML its input as it wants it (elements with the prefix m:, an environment), and keeps the text of a formula which cannot be converted. On export, tmof-mathml writes the characters themselves for the names of MathML (α), which an office file does not know.

6.From and to TeXmacs

office->texmacs takes together the blocks which belong together (the paragraphs of a quotation, the lines of code, a figure or a table and its caption, terms and definitions) and writes a table as a tabular with few formats of rectangles of cells (oftm-rectangles), or as a block when all its borders are there.

texmacs->office comes from the converter to Markdown (see the Markdown converters), with the same handlers and the same expansion of the document before it is converted (edit_typeset_rep::exec_office,

tmoffice-expand.scm

). Its handlers build a tree like the one of Markdown, which tmof-finalize turns into the office tree. What Markdown has not is added: formulas as MathML, images with their bytes and their sizes (tmof-image), the formats of the tables (tmof-table), colors, notes in their place.

A drawing (graphics, alone or over a text) and an image which the office programs cannot read are made into a picture by the editor (tmof-picture): print-snippet typesets the tree and writes a PNG of it, and its PDF, of which pdf->svg-native makes an SVG with MuPDF (mupdf_pdf_to_svg in

Plugins/MuPDF/mupdf_pdf_renderer.cpp

; false in the builds without it). The node image then has the SVG as its attribute svg, and the writers put both in the archive: Word shows the SVG named by the extension of the bitmap, and a frame of OpenDocument has the two images, of which the first one that a program can show is shown. The SVG is only made for drawings: the one of the picture of an embedded image is not always shown well.

7.Numbers, labels and references

The numbers of a document which is expanded are text: Theorem 1, a section title laid out as a table of its number and its text, a link for each reference. texmacs->office makes of them what the office programs count themselves.

The writers make fields of them. In Word a number is a field SEQ inside the bookmarks of its labels, and a reference a field REF to one of them, which shows what the bookmark holds (\r, the number of its paragraph, for a heading); the headings are numbered by a list which their styles refer to. The fields are written in their long form (a run which begins the field, its instruction, a separator, its result, its end): not all programs read w:fldSimple. In OpenDocument a number is a text:sequence inside bookmarks, a reference a text:bookmark-ref to the text of the bookmark or to the number of the heading, and the headings are numbered by the outline style.

In the other direction the readers give the fields SEQ and the elements text:sequence as nodes seq, and office->texmacs takes away the names and the numbers which TeXmacs writes itself (oftm-named-start): a paragraph of the role theorem which starts with Lemma 2. is a lemma, a formula followed by its number an equation, the entries of a bibliography a bibliography. A reference is a reference when its target is the label of something with a number (oftm-numbered-labels) and its text is a number; else it stays a link with its text.

8.Writing

The writers build the XML files as trees and serialize them (ox-serialize). Both write named styles for the paragraphs, so that the result can be restyled.

9.Tests

The suite office (

check/office-test.scm

) tests the archives, the two readers on small archives which it makes from their XML, the formulas, the conversions from and to TeXmacs, the writers by reading back what they write, and the round trips through both formats: (run-regression-suite "office").

The suite cannot tell whether the office programs accept the files. For this, convert a file which was exported with LibreOffice (soffice --headless --convert-to pdf), read it with pandoc, and open it in Word. pandoc also makes test documents of both formats from Markdown.

10.Known limitations

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".