Example #1
0
  public static Bitmap parseLongPost(Context context, String text, Bitmap pic) {
    if (DEBUG) {
      Log.d(TAG, "parseLongPost");
      Log.d(TAG, "text = " + text);
    }

    // Get width and height
    int fontHeight = getFontHeight(context, 15.0f);
    int width = fontHeight * 17;
    int height = -1; // We will calculate this later
    int picWidth = width - 20, picHeight = 0; // We will calculate this later

    // Create the paint first to measue text
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextSize(15.0f);

    // Split the text into lines
    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<HashMap<String, Integer>> format = new ArrayList<HashMap<String, Integer>>();
    String tmp = text;

    while (tmp.length() > 0) {
      if (DEBUG) {
        Log.d(TAG, "tmp = " + tmp);
      }

      String line = "";

      boolean ignore = false;

      while (tmp.length() > 0) {
        String str = tmp.substring(0, 1);

        // The escape character is "\"
        if (str.equals("\\") && !ignore) {
          // \*This is not Italic text \*
          tmp = tmp.substring(1, tmp.length());
          ignore = true;
          continue;
        }

        // Simple text formatting
        // Thanks to Markdown
        if (str.equals("_") && tmp.length() > 1 && tmp.substring(1, 2).equals("_") && !ignore) {
          // __This is bold text__
          tmp = tmp.substring(2, tmp.length());
          HashMap<String, Integer> map = new HashMap<String, Integer>();
          map.put("line", lines.size());
          map.put("pos", line.length());
          map.put("type", 0);
          format.add(map);
          continue;
        } else if (str.equals("*") && !ignore) {
          // *This is Italic text*
          tmp = tmp.substring(1, tmp.length());
          HashMap<String, Integer> map = new HashMap<String, Integer>();
          map.put("line", lines.size());
          map.put("pos", line.length());
          map.put("type", 1);
          format.add(map);
          continue;
        } else if (str.equals("~")
            && tmp.length() > 1
            && tmp.substring(1, 2).equals("~")
            && !ignore) {
          // ~~This is deleted text~~
          tmp = tmp.substring(2, tmp.length());
          HashMap<String, Integer> map = new HashMap<String, Integer>();
          map.put("line", lines.size());
          map.put("pos", line.length());
          map.put("type", 2);
          format.add(map);
          continue;
        } else if (str.equals("[") && tmp.length() > 1 && !ignore) {
          // Inspired from shell's coloring
          // [rRed Text[d
          // [gGreen Text[d
          // [bBlue Text[d
          // [yYellow Text[d
          // [cCyan Text[d
          // [mMagenta Text[d
          // [dDefault Color[d
          String color = tmp.substring(1, 2);
          int type = Integer.MIN_VALUE;
          if (color.equals("r")) {
            type = Color.RED;
          } else if (color.equals("g")) {
            type = Color.GREEN;
          } else if (color.equals("b")) {
            type = Color.BLUE;
          } else if (color.equals("y")) {
            type = Color.YELLOW;
          } else if (color.equals("c")) {
            type = Color.CYAN;
          } else if (color.equals("m")) {
            type = Color.MAGENTA;
          } else if (color.equals("d")) {
            type = -1;
          } else if (color.equals("#")) {
            color = tmp.substring(1, 8);
            type = Color.parseColor(color);
          }

          if (type > Integer.MIN_VALUE) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("line", lines.size());
            map.put("pos", line.length());
            map.put("type", type);
            format.add(map);
            tmp = tmp.substring(color.length() + 1, tmp.length());
            continue;
          }
        }

        ignore = false;

        line += str;
        tmp = tmp.substring(1, tmp.length());

        if (line.contains("\n") || paint.measureText(line) >= width - fontHeight * 2) {
          break;
        }
      }

      lines.add(line);
    }

    lines.add("");

    // Generated By BlackLight
    String from = context.getResources().getString(R.string.long_from);
    lines.add(from);

    // Calculate height
    height = fontHeight * (lines.size() + 2);

    if (pic != null) {
      picHeight = (int) (((float) picWidth / (float) pic.getWidth()) * pic.getHeight());
      height += picHeight + 20;

      if (DEBUG) {
        Log.d(
            TAG,
            "picHeight = "
                + picHeight
                + "; height = "
                + height
                + "; pic.getHeight() = "
                + pic.getHeight());
        Log.d(TAG, "picWidth = " + picWidth + "; pic.getWidth() = " + pic.getWidth());
      }
    }

    // Create the bitmap and draw
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);

    paint.setColor(context.getResources().getColor(android.R.color.background_light));
    canvas.drawRect(0, 0, width, height, paint);

    int defColor = context.getResources().getColor(R.color.darker_gray);
    paint.setColor(defColor);

    float y = fontHeight * 1.5f;
    float x = fontHeight;

    int i = 0;

    for (String line : lines) {
      if (DEBUG) {
        Log.d(TAG, "line = " + line);
        Log.d(TAG, "y = " + y);
      }

      if (line.equals(from)) {
        paint.setColor(context.getResources().getColor(R.color.gray));
      }

      int lastPos = 0;

      float xOffset = 0;

      while (format.size() > 0) {
        HashMap<String, Integer> map = format.get(0);

        if (map.get("line") != i) {
          break;
        } else {
          format.remove(0);
          int pos = map.get("pos");
          String str = line.substring(lastPos, pos);
          canvas.drawText(str, x + xOffset, y, paint);
          xOffset += paint.measureText(str);
          lastPos = pos;

          int type = map.get("type");
          switch (type) {
            case 0:
              paint.setFakeBoldText(!paint.isFakeBoldText());
              break;
            case 1:
              if (paint.getTextSkewX() >= 0.0f) paint.setTextSkewX(-0.25f);
              else paint.setTextSkewX(0.0f);
              break;
            case 2:
              paint.setStrikeThruText(!paint.isStrikeThruText());
              break;
            case -1:
              paint.setColor(defColor);
              break;
            default:
              paint.setColor(type);
              break;
          }
        }
      }

      canvas.drawText(line.substring(lastPos, line.length()), x + xOffset, y, paint);

      y += fontHeight;
      i++;
    }

    // Draw the picture
    if (pic != null) {
      canvas.drawBitmap(
          pic,
          new Rect(0, 0, pic.getWidth(), pic.getHeight()),
          new Rect(10, (int) (y + 10), picWidth + 10, (int) (picHeight + y + 10)),
          paint);
    }

    // Finished, return
    return bmp;
  }