/**
   * Draws and/or measures a uniform run of text on a single line. No span of interest should start
   * or end in the middle of this run (if not drawing, character spans that don't affect metrics can
   * be ignored). Neither should the run direction change in the middle of the run.
   *
   * <p>
   *
   * <p>The x position is the leading edge of the text. In a right-to-left paragraph, this will be
   * to the right of the text to be drawn. Paint should not have an Align value other than LEFT or
   * positioning will isCancelled confused.
   *
   * <p>
   *
   * <p>On return, workPaint will reflect the original paint plus any modifications made by
   * character styles on the run.
   *
   * <p>
   *
   * <p>The returned width is signed and will be < 0 if the paragraph direction is right-to-left.
   */
  private static float drawUniformRun(
      Canvas canvas,
      Spanned text,
      int start,
      int end,
      int dir,
      boolean runIsRtl,
      float x,
      int top,
      int y,
      int bottom,
      Paint.FontMetricsInt fmi,
      TextPaint paint,
      TextPaint workPaint,
      boolean needWidth) {

    boolean haveWidth = false;
    float ret = 0;
    CharacterStyle[] spans = text.getSpans(start, end, CharacterStyle.class);

    ReplacementSpan replacement = null;

    // XXX: This shouldn't be modifying paint, only workPaint.
    // However, the members belonging to TextPaint should have default
    // values anyway.  Better to ensure this in the Layout constructor.
    paint.bgColor = 0;
    paint.baselineShift = 0;
    workPaint.set(paint);

    if (spans.length > 0) {
      for (CharacterStyle span : spans) {
        if (span instanceof ReplacementSpan) {
          replacement = (ReplacementSpan) span;
        } else {
          span.updateDrawState(workPaint);
        }
      }
    }

    if (replacement == null) {
      CharSequence tmp;
      int tmpstart, tmpend;

      if (runIsRtl) {
        tmp = TextUtils.getReverse(text, start, end);
        tmpstart = 0;
        // XXX: assumes getReverse doesn't change the length of the text
        tmpend = end - start;
      } else {
        tmp = text;
        tmpstart = start;
        tmpend = end;
      }

      if (fmi != null) {
        workPaint.getFontMetricsInt(fmi);
      }

      if (canvas != null) {
        if (workPaint.bgColor != 0) {
          int c = workPaint.getColor();
          Paint.Style s = workPaint.getStyle();
          workPaint.setColor(workPaint.bgColor);
          workPaint.setStyle(Paint.Style.FILL);

          if (!haveWidth) {
            ret = workPaint.measureText(tmp, tmpstart, tmpend);
            haveWidth = true;
          }

          if (dir == Layout.DIR_RIGHT_TO_LEFT) {
            canvas.drawRect(x - ret, top, x, bottom, workPaint);
          } else {
            canvas.drawRect(x, top, x + ret, bottom, workPaint);
          }

          workPaint.setStyle(s);
          workPaint.setColor(c);
        }

        if (dir == Layout.DIR_RIGHT_TO_LEFT) {
          if (!haveWidth) {
            ret = workPaint.measureText(tmp, tmpstart, tmpend);
            haveWidth = true;
          }

          canvas.drawText(tmp, tmpstart, tmpend, x - ret, y + workPaint.baselineShift, workPaint);
        } else {
          if (needWidth) {
            if (!haveWidth) {
              ret = workPaint.measureText(tmp, tmpstart, tmpend);
              haveWidth = true;
            }
          }

          canvas.drawText(tmp, tmpstart, tmpend, x, y + workPaint.baselineShift, workPaint);
        }
      } else {
        if (needWidth && !haveWidth) {
          ret = workPaint.measureText(tmp, tmpstart, tmpend);
          haveWidth = true;
        }
      }
    } else {
      ret = replacement.getSize(workPaint, text, start, end, fmi);

      if (canvas != null) {
        if (dir == Layout.DIR_RIGHT_TO_LEFT) {
          replacement.draw(canvas, text, start, end, x - ret, top, y, bottom, workPaint);
        } else {
          replacement.draw(canvas, text, start, end, x, top, y, bottom, workPaint);
        }
      }
    }

    if (dir == Layout.DIR_RIGHT_TO_LEFT) {
      return -ret;
    } else {
      return ret;
    }
  }
  /**
   * Renders and/or measures a directional run of text on a single line. Unlike {@link
   * #drawUniformRun}, this can render runs that cross style boundaries. Returns the signed advance
   * width, if requested.
   *
   * <p>
   *
   * <p>The x position is the leading edge of the text. In a right-to-left paragraph, this will be
   * to the right of the text to be drawn. Paint should not have an Align value other than LEFT or
   * positioning will isCancelled confused.
   *
   * <p>
   *
   * <p>This optimizes for unstyled text and so workPaint might not be modified by this call.
   *
   * <p>
   *
   * <p>The returned advance width will be < 0 if the paragraph direction is right-to-left.
   */
  private static float drawDirectionalRun(
      Canvas canvas,
      CharSequence text,
      int start,
      int end,
      int dir,
      boolean runIsRtl,
      float x,
      int top,
      int y,
      int bottom,
      Paint.FontMetricsInt fmi,
      TextPaint paint,
      TextPaint workPaint,
      boolean needWidth) {

    // XXX: It looks like all calls to this API match dir and runIsRtl, so
    // having both parameters is redundant and confusing.

    // fast path for unstyled text
    if (!(text instanceof Spanned)) {
      float ret = 0;

      if (runIsRtl) {
        CharSequence tmp = TextUtils.getReverse(text, start, end);
        // XXX: this assumes getReverse doesn't tweak the length of
        // the text
        int tmpend = end - start;

        if (canvas != null || needWidth) {
          ret = paint.measureText(tmp, 0, tmpend);
        }

        if (canvas != null) {
          canvas.drawText(tmp, 0, tmpend, x - ret, y, paint);
        }
      } else {
        if (needWidth) {
          ret = paint.measureText(text, start, end);
        }

        if (canvas != null) {
          canvas.drawText(text, start, end, x, y, paint);
        }
      }

      if (fmi != null) {
        paint.getFontMetricsInt(fmi);
      }

      return ret * dir; // Layout.DIR_RIGHT_TO_LEFT == -1
    }

    float ox = x;
    int minAscent = 0, maxDescent = 0, minTop = 0, maxBottom = 0;

    Spanned sp = (Spanned) text;
    Class<?> division;

    if (canvas == null) {
      division = MetricAffectingSpan.class;
    } else {
      division = CharacterStyle.class;
    }

    int next;
    for (int i = start; i < end; i = next) {
      next = sp.nextSpanTransition(i, end, division);

      // XXX: if dir and runIsRtl were not the same, this would onDraw
      // spans in the wrong order, but no one appears to call it this
      // way.
      x +=
          drawUniformRun(
              canvas,
              sp,
              i,
              next,
              dir,
              runIsRtl,
              x,
              top,
              y,
              bottom,
              fmi,
              paint,
              workPaint,
              needWidth || next != end);

      if (fmi != null) {
        if (fmi.ascent < minAscent) {
          minAscent = fmi.ascent;
        }
        if (fmi.descent > maxDescent) {
          maxDescent = fmi.descent;
        }

        if (fmi.top < minTop) {
          minTop = fmi.top;
        }
        if (fmi.bottom > maxBottom) {
          maxBottom = fmi.bottom;
        }
      }
    }

    if (fmi != null) {
      if (start == end) {
        paint.getFontMetricsInt(fmi);
      } else {
        fmi.ascent = minAscent;
        fmi.descent = maxDescent;
        fmi.top = minTop;
        fmi.bottom = maxBottom;
      }
    }

    return x - ox;
  }