![]() |
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.
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.
parse-docx-document, in
docxin.scm
.
parse-odt-document, in
odtin.scm
.
office->texmacs, in
officetm.scm
.
texmacs->office, in
tmoffice.scm
.
serialize-docx-document in
docxout.scm
and serialize-odt-document in
odtout.scm
.
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.
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.
(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 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 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).
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.
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.
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.
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:
In Word, a list is not a structure: its items are paragraphs with the number of a list and a level, from which docx-group-lists rebuilds the nested lists. The items of one list may refer to several lists which share their definition.
The borders of a Word table come from its style (with other borders for the first row, the last one, the bands of rows…), from the table and from each cell: docx-cell-format works out the sides of each cell.
A field of Word is several runs: its beginning, its instruction, a separator, its result, its end (docx-inlines). A field HYPERLINK is a link, REF a reference, and of the other ones the result is kept.
In OpenDocument the formatting by hand is in automatic styles, and the spaces of the file do not all count (odt-collapse, text:s).
A list which goes on at a deeper level is written by some programs as a new list whose first item only holds a list (odt-merge-lists).
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.
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.
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.
A number is a node (seq "1") of a sequence (tmof-seq): the first of its candidates of which it is the next number (Theorem for all the theorems, whose counter is shared, or the name of the theorem; Figure, Table, Equation, Reference). A number which is the next one of no sequence has no sequence, and is written as it is.
The number of a heading is apart from its title (tmof-numbered-title), in the attribute number.
A label is a bookmark, and a link inside the document a node ref. As in TeXmacs, a label is the label of the last number before it: tmof-bind gives the bookmarks to the numbers (the attribute labels of a seq or of a heading) and tells each ref what it refers to (kind).
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.
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.
docxout.scm
: each list of the text is a list of its own for Word, so that its numbers start at 1; the notes go to their file; the links and the images are relations of the text.
odtout.scm
: the same formatting by hand is the same automatic style (ot-style); a formula is a file of MathML in a directory of the archive, listed in the manifest.
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.
A number which is not the next one of its sequence (numbers by sections) is text. The names of the theorems are only known in English on import.
The drawings, charts and text boxes of the office programs, comments and tracked changes are not imported.
The entries of an archive are written without compression.