Exemplo n.º 1
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;
}
Exemplo n.º 2
0
static void test_parse(void)
{
    const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css");
    if (inputCSS == NULL) {
        DFBufferFormat(utgetoutput(),"input.css not defined");
        return;
    }
    CSSSheet *styleSheet = CSSSheetNew();
    CSSSheetUpdateFromCSSText(styleSheet,inputCSS);
    char *text = CSSSheetCopyText(styleSheet);
    DFBufferFormat(utgetoutput(),"%s",text);
    free(text);
    DFBufferFormat(utgetoutput(),
                   "================================================================================\n");
    char *cssText = CSSSheetCopyCSSText(styleSheet);
    DFBufferFormat(utgetoutput(),"%s",cssText);
    free(cssText);
    CSSSheetRelease(styleSheet);
}
Exemplo n.º 3
0
static void test_setHeadingNumbering(void)
{
    const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css");
    if (inputCSS == NULL) {
        DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: input.css not defined");
        return;
    }
    if (utgetargc() < 1) {
        DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: expected 1 argument");
        return;
    }

    CSSSheet *styleSheet = CSSSheetNew();
    CSSSheetUpdateFromCSSText(styleSheet,inputCSS);
    int on = !strcasecmp(utgetargv()[0],"true");
    CSSSheetSetHeadingNumbering(styleSheet,on);
    char *cssText = CSSSheetCopyCSSText(styleSheet);
    DFBufferFormat(utgetoutput(),"%s",cssText);
    free(cssText);
    CSSSheetRelease(styleSheet);
}
Exemplo n.º 4
0
int testCSS(const char *filename, DFError **error)
{
    char *input = DFStringReadFromFile(filename,error);
    if (input == NULL) {
        DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error));
        return 0;
    }

    CSSSheet *styleSheet = CSSSheetNew();
    CSSSheetUpdateFromCSSText(styleSheet,input);
    char *text = CSSSheetCopyText(styleSheet);
    printf("%s",text);
    free(text);
    printf("================================================================================\n");
    char *cssText = CSSSheetCopyCSSText(styleSheet);
    printf("%s",cssText);
    free(cssText);
    CSSSheetRelease(styleSheet);
    free(input);

    return 1;
}
Exemplo n.º 5
0
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;
}