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

RIGHT and CENTER alignment in tabs #193

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import fr.opensagres.xdocreport.itext.extension.ExtendedPdfPTable;
import fr.opensagres.xdocreport.itext.extension.IITextContainer;
import fr.opensagres.xdocreport.itext.extension.font.FontGroup;
import java.util.ArrayList;

public class PdfMapper extends
XWPFDocumentVisitor<IITextContainer, PdfOptions, StylableMasterPage> {
Expand Down Expand Up @@ -589,15 +590,41 @@ private boolean isFontExists(Font font) {
return font != null && font.getBaseFont() != null;
}

private InternalTabHandler tabHandler;

@Override
protected void visitText(CTText docxText, boolean pageNumber,
IITextContainer pdfParagraphContainer) throws Exception {
Font font = currentRunFontAscii;
Font fontAsian = currentRunFontEastAsia;
Font fontComplex = currentRunFontHAnsi;
createAndAddChunks(pdfParagraphContainer, docxText.getStringValue(),
currentRunUnderlinePatterns, currentRunBackgroundColor,
pageNumber, font, fontAsian, fontComplex);

//because currentRunX is updated while creating the chunks
Float initialRunX = currentRunX;
currentRunX = 0f;

List<Chunk> chunks = createTextChunks(docxText.getStringValue(),
currentRunUnderlinePatterns, currentRunBackgroundColor, pageNumber,
font, fontAsian, fontComplex);

if(tabHandler!=null) {
float chunkWidth = currentRunX;
float startPoint = tabHandler.getStartPosition(chunkWidth);
if(currentRunX != null) {
currentRunX = startPoint + chunkWidth;
}

if(initialRunX == null || startPoint > initialRunX) {
VerticalPositionMark vpm = createVerticalPositionMark(tabHandler.currentTabLeader);
pdfParagraphContainer.addElement(new Chunk(vpm, startPoint));
}

tabHandler = null;
}

for(Chunk chunk : chunks) {
pdfParagraphContainer.addElement(chunk);
}
}

private Chunk createTextChunk(String text, boolean pageNumber,
Expand Down Expand Up @@ -641,10 +668,11 @@ private Chunk createTextChunk(String text, boolean pageNumber,
}
return textChunk;
}

private void createAndAddChunks(IITextContainer parent, String textContent,
UnderlinePatterns underlinePatterns, Color backgroundColor,
boolean pageNumber, Font font, Font fontAsian, Font fontComplex) {

private List<Chunk> createTextChunks(String textContent, UnderlinePatterns underlinePatterns,
Color backgroundColor,boolean pageNumber, Font font, Font fontAsian,
Font fontComplex) {
List<Chunk> chunks = new ArrayList<Chunk>(8);
StringBuilder sbuf = new StringBuilder();
FontGroup currentGroup = FontGroup.WESTERN;
for (int i = 0; i < textContent.length(); i++) {
Expand All @@ -660,7 +688,7 @@ private void createAndAddChunks(IITextContainer parent, String textContent,
currentGroup);
Chunk chunk = createTextChunk(sbuf.toString(), pageNumber,
chunkFont, underlinePatterns, backgroundColor);
parent.addElement(chunk);
chunks.add(chunk);
// start new chunk
sbuf.setLength(0);
sbuf.append(ch);
Expand All @@ -671,7 +699,8 @@ private void createAndAddChunks(IITextContainer parent, String textContent,
Font chunkFont = getFont(font, fontAsian, fontComplex, currentGroup);
Chunk chunk = createTextChunk(sbuf.toString(), pageNumber, chunkFont,
underlinePatterns, backgroundColor);
parent.addElement(chunk);
chunks.add(chunk);
return chunks;
}

private Font getFont(Font font, Font fontAsian, Font fontComplex,
Expand Down Expand Up @@ -723,6 +752,10 @@ protected void visitTabs(CTTabs tabs, IITextContainer pdfParagraphContainer)
currentRunX += chunk.getWidthPoint();
}
} else {
if (tabHandler != null) {
currentRunX = tabHandler.currentTabPosition;
}

if (currentRunX >= pdfDocument.getPageWidth()) {
currentRunX = 0f;
}
Expand Down Expand Up @@ -761,22 +794,20 @@ protected void visitTabs(CTTabs tabs, IITextContainer pdfParagraphContainer)
}

if (tabPosition != null) {
currentRunX = tabPosition;
// tab leader : Specifies the character which shall be used to fill
// in the space created by a tab
// which
// ends
// at this custom tab stop. This character shall be repeated as
// required to completely fill the
// tab spacing generated by the tab character.
VerticalPositionMark mark = createVerticalPositionMark(tabLeader);
Chunk pdfTab = null;
if (STTabJc.RIGHT.equals(tabVal)) {
pdfTab = new Chunk(mark);
} else {
pdfTab = new Chunk(mark, currentRunX);
}
pdfParagraphContainer.addElement(pdfTab);

tabHandler = new InternalTabHandler();
tabHandler.currentTabLeader = tabLeader;
tabHandler.currentTabPosition = tabPosition;
tabHandler.currentTabVal = tabVal;
tabHandler.initialRunX = currentRunX;
}
}

Expand Down Expand Up @@ -844,33 +875,9 @@ private CTTabStop getTabStop(List<CTTabStop> tabList) {
}

private boolean canApplyTabStop(CTTabStop tabStop) {
if (tabStop.getVal().equals(STTabJc.LEFT)) {

if (currentRunX < DxaUtil.dxa2points(tabStop.getPos().floatValue())) {
return true;
}
} else if (tabStop.getVal().equals(STTabJc.RIGHT)) {
if (isWordDocumentPartParsing()) {
if (pdfDocument.getWidthLimit()
- (currentRunX + DxaUtil.dxa2points(tabStop.getPos()
.floatValue())) <= 0) {
return true;
}
} else {
if (currentPageWidth == null) {
return true;
}
if (currentPageWidth.floatValue()
- (currentRunX + DxaUtil.dxa2points(tabStop.getPos()
.floatValue())) <= 0) {
return true;
}
}

} else if (tabStop.getVal().equals(STTabJc.CENTER)) {

}
return false;
float tabPosition = tabStop.getPos().floatValue();
float tabPositionPoints = DxaUtil.dxa2points(tabPosition);
return tabPositionPoints > currentRunX;
}

private boolean isClearTab(CTTabStop tabStop) {
Expand Down Expand Up @@ -1282,4 +1289,31 @@ public boolean useTotalPageField() {
return totalPageFieldUsed;
}

private static class InternalTabHandler {
float initialRunX;
float currentTabPosition;
STTabTlc.Enum currentTabLeader;
STTabJc.Enum currentTabVal;

public float getStartPosition(float chunkWidth) {
if(currentTabVal == STTabJc.LEFT) {
return currentTabPosition;
} else if(currentTabVal == STTabJc.RIGHT) {
float startPoint = currentTabPosition - chunkWidth;
if(startPoint < initialRunX) {
return initialRunX;
}
return startPoint;
} else if(currentTabVal == STTabJc.CENTER) {
float startPoint = currentTabPosition - chunkWidth/2.0f;
if(startPoint < initialRunX) {
return initialRunX;
}
return startPoint;
} else {
//TODO add support for other tabulation types
return currentTabPosition;
}
}
}
}
Binary file not shown.