public void FontMeasures(string fontName) { var writer = new StringWriter(); writer.WriteLine("public class FontMeasure{0}:FontMeasure {{", fontName); writer.WriteLine("\tpublic override void Make() {"); var font = new Font(fontName, 10); CharacterRange[] characterRanges = { new CharacterRange(0, 0) }; var stringFormat = GdiConverter.GetDefaultStringFormat().Clone() as StringFormat; stringFormat.SetMeasurableCharacterRanges(characterRanges); for (var i = 0x21; i < 0x17e; i++) { var c = char.ConvertFromUtf32(i); if (false) { // something wrong here; gives always 0 var mcrSize = GdiUtils.DeviceContext.MeasureCharacterRanges(c, font, new Rectangle(0, 0, 1000, 1000), stringFormat); ReportDetail("{0}\t{1}", c, mcrSize[0].GetBounds(GdiUtils.DeviceContext).Size.ToXwt()); } var size = GdiUtils.GetTextDimension(font, c, new SizeF()); ReportDetail("{0}\t{1}", c, size.Width); writer.WriteLine("Add({0},{1},{2}); // {3}", i, size.Width, size.Height, c); } ReportDetail(writer.ToString()); }
/// <summary> /// Adds a log message about mouse event. /// </summary> /// <param name="annotationView">The annotation view.</param> /// <param name="eventName">The event name.</param> /// <param name="e">The <see cref="ObjectPropertyChangedEventArgs"/> instance containing the event data.</param> private void AddMouseEventLogMessage( AnnotationView annotationView, string eventName, MouseEventArgs e) { // location in viewer space PointF locationInViewerSpace = e.Location; // transformation from annotation space (DIP) to the viewer space PointFTransform toViewerTransform = annotationView.GetPointTransform(AnnotationViewer, AnnotationViewer.Image); PointFTransform inverseTransform = toViewerTransform.GetInverseTransform(); // location in annotation space (DIP) PointF locationInAnnotationSpace = inverseTransform.TransformPoint(locationInViewerSpace); // location in annotation content space PointF locationInAnnotationContentSpace; // matrix from annotation content space to the annotation space (DIP) using (Matrix fromDipToContentSpace = GdiConverter.Convert(annotationView.GetTransformFromContentToImageSpace())) { // DIP space -> annotation content space fromDipToContentSpace.Invert(); PointF[] points = new PointF[] { locationInAnnotationSpace }; fromDipToContentSpace.TransformPoints(points); locationInAnnotationContentSpace = points[0]; } AddLogMessage(string.Format("{0}.{1}: ViewerSpace={2}; ContentSpace={3}", GetAnnotationInfo(annotationView), eventName, locationInViewerSpace, locationInAnnotationContentSpace)); }
protected override void SetEditorFromLayout(IGraphSceneLayout <IVisual, IVisualEdge> layout) { disableChanges = true; this.Distance = layout.Distance; this.Dimension = layout.Dimension; this.Centered = layout.Centered; this.LayoutBackColor = GdiConverter.ToGdi(layout.StyleSheet.BackColor); disableChanges = false; }
/// <summary> /// Returns an annotation selection as <see cref="GraphicsPath"/> in annotation content space. /// </summary> public override GraphicsPath GetSelectionAsGraphicsPath() { GraphicsPath path = new GraphicsPath(); SizeF size = Size; path.AddRectangle(new RectangleF(-size.Width / 2, -size.Height / 2, size.Width, size.Height)); using (Matrix transform = GdiConverter.Convert(GetTransformFromContentToImageSpace())) path.Transform(transform); return(path); }
public virtual void OnPaint(System.Windows.Forms.PaintEventArgs e) { var g = e.Graphics; var b = new SolidBrush(GdiConverter.ToGdi(BackColor())); g.FillRectangle(b, e.ClipRectangle); Layer.OnPaint(Converter.Convert(e)); }
/// <summary> /// AfterRender - Dispose of Graphics object created for rendering. /// </summary> private void RendererAfterRender() { if (graphics != null) { // Check if we only invalidated a rect if (invalidRect != SvgRectF.Empty) { // We actually drew everything on invalidatedRasterImage and now we // need to copy that to rasterImage Graphics tempGraphics = Graphics.FromImage(rasterImage); tempGraphics.DrawImage(invalidatedRasterImage, invalidRect.X, invalidRect.Y, GdiConverter.ToRectangle(invalidRect), GraphicsUnit.Pixel); tempGraphics.Dispose(); tempGraphics = null; // If we currently have an idMapRaster here, then we need to create // a temporary graphics object to draw the invalidated portion from // our main graphics window onto it. if (idMapRaster != null) { tempGraphics = Graphics.FromImage(idMapRaster); tempGraphics.DrawImage(graphics.IdMapRaster, invalidRect.X, invalidRect.Y, GdiConverter.ToRectangle(invalidRect), GraphicsUnit.Pixel); tempGraphics.Dispose(); tempGraphics = null; } else { idMapRaster = graphics.IdMapRaster; } // We have updated the invalid region invalidRect = SvgRectF.Empty; } else { if (idMapRaster != null && idMapRaster != graphics.IdMapRaster) { idMapRaster.Dispose(); } idMapRaster = graphics.IdMapRaster; } graphics.Dispose(); graphics = null; } }
public void OnRender(SvgRectF updatedRect) { if (surface != null) { if (updatedRect == SvgRectF.Empty) { Draw(surface); } else { Draw(surface, GdiConverter.ToRectangle(updatedRect)); } } else { surface = CreateGraphics(); UpdateGraphics(surface); if (updatedRect == SvgRectF.Empty) { Draw(surface); } else { Draw(surface, GdiConverter.ToRectangle(updatedRect)); } surface.Dispose(); surface = null; } // Collect the rendering regions for later updates //SvgDocument doc = (window.Document as SvgDocument); //SvgElement root = (doc.RootElement as SvgElement); //root.CacheRenderingRegion(renderer); }
protected void SetClip(GdiGraphics graphics) { if (_svgElement == null) { return; } SvgRenderingHint hint = _svgElement.RenderingHint; // todo: should we correct the clipping to adjust to the off-one-pixel drawing? graphics.TranslateClip(1, 1); #region Clip with clip // see http://www.w3.org/TR/SVG/masking.html#OverflowAndClipProperties if (_svgElement is ISvgSvgElement || _svgElement is ISvgMarkerElement || _svgElement is ISvgSymbolElement || _svgElement is ISvgPatternElement) { // check overflow property CssValue overflow = _svgElement.GetComputedCssValue("overflow", string.Empty) as CssValue; // TODO: clip can have "rect(10 10 auto 10)" CssPrimitiveValue clip = _svgElement.GetComputedCssValue("clip", string.Empty) as CssPrimitiveValue; string sOverflow = null; if (overflow != null || overflow.CssText == "") { sOverflow = overflow.CssText; } else { if (this is ISvgSvgElement) { sOverflow = "hidden"; } } if (sOverflow != null) { // "If the 'overflow' property has a value other than hidden or scroll, the property has no effect (i.e., a clipping rectangle is not created)." if (sOverflow == "hidden" || sOverflow == "scroll") { RectangleF clipRect = RectangleF.Empty; if (clip != null && clip.PrimitiveType == CssPrimitiveType.Rect) { if (_svgElement is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); ICssRect clipShape = (CssRect)clip.GetRectValue(); if (clipShape.Top.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Y += (float)clipShape.Top.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Left.PrimitiveType != CssPrimitiveType.Ident) { clipRect.X += (float)clipShape.Left.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Right.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Width = (clipRect.Right - clipRect.X) - (float)clipShape.Right.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Bottom.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Height = (clipRect.Bottom - clipRect.Y) - (float)clipShape.Bottom.GetFloatValue(CssPrimitiveType.Number); } } } else if (clip == null || (clip.PrimitiveType == CssPrimitiveType.Ident && clip.GetStringValue() == "auto")) { if (_svgElement is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); } else if (_svgElement is ISvgMarkerElement || _svgElement is ISvgSymbolElement || _svgElement is ISvgPatternElement) { // TODO: what to do here? } } if (clipRect != RectangleF.Empty) { graphics.SetClip(clipRect); } } } } #endregion #region Clip with clip-path // see: http://www.w3.org/TR/SVG/masking.html#EstablishingANewClippingPath if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text || hint == SvgRenderingHint.Clipping || hint == SvgRenderingHint.Masking || hint == SvgRenderingHint.Containment || hint == SvgRenderingHint.Image) { CssPrimitiveValue clipPath = _svgElement.GetComputedCssValue("clip-path", string.Empty) as CssPrimitiveValue; if (clipPath != null && clipPath.PrimitiveType == CssPrimitiveType.Uri) { string absoluteUri = _svgElement.ResolveUri(clipPath.GetStringValue()); SvgClipPathElement eClipPath = _svgElement.OwnerDocument.GetNodeByUri(absoluteUri) as SvgClipPathElement; if (eClipPath != null) { GraphicsPath gpClip = CreateClippingRegion(graphics, eClipPath); RectangleF clipBounds = gpClip != null?gpClip.GetBounds() : RectangleF.Empty; if (clipBounds.Width.Equals(0) || clipBounds.Height.Equals(0)) { return; } SvgUnitType pathUnits = (SvgUnitType)eClipPath.ClipPathUnits.AnimVal; if (pathUnits == SvgUnitType.ObjectBoundingBox) { SvgTransformableElement transElement = _svgElement as SvgTransformableElement; if (transElement != null) { ISvgRect bbox = transElement.GetBBox(); // scale clipping path Matrix matrix = new Matrix(); matrix.Scale((float)bbox.Width, (float)bbox.Height); gpClip.Transform(matrix); graphics.SetClip(gpClip); // offset clip graphics.TranslateClip((float)bbox.X, (float)bbox.Y); } else { throw new NotImplementedException("clip-path with SvgUnitType.ObjectBoundingBox " + "not supported for this type of element: " + _svgElement.GetType()); } } else { graphics.SetClip(gpClip); } gpClip.Dispose(); gpClip = null; } } } #endregion }
private Brush GetBrush(GraphicsPath gp, string propPrefix) { SvgPaint painter; SvgPaintType curPaintType = this.PaintType; if (curPaintType == SvgPaintType.None) { return(null); } else if (curPaintType == SvgPaintType.CurrentColor) { painter = new GdiSvgPaint(_element, "color"); } else { painter = this; } SvgPaintType paintType = painter.PaintType; if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor || paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor || paintType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(painter.Uri); if (_paintFill != null) { Brush br = _paintFill.GetBrush(gp.GetBounds(), this.GetOpacityValue(propPrefix)); if (_paintFill.FillType == GdiFillType.Pattern) { return(br); } if (_paintFill.FillType == GdiFillType.LinearGradient) { LinearGradientBrush lgb = br as LinearGradientBrush; if (lgb != null) { int opacityl = GetOpacity(propPrefix); for (int i = 0; i < lgb.InterpolationColors.Colors.Length; i++) { lgb.InterpolationColors.Colors[i] = Color.FromArgb(opacityl, lgb.InterpolationColors.Colors[i]); } for (int i = 0; i < lgb.LinearColors.Length; i++) { lgb.LinearColors[i] = Color.FromArgb(opacityl, lgb.LinearColors[i]); } return(br); } } if (_paintFill.FillType == GdiFillType.RadialGradient) { PathGradientBrush pgb = br as PathGradientBrush; if (pgb != null) { int opacityl = GetOpacity(propPrefix); for (int i = 0; i < pgb.InterpolationColors.Colors.Length; i++) { pgb.InterpolationColors.Colors[i] = Color.FromArgb(opacityl, pgb.InterpolationColors.Colors[i]); } for (int i = 0; i < pgb.SurroundColors.Length; i++) { pgb.SurroundColors[i] = Color.FromArgb(opacityl, pgb.SurroundColors[i]); } return(br); } } } else { if (curPaintType == SvgPaintType.UriNone || curPaintType == SvgPaintType.Uri) { return(null); } else if (curPaintType == SvgPaintType.UriCurrentColor) { painter = new GdiSvgPaint(_element, "color"); } else { painter = this; } } } if (painter == null || painter.RgbColor == null) { return(null); } SolidBrush brush = new SolidBrush(GdiConverter.ToColor(painter.RgbColor)); int opacity = GetOpacity(propPrefix); brush.Color = Color.FromArgb(opacity, brush.Color); return(brush); }
public static Size GetTextDimension(System.Drawing.Font font, string text, System.Drawing.SizeF textSize) { return(GetTextDimension(DeviceContext, font, text, GdiConverter.GetDefaultStringFormat(), textSize)); }