Ejemplo n.º 1
0
  private static void initNativeObject(final Bitmap pBitmap) {
    final byte[] pixels = Cocos2dxBitmap.getPixels(pBitmap);
    if (pixels == null) {
      return;
    }

    Cocos2dxBitmap.nativeInitBitmapDC(pBitmap.getWidth(), pBitmap.getHeight(), pixels);
  }
Ejemplo n.º 2
0
  /*
   * If maxWidth or maxHeight is not 0, split the string to fix the maxWidth
   * and maxHeight.
   */
  private static String[] splitString(
      final String pString, final int pMaxWidth, final int pMaxHeight, final Paint pPaint) {
    final String[] lines = pString.split("\\n");
    String[] ret = null;
    final FontMetricsInt fm = pPaint.getFontMetricsInt();
    final int heightPerLine = (int) Math.ceil(fm.bottom - fm.top);
    final int maxLines = pMaxHeight / heightPerLine;

    if (pMaxWidth != 0) {
      final LinkedList<String> strList = new LinkedList<String>();
      for (final String line : lines) {
        /*
         * The width of line is exceed maxWidth, should divide it into
         * two or more lines.
         */
        final int lineWidth = (int) FloatMath.ceil(pPaint.measureText(line));
        if (lineWidth > pMaxWidth) {
          strList.addAll(Cocos2dxBitmap.divideStringWithMaxWidth(line, pMaxWidth, pPaint));
        } else {
          strList.add(line);
        }

        /* Should not exceed the max height. */
        if (maxLines > 0 && strList.size() >= maxLines) {
          break;
        }
      }

      /* Remove exceeding lines. */
      if (maxLines > 0 && strList.size() > maxLines) {
        while (strList.size() > maxLines) {
          strList.removeLast();
        }
      }

      ret = new String[strList.size()];
      strList.toArray(ret);
    } else if (pMaxHeight != 0 && lines.length > maxLines) {
      /* Remove exceeding lines. */
      final LinkedList<String> strList = new LinkedList<String>();
      for (int i = 0; i < maxLines; i++) {
        strList.add(lines[i]);
      }
      ret = new String[strList.size()];
      strList.toArray(ret);
    } else {
      ret = lines;
    }

    return ret;
  }
Ejemplo n.º 3
0
  private static TextProperty computeTextProperty(
      final String pString, final int pWidth, final int pHeight, final Paint pPaint) {
    final FontMetricsInt fm = pPaint.getFontMetricsInt();
    final int h = (int) Math.ceil(fm.bottom - fm.top);
    int maxContentWidth = 0;

    final String[] lines = Cocos2dxBitmap.splitString(pString, pWidth, pHeight, pPaint);

    if (pWidth != 0) {
      maxContentWidth = pWidth;
    } else {
      /* Compute the max width. */
      int temp = 0;
      for (final String line : lines) {
        temp = (int) FloatMath.ceil(pPaint.measureText(line, 0, line.length()));
        if (temp > maxContentWidth) {
          maxContentWidth = temp;
        }
      }
    }

    return new TextProperty(maxContentWidth, h, lines);
  }
Ejemplo n.º 4
0
  public static void createTextBitmapShadowStroke(
      String pString,
      final String pFontName,
      final int pFontSize,
      final float fontTintR,
      final float fontTintG,
      final float fontTintB,
      final int pAlignment,
      final int pWidth,
      final int pHeight,
      final boolean shadow,
      final float shadowDX,
      final float shadowDY,
      final float shadowBlur,
      final boolean stroke,
      final float strokeR,
      final float strokeG,
      final float strokeB,
      final float strokeSize) {

    final int horizontalAlignment = pAlignment & 0x0F;
    final int verticalAlignment = (pAlignment >> 4) & 0x0F;

    pString = Cocos2dxBitmap.refactorString(pString);
    final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment);

    // set the paint color
    paint.setARGB(
        255, (int) (255.0 * fontTintR), (int) (255.0 * fontTintG), (int) (255.0 * fontTintB));

    final TextProperty textProperty =
        Cocos2dxBitmap.computeTextProperty(pString, pWidth, pHeight, paint);
    final int bitmapTotalHeight = (pHeight == 0 ? textProperty.mTotalHeight : pHeight);

    // padding needed when using shadows (not used otherwise)
    float bitmapPaddingX = 0.0f;
    float bitmapPaddingY = 0.0f;
    float renderTextDeltaX = 0.0f;
    float renderTextDeltaY = 0.0f;

    if (shadow) {

      int shadowColor = 0xff7d7d7d;
      paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, shadowColor);

      bitmapPaddingX = Math.abs(shadowDX);
      bitmapPaddingY = Math.abs(shadowDY);

      if (shadowDX < 0.0) {
        renderTextDeltaX = bitmapPaddingX;
      }

      if (shadowDY < 0.0) {
        renderTextDeltaY = bitmapPaddingY;
      }
    }

    final Bitmap bitmap =
        Bitmap.createBitmap(
            textProperty.mMaxWidth + (int) bitmapPaddingX,
            bitmapTotalHeight + (int) bitmapPaddingY,
            Bitmap.Config.ARGB_8888);

    final Canvas canvas = new Canvas(bitmap);

    /* Draw string. */
    final FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();

    int x = 0;
    int y =
        Cocos2dxBitmap.computeY(
            fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment);

    final String[] lines = textProperty.mLines;

    for (final String line : lines) {

      x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
      canvas.drawText(line, x + renderTextDeltaX, y + renderTextDeltaY, paint);
      y += textProperty.mHeightPerLine;
    }

    // draw again with stroke on if needed
    if (stroke) {

      final Paint paintStroke = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment);
      paintStroke.setStyle(Paint.Style.STROKE);
      paintStroke.setStrokeWidth(strokeSize * 0.5f);
      paintStroke.setARGB(255, (int) strokeR * 255, (int) strokeG * 255, (int) strokeB * 255);

      x = 0;
      y =
          Cocos2dxBitmap.computeY(
              fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment);
      final String[] lines2 = textProperty.mLines;

      for (final String line : lines2) {

        x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
        canvas.drawText(line, x + renderTextDeltaX, y + renderTextDeltaY, paintStroke);
        y += textProperty.mHeightPerLine;
      }
    }

    Cocos2dxBitmap.initNativeObject(bitmap);
  }