Commit d0c450d0 authored by tebjan's avatar tebjan
Browse files

Merge remote-tracking branch 'origin/master' into textEvents

Conflicts:
	Source/SvgElementCollection.cs
parents 5fba6855 31a24ea2
using System;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using Svg.Transforms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
using Svg.Transforms;
namespace Svg
{
......@@ -91,14 +94,80 @@ namespace Svg
/// <summary>
/// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
/// </summary>
protected override void Render(SvgRenderer renderer)
{
if (Width.Value > 0.0f && Height.Value > 0.0f)
{
//TODO:
//base.Render(renderer);
}
/// </summary>
protected override void Render(SvgRenderer renderer)
{
if (Width.Value > 0.0f && Height.Value > 0.0f && this.Href != null)
{
using (Image b = GetImage(this.Href))
{
if (b != null)
{
this.PushTransforms(renderer);
this.SetClip(renderer);
RectangleF srcRect = new RectangleF(0, 0, b.Width, b.Height);
var destRect = new RectangleF(this.Location.ToDeviceValue(),
new SizeF(Width.ToDeviceValue(), Height.ToDeviceValue()));
renderer.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);
this.ResetClip(renderer);
this.PopTransforms(renderer);
}
}
// TODO: cache images... will need a shared context for this
// TODO: support preserveAspectRatio, etc
}
}
protected Image GetImage(Uri uri)
{
try
{
// handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)
if (uri.Scheme == "data")
{
string uriString = uri.OriginalString;
int dataIdx = uriString.IndexOf(",") + 1;
if (dataIdx <= 0 || dataIdx + 1 > uriString.Length)
throw new Exception("Invalid data URI");
// we're assuming base64, as ascii encoding would be *highly* unsusual for images
// also assuming it's png or jpeg mimetype
byte[] imageBytes = Convert.FromBase64String(uriString.Substring(dataIdx));
Image image = Image.FromStream(new MemoryStream(imageBytes));
return image;
}
// should work with http: and file: protocol urls
var httpRequest = WebRequest.Create(uri);
using (WebResponse webResponse = httpRequest.GetResponse())
{
MemoryStream ms = BufferToMemoryStream(webResponse.GetResponseStream());
Image image = Bitmap.FromStream(ms);
return image;
}
}
catch (Exception ex)
{
Trace.TraceError("Error loading image: '{0}', error: {1} ", uri, ex.Message);
return null;
}
}
protected static MemoryStream BufferToMemoryStream(Stream input)
{
byte[] buffer = new byte[4 * 1024];
int len;
MemoryStream ms = new MemoryStream();
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, len);
}
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
......
......@@ -222,6 +222,10 @@ namespace Svg
{
var lastSegment = segments.Last;
// if the last element is a SvgClosePathSegment the position of the previous element should be used because the position of SvgClosePathSegment is 0,0
if (lastSegment is SvgClosePathSegment)
lastSegment = segments[segments.Count - 2];
if (isRelativeX)
{
point.X += lastSegment.End.X;
......
......@@ -95,8 +95,8 @@ namespace Svg
{
if (!this._mock)
{
if (this._owner.OwnerDocument != null)
{
if (this._owner.OwnerDocument != null)
{
this._owner.OwnerDocument.IdManager.AddAndFixID(item, sibling, autoFixID, logElementOldIDNewID);
if (!(item is SvgDocument)) //don't add subtree of a document to parent document
......
......@@ -52,6 +52,11 @@ namespace Svg
this._innerGraphics.DrawImageUnscaled(image, location);
}
public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit graphicsUnit)
{
_innerGraphics.DrawImage(image, destRect, srcRect, graphicsUnit);
}
public void SetClip(Region region)
{
this._innerGraphics.SetClip(region, CombineMode.Complement);
......
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
......@@ -172,7 +173,35 @@ namespace Svg
public virtual string Font
{
get { return this._font; }
set { this._font = value; this.IsPathDirty = true; }
set
{
var parts = value.Split(',');
foreach (var part in parts)
{
//This deals with setting font size. Looks for either <number>px or <number>pt style="font: bold 16px/normal 'trebuchet ms', verdana, sans-serif;"
Regex rx = new Regex(@"(\d+)+(?=pt|px)");
var res = rx.Match(part);
if (res.Success)
{
int fontSize = 10;
int.TryParse(res.Value, out fontSize);
this.FontSize = new SvgUnit((float)fontSize);
}
//this assumes "bold" has spaces around it. e.g.: style="font: bold 16px/normal
rx = new Regex(@"\sbold\s");
res = rx.Match(part);
if (res.Success)
{
this.FontWeight = SvgFontWeight.bold;
}
}
var font = ValidateFontFamily(value);
this._fontFamily = font;
this._font = font; //not sure this is used?
this.IsPathDirty = true;
}
}
/// <summary>
......@@ -289,7 +318,8 @@ namespace Svg
var families = System.Drawing.FontFamily.Families;
// Find a the first font that exists in the list of installed font families.
foreach (var f in fontParts.Where(f => families.Any(family => family.Name == f)))
//styles from IE get sent through as lowercase.
foreach (var f in fontParts.Where(f => families.Any(family => family.Name.ToLower() == f.ToLower())))
{
return f;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment