diff --git a/webodf/lib/core/CSSUnits.js b/webodf/lib/core/CSSUnits.js index 9f176117..1bce4189 100644 --- a/webodf/lib/core/CSSUnits.js +++ b/webodf/lib/core/CSSUnits.js @@ -78,4 +78,9 @@ core.CSSUnits = function CSSUnits() { return newMeasure.toString(); }; + + this.getUnits = function (measure) { + return measure.substr(measure.length - 2, measure.length); + }; + }; diff --git a/webodf/lib/odf/Style2CSS.js b/webodf/lib/odf/Style2CSS.js index 21b3ac5d..074653dd 100644 --- a/webodf/lib/odf/Style2CSS.js +++ b/webodf/lib/odf/Style2CSS.js @@ -33,10 +33,12 @@ * @source: http://gitorious.org/webodf/webodf/ */ -/*global odf, runtime*/ +/*global odf, runtime, xmldom, core, document, window*/ runtime.loadClass("odf.Namespaces"); runtime.loadClass("odf.OdfUtils"); +runtime.loadClass("xmldom.XPath"); +runtime.loadClass("core.CSSUnits"); /** * @constructor @@ -113,8 +115,7 @@ odf.Style2CSS = function Style2CSS() { // this sets the element background, not just the text background [ fons, 'background-color', 'background-color' ], [ fons, 'font-weight', 'font-weight' ], - [ fons, 'font-style', 'font-style' ], - [ fons, 'font-size', 'font-size' ] + [ fons, 'font-style', 'font-style' ] ], /**@const@type{!Array.>}*/ @@ -213,7 +214,11 @@ odf.Style2CSS = function Style2CSS() { /**@type{!Object.}*/ fontFaceDeclsMap = {}, utils = new odf.OdfUtils(), - documentType; + documentType, + odfRoot, + defaultFontSize, + xpath = new xmldom.XPath(), + cssUnits = new core.CSSUnits(); // helper functions /** @@ -426,12 +431,54 @@ odf.Style2CSS = function Style2CSS() { return rule; } + /** + * Returns the font size attribute value from the text properties of a style node + * @param {!Node} styleNode + * @return {string} + */ + function getFontSize(styleNode) { + var props = getDirectChild(/**@type{Element}*/(styleNode), stylens, 'text-properties'); + if (props) { + return props.getAttributeNS(fons, 'font-size'); + } + } + + /* + * 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; + } /** * @param {!Element} props * @return {!string} */ function getTextProperties(props) { - var rule = '', fontName, value, textDecoration = ''; + var rule = '', fontName, fontSize, value, textDecoration = '', + fontSizeRule = '', + sizeMultiplier = 1, + parentStyle; + rule += applySimpleMapping(props, textPropertySimpleMapping); value = props.getAttributeNS(stylens, 'text-underline-style'); @@ -448,11 +495,44 @@ odf.Style2CSS = function Style2CSS() { rule += textDecoration; } - fontName = props.getAttributeNS(stylens, 'font-name'); + fontName = props.getAttributeNS(stylens, 'font-name') + || props.getAttributeNS(fons, 'font-family'); if (fontName) { value = fontFaceDeclsMap[fontName]; - rule += 'font-family: ' + (value || fontName) + ';'; + rule += 'font-family: ' + (value || fontName) + ', sans-serif;'; } + + 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.indexOf('%') === -1) { + fontSize = parseFloat(fontSize) * sizeMultiplier + cssUnits.getUnits(fontSize); + fontSizeRule = 'font-size: ' + fontSize + ';'; + break; + } else { + // If we got a % font size for the current style, then update the multiplier with it's 'normalized' multiplier + sizeMultiplier *= (parseFloat(fontSize) / 100); + } + } + // 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 (!fontSize) { + fontSizeRule = 'font-size: ' + parseFloat(defaultFontSize) * sizeMultiplier + cssUnits.getUnits(defaultFontSize) + ';'; + } + + rule += fontSizeRule; return rule; } /** @@ -905,15 +985,18 @@ odf.Style2CSS = function Style2CSS() { doc = null; if (styles) { doc = styles.ownerDocument; + odfRoot = styles.parentNode; } if (autostyles) { doc = autostyles.ownerDocument; + odfRoot = autostyles.parentNode; } if (!doc) { return; } - // add @namespace rules - odf.Namespaces.forEachPrefix(function(prefix, ns) { + + // add @namodfRoot espace rules + odf.Namespaces.forEachPrefix(function (prefix, ns) { rule = '@namespace ' + prefix + ' url(' + ns + ');'; try { stylesheet.insertRule(rule, stylesheet.cssRules.length); @@ -925,6 +1008,7 @@ odf.Style2CSS = function Style2CSS() { fontFaceDeclsMap = fontFaceMap; documentType = doctype; + defaultFontSize = window.getComputedStyle(document.body, null).getPropertyValue('font-size') || '12pt'; // add the various styles stylenodes = getStyleMap(styles);