Skip to content

Commit

Permalink
Merge pull request #171 from AY2223S2-CS2103-W17-1/gwyneth-code-quality
Browse files Browse the repository at this point in the history
Improve code quality in PdfConverter.java
  • Loading branch information
toh-xinyi authored Apr 7, 2023
2 parents ee505e0 + e39880b commit dc6eaf8
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 deletions src/main/java/seedu/address/model/PdfConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ public class PdfConverter {

private PDPageContentStream contentStream;

/**
* Exports {@code key}'s task and score list in the form of a PDF file from this {@code Mathutoring}.
*/
public PDDocument exportProgress(Student key) throws IOException {
requireAllNonNull(key);
public void setup() throws IOException {
this.document = new PDDocument();
this.page = new PDPage();
this.document.addPage(page);
this.contentStream = new PDPageContentStream(document, page);
this.x = this.xInit;
this.y = this.yInit;
this.curColor = black;
}

/**
* Create contents of the pdf file
* @param key student to be exported
*/
public void createContents(Student key) throws IOException {
String docTitle = key.getName().fullName + "'s Progress Report";
String dateCreated = "Date created: " + LocalDate.now();
String taskList = "Task List";
Expand All @@ -104,7 +106,15 @@ public PDDocument exportProgress(Student key) throws IOException {
this.y -= 2 * textHeight(fontBold, 18, 0);

createScoreTable(key.getScoreList());
}

/**
* Exports {@code key}'s task and score list in the form of a PDF file from this {@code Mathutoring}.
*/
public PDDocument exportProgress(Student key) throws IOException {
requireAllNonNull(key);
setup();
createContents(key);
this.contentStream.close();
return this.document;
}
Expand Down Expand Up @@ -177,12 +187,10 @@ public void createTableRow(List<String> headers, List<String> maxContentWidthStr
float wrapCur;
if (i != 0 && i != headers.size() - 1) {
wrapCur = this.horizontalWrap - (this.x - this.xInit) - 4 * this.margin
- textLength(maxContentWidthString.get(i + 1), fontBold,
fontTableHeaderSize);
- textLength(maxContentWidthString.get(i + 1), fontBold, fontTableHeaderSize);
} else {
wrapCur = this.horizontalWrap - (this.x - this.xInit) - 2 * this.margin;
}
logger.info(String.valueOf(wrapCur));
this.x += this.margin;
this.y -= this.margin;
wrapText(headers.get(i), wrapCur, font, fontSize, maxContentWidthString);
Expand Down Expand Up @@ -298,10 +306,13 @@ public void createTableContentForScore(UniqueScoreList scores, List<String> maxC
* @param y initial y-coordinate of the text
* @throws IOException
*/
public void setUpContentStream(PDFont font, int fontSize, float x, float y) throws IOException {
public void setUpContentStream(String curString, PDFont font, int fontSize, float x, float y, Color color)
throws IOException {
this.contentStream.beginText();
this.contentStream.setFont(font, fontSize);
this.contentStream.moveTextPositionByAmount(x, y);
this.contentStream.setNonStrokingColor(color);
drawText(curString, font, fontSize);
}

/**
Expand Down Expand Up @@ -392,6 +403,50 @@ public PDPageContentStream handleNextPage(float yFinal, List<String> maxContentW
return contentStream;
}

/**
* Write text to the pdf
* @param curString current string to be written
* @param font font of the text
* @param fontSize font size of the text
*/
public void drawText(String curString, PDFont font, int fontSize) throws IOException {
this.contentStream.drawString(curString);
this.contentStream.endText();
this.x += textLength(curString, font, fontSize);
}

/**
* Change position to next line
* @param count determines whether the current string is the first line of the string
* @param font font of the text
* @param fontSize font size of the text
* @param xPosition x coordinate of the text
*/
public void handleNextLine(int count, PDFont font, int fontSize, float xPosition) {
if (count != 0) {
this.y -= textHeight(font, fontSize, this.margin / 2);
}
this.x = xPosition;
}

/**
* Handle next page for currently wrapped text
* @param count determines whether the current string is the first line of the string
* @param yPrev y coordinate of the previous line
* @param maxContentWidthString list of strings with the maximum length for each column
* @param font font of the text
* @param fontSize font size of the text
* @param xPosition x coordinate of the text
*/
public void handleWrapNextPage(int count, float yPrev, List<String> maxContentWidthString, PDFont font,
int fontSize, float xPosition) throws IOException {
float yTemp = (count != 0) ? this.y - 2 * this.margin : yPrev - this.margin;
this.contentStream = handleNextPage(yTemp, maxContentWidthString, this.margin, font, fontSize);
this.x = xPosition;
this.y = yInit;
this.yInitTable = this.y + textHeight(font, fontSize, 0) / 2 + 2 * this.margin;
}

/**
* Wraps the string given to fit in the specified wrap length.
* @param text text string
Expand Down Expand Up @@ -420,28 +475,13 @@ public void wrapText(String text, float wrap, PDFont font, int fontSize, List<St
}
float yPrev = this.y;
if (lengthUsed > wrap) {
if (count != 0) {
this.y -= textHeight(font, fontSize, this.margin / 2);
}
this.x = xPosition;
handleNextLine(count, font, fontSize, xPosition);
lengthUsed = textLength(curString, font, fontSize);
}
if (this.y <= 90) {
float yTemp = yPrev - this.margin;
if (count != 0) {
yTemp = this.y - 2 * this.margin;
}
contentStream = handleNextPage(yTemp, maxContentWidthString, margin, font,
fontSize);
this.x = xPosition;
this.y = yInit;
this.yInitTable = this.y + textHeight(font, fontSize, 0) / 2 + 2 * this.margin;
handleWrapNextPage(count, yPrev, maxContentWidthString, font, fontSize, xPosition);
}
setUpContentStream(font, fontSize, this.x, this.y);
this.contentStream.setNonStrokingColor(this.curColor);
this.contentStream.drawString(curString);
this.contentStream.endText();
this.x += textLength(curString, font, fontSize);
setUpContentStream(curString, font, fontSize, this.x, this.y, this.curColor);
count += 1;
}
this.x = xPosition;
Expand Down

0 comments on commit dc6eaf8

Please sign in to comment.