public static Rect OffsetRect(Rect rect, Vector offset, double scale) { Rect result = new Rect(rect.TopLeft, rect.BottomRight); result.Offset(offset); result.Scale(scale, scale); return result; }
partial static void ScaleRect(ref Rect rect, ref Transform transform) { // Scales the RectangleGeometry to compensate inaccurate hit testing in WPF. // See http://stackoverflow.com/a/19335624/1136211 rect.Scale(1e6, 1e6); var scaleTransform = new ScaleTransform(1e-6, 1e-6); // reverts rect scaling scaleTransform.Freeze(); var transformGroup = new TransformGroup(); transformGroup.Children.Add(scaleTransform); transformGroup.Children.Add(transform); transform = transformGroup; }
Rect GetViewerBounds() { ScrollViewer parentView = this.DesignerView.ScrollViewer; Rect viewerBounds = new Rect(parentView.HorizontalOffset, parentView.VerticalOffset, parentView.ViewportWidth, parentView.ViewportHeight); viewerBounds.Scale(1 / this.designerView.ZoomFactor, 1 / this.designerView.ZoomFactor); return viewerBounds; }
public List<ContentLocation> ContentToPixel(String wordToSearch, int actualPage, double width, double height) { List<ContentLocation> results = new List<ContentLocation>(); if (wordToSearch == null || wordToSearch.Length == 0) return results; using (FileStream fileIn = new FileStream(DocumentPath, FileMode.Open, FileAccess.Read)) { //0- Open and load the PDF Document PdfDocument = new Document(fileIn); for (int pageIndex = 0; pageIndex < PdfDocument.Pages.Count; pageIndex++) { // searches only on the actual page, or on all of them if actualPage == -1 if (actualPage != -1 && actualPage != pageIndex) continue; //1- try to find the piece of content the mouse is hovering TallComponents.PDF.Page page = PdfDocument.Pages[pageIndex]; double widthT = width / page.Width; double heightT = height / page.Height; //retrieve all glyphs from the current page //Notice that you grep a strong reference to the glyphs, otherwise the GC can decide to recycle. GlyphCollection glyphs = page.Glyphs; //default the glyph collection is ordered as they are present in the PDF file. //we want them in reading order. glyphs.Sort(); //the bounds of the last glyph analysed Rect glyphBounds = Rect.Empty; //the current word over which the user clicked StringBuilder currentWord = new StringBuilder(); Rect wordBounds = Rect.Empty; bool foundWord = false; int wordIndex = 0; foreach (TallComponents.PDF.TextExtraction.Glyph glyph in glyphs) { if (glyph.Characters.Length == 0 || wordIndex == 0) { if (foundWord) { double wordWidth = glyphBounds.Right - wordBounds.Left; if (wordWidth > 0) //multi-line word -- the bounds cover only the upper part of it wordBounds = new Rect(wordBounds.Left, wordBounds.Top, wordWidth, wordBounds.Height); foundWord = false; results.Add(new ContentLocation() { Content = currentWord.ToString(), PageIndex = pageIndex, ContentBounds = wordBounds }); } wordIndex = 0; wordBounds = Rect.Empty; currentWord.Clear(); //continue; } glyphBounds = new Rect( glyph.TopLeft.X, page.Height - glyph.TopLeft.Y, glyph.TopRight.X - glyph.TopLeft.X, glyph.TopLeft.Y - glyph.BottomLeft.Y); glyphBounds.Scale(widthT, heightT); if (wordBounds == Rect.Empty) wordBounds = glyphBounds; string chars = String.Empty; foreach (char ch in glyph.Characters) currentWord.Append(ch); if (!wordToSearch[wordIndex].ToString().Equals(glyph.Characters[0].ToString(), StringComparison.CurrentCultureIgnoreCase)) wordIndex = 0; else wordIndex++; if (wordIndex == wordToSearch.Length) { foundWord = true; wordIndex = 0; } } } } return results; }
public string PixelToContent(Point position, int actualPage, double width, double height, out Rect wordBounds) { using (FileStream fileIn = new FileStream(DocumentPath, FileMode.Open, FileAccess.Read)) { //0- Open and load the PDF Document PdfDocument = new Document(fileIn); //1- try to find the piece of content the mouse is hovering TallComponents.PDF.Page page = PdfDocument.Pages[actualPage]; double widthT = width / page.Width; double heightT = height / page.Height; //retrieve all glyphs from the current page //Notice that you grep a strong reference to the glyphs, otherwise the GC can decide to recycle. GlyphCollection glyphs = page.Glyphs; //default the glyph collection is ordered as they are present in the PDF file. //we want them in reading order. glyphs.Sort(); //the bounds of the last glyph analysed Rect glyphBounds = Rect.Empty; //the current word over which the user clicked StringBuilder currentWord = new StringBuilder(); wordBounds = Rect.Empty; bool foundWord = false; foreach (TallComponents.PDF.TextExtraction.Glyph glyph in glyphs) { if (glyph.Characters.Length == 0 || glyph.Characters[0] == ' ') { if (foundWord) { double wordWidth = glyphBounds.Right - wordBounds.Left; if (wordWidth > 0) //multi-line word -- the bounds cover only the upper part of it wordBounds = new Rect(wordBounds.Left, wordBounds.Top, wordWidth, wordBounds.Height); return currentWord.ToString(); } wordBounds = Rect.Empty; currentWord.Clear(); continue; } glyphBounds = new Rect( glyph.TopLeft.X, page.Height - glyph.TopLeft.Y, glyph.TopRight.X - glyph.TopLeft.X, glyph.TopLeft.Y - glyph.BottomLeft.Y); glyphBounds.Scale(widthT, heightT); if (wordBounds == Rect.Empty) wordBounds = glyphBounds; string chars = String.Empty; foreach (char ch in glyph.Characters) currentWord.Append(ch); if (!glyphBounds.Contains(position)) continue; foundWord = true; //Console.WriteLine("{0} -[{1},{2},{3},{4}] Font={5}({6})", chars, glyph.BottomLeft, // glyph.BottomRight, glyph.TopLeft, glyph.TopRight, glyph.Font.Name, glyph.FontSize); } return null; } }
public void CalculateViewport() { Rect actualView = new Rect(0, 0, this.ActualWidth, this.ActualHeight); actualView.Scale(1 / Scale, 1 / Scale); actualView.Offset(-this.XViewOffset, -this.YViewOffset); _viewport = actualView; }
protected override void OnPropertyChanged(PropertyNotificationEventArgs args) { if (args.PropertyName.Equals("Width") || args.PropertyName.Equals("Height")) { double scaleX = Width / NativeSize.Width; double scaleY = Height / NativeSize.Height; _guardUpRegion = GuardedToggleSwitch.GuardUpRegion; _guardUpRegion.Scale(scaleX, scaleY); _switchRegion = GuardedToggleSwitch.SwitchRegion; _switchRegion.Scale(scaleX, scaleY); _guardDownRegion = GuardedToggleSwitch.GuardDownRegion; _guardDownRegion.Scale(scaleX, scaleY); } base.OnPropertyChanged(args); }
internal override void TransformRect(ref Rect rect) { if (rect.IsEmpty) { return; } double scaleX = ScaleX; double scaleY = ScaleY; double centerX = CenterX; double centerY = CenterY; bool translateCenter = centerX != 0 || centerY != 0; if (translateCenter) { rect.X -= centerX; rect.Y -= centerY; } rect.Scale(scaleX, scaleY); if (translateCenter) { rect.X += centerX; rect.Y += centerY; } }
protected override void OnRender(DrawingContext dc) { //warning : много хаков! if (_diagram == null) return; Rect actualViewport = new Rect(0, 0, ActualWidth, ActualHeight); dc.DrawRectangle(Brushes.White, null, actualViewport); Rect boundaries = _diagram.Boundaries; Rect viewport = _diagram.Viewport; double scale; double scaleX = actualViewport.Width / boundaries.Width; double scaleY = actualViewport.Height / boundaries.Height; scale = scaleX > scaleY ? scaleY : scaleX; //прямоугольник с элементами Rect viewerBoundaries = new Rect(0, 0, boundaries.Width * scale, boundaries.Height * scale); dc.DrawRectangle(Brushes.White, GlobalData.BorderPen, viewerBoundaries); //вьюпорт Vector offset = new Vector(-boundaries.Left, -boundaries.Top); Rect viewerViewport = new Rect(viewport.TopLeft, viewport.BottomRight); viewerViewport.Offset(offset.X , offset.Y); viewerViewport.Scale(scale, scale); //пересекаем RectangleGeometry geometryBoundaries = new RectangleGeometry(viewerBoundaries); RectangleGeometry geometryViewport = new RectangleGeometry(viewerViewport); CombinedGeometry geometryCombined = new CombinedGeometry(GeometryCombineMode.Exclude, geometryBoundaries, geometryViewport); SolidColorBrush brush = new SolidColorBrush(Colors.LightGray); dc.PushOpacity(0.3); dc.DrawGeometry(brush, null, geometryCombined); dc.Pop(); dc.PushTransform(new ScaleTransform(scale, scale)); dc.PushTransform(new TranslateTransform(offset.X, offset.Y)); _diagram.DrawItems(dc); dc.Pop(); dc.Pop(); }
/// <summary> /// Forces content to scroll until the coordinate space of a <see cref="T:System.Windows.Media.Visual" /> object is visible. /// </summary> /// <param name="visual">A <see cref="T:System.Windows.Media.Visual" /> that becomes visible.</param> /// <param name="rectangle">A bounding rectangle that identifies the coordinate space to make visible.</param> /// <returns>A <see cref="T:System.Windows.Rect" /> that is visible.</returns> public Rect MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual)) { return Rect.Empty; } rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle); //rectangle.Inflate(50, 50); rectangle.Scale(1.2, 1.2); Rect viewRect = new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight); rectangle.X += viewRect.X; rectangle.Y += viewRect.Y; viewRect.X = CalculateNewScrollOffset(viewRect.Left, viewRect.Right, rectangle.Left, rectangle.Right); viewRect.Y = CalculateNewScrollOffset(viewRect.Top, viewRect.Bottom, rectangle.Top, rectangle.Bottom); SetHorizontalOffset(viewRect.X); SetVerticalOffset(viewRect.Y); rectangle.Intersect(viewRect); rectangle.X -= viewRect.X; rectangle.Y -= viewRect.Y; return rectangle; }
internal static Arc Fill(Size availableSize, double start, double end) { if (availableSize.Width == 0 || double.IsNaN(availableSize.Width) || availableSize.Height == 0 || double.IsNaN(availableSize.Height)) { return new Arc(new Point(0, 0), start, end, 0, false); } var p0 = new Point(0, 0); var arc = new Arc(p0, start, end, 1, false); var rect = new Rect(); var ps = arc.GetPoint(start); rect.Union(ps); rect.Union(arc.GetPoint(end)); foreach (var quadrant in arc.GetQuadrants(start, end)) { rect.Union(quadrant); } var wf = availableSize.Width / rect.Width; var hf = availableSize.Height / rect.Height; var r = Math.Min(wf, hf); rect.Scale(r, r); var v = rect.FindTranslationToCenter(availableSize); return new Arc(p0 + v, start, end, r, false); }