CSSStyle *CSSSheetFlattenedStyle(CSSSheet *sheet, CSSStyle *orig) { // FIXME: Need tests for parent cycles CSSStyle *ancestor = orig; CSSStyle *result = CSSStyleNew("temp"); DFHashTable *visited = DFHashTableNew((DFCopyFunction)xstrdup,(DFFreeFunction)free); const char **allSuffixes = NULL; while (1) { free(allSuffixes); allSuffixes = CSSStyleCopySuffixes(ancestor); for (int suffixIndex = 0; allSuffixes[suffixIndex]; suffixIndex++) { const char *suffix = allSuffixes[suffixIndex]; CSSProperties *origProperties = CSSStyleRuleForSuffix(ancestor,suffix); CSSProperties *collapsedProperties = CSSStyleRuleForSuffix(result,suffix); const char **allNames = CSSPropertiesCopyNames(origProperties); for (int nameIndex = 0; allNames[nameIndex]; nameIndex++) { const char *name = allNames[nameIndex]; if (!strcmp(name,"-uxwrite-default") && (ancestor != orig)) continue; if (CSSGet(collapsedProperties,name) == NULL) CSSPut(collapsedProperties,name,CSSGet(origProperties,name)); } free(allNames); } DFHashTableAdd(visited,ancestor->selector,""); ancestor = CSSSheetGetStyleParent(sheet,ancestor); if ((ancestor == NULL) || (DFHashTableLookup(visited,ancestor->selector) != NULL)) break; } free(allSuffixes); DFHashTableRelease(visited); return result; }
static ImageInfo *getImageInfoObject(DFNode *concrete) { DFNode *shape = DFChildWithTag(concrete,VML_SHAPE); DFNode *imageData = DFChildWithTag(shape,VML_IMAGEDATA); const char *cssText = DFGetAttribute(shape,NULL_STYLE); const char *rId = DFGetAttribute(imageData,OREL_ID); if ((shape == NULL) || (imageData == NULL) || (cssText == NULL) || (rId == NULL)) return NULL; CSSProperties *imgProperties = CSSPropertiesNewWithString(cssText); CSSLength width = CSSLengthFromString(CSSGet(imgProperties,"width")); CSSLength height = CSSLengthFromString(CSSGet(imgProperties,"height")); CSSPropertiesRelease(imgProperties); if (!CSSLengthIsValid(width) || !CSSLengthIsAbsolute(width)) return NULL; if (!CSSLengthIsValid(height) || !CSSLengthIsAbsolute(height)) return NULL; double widthPts = convertBetweenUnits(width.value,width.units,UnitsPt); double heightPts = convertBetweenUnits(height.value,height.units,UnitsPt); ImageInfo *info = ImageInfoNew(rId,widthPts,heightPts); DFNode *oleObject = DFChildWithTag(concrete,MSOFFICE_OLEOBJECT); info->progId = DFGetAttribute(oleObject,NULL_PROGID); return info; }
static void fixParagraphSpacing(CSSStyle *style, DFNode *element) { DFNode *pPr = DFChildWithTag(element,WORD_PPR); DFNode *spacing = DFChildWithTag(pPr,WORD_SPACING); const char *beforeAuto = DFGetAttribute(spacing,WORD_BEFOREAUTOSPACING); const char *afterAuto = DFGetAttribute(spacing,WORD_AFTERAUTOSPACING); CSSProperties *rule = CSSStyleRule(style); if (beforeAuto != NULL) { if (Word_parseOnOff(beforeAuto)) CSSPut(rule,"margin-top",NULL); else CSSPut(rule,"margin-top","0"); } else { if (CSSGet(rule,"margin-top") == NULL) CSSPut(rule,"margin-top","0"); } int isBeforeAuto = ((beforeAuto != NULL) && Word_parseOnOff(beforeAuto)); int isAfterAuto = ((afterAuto != NULL) && Word_parseOnOff(afterAuto)); if (isBeforeAuto) CSSPut(rule,"margin-top",NULL); else if (CSSGet(rule,"margin-top") == NULL) CSSPut(rule,"margin-top","0"); if (isAfterAuto) CSSPut(rule,"margin-bottom",NULL); else if (CSSGet(rule,"margin-bottom") == NULL) CSSPut(rule,"margin-bottom","0"); }
CSSSheet *WordParseStyles(WordConverter *converter) { CSSSheet *styleSheet = CSSSheetNew(); CSSStyle *bodyStyle = CSSSheetLookupElement(styleSheet,"body",NULL,1,0); CSSPut(CSSStyleRule(bodyStyle),"counter-reset","h1 h2 h3 h4 h5 h6 figure table"); parseBody(converter,styleSheet); if (converter->package->styles == NULL) return styleSheet;; DFNode *root = converter->package->styles->root; if (root == NULL) return styleSheet; if (root->tag != WORD_STYLES) return styleSheet;; WordSheet *sheet = converter->styles; const char **allIdents = WordSheetCopyIdents(sheet); for (int i = 0; allIdents[i]; i++) { WordStyle *wordStyle = WordSheetStyleForIdent(sheet,allIdents[i]); if ((wordStyle->selector == NULL) || WordStyleIsProtected(wordStyle)) continue; CSSStyle *style = CSSSheetLookupSelector(styleSheet,wordStyle->selector,1,0); styleParse(wordStyle,converter,style); const char *defaultVal = DFGetAttribute(wordStyle->element,WORD_DEFAULT); if ((defaultVal != NULL) && Word_parseOnOff(defaultVal)) { StyleFamily family = WordStyleFamilyForSelector(style->selector); CSSSheetSetDefaultStyle(styleSheet,style,family); CSSSetDefault(CSSStyleRule(style),1); if (family == StyleFamilyParagraph) fixParagraphSpacing(style,wordStyle->element); } } free(allIdents); DFNode *docDefaults = DFChildWithTag(root,WORD_DOCDEFAULTS); DFNode *pPrDefault = DFChildWithTag(docDefaults,WORD_PPRDEFAULT); DFNode *pPr = DFChildWithTag(pPrDefault,WORD_PPR); if (pPr != NULL) { CSSStyle *body = CSSSheetLookupElement(styleSheet,"body",NULL,1,0); const char *styleId = NULL; WordGetPPr(pPr,CSSStyleRule(body),&styleId,converter->mainSection); } // Special case for figure style: set left and right margin to auto, if not already set CSSStyle *figure = CSSSheetLookupElement(styleSheet,"figure",NULL,0,0); if (figure != NULL) { if (CSSGet(CSSStyleRule(figure),"margin-left") == NULL) CSSPut(CSSStyleRule(figure),"margin-left","auto"); if (CSSGet(CSSStyleRule(figure),"margin-right") == NULL) CSSPut(CSSStyleRule(figure),"margin-right","auto"); } return styleSheet; }
static void updateFromRawCSSRules(CSSSheet *sheet, DFHashTable *rules) { // FIXME: Handle class names containing escape sequences DFHashTableRelease(sheet->_styles); sheet->_styles = DFHashTableNew((DFCopyFunction)CSSStyleRetain,(DFFreeFunction)CSSStyleRelease); const char **sortedSelectors = DFHashTableCopyKeys(rules); DFSortStringsCaseInsensitive(sortedSelectors); for (int selIndex = 0; sortedSelectors[selIndex]; selIndex++) { const char *constSelector = sortedSelectors[selIndex]; // Treat any selectors specifying the class name only as paragraph styles char *selector; if (!strncmp(constSelector,".",1)) selector = DFFormatString("p%s",constSelector); // FIXME: Not covered by tests else selector = xstrdup(constSelector); DFHashTable *raw = DFHashTableLookup(rules,constSelector); char *baseId = NULL; char *suffix = NULL; CSSParseSelector(selector,&baseId,&suffix); CSSStyle *style = CSSSheetLookupSelector(sheet,baseId,0,0); if (style == NULL) { style = CSSStyleNew(baseId); CSSSheetAddStyle(sheet,style); CSSStyleRelease(style); } CSSProperties *properties = CSSStyleRuleForSuffix(style,suffix); CSSProperties *expanded = CSSPropertiesNewWithRaw(raw); const char **allNames = CSSPropertiesCopyNames(expanded); for (int nameIndex = 0; allNames[nameIndex]; nameIndex++) { const char *name = allNames[nameIndex]; CSSPut(properties,name,CSSGet(expanded,name)); } free(allNames); if (!strcmp(suffix,"")) { const char *defaultVal = CSSGet(properties,"-uxwrite-default"); if ((defaultVal != NULL) && DFStringEqualsCI(defaultVal,"true")) CSSSheetSetDefaultStyle(sheet,style,StyleFamilyFromHTMLTag(style->tag)); } CSSPropertiesRelease(expanded); free(baseId); free(suffix); free(selector); } free(sortedSelectors); removeRedundantProperties(sheet); }
static void WordPutNumPr(DFNode *concrete, CSSProperties *newp) { DFNode *children[PREDEFINED_TAG_COUNT]; childrenToArray(concrete,children); CSSProperties *oldp = CSSPropertiesNew(); WordGetNumPr(concrete,oldp); if (!DFStringEquals(CSSGet(oldp,"-word-numId"),CSSGet(newp,"-word-numId")) || !DFStringEquals(CSSGet(oldp,"-word-ilvl"),CSSGet(newp,"-word-ilvl"))) { if (CSSGet(newp,"-word-numId") != NULL) { children[WORD_NUMID] = DFCreateElement(concrete->doc,WORD_NUMID); DFSetAttribute(children[WORD_NUMID],WORD_VAL,CSSGet(newp,"-word-numId")); if (CSSGet(newp,"-word-ilvl") != NULL) { children[WORD_ILVL] = DFCreateElement(concrete->doc,WORD_ILVL); DFSetAttribute(children[WORD_ILVL],WORD_VAL,CSSGet(newp,"-word-ilvl")); } else { children[WORD_ILVL] = NULL; } } else { children[WORD_NUMID] = NULL; children[WORD_ILVL] = NULL; } } replaceChildrenFromArray(concrete,children,WordNumPr_Children); CSSPropertiesRelease(oldp); }
// FIXME: make private to this file double listDesiredIndent(WordConverter *conv, const char *numId, const char *ilvl) { CSSProperties *properties = CSSPropertiesNew(); listProperties(conv,numId,ilvl,properties); double result = 0.0; if (CSSGet(properties,"margin-left") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"margin-left")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) result += length.value; } CSSPropertiesRelease(properties); return result; }
char *CSSSheetCopyText(CSSSheet *sheet) { DFBuffer *result = DFBufferNew(); const char **allSelectors = CSSSheetCopySelectors(sheet); DFSortStringsCaseInsensitive(allSelectors); for (int selIndex = 0; allSelectors[selIndex]; selIndex++) { CSSStyle *style = CSSSheetLookupSelector(sheet,allSelectors[selIndex],0,0); DFBufferFormat(result,"%s\n",style->selector); const char **sortedSuffixes = CSSStyleCopySuffixes(style); DFSortStringsCaseInsensitive(sortedSuffixes); for (int suffixIndex = 0; sortedSuffixes[suffixIndex]; suffixIndex++) { const char *suffix = sortedSuffixes[suffixIndex]; char *quotedSuffix = DFQuote(suffix); DFBufferFormat(result," %s\n",quotedSuffix); free(quotedSuffix); CSSProperties *properties = CSSStyleRuleForSuffix(style,suffix); const char **sortedNames = CSSPropertiesCopyNames(properties); DFSortStringsCaseInsensitive(sortedNames); for (int nameIndex = 0; sortedNames[nameIndex]; nameIndex++) { const char *name = sortedNames[nameIndex]; const char *value = CSSGet(properties,name); DFBufferFormat(result," %s = %s\n",name,value); } free(sortedNames); } free(sortedSuffixes); } free(allSelectors); char *str = xstrdup(result->data); DFBufferRelease(result); return str; }
static void removeRedundantProperties(CSSSheet *sheet) { // Remove any properties set on a style that have the same value as the corresponding property // on the parent style. This is necessary because CSS doesn't support style inheritance (in // the sense of Word & ODF's styles), so when we save out a HTML file, every style has all // properties of its ancestors. After reading in a HTML file for the purposes of updating the // original Word or ODF style, we don't want these extra property settings to remain, so that // we can avoid adding spurious extra redundant property settings to the original file. breakCycles(sheet); const char **sortedSelectors = reverseTopologicalSortedSelectors(sheet); for (size_t selIndex = 0; sortedSelectors[selIndex]; selIndex++) { const char *selector = sortedSelectors[selIndex]; CSSStyle *child = CSSSheetLookupSelector(sheet,selector,0,0); CSSStyle *parent = CSSSheetGetStyleParent(sheet,child); if (parent == NULL) continue; const char **allSuffixes = CSSStyleCopySuffixes(child); for (int suffixIndex = 0; allSuffixes[suffixIndex]; suffixIndex++) { const char *suffix = allSuffixes[suffixIndex]; int isCell = !strcmp(suffix," > * > tr > td"); CSSProperties *childProperties = CSSStyleRuleForSuffix(child,suffix); CSSProperties *parentProperties = CSSStyleRuleForSuffix(parent,suffix); const char **allNames = CSSPropertiesCopyNames(childProperties); for (int nameIndex = 0; allNames[nameIndex]; nameIndex++) { const char *name = allNames[nameIndex]; // In docx's styles.xml, the tblCellMar values in table styles are not inherited // (this seems like a bug in word, as isn't inconsistent with all other properties) // So keep these ones. if (isCell && DFStringHasPrefix(name,"padding-")) continue; const char *childVal = CSSGet(childProperties,name); const char *parentVal = CSSGet(parentProperties,name); if ((childVal != NULL) && (parentVal != NULL) && DFStringEquals(childVal,parentVal)) CSSPut(childProperties,name,NULL); } free(allNames); } free(allSuffixes); } free(sortedSelectors); }
void CSSPropertiesPrint(CSSProperties *properties, const char *indent) { const char **allNames = CSSPropertiesCopyNames(properties); DFSortStringsCaseInsensitive(allNames); for (int nameIndex = 0; allNames[nameIndex]; nameIndex++) { const char *name = allNames[nameIndex]; const char *value = CSSGet(properties,name); printf("%s%s = %s\n",indent,name,value); } free(allNames); }
static void Word_flattenList(WordConverter *word, DFNode *list, int ilvl, DFNode *parent, DFNode *nextSibling) { const char *type = (list->tag == HTML_OL) ? "decimal" : "disc"; const char *cssText = DFGetAttribute(list,HTML_STYLE); CSSProperties *properties = CSSPropertiesNewWithString(cssText); if (CSSGet(properties,"list-style-type") != NULL) type = CSSGet(properties,"list-style-type"); DFFormatAttribute(list,CONV_LISTNUM,"%u",list->seqNo); DFFormatAttribute(list,CONV_ILVL,"%d",ilvl); DFSetAttribute(list,CONV_LISTTYPE,type); for (DFNode *li = list->first; li != NULL; li = li->next) { DFNode *first = li->first; DFNode *liChildNext; for (DFNode *liChild = li->first; liChild != NULL; liChild = liChildNext) { liChildNext = liChild->next; if ((liChild->tag == HTML_UL) || (liChild->tag == HTML_OL)) { Word_flattenList(word,liChild,ilvl+1,parent,nextSibling); } else { if (liChild->tag == HTML_P) { DFFormatAttribute(liChild,CONV_LISTNUM,"%u",list->seqNo); DFFormatAttribute(liChild,CONV_ILVL,"%d",ilvl); DFSetAttribute(liChild,CONV_LISTTYPE,type); if (liChild == first) DFSetAttribute(liChild,CONV_LISTITEM,"true"); } DFInsertBefore(parent,liChild,nextSibling); Word_preProcessLists(word,liChild,ilvl); } } } DFRemoveNode(list); CSSPropertiesRelease(properties); }
static ListDimensions cssPropertiesIndent(CSSProperties *properties) { ListDimensions result; result.marginLeftPct = 0; result.textIndentPct = 0; result.totalPct = 0; if (CSSGet(properties,"margin-left") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"margin-left")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) result.marginLeftPct = length.value; } if (CSSGet(properties,"text-indent") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"text-indent")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) result.textIndentPct = length.value; } result.totalPct = result.marginLeftPct + result.textIndentPct; return result; }
int CSSSheetHeadingNumbering(CSSSheet *sheet) { const char **allSelectors = CSSSheetCopySelectors(sheet); for (int i = 0; allSelectors[i]; i++) { CSSStyle *style = CSSSheetLookupSelector(sheet,allSelectors[i],0,0); if (style->headingLevel == 0) continue; if (CSSGet(CSSStyleBefore(style),"content") == NULL) continue; DFArray *contentParts = CSSParseContent(CSSGet(CSSStyleBefore(style),"content")); for (size_t partIndex = 0; partIndex < DFArrayCount(contentParts); partIndex++) { ContentPart *part = DFArrayItemAt(contentParts,partIndex); if (part->type == ContentPartCounter) { free(allSelectors); DFArrayRelease(contentParts); return 1; } } DFArrayRelease(contentParts); } free(allSelectors); return 0; }
static CellPadding getPadding(CSSSheet *styleSheet, WordStyle *wordStyle, CSSProperties *cellProperties) { const char *paddingLeftStr = NULL; const char *paddingRightStr = NULL; CSSStyle *style; if ((wordStyle != NULL) && (wordStyle->selector != NULL)) { style = CSSSheetLookupSelector(styleSheet,wordStyle->selector,0,0); } else { style = CSSSheetDefaultStyleForFamily(styleSheet,StyleFamilyTable); } if (style != NULL) { paddingLeftStr = CSSGet(CSSStyleCell(style),"padding-left"); paddingRightStr = CSSGet(CSSStyleCell(style),"padding-right"); } if (CSSGet(cellProperties,"padding-left") != NULL) paddingLeftStr = CSSGet(cellProperties,"padding-left"); if (CSSGet(cellProperties,"padding-right") != NULL) paddingRightStr = CSSGet(cellProperties,"padding-right");; CellPadding padding; padding.leftPts = 0; padding.rightPts = 0; CSSLength paddingLeftLength = CSSLengthFromString(paddingLeftStr); if (CSSLengthIsValid(paddingLeftLength) && (paddingLeftLength.units == UnitsPt)) padding.leftPts = paddingLeftLength.value;; CSSLength paddingRightLength = CSSLengthFromString(paddingRightStr); if (CSSLengthIsValid(paddingRightLength) && (paddingRightLength.units == UnitsPt)) padding.rightPts = paddingRightLength.value;; return padding; }
int CSSGetLinethrough(CSSProperties *properties) { return (CSSGet(properties,"text-decoration-line-through") != NULL); }
static void adjustMarginLeft(DFNode *element, double adjustPct, int noTextIndent) { if ((element->tag != HTML_TABLE) && !HTML_isParagraphTag(element->tag)) return;; const char *cssText = DFGetAttribute(element,HTML_STYLE); CSSProperties *properties = CSSPropertiesNewWithString(cssText); double oldMarginLeft = 0; if (CSSGet(properties,"margin-left") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"margin-left")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) oldMarginLeft = length.value; if (CSSGet(properties,"width") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"width")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) { double oldWidth = length.value; double newWidth = oldWidth + oldMarginLeft; char buf[100]; CSSPut(properties,"width",DFFormatDoublePct(buf,100,newWidth)); } } } double oldTextIndent = 0; if (CSSGet(properties,"text-indent") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(properties,"text-indent")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) oldTextIndent = length.value; } double newMarginLeft = oldMarginLeft + adjustPct; double newTextIndent = oldTextIndent; if (newMarginLeft < 0) newMarginLeft = 0; if (fabs(newMarginLeft) >= 0.01) { char buf[100]; CSSPut(properties,"margin-left",DFFormatDoublePct(buf,100,newMarginLeft)); } else { CSSPut(properties,"margin-left",NULL); } if (noTextIndent) { CSSPut(properties,"text-indent",NULL); } else if (newTextIndent < -newMarginLeft) { // Don't allow negative text-indent newTextIndent = -newMarginLeft; if (fabs(newTextIndent) >= 0.01) { char buf[100]; CSSPut(properties,"text-indent",DFFormatDoublePct(buf,100,newTextIndent)); } else { CSSPut(properties,"text-indent",NULL); } } char *propertiesText = CSSPropertiesCopyDescription(properties); if (strlen(propertiesText) == 0) DFRemoveAttribute(element,HTML_STYLE); else DFSetAttribute(element,HTML_STYLE,propertiesText); free(propertiesText); CSSPropertiesRelease(properties); }
int CSSGetOverline(CSSProperties *properties) { return (CSSGet(properties,"text-decoration-overline") != NULL); }
int WordConverterUpdateFromHTML(WordConverter *converter, DFError **error) { if (converter->package->document == NULL) { DFErrorFormat(error,"document.xml not found"); return 0; } DFNode *wordDocument = DFChildWithTag(converter->package->document->docNode,WORD_DOCUMENT); if (wordDocument == NULL) { DFErrorFormat(error,"word:document not found"); return 0; } // FIXME: Need a more reliable way of telling whether this is a new document or not - it could be that the // document already existed (with styles set up) but did not have any content DFNode *wordBody = DFChildWithTag(wordDocument,WORD_BODY); int creating = ((wordBody == NULL) || (wordBody->first == NULL)); converter->haveFields = Word_simplifyFields(converter->package); Word_mergeRuns(converter->package); assert(converter->package->styles); CSSSheetRelease(converter->styleSheet); converter->styleSheet = CSSSheetNew(); char *cssText = HTMLCopyCSSText(converter->html); CSSSheetUpdateFromCSSText(converter->styleSheet,cssText); free(cssText); addMissingDefaultStyles(converter); CSSEnsureReferencedStylesPresent(converter->html,converter->styleSheet); if (creating) CSSSetHTMLDefaults(converter->styleSheet); CSSEnsureUnique(converter->styleSheet,converter->html,creating); CSSStyle *pageStyle = CSSSheetLookupElement(converter->styleSheet,"@page",NULL,0,0); CSSStyle *bodyStyle = CSSSheetLookupElement(converter->styleSheet,"body",NULL,1,0); CSSProperties *page = (pageStyle != NULL) ? CSSPropertiesRetain(CSSStyleRule(pageStyle)) : CSSPropertiesNew(); CSSProperties *body = (bodyStyle != NULL) ? CSSPropertiesRetain(CSSStyleRule(bodyStyle)) : CSSPropertiesNew(); if (CSSGet(body,"margin-left") == NULL) CSSPut(body,"margin-left","10%"); if (CSSGet(body,"margin-right") == NULL) CSSPut(body,"margin-right","10%"); if (CSSGet(body,"margin-top") == NULL) CSSPut(body,"margin-top","10%"); if (CSSGet(body,"margin-bottom") == NULL) CSSPut(body,"margin-bottom","10%"); WordSectionUpdateFromCSSPage(converter->mainSection,page,body); WordPutData put; put.conv = converter; put.numIdByHtmlId = DFHashTableNew((DFCopyFunction)strdup,free); put.htmlIdByNumId = DFHashTableNew((DFCopyFunction)strdup,free); // Make sure we update styles.xml from the CSS stylesheet *before* doing any conversion of the content, // since the latter requires a full mapping of CSS selectors to styleIds to be in place. WordUpdateStyles(converter,converter->styleSheet); Word_preProcessHTMLDoc(converter,converter->html); buildListMapFromHTML(&put,converter->html->docNode); updateListTypes(&put); WordBookmarks_removeCaptionBookmarks(converter->package->document); WordObjectsCollapseBookmarks(converter->objects); WordObjectsScan(converter->objects); Word_setupBookmarkLinks(&put); WordObjectsAnalyzeBookmarks(converter->objects,converter->styles); WordDocumentLens.put(&put,converter->html->root,wordDocument); WordObjectsExpandBookmarks(converter->objects); WordRemoveNbsps(converter->package->document); // Make sure the updateFields flag is set Word_updateSettings(converter->package,converter->haveFields); // Remove any abstract numbering definitions that are no longer referenced from concrete // numbering definitions WordNumberingRemoveUnusedAbstractNums(converter->numbering); // Remove any relationships and images that have been removed from the HTML file and no longer // have any other references pointing to them WordGarbageCollect(converter->package); CSSPropertiesRelease(page); CSSPropertiesRelease(body); DFHashTableRelease(put.numIdByHtmlId); DFHashTableRelease(put.htmlIdByNumId); return 1; }
static DFNode *WordTblGet(WordGetData *get, DFNode *concrete) { if (concrete->tag != WORD_TBL) return NULL;; DFNode *table = WordConverterCreateAbstract(get,HTML_TABLE,concrete); ConcreteInfo *cinfo = getConcreteInfo(get->conv,concrete); calcTotals(get,cinfo); const char *cellWidthType = cellWidthTypeForTable(concrete); int autoWidth = DFStringEquals(cellWidthType,"auto"); if ((CSSGet(cinfo->tableProperties,"width") == NULL) && autoWidth) { CSSPut(cinfo->tableProperties,"width",NULL); } else { // Determine column widths and table width if (cinfo->totalWidthPts > 0) { DFNode *colgroup = HTML_createColgroup(get->conv->html,cinfo->structure); DFAppendChild(table,colgroup); double tableWidthPct = 100.0; if (WordSectionContentWidth(get->conv->mainSection) > 0) { double contentWidthPts = WordSectionContentWidth(get->conv->mainSection)/20.0; tableWidthPct = 100.0*cinfo->totalWidthPts/contentWidthPts; if (CSSGet(cinfo->tableProperties,"width") == NULL) { char buf[100]; CSSPut(cinfo->tableProperties,"width",DFFormatDoublePct(buf,100,tableWidthPct)); } } } if (CSSGet(cinfo->tableProperties,"width") == NULL) CSSPut(cinfo->tableProperties,"width","100%"); } DFHashTable *collapsed = CSSCollapseProperties(cinfo->tableProperties); char *styleValue = CSSSerializeProperties(collapsed); DFHashTableRelease(collapsed); if (strlen(styleValue) > 0) DFSetAttribute(table,HTML_STYLE,styleValue); free(styleValue); if ((cinfo->style != NULL) && (cinfo->style->selector != NULL)) { char *className = CSSSelectorCopyClassName(cinfo->style->selector); DFSetAttribute(table,HTML_CLASS,className); free(className); } else { CSSStyle *defaultStyle = CSSSheetDefaultStyleForFamily(get->conv->styleSheet,StyleFamilyTable); if (defaultStyle != NULL) DFSetAttribute(table,HTML_CLASS,defaultStyle->className); } // Create rows and cells int row = 0; for (DFNode *tblChild = concrete->first; tblChild != NULL; tblChild = tblChild->next) { if (tblChild->tag != WORD_TR) continue; DFNode *tr = WordConverterCreateAbstract(get,HTML_TR,tblChild); DFAppendChild(table,tr); unsigned int col = 0; while (col < cinfo->structure->cols) { DFCell *cell = DFTableGetCell(cinfo->structure,row,col); if (cell == NULL) { DFNode *td = DFCreateElement(get->conv->html,HTML_TD); DFAppendChild(tr,td); col++; continue; } if (row == cell->row) { DFNode *td = WordTcGet(get,cell->element); DFAppendChild(tr,td); if (cell->colSpan != 1) DFFormatAttribute(td,HTML_COLSPAN,"%d",cell->colSpan); if (cell->rowSpan != 1) DFFormatAttribute(td,HTML_ROWSPAN,"%d",cell->rowSpan); } col += cell->colSpan; } row++; } ConcreteInfoFree(cinfo); return table; }
static void WordPutSectPr(DFNode *concrete, CSSSheet *styleSheet, WordSection *section) { // Note: The sectPr element can potentially contain one or more headerReference or // footerReference elements at the start, before the elements in WordSectPr_Children. // So the straight childrenToArray/replaceChildrenFromArray method used elsewhere doesn't // work here - we need to make sure these other elements are maintained DFNode *children[PREDEFINED_TAG_COUNT]; childrenToArray(concrete,children); CSSProperties *oldBody = CSSPropertiesNew(); CSSProperties *oldPage = CSSPropertiesNew(); WordGetSectPr(concrete,oldBody,oldPage,section); CSSProperties *newBody = CSSSheetBodyProperties(styleSheet); CSSProperties *newPage = CSSSheetPageProperties(styleSheet); // Page size if (children[WORD_PGSZ] == NULL) children[WORD_PGSZ] = DFCreateElement(concrete->doc,WORD_PGSZ); int updatePageSize = 0; const char *widthStr = DFGetAttribute(children[WORD_PGSZ],WORD_W); const char *heightStr = DFGetAttribute(children[WORD_PGSZ],WORD_H); int widthTwips; int heightTwips; if ((widthStr == NULL) || (atoi(widthStr) <= 0) || (heightStr == NULL) || (atoi(heightStr) <= 0)) { // Invalid or missing page size: Set to A4 portrait widthTwips = A4_WIDTH_TWIPS; heightTwips = A4_HEIGHT_TWIPS; updatePageSize = 1; } else { widthTwips = atoi(widthStr); heightTwips = atoi(heightStr); } if (!DFStringEquals(CSSGet(oldPage,"size"),CSSGet(newPage,"size"))) { const char *newSize = CSSGet(newPage,"size"); if (DFStringEqualsCI(newSize,"A4 portrait")) { widthTwips = A4_WIDTH_TWIPS; heightTwips = A4_HEIGHT_TWIPS; } else if (DFStringEqualsCI(newSize,"A4 landscape")) { widthTwips = A4_HEIGHT_TWIPS; heightTwips = A4_WIDTH_TWIPS; } else if (DFStringEqualsCI(newSize,"letter portrait")) { widthTwips = LETTER_WIDTH_TWIPS; heightTwips = LETTER_HEIGHT_TWIPS; } else if (DFStringEqualsCI(newSize,"letter landscape")) { widthTwips = LETTER_HEIGHT_TWIPS; heightTwips = LETTER_WIDTH_TWIPS; } else { widthTwips = A4_WIDTH_TWIPS; heightTwips = A4_HEIGHT_TWIPS; } updatePageSize = 1; } if (updatePageSize) { DFFormatAttribute(children[WORD_PGSZ],WORD_W,"%d",widthTwips); DFFormatAttribute(children[WORD_PGSZ],WORD_H,"%d",heightTwips); if (widthTwips > heightTwips) DFSetAttribute(children[WORD_PGSZ],WORD_ORIENT,"landscape"); else DFRemoveAttribute(children[WORD_PGSZ],WORD_ORIENT); } if (children[WORD_PGMAR] == NULL) children[WORD_PGMAR] = DFCreateElement(concrete->doc,WORD_PGMAR); // Page margins if (!DFStringEquals(CSSGet(oldBody,"margin-left"),CSSGet(newBody,"margin-left")) || updatePageSize) updateTwipsFromLength(children[WORD_PGMAR],WORD_LEFT,CSSGet(newBody,"margin-left"),widthTwips); if (!DFStringEquals(CSSGet(oldBody,"margin-right"),CSSGet(newBody,"margin-right")) || updatePageSize) updateTwipsFromLength(children[WORD_PGMAR],WORD_RIGHT,CSSGet(newBody,"margin-right"),widthTwips); if (!DFStringEquals(CSSGet(oldBody,"margin-top"),CSSGet(newBody,"margin-top")) || updatePageSize) updateTwipsFromLength(children[WORD_PGMAR],WORD_TOP,CSSGet(newBody,"margin-top"),widthTwips); if (!DFStringEquals(CSSGet(oldBody,"margin-bottom"),CSSGet(newBody,"margin-bottom")) || updatePageSize) updateTwipsFromLength(children[WORD_PGMAR],WORD_BOTTOM,CSSGet(newBody,"margin-bottom"),widthTwips); if (children[WORD_PGMAR]->attrsCount == 0) children[WORD_PGMAR] = NULL;; DFArray *extra = DFArrayNew(NULL,NULL); for (DFNode *child = concrete->first; child != NULL; child = child->next) { switch (child->tag) { case WORD_HEADERREFERENCE: case WORD_FOOTERREFERENCE: DFArrayAppend(extra,child); break; } } replaceChildrenFromArray(concrete,children,WordSectPr_Children); for (long i = (long)(DFArrayCount(extra)-1); i >= 0; i--) { DFNode *child = DFArrayItemAt(extra,i); DFInsertBefore(concrete,child,concrete->first); } DFArrayRelease(extra); CSSPropertiesRelease(oldBody); CSSPropertiesRelease(oldPage); }
static void WordGetStyle(DFNode *concrete, CSSStyle *style, WordConverter *converter) { WordSection *section = converter->mainSection; for (DFNode *child = concrete->first; child != NULL; child = child->next) { const char *styleId = NULL; switch (child->tag) { case WORD_NAME: CSSStyleSetDisplayName(style,DFGetAttribute(child,WORD_VAL)); break; case WORD_NEXT: { // FIXME: do he need to handle style types other than paragraph here? const char *nextName = DFGetAttribute(child,WORD_VAL); if (nextName == NULL) continue; WordStyle *nextStyle = WordSheetStyleForTypeId(converter->styles,"paragraph",nextName); if (nextStyle == NULL) continue; CSSStyleSetNext(style,nextStyle->selector); break; } case WORD_RPR: WordGetRPr(child,CSSStyleRule(style),&styleId,converter->theme); break; case WORD_PPR: { WordGetPPr(child,CSSStyleRule(style),&styleId,section); if (style->headingLevel > 0) { DFNode *numPr = DFChildWithTag(child,WORD_NUMPR); if (numPr != NULL) WordGetNumPrStyle(numPr,style,converter); } break; } case WORD_TBLPR: WordGetTblPr(child,CSSStyleRule(style),CSSStyleCell(style),section,&styleId); break; case WORD_TRPR: // FIXME break; case WORD_TCPR: WordGetTcPr(child,CSSStyleRule(style)); break; case WORD_TBLSTYLEPR: WordGetTblStylePr(child,style,section,converter->theme); break; } } // Special case: The ListParagraph style that word automatically adds specifies an indentation // of 36pt. We don't actually want this, because HTML automatically indents lists anyway. If // we see this, clear it, but keep the old value around for when we update the word document. StyleFamily family = WordStyleFamilyForSelector(style->selector); const char *name = WordSheetStyleIdForSelector(converter->styles,style->selector); if ((family == StyleFamilyParagraph) && DFStringEquals(name,"ListParagraph")) { CSSProperties *properties = CSSStyleRule(style); const char *wordMarginLeft = CSSGet(properties,"margin-left"); CSSPut(properties,"-word-margin-left",wordMarginLeft); CSSPut(properties,"margin-left",NULL); } DFNode *pPr = DFChildWithTag(concrete,WORD_PPR); DFNode *numPr = DFChildWithTag(pPr,WORD_NUMPR); const char *numId = DFGetChildAttribute(numPr,WORD_NUMID,WORD_VAL); if ((numId != NULL) && (atoi(numId) == 0)) { switch (style->tag) { case HTML_H1: case HTML_H2: case HTML_H3: case HTML_H4: case HTML_H5: case HTML_H6: case HTML_FIGURE: case HTML_TABLE: { char *counterIncrement = DFFormatString("%s 0",style->elementName); CSSPut(CSSStyleRule(style),"counter-reset","null"); CSSPut(CSSStyleRule(style),"counter-increment",counterIncrement); CSSPut(CSSStyleBefore(style),"content","none"); free(counterIncrement); } } } }
int CSSGetDefault(CSSProperties *properties) { return DFStringEquals(CSSGet(properties,"-uxwrite-default"),"true"); }
int CSSStyleIsNumbered(CSSStyle *style) { const char *beforeContent = CSSGet(CSSStyleBefore(style),"content"); return ((beforeContent != NULL) && !DFStringEquals(beforeContent,"\"\"")); }
// FIXME: This won't work now for Word documents, where styles always have class names // FIXME: Not covered by tests int CSSSheetIsNumberingUsed(CSSSheet *sheet) { CSSStyle *style = CSSSheetLookupElement(sheet,"body",NULL,0,0); return (CSSGet(CSSStyleRule(style),"counter-reset") != NULL); }
static void WordPutStyle(DFNode *concrete, CSSStyle *style, WordConverter *converter) { // If we find a style with a counter-increment value of "<elementname> 0", this means that it's // an explicitly unnumbered paragraph. So we set the numbering id to 0, so word doesn't increment // the corresponding counter when encountering this style if (CSSGet(CSSStyleRule(style),"counter-increment") != NULL) { char *zeroIncrement = DFFormatString("%s 0",style->elementName); if (DFStringEquals(CSSGet(CSSStyleRule(style),"counter-increment"),zeroIncrement)) CSSPut(CSSStyleRule(style),"-word-numId","0"); free(zeroIncrement); } int outlineLvl = -1; if ((style->headingLevel >= 1) && (style->headingLevel <= 6)) { outlineLvl = style->headingLevel-1; char *nextSelector = CSSStyleCopyNext(style); if (nextSelector == NULL) { CSSStyle *def = CSSSheetDefaultStyleForFamily(converter->styleSheet,StyleFamilyParagraph); if (def != NULL) CSSStyleSetNext(style,def->selector); } free(nextSelector); } DFNode *children[PREDEFINED_TAG_COUNT]; childrenToArray(concrete,children); char *parentSelector = CSSStyleCopyParent(style); if (parentSelector == NULL) { if (style->className != NULL) { if ((style->tag != HTML_P) && (style->tag != HTML_SPAN) && (style->tag != HTML_TABLE)) { CSSStyle *parentStyle = CSSSheetLookupElement(converter->styleSheet,style->elementName,NULL,0,0); if ((parentStyle != NULL) && !parentStyle->latent) parentSelector = xstrdup(style->elementName); } } } // Based on const char *basedOn = WordSheetStyleIdForSelector(converter->styles,parentSelector); if (basedOn == NULL) { children[WORD_BASEDON] = NULL; } else { children[WORD_BASEDON] = DFCreateElement(concrete->doc,WORD_BASEDON); DFSetAttribute(children[WORD_BASEDON],WORD_VAL,basedOn); } // Style for next paragraph children[WORD_NEXT] = NULL; char *nextSelector = CSSStyleCopyNext(style); if (nextSelector != NULL) { StyleFamily thisFamily = WordStyleFamilyForSelector(style->selector); StyleFamily nextFamily = WordStyleFamilyForSelector(nextSelector); if (nextFamily == thisFamily) { children[WORD_NEXT] = DFCreateElement(concrete->doc,WORD_NEXT); const char *nextStyleId = WordSheetStyleIdForSelector(converter->styles,nextSelector); DFSetAttribute(children[WORD_NEXT],WORD_VAL,nextStyleId); } } if (WordStyleFamilyForSelector(style->selector) == StyleFamilyTable) { // Table properties if (children[WORD_TBLPR] == NULL) children[WORD_TBLPR] = DFCreateElement(concrete->doc,WORD_TBLPR);; const char *oldJc = DFGetChildAttribute(children[WORD_TBLPR],WORD_JC,WORD_VAL); CSSProperties *wholeTable = CSSStyleRuleForSuffix(style,DFTableSuffixWholeTable); WordPutTblPr(children[WORD_TBLPR],wholeTable,CSSStyleCell(style),converter->mainSection,NULL); const char *newJc = DFGetChildAttribute(children[WORD_TBLPR],WORD_JC,WORD_VAL); if (children[WORD_TBLPR]->first == NULL) children[WORD_TBLPR] = NULL; if (children[WORD_TRPR] == NULL) children[WORD_TRPR] = DFCreateElement(concrete->doc,WORD_TRPR); WordPutTrPr(children[WORD_TRPR],oldJc,newJc); if (children[WORD_TRPR]->first == NULL) children[WORD_TRPR] = NULL; } else { // Paragraph properties if (children[WORD_PPR] == NULL) children[WORD_PPR] = DFCreateElement(concrete->doc,WORD_PPR); WordPutPPr(children[WORD_PPR],CSSStyleRule(style),NULL,converter->mainSection,outlineLvl); if (children[WORD_PPR]->first == NULL) children[WORD_PPR] = NULL; // Run properties if (children[WORD_RPR] == NULL) children[WORD_RPR] = DFCreateElement(concrete->doc,WORD_RPR); WordPutRPr(children[WORD_RPR],CSSStyleRule(style),NULL,converter->theme); if (children[WORD_RPR]->first == NULL) children[WORD_RPR] = NULL; } replaceChildrenFromArray(concrete,children,WordStyle_Children); free(parentSelector); free(nextSelector); }
static void Word_preProcessHTML(WordConverter *word, DFNode *node) { switch (node->tag) { case HTML_TABLE: case HTML_FIGURE: { DFNode *next; for (DFNode *child = node->first; child != NULL; child = next) { next = child->next; if ((child->tag != HTML_CAPTION) && (child->tag != HTML_FIGCAPTION)) continue; WordCaption *caption = WordCaptionNew(child); WordObjectsSetCaption(word->objects,caption,node); caption->contentStart = child->first; WordCaptionRelease(caption); const char *className = DFGetAttribute(child,HTML_CLASS); CSSStyle *style; if (child->tag == HTML_CAPTION) style = CSSSheetLookupElement(word->styleSheet,"caption",className,0,0); else style = CSSSheetLookupElement(word->styleSheet,"figcaption",className,0,0); CSSProperties *before = CSSStyleBefore(style); if (CSSGet(before,"content") != NULL) Word_addContentParts(child,CSSGet(before,"content"),caption); child->tag = HTML_P; DFSetAttribute(child,HTML_CLASS,"Caption"); DFInsertBefore(node->parent,child,node->next); Word_preProcessHTML(word,child); } // The HTML normalization process ensures that apart from the <figcaption> element, // all children of a <figure> are paragraphs or containers. Currently the editor only // lets you create figures that contain a single image, so it's always a single // paragraph. Since the HTML <figure> element gets mapped to a single <w:p> element // by WordParagraphLens, we want to make sure it only contains inline children. for (DFNode *child = node->first; child != NULL; child = next) { next = child->next; if (HTML_isParagraphTag(child->tag)) DFRemoveNodeButKeepChildren(child); } // FIXME: Handle <div>, <pre>, lists, tables etc which could also theoretically // exist inside the <figure> element break; } case HTML_NAV: { const char *className = DFGetAttribute(node,HTML_CLASS); const char *instr = NULL; if (DFStringEquals(className,DFTableOfContentsClass)) instr = " TOC \\o \"1-3\" "; else if (DFStringEquals(className,DFListOfFiguresClass)) instr = " TOC \\c \"Figure\" "; else if (DFStringEquals(className,DFListOfTablesClass)) instr = " TOC \\c \"Table\" "; if (instr != NULL) { DFNode *p = DFCreateElement(word->html,HTML_P); DFNode *field = DFCreateChildElement(p,HTML_SPAN); DFSetAttribute(field,HTML_CLASS,DFFieldClass); DFCreateChildTextNode(field,instr); DFInsertBefore(node->parent,p,node); DFRemoveNode(node); } break; } } DFNode *next; for (DFNode *child = node->first; child != NULL; child = next) { next = child->next; Word_preProcessHTML(word,child); } }
static void Word_postProcessHTML(WordConverter *conv, DFNode *node) { DFNode *next; for (DFNode *child = node->first; child != NULL; child = next) { next = child->next; switch (child->tag) { case HTML_SPAN: { const char *className = DFGetAttribute(child,HTML_CLASS); if (DFStringEquals(className,DFBookmarkClass)) { if (child->first != NULL) next = child->first; DFRemoveNodeButKeepChildren(child); } break; } case HTML_CAPTION: { const char *counterName = NULL; if ((child->prev != NULL) && (child->prev->tag == HTML_FIGURE) && (DFChildWithTag(child->prev,HTML_FIGCAPTION) == NULL)) { child->tag = HTML_FIGCAPTION; counterName = "figure"; DFAppendChild(child->prev,child); } else if ((child->prev != NULL) && (child->prev->tag == HTML_TABLE) && (DFChildWithTag(child->prev,HTML_CAPTION) == NULL)) { counterName = "table"; DFInsertBefore(child->prev,child,child->prev->first); } else if ((child->next != NULL) && (child->next->tag == HTML_FIGURE) && (DFChildWithTag(child->next,HTML_FIGCAPTION) == NULL)) { child->tag = HTML_FIGCAPTION; counterName = "figure"; DFInsertBefore(child->next,child,child->next->first); } else if ((child->next != NULL) && (child->next->tag == HTML_TABLE) && (DFChildWithTag(child->next,HTML_CAPTION) == NULL)) { counterName = "table"; DFSetAttribute(child,HTML_STYLE,"caption-side: top"); DFInsertBefore(child->next,child,child->next->first); } if (counterName != NULL) { char *beforeText = extractPrefix(child,counterName); if (beforeText != NULL) { CSSStyle *style = CSSSheetLookupElement(conv->styleSheet,DFNodeName(child),NULL,1,0); if (CSSGet(CSSStyleBefore(style),"content") == NULL) { CSSPut(CSSStyleRule(style),"counter-increment",counterName); CSSPut(CSSStyleBefore(style),"content",beforeText); } } free(beforeText); } break; } case HTML_NAV: { if (HTML_isParagraphTag(node->tag)) { if (child->prev != NULL) { DFNode *beforeP = DFCreateElement(conv->package->document,node->tag); while (child->prev != NULL) DFInsertBefore(beforeP,child->prev,beforeP->first); DFInsertBefore(node->parent,beforeP,node); } DFInsertBefore(node->parent,child,node); if ((node->first == NULL) || ((node->first->tag == HTML_BR) && (node->first->next == NULL))) { DFRemoveNode(node); return; } next = NULL; } break; } } } for (DFNode *child = node->first; child != NULL; child = next) { next = child->next; Word_postProcessHTML(conv,child); } }
static void WordTblPut(WordPutData *put, DFNode *abstract, DFNode *concrete) { if ((abstract->tag != HTML_TABLE) || (concrete->tag != WORD_TBL)) return;; DFTable *abstractStructure = HTML_tableStructure(abstract); const char *inlineCSSText = DFGetAttribute(abstract,HTML_STYLE); CSSProperties *tableProperties = CSSPropertiesNewWithString(inlineCSSText); CSSProperties *cellProperties = CSSPropertiesNew(); const char *className = DFGetAttribute(abstract,HTML_CLASS); char *selector = CSSMakeSelector("table",className); WordStyle *style = WordSheetStyleForSelector(put->conv->styles,selector); CellPadding padding = getPadding(put->conv->styleSheet,style,cellProperties); DFNode *tblPr = DFChildWithTag(concrete,WORD_TBLPR); if (tblPr == NULL) tblPr = DFCreateElement(concrete->doc,WORD_TBLPR);; DFNode *tblGrid = DFChildWithTag(concrete,WORD_TBLGRID); if (tblGrid == NULL) tblGrid = DFCreateElement(concrete->doc,WORD_TBLGRID); while (concrete->first != NULL) DFRemoveNode(concrete->first); const char *oldJc = DFGetChildAttribute(tblPr,WORD_JC,WORD_VAL); WordPutTblPr(tblPr,tableProperties,NULL,put->conv->mainSection,style != NULL ? style->styleId : NULL); const char *newJc = DFGetChildAttribute(tblPr,WORD_JC,WORD_VAL); double tableWidthPct = 100; if (CSSGet(tableProperties,"width") != NULL) { CSSLength length = CSSLengthFromString(CSSGet(tableProperties,"width")); if (CSSLengthIsValid(length) && (length.units == UnitsPct)) tableWidthPct = length.value; } double contentWidthPts = WordSectionContentWidthPts(put->conv->mainSection); double totalWidthPts = (contentWidthPts+padding.leftPts+padding.rightPts)*(tableWidthPct/100.0); while (tblGrid->first != NULL) DFRemoveNode(tblGrid->first); for (unsigned int i = 0; i < abstractStructure->cols; i++) { DFNode *gridCol = DFCreateChildElement(tblGrid,WORD_GRIDCOL); double colWidthPct = DFTablePctWidthForCol(abstractStructure,i); double colWidthPts = totalWidthPts*colWidthPct/100.0; int colWidthTwips = (int)round(colWidthPts*20); DFFormatAttribute(gridCol,WORD_W,"%d",colWidthTwips); } DFAppendChild(concrete,tblPr); DFAppendChild(concrete,tblGrid); for (unsigned int row = 0; row < abstractStructure->rows; row++) { DFNode *htmlTr = DFTableGetRowElement(abstractStructure,row); DFNode *wordTr = concreteRowForAbstractRow(put,htmlTr); updateTrJc(wordTr,oldJc,newJc); DFAppendChild(concrete,wordTr); unsigned int col = 0; while (col < abstractStructure->cols) { DFCell *cell = DFTableGetCell(abstractStructure,row,col); assert(cell != NULL); DFNode *tc = WordConverterGetConcrete(put,cell->element); if ((tc == NULL) || (row != cell->row)) tc = DFCreateElement(concrete->doc,WORD_TC); DFAppendChild(wordTr,tc); if (cell->row == row) WordTcPut(put,cell->element,tc);; const char *vMerge = NULL; if (cell->rowSpan > 1) { if (row == cell->row) vMerge = "restart"; else vMerge = "continue"; } DFNode *tcPr = DFChildWithTag(tc,WORD_TCPR); if (tcPr == NULL) tcPr = DFCreateElement(concrete->doc,WORD_TCPR); // Make sure tcPr comes first DFInsertBefore(tc,tcPr,tc->first); WordPutTcPr2(tcPr,cell->colSpan,vMerge); const char *inlineCSSText = DFGetAttribute(cell->element,HTML_STYLE); CSSProperties *innerCellProperties = CSSPropertiesNewWithString(inlineCSSText); if ((row == cell->row) && (totalWidthPts > 0)) { double spannedWidthPct = 0; for (unsigned int c = col; c < col + cell->colSpan; c++) spannedWidthPct += DFTablePctWidthForCol(abstractStructure,c); char buf[100]; CSSPut(innerCellProperties,"width",DFFormatDoublePct(buf,100,spannedWidthPct)); } WordPutTcPr1(tcPr,innerCellProperties); int haveBlockLevelElement = 0; for (DFNode *tcChild = tc->first; tcChild != NULL; tcChild = tcChild->next) { if (WordBlockLevelLens.isVisible(put,tcChild)) haveBlockLevelElement = 1; } // Every cell must contain at least one block-level element if (!haveBlockLevelElement) { DFNode *p = DFCreateElement(concrete->doc,WORD_P); DFAppendChild(tc,p); } col += cell->colSpan; CSSPropertiesRelease(innerCellProperties); } } free(selector); DFTableRelease(abstractStructure); CSSPropertiesRelease(tableProperties); CSSPropertiesRelease(cellProperties); }
void WordUpdateStyles(WordConverter *converter, CSSSheet *styleSheet) { CSSStyle *paraDefault = CSSSheetDefaultStyleForFamily(styleSheet,StyleFamilyParagraph); if (CSSGet(CSSStyleRule(paraDefault),"margin-top") == NULL) CSSPut(CSSStyleRule(paraDefault),"margin-top","-word-auto"); if (CSSGet(CSSStyleRule(paraDefault),"margin-bottom") == NULL) CSSPut(CSSStyleRule(paraDefault),"margin-bottom","-word-auto"); if (converter->package->styles == NULL) // FIXME: create this document return;; DFNode *root = converter->package->styles->root; if ((root == NULL) || (root->tag != WORD_STYLES)) return;; DFHashTable *remainingSelectors = DFHashTableNew(NULL,NULL); // Used as a set const char **allSelectors = CSSSheetCopySelectors(styleSheet); for (int i = 0; allSelectors[i]; i++) { const char *selector = allSelectors[i]; DFHashTableAdd(remainingSelectors,selector,""); } free(allSelectors); WordSheet *sheet = converter->styles; DFHashTable *oldConcreteNumIds = WordSheetFindUsedConcreteNumIds(sheet); updateNumbering(converter,styleSheet); // Update or remove existing styles const char **allIdents = WordSheetCopyIdents(sheet); for (int i = 0; allIdents[i]; i++) { WordStyle *wordStyle = WordSheetStyleForIdent(sheet,allIdents[i]); DFNode *element = wordStyle->element; if (WordStyleIsProtected(wordStyle)) { DFHashTableRemove(remainingSelectors,wordStyle->selector); continue; } if (!DFStringEquals(wordStyle->type,"paragraph") && !DFStringEquals(wordStyle->type,"character") && !DFStringEquals(wordStyle->type,"table")) continue; CSSStyle *cssStyle = CSSSheetLookupSelector(styleSheet,wordStyle->selector,0,0); if (cssStyle == NULL) { // Remove style WordSheetRemoveStyle(sheet,wordStyle); continue; } // Update style WordPutStyle(element,cssStyle,converter); updateDefault(cssStyle,element,styleSheet,converter); DFHashTableRemove(remainingSelectors,wordStyle->selector); } free(allIdents); // Sort the list of new styles, so that test output is deterministic const char **sortedSelectors = DFHashTableCopyKeys(remainingSelectors); DFSortStringsCaseInsensitive(sortedSelectors); // Add new styles. We do this in two stages - first creating the styles, and then setting their properties. // This is because the second stage depends on referenced styles (e.g. based on and next) to be already // present. for (int selIndex = 0; sortedSelectors[selIndex]; selIndex++) { const char *selector = sortedSelectors[selIndex]; CSSStyle *style = CSSSheetLookupSelector(styleSheet,selector,0,0); const char *familyStr = NULL; StyleFamily family = WordStyleFamilyForSelector(selector); if (family == StyleFamilyParagraph) familyStr = "paragraph"; else if (family == StyleFamilyCharacter) familyStr = "character"; else if (family == StyleFamilyTable) familyStr = "table"; else continue; char *styleId = WordStyleIdForStyle(style); char *name = WordStyleNameForStyle(style); if (name == NULL) name = xstrdup(styleId);; WordStyle *wordStyle = WordSheetAddStyle(sheet,familyStr,styleId,name,selector); DFCreateChildElement(wordStyle->element,WORD_QFORMAT); free(styleId); free(name); } for (int selIndex = 0; sortedSelectors[selIndex]; selIndex++) { const char *selector = sortedSelectors[selIndex]; StyleFamily family = WordStyleFamilyForSelector(selector); if ((family != StyleFamilyParagraph) && (family != StyleFamilyCharacter) && (family != StyleFamilyTable)) continue; CSSStyle *style = CSSSheetLookupSelector(styleSheet,selector,0,0); WordStyle *wordStyle = WordSheetStyleForSelector(converter->styles,selector); assert(wordStyle != NULL); CSSStyleAddDefaultHTMLProperties(style); // FIXME: language // FIXME: not covered by tests if ((style->headingLevel >= 1) && (style->headingLevel <= 6)) CSSStyleSetNext(style,"p.Normal"); WordPutStyle(wordStyle->element,style,converter); updateDefault(style,wordStyle->element,styleSheet,converter); } free(sortedSelectors); // Update body style (document defaults) updateDefaults(converter,styleSheet); updateBody(converter,styleSheet); DFHashTable *newConcreteNumIds = WordSheetFindUsedConcreteNumIds(sheet); const char **oldKeys = DFHashTableCopyKeys(oldConcreteNumIds); for (int oldIndex = 0; oldKeys[oldIndex]; oldIndex++) { const char *numId = oldKeys[oldIndex]; if (DFHashTableLookup(newConcreteNumIds,numId) == NULL) { WordConcreteNum *concreteNum = WordNumberingConcreteWithId(converter->numbering,numId); if (concreteNum != NULL) WordNumberingRemoveConcrete(converter->numbering,concreteNum); } } free(oldKeys); DFHashTableRelease(remainingSelectors); DFHashTableRelease(oldConcreteNumIds); DFHashTableRelease(newConcreteNumIds); }
void WordPutPPr(DFNode *pPr, CSSProperties *properties, const char *styleId, WordSection *section, int outlineLvl) { assert(pPr->tag == WORD_PPR); // The child elements of pPr have to be in a specific order. So we build up a structure based // on the existing elements (updated as appropriate from newProperties), and then rebuild // from that CSSProperties *oldp = CSSPropertiesNew(); CSSProperties *newp = properties; const char *oldStyleId = NULL; const char *newStyleId = styleId; WordGetPPr(pPr,oldp,&oldStyleId,section); { DFNode *children[PREDEFINED_TAG_COUNT]; childrenToArray(pPr,children); int existingOutlineLvl = -1; if (children[WORD_OUTLINELVL] != NULL) { const char *value = DFGetAttribute(children[WORD_OUTLINELVL],WORD_VAL); if (value != NULL) existingOutlineLvl = atoi(value); } // Style name if (!DFStringEquals(oldStyleId,newStyleId)) { if (newStyleId != NULL) { children[WORD_PSTYLE] = DFCreateElement(pPr->doc,WORD_PSTYLE); DFSetAttribute(children[WORD_PSTYLE],WORD_VAL,newStyleId); } else { children[WORD_PSTYLE] = NULL; } } // Paragraph border (pBdr) if (children[WORD_PBDR] == NULL) children[WORD_PBDR] = DFCreateElement(pPr->doc,WORD_PBDR); WordPutPBdr(children[WORD_PBDR],oldp,newp); if (children[WORD_PBDR]->first == NULL) children[WORD_PBDR] = NULL; // Numbering and outline level // Don't change these properties for styles with outline level >= 6, as these can't be // represented with the standard HTML heading elements, which only go from h1 - h6 // (outline levels 0 - 5) if (existingOutlineLvl < 6) { if (children[WORD_NUMPR] == NULL) children[WORD_NUMPR] = DFCreateElement(pPr->doc,WORD_NUMPR); WordPutNumPr(children[WORD_NUMPR],newp); if (children[WORD_NUMPR]->first == NULL) children[WORD_NUMPR] = NULL; if ((outlineLvl >= 0) && (outlineLvl <= 5)) { if (children[WORD_OUTLINELVL] == NULL) children[WORD_OUTLINELVL] = DFCreateElement(pPr->doc,WORD_OUTLINELVL); DFFormatAttribute(children[WORD_OUTLINELVL],WORD_VAL,"%d",outlineLvl); } else { children[WORD_OUTLINELVL] = NULL; } } // background-color char *oldBackgroundColor = CSSHexColor(CSSGet(oldp,"background-color"),0); char *newBackgroundColor = CSSHexColor(CSSGet(newp,"background-color"),0); if (!DFStringEquals(oldBackgroundColor,newBackgroundColor)) WordPutShd(pPr->doc,&children[WORD_SHD],newBackgroundColor); free(oldBackgroundColor); free(newBackgroundColor); // text-align if (!DFStringEquals(CSSGet(oldp,"text-align"),CSSGet(newp,"text-align"))) { const char *newTextAlign = CSSGet(newp,"text-align"); if (newTextAlign != NULL) { const char *val = NULL; if (!strcmp(newTextAlign,"left")) val = "left"; else if (!strcmp(newTextAlign,"right")) val = "right"; else if (!strcmp(newTextAlign,"center")) val = "center"; else if (!strcmp(newTextAlign,"justify")) val = "both"; if (val != NULL) { children[WORD_JC] = DFCreateElement(pPr->doc,WORD_JC); DFSetAttribute(children[WORD_JC],WORD_VAL,val); } } else { children[WORD_JC] = NULL; } } if ((section != NULL) && (WordSectionContentWidth(section) >= 0)) { const char *oldMarginLeft = CSSGet(oldp,"margin-left"); const char *oldMarginRight = CSSGet(oldp,"margin-right"); const char *oldTextIndent = CSSGet(oldp,"text-indent"); const char *newMarginLeft = CSSGet(newp,"margin-left"); const char *newMarginRight = CSSGet(newp,"margin-right"); const char *newTextIndent = CSSGet(newp,"text-indent"); // Special case of the List_Paragraph style, which is used by Word to ensure lists are indented. We // don't set this property for HTML, because it automatically indents lists. However we need to ensure // that it remains unchanged when updating the word document const char *newWordMarginLeft = CSSGet(newp,"-word-margin-left"); if (newMarginLeft == NULL) newMarginLeft = newWordMarginLeft; if ((newMarginLeft == NULL) && (newMarginRight == NULL) && (newTextIndent == NULL)) { children[WORD_IND] = NULL; } else { if (children[WORD_IND] == NULL) children[WORD_IND] = DFCreateElement(pPr->doc,WORD_IND); if (!DFStringEquals(oldMarginLeft,newMarginLeft)) { if (newMarginLeft != NULL) updateTwipsFromLength(children[WORD_IND],WORD_LEFT,newMarginLeft,WordSectionContentWidth(section)); else DFRemoveAttribute(children[WORD_IND],WORD_LEFT); DFRemoveAttribute(children[WORD_IND],WORD_START); } if (!DFStringEquals(oldMarginRight,newMarginRight)) { if (newMarginRight != NULL) updateTwipsFromLength(children[WORD_IND],WORD_RIGHT,newMarginRight,WordSectionContentWidth(section)); else DFRemoveAttribute(children[WORD_IND],WORD_RIGHT); DFRemoveAttribute(children[WORD_IND],WORD_END); } if (!DFStringEquals(oldTextIndent,newTextIndent)) { if (newTextIndent != NULL) { CSSLength length = CSSLengthFromString(newTextIndent); if (CSSLengthIsValid(length)) { double pts = CSSLengthToPts(length,WordSectionContentWidthPts(section)); int twips = (int)round(pts*20); if (twips >= 0) { DFFormatAttribute(children[WORD_IND],WORD_FIRSTLINE,"%d",twips); DFRemoveAttribute(children[WORD_IND],WORD_HANGING); } else { DFFormatAttribute(children[WORD_IND],WORD_HANGING,"%d",-twips); DFRemoveAttribute(children[WORD_IND],WORD_FIRSTLINE); } } } else { DFRemoveAttribute(children[WORD_IND],WORD_FIRSTLINE); DFRemoveAttribute(children[WORD_IND],WORD_HANGING); } } } } if (!DFStringEquals(CSSGet(oldp,"margin-top"),CSSGet(newp,"margin-top")) || !DFStringEquals(CSSGet(oldp,"margin-bottom"),CSSGet(newp,"margin-bottom")) || !DFStringEquals(CSSGet(oldp,"line-height"),CSSGet(newp,"line-height"))) { if ((CSSGet(newp,"margin-top") == NULL) && (CSSGet(newp,"margin-bottom") == NULL) && (CSSGet(newp,"line-height") == NULL)) { children[WORD_SPACING] = NULL; } else { children[WORD_SPACING] = DFCreateElement(pPr->doc,WORD_SPACING); if (DFStringEquals(CSSGet(newp,"margin-top"),"-word-auto")) { DFSetAttribute(children[WORD_SPACING],WORD_BEFORE,"100"); DFSetAttribute(children[WORD_SPACING],WORD_BEFOREAUTOSPACING,"1"); } else { char *before = twipsFromCSS(CSSGet(newp,"margin-top"),WordSectionContentWidth(section)); DFSetAttribute(children[WORD_SPACING],WORD_BEFORE,before); DFSetAttribute(children[WORD_SPACING],WORD_BEFOREAUTOSPACING,NULL); free(before); } if (DFStringEquals(CSSGet(newp,"margin-bottom"),"-word-auto")) { DFSetAttribute(children[WORD_SPACING],WORD_AFTER,"100"); DFSetAttribute(children[WORD_SPACING],WORD_AFTERAUTOSPACING,"1"); } else { char *after = twipsFromCSS(CSSGet(newp,"margin-bottom"),WordSectionContentWidth(section)); DFSetAttribute(children[WORD_SPACING],WORD_AFTER,after); DFSetAttribute(children[WORD_SPACING],WORD_AFTERAUTOSPACING,NULL); free(after); } CSSLength lineHeight = CSSLengthFromString(CSSGet(newp,"line-height")); if (CSSLengthIsValid(lineHeight) && (lineHeight.units == UnitsPct)) { int value = (int)round(lineHeight.value*2.4); DFFormatAttribute(children[WORD_SPACING],WORD_LINE,"%d",value); } if (children[WORD_SPACING]->attrsCount == 0) children[WORD_SPACING] = NULL; } } replaceChildrenFromArray(pPr,children,WordPPR_Children); } CSSPropertiesRelease(oldp); }