/// <summary> /// Creates templates representing text inside cell form rectangles /// </summary> /// <param name="rowRectangle"></param> /// <param name="verticalDividers"></param> /// <param name="minWidth"></param> private List <TextRectangleTemplate> CreateTemplates(Rectangle rowRectangle, List <float> verticalDividers, float minWidth) { var templates = new List <TextRectangleTemplate>(); var added = false; for (var index = 1; index < verticalDividers.Count; index++) { var divider = verticalDividers[index]; if (Math.Abs(divider - (rowRectangle.X + rowRectangle.Width)) < 2) { //hom many cells to span var j = index - 1; var span = 0; while (j >= 0) { span++; if (Math.Abs(verticalDividers[j] - rowRectangle.X) < 2) { break; } j--; } //add missing templates var templ = new TextRectangleTemplate { Text = new List <TextRectangle>(), X = rowRectangle.X, Width = rowRectangle.Width, HorizontalSpan = span, VerticalSpan = (int)(rowRectangle.Height / minWidth), Y = rowRectangle.Y, Height = rowRectangle.Height }; templates.Add(templ); added = true; } if (divider > rowRectangle.X + rowRectangle.Width + 1) { if (!added) { var templ = new TextRectangleTemplate { Text = new List <TextRectangle>(), X = rowRectangle.X, Width = rowRectangle.Width, HorizontalSpan = 1, VerticalSpan = (int)(rowRectangle.Height / minWidth), Y = rowRectangle.Y, Height = rowRectangle.Height }; templates.Add(templ); } break; } } return(templates); }
/// <summary> /// Places text rectangles into templates /// </summary> /// <param name="textRect"></param> /// <param name="template"></param> /// <param name="yCoordKey"></param> /// <param name="overflowDelta"></param> private void PlaceTextRectIntoTemplates(List <TextRectangle> textRect, Dictionary <float, List <TextRectangleTemplate> > template, float yCoordKey, float overflowDelta) { if (textRect.Count > 0) { foreach (var textRectangle in textRect) { foreach (var templateYcoord in template.Keys) { var maxHeight = template[templateYcoord].Max(x => x.Height); if ((template[templateYcoord].Count > 0) && (templateYcoord >= textRectangle.Y) && (templateYcoord - maxHeight <= textRectangle.Y - textRectangle.Height)) { var found = false; foreach (var textRectangleTemplate in template[templateYcoord]) { if ((textRectangle.X >= textRectangleTemplate.X) && (textRectangle.X < textRectangleTemplate.X + textRectangleTemplate.Width)) { textRectangleTemplate.Text.Add(textRectangle); textRectangle.Assigned = true; found = true; break; } } if (!found) { //need to adjust other cells var ordered = template[templateYcoord].OrderBy(x => x.X).ToList(); var i = 0; while ((textRectangle.X > ordered[i].X) && (i < ordered.Count)) { i++; } var xCoord = i == 0 ? 0 : ordered[i].X + ordered[i].Width; foreach (var key in template.Keys) { var yCoord = template[key].Max(x => x.Y); var newTempl = new TextRectangleTemplate { X = xCoord, Y = yCoord, Height = textRectangle.Height + overflowDelta, Width = ordered[i].X - xCoord, Text = new List <TextRectangle>() }; template[key].Add(newTempl); if (Math.Abs(key - templateYcoord) < TOLERANCE) { newTempl.Text.Add(textRectangle); } } textRectangle.Assigned = true; } } } } } textRect = textRect.Where(x => !x.Assigned).ToList(); if (textRect.Count > 0) { if (!template.ContainsKey(yCoordKey)) { template[yCoordKey] = new List <TextRectangleTemplate>(); } var templ = new TextRectangleTemplate { Text = textRect }; template[yCoordKey].Add(templ); } }