Skip to content

Commit

Permalink
Update Direct2D to new interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Aug 9, 2015
1 parent f0c7772 commit 7fc171b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion NGraphics/ICanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ICanvas
void Transform (Transform transform);
void RestoreState ();

Size MeasureText(string text, Font font);
Size MeasureText (string text, Font font);
void DrawText (string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null);
void DrawPath (IEnumerable<PathOp> ops, Pen pen = null, Brush brush = null);
void DrawRectangle (Rect frame, Pen pen = null, Brush brush = null);
Expand Down
2 changes: 1 addition & 1 deletion NGraphics/SvgReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ void ReadPath (Path p, string pathDescriptor)
match = match.Replace("-", " -");
var args = match.Substring(1).Split(WSC, StringSplitOptions.RemoveEmptyEntries);

Point previousPoint;
Point previousPoint = new Point ();
if (p.Operations.Count > 0 && !(p.Operations.Last() is ClosePath))
previousPoint = p.Operations.Last().EndPoint;

Expand Down
27 changes: 25 additions & 2 deletions Platforms/NGraphics.WindowsStore/Direct2DCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public WICBitmapCanvas (WIC.Bitmap bmp, D2D1.RenderTargetProperties properties,
public IImage GetImage ()
{
renderTarget.EndDraw ();
return new WICBitmapSourceImage (Bmp, factories);
return new WICBitmapSourceImage (Bmp, scale, factories);
}

public Size Size
Expand All @@ -60,14 +60,19 @@ public class WICBitmapSourceImage : IImage
{
readonly WIC.BitmapSource bmp;
readonly Direct2DFactories factories;
readonly double scale;

public WIC.BitmapSource Bitmap { get { return bmp; } }

public WICBitmapSourceImage (WIC.BitmapSource bmp, Direct2DFactories factories = null)
public Size Size { get { return Conversions.ToSize (bmp.Size); } }
public double Scale { get { return scale; } }

public WICBitmapSourceImage (WIC.BitmapSource bmp, double scale, Direct2DFactories factories = null)
{
if (bmp == null)
throw new ArgumentNullException ("bmp");
this.bmp = bmp;
this.scale = scale;
this.factories = factories ?? Direct2DFactories.Shared;
}

Expand Down Expand Up @@ -173,6 +178,14 @@ public void RestoreState ()
}
}

public Size MeasureText (string text, Font font)
{
float maxWidth = float.MaxValue;
float maxHeight = float.MaxValue;
var layout = new DW.TextLayout (factories.DWFactory, text, GetTextFormat (font), maxWidth, maxHeight);
return new Size (layout.Metrics.Width, layout.Metrics.Height);
}

public void DrawText (string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null)
{
var layout = new DW.TextLayout (factories.DWFactory, text, GetTextFormat (font), (float)frame.Width, (float)frame.Height);
Expand Down Expand Up @@ -435,6 +448,16 @@ public static Size2F ToSize2F (this Size size)
return new Size2F ((float)size.Width, (float)size.Height);
}

public static Size ToSize (this Size2F size)
{
return new Size (size.Width, size.Height);
}

public static Size ToSize (this Size2 size)
{
return new Size (size.Width, size.Height);
}

public static RectangleF ToRectangleF (this Rect rect)
{
return new RectangleF ((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height);
Expand Down
4 changes: 2 additions & 2 deletions Platforms/NGraphics.WindowsStore/WinRTPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IImage CreateImage (Color[] colors, int width, double scale = 1.0)
DataPointer = (IntPtr)p,
};
var bmp = new WIC.Bitmap (factories.WICFactory, width, colors.Length / width, pf, data);
return new WICBitmapSourceImage (bmp, factories);
return new WICBitmapSourceImage (bmp, scale, factories);
}
}
}
Expand All @@ -55,7 +55,7 @@ public IImage LoadImage (Stream stream)
}

// Convert the BitmapSource to a Bitmap so we can allow the decoder to go out of memory
return new WICBitmapSourceImage (new WIC.Bitmap (factories.WICFactory, b, WIC.BitmapCreateCacheOption.CacheOnLoad), factories);
return new WICBitmapSourceImage (new WIC.Bitmap (factories.WICFactory, b, WIC.BitmapCreateCacheOption.CacheOnLoad), 1.0, factories);
}

public IImage LoadImage (string path)
Expand Down

0 comments on commit 7fc171b

Please sign in to comment.