Skip to content

Commit

Permalink
Fixed bug that didn't take into account multi-line paragraphs in a Si…
Browse files Browse the repository at this point in the history
…nglePage.
  • Loading branch information
aiman-al-masoud committed Aug 9, 2021
1 parent 24dec2b commit 1f403a2
Showing 1 changed file with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
Expand Down Expand Up @@ -358,7 +358,6 @@ private String generateImgTag(String path){
}



/**
* Add an image to this Page.
* @param path
Expand Down Expand Up @@ -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()){

Expand Down Expand Up @@ -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<pars.length; i++){
numLinesPerPar[i] = pars[i].split("<br>").length;
if(numLinesPerPar[i]==1){
numLinesPerPar[i] = 2;
}
}

//test log how many lines in each paragraph
for(int i =0; i<numLinesPerPar.length; i++){
Log.d("LINE_NUM", "par "+i+" has: "+numLinesPerPar[i]+" lines");
}

//convert the lineNum to a paragraph num
int accumulLines = 0;
for(int i =0; i<numLinesPerPar.length; i++){
accumulLines += numLinesPerPar[i];
if(lineNum <= accumulLines){
Log.d("LINE_NUM", "line "+lineNum+ " is in paragraph: "+i);
return i;
}
}

return numLinesPerPar.length-1;
}


/**
* Get the html source as a list of paragraphs.
Expand All @@ -453,6 +487,9 @@ private String[] getParagraphs(){
//split the html source by end of paragraph tags
String[] pars = getText().split("</p>");

//remove the last empty "paragraph"
pars = Arrays.copyOf(pars, pars.length-1);

//adjust each paragraph
for(int i =0; i<pars.length; i++){
pars[i] = pars[i].replaceAll("\n", "");
Expand All @@ -471,25 +508,15 @@ private String[] getParagraphs(){
*/
@Override
public void addHtmlTag(int pos, String tag){
//lines are interpreted as paragraphs in html.
//a paragraph is the smallest unit that admits styling.

int lineNum = getLine(pos);
Log.d("LINE_NUM", lineNum+"");

//get the paragraphs
String[] pars = getParagraphs();

//Log.d("LINE_NUM", "PARAGRAPHS:");
//for(int i =0; i<pars.length-1; i++){
// Log.d("LINE_NUM", i+") "+pars[i]);
//}

//convert the line number to the paragraph number.
lineNum = (int)(((double)0.5*lineNum) -0.5);

Log.d("LINE_NUM", "HEEEEEEEEERE");
Log.d("LINE_NUM", pars[lineNum]);
lineNum = lineToParagraph(lineNum);

//make the tags from the inner part
String startTag = "<"+tag+">";
Expand All @@ -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){
Expand All @@ -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];

Expand All @@ -541,7 +567,6 @@ public void removeHtmlTags(int pos){

//save it.
setText(newHtml);

}


Expand Down

0 comments on commit 1f403a2

Please sign in to comment.