Files
WebODF/webodf/lib/odf/Style2CSS.js
T

1042 lines
37 KiB
JavaScript
Raw Normal View History

/**
* @license
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
*
* @licstart
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public License
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. The code is distributed
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
*
* As additional permission under GNU AGPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* As a special exception to the AGPL, any HTML file which merely makes function
* calls to this code, and for that purpose includes it by reference shall be
* deemed a separate work for copyright law purposes. In addition, the copyright
* holders of this code give you permission to combine this code with free
* software libraries that are released under the GNU LGPL. You may copy and
* distribute such a system following the terms of the GNU AGPL for this code
* and the LGPL for the libraries. If you modify this code, you may extend this
* exception to your version of the code, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* version.
*
* This license applies to this entire compilation.
* @licend
* @source: http://www.webodf.org/
* @source: http://gitorious.org/webodf/webodf/
*/
2013-04-17 16:22:52 +02:00
2013-06-14 18:56:33 +02:00
/*global odf, runtime, xmldom, core, document, window*/
2013-04-17 16:22:52 +02:00
runtime.loadClass("odf.Namespaces");
2013-06-17 12:45:56 +10:00
runtime.loadClass("odf.OdfUtils");
2013-06-14 18:56:33 +02:00
runtime.loadClass("xmldom.XPath");
runtime.loadClass("core.CSSUnits");
2013-04-17 16:22:52 +02:00
2010-11-03 15:39:05 +01:00
/**
* @constructor
*/
odf.Style2CSS = function Style2CSS() {
"use strict";
var // helper constants
2013-04-17 16:22:52 +02:00
/**@const@type{!string}*/ drawns = odf.Namespaces.drawns,
/**@const@type{!string}*/ fons = odf.Namespaces.fons,
/**@const@type{!string}*/ stylens = odf.Namespaces.stylens,
/**@const@type{!string}*/ svgns = odf.Namespaces.svgns,
/**@const@type{!string}*/ textns = odf.Namespaces.textns,
/**@const@type{!string}*/ xlinkns = odf.Namespaces.xlinkns,
/**@const@type{!string}*/ presentationns = odf.Namespaces.presentationns,
2010-06-14 17:31:36 +02:00
/**@const@type{!Object.<string,!string>}*/
2010-11-03 00:01:21 +01:00
familynamespaceprefixes = {
2011-06-03 21:40:42 +02:00
'graphic': 'draw',
'drawing-page': 'draw',
2011-06-03 21:40:42 +02:00
'paragraph': 'text',
'presentation': 'presentation',
'ruby': 'text',
'section': 'text',
'table': 'table',
2010-11-03 00:01:21 +01:00
'table-cell': 'table',
'table-column': 'table',
'table-row': 'table',
2011-06-03 21:40:42 +02:00
'text': 'text',
'list': 'text',
'page': 'office'
2010-11-03 00:01:21 +01:00
},
2010-06-14 17:31:36 +02:00
/**@const@type{!Object.<string,!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
familytagnames = {
2011-06-03 21:40:42 +02:00
'graphic': ['circle', 'connected', 'control', 'custom-shape',
2010-11-03 00:01:21 +01:00
'ellipse', 'frame', 'g', 'line', 'measure', 'page',
'page-thumbnail', 'path', 'polygon', 'polyline', 'rect',
'regular-polygon' ],
2011-06-03 21:40:42 +02:00
'paragraph': ['alphabetical-index-entry-template', 'h',
2010-11-03 00:01:21 +01:00
'illustration-index-entry-template', 'index-source-style',
'object-index-entry-template', 'p',
'table-index-entry-template', 'table-of-content-entry-template',
'user-index-entry-template'],
2011-06-03 21:40:42 +02:00
'presentation': ['caption', 'circle', 'connector', 'control',
2010-11-03 00:01:21 +01:00
'custom-shape', 'ellipse', 'frame', 'g', 'line', 'measure',
'page-thumbnail', 'path', 'polygon', 'polyline', 'rect',
'regular-polygon'],
'drawing-page': ['caption', 'circle', 'connector', 'control', 'page',
'custom-shape', 'ellipse', 'frame', 'g', 'line', 'measure',
'page-thumbnail', 'path', 'polygon', 'polyline', 'rect',
'regular-polygon'],
2011-06-03 21:40:42 +02:00
'ruby': ['ruby', 'ruby-text'],
'section': ['alphabetical-index', 'bibliography',
2010-11-03 00:01:21 +01:00
'illustration-index', 'index-title', 'object-index', 'section',
'table-of-content', 'table-index', 'user-index'],
2011-06-03 21:40:42 +02:00
'table': ['background', 'table'],
2010-11-03 00:01:21 +01:00
'table-cell': ['body', 'covered-table-cell', 'even-columns',
'even-rows', 'first-column', 'first-row', 'last-column',
'last-row', 'odd-columns', 'odd-rows', 'table-cell'],
'table-column': ['table-column'],
'table-row': ['table-row'],
2011-06-03 21:40:42 +02:00
'text': ['a', 'index-entry-chapter', 'index-entry-link-end',
2010-11-03 00:01:21 +01:00
'index-entry-link-start', 'index-entry-page-number',
'index-entry-span', 'index-entry-tab-stop', 'index-entry-text',
'index-title-template', 'linenumbering-configuration',
'list-level-style-number', 'list-level-style-bullet',
2011-04-19 21:46:03 +02:00
'outline-level-style', 'span'],
2011-06-03 21:40:42 +02:00
'list': ['list-item']
2010-11-03 00:01:21 +01:00
},
2010-06-14 17:31:36 +02:00
/**@const@type{!Array.<!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
textPropertySimpleMapping = [
[ fons, 'color', 'color' ],
// this sets the element background, not just the text background
[ fons, 'background-color', 'background-color' ],
[ fons, 'font-weight', 'font-weight' ],
2013-06-14 18:56:33 +02:00
[ fons, 'font-style', 'font-style' ]
2010-11-03 00:01:21 +01:00
],
/**@const@type{!Array.<!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
bgImageSimpleMapping = [
[ stylens, 'repeat', 'background-repeat' ]
],
/**@const@type{!Array.<!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
paragraphPropertySimpleMapping = [
[ fons, 'background-color', 'background-color' ],
[ fons, 'text-align', 'text-align' ],
[ fons, 'text-indent', 'text-indent' ],
2013-06-13 21:40:57 +02:00
[ fons, 'padding', 'padding' ],
2010-11-03 00:01:21 +01:00
[ fons, 'padding-left', 'padding-left' ],
[ fons, 'padding-right', 'padding-right' ],
[ fons, 'padding-top', 'padding-top' ],
[ fons, 'padding-bottom', 'padding-bottom' ],
[ fons, 'border-left', 'border-left' ],
[ fons, 'border-right', 'border-right' ],
[ fons, 'border-top', 'border-top' ],
[ fons, 'border-bottom', 'border-bottom' ],
2013-06-13 21:40:57 +02:00
[ fons, 'margin', 'margin' ],
2010-11-03 00:01:21 +01:00
[ fons, 'margin-left', 'margin-left' ],
[ fons, 'margin-right', 'margin-right' ],
[ fons, 'margin-top', 'margin-top' ],
[ fons, 'margin-bottom', 'margin-bottom' ],
[ fons, 'border', 'border' ]
],
2011-09-29 13:57:04 +02:00
/**@const@type{!Array.<!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
graphicPropertySimpleMapping = [
2013-05-17 12:05:26 +10:00
[ fons, 'background-color', 'background-color'],
2010-11-03 00:01:21 +01:00
[ fons, 'min-height', 'min-height' ],
[ drawns, 'stroke', 'border' ],
2013-06-13 21:40:57 +02:00
[ svgns, 'stroke-color', 'border-color' ],
[ svgns, 'stroke-width', 'border-width' ]
2010-11-03 00:01:21 +01:00
],
2011-09-29 13:57:04 +02:00
/**@const@type{!Array.<!Array.<!string>>}*/
2010-11-03 00:01:21 +01:00
tablecellPropertySimpleMapping = [
[ fons, 'background-color', 'background-color' ],
[ fons, 'border-left', 'border-left' ],
[ fons, 'border-right', 'border-right' ],
[ fons, 'border-top', 'border-top' ],
2012-04-27 12:39:11 +02:00
[ fons, 'border-bottom', 'border-bottom' ],
[ fons, 'border', 'border' ]
2012-04-27 09:46:01 +02:00
],
tablecolumnPropertySimpleMapping = [
[ stylens, 'column-width', 'width' ]
],
/**@const@type{!Array.<!Array.<!string>>}*/
2012-04-27 09:46:01 +02:00
tablerowPropertySimpleMapping = [
[ stylens, 'row-height', 'height' ],
[ fons, 'keep-together', null ]
],
/**@const@type{!Array.<!Array.<!string>>}*/
2012-04-27 09:46:01 +02:00
tablePropertySimpleMapping = [
[ stylens, 'width', 'width' ],
[ fons, 'margin-left', 'margin-left' ],
[ fons, 'margin-right', 'margin-right' ],
[ fons, 'margin-top', 'margin-top' ],
[ fons, 'margin-bottom', 'margin-bottom' ]
],
/**@const@type{!Array.<!Array.<!string>>}*/
pageContentPropertySimpleMapping = [
[ fons, 'background-color', 'background-color' ],
[ fons, 'padding', 'padding' ],
[ fons, 'padding-left', 'padding-left' ],
[ fons, 'padding-right', 'padding-right' ],
[ fons, 'padding-top', 'padding-top' ],
[ fons, 'padding-bottom', 'padding-bottom' ],
[ fons, 'border', 'border' ],
[ fons, 'border-left', 'border-left' ],
[ fons, 'border-right', 'border-right' ],
[ fons, 'border-top', 'border-top' ],
[ fons, 'border-bottom', 'border-bottom' ],
[ fons, 'margin', 'margin' ],
[ fons, 'margin-left', 'margin-left' ],
[ fons, 'margin-right', 'margin-right' ],
[ fons, 'margin-top', 'margin-top' ],
[ fons, 'margin-bottom', 'margin-bottom' ]
],
/**@const@type{!Array.<!Array.<!string>>}*/
pageSizePropertySimpleMapping = [
[ fons, 'page-width', 'width' ],
[ fons, 'page-height', 'height' ]
],
// A font-face declaration map, to be populated once style2css is called.
/**@type{!Object.<string,string>}*/
2013-06-17 12:45:56 +10:00
fontFaceDeclsMap = {},
utils = new odf.OdfUtils(),
2013-06-14 18:56:33 +02:00
documentType,
odfRoot,
defaultFontSize,
xpath = new xmldom.XPath(),
cssUnits = new core.CSSUnits();
2011-09-29 13:57:04 +02:00
2010-11-03 00:01:21 +01:00
// helper functions
2010-11-03 15:39:05 +01:00
/**
* @param {!Element} stylesnode
* @return {!Object}
*/
function getStyleMap(stylesnode) {
2010-11-03 00:01:21 +01:00
// put all style elements in a hash map by family and name
var stylemap = {}, node, name, family, style;
if (!stylesnode) {
return stylemap;
}
node = stylesnode.firstChild;
2010-11-03 00:01:21 +01:00
while (node) {
if (node.namespaceURI === stylens &&
((node.localName === 'style') ||
(node.localName === 'default-style'))) {
family = node.getAttributeNS(stylens, 'family');
2011-04-19 21:46:03 +02:00
} else if (node.namespaceURI === textns &&
node.localName === 'list-style') {
family = "list";
} else if (node.namespaceURI === stylens &&
(node.localName === 'page-layout' || node.localName === 'default-page-layout')) {
family = "page";
} else {
// Skip insignificant white-space only nodes in the style tree
family = undefined;
2010-11-03 00:01:21 +01:00
}
if (family) {
// get style name
name = node.getAttributeNS &&
node.getAttributeNS(stylens, 'name');
if (!name) {
// For a default style, there is no name
name = '';
}
// get style (and create, if not yet existing)
style = stylemap[family] = stylemap[family] || {};
// then store style node in map
style[name] = node;
}
node = node.nextSibling;
2010-11-03 00:01:21 +01:00
}
2010-11-03 00:01:21 +01:00
return stylemap;
2010-06-14 17:31:36 +02:00
}
2010-11-03 15:39:05 +01:00
/**
* @param {?Object} stylestree
* @param {?string} name
* @return {?string}
*/
2010-11-03 00:01:21 +01:00
function findStyle(stylestree, name) {
if (!name || !stylestree) {
return null;
}
if (stylestree[name]) {
return stylestree[name];
}
var n,
2012-04-27 09:46:01 +02:00
style;
2010-11-03 00:01:21 +01:00
for (n in stylestree) {
if (stylestree.hasOwnProperty(n)) {
style = findStyle(stylestree[n].derivedStyles, name);
if (style) {
return style;
}
}
}
return null;
2010-06-25 12:39:10 +02:00
}
2010-11-03 15:39:05 +01:00
/**
* @param {!string} stylename
* @param {!Object} stylesmap
* @param {!Object} stylestree
* @return {undefined}
*/
2010-11-03 00:01:21 +01:00
function addStyleToStyleTree(stylename, stylesmap, stylestree) {
var style = stylesmap[stylename], parentname, parentstyle;
if (!style) {
return;
}
parentname = style.getAttributeNS(stylens, 'parent-style-name');
parentstyle = null;
if (parentname) {
parentstyle = findStyle(stylestree, parentname);
if (!parentstyle && stylesmap[parentname]) {
// parent style has not been handled yet, do that now
addStyleToStyleTree(parentname, stylesmap, stylestree);
parentstyle = stylesmap[parentname];
stylesmap[parentname] = null;
}
}
if (parentstyle) {
if (!parentstyle.derivedStyles) {
parentstyle.derivedStyles = {};
}
parentstyle.derivedStyles[stylename] = style;
} else {
// no parent so add the root
2011-09-29 13:57:04 +02:00
stylestree[stylename] = style;
2010-11-03 00:01:21 +01:00
}
}
2010-11-03 15:39:05 +01:00
/**
* @param {!Object} stylesmap
* @param {!Object} stylestree
* @return {undefined}
*/
2010-11-03 00:01:21 +01:00
function addStyleMapToStyleTree(stylesmap, stylestree) {
var name;
for (name in stylesmap) {
if (stylesmap.hasOwnProperty(name)) {
addStyleToStyleTree(name, stylesmap, stylestree);
stylesmap[name] = null;
}
}
}
2010-11-03 15:39:05 +01:00
/**
* @param {!string} family
* @param {!string} name
* @return {?string}
*/
2010-11-03 00:01:21 +01:00
function createSelector(family, name) {
var prefix = familynamespaceprefixes[family],
namepart,
selector;
2010-11-03 00:01:21 +01:00
if (prefix === null) {
return null;
}
// If there is no name, it is a default style, in which case style-name shall be used without a value
if (name) {
namepart = '[' + prefix + '|style-name="' + name + '"]';
} else {
namepart = '[' + prefix + '|style-name]';
}
2010-11-03 00:01:21 +01:00
if (prefix === 'presentation') {
prefix = 'draw';
if (name) {
namepart = '[presentation|style-name="' + name + '"]';
} else {
namepart = '[presentation|style-name]';
}
2010-11-03 00:01:21 +01:00
}
2012-04-27 09:46:01 +02:00
selector = prefix + '|' + familytagnames[family].join(
namepart + ',' + prefix + '|'
) + namepart;
return selector;
2010-11-03 00:01:21 +01:00
}
2010-11-03 15:39:05 +01:00
/**
* @param {!string} family
* @param {!string} name
* @param {!Element} node
* @return {!Array}
*/
2010-11-03 00:01:21 +01:00
function getSelectors(family, name, node) {
var selectors = [], n, ss, s;
selectors.push(createSelector(family, name));
for (n in node.derivedStyles) {
if (node.derivedStyles.hasOwnProperty(n)) {
ss = getSelectors(family, n, node.derivedStyles[n]);
for (s in ss) {
if (ss.hasOwnProperty(s)) {
selectors.push(ss[s]);
}
}
}
}
return selectors;
}
2010-11-03 15:39:05 +01:00
/**
* @param {?Element} node
* @param {!string} ns
* @param {!string} name
* @return {?Element}
*/
2010-11-03 00:01:21 +01:00
function getDirectChild(node, ns, name) {
if (!node) {
return null;
}
2011-02-23 22:46:26 +01:00
var c = node.firstChild, e;
2010-11-03 00:01:21 +01:00
while (c) {
if (c.namespaceURI === ns && c.localName === name) {
2011-02-23 22:46:26 +01:00
e = /**@type{Element}*/(c);
return e;
2010-11-03 00:01:21 +01:00
}
c = c.nextSibling;
2010-06-25 10:08:51 -07:00
}
2010-11-03 15:39:05 +01:00
return null;
}
2010-11-03 15:39:05 +01:00
/**
* @param {!Element} props
* @param {!Object} mapping
* @return {!string}
*/
2010-11-03 00:01:21 +01:00
function applySimpleMapping(props, mapping) {
var rule = '', r, value;
for (r in mapping) {
if (mapping.hasOwnProperty(r)) {
r = mapping[r];
value = props.getAttributeNS(r[0], r[1]);
if (value) {
rule += r[2] + ':' + value + ';';
}
}
}
return rule;
}
2013-06-14 18:56:33 +02:00
/**
* Returns the font size attribute value from the text properties of a style node
* @param {!Node} styleNode
* @return {?{value: !number, unit: !string}}
2013-06-14 18:56:33 +02:00
*/
function getFontSize(styleNode) {
var props = getDirectChild(/**@type{Element}*/(styleNode), stylens, 'text-properties');
if (props) {
2013-06-19 17:46:23 +02:00
return utils.parseLength(props.getAttributeNS(fons, 'font-size'));
2013-06-14 18:56:33 +02:00
}
return null;
2013-06-14 18:56:33 +02:00
}
/*
* Returns the parent style node of a given style node
* @param {!Node} styleNode
* @return {Node}
*/
function getParentStyleNode(styleNode) {
var parentStyleName = '',
parentStyleFamily = '',
parentStyleNode = null,
xp;
if (styleNode.localName === 'default-style') {
return null;
}
parentStyleName = styleNode.getAttributeNS(stylens, 'parent-style-name');
parentStyleFamily = styleNode.getAttributeNS(stylens, 'family');
if (parentStyleName) {
xp = "//style:*[@style:name='" + parentStyleName + "'][@style:family='" + parentStyleFamily + "']";
} else {
xp = "//style:default-style[@style:family='" + parentStyleFamily + "']";
}
parentStyleNode = xpath.getODFElementsWithXPath(/**@type{!Element}*/(odfRoot), xp, odf.Namespaces.resolvePrefix)[0];
return parentStyleNode;
}
2010-11-03 15:39:05 +01:00
/**
* @param {!Element} props
* @return {!string}
*/
2010-11-03 00:01:21 +01:00
function getTextProperties(props) {
2013-06-14 18:56:33 +02:00
var rule = '', fontName, fontSize, value, textDecoration = '',
fontSizeRule = '',
sizeMultiplier = 1,
parentStyle;
2010-11-03 00:01:21 +01:00
rule += applySimpleMapping(props, textPropertySimpleMapping);
2010-11-03 00:01:21 +01:00
value = props.getAttributeNS(stylens, 'text-underline-style');
if (value === 'solid') {
textDecoration += ' underline';
2010-11-03 00:01:21 +01:00
}
value = props.getAttributeNS(stylens, 'text-line-through-style');
if (value === 'solid') {
textDecoration += ' line-through';
}
2013-06-19 16:47:14 +02:00
2013-02-18 23:58:55 +05:30
if (textDecoration.length) {
textDecoration = 'text-decoration:' + textDecoration + ';';
rule += textDecoration;
}
2013-06-14 18:56:33 +02:00
fontName = props.getAttributeNS(stylens, 'font-name')
|| props.getAttributeNS(fons, 'font-family');
if (fontName) {
value = fontFaceDeclsMap[fontName];
2013-06-14 18:56:33 +02:00
rule += 'font-family: ' + (value || fontName) + ', sans-serif;';
2010-11-03 00:01:21 +01:00
}
2013-06-14 18:56:33 +02:00
parentStyle = props.parentNode;
fontSize = getFontSize(/**@type{!Node}*/(parentStyle));
// This is actually the font size of the current style.
if (!fontSize) {
return rule;
}
while (parentStyle) {
fontSize = getFontSize(/**@type{!Node}*/(parentStyle));
if (fontSize) {
// If the current style's font size is a non-% value, then apply the multiplier to get the child style (with the %)'s
// actual font size. And now we can stop crawling up the style ancestry since we have a concrete font size.
if (fontSize.unit !== '%') {
fontSizeRule = 'font-size: ' + (fontSize.value * sizeMultiplier) + fontSize.unit + ';';
2013-06-14 18:56:33 +02:00
break;
} else {
// If we got a % font size for the current style, then update the multiplier with it's 'normalized' multiplier
sizeMultiplier *= (fontSize.value / 100);
2013-06-14 18:56:33 +02:00
}
}
// Crawl up the style ancestry
parentStyle = getParentStyleNode(parentStyle);
}
// If there was nothing in the ancestry that specified a concrete font size, just apply the multiplier onto the page's default font size.
if (!fontSizeRule) {
2013-06-14 18:56:33 +02:00
fontSizeRule = 'font-size: ' + parseFloat(defaultFontSize) * sizeMultiplier + cssUnits.getUnits(defaultFontSize) + ';';
}
rule += fontSizeRule;
2010-11-03 00:01:21 +01:00
return rule;
}
2010-11-03 15:39:05 +01:00
/**
* @param {!Element} props
* @return {!string}
*/
2010-11-03 00:01:21 +01:00
function getParagraphProperties(props) {
2013-06-19 17:46:23 +02:00
var rule = '', imageProps, url, element, lineHeight;
2010-11-03 00:01:21 +01:00
rule += applySimpleMapping(props, paragraphPropertySimpleMapping);
imageProps = props.getElementsByTagNameNS(stylens, 'background-image');
if (imageProps.length > 0) {
url = imageProps.item(0).getAttributeNS(xlinkns, 'href');
if (url) {
rule += "background-image: url('odfkit:" + url + "');";
//rule += "background-repeat: repeat;"; //FIXME test
2010-11-03 15:39:05 +01:00
element = /**@type{!Element}*/(imageProps.item(0));
rule += applySimpleMapping(element, bgImageSimpleMapping);
2010-11-03 00:01:21 +01:00
}
}
2013-06-19 17:46:23 +02:00
lineHeight = props.getAttributeNS(fons, 'line-height');
if (lineHeight && lineHeight !== 'normal') {
lineHeight = utils.parseLength(lineHeight);
if (lineHeight.unit !== '%') {
rule += 'line-height: ' + lineHeight.value + ';';
} else {
2013-06-19 17:46:23 +02:00
rule += 'line-height: ' + lineHeight.value / 100 + ';';
}
}
2010-11-03 00:01:21 +01:00
return rule;
}
2013-06-19 11:24:53 +02:00
/**
* @param {!string} hex
* @return {?{ r: number, g: number, b: number}}
*/
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var result,
shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function (m, r, g, b) {
return r + r + g + g + b + b;
});
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function isNumber(n) {
return !isNaN(parseFloat(n));
}
/**
2010-11-03 15:39:05 +01:00
* @param {!Element} props
* @return {!string}
*/
2010-11-03 00:01:21 +01:00
function getGraphicProperties(props) {
var rule = '', alpha, bgcolor, fill;
2010-11-03 00:01:21 +01:00
rule += applySimpleMapping(props, graphicPropertySimpleMapping);
alpha = props.getAttributeNS(drawns, 'opacity');
fill = props.getAttributeNS(drawns, 'fill');
bgcolor = props.getAttributeNS(drawns, 'fill-color');
if (fill === 'solid' || fill === 'hatch') {
if (bgcolor && bgcolor !== 'none') {
alpha = isNumber(alpha) ? parseFloat(alpha) / 100 : 1;
bgcolor = hexToRgb(bgcolor);
2013-06-19 11:24:53 +02:00
if (bgcolor) {
rule += "background-color: rgba("
+ bgcolor.r + ","
+ bgcolor.g + ","
+ bgcolor.b + ","
+ alpha + ");";
}
} else {
rule += "background: none;";
}
} else if (fill === "none") {
rule += "background: none;";
}
2010-11-03 00:01:21 +01:00
return rule;
}
/**
* @param {!Element} props
* @return {!string}
*/
function getDrawingPageProperties(props) {
var rule = '';
rule += applySimpleMapping(props, graphicPropertySimpleMapping);
if (props.getAttributeNS(presentationns, 'background-visible') === 'true') {
rule += "background: none;";
}
return rule;
}
2010-11-03 15:39:05 +01:00
/**
* @param {!Element} props
* @return {!string}
*/
2010-11-03 00:01:21 +01:00
function getTableCellProperties(props) {
var rule = '';
rule += applySimpleMapping(props, tablecellPropertySimpleMapping);
return rule;
}
2012-04-27 09:46:01 +02:00
/**
* @param {!Element} props
* @return {!string}
*/
function getTableRowProperties(props) {
var rule = '';
rule += applySimpleMapping(props, tablerowPropertySimpleMapping);
return rule;
}
/**
* @param {!Element} props
* @return {!string}
*/
function getTableColumnProperties(props) {
var rule = '';
rule += applySimpleMapping(props, tablecolumnPropertySimpleMapping);
return rule;
}
/**
* @param {!Element} props
* @return {!string}
*/
function getTableProperties(props) {
var rule = '';
rule += applySimpleMapping(props, tablePropertySimpleMapping);
return rule;
}
2010-11-03 15:39:05 +01:00
/**
* @param {!StyleSheet} sheet
* @param {!string} family
* @param {!string} name
* @param {!Element} node
* @return {undefined}
*/
2011-04-19 21:46:03 +02:00
function addStyleRule(sheet, family, name, node) {
2010-11-03 00:01:21 +01:00
var selectors = getSelectors(family, name, node),
selector = selectors.join(','),
rule = '',
properties = getDirectChild(node, stylens, 'text-properties');
if (properties) {
rule += getTextProperties(properties);
}
properties = getDirectChild(node, stylens, 'paragraph-properties');
if (properties) {
rule += getParagraphProperties(properties);
}
properties = getDirectChild(node, stylens, 'graphic-properties');
if (properties) {
rule += getGraphicProperties(properties);
}
properties = getDirectChild(node, stylens, 'drawing-page-properties');
if (properties) {
rule += getDrawingPageProperties(properties);
}
2010-11-03 00:01:21 +01:00
properties = getDirectChild(node, stylens, 'table-cell-properties');
if (properties) {
rule += getTableCellProperties(properties);
}
2012-04-27 09:46:01 +02:00
properties = getDirectChild(node, stylens, 'table-row-properties');
if (properties) {
rule += getTableRowProperties(properties);
}
properties = getDirectChild(node, stylens, 'table-column-properties');
if (properties) {
rule += getTableColumnProperties(properties);
}
properties = getDirectChild(node, stylens, 'table-properties');
if (properties) {
rule += getTableProperties(properties);
}
if (family === "table") {
runtime.log(rule);
}
2010-11-03 00:01:21 +01:00
if (rule.length === 0) {
return;
}
rule = selector + '{' + rule + '}';
try {
2010-11-03 15:39:05 +01:00
sheet.insertRule(rule, sheet.cssRules.length);
2010-11-03 00:01:21 +01:00
} catch (e) {
throw e;
}
}
2011-04-19 21:46:03 +02:00
/**
* @param {!Element} node
* @return {!string}
*/
function getNumberRule(node) {
var style = node.getAttributeNS(stylens, "num-format"),
suffix = node.getAttributeNS(stylens, "num-suffix"),
prefix = node.getAttributeNS(stylens, "num-prefix"),
2011-06-03 21:40:42 +02:00
stylemap = {'1': 'decimal', 'a': 'lower-latin', 'A': 'upper-latin',
'i': 'lower-roman', 'I': 'upper-roman'},
content = prefix || "";
2012-07-11 18:50:36 +02:00
2011-07-13 10:00:16 +02:00
if (stylemap.hasOwnProperty(style)) {
2011-04-19 21:46:03 +02:00
content += " counter(list, " + stylemap[style] + ")";
} else if (style) {
content += "'" + style + "';";
} else {
content += " ''";
}
if (suffix) {
content += " '" + suffix + "'";
}
return "content: " + content + ";";
2011-04-19 21:46:03 +02:00
}
/**
* @param {!Element} node
* @return {!string}
*/
function getImageRule(node) {
var rule = "content: none;";
return rule;
}
/**
* @param {!Element} node
* @return {!string}
*/
function getBulletRule(node) {
var bulletChar = node.getAttributeNS(textns, "bullet-char");
2011-04-19 21:46:03 +02:00
return "content: '" + bulletChar + "';";
}
/**
* @param {!StyleSheet} sheet
* @param {!string} name
* @param {!Element} node
* @return {undefined}
*/
function addListStyleRule(sheet, name, node, itemrule) {
2012-07-11 18:50:36 +02:00
var selector = 'text|list[text|style-name="' + name + '"]',
2011-04-19 21:46:03 +02:00
level = node.getAttributeNS(textns, "level"),
2012-07-11 18:50:36 +02:00
itemSelector,
listItemRule,
2013-06-17 12:45:56 +10:00
listLevelProps = utils.getFirstNonWhitespaceChild(node), // {Element}
listLevelLabelAlign = utils.getFirstNonWhitespaceChild(listLevelProps), // {Element}
2012-07-11 18:50:36 +02:00
labelAlignAttr,
bulletIndent,
listIndent,
bulletWidth,
rule;
2012-07-11 18:50:36 +02:00
if (listLevelLabelAlign) {
labelAlignAttr = listLevelLabelAlign.attributes;
bulletIndent = labelAlignAttr["fo:text-indent"] ? labelAlignAttr["fo:text-indent"].value : undefined;
listIndent = labelAlignAttr["fo:margin-left"] ? labelAlignAttr["fo:margin-left"].value : undefined;
2012-07-11 18:50:36 +02:00
}
// If no values are specified, use default values
if (!bulletIndent) {
bulletIndent = "-0.6cm";
}
// bulletWidth is the negative of bulletIndent
// Obtain this my stripping the fist character
2012-08-25 00:37:25 +02:00
if (bulletIndent.charAt(0) === '-') {
2012-07-11 18:50:36 +02:00
bulletWidth = bulletIndent.substring(1);
2012-08-25 00:37:25 +02:00
} else {
2012-07-11 18:50:36 +02:00
bulletWidth = "-" + bulletIndent;
}
2011-04-19 21:46:03 +02:00
level = level && parseInt(level, 10);
while (level > 1) {
2012-07-11 18:50:36 +02:00
selector += ' > text|list-item > text|list';
2011-04-19 21:46:03 +02:00
level -= 1;
}
2012-07-11 18:50:36 +02:00
itemSelector = selector;
itemSelector += ' > text|list-item > *:not(text|list):first-child';
2012-08-25 00:37:25 +02:00
if (listIndent !== undefined) {
listItemRule = itemSelector + '{margin-left:' + listIndent + ';}';
sheet.insertRule(listItemRule, sheet.cssRules.length);
}
2012-07-11 18:50:36 +02:00
// insert a block before every immediate child of the list-item, except for lists
selector += ' > text|list-item > *:not(text|list):first-child:before';
2011-04-19 21:46:03 +02:00
rule = itemrule;
2012-07-11 18:50:36 +02:00
rule = selector + '{' + rule + ';';
rule += 'counter-increment:list;';
2012-08-25 00:37:25 +02:00
rule += 'margin-left:' + bulletIndent + ';';
2012-07-11 18:50:36 +02:00
rule += 'width:' + bulletWidth + ';';
rule += 'display:inline-block}';
2011-04-19 21:46:03 +02:00
try {
sheet.insertRule(rule, sheet.cssRules.length);
} catch (e) {
throw e;
}
}
/**
* @param {!StyleSheet} sheet
2013-06-12 16:45:33 +10:00
* @param {!Element} node
* @return {undefined}
*/
function addPageStyleRules(sheet, node) {
var rule = '', imageProps, url, element,
contentLayoutRule = '',
pageSizeRule = '',
height,
width,
props = node.getElementsByTagNameNS(stylens, 'page-layout-properties')[0],
masterStyles = props.parentNode.parentNode.parentNode.masterStyles,
masterPages,
masterStyleName = '',
i;
rule += applySimpleMapping(props, pageContentPropertySimpleMapping);
imageProps = props.getElementsByTagNameNS(stylens, 'background-image');
if (imageProps.length > 0) {
url = imageProps.item(0).getAttributeNS(xlinkns, 'href');
if (url) {
rule += "background-image: url('odfkit:" + url + "');";
//rule += "background-repeat: repeat;"; //FIXME test
element = /**@type{!Element}*/(imageProps.item(0));
rule += applySimpleMapping(element, bgImageSimpleMapping);
}
}
if (documentType === 'presentation') {
if (masterStyles) {
masterPages = masterStyles.getElementsByTagNameNS(stylens, 'master-page');
for (i = 0; i < masterPages.length; i += 1) {
// Generate CSS for all the pages that use the master page that use this page-layout
if (masterPages[i].getAttributeNS(stylens, 'page-layout-name') === props.parentNode.getAttributeNS(stylens, 'name')) {
masterStyleName = masterPages[i].getAttributeNS(stylens, 'name');
contentLayoutRule = 'draw|page[draw|master-page-name=' + masterStyleName + '] {' + rule + '}';
pageSizeRule = 'office|body, draw|page[draw|master-page-name=' + masterStyleName + '] {'
+ applySimpleMapping(props, pageSizePropertySimpleMapping)
+ ' }';
try {
sheet.insertRule(contentLayoutRule, sheet.cssRules.length);
sheet.insertRule(pageSizeRule, sheet.cssRules.length);
} catch (e1) {
throw e1;
}
}
}
}
} else if (documentType === 'text') {
contentLayoutRule = 'office|text {' + rule + '}';
rule = '';
// TODO: We want to use the simpleMapping for ODTs, but not until we have pagination.
// So till then, set only the width.
//rule += applySimpleMapping(props, pageSizePropertySimpleMapping);
pageSizeRule = 'office|body {'
+ 'width: ' + props.getAttributeNS(fons, 'page-width') + ';'
+ '}';
try {
sheet.insertRule(contentLayoutRule, sheet.cssRules.length);
sheet.insertRule(pageSizeRule, sheet.cssRules.length);
} catch (e2) {
throw e2;
}
}
}
/**
* @param {!Element} props
* @return {!string}
*/
function getPageSizeProperties(props) {
var rule = '';
rule += applySimpleMapping(props, pageSizePropertySimpleMapping);
return rule;
}
2013-06-19 16:47:14 +02:00
2011-04-19 21:46:03 +02:00
/**
* @param {!StyleSheet} sheet
* @param {!string} name
* @param {!Element} node
* @return {undefined}
*/
function addListStyleRules(sheet, name, node) {
var n = node.firstChild, e, itemrule;
2011-04-19 21:46:03 +02:00
while (n) {
if (n.namespaceURI === textns) {
e = /**@type{!Element}*/(n);
2011-04-19 21:46:03 +02:00
if (n.localName === "list-level-style-number") {
itemrule = getNumberRule(e);
addListStyleRule(sheet, name, e, itemrule);
2011-04-19 21:46:03 +02:00
} else if (n.localName === "list-level-style-image") {
itemrule = getImageRule(e);
addListStyleRule(sheet, name, e, itemrule);
2011-04-19 21:46:03 +02:00
} else if (n.localName === "list-level-style-bullet") {
itemrule = getBulletRule(e);
addListStyleRule(sheet, name, e, itemrule);
2011-04-19 21:46:03 +02:00
}
}
n = n.nextSibling;
}
}
/**
* @param {!StyleSheet} sheet
* @param {!string} family
* @param {!string} name
* @param {!Element} node
* @return {undefined}
*/
function addRule(sheet, family, name, node) {
if (family === "list") {
addListStyleRules(sheet, name, node);
} else if (family === "page") {
addPageStyleRules(sheet, node);
2011-04-19 21:46:03 +02:00
} else {
addStyleRule(sheet, family, name, node);
}
}
2010-11-03 15:39:05 +01:00
/**
* @param {!StyleSheet} sheet
* @param {!string} family
* @param {!string} name
* @param {!Element} node
* @return {undefined}
*/
2010-11-03 00:01:21 +01:00
function addRules(sheet, family, name, node) {
addRule(sheet, family, name, node);
var n;
for (n in node.derivedStyles) {
if (node.derivedStyles.hasOwnProperty(n)) {
addRules(sheet, family, n, node.derivedStyles[n]);
}
}
}
// css vs odf styles
// ODF styles occur in families. A family is a group of odf elements to
2011-02-23 22:46:26 +01:00
// which an element applies. ODF families can be mapped to a group of css
// elements
2010-11-03 00:01:21 +01:00
2010-11-03 15:39:05 +01:00
/**
2013-06-19 16:47:14 +02:00
* @param {!string} doctype
2010-11-03 15:39:05 +01:00
* @param {!StyleSheet} stylesheet
* @param {!Object.<string,string>} fontFaceMap
2010-11-03 15:39:05 +01:00
* @param {!Element} styles
* @param {!Element} autostyles
* @return {undefined}
2011-09-29 13:57:04 +02:00
*/
this.style2css = function (doctype, stylesheet, fontFaceMap, styles, autostyles) {
var doc, prefix, styletree, tree, name, rule, family,
2010-11-03 15:39:05 +01:00
stylenodes, styleautonodes;
// make stylesheet empty
while (stylesheet.cssRules.length) {
stylesheet.deleteRule(stylesheet.cssRules.length - 1);
}
doc = null;
if (styles) {
doc = styles.ownerDocument;
2013-06-14 18:56:33 +02:00
odfRoot = styles.parentNode;
}
if (autostyles) {
doc = autostyles.ownerDocument;
2013-06-14 18:56:33 +02:00
odfRoot = autostyles.parentNode;
}
2010-11-03 15:39:05 +01:00
if (!doc) {
return;
}
2013-06-14 18:56:33 +02:00
// add @namodfRoot espace rules
odf.Namespaces.forEachPrefix(function (prefix, ns) {
2013-04-17 16:22:52 +02:00
rule = '@namespace ' + prefix + ' url(' + ns + ');';
try {
stylesheet.insertRule(rule, stylesheet.cssRules.length);
} catch (e) {
// WebKit can throw an exception here, but it will have
// retained the namespace declarations anyway.
2010-11-03 00:01:21 +01:00
}
2013-04-17 16:22:52 +02:00
});
fontFaceDeclsMap = fontFaceMap;
documentType = doctype;
2013-06-14 18:56:33 +02:00
defaultFontSize = window.getComputedStyle(document.body, null).getPropertyValue('font-size') || '12pt';
2010-11-03 15:39:05 +01:00
// add the various styles
stylenodes = getStyleMap(styles);
styleautonodes = getStyleMap(autostyles);
2011-09-29 13:57:04 +02:00
styletree = {};
2010-11-03 15:39:05 +01:00
for (family in familynamespaceprefixes) {
if (familynamespaceprefixes.hasOwnProperty(family)) {
tree = styletree[family] = {};
addStyleMapToStyleTree(stylenodes[family], tree);
addStyleMapToStyleTree(styleautonodes[family], tree);
2011-09-29 13:57:04 +02:00
2010-11-03 15:39:05 +01:00
for (name in tree) {
if (tree.hasOwnProperty(name)) {
addRules(stylesheet, family, name, tree[name]);
}
}
}
}
};
};