From 1f403a262be6c421a50aa27111d69e581103786f Mon Sep 17 00:00:00 2001 From: aiman-al-masoud Date: Mon, 9 Aug 2021 16:43:01 +0200 Subject: [PATCH] Fixed bug that didn't take into account multi-line paragraphs in a SinglePage. --- .../model/classes/SinglePage.java | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/luxlunaris/noadpadlight/model/classes/SinglePage.java b/app/src/main/java/com/luxlunaris/noadpadlight/model/classes/SinglePage.java index 9a5947f..1327dba 100644 --- a/app/src/main/java/com/luxlunaris/noadpadlight/model/classes/SinglePage.java +++ b/app/src/main/java/com/luxlunaris/noadpadlight/model/classes/SinglePage.java @@ -1,7 +1,6 @@ package com.luxlunaris.noadpadlight.model.classes; import android.text.Html; -import android.text.TextUtils; import android.util.Log; import com.luxlunaris.noadpadlight.control.interfaces.PageListener; @@ -12,6 +11,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; /** @@ -358,7 +358,6 @@ private String generateImgTag(String path){ } - /** * Add an image to this Page. * @param path @@ -405,8 +404,6 @@ private void checkDeleteImages(){ //get the html source String text = getText(); - //Log.d( "IMAGE_DEL", imageDir.listFiles().length+" how many imgs"); - //for each image... for(File imgFile : imageDir.listFiles()){ @@ -444,6 +441,43 @@ private int getLine(int pos){ return newLines; } + /** + * Given a line number find the paragraph containing it. + * @param lineNum + * @return + */ + private int lineToParagraph(int lineNum){ + + //get the paragraphs in this Page + String[] pars = getParagraphs(); + + //get the number of lines in each paragraph + int[] numLinesPerPar = new int[pars.length]; + for(int i =0; i").length; + if(numLinesPerPar[i]==1){ + numLinesPerPar[i] = 2; + } + } + + //test log how many lines in each paragraph + for(int i =0; i"); + //remove the last empty "paragraph" + pars = Arrays.copyOf(pars, pars.length-1); + //adjust each paragraph for(int i =0; i"; @@ -498,7 +525,6 @@ public void addHtmlTag(int pos, String tag){ //apply the html tag pars[lineNum] = startTag+pars[lineNum]+endTag; - //re-build the html source from the single paragraphs. String newHtml = ""; for(String par : pars){ @@ -523,7 +549,7 @@ public void removeHtmlTags(int pos){ String[] pars = getParagraphs(); //convert the line number to the paragraph number. - lineNum = (int)(((double)0.5*lineNum) -0.5); + lineNum = lineToParagraph(lineNum); String modifiedPar = pars[lineNum]; @@ -541,7 +567,6 @@ public void removeHtmlTags(int pos){ //save it. setText(newHtml); - }