Skip to content

- fix style text issue. underline, superscript, subscript, strikethro… #7

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

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions pdfreporter-android/src/org/oss/pdfreporter/pdf/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,9 @@ public void setCharacterSpacing(float spacing) {
delegate.setCharacterSpacing(spacing);

}
@Override
public void setTextRise(float rise) {
delegate.setTextRise(rise);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@
import org.oss.pdfreporter.engine.type.RotationEnum;
import org.oss.pdfreporter.engine.util.JRPdfaIccProfileNotFoundException;
import org.oss.pdfreporter.engine.util.JRStyledText;
import org.oss.pdfreporter.font.IFontManager;
import org.oss.pdfreporter.font.IFont.FontDecoration;
import org.oss.pdfreporter.font.IFont.FontStyle;
import org.oss.pdfreporter.font.IFontManager;
import org.oss.pdfreporter.font.factory.IFontFactory;
import org.oss.pdfreporter.font.text.ITextLayout;
import org.oss.pdfreporter.font.text.TextAttribute;
import org.oss.pdfreporter.geometry.IAffineTransformMatrix;
import org.oss.pdfreporter.geometry.IColor;
Expand All @@ -84,13 +85,13 @@
import org.oss.pdfreporter.image.IImage;
import org.oss.pdfreporter.pdf.DocumentException;
import org.oss.pdfreporter.pdf.IDocument;
import org.oss.pdfreporter.pdf.IEncryption;
import org.oss.pdfreporter.pdf.IPage;
import org.oss.pdfreporter.pdf.ParagraphRenderer;
import org.oss.pdfreporter.pdf.IDocument.ConformanceLevel;
import org.oss.pdfreporter.pdf.IDocument.PageOrientation;
import org.oss.pdfreporter.pdf.IEncryption;
import org.oss.pdfreporter.pdf.IPage;
import org.oss.pdfreporter.pdf.IPage.LineCap;
import org.oss.pdfreporter.pdf.IPage.ScaleMode;
import org.oss.pdfreporter.pdf.ParagraphRenderer;
import org.oss.pdfreporter.registry.ApiRegistry;
import org.oss.pdfreporter.text.HorizontalAlignment;
import org.oss.pdfreporter.text.IPositionedLine;
Expand Down Expand Up @@ -1909,12 +1910,16 @@ private static org.oss.pdfreporter.font.IFont toPdfFont(JRFont font, boolean und
return fontManager.getModifiedFont(pdfFont, font.getFontSize() * fontSizeScale, decoration);
}

static void drawParagraph(IPage aPdfPage, Paragraph paragraph, float llx, float lly, float urx, float ury, float leading, HorizontalAlignment alignment) {
static void drawParagraph(IPage aPdfPage, Paragraph paragraph, float llx, float lly, float urx, float ury, float leading, HorizontalAlignment alignment) {
drawParagraph(aPdfPage, paragraph, llx, lly, urx, ury, leading, null, alignment);
}

static void drawParagraph(IPage aPdfPage, Paragraph paragraph, float llx, float lly, float urx, float ury, float leading, ITextLayout textLayout, HorizontalAlignment alignment) {
logger.finest("drawParagraph('" + paragraph.getText() + "', " + llx + ", " + lly + ", " + urx + ", " + ury + ", " + leading + ", " + alignment);
logger.finest("Pos(x=" + (int)llx + ", y=" + (int)lly + ", width=" + (int)(urx - llx) + ", height=" + (int)(lly - ury));
float verticalAlignmentHack = paragraph.getFirstParagraphText().getFont().getSize();
IRectangle rect = ApiRegistry.getGeometryFactory().newRectangle((int)llx, (int)(lly - verticalAlignmentHack), (int)(urx - llx), (int)(lly - ury));
ParagraphRenderer renderer = new ParagraphRenderer(paragraph, alignment, rect);
ParagraphRenderer renderer = new ParagraphRenderer(paragraph, alignment, rect, textLayout);
renderer.render(aPdfPage, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.oss.pdfreporter.engine.JRPropertiesUtil;
import org.oss.pdfreporter.engine.JasperReportsContext;
import org.oss.pdfreporter.engine.util.JRStyledText;
import org.oss.pdfreporter.font.text.ITextLayout;


/**
Expand Down Expand Up @@ -74,6 +75,7 @@ public void draw()
TabSegment segment = segments.get(segmentIndex);

float advance = segment.layout.getAdvance();
ITextLayout textLayout = segment.layout;

JRPdfExporter.drawParagraph(pdfPage,
pdfExporter.getParagraph(segment.as, segment.text, text),
Expand All @@ -94,6 +96,7 @@ public void draw()
-400//+ lineHeight//FIXMETAB
- drawPosY,
0,//text.getLineSpacingFactor(),// * text.getFont().getSize(),
textLayout,
horizontalAlignment
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ public interface ITextLayout {
*/
Paragraph getParagraph();

float getAvailableWidth();
}
2 changes: 2 additions & 0 deletions pdfreporter-core/src/org/oss/pdfreporter/pdf/IPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,6 @@ public enum ScaleMode {
*/
void drawCropped(IImage image, float xoffset, float yoffset, float x, float y, float width, float height) throws DocumentException;

void setTextRise(float rise);

}
157 changes: 154 additions & 3 deletions pdfreporter-core/src/org/oss/pdfreporter/pdf/ParagraphRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.oss.pdfreporter.font.IFont;
import org.oss.pdfreporter.font.IFont.FontDecoration;
import org.oss.pdfreporter.font.text.ITextLayout;
import org.oss.pdfreporter.geometry.IRectangle;
import org.oss.pdfreporter.text.HorizontalAlignment;
import org.oss.pdfreporter.text.IPositionedLine;
import org.oss.pdfreporter.text.Paragraph;
import org.oss.pdfreporter.text.ParagraphText;


public class ParagraphRenderer {
private static float LEADING_FACTOR = 1.25f;
private final Paragraph paragraph;
Expand All @@ -27,6 +31,7 @@ public class ParagraphRenderer {
private final List<ParagraphText> textLine;
private float leading;
private float y, widthLeft;
private ITextLayout textLayout;

public ParagraphRenderer(Paragraph paragraph,
HorizontalAlignment alignment, IRectangle bounding) {
Expand All @@ -37,6 +42,16 @@ public ParagraphRenderer(Paragraph paragraph,
this.textLine = new ArrayList<ParagraphText>();
}

public ParagraphRenderer(Paragraph paragraph,
HorizontalAlignment alignment, IRectangle bounding, ITextLayout textLayout) {
super();
this.paragraph = paragraph;
this.alignment = alignment;
this.bounding = bounding;
this.textLine = new ArrayList<ParagraphText>();
this.textLayout = textLayout;
}

public void render(IPage page, boolean wordwrap) {
page.beginText();
leading = 0;
Expand Down Expand Up @@ -76,6 +91,8 @@ private void add(ParagraphText text) {

private void renderLine(IPage page) {
float x;
int spaceToAdd = 0;
float textLineLengthJustify = 0f;
switch (alignment) {
case ALIGN_LEFT:
x = bounding.getX();
Expand All @@ -87,16 +104,150 @@ private void renderLine(IPage page) {
x = bounding.getX() + widthLeft / 2;
break;
case ALIGN_JUSTIFY:
// TODO adjust word and character spacing to consume widthLeft
// adjust word and character spacing to consume widthLeft
// How is this done best with different fonts and sizes ?
x = bounding.getX();
if (textLayout != null) {
try {
float spaceSize = textLayout.getAdvance()-textLayout.getVisibleAdvance();
// System.out.println("spaceSize:"+spaceSize);
float extraWidthToOffSet = textLayout.getAvailableWidth()-bounding.getWidth();
/** SKNG : calculate the extra space to add to fill up the blank **/
spaceToAdd = (int) (extraWidthToOffSet/spaceSize);
if (extraWidthToOffSet%spaceSize > spaceSize*0.75) {
// System.out.println("remaining : "+extraWidthToOffSet%spaceWidthConstantForCarlito+", spaceWidthConstantForCarlito*0.75 : "+spaceWidthConstantForCarlito*0.75);
spaceToAdd++;
}
} catch (Exception e) {
// anything happen, no justify.
System.out.println("Error in calculating spaceToAdd!!! "+e.toString());
spaceToAdd = 0;
}
textLineLengthJustify = textLayout.getAvailableWidth();
}
break;
default:
x = bounding.getX();
}
for (ParagraphText text : textLine) {
// TODO render background
float textLineLength = text.getWidth();
if (textLineLengthJustify > 0f) {
textLineLength = textLineLengthJustify;
}
page.setFont(text.getFont());
page.setRGBColorFill(text.getForeground());
page.setTextPos(x, y);
page.textOut(text.getText());
/** SKNG : reset the text rose for supsubscript **/
page.setTextRise(0f);
StringBuilder sb = new StringBuilder();
/** SKNG : add some space to fill the gap **/
if (spaceToAdd > 0) {
// if the last char is a space.
if (text.getText().endsWith(" ")) {
spaceToAdd++;
}
// System.out.println("text : "+text.getText());
StringTokenizer st = new StringTokenizer(text.getText());
int tokens = st.countTokens()-1;
// System.out.println("space tokens found:"+tokens);
int multiplier = 1;
int spaceToAddWithMultiplier = spaceToAdd;
if (spaceToAdd > tokens) {
multiplier += (int) spaceToAdd/tokens;
spaceToAddWithMultiplier = spaceToAdd % tokens;
}
for (char c : text.getText().toCharArray()) {
sb.append(c);
if (' ' == c && spaceToAdd > 0) {
if (multiplier>1 && spaceToAddWithMultiplier>0) {
// System.out.println("[adding space] multiplier:"+multiplier);
for (int times=0; times<multiplier; times++) {
// System.out.println("adding "+(times+1)+" space...");
sb.append(c);
spaceToAdd--;
}
spaceToAddWithMultiplier--;
} else if (multiplier>1) {
int remainderMultiplier = multiplier -1;
for (int times=0; times<remainderMultiplier; times++) {
// System.out.println("adding "+(times+1)+" space...");
sb.append(c);
spaceToAdd--;
}
} else {
sb.append(c);
spaceToAdd--;
}
}
}
} else {
sb.append(text.getText());
}
// System.out.println("text:"+sb.toString());

/** SKNG : decorated font line here... **/
IFont font = text.getFont();
if (font!=null) {
IPage tempPage = page;

/** ParagraphText doesn't support multiple fontDecoration yet!! **/
FontDecoration fontDecor = font.getDecoration();
if (fontDecor!=null) {
switch (fontDecor) {
case UNDERLINE :
System.out.println("UNDERLINE");
tempPage.textOut(sb.toString());
IPositionedLine line = text.getLine();
float position = line.getPosition();
float lineWidth = line.getThikness();

tempPage.setLineWidth(lineWidth);
tempPage.setLineCap(IPage.LineCap.BUTT_END);
// follow the text color
tempPage.setRGBColorStroke(text.getForeground());
tempPage.setLineDash(null,0);
tempPage.moveTo(x, y+position);
tempPage.lineTo(x+textLineLength, y+position);
tempPage.stroke();
/** TODO : SKNG : don't break, multiple decoration allow in the same line **/
break;
case STRIKE_THROUGH:
System.out.println("STRIKE_THROUGH");
tempPage.textOut(sb.toString());
IPositionedLine lineST = text.getLine();
float positionST = lineST.getPosition();
float lineWidthST = lineST.getThikness();

tempPage.setLineWidth(lineWidthST);
tempPage.setLineCap(IPage.LineCap.BUTT_END);
// follow the text color
tempPage.setRGBColorStroke(text.getForeground());
tempPage.setLineDash(null,0);
tempPage.moveTo(x, y+positionST);
tempPage.lineTo(x+textLineLength, y+positionST);
tempPage.stroke();
break;
case SUPERSCRIPT :
System.out.println("SUPERSCRIPT");
tempPage.setTextRise(font.getSize()/2);
tempPage.textOut(sb.toString());
case SUBSCRIPT :
System.out.println("SUBSCRIPT");
tempPage.setTextRise(-font.getSize()/4);
tempPage.textOut(sb.toString());
break;
case NONE:
tempPage.textOut(sb.toString());
break;
}
}
}
/** **/
else {
page.textOut(sb.toString());
}

x += text.getWidth();
}
y -= leading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ public String toString() {
+ getParagraph().getText() + "']";
}

@Override
public float getAvailableWidth() {
// TODO Auto-generated method stub
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,9 @@ public void setCharacterSpacing(float spacing) {
delegate.setCharacterSpacing(spacing);

}
@Override
public void setTextRise(float rise) {
delegate.setTextRise(rise);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TextLayout implements ITextLayout {
private Float ascent = null;
private Float descent = null;
private Float leading = null;
private float availableWidth = 0f;

TextLayout(Paragraph paragraph, int characterCount) {
this.paragraph = paragraph;
Expand All @@ -31,6 +32,7 @@ public class TextLayout implements ITextLayout {

@Override
public ITextLayout getJustifiedLayout(float justificationWidth) {
this.availableWidth = justificationWidth;
return this;
}

Expand Down Expand Up @@ -124,5 +126,10 @@ private static String invisibleCharsAtEnd(String text) {
return text.substring(begin, text.length());
}

@Override
public float getAvailableWidth() {
return availableWidth;
}


}