/// <summary> /// Adds a new text edge to the current dict. /// </summary> /// <param name="textline"></param> /// <param name="align"></param> public void Add(TextLine textline, string align) { var x = GetXCoord(textline, align); float y0 = textline.Y0(); float y1 = textline.Y1(); var te = new TextEdge(x, y0, y1, align); _textedges[align].Add(te); }
/// <summary> /// Returns the index of an existing text edge using the specified x coordinate and alignment. /// </summary> /// <param name="x_coord"></param> /// <param name="align"></param> /// <returns>Returns the index of an existing text edge using the specified x coordinate and alignment. Null if not found.</returns> public int?Find(float x_coord, string align) { var edges = _textedges[align]; for (int i = 0; i < edges.Count; i++) { TextEdge te = edges[i]; if (MathExtensions.AlmostEquals(te.X, x_coord, 0.5)) { return(i); } } return(null); }
/// <summary> /// Resets the TextEdge property to its default value. /// </summary> public void ResetTextEdge() { TextEdge = TextEdge.Right; }
private TextEdges getTextEdges(List <TableLine> lines) { // get all text edges (lines that align with the left, middle and right of chunks of text) that extend // uninterrupted over at least REQUIRED_TEXT_LINES_FOR_EDGE lines of text List <TextEdge> leftTextEdges = new List <TextEdge>(); List <TextEdge> midTextEdges = new List <TextEdge>(); List <TextEdge> rightTextEdges = new List <TextEdge>(); Dictionary <int, List <TextChunk> > currLeftEdges = new Dictionary <int, List <TextChunk> >(); Dictionary <int, List <TextChunk> > currMidEdges = new Dictionary <int, List <TextChunk> >(); Dictionary <int, List <TextChunk> > currRightEdges = new Dictionary <int, List <TextChunk> >(); foreach (TableLine textRow in lines) { foreach (TextChunk text in textRow.TextElements) { if (text.GetText().Equals("")) { continue; // added by bobld } int left = (int)Math.Floor(text.Left); int right = (int)Math.Floor(text.Right); int mid = (int)(left + ((right - left) / 2)); // first put this chunk into any edge buckets it belongs to if (!currLeftEdges.TryGetValue(left, out List <TextChunk> leftEdge)) { leftEdge = new List <TextChunk>(); currLeftEdges[left] = leftEdge; } leftEdge.Add(text); if (!currMidEdges.TryGetValue(mid, out List <TextChunk> midEdge)) { midEdge = new List <TextChunk>(); currMidEdges[mid] = midEdge; } midEdge.Add(text); if (!currRightEdges.TryGetValue(right, out List <TextChunk> rightEdge)) { rightEdge = new List <TextChunk>(); currRightEdges[right] = rightEdge; } rightEdge.Add(text); // now see if this text chunk blows up any other edges //for (Iterator<Map.Entry<Integer, List<TextChunk>>> iterator = currLeftEdges.entrySet().iterator(); iterator.hasNext();) foreach (var entry in currLeftEdges.ToList()) // use tolist to be able to remove { int key = entry.Key; if (key > left && key < right) { currLeftEdges.Remove(key); List <TextChunk> edgeChunks = entry.Value; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom) edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); leftTextEdges.Add(edge); } } } //for (Iterator<Map.Entry<Integer, List<TextChunk>>> iterator = currMidEdges.entrySet().iterator(); iterator.hasNext();) foreach (var entry in currMidEdges.ToList()) { int key = entry.Key; if (key > left && key < right && Math.Abs(key - mid) > 2) { currMidEdges.Remove(key); List <TextChunk> edgeChunks = entry.Value; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom) edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); midTextEdges.Add(edge); } } } //for (Iterator<Map.Entry<Integer, List<TextChunk>>> iterator = currRightEdges.entrySet().iterator(); iterator.hasNext();) foreach (var entry in currRightEdges.ToList()) { int key = entry.Key; if (key > left && key < right) { currRightEdges.Remove(key); List <TextChunk> edgeChunks = entry.Value; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom) edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); rightTextEdges.Add(edge); } } } } } // add the leftovers foreach (int key in currLeftEdges.Keys) { List <TextChunk> edgeChunks = currLeftEdges[key]; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom) edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); leftTextEdges.Add(edge); } } foreach (int key in currMidEdges.Keys) { List <TextChunk> edgeChunks = currMidEdges[key]; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom); edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); midTextEdges.Add(edge); } } foreach (int key in currRightEdges.Keys) { List <TextChunk> edgeChunks = currRightEdges[key]; if (edgeChunks.Count >= REQUIRED_TEXT_LINES_FOR_EDGE) { TextChunk first = edgeChunks[0]; TextChunk last = edgeChunks[edgeChunks.Count - 1]; TextEdge edge = new TextEdge(key, last.Bottom, key, first.Top); // bobld: (key, first.Top, key, last.Bottom) edge.intersectingTextRowCount = Math.Min(edgeChunks.Count, lines.Count); rightTextEdges.Add(edge); } } return(new TextEdges(leftTextEdges, midTextEdges, rightTextEdges)); }
public void UpdateCoords() { var te0 = new TextEdge(0, 1, 2); Assert.Equal(0, te0.X, 4); Assert.Equal(1, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.False(te0.IsValid); Assert.Equal(0, te0.Intersections); te0.UpdateCoords(1, 10); Assert.Equal(1.0, te0.X, 4); Assert.Equal(10, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.False(te0.IsValid); Assert.Equal(1, te0.Intersections); te0.UpdateCoords(1.54f, 8.5f); Assert.Equal(1.27, te0.X, 4); Assert.Equal(8.5, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.False(te0.IsValid); Assert.Equal(2, te0.Intersections); te0.UpdateCoords(2.48f, 5.56f); Assert.Equal(1.6733333333333331, te0.X, 4); Assert.Equal(5.56, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.False(te0.IsValid); Assert.Equal(3, te0.Intersections); te0.UpdateCoords(7.8f, 15.94f); Assert.Equal(3.205, te0.X, 4); Assert.Equal(15.94, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.False(te0.IsValid); Assert.Equal(4, te0.Intersections); te0.UpdateCoords(4.8f, 5.41f); Assert.Equal(3.524, te0.X, 4); Assert.Equal(5.41, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.True(te0.IsValid); Assert.Equal(5, te0.Intersections); // more than 50, intersection count is unchanged te0.UpdateCoords(4.8f, 60.41f); Assert.Equal(3.524, te0.X, 4); Assert.Equal(5.41, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.True(te0.IsValid); Assert.Equal(5, te0.Intersections); te0.UpdateCoords(80.8f, 6.41f); Assert.Equal(16.403333333333332, te0.X, 4); Assert.Equal(6.41, te0.Y0, 4); Assert.Equal(2, te0.Y1, 4); Assert.Equal("left", te0.Align); Assert.True(te0.IsValid); Assert.Equal(6, te0.Intersections); }