private CharSequence applySpans(CharSequence text, int offset) {
    List<HighLight> highLights = highlightManager.getHighLights(bookView.getFileName());
    int end = offset + text.length() - 1;

    for (final HighLight highLight : highLights) {
      if (highLight.getIndex() == bookView.getIndex()
          && highLight.getStart() >= offset
          && highLight.getStart() < end) {

        LOG.debug(
            "Got highlight from "
                + highLight.getStart()
                + " to "
                + highLight.getEnd()
                + " with offset "
                + offset);

        int highLightEnd = Math.min(end, highLight.getEnd());

        ((Spannable) text)
            .setSpan(
                new HighlightSpan(highLight),
                highLight.getStart() - offset,
                highLightEnd - offset,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      }
    }

    return text;
  }
  @Override
  public void pageDown() {

    this.storedPosition = -1;

    if (isAtEnd()) {
      PageTurnerSpine spine = bookView.getSpine();

      if (spine == null || !spine.navigateForward()) {
        return;
      }

      this.clearText();
      this.pageNum = 0;
      bookView.loadText();

    } else {
      this.pageNum = Math.min(pageNum + 1, this.pageOffsets.size() - 1);
      updatePosition();
    }
  }
  @Override
  public void pageUp() {

    this.storedPosition = -1;

    if (isAtStart()) {
      PageTurnerSpine spine = bookView.getSpine();

      if (spine == null || !spine.navigateBack()) {
        return;
      }

      this.clearText();
      this.storedPosition = Integer.MAX_VALUE;
      this.bookView.loadText();
    } else {
      this.pageNum = Math.max(pageNum - 1, 0);
      updatePosition();
    }
  }
 @Override
 public void setBookView(BookView bookView) {
   this.bookView = bookView;
   this.childView = bookView.getInnerView();
 }
  public List<Integer> getPageOffsets(CharSequence text, boolean includePageNumbers) {

    if (text == null) {
      return new ArrayList<Integer>();
    }

    List<Integer> pageOffsets = new ArrayList<Integer>();

    TextPaint textPaint = bookView.getInnerView().getPaint();
    int boundedWidth = bookView.getInnerView().getWidth();

    LOG.debug("Page width: " + boundedWidth);

    StaticLayout layout =
        layoutFactory.create(text, textPaint, boundedWidth, bookView.getLineSpacing());
    LOG.debug("Layout height: " + layout.getHeight());

    layout.draw(new Canvas());

    // Subtract the height of the top margin
    int pageHeight = bookView.getHeight() - bookView.getVerticalMargin();

    if (includePageNumbers) {
      String bottomSpace = "0\n";

      StaticLayout numLayout =
          layoutFactory.create(bottomSpace, textPaint, boundedWidth, bookView.getLineSpacing());
      numLayout.draw(new Canvas());

      // Subtract the height needed to show page numbers, or the
      // height of the margin, whichever is more
      pageHeight = pageHeight - Math.max(numLayout.getHeight(), bookView.getVerticalMargin());
    } else {
      // Just subtract the bottom margin
      pageHeight = pageHeight - bookView.getVerticalMargin();
    }

    LOG.debug("Got pageHeight " + pageHeight);

    int totalLines = layout.getLineCount();
    int topLineNextPage = -1;
    int pageStartOffset = 0;

    while (topLineNextPage < totalLines - 1) {

      LOG.debug("Processing line " + topLineNextPage + " / " + totalLines);

      int topLine = layout.getLineForOffset(pageStartOffset);
      topLineNextPage = layout.getLineForVertical(layout.getLineTop(topLine) + pageHeight);

      LOG.debug("topLine " + topLine + " / " + topLineNextPage);
      if (topLineNextPage == topLine) { // If lines are bigger than can fit on a page
        topLineNextPage = topLine + 1;
      }

      int pageEnd = layout.getLineEnd(topLineNextPage - 1);

      LOG.debug("pageStartOffset=" + pageStartOffset + ", pageEnd=" + pageEnd);

      if (pageEnd > pageStartOffset) {
        if (text.subSequence(pageStartOffset, pageEnd).toString().trim().length() > 0) {
          pageOffsets.add(pageStartOffset);
        }
        pageStartOffset = layout.getLineStart(topLineNextPage);
      }
    }

    return pageOffsets;
  }