Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docx output #138

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,29 @@ public class StyleReference {
* @param userAgent PARAM
*/
public StyleReference(UserAgentCallback userAgent) {
this(userAgent, null);
}


/**
* A docx4j extension to standard FS, allowing extra stylesheets
* (ie that aren't explicit, either linked or embedded) to be specified
* and applied. This is useful, for example, for adding CSS which
* represent WordML styles in the target package, which can be
* used via @class. OpenDoPE uses this in its XHTML import.
* It could also be useful in a web-based editing scenario.
*/
private StylesheetInfo[] extraCSS;

/**
* Default constructor for initializing members.
*
* @param userAgent PARAM
*/
public StyleReference(UserAgentCallback userAgent, StylesheetInfo[] extraCSS) {
_uac = userAgent;
_stylesheetFactory = new StylesheetFactoryImpl(userAgent);
this.extraCSS = extraCSS;
}

/**
Expand Down Expand Up @@ -263,8 +284,11 @@ private List getStylesheets() {
refs[i].setUri(null);
}
}
infos.addAll(Arrays.asList(refs));
}
infos.addAll(Arrays.asList(refs));

// docx4j addition
handleExtraCSS( infos, inlineStyleCount);

// TODO: here we should also get user stylesheet from userAgent

Expand All @@ -273,7 +297,36 @@ private List getStylesheets() {

return infos;
}


/**
* Dynamically add any extra CSS
*
* @param infos
* @param inlineStyleCount
* @since 3.0
*/
private void handleExtraCSS(List infos, int inlineStyleCount) {

if (extraCSS != null) {
for (int i = 0; i < extraCSS.length; i++) {
String uri;

if (! extraCSS[i].isInline()) {
uri = _uac.resolveURI(extraCSS[i].getUri());
extraCSS[i].setUri(uri);
} else {
// Expect that extra CSS will be effectively inline
extraCSS[i].setUri(_uac.getBaseURL() + "#inline_style_" + (++inlineStyleCount));
Stylesheet sheet = _stylesheetFactory.parse(
new StringReader(extraCSS[i].getContent()), extraCSS[i]);
extraCSS[i].setStylesheet(sheet);
extraCSS[i].setUri(null);
}
}
infos.addAll(Arrays.asList(extraCSS));
}
}

public void removeStyle(Element e) {
if (_matcher != null) {
_matcher.removeStyle(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.css.CSSPrimitiveValue;
import org.xhtmlrenderer.css.parser.FSColor;
import org.xhtmlrenderer.css.style.CssContext;
import org.xhtmlrenderer.css.style.FSDerivedValue;
Expand Down Expand Up @@ -55,6 +56,16 @@
public class IdentValue implements FSDerivedValue {
private static int maxAssigned = 0;

// JBH
private CSSPrimitiveValue cssPrimitiveValue;
public void setCSSPrimitiveValue(CSSPrimitiveValue cssPrimitiveValue) {
this.cssPrimitiveValue = cssPrimitiveValue;
}

public CSSPrimitiveValue getCSSPrimitiveValue() {
return cssPrimitiveValue;
}

/**
* Description of the Field
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,39 @@ private String genStyleKey() {

}

public String toStringMine() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < _derivedValuesById.length; i++) {
CSSName name = CSSName.getByID(i);
FSDerivedValue val = this.valueByName(name); // walks parents as necessary to get the value
//FSDerivedValue val = _derivedValuesById[i];
if (val != null) {
sb.append(name.toString()+ ": " + val.asString() );
} else {
// sb.append(name.toString() );
// sb.append("(no prop assigned in this pos)");
}
sb.append("; ");
}
return sb.toString();

}

public String getDisplayMine() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < _derivedValuesById.length; i++) {
CSSName name = CSSName.getByID(i);
if (name.equals(CSSName.DISPLAY) ) {
return _derivedValuesById[i].asString();
}
}
return null;
}

public FSDerivedValue[] getDerivedValues() {
return _derivedValuesById;
}

public RectPropertySet getCachedPadding() {
if (_padding == null) {
throw new XRRuntimeException("No padding property cached yet; should have called getPropertyRect() at least once before.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ public abstract class DerivedValue implements FSDerivedValue {

private short _cssSacUnitType;

private CSSPrimitiveValue cssPrimitiveValue;
public CSSPrimitiveValue getCSSPrimitiveValue() {
return cssPrimitiveValue;
}

protected DerivedValue() {}

protected DerivedValue(
CSSName name,
CSSPrimitiveValue cssPrimitiveValue,
short cssSACUnitType,
String cssText,
String cssStringValue) {

this.cssPrimitiveValue = cssPrimitiveValue;

this._cssSacUnitType = cssSACUnitType;

if ( cssText == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ColorValue extends DerivedValue {
private FSColor _color;

public ColorValue(CSSName name, PropertyValue value) {
super(name, value.getPrimitiveType(), value.getCssText(), value.getCssText());
super(name, value, value.getPrimitiveType(), value.getCssText(), value.getCssText());

_color = value.getFSColor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static FSDerivedValue newDerivedValue(
if (ident == null) {
ident = IdentValue.getByIdentString(value.getStringValue());
}
ident.setCSSPrimitiveValue(value);
return ident;
case PropertyValue.VALUE_TYPE_STRING:
return new StringValue(cssName, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class FunctionValue extends DerivedValue {
private FSFunction _function;

public FunctionValue(CSSName name, PropertyValue value) {
super(name, value.getPrimitiveType(), value.getCssText(), value.getCssText());
super(name, value, value.getPrimitiveType(), value.getCssText(), value.getCssText());

_function = value.getFunction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class LengthValue extends DerivedValue {
private short _lengthPrimitiveType;

public LengthValue(CalculatedStyle style, CSSName name, PropertyValue value) {
super(name, value.getPrimitiveType(), value.getCssText(), value.getCssText());
super(name, value, value.getPrimitiveType(), value.getCssText(), value.getCssText());

_style = style;
_lengthAsFloat = value.getFloatValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ListValue extends DerivedValue {
private List _values;

public ListValue(CSSName name, PropertyValue value) {
super(name, value.getPrimitiveType(), value.getCssText(), value.getCssText());
super(name, value, value.getPrimitiveType(), value.getCssText(), value.getCssText());

_values = value.getValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NumberValue extends DerivedValue {
private float _floatValue;

public NumberValue(CSSName cssName, PropertyValue value) {
super(cssName, value.getPrimitiveType(), value.getCssText(), value.getCssText());
super(cssName, value, value.getPrimitiveType(), value.getCssText(), value.getCssText());
_floatValue = value.getFloatValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class StringValue extends DerivedValue {
private String[] _stringAsArray;

public StringValue(CSSName name, PropertyValue value) {
super(name, value.getPrimitiveType(), value.getCssText(), value.getStringValue());
super(name, value, value.getPrimitiveType(), value.getCssText(), value.getStringValue());
if (value.getStringArrayValue() != null) {
_stringAsArray = value.getStringArrayValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,16 +1212,21 @@ private static void createChildren(
child = createInlineBox(text.toString(), parent, parentStyle, textNode);
*/

child = createInlineBox(textNode.getData(), parent, parentStyle, textNode);

InlineBox iB = (InlineBox) child;
iB.setEndsHere(true);
if (previousIB == null) {
iB.setStartsHere(true);
if ("script".equals(parent.getLocalName())) {
// This is an important change, for us. It gets rid of a lot of extra unwanted parents inserted
//System.out.println("Skipping empty text");
} else {
previousIB.setEndsHere(false);
child = createInlineBox(textNode.getData(), parent, parentStyle, textNode);

InlineBox iB = (InlineBox) child;
iB.setEndsHere(true);
if (previousIB == null) {
iB.setStartsHere(true);
} else {
previousIB.setEndsHere(false);
}
previousIB = iB;
}
previousIB = iB;
} else if(nodeType == Node.ENTITY_REFERENCE_NODE) {
EntityReference entityReference = (EntityReference)working;
child = createInlineBox(entityReference.getTextContent(), parent, parentStyle, null);
Expand All @@ -1241,7 +1246,12 @@ private static void createChildren(
}
} while ((working = working.getNextSibling()) != null);
}
if (needStartText || needEndText) {
if ("br".equals(parent.getNodeName())
//|| "a".equals(parent.getNodeName())
) {
// avoid double node being inserted for br!
// TODO: understand needStartText/needEndText so this isn't necessary
} else if (needStartText || needEndText) {
InlineBox iB = createInlineBox("", parent, parentStyle, null);
iB.setStartsHere(needStartText);
iB.setEndsHere(needEndText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class SharedContext {
private Rectangle temp_canvas;

private LineBreakingStrategy lineBreakingStrategy = new DefaultLineBreakingStrategy();
private int _realContentWidth = 0;

public SharedContext() {
}
Expand Down Expand Up @@ -630,6 +631,16 @@ public LineBreakingStrategy getLineBreakingStrategy() {
public void setLineBreakingStrategy(LineBreakingStrategy lineBreakingStrategy) {
this.lineBreakingStrategy = lineBreakingStrategy;
}

public int getRealContentWidth(){
return _realContentWidth;
}


public int setRealContentWidth(int newContentWidth){
_realContentWidth = newContentWidth > _realContentWidth ? newContentWidth : _realContentWidth;
return _realContentWidth;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
// manage colspans and the details of the table layout algorithms). Many kudos
// to the KHTML developers for making such an amazing piece of software!
public class TableBox extends BlockBox {

public TableBox() {}

private final List _columns = new ArrayList();
private int[] _columnPos;
private TableLayout _tableLayout;
Expand Down Expand Up @@ -197,7 +200,7 @@ public List getColumns() {
return _columns;
}

private void recalcSections(LayoutContext c) {
public void recalcSections(LayoutContext c) {
ensureChildren(c);
for (Iterator i = getChildIterator(); i.hasNext(); ) {
TableSectionBox section = (TableSectionBox)i.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public TableBox getTable() {
return _table;
}

protected TableSectionBox getSection() {
public TableSectionBox getSection() {
if (_section == null) {
_section = (TableSectionBox)getParent().getParent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,18 @@ private void addBoxID(LayoutContext c) {
c.addBoxId(id, this);
}
}
c.getSharedContext().setRealContentWidth(getMinWidth());
}

public void layout(LayoutContext c) {
layout(c, 0);
}

private RectPropertySet margin;
public RectPropertySet getMargin() {
return margin;
}

public void layout(LayoutContext c, int contentStart) {
CalculatedStyle style = getStyle();

Expand Down Expand Up @@ -820,7 +826,7 @@ public void layout(LayoutContext c, int contentStart) {
}

BorderPropertySet border = getBorder(c);
RectPropertySet margin = getMargin(c);
margin = getMargin(c);
RectPropertySet padding = getPadding(c);

// save height in case fixed height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ private void setParserFeatures(XMLReader xmlReader) {
xmlReader.setFeature("http://xml.org/sax/features/validation", false);
// perf: namespaces
xmlReader.setFeature("http://xml.org/sax/features/namespaces", true);

// prevent behaviour: " no local mapping. Parser will probably pull from network."
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (SAXException s) {
// nothing to do--some parsers will not allow setting features
XRLog.load(Level.WARNING, "Could not set validation/namespace features for XML parser," +
Expand Down
Loading