public static BitmapData GetTileBitmap(DashStyle dashStyle, float thickness, Color color) { // fix for case 122461: values lower 1 causes bitmap rectangle to get empty thickness = System.Math.Max(1, thickness); //try to find DashTile in cache foreach (Tile tile in Tiles) { if (tile.DashStyle == dashStyle && tile.Color == color && tile.Thickness == thickness) return tile.BitmapData; } //if dashtile is not cached, create it and add to cache Shape dashTile; switch (dashStyle) { case DashStyle.Dash: dashTile = DashTile; break; case DashStyle.DashDot: dashTile = DashDotTile; break; case DashStyle.DashDotDot: dashTile = DashDotDotTile; break; case DashStyle.Dot: dashTile = DotTile; break; default: throw new System.NotSupportedException(); } dashTile.height = DotCap; // prepare matrix and color transforms for the tile flash.geom.Matrix scaleMatrix = new flash.geom.Matrix(); scaleMatrix.scale(thickness, thickness); ColorTransform colorTransform = new ColorTransform(0, 0, 0, 0, color.R, color.G, color.B, 255); // draw bitmap with a transformed tile BitmapData bmData = new BitmapData((int)Avm.Math.ceil(dashTile.width * thickness), (int)(dashTile.height * thickness), true, 0x00ffffff); //default transparent color bmData.draw(dashTile, scaleMatrix, colorTransform); //add to cache Tiles.Add(new Tile(bmData, dashStyle, thickness, color)); return bmData; }
// WHAT IS THE DEFAULT FONT FOR ANDROID? #endif // Partial class implementation of draw() in c# to allow use of unsafe code.. public unsafe void draw(IBitmapDrawable source, flash.geom.Matrix matrix = null, ColorTransform colorTransform = null, string blendMode = null, Rectangle clipRect = null, Boolean smoothing = false) { #if PLATFORM_MONOMAC || PLATFORM_MONOTOUCH if (source is flash.text.TextField) { flash.text.TextField tf = source as flash.text.TextField; flash.text.TextFormat format = tf.defaultTextFormat; // $$TODO figure out how to get rid of this extra data copy var sizeToDraw = (width * height) << 2; if (sizeToDraw == 0) { return; } string fontName = format.font; float fontSize = (format.size is double) ? (float)(double)format.size : ((format.size is int) ? (float)(int)format.size : 10); // Check if the font is installed? bool hasFont = true; if (!sHasFont.TryGetValue(fontName, out hasFont)) { #if PLATFORM_MONOTOUCH UIFont font = UIFont.FromName(fontName, 10); sHasFont[fontName] = hasFont = font != null; if (font != null) { font.Dispose(); } #elif PLATFORM_MONOMAC NSFont font = NSFont.FromFontName(fontName, 10); sHasFont[fontName] = hasFont = font != null; if (font != null) { font.Dispose(); } #else sHasFont[fontName] = false; #endif } if (!hasFont) { fontName = DEFAULT_FONT; } fixed(uint *data = mData) { using (CGBitmapContext context = new CGBitmapContext(new IntPtr(data), width, height, 8, 4 * width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast)) { uint tfColor = format.color != null ? (uint)(format.color) : 0; float r = (float)((tfColor >> 16) & 0xFF) / 255.0f; float g = (float)((tfColor >> 8) & 0xFF) / 255.0f; float b = (float)((tfColor >> 0) & 0xFF) / 255.0f; float a = (float)(tf.alpha); CGColor color = new CGColor(r, g, b, a); context.SetFillColor(color); context.SetStrokeColor(color); context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman); context.SetAllowsAntialiasing(((tf.antiAliasType as string) == flash.text.AntiAliasType.ADVANCED)); double x = matrix.tx; double y = matrix.ty; // invert y because the CG origin is bottom,left y = height - tf.textHeight - y; // align text switch (format.align) { case TextFormatAlign.LEFT: // no adjustment required break; case TextFormatAlign.CENTER: // center x x += width / 2; x -= tf.textWidth / 2; break; case TextFormatAlign.RIGHT: // right justify x x += width; x -= tf.textWidth; break; default: throw new System.NotImplementedException(); } // draw text context.ShowTextAtPoint((float)x, (float)y, tf.text); } } } else #elif PLATFORM_MONODROID if (source is flash.text.TextField) { flash.text.TextField tf = source as flash.text.TextField; flash.text.TextFormat format = tf.defaultTextFormat; // $$TODO figure out how to get rid of this extra data copy var data = new byte[width * height * 4]; System.Buffer.BlockCopy(mData, 0, data, 0, data.Length); Android.Graphics.Bitmap.Config config = Android.Graphics.Bitmap.Config.Argb8888; Android.Graphics.Bitmap bitmap = Android.Graphics.Bitmap.CreateBitmap(width, height, config); Canvas canvas = new Canvas(bitmap); var x = matrix.tx; var y = matrix.ty; // invert y because the CG origin is bottom,left // y = height - tf.textHeight - y; // align text switch (format.align) { case TextFormatAlign.LEFT: // no adjustment required break; case TextFormatAlign.CENTER: // center x x += width / 2; x -= tf.textWidth / 2; break; case TextFormatAlign.RIGHT: // right justify x x += width; x -= tf.textWidth; break; default: throw new System.NotImplementedException(); } Paint paint = new Paint(PaintFlags.AntiAlias); paint.Color = Color.Black; paint.TextSize = (float)format.size; paint.SetTypeface(Typeface.Create(format.font, TypefaceStyle.Normal)); paint.TextAlign = Paint.Align.Center; canvas.DrawText(tf.text, (float)x, (float)y, paint); mData = new uint[bitmap.Width * bitmap.Height]; var buffer = new int[bitmap.Width * bitmap.Height]; bitmap.GetPixels(buffer, 0, width, 0, 0, width, height); for (int i = 0; i < buffer.Length; i++) { mData[i] = (uint)buffer[i]; } } else #endif if (source is flash.display.BitmapData) { //naive implementation , //to be implemented: // -smoothing / antialiasing, // -blend mode // -colorTransform // -cliprect BitmapData sourceBitmap = source as BitmapData; flash.geom.Matrix matInverse = (matrix != null) ? matrix.clone() : new flash.geom.Matrix(); matInverse.invert(); for (int y = 0; y < mHeight; y++) { for (int x = 0; x < mWidth; x++) { int x2 = (int)(x * matInverse.a + y * matInverse.c + matInverse.tx); int y2 = (int)(x * matInverse.b + y * matInverse.d + matInverse.ty); if (x2 >= 0 && y2 >= 0 && x2 < sourceBitmap.width && y2 < sourceBitmap.height) { mData[x + y * mWidth] = sourceBitmap.mData[x2 + y2 * sourceBitmap.mWidth]; } } } } else { _root.trace_fn.trace("NotImplementedWarning: BitmapData.draw()"); } }