Skip to content

Commit

Permalink
tcPr textDirection property (rotated text)
Browse files Browse the repository at this point in the history
  • Loading branch information
plutext committed Jun 17, 2018
1 parent f77f965 commit d8254cb
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ protected Node toNode(AbstractWmlConversionContext context, AbstractTableWriterM
log.warn("model cell had no contents!");
} else {
log.debug("copying cell contents..");

cellNode = interposeBlockContainer(doc, cellNode, cell.getTcPr());

XmlUtils.treeCopy( ((AbstractTableWriterModelCell)cell).getContent().getChildNodes(),
cellNode);
}
Expand All @@ -296,6 +299,18 @@ protected Node toNode(AbstractWmlConversionContext context, AbstractTableWriterM
}
return docfrag;
}

/**
* In the FO case, if we need to rotate the text, we do that
* by inserting a block-container.
*
* @param cellNode
* @return
*/
protected Element interposeBlockContainer(Document doc, Element cellNode, TcPr tcPr) {

return cellNode;
}

protected Element createNode(Document doc, Element parent, int nodeType) {
Element ret = createNode(doc, nodeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.docx4j.model.properties.table.CellMarginRight;
import org.docx4j.model.properties.table.CellMarginTop;
import org.docx4j.model.properties.table.tc.Shading;
import org.docx4j.model.properties.table.tc.TextDir;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.wml.CTTblPrBase;
import org.docx4j.wml.CTTblStylePr;
Expand Down Expand Up @@ -199,12 +200,16 @@ public static void createPropertiesTable(List<Property> properties, TcPr tcPr) {
if (tcPr.getShd()!=null) {
properties.add(new Shading(tcPr.getShd()));
}
if (tcPr.getTextDirection() != null ) {
properties.add(new TextDir(tcPr.getTextDirection()));
}

}

public static void createProperties(List<Property> properties, TcPr tcPr) {
createPropertiesTable(properties, tcPr);

// Why is this done here rather than in createPropertiesTable (or vice versa)?
if (tcPr.getTcMar() != null) {
TcMar tcMar = tcPr.getTcMar();
if (tcMar.getTop() != null)
Expand All @@ -216,6 +221,7 @@ public static void createProperties(List<Property> properties, TcPr tcPr) {
if (tcMar.getRight() != null)
properties.add(new CellMarginRight(tcMar.getRight()));
}

}

public static List<Property> createProperties(OpcPackage wmlPackage, RPr rPr) {
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/org/docx4j/model/properties/table/tc/TextDir.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2018, Plutext Pty Ltd.
*
* This file is part of docx4j.
docx4j is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.docx4j.model.properties.table.tc;

import org.docx4j.jaxb.Context;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.STVerticalJc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TextDirection;
import org.w3c.dom.Element;
import org.w3c.dom.css.CSSValue;

/**
* @since 3.4.0
*/
public class TextDir extends AbstractTcProperty {

// see http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/ST_TextDirection.html

public final static String CSS_NAME = "transform"; // eg transform: rotate(90deg);
public final static String FO_NAME = "reference-orientation";

private final static String DEFAULT_LEFT_TO_RIGHT = "lrTb";


public String getCssName() {
return CSS_NAME;
}

public TextDir() {
TextDirection textDirection = Context.getWmlObjectFactory().createTextDirection();
textDirection.setVal(DEFAULT_LEFT_TO_RIGHT);
this.setObject(textDirection);
}

public TextDir(org.docx4j.wml.TextDirection textDirection) {
this.setObject(textDirection);
}

public TextDir(CSSValue value) {

debug(CSS_NAME, value);

TextDirection textDirection = Context.getWmlObjectFactory().createTextDirection();

if (value.getCssText().toLowerCase().equals("rotate(0deg)")) {
textDirection.setVal(DEFAULT_LEFT_TO_RIGHT);
} else if (value.getCssText().toLowerCase().equals("rotate(270deg)")) {
textDirection.setVal("btLr");
} else if (value.getCssText().toLowerCase().equals("rotate(90deg)")) {
textDirection.setVal("tbRl");
} else {
log.warn("How to handle transform: " + value.getCssText());
textDirection.setVal(DEFAULT_LEFT_TO_RIGHT);
}

this.setObject( textDirection );
}

@Override
public String getCssProperty() {

String val = ((TextDirection)this.getObject()).getVal();
if (("btLr").equals(val) ) {
return composeCss(CSS_NAME, "rotate(270deg)");

} else if (("lrTb").equals(val) ) {
return composeCss(CSS_NAME, "rotate(0deg)");
} else if (("lrTbV").equals(val) ) {
// TODO
return composeCss(CSS_NAME, "rotate(0deg)");
} else if (("tbLrV").equals(val) ) {
// TODO
return composeCss(CSS_NAME, "rotate(270deg)");

} else if (("tbRl").equals(val) ) {
return composeCss(CSS_NAME, "rotate(90deg)");

} else if (("btLrl").equals(val) ) {
return composeCss(CSS_NAME, "rotate(270deg)");
} else {
log.warn("How to handle TextDirection of " + val);
return CSS_NULL;
}
}


@Override
public void setXslFO(Element foElement) {

String val = ((TextDirection)this.getObject()).getVal();
if (("btLr").equals(val) ) {
foElement.setAttribute(FO_NAME, "90");
} else if (("lrTb").equals(val) ) {
foElement.setAttribute(FO_NAME, "0");
} else if (("lrTbV").equals(val) ) {
// TODO
foElement.setAttribute(FO_NAME, "0");
} else if (("tbLrV").equals(val) ) {
// TODO
foElement.setAttribute(FO_NAME, "90");
} else if (("tbRl").equals(val) ) {
foElement.setAttribute(FO_NAME, "-90");
} else if (("tbRl").equals(val) ) {
foElement.setAttribute(FO_NAME, "-90");
} else {
log.warn("How to handle TextDirection of " + val);
}

}

@Override
public void set(TcPr tcPr) {
tcPr.setTextDirection( (TextDirection)this.getObject() );
}

}

0 comments on commit d8254cb

Please sign in to comment.