OSDN Git Service

123
[psychlops/silverlight.git] / dev3 / psychlops / core / graphic / canvas.cs
index 9653142..9dfe031 100644 (file)
@@ -7,85 +7,152 @@ using System.Windows.Media;
 using System.Windows.Media.Animation;\r
 using System.Windows.Media.Imaging;\r
 using System.Windows.Shapes;\r
+using System.Windows.Browser;\r
 \r
 \r
 \r
 namespace Psychlops\r
 {\r
 \r
-       public class Canvas : Drawable\r
+       namespace Templates\r
        {\r
-               public static System.Windows.Controls.Image default_buffer_frame;\r
+\r
+               public class StackableDrawable : Drawable\r
+               {\r
+                       protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
+\r
+                       public StackableDrawable()\r
+                       {\r
+                               stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
+                       }\r
+\r
+                       public void clear()\r
+                       {\r
+                               clear(Color.black);\r
+                       }\r
+                       public virtual void clear(Color col)\r
+                       {\r
+                               //rect(back_panel, col);\r
+                       }\r
+\r
+                       public virtual void pix(int x, int y, Color col)\r
+                       {\r
+                       }\r
+\r
+                       public virtual void line(Line drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public virtual void rect(Rectangle drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public virtual void ellipse(Ellipse drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public virtual void polygon(Polygon drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public virtual void letters(Letters drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public virtual void image(Image drawee)\r
+                       {\r
+                               stack.Enqueue(drawee.clone());\r
+                       }\r
+\r
+                       public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
+                       public virtual void msg(string dstr, double x, double y, Color col)\r
+                       {\r
+                               var let = new Letters(dstr);\r
+                               let.locate(x, y);\r
+                               let.fill = col;\r
+                               this.letters(let);\r
+                       }\r
+                       public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
+                       public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
+\r
+                       public virtual Point getCenter() { return new Point(0, 0, 0); }\r
+               }\r
+\r
+       }\r
+\r
+       public class Canvas : Templates.StackableDrawable\r
+       {\r
+\r
+               #region initializer\r
+\r
+               internal delegate void TwoIntProcedure(int x, int y);\r
+               internal delegate void SimpleProcedure();\r
+               SimpleProcedure flipexec;\r
+\r
+               //public static System.Windows.Controls.Image default_buffer_frame;\r
                public static System.Windows.Controls.UserControl default_panel;\r
                public static System.Windows.Controls.Canvas default_api_canvas;\r
                public static WriteableBitmap default_buffer;\r
-               System.Windows.Controls.Image instance;\r
+               //WriteableBitmap buffer;\r
+               //System.Windows.Controls.Image instance;\r
                System.Windows.Controls.Canvas api_canvas;\r
                System.Windows.Controls.UserControl panel;\r
-               WriteableBitmap buffer;\r
-               System.Windows.Shapes.Rectangle back_panel;\r
-               System.Windows.Media.SolidColorBrush back_panel_color;\r
+               Rectangle back_panel;\r
+               double width_, height_;\r
 \r
                public Canvas(int wid, int hei)\r
                {\r
                        panel = default_panel;\r
                        api_canvas = default_api_canvas;\r
-                       instance = default_buffer_frame;\r
                        initialize(wid, hei);\r
                }\r
-               public Canvas(int wid, int hei, System.Windows.Controls.Image target, System.Windows.Controls.Canvas apicnvs,  System.Windows.Controls.UserControl system)\r
+               public Canvas(int wid, int hei, System.Windows.Controls.Canvas apicnvs, System.Windows.Controls.UserControl system)\r
                {\r
                        panel = system;\r
                        api_canvas = apicnvs;\r
-                       instance = target;\r
                        initialize(wid, hei);\r
                }\r
                protected void initialize(int wid, int hei)\r
                {\r
-                       instance.Width = wid;\r
-                       instance.Height = hei;\r
-                       instance.Source = buffer;\r
+\r
+                       width_ = wid;\r
+                       height_ = hei;\r
+                       api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
+                       Mouse._prime = api_canvas;\r
+                       Main.drawable = this;\r
+                       Main.canvas = this;\r
+\r
+                       back_panel = new Rectangle(wid, hei);\r
+\r
+                       flipexec = new SimpleProcedure(executeFlip);\r
+               }\r
+               protected void initialize__(int wid, int hei)\r
+               {\r
                        api_canvas.Width = wid;\r
                        api_canvas.Height = hei;\r
-                       buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
                        api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
                        api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
                        api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
                        api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
                        panel.KeyDown += Keyboard.Canvas_KeyDown;\r
                        panel.KeyUp += Keyboard.Canvas_KeyUp;\r
-                       Mouse._prime = api_canvas;\r
-                       Main.drawable = this;\r
-                       default_buffer = buffer;\r
-\r
-                       back_panel = new System.Windows.Shapes.Rectangle();\r
-                       back_panel.Width = wid;\r
-                       back_panel.Height = hei;\r
-                       back_panel_color = new SolidColorBrush();\r
-                       back_panel.Fill = back_panel_color;\r
-               }\r
-               public Point getCenter()\r
-               {\r
-                       return new Point(instance.Width/2.0, instance.Height/2.0, 0);\r
-               }\r
 \r
-               public void clear()\r
-               {\r
-                       buffer.Clear(Color.black);\r
-               }\r
-               public void clear(Color col)\r
-               {\r
-                       api_canvas.Children.Clear();\r
-                       back_panel_color.Color = col;\r
-                       api_canvas.Children.Add(back_panel);\r
-                       //buffer.Clear(col);\r
+                       HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost");\r
+                       //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null");\r
+                       htmlHost.SetStyleAttribute("width", (200+wid).ToString()+"px");\r
+                       htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
+                       htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
                }\r
 \r
-               public void pix(int x, int y, Color col)\r
-               {\r
-                       buffer.SetPixel(x, y, col);\r
-               }\r
+               #endregion\r
 \r
+               #region static initializer\r
+               /*\r
                static System.Windows.Shapes.Line api_line;\r
                static System.Windows.Shapes.Path api_curve;\r
                static System.Windows.Shapes.Rectangle api_rect;\r
@@ -96,24 +163,8 @@ namespace Psychlops
                static System.Windows.Media.SolidColorBrush api_fill;\r
                static System.Windows.Media.SolidColorBrush api_stroke;\r
                static System.Windows.Media.TranslateTransform api_translation;\r
-               protected static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
-               protected static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
-               protected static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
                static Canvas()\r
                {\r
-                       FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
-                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
-                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
-                       FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
-                       FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
-                       LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_LEFT, TextAlignment.Left);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_CENTER, TextAlignment.Center);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.TEXT_ALIGN_RIGHT, TextAlignment.Right);\r
-                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.NOT_SPECIFIED, TextAlignment.Left);\r
-\r
                        api_line = new System.Windows.Shapes.Line();\r
                        api_curve    = new System.Windows.Shapes.Path();\r
                        api_rect     = new System.Windows.Shapes.Rectangle();\r
@@ -125,129 +176,291 @@ namespace Psychlops
                        api_stroke      = new System.Windows.Media.SolidColorBrush();\r
                        api_translation = new System.Windows.Media.TranslateTransform();\r
                }\r
+               */\r
+               #endregion\r
+\r
 \r
-               public void line(Line drawee, Color col)\r
-               {\r
-                       //buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
-                       var zapi_shape = new System.Windows.Shapes.Line();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.X1 = (int)drawee.begin.x;\r
-                       zapi_shape.Y1 = (int)drawee.begin.y;\r
-                       zapi_shape.X2 = (int)drawee.end.x;\r
-                       zapi_shape.Y2 = (int)drawee.end.y;\r
-                       zapi_fill.Color = col;\r
-                       zapi_fill.Opacity = 1.0;\r
-                       zapi_shape.Stroke = zapi_fill;\r
-                       zapi_shape.StrokeThickness = 1;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       //System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       //System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
-               }\r
-               public void rect(Rectangle drawee, Color col)\r
-               {\r
-                       //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col);\r
-\r
-                       var zapi_shape = new System.Windows.Shapes.Rectangle();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = drawee.width;\r
-                       zapi_shape.Height = drawee.height;\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
-               }\r
-               public void ellipse(Ellipse drawee, Color col)\r
-               {\r
-                       //buffer.DrawEllipse((int)(drawee.datum.x - drawee.xdiameter / 2), (int)(drawee.datum.y - drawee.ydiameter / 2), (int)(drawee.datum.x + drawee.xdiameter / 2), (int)(drawee.datum.y + drawee.ydiameter / 2), col);\r
-\r
-                       var zapi_shape = new System.Windows.Shapes.Ellipse();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = drawee.width;\r
-                       zapi_shape.Height = drawee.height;\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
-               }\r
-               public void polygon(Polygon drawee, Color col)\r
-               {\r
-                       /*\r
-                       int[] ps = new int[drawee.vertices.Count];\r
-                       buffer.DrawPolyline(ps, col);\r
-                       */\r
-                       var zapi_shape = new System.Windows.Shapes.Polygon();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       int i = 0;\r
-                       foreach (Point p in drawee.vertices)\r
-                       {\r
-                               zapi_shape.Points.Add(p);\r
-                       }\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Fill = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
-               }\r
-               public void letters(Letters drawee, Color col)\r
+               public override void clear(Color col)\r
                {\r
-                       //var zapi_shape = new System.Windows.Documents.Glyphs();\r
-                       var zapi_shape = new System.Windows.Controls.TextBlock();\r
-                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
-                       zapi_shape.Width = width;\r
-                       zapi_shape.Height = height;\r
-                       zapi_shape.Text = drawee.str;\r
-                       //zapi_shape.FontFamily = ;\r
-                       zapi_shape.FontSize = drawee.font.size;\r
-                       zapi_shape.FontStyle = FONT_STYLE_BRIDGE[drawee.font.style];\r
-                       zapi_shape.FontWeight = FONT_WEIGHT_BRIDGE[drawee.font.weight];\r
-                       zapi_shape.TextAlignment = LETTERS_H_ALIGN_BRIDGE[drawee.align];\r
-                       zapi_fill.Color = col;\r
-                       zapi_shape.Foreground = zapi_fill;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       double left = 0;\r
-                       switch(drawee.align)\r
+                       back_panel.fill = col;\r
+                       rect(back_panel);\r
+               }\r
+\r
+               int nextIntervalFrame = 1, chacked = 0;\r
+               public void flip()\r
+               {\r
+                       //api_canvas.Dispatcher.BeginInvoke(flipexec);\r
+                       //flipexec();\r
+                       //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);\r
+                       lock (this)\r
                        {\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_LEFT: break;\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_CENTER: left = zapi_shape.Width/2; break;\r
-                               case Letters.HorizontalAlign.TEXT_ALIGN_RIGHT: left = zapi_shape.Width; break;\r
+                               nextIntervalFrame = 1;\r
+                               chacked = 1;\r
                        }\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x - left);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y - drawee.font.size);\r
+                       Internal.Main.canvas_flag.WaitOne();\r
                }\r
-               public void image(Image drawee)\r
+               public void executeFlip()\r
                {\r
-                       //buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\r
-                       var zapi_shape = new System.Windows.Controls.Image();\r
-                       zapi_shape.Source = drawee.buffer;\r
-                       api_canvas.Children.Add(zapi_shape);\r
-                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
-                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
+                       lock (this)\r
+                       {\r
+                               nextIntervalFrame--;\r
+                       }\r
+                       if (nextIntervalFrame <= 0)\r
+                       {\r
+                               //Psychlops.AppState.statusBar = chacked.ToString();\r
+                               if (chacked > 0)\r
+                               {\r
+                                       var tmp = new System.Windows.Controls.Canvas();\r
+                                       if (stack.Count > 0)\r
+                                       {\r
+                                               foreach (Internal.PrimitiveFigure f in stack)\r
+                                               {\r
+                                                       tmp.Children.Add(f.toNative());\r
+                                               }\r
+                                       }\r
+                                       api_canvas.Children.Clear();\r
+                                       api_canvas.Children.Add(tmp);\r
+                                       stack.Clear();\r
+                                       lock (this)\r
+                                       {\r
+                                               chacked = 0;\r
+                                       }\r
+                                       Psychlops.Internal.Main.canvas_flag.Set();\r
+                               }\r
+                       }\r
+                       System.Threading.Thread.Sleep(0);\r
                }\r
 \r
+               #region Properties\r
+\r
+               public double width { get { return width_; } }\r
+               public double height { get { return height_; } }\r
+               public Point center { get { return new Point(width / 2.0, height / 2.0, 0); } }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+               public override Point getCenter() { return center; }\r
+               public double getHCenter() { return width / 2; }\r
+               public double getVCenter() { return height / 2; }\r
+               public double getRefreshRate() { return 60; }\r
+\r
+               #endregion\r
+\r
+\r
+       }\r
+\r
 \r
-               public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
-               public void msg(string str, double x, double y, Color col)\r
+\r
+       #region primitive tokenizer\r
+\r
+       partial struct Point\r
+       {\r
+               public static implicit operator System.Windows.Point(Point d)\r
                {\r
-                       var let = new Letters(str);\r
-                       let.locate(x, y);\r
-                       this.letters(let, col);\r
+                       return new System.Windows.Point(d.x, d.y);\r
                }\r
-               public void var<Type>(Type val, double x, double y) { msg(val.ToString(), x, y, Color.white); }\r
-               public void var<Type>(Type val, double x, double y, Color col) { msg(val.ToString(), x, y, col); }\r
+       }\r
 \r
+       partial struct Color\r
+       {\r
+               public static implicit operator System.Windows.Media.Color(Color d)\r
+               {\r
+                       return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
+               }\r
+               public static implicit operator System.Windows.Media.SolidColorBrush(Color d)\r
+               {\r
+                       return new SolidColorBrush { Color = d };\r
+               }\r
+       }\r
 \r
-               public int frame;\r
-               public void flip()\r
+       partial struct Stroke\r
+       {\r
+               public void apply(System.Windows.Shapes.Shape target)\r
+               {\r
+                       target.Stroke = this;\r
+                       //target.StrokeDashArray\r
+                       target.StrokeThickness = thick;\r
+               }\r
+               public static implicit operator SolidColorBrush(Stroke d)\r
+               {\r
+                       return new SolidColorBrush { Color = d.color };\r
+               }\r
+       }\r
+\r
+       partial class Line\r
+       {\r
+               public Line clone()\r
+               {\r
+                       return (Line)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Line(Line d)\r
+               {\r
+                       var tmp =  new System.Windows.Shapes.Line() { X1 = d.begin.x, Y1 = d.begin.y, X2 = d.end.x, Y2 = d.end.y };\r
+                       if (d.stroke.thick == 0.0) tmp.Stroke = d.fill;\r
+                       else d.stroke.apply(tmp);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Rectangle\r
+       {\r
+               public Rectangle clone()\r
+               {\r
+                       return (Rectangle)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Rect(Rectangle d)\r
+               {\r
+                       return new System.Windows.Rect(d.v1.x, d.v1.y, d.v2.x, d.v2.y);\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Rectangle(Rectangle d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Rectangle { Width = d.width, Height = d.height, Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Ellipse\r
+       {\r
+               public Ellipse clone()\r
+               {\r
+                       return (Ellipse)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Ellipse(Ellipse d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Ellipse { Width = d.width, Height = d.height, Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+\r
+       partial class Polygon\r
+       {\r
+               public Polygon clone()\r
+               {\r
+                       return (Polygon)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Shapes.Polygon(Polygon d)\r
+               {\r
+                       var tmp = new System.Windows.Shapes.Polygon { Fill = d.fill };\r
+                       d.stroke.apply(tmp);\r
+                       foreach (Point p in d.vertices)\r
+                       {\r
+                               tmp.Points.Add(p);\r
+                       }\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
+       \r
+       partial class Letters\r
+       {\r
+               #region static initializer\r
+               internal static System.Collections.Generic.Dictionary<int, System.Windows.FontWeight> FONT_WEIGHT_BRIDGE;\r
+               internal static System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle> FONT_STYLE_BRIDGE;\r
+               internal static System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment> LETTERS_H_ALIGN_BRIDGE;\r
+               static Letters()\r
+               {\r
+                       FONT_WEIGHT_BRIDGE = new System.Collections.Generic.Dictionary<int, System.Windows.FontWeight>();\r
+                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.normal, System.Windows.FontWeights.Normal);\r
+                       FONT_WEIGHT_BRIDGE.Add((int)Font.Weight.bold, System.Windows.FontWeights.Bold);\r
+                       FONT_STYLE_BRIDGE = new System.Collections.Generic.Dictionary<Font.Style, System.Windows.FontStyle>();\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.normal, System.Windows.FontStyles.Normal);\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.italic, System.Windows.FontStyles.Italic);\r
+                       FONT_STYLE_BRIDGE.Add(Font.Style.oblique, System.Windows.FontStyles.Italic);\r
+                       LETTERS_H_ALIGN_BRIDGE = new System.Collections.Generic.Dictionary<Letters.HorizontalAlign, TextAlignment>();\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.left, TextAlignment.Left);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.center, TextAlignment.Center);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.right, TextAlignment.Right);\r
+                       LETTERS_H_ALIGN_BRIDGE.Add(Letters.HorizontalAlign.not_specified, TextAlignment.Left);\r
+               }\r
+               #endregion\r
+               public Letters clone()\r
                {\r
-                       buffer.Invalidate();\r
-                       frame++;\r
+                       return (Letters)MemberwiseClone();\r
                }\r
+               public static implicit operator System.Windows.Controls.TextBlock(Letters d)\r
+               {\r
+                       //var zapi_shape = new System.Windows.Documents.Glyphs();\r
+                       var tmp = new System.Windows.Controls.TextBlock {\r
+                               Text = d.str, Width = 500, Height = 500,\r
+                               FontSize = d.font.size,\r
+                               //tmp.FontFamily = ,\r
+                               FontStyle = FONT_STYLE_BRIDGE[d.font.style],\r
+                               FontWeight = FONT_WEIGHT_BRIDGE[d.font.weight],\r
+                               TextAlignment = LETTERS_H_ALIGN_BRIDGE[d.align],\r
+                               Foreground = d.fill\r
+                       };\r
+                       double left = 0;\r
+                       switch (d.align)\r
+                       {\r
+                               case Letters.HorizontalAlign.left: break;\r
+                               case Letters.HorizontalAlign.center: left = tmp.Width / 2; break;\r
+                               case Letters.HorizontalAlign.right: left = tmp.Width; break;\r
+                       }\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x - left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y - d.font.size);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
+       }\r
 \r
-               public double width { get { return api_canvas.Width; } }\r
-               public double height { get { return api_canvas.Height; } }\r
+       partial class Image\r
+       {\r
+               internal void initialize__(int wid, int hei)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei);\r
+                       //buffer = new WriteableBitmap(wid, hei);\r
+               }\r
+               internal void create__(int wid, int hei)\r
+               {\r
+                       buffer = new WriteableBitmap(wid, hei);\r
+               }\r
+               delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
+               delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
+               public void field__(System.Func<int, int, System.Windows.Media.Color> func)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc1(field___), func);\r
+                       //buffer.ForEach(func);\r
+               }\r
+               public void field__(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
+               {\r
+                       Canvas.default_api_canvas.Dispatcher.BeginInvoke(new FieldFunc2(field___), func);\r
+                       //buffer.ForEach(func);\r
+               }\r
+               public void field___(System.Func<int, int, System.Windows.Media.Color> func)\r
+               {\r
+                       buffer.ForEach(func);\r
+               }\r
+               public void field___(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func)\r
+               {\r
+                       buffer.ForEach(func);\r
+               }\r
+\r
+               public Image clone()\r
+               {\r
+                       return (Image)MemberwiseClone();\r
+               }\r
+               public static implicit operator System.Windows.Controls.Image(Image d)\r
+               {\r
+                       var tmp = new System.Windows.Controls.Image();\r
+                       tmp.Source = d.buffer;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y);\r
+                       return tmp;\r
+               }\r
+               public UIElement toNative() { return this; }\r
        }\r
 \r
+       #endregion\r
+\r
 \r
 }
\ No newline at end of file