ASF Bugzilla – Attachment 35010 Details for
Bug 60912
Bullets font and color
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Extension of WordToHtmlConverter to avoid decimal font size issue. Details mentioned in comments
CustomWordToHtmlConverter.java (text/x-java), 12.90 KB, created by
pooja.dhannawat535
on 2017-05-29 07:09:07 UTC
(
hide
)
Description:
Extension of WordToHtmlConverter to avoid decimal font size issue. Details mentioned in comments
Filename:
MIME Type:
Creator:
pooja.dhannawat535
Created:
2017-05-29 07:09:07 UTC
Size:
12.90 KB
patch
obsolete
>package docs.conversion.poc.poi.doc; > >import org.apache.poi.hwpf.HWPFDocumentCore; >import org.apache.poi.hwpf.converter.*; >import org.apache.poi.hwpf.model.StyleSheet; >import org.apache.poi.hwpf.usermodel.*; >import org.w3c.dom.Element; >import org.w3c.dom.Text; > >import java.util.Base64; >import java.util.Stack; > >public class CustomWordToHtmlConverter extends WordToHtmlConverter { > > private final Stack<CustomWordToHtmlConverter.BlocksProperties> blocksProperties = new Stack(); > private final HtmlDocumentFacade htmlDocumentFacade; > private AbstractWordUtils.NumberingState numberingState = new AbstractWordUtils.NumberingState(); > > public CustomWordToHtmlConverter(HtmlDocumentFacade document) { > super(document); > this.htmlDocumentFacade = document; > } > > @Override > protected void processImageWithoutPicturesManager(Element currentBlock, > boolean inlined, Picture picture) > { > Element imgNode = currentBlock.getOwnerDocument().createElement("img"); > StringBuilder sb = new StringBuilder(); > sb.append(Base64.getMimeEncoder().encodeToString(picture.getRawContent())); > sb.insert(0, "data:"+picture.getMimeType()+";base64,"); > imgNode.setAttribute("src", sb.toString()); > currentBlock.appendChild(imgNode); > } > > > @Override > protected void processParagraph(HWPFDocumentCore hwpfDocument, Element parentElement, int currentTableLevel, Paragraph paragraph, String bulletText) { > Element pElement = this.htmlDocumentFacade.createParagraph(); > parentElement.appendChild(pElement); > StringBuilder style = new StringBuilder(); > WordToHtmlUtils.addParagraphProperties(paragraph, style); > int charRuns = paragraph.numCharacterRuns(); > if(charRuns != 0) { > CharacterRun nextStop = paragraph.getCharacterRun(0); > String textNode; > float firstLinePosition; > if(nextStop != null) { > FontReplacer.Triplet spanMinWidth = this.getCharacterRunTriplet(nextStop); > firstLinePosition = nextStop.getFontSize() / 2.0F; > textNode = spanMinWidth.fontName; > CustomWordToHtmlUtils.addFontFamily(textNode, style); > CustomWordToHtmlUtils.addFontSize(firstLinePosition, style); > } else { > firstLinePosition = -1; > textNode = ""; > } > > this.blocksProperties.push(new CustomWordToHtmlConverter.BlocksProperties(textNode, firstLinePosition)); > > try { > if(CustomWordToHtmlUtils.isNotEmpty(bulletText)) { > > if(bulletText.endsWith("\t")) { > // Replace bullet with dot (in-case of unordered list) > if(!bulletText.matches(".*[a-zA-Z0-9]+.*")){ > //Replace ANSI characters with UTF-8 > bulletText = bulletText.replace("\uF0B7", "\u2022"); > bulletText = bulletText.replace("\u006F", "\u00A0\u00A0\u26AA"); > bulletText = bulletText.replace("\uF0A7", "\u00A0\u00A0\u00A0\u00A0\u25AA"); > } > float firstLinePosition1 = (float)(paragraph.getIndentFromLeft() + paragraph.getFirstLineIndent() + 20); > float nextStop1 = (float)(Math.ceil((double)(firstLinePosition1 / 720.0F)) * 720.0D); > float spanMinWidth1 = nextStop1 - firstLinePosition1; > Element span = this.htmlDocumentFacade.getDocument().createElement("span"); > String spanStyle ="display: inline-block; text-indent: 0; min-width: " + spanMinWidth1 / 1440.0F + "in;"; > this.htmlDocumentFacade.addStyleClass(span, "s", spanStyle); > pElement.appendChild(span); > Text textNode1 = this.htmlDocumentFacade.createText(bulletText + '\u200b' + 'Â '); > span.appendChild(textNode1); > } else { > Text textNode3 = this.htmlDocumentFacade.createText(bulletText.substring(0, bulletText.length() - 1)); > pElement.appendChild(textNode3); > } > } > this.processCharacters(hwpfDocument, currentTableLevel, paragraph, pElement); > } finally { > this.blocksProperties.pop(); > } > > if(style.length() > 0) { > this.htmlDocumentFacade.addStyleClass(pElement, "p", style.toString()); > } > CustomWordToHtmlUtils.compactSpans(pElement); > } > } > > protected void processParagraph(HWPFDocumentCore hwpfDocument, Element parentElement, int currentTableLevel, > Paragraph paragraph, String bulletText, int bulletColor,double bulletFont) { > > Element pElement = this.htmlDocumentFacade.createParagraph(); > parentElement.appendChild(pElement); > StringBuilder style = new StringBuilder(); > WordToHtmlUtils.addParagraphProperties(paragraph, style); > int charRuns = paragraph.numCharacterRuns(); > if(charRuns != 0) { > CharacterRun nextStop = paragraph.getCharacterRun(0); > String textNode; > float firstLinePosition; > if(nextStop != null) { > FontReplacer.Triplet spanMinWidth = this.getCharacterRunTriplet(nextStop); > firstLinePosition = nextStop.getFontSize() / 2.0F; > textNode = spanMinWidth.fontName; > CustomWordToHtmlUtils.addFontFamily(textNode, style); > CustomWordToHtmlUtils.addFontSize(firstLinePosition, style); > } else { > firstLinePosition = -1; > textNode = ""; > } > > this.blocksProperties.push(new CustomWordToHtmlConverter.BlocksProperties(textNode, firstLinePosition)); > > try { > if(CustomWordToHtmlUtils.isNotEmpty(bulletText)) { > > if(bulletText.endsWith("\t")) { > // Replace bullet with dot (in-case of unordered list) > if(!bulletText.matches(".*[a-zA-Z0-9]+.*")){ > /* // Replace ANSI characters with UTF-8 > bulletText = bulletText.replace("\uF0B7", "\u2022"); > bulletText = bulletText.replace("\u006F", "\u00A0\u00A0\u26AA"); > bulletText = bulletText.replace("\uF0A7", "\u00A0\u00A0\u00A0\u00A0\u25AA");*/ > bulletText = "\u2022"; > } > float firstLinePosition1 = (float)(paragraph.getIndentFromLeft() + paragraph.getFirstLineIndent() + 20); > float nextStop1 = (float)(Math.ceil((double)(firstLinePosition1 / 720.0F)) * 720.0D); > float spanMinWidth1 = nextStop1 - firstLinePosition1; > Element span = this.htmlDocumentFacade.getDocument().createElement("span"); > String spanStyle ="display: inline-block; text-indent: 0; min-width: " + spanMinWidth1 / 1440.0F + "in;"; > spanStyle = spanStyle.concat("font-size:" + bulletFont/2.0F + "pt;"); > //spanStyle = spanStyle.concat("color:" + CustomWordToHtmlUtils.getColor(bulletColor) + ";"); > this.htmlDocumentFacade.addStyleClass(span, "s", spanStyle); > pElement.appendChild(span); > Text textNode1 = this.htmlDocumentFacade.createText(bulletText + '\u200b' + 'Â '); > span.appendChild(textNode1); > } else { > Text textNode3 = this.htmlDocumentFacade.createText(bulletText.substring(0, bulletText.length() - 1)); > pElement.appendChild(textNode3); > } > } > this.processCharacters(hwpfDocument, currentTableLevel, paragraph, pElement); > } finally { > this.blocksProperties.pop(); > } > > if(style.length() > 0) { > this.htmlDocumentFacade.addStyleClass(pElement, "p", style.toString()); > } > CustomWordToHtmlUtils.compactSpans(pElement); > } > } > > @Override > protected void processParagraphes(HWPFDocumentCore wordDocument, Element flow, Range range, int currentTableLevel) { > int paragraphs = range.numParagraphs(); > for(int p = 0; p < paragraphs; ++p) { > Paragraph paragraph = range.getParagraph(p); > if(paragraph.isInTable() && paragraph.getTableLevel() != currentTableLevel) { > if(paragraph.getTableLevel() < currentTableLevel) { > throw new IllegalStateException("Trying to process table cell with higher level (" + paragraph.getTableLevel() + ") than current table level (" + currentTableLevel + ") as inner table part"); > } > > Table var12 = range.getTable(paragraph); > this.processTable(wordDocument, flow, var12); > p += var12.numParagraphs(); > --p; > } else { > if(paragraph.text().equals("\f")) { > this.processPageBreak(wordDocument, flow); > } > > boolean processed = false; > if(paragraph.isInList()) { > try { > HWPFList exc = paragraph.getList(); > StyleSheet styleSheet = wordDocument.getStyleSheet(); > String label = AbstractWordUtils.getBulletText(this.numberingState, exc, (char) paragraph.getIlvl()); > /** > * TODO > * Code for bullet size and color > */ > CharacterProperties bulletProperties = getBulletProperties(styleSheet); > if(null != bulletProperties){ > this.processParagraph(wordDocument, flow, currentTableLevel, paragraph, > label, styleSheet.getStyleDescription(paragraph.getStyleIndex()).getCHP().getColor(), > bulletProperties.getFontSize()); > }else{ > this.processParagraph(wordDocument, flow, currentTableLevel, paragraph, > label); > } > processed = true; > } catch (Exception var11) { > //this.log.log(5, "Can\'t process paragraph as list entry, will be processed without list information", var11); > } > } > > if(!processed) { > this.processParagraph(wordDocument, flow, currentTableLevel, paragraph, ""); > } > } > } > > } > > private CharacterProperties getBulletProperties(StyleSheet styleSheet) { > for(int i=0 ; i< styleSheet.numStyles(); i++){ > if(styleSheet.getStyleDescription(i) != null && styleSheet.getStyleDescription(i).getName().contains("Bullets")){ > return styleSheet.getStyleDescription(i).getCHP(); > } > } > return null; > } > > protected void outputCharacters(Element pElement, CharacterRun characterRun, String text) { > Element span = this.htmlDocumentFacade.getDocument().createElement("span"); > pElement.appendChild(span); > StringBuilder style = new StringBuilder(); > CustomWordToHtmlConverter.BlocksProperties blockProperies = this.blocksProperties.peek(); > FontReplacer.Triplet triplet = this.getCharacterRunTriplet(characterRun); > if(CustomWordToHtmlUtils.isNotEmpty(triplet.fontName) && !CustomWordToHtmlUtils.equals(triplet.fontName, blockProperies.pFontName)) { > style.append("font-family:" + triplet.fontName + ";"); > } > > if((float)characterRun.getFontSize() / 2.0F != blockProperies.pFontSize) { > style.append("font-size:" + characterRun.getFontSize() / 2.0F + "pt;"); > } > > if(characterRun.isHighlighted()) { > style.append("background-color:" + CustomWordToHtmlUtils.getColor(characterRun.getHighlightedColor()) + ";"); > } > > if(triplet.bold) { > style.append("font-weight:bold;"); > } > > if(triplet.italic) { > style.append("font-style:italic;"); > } > > CustomWordToHtmlUtils.addCharactersProperties(characterRun, style); > if(style.length() != 0) { > this.htmlDocumentFacade.addStyleClass(span, "s", style.toString()); > } > > Text textNode = this.htmlDocumentFacade.createText(text); > span.appendChild(textNode); > } > > private static class BlocksProperties { > final String pFontName; > final float pFontSize; > > public BlocksProperties(String pFontName, float pFontSize) { > this.pFontName = pFontName; > this.pFontSize = pFontSize; > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 60912
: 35010