15

I have to implement Go To Page feature in epub reader. I have try to implement this feature in source code of Page-Turner, but its not working successfully because of multiple xhtml in .epub file, as we know that each chapter have single xhtml file and its divide as per screen size in this app. So whenever screen size is big then total number of pages are less and more number of pages when screen is small, So there is no fix page number where to jump. I have edit and try to implement like giver below.

ReadingFragment.java

public void performSearch(String query) {

    int index = Integer.parseInt(query);

    if (index > bookView.getTotalNumberOfPages()) {
        Toast.makeText(context, "Please enter number between 0 to " + bookView.getTotalNumberOfPages(), Toast.LENGTH_SHORT).show();
    } else {
        bookView.gotoPageNumber(index);
    }
}

BookView.java

public void gotoPageNumber(int pageNum) {
    strategy.gotoPage(pageNum);
    progressUpdate();
}

PageChangeStrategy.java

public void gotoPage(int pageNumber);

FixedPagesStrategy.java

@Override
public void gotoPage(int pageNumber) {

    PageTurnerSpine spinePos = bookView.getSpine();

    this.storedPosition = -1;

    int currentPage = getCurrentPage() + spinePos.getUptoPage(spinePos.getPosition());
    Log.e(TAG, "Adding >> Upto Page : " + spinePos.getUptoPage(spinePos.getPosition())
            + ", currentPage :  " + getCurrentPage());
    Log.e(TAG, "pagenum : " + pageNum);

    if (pageNumber > currentPage) {     //pageNumber is greater then current page

        int jumpSpine = spinePos.getIndexFromPage(pageNumber);
        int currentSpine = spinePos.getPosition();
        Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);

        if (jumpSpine == currentSpine) {

            int diffrence = pageNumber - currentPage;
            Log.e(TAG, "diffrence < : " + diffrence);

            Log.e(TAG, "Minimum >> PageOffSets - 1 : " + (spinePos.getPageOffSets(currentSpine) - 1)
                    + ", pageNum + diffrence : " + (pageNum + diffrence));

            this.pageNum = Math.min(pageNum + diffrence, spinePos.getPageOffSets(currentSpine) - 1);
            updatePosition();

        } else {

            PageTurnerSpine spine = bookView.getSpine();

            if (spine == null || !spine.navigateFrontSpine(spine.getIndexFromPage(pageNumber))) {
                return;
            }
            this.pageNum = 0;

            gotoPage(pageNumber);

        }

    } else {                            //pageNumber is less then current page

        int jumpSpine = spinePos.getIndexFromPage(pageNumber);
        int currentSpine = spinePos.getPosition();
        Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);

        if (jumpSpine == currentSpine) {

            int diffrence = currentPage - pageNumber;
            Log.e(TAG, "diffrence > : " + diffrence);

            Log.e(TAG, "pagenum - diffrence : " + (pageNum - diffrence));

            this.pageNum = Math.max(pageNum - diffrence, 0);
            updatePosition();

        } else {

            PageTurnerSpine spine = bookView.getSpine();

            if (spine == null || !spine.navigateBackSpine(spine.getIndexFromPage(pageNumber))) {
                return;
            }
            this.pageNum = 0;

            gotoPage(pageNumber);
        }
    }

    Log.e(TAG, "In last pageNum : " + pageNum);
}

PageTurnerSpine.java

public int getIndexFromPage(int pageNumber) {
    int total = 0;
    int totalIndex = 0;

    for (List<Integer> pagesPerSection : pageOffsets) {
        total += pagesPerSection.size();
        totalIndex = totalIndex + 1;
        if (total - 1 >= pageNumber) {
            return totalIndex - 1;
        }
    }
    return 0;
}

public int getUptoPage(int position) {
    int total = 0, max = 0;
    for (List<Integer> pagesPerSection : pageOffsets) {
        max = max + 1;

        if (position == max - 1) {
            return total;
        }
        total += pagesPerSection.size();
    }
    return 0;
}

public int getPageOffSets(int position) {
    int max = 0;
    for (List<Integer> pagesPerSection : pageOffsets) {
        max = max + 1;

        if (position == max - 1) {
            return pagesPerSection.size();
        }
    }
    return 0;
}

public boolean navigateFrontSpine(int indexSpine) {

    if (this.position == size() - 1) {
        return false;
    }

    this.position = indexSpine;
    return true;
}

public boolean navigateBackSpine(int indexSpine) {

    if (this.position == 0) {
        return false;
    }

    this.position = indexSpine;
    return true;
}

When i apply this code and run some time it work correctly. but some time it jump on other page number, like if i enter 110 then it will jump on 109. and when i am trying to jump on chapter starting page then contain dose not changes. Please help me.

https://github.com/NightWhistler/PageTurner In this source code i have edited some file given above. they already exists in this project.

Mostly i see that most of epub reader like Kindle, FBReader, etc... does not implement Go To Page feature. So, i want to also know that is it possible to implement this feature or not?

Thanks for Help :)

Patel Pinkal
  • 6,738
  • 2
  • 21
  • 44
  • I suggest you post more specific code that identifies the problem. As it stands, the amount of code in this question is fairly large and that makes it hard to debug without being able to run it. http://stackoverflow.com/help/mcve – Anthony Dec 06 '16 at 14:16
  • https://github.com/NightWhistler/PageTurner In this source code i have edited some file given above. they exist already in project. – Patel Pinkal Dec 07 '16 at 04:22

1 Answers1

3

You really need to consult the IDPF Epub standards at IDPF.org/epub .

One approach, that I think is optional within the standard (but am not sure about) is to mark the content with the physical page numbers from the paper book (if there is one), or decide on your own numbering system along with your Table of Contents, and use a corresponding virtual page as the start of it.

This enables going to the start of the same page, but the number of virtual pages per physical page will vary, depending on font sizes etc. currently in use.

It's a data issue as much as a programming one.

Jool
  • 1,498
  • 13
  • 10