Esempio n. 1
0
  @Override
  public void applySpan(HtmlSpanner spanner, SpannableStringBuilder builder) {

    if (useStyle.getFontFamily() != null
        || useStyle.getFontStyle() != null
        || useStyle.getFontWeight() != null) {

      FontFamilySpan originalSpan = getFontFamilySpan(builder, start, end);
      FontFamilySpan newSpan;

      if (useStyle.getFontFamily() == null && originalSpan == null) {
        newSpan = new FontFamilySpan(this.defaultFont);
      } else if (useStyle.getFontFamily() != null) {
        newSpan = new FontFamilySpan(useStyle.getFontFamily());
      } else {
        newSpan = new FontFamilySpan(originalSpan.getFontFamily());
      }

      if (useStyle.getFontWeight() != null) {
        newSpan.setBold(useStyle.getFontWeight() == Style.FontWeight.BOLD);
      } else if (originalSpan != null) {
        newSpan.setBold(originalSpan.isBold());
      }

      if (useStyle.getFontStyle() != null) {
        newSpan.setItalic(useStyle.getFontStyle() == Style.FontStyle.ITALIC);
      } else if (originalSpan != null) {
        newSpan.setItalic(originalSpan.isItalic());
      }

      // Log.d("StyleCallback", "Applying FontFamilySpan from " + start + " to " + end + " on text "
      // + builder.subSequence(start, end));
      // Log.d("StyleCallback", "FontFamilySpan: " + newSpan );

      builder.setSpan(newSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    // If there's no border, we use a BackgroundColorSpan to draw colour behind the text
    if (spanner.isUseColoursFromStyle()
        && useStyle.getBackgroundColor() != null
        && useStyle.getBorderStyle() == null) {
      // Log.d("StyleCallback", "Applying BackgroundColorSpan with color " +
      // useStyle.getBackgroundColor() + " from " + start + " to " + end + " on text " +
      // builder.subSequence(start, end));
      builder.setSpan(
          new BackgroundColorSpan(useStyle.getBackgroundColor()),
          start,
          end,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    // If there is a border, the BorderSpan will also draw the background colour if needed.
    if (useStyle.getBorderStyle() != null) {
      builder.setSpan(
          new BorderSpan(useStyle, start, end, spanner.isUseColoursFromStyle()),
          start,
          end,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (useStyle.getFontSize() != null) {

      StyleValue styleValue = useStyle.getFontSize();

      if (styleValue.getUnit() == StyleValue.Unit.PS) {
        if (styleValue.getFloatValue() > 0) {
          // Log.d("StyleCallback", "Applying AbsoluteFloatSizeSpan with size " +
          // useStyle.getAbsoluteFontSize() + " from " + start + " to " + end + " on text " +
          // builder.subSequence(start, end));
          builder.setSpan(
              new AbsoluteFloatSizeSpan(styleValue.getFloatValue()),
              start,
              end,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      } else if (styleValue.getUnit() == StyleValue.Unit.PX) {
        if (styleValue.getIntValue() > 0) {
          // Log.d("StyleCallback", "Applying AbsoluteSizeSpan with size " +
          // useStyle.getAbsoluteFontSize() + " from " + start + " to " + end + " on text " +
          // builder.subSequence(start, end));
          builder.setSpan(
              new AbsoluteSizeSpan(styleValue.getIntValue()),
              start,
              end,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      } else {
        if (styleValue.getFloatValue() > 0f) {
          // Log.d("StyleCallback", "Applying RelativeSizeSpan with size " +
          // useStyle.getRelativeFontSize() + " from " + start + " to " + end + " on text " +
          // builder.subSequence(start, end));
          builder.setSpan(
              new RelativeSizeSpan(styleValue.getFloatValue()),
              start,
              end,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      }
    }

    if (spanner.isUseColoursFromStyle() && useStyle.getColor() != null) {
      // Log.d("StyleCallback", "Applying ForegroundColorSpan from " + start + " to " + end + " on
      // text " + builder.subSequence(start, end) );
      builder.setSpan(
          new ForegroundColorSpan(useStyle.getColor()),
          start,
          end,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (useStyle.getTextAlignment() != null) {

      AlignmentSpan alignSpan = null;

      switch (useStyle.getTextAlignment()) {
        case LEFT:
          alignSpan = new AlignNormalSpan();
          break;
        case CENTER:
          alignSpan = new CenterSpan();
          break;
        case RIGHT:
          alignSpan = new AlignOppositeSpan();
          break;
      }

      // Log.d("StyleCallback", "Applying AlignmentSpan from " + start + " to " + end + " on text "
      // + builder.subSequence(start, end) );
      builder.setSpan(alignSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (useStyle.getTextIndent() != null) {

      StyleValue styleValue = useStyle.getTextIndent();

      int marginStart = start;
      while (marginStart < end && builder.charAt(marginStart) == '\n') {
        marginStart++;
      }

      int marginEnd = min(end, marginStart + 1);

      Log.d(
          "StyleCallback",
          "Applying LeadingMarginSpan from "
              + marginStart
              + " to "
              + marginEnd
              + " on text "
              + builder.subSequence(marginStart, marginEnd));

      if (styleValue.getUnit() == StyleValue.Unit.PX) {
        if (styleValue.getIntValue() > 0) {
          builder.setSpan(
              new LeadingMarginSpan.Standard(styleValue.getIntValue(), 0),
              marginStart,
              marginEnd,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

      } else {
        if (styleValue.getFloatValue() > 0f) {
          builder.setSpan(
              new LeadingMarginSpan.Standard(
                  (int) (HtmlSpanner.HORIZONTAL_EM_WIDTH * styleValue.getFloatValue()), 0),
              marginStart,
              marginEnd,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      }
    }

    /* We ignore negative horizontal margins, since that would cause the text to be rendered off-screen */
    if (useStyle.getMarginLeft() != null) {
      StyleValue styleValue = useStyle.getMarginLeft();

      if (styleValue.getUnit() == StyleValue.Unit.PX) {
        if (styleValue.getIntValue() > 0) {
          builder.setSpan(
              new LeadingMarginSpan.Standard(styleValue.getIntValue()),
              start,
              end,
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

      } else if (styleValue.getFloatValue() > 0f) {
        builder.setSpan(
            new LeadingMarginSpan.Standard(
                (int) (HtmlSpanner.HORIZONTAL_EM_WIDTH * styleValue.getFloatValue())),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      }
    }
  }
Esempio n. 2
0
 RGB getBackgroundColor() {
   return style.getBackgroundColor();
 }