public void DrawSymbol(SvgSymbol svgSymbol, bool ignoreDisplay) { if (!CanDraw(svgSymbol, ignoreDisplay)) { return; } _skCanvas.Save(); float x = 0f; float y = 0f; float width = svgSymbol.ViewBox.Width; float height = svgSymbol.ViewBox.Height; if (svgSymbol.CustomAttributes.TryGetValue("width", out string?_widthString)) { if (new SvgUnitConverter().ConvertFrom(_widthString) is SvgUnit _width) { width = _width.ToDeviceValue(null, UnitRenderingType.Horizontal, svgSymbol); } } if (svgSymbol.CustomAttributes.TryGetValue("height", out string?heightString)) { if (new SvgUnitConverter().ConvertFrom(heightString) is SvgUnit _height) { height = _height.ToDeviceValue(null, UnitRenderingType.Vertical, svgSymbol); } } var skRectBounds = SKRect.Create(x, y, width, height); var skMatrixViewBox = SkiaUtil.GetSvgViewBoxTransform(svgSymbol.ViewBox, svgSymbol.AspectRatio, x, y, width, height); var skMatrix = SkiaUtil.GetSKMatrix(svgSymbol.Transforms); SKMatrix.PreConcat(ref skMatrix, ref skMatrixViewBox); SetTransform(skMatrix); SetClipPath(svgSymbol, _disposable); var skPaintOpacity = SetOpacity(svgSymbol, _disposable); var skPaintFilter = SetFilter(svgSymbol, _disposable); foreach (var svgElement in svgSymbol.Children) { Draw(svgElement, ignoreDisplay); } if (skPaintFilter != null) { _skCanvas.Restore(); } if (skPaintOpacity != null) { _skCanvas.Restore(); } _skCanvas.Restore(); }
internal void SetTransform(SKMatrix skMatrix) { var skMatrixTotal = _skCanvas.TotalMatrix; SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrix); _skCanvas.SetMatrix(skMatrixTotal); }
protected override void OnDrawSample(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var blockSize = 30; // create the path var path = new SKPath(); // the rect must be offset as the path uses the center var rect = SKRect.Create(blockSize / -2, blockSize / -2, blockSize, blockSize); path.AddRect(rect); // move the path around: across 1 block var offsetMatrix = SKMatrix.MakeScale(2 * blockSize, blockSize); // each row, move across a bit / offset SKMatrix.PreConcat(ref offsetMatrix, SKMatrix.MakeSkew(0.5f, 0)); // create the paint var paint = new SKPaint(); paint.PathEffect = SKPathEffect.Create2DPath(offsetMatrix, path); paint.Color = SKColors.LightGray; // draw a rectangle canvas.DrawRect(SKRect.Create(width, height), paint); }
internal PdfType1Font(PdfDirectObject baseObject) : base(baseObject) { codeToBytesMap = new Dictionary <int, byte[]>(); var fd = FontDescriptor; Type1Font t1 = null; bool fontIsDamaged = false; if (fd != null) { // a Type1 font may contain a Type1C font var fontFile3 = fd.FontFile3; if (fontFile3 != null) { throw new ArgumentException("Use PDType1CFont for FontFile3"); } // or it may contain a PFB var fontFile = fd.FontFile; if (fontFile != null) { try { t1 = LoadType1Font(fontFile); } catch (DamagedFontException e) { Debug.WriteLine($"warn: Can't read damaged embedded Type1 font {fd.FontName} {e}"); fontIsDamaged = true; } catch (IOException e) { Debug.WriteLine($"error: Can't read the embedded Type1 font {fd.FontName} {e}"); fontIsDamaged = true; } } } isEmbedded = t1 != null; isDamaged = fontIsDamaged; type1font = t1; // find a generic font to use for rendering, could be a .pfb, but might be a .ttf if (type1font != null) { genericFont = type1font; } else { FontMapping <BaseFont> mapping = FontMappers.Instance.GetBaseFont(BaseFont, fd); genericFont = mapping.Font; if (mapping.IsFallback) { Debug.WriteLine($"warn Using fallback font {genericFont.Name} for {BaseFont}"); } } ReadEncoding(); fontMatrixTransform = FontMatrix; fontMatrixTransform = fontMatrixTransform.PreConcat(SKMatrix.CreateScale(1000, 1000)); }
public static SKMatrix Concat(this SKMatrix m, SKMatrix m2) { var target = m; SKMatrix.PreConcat(ref target, m2); return(target); }
public override void MoveTo(SKRect newBox) { var oldBox = Box; if (oldBox.Width != newBox.Width || oldBox.Height != newBox.Height) { Appearance.Normal[null] = null; } //base.MoveTo(newBox); var dif = SKMatrix.MakeIdentity(); SKMatrix.PreConcat(ref dif, SKMatrix.MakeTranslation(newBox.MidX, newBox.MidY)); SKMatrix.PreConcat(ref dif, SKMatrix.MakeScale(newBox.Width / oldBox.Width, newBox.Height / oldBox.Height)); SKMatrix.PreConcat(ref dif, SKMatrix.MakeTranslation(-oldBox.MidX, -oldBox.MidY)); var oldPaths = Paths; var newPaths = new List <SKPath>(); foreach (var path in oldPaths) { var vertices = path.Points; for (int i = 0; i < vertices.Length; i++) { vertices[i] = dif.MapPoint(vertices[i]); } var newPath = new SKPath(); newPath.AddPoly(vertices, false); newPaths.Add(newPath); } Paths = newPaths; foreach (var oldPath in oldPaths) { oldPath.Dispose(); } }
public void Zoom(SKPoint point, float scale) { var currentScale = GetScale(); var scaleFactor = scale / currentScale; SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeScale(scaleFactor, scaleFactor, point.X, point.Y)); }
public void Rotate(SKPoint point, float radians) { var currentAngle = GetAngleInRadians(); var angleDiff = radians - currentAngle; SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeRotation(angleDiff, point.X, point.Y)); }
public void MoveToPoint(SKPoint point) { var center = GetCenter(); SKPoint diff = center - point; SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeTranslation(diff.X, diff.Y)); }
public override void OnDraw(SKCanvas canvas, Attributes ignoreAttributes, Drawable?until) { if (until != null && this == until) { return; } if (Image != null) { using var skImagePaint = new SKPaint() { IsAntialias = true, FilterQuality = SKFilterQuality.High }; canvas.DrawImage(Image, SrcRect, DestRect, skImagePaint); } if (FragmentDrawable != null) { canvas.Save(); var skMatrixTotal = canvas.TotalMatrix; SKMatrix.PreConcat(ref skMatrixTotal, ref FragmentTransform); canvas.SetMatrix(skMatrixTotal); FragmentDrawable.Draw(canvas, ignoreAttributes, until); canvas.Restore(); } }
/** * Constructor. * * @param fontDictionary the corresponding dictionary * @throws IOException it something went wrong */ public PdfType1CFont(PdfDirectObject fontDictionary) : base(fontDictionary) { FontDescriptor fd = FontDescriptor; byte[] bytes = null; if (fd != null) { var ff3Stream = fd.FontFile3; if (ff3Stream != null) { bytes = ff3Stream.BaseDataObject.ExtractBody(true).GetBuffer(); if (bytes.Length == 0) { Debug.WriteLine($"error: Invalid data for embedded Type1C font {Name}"); bytes = null; } } } bool fontIsDamaged = false; CFFType1Font cffEmbedded = null; try { if (bytes != null) { // note: this could be an OpenType file, fortunately CFFParser can handle that CFFParser cffParser = new CFFParser(); cffEmbedded = (CFFType1Font)cffParser.Parse(bytes, new FF3ByteSource(fd, bytes))[0]; } } catch (Exception e) { Debug.WriteLine($"error: Can't read the embedded Type1C font {Name} {e}"); fontIsDamaged = true; } isDamaged = fontIsDamaged; cffFont = cffEmbedded; if (cffFont != null) { genericFont = cffFont; isEmbedded = true; } else { FontMapping <BaseFont> mapping = FontMappers.Instance.GetBaseFont(BaseFont, fd); genericFont = mapping.Font; if (mapping.IsFallback) { Debug.WriteLine($"warn: Using fallback font {genericFont.Name} for {BaseFont}"); } isEmbedded = false; } ReadEncoding(); fontMatrixTransform = FontMatrix; fontMatrixTransform = fontMatrixTransform.PreConcat(SKMatrix.CreateScale(1000, 1000)); }
private static void CalculateBounds(bool forDrawing, SkiaSharp.Extended.Svg.SKSvg svg, Aspect aspect, double maxWidth, double maxHeight, out SKMatrix drawMatrix, out SKSizeI pixelSize) { // Determine bounds of SVG SKRect svgBounds = svg.ViewBox; if (svgBounds.Width == 0 || svgBounds.Height == 0) { svgBounds = new SKRect(0, 0, svg.CanvasSize.Width, svg.CanvasSize.Height); } // Unable to scale? double scaleX, scaleY; if (svgBounds.Width == 0 || svgBounds.Height == 0 || double.IsInfinity(maxWidth) || double.IsInfinity(maxHeight)) { scaleX = scaleY = 1; } else { // Scale properly double ratioX = maxWidth / svgBounds.Width, ratioY = maxHeight / svgBounds.Height; switch (aspect) { case Aspect.AspectFill: scaleX = scaleY = Math.Max(ratioX, ratioY); break; case Aspect.AspectFit: scaleX = scaleY = Math.Min(ratioX, ratioY); break; case Aspect.Fill: default: scaleX = ratioX; scaleY = ratioY; break; } } // Increase sizing for drawing if (forDrawing) { scaleX *= DemoUtilities.DisplayDensity; scaleY *= DemoUtilities.DisplayDensity; } // Apply scaling if (svg.ViewBox.Width != 0 && svg.ViewBox.Height != 0) { drawMatrix = SKMatrix.MakeTranslation(-svg.ViewBox.Left, -svg.ViewBox.Top); } else { drawMatrix = SKMatrix.MakeIdentity(); } SKMatrix.PreConcat(ref drawMatrix, SKMatrix.MakeScale((float)scaleX, (float)scaleY)); pixelSize = new SKSizeI((int)Math.Ceiling(scaleX * svgBounds.Width), (int)Math.Ceiling(scaleY * svgBounds.Height)); }
public void MatrixCanPreConcat() { var a = SKMatrix.MakeTranslation(10, 20); var b = SKMatrix.MakeTranslation(5, 7); SKMatrix.PreConcat(ref a, ref b); Assert.Equal(SKMatrix.MakeTranslation(15, 27).Values, a.Values); }
public override void Scan(GraphicsState state) { var tlm = state.TextState.Tlm; SKMatrix.PreConcat(ref tlm, new SKMatrix { Values = new[] { 1, 0, 0, 0, 1, (float)-state.Lead, 0, 0, 1 } }); state.TextState.Tlm = state.TextState.Tm = tlm; }
public void Scale(float width, float height, MatrixOrder order) { var m = SKMatrix.MakeScale(width, height); if (order == MatrixOrder.Append) { SKMatrix.PostConcat(ref _m, ref m); } else { SKMatrix.PreConcat(ref _m, ref m); } }
public void RotateAt(float angleDegrees, PointF midPoint, MatrixOrder order) { var m = SKMatrix.MakeRotationDegrees(angleDegrees, midPoint.X, midPoint.Y); if (order == MatrixOrder.Append) { SKMatrix.PostConcat(ref _m, ref m); } else { SKMatrix.PreConcat(ref _m, ref m); } }
public void Translate(float left, float top, MatrixOrder order) { var m = SKMatrix.MakeTranslation(left, top); if (order == MatrixOrder.Append) { SKMatrix.PostConcat(ref _m, ref m); } else { SKMatrix.PreConcat(ref _m, ref m); } }
public void Multiply(Matrix matrix, MatrixOrder order) { var m = ((Matrix)matrix).SKMatrix; if (order == MatrixOrder.Append) { SKMatrix.PostConcat(ref _m, ref m); } else { SKMatrix.PreConcat(ref _m, ref m); } }
/** * <summary>Expands the size of this quad stretching around its center.</summary> * <param name="valueX">Expansion's horizontal extent.</param> * <param name="valueY">Expansion's vertical extent.</param> * <returns>This quad.</returns> */ public Quad Inflate(float valueX, float valueY) { SKRect oldBounds = GetBounds(); var matrix = SKMatrix.MakeTranslation(oldBounds.MidX, oldBounds.MidY); SKMatrix.PreConcat(ref matrix, SKMatrix.MakeScale(1 + valueX * 2 / oldBounds.Width, 1 + valueY * 2 / oldBounds.Height)); SKMatrix.PreConcat(ref matrix, SKMatrix.MakeTranslation(-(oldBounds.MidX), -(oldBounds.MidY))); pointTopLeft = matrix.MapPoint(pointTopLeft); pointTopRight = matrix.MapPoint(pointTopRight); pointBottomRight = matrix.MapPoint(pointBottomRight); pointBottomLeft = matrix.MapPoint(pointBottomLeft); return(this); }
public override void Scan(GraphicsState state) { var ctm = state.Ctm; SKMatrix.PreConcat(ref ctm, Value); state.Ctm = ctm; var context = state.Scanner.RenderContext; if (context != null) { //var matrix = context.TotalMatrix; //SKMatrix.PreConcat(ref matrix, ctm); context.SetMatrix(state.Ctm); } }
public override void RotateTransform(float angle, MatrixOrder order) { if (order == MatrixOrder.Prepend) { _image.RotateDegrees(angle); } if (order == MatrixOrder.Append) { //checkthis var old = _image.TotalMatrix; var extra = SKMatrix.MakeRotation(angle); SKMatrix.PreConcat(ref old, extra); _image.SetMatrix(old); } }
private void DrawGrid(SKCanvas canvas, double dx, double dy, double zx, double zy) { float gw = (float)_view.Width; float gh = (float)_view.Height; float cw = 15.0f; float ch = 15.0f; canvas.Save(); canvas.Translate((float)dx, (float)dy); canvas.Scale((float)zx, (float)zy); var hlattice = SKMatrix.MakeScale(cw, ch); SKMatrix.PreConcat(ref hlattice, SKMatrix.MakeRotation((float)(Math.PI * 0.0 / 180.0))); var vlattice = SKMatrix.MakeScale(cw, ch); SKMatrix.PreConcat(ref vlattice, SKMatrix.MakeRotation((float)(Math.PI * 90.0 / 180.0))); using (var heffect = SKPathEffect.Create2DLine((float)(1.0 / zx), hlattice)) using (var veffect = SKPathEffect.Create2DLine((float)(1.0 / zx), vlattice)) using (var hpaint = new SKPaint()) using (var vpaint = new SKPaint()) { hpaint.IsAntialias = false; hpaint.Color = SKColors.LightGray; hpaint.PathEffect = heffect; canvas.DrawRect(SKRect.Create(0.0f, ch, gw, gh - ch), hpaint); vpaint.IsAntialias = false; vpaint.Color = SKColors.LightGray; vpaint.PathEffect = veffect; canvas.DrawRect(SKRect.Create(cw, 0.0f, gw - cw, gh), vpaint); } using (SKPaint strokePaint = new SKPaint()) { strokePaint.IsAntialias = false; strokePaint.StrokeWidth = (float)(1.0 / zx); strokePaint.Color = SKColors.Red; strokePaint.Style = SKPaintStyle.Stroke; canvas.DrawRect(SKRect.Create(0.0f, 0.0f, gw, gh), strokePaint); } canvas.Restore(); }
public void Rotate(float elementRotation, MatrixOrder append) { var rot = SKMatrix.MakeRotationDegrees(elementRotation); var main = new SKMatrix(M11, M12, OffsetX, M21, M22, OffsetY, 0, 0, 1); if (append == MatrixOrder.Prepend) { SKMatrix.PreConcat(ref main, rot); } else { SKMatrix.PostConcat(ref main, rot); } this.SetMatrix(main.ScaleX, main.SkewX, main.SkewY, main.ScaleY, main.TransX, main.TransY, MatrixTypes.TRANSFORM_IS_UNKNOWN); }
public void Translate(float elementTranslationX, float elementTranslationY, MatrixOrder append) { var main = new SKMatrix(M11, M12, OffsetX, M21, M22, OffsetY, 0, 0, 1); var tra = SKMatrix.MakeTranslation(elementTranslationX, elementTranslationY); if (append == MatrixOrder.Prepend) { SKMatrix.PreConcat(ref main, tra); } else { SKMatrix.PostConcat(ref main, tra); } this.SetMatrix(main.ScaleX, main.SkewX, main.SkewY, main.ScaleY, main.TransX, main.TransY, MatrixTypes.TRANSFORM_IS_UNKNOWN); }
public void ZoomByScaleFactor(SKPoint point, float scaleFactor) { var currentScale = GetScale(); currentScale *= scaleFactor; if (currentScale < MinScale || currentScale > MaxScale) { return; } if (!CenterBoundary.IsEmpty) { var center = GetCenter(); if (!CenterBoundary.Contains(center)) { return; } } SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeScale(scaleFactor, scaleFactor, point.X, point.Y)); }
private void DrawTransparencyBackground(SKCanvas canvas, int width, int height, float scale) { var blockSize = BaseBlockSize * scale; var offsetMatrix = SKMatrix.MakeScale(2 * blockSize, blockSize); var skewMatrix = SKMatrix.MakeSkew(0.5f, 0); SKMatrix.PreConcat(ref offsetMatrix, ref skewMatrix); using (var path = new SKPath()) using (var paint = new SKPaint()) { path.AddRect(SKRect.Create(blockSize / -2, blockSize / -2, blockSize, blockSize)); paint.PathEffect = SKPathEffect.Create2DPath(offsetMatrix, path); paint.Color = AlternatePaneColor; canvas.DrawRect(SKRect.Create(width + blockSize, height + blockSize), paint); } }
// TODO: //public void DrawAnchor(SvgAnchor svgAnchor, bool ignoreDisplay) //{ // _skCanvas.Save(); // // var skMatrix = SkiaUtil.GetSKMatrix(svgAnchor.Transforms); // SetTransform(skMatrix); // // var skPaintOpacity = SetOpacity(svgAnchor, _disposable); // // foreach (var svgElement in svgAnchor.Children) // { // Draw(svgElement, ignoreDisplay); // } // // if (skPaintOpacity != null) // { // _skCanvas.Restore(); // } // // _skCanvas.Restore(); //} public void DrawFragment(SvgFragment svgFragment, bool ignoreDisplay) { float x = svgFragment.X.ToDeviceValue(null, UnitRenderingType.Horizontal, svgFragment); float y = svgFragment.Y.ToDeviceValue(null, UnitRenderingType.Vertical, svgFragment); var skSize = SkiaUtil.GetDimensions(svgFragment); _skCanvas.Save(); switch (svgFragment.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: break; default: var skClipRect = SKRect.Create(x, y, skSize.Width, skSize.Height); _skCanvas.ClipRect(skClipRect, SKClipOperation.Intersect); break; } var skMatrixViewBox = SkiaUtil.GetSvgViewBoxTransform(svgFragment.ViewBox, svgFragment.AspectRatio, x, y, skSize.Width, skSize.Height); var skMatrix = SkiaUtil.GetSKMatrix(svgFragment.Transforms); SKMatrix.PreConcat(ref skMatrix, ref skMatrixViewBox); SetTransform(skMatrix); var skPaintOpacity = SetOpacity(svgFragment, _disposable); foreach (var svgElement in svgFragment.Children) { Draw(svgElement, ignoreDisplay); } if (skPaintOpacity != null) { _skCanvas.Restore(); } _skCanvas.Restore(); }
public static SKMatrix GetMatrix(SkiaSharp.Extended.Svg.SKSvg svg, float left, float top, float widthR, float heightR, float rotate = 0) { float canvasMin = Math.Min(widthR, heightR); // get the size of the picture float svgMax = Math.Max(svg.Picture.CullRect.Width, svg.Picture.CullRect.Height); // get the scale to fill the screen float scale = canvasMin / svgMax; var width = svg.Picture.CullRect.Width * scale; var height = svg.Picture.CullRect.Height * scale; var matrix = SKMatrix.MakeIdentity(); if (rotate > 0) { SKMatrix.PreConcat(ref matrix, SKMatrix.MakeRotationDegrees(rotate, (left + widthR / 2), (top + heightR / 2))); } SKMatrix.PreConcat(ref matrix, SKMatrix.MakeTranslation(left + (widthR - width) / 2F, top + (heightR - height) / 2F)); SKMatrix.PreConcat(ref matrix, SKMatrix.MakeScale(scale, scale)); return(matrix); }
public override void Scan(GraphicsState state) { if (state.Scanner?.RenderContext != null) { var canvas = state.Scanner.RenderContext; var image = LoadImage(state); if (image != null) { var size = Size; var matrix = canvas.TotalMatrix; SKMatrix.PreConcat(ref matrix, Matrix); SKMatrix.PreConcat(ref matrix, SKMatrix.MakeTranslation(0, -size.Height)); canvas.SetMatrix(matrix); var rect = SKRect.Create(0, 0, size.Width, size.Height); var test = canvas.TotalMatrix.MapRect(rect); canvas.DrawBitmap(image, 0, 0, new SKPaint { FilterQuality = SKFilterQuality.Medium }); } } }
public void MoveByVector(SKPoint vector) { SKMatrix invertedMatrix; if (!Matrix.TryInvert(out invertedMatrix)) { return; } var resultPoint = invertedMatrix.MapVector(vector.X, vector.Y); SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeTranslation(resultPoint.X, resultPoint.Y)); if (CenterBoundary.IsEmpty) { return; } var center = GetCenter(); if (!CenterBoundary.Contains(center)) { //rollback SKMatrix.PreConcat(ref Matrix, SKMatrix.MakeTranslation(-resultPoint.X, -resultPoint.Y)); } }