Skip to content

Commit

Permalink
63290 - PPTX To Png changes font sizes and colors
Browse files Browse the repository at this point in the history
various fixes to HSLF
moved line spacing to the following line
refactored PropertyFetcher with lambdas

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1878492 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
kiwiwings committed Jun 4, 2020
1 parent 84330c6 commit 5a18307
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 581 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DrawFontManagerDefault implements DrawFontManager {
public DrawFontManagerDefault() {
knownSymbolFonts.add("Wingdings");
knownSymbolFonts.add("Symbol");
// knownSymbolFonts.add("Monotype Sorts");
}

@Override
Expand Down
19 changes: 11 additions & 8 deletions src/java/org/apache/poi/sl/draw/DrawTextFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more

import java.awt.Graphics2D;
import java.awt.font.TextLayout;
import java.text.*;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.text.CharacterIterator;

public class DrawTextFragment implements Drawable {
final TextLayout layout;
final AttributedString str;
double x, y;

public DrawTextFragment(TextLayout layout, AttributedString str) {
this.layout = layout;
this.str = str;
Expand Down Expand Up @@ -57,20 +59,20 @@ public void applyTransform(Graphics2D graphics) {

public void drawContent(Graphics2D graphics) {
}

public TextLayout getLayout() {
return layout;
}

public AttributedString getAttributedString() {
return str;
}

/**
* @return full height of this text run which is sum of ascent, descent and leading
*/
public float getHeight(){
double h = layout.getAscent() + layout.getDescent() + getLeading();
public float getHeight(){
double h = layout.getAscent() + layout.getDescent();
return (float)h;
}

Expand All @@ -80,14 +82,15 @@ public float getHeight(){
public float getLeading() {
// fix invalid leadings (leading == 0)
double l = layout.getLeading();

if (l == 0) {
// see https://stackoverflow.com/questions/925147
// we use a 115% value instead of the 120% proposed one, as this seems to be closer to LO/OO
l = (layout.getAscent()+layout.getDescent())*0.15;
}
return (float)l;
}

/**
*
* @return width if this text run
Expand Down Expand Up @@ -115,5 +118,5 @@ public String getString(){
public String toString(){
return "[" + getClass().getSimpleName() + "] " + getString();
}

}
143 changes: 74 additions & 69 deletions src/java/org/apache/poi/sl/draw/DrawTextParagraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ public class DrawTextParagraph implements Drawable {
protected String rawText;
protected DrawTextFragment bullet;
protected int autoNbrIdx;

/**
* the highest line in this paragraph. Used for line spacing.
*/
protected double maxLineHeight;
protected boolean firstParagraph = true;

/**
* Defines an attribute used for storing the hyperlink associated with
Expand Down Expand Up @@ -132,9 +128,10 @@ public void draw(Graphics2D graphics){
return;
}

final boolean isHSLF = isHSLF();

double penY = y;

boolean firstLine = true;
int indentLevel = paragraph.getIndentLevel();
Double leftMargin = paragraph.getLeftMargin();
if (leftMargin == null) {
Expand All @@ -145,10 +142,6 @@ public void draw(Graphics2D graphics){
if (indent == null) {
indent = Units.toPoints(347663L*indentLevel);
}
if (isHSLF()) {
// special handling for HSLF
indent -= leftMargin;
}

// Double rightMargin = paragraph.getRightMargin();
// if (rightMargin == null) {
Expand All @@ -161,26 +154,41 @@ public void draw(Graphics2D graphics){
spacing = 100d;
}

DrawTextFragment lastLine = null;
for(DrawTextFragment line : lines){
double penX;

if(firstLine) {

if (!(isFirstParagraph() && lastLine == null)) {
// penY is now on descent line of the last text fragment
// need to substract descent height to get back to the baseline of the last fragment
// then add a multiple of the line height of the current text height
penY -= line.getLeading() + ((lastLine == null) ? 0 : lastLine.getLayout().getDescent());

if(spacing > 0) {
// If linespacing >= 0, then linespacing is a percentage of normal line height.
penY += (spacing*0.01) * line.getHeight(); // + (isHSLF ? line.getLayout().getLeading() : 0));
} else {
// negative value means absolute spacing in points
penY += -spacing;
}
penY -= line.getLayout().getAscent();
}

penX = x + (isHSLF ? leftMargin : leftMargin);
if (lastLine == null) {
if (!isEmptyParagraph()) {
// TODO: find out character style for empty, but bulleted/numbered lines
bullet = getBullet(graphics, line.getAttributedString().getIterator());
}

if (bullet != null){
bullet.setPosition(x+leftMargin+indent, penY);
if (bullet != null) {
bullet.setPosition(isHSLF ? x+indent : x+leftMargin+indent, penY);
bullet.draw(graphics);
// don't let text overlay the bullet and advance by the bullet width
double bulletWidth = bullet.getLayout().getAdvance() + 1;
penX = x + Math.max(leftMargin, leftMargin+indent+bulletWidth);
} else {
penX = x + leftMargin;
penX = x + (isHSLF ? leftMargin : Math.max(leftMargin, leftMargin+indent+bulletWidth));
}
} else {
penX = x + leftMargin;
}

Rectangle2D anchor = DrawShape.getAnchor(graphics, paragraph.getParentShape());
Expand All @@ -207,16 +215,9 @@ public void draw(Graphics2D graphics){

line.setPosition(penX, penY);
line.draw(graphics);
penY += line.getHeight();

if(spacing > 0) {
// If linespacing >= 0, then linespacing is a percentage of normal line height.
penY += spacing*0.01* line.getHeight();
} else {
// negative value means absolute spacing in points
penY += -spacing;
}

firstLine = false;
lastLine = line;
}

y = penY - y;
Expand Down Expand Up @@ -257,7 +258,6 @@ protected void breakText(Graphics2D graphics){
DrawFactory fact = DrawFactory.getInstance(graphics);
StringBuilder text = new StringBuilder();
AttributedString at = getAttributedString(graphics, text);
boolean emptyParagraph = text.toString().trim().isEmpty();

AttributedCharacterIterator it = at.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext());
Expand All @@ -271,42 +271,47 @@ protected void breakText(Graphics2D graphics){
wrappingWidth = 1;
}

int nextBreak = text.indexOf("\n", startIndex + 1);
if (nextBreak == -1) {
nextBreak = it.getEndIndex();
}
// usually "\n" is added after a line, if it occurs before it - only possible as first char -
// we need to add an empty line
TextLayout layout;
int endIndex;
if (startIndex == 0 && text.toString().startsWith("\n")) {
layout = measurer.nextLayout((float) wrappingWidth, 1, false);
endIndex = 1;
} else {
int nextBreak = text.indexOf("\n", startIndex + 1);
if (nextBreak == -1) {
nextBreak = it.getEndIndex();
}

TextLayout layout = measurer.nextLayout((float)wrappingWidth, nextBreak, true);
if (layout == null) {
// layout can be null if the entire word at the current position
// does not fit within the wrapping width. Try with requireNextWord=false.
layout = measurer.nextLayout((float)wrappingWidth, nextBreak, false);
}
layout = measurer.nextLayout((float) wrappingWidth, nextBreak, true);
if (layout == null) {
// layout can be null if the entire word at the current position
// does not fit within the wrapping width. Try with requireNextWord=false.
layout = measurer.nextLayout((float) wrappingWidth, nextBreak, false);
}

if(layout == null) {
// exit if can't break any more
break;
}
if (layout == null) {
// exit if can't break any more
break;
}

int endIndex = measurer.getPosition();
// skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
if(endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n'){
measurer.setPosition(endIndex + 1);
}
endIndex = measurer.getPosition();
// skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
if (endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n') {
measurer.setPosition(endIndex + 1);
}

TextAlign hAlign = paragraph.getTextAlign();
if(hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
layout = layout.getJustifiedLayout((float)wrappingWidth);
TextAlign hAlign = paragraph.getTextAlign();
if (hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
layout = layout.getJustifiedLayout((float) wrappingWidth);
}
}

AttributedString str = (emptyParagraph)
? null // we will not paint empty paragraphs
: new AttributedString(it, startIndex, endIndex);
AttributedString str = new AttributedString(it, startIndex, endIndex);
DrawTextFragment line = fact.getTextFragment(layout, str);
lines.add(line);

maxLineHeight = Math.max(maxLineHeight, line.getHeight());

if(endIndex == it.getEndIndex()) {
break;
}
Expand Down Expand Up @@ -450,6 +455,7 @@ private String tab2space(TextRun tr) {
* @return wrapping width in points
*/
protected double getWrappingWidth(boolean firstLine, Graphics2D graphics){
final long TAB_SIZE = 347663L;
TextShape<?,?> ts = paragraph.getParentShape();

// internal margins for the text box
Expand All @@ -465,11 +471,11 @@ protected double getWrappingWidth(boolean firstLine, Graphics2D graphics){
Double leftMargin = paragraph.getLeftMargin();
if (leftMargin == null) {
// if the marL attribute is omitted, then a value of 347663 is implied
leftMargin = Units.toPoints(347663L*(indentLevel+1));
leftMargin = Units.toPoints(TAB_SIZE * indentLevel);
}
Double indent = paragraph.getIndent();
if (indent == null) {
indent = Units.toPoints(347663L*indentLevel);
indent = 0.;
}
Double rightMargin = paragraph.getRightMargin();
if (rightMargin == null) {
Expand Down Expand Up @@ -503,18 +509,9 @@ protected double getWrappingWidth(boolean firstLine, Graphics2D graphics){
width = anchor.getHeight() - leftInset - rightInset - leftMargin - rightMargin;
break;
}
if (firstLine && !isHSLF()) {
if (bullet != null){
if (indent > 0) {
width -= indent;
}
} else {
if (indent > 0) {
width -= indent; // first line indentation
} else if (indent < 0) { // hanging indentation: the first line start at the left margin
width += leftMargin;
}
}
if (firstLine && bullet == null) {
// indent is usually negative in XSLF
width += isHSLF() ? (leftMargin - indent) : -indent;
}
}

Expand Down Expand Up @@ -727,4 +724,12 @@ private static int nextPart(Font fontMapped, String runText, int beginPart, int
protected boolean isHSLF() {
return DrawShape.isHSLF(paragraph.getParentShape());
}

protected boolean isFirstParagraph() {
return firstParagraph;
}

protected void setFirstParagraph(boolean firstParagraph) {
this.firstParagraph = firstParagraph;
}
}

0 comments on commit 5a18307

Please sign in to comment.