Пример #1
0
  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;
  }
Пример #2
0
    @Override
    public CharSequence filter(
        CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

      if (textSetup) return source;

      if (!(source instanceof SpannableStringBuilder)) {
        StringBuilder filteredStringBuilder = new StringBuilder();
        final boolean charAllowed = isCharAllowed(dstart);
        for (int i = start; i < end; i++) {
          char currentChar = source.charAt(i);

          if (charAllowed) {
            isUserInput = false;
            MaskedEditText.this.getText().replace(dstart, dstart + 1, "");
            isUserInput = true;
            filteredStringBuilder.append(currentChar);
            int index;
            if (!isCharAllowed(dstart + 1)) index = dstart + 1;
            else index = dstart;

            skipSymbol(index);

          } else {
            if (dstart != mask.length()) skipSymbol(dstart);
          }
        }
        if (isUserInput && TextUtils.isEmpty(source)) { // deletion detection
          if (dend != 0) {
            if (charAllowed) {
              filteredStringBuilder.append(deleteChar);
              skipSymbolAfterDeletion(dstart);
            } else {
              filteredStringBuilder.append(mask.charAt(dstart));
              skipSymbolAfterDeletion(dstart);
            }
          }
        }

        return filteredStringBuilder.toString();
      }
      return source;
    }
Пример #3
0
 @Override
 public CharSequence filter(
     CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
   if ((end == 1)
       && (dstart != 0)
       && (dend != dest.length())
       && (source.charAt(start) == '\n')) {
     StringBuilder ident = new StringBuilder();
     // find the last line
     int n = 1;
     while (dest.charAt(dstart - n) != '\n') ++n;
     // copy previous ident and autoexpand comments
     int s = 1;
     char c = dest.charAt(dstart - n + s++);
     while (c != '\n' && (c == ' ' || c == '/')) {
       ident.append(c);
       c = dest.charAt(dstart - n + s++);
     }
     // extra ident after these chars
     if ("{(*+/-=%".contains(String.valueOf(dest.charAt(dstart - 1)))) ident.append("   ");
     return source + ident.toString();
   }
   return source;
 }
Пример #4
0
  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;
  }