public static TextBlockSetCollection MergeTextLineByVertical(TextBlockCollection sources, bool isBottomFirst = true, double sizeRatio = SIZE_RATIO, double alignRatio = ALIGN_RATIO) { if (sources == null) { throw new NullReferenceException(); } TextBlockSetCollection lineTexts = new TextBlockSetCollection(); TextBlockCollection textsLine, textsLineOrder; TextBlock main, sub; Dictionary <int, int> duplicateDic = new Dictionary <int, int>(); int size, align, index = 0; for (int i = 0; i < sources.Count; i++) { if (!duplicateDic.ContainsKey(i)) { main = sources[i]; size = (int)Math.Round(main.Width * sizeRatio, MidpointRounding.AwayFromZero); align = (int)Math.Round(main.Width * alignRatio, MidpointRounding.AwayFromZero); textsLine = new TextBlockCollection(); textsLine.Add(main); duplicateDic.Add(i, i); for (int j = i + 1; j < sources.Count; j++) { sub = sources[j]; if (!duplicateDic.ContainsKey(j)) { if (IsSameFontSizeByVertical(blockA: main, blockB: sub, size: size) && IsAlignByVertical(blockA: main, blockB: sub, align: align)) { textsLine.Add(sub); duplicateDic.Add(j, j); } } } textsLineOrder = isBottomFirst ? new TextBlockCollection(textsLine.OrderBy(text => text.BottomY)) : new TextBlockCollection(textsLine.OrderBy(text => text.TopY)); lineTexts.Add(new TextBlockSet(index: index++, textBlocks: textsLineOrder)); } } return(lineTexts); }
public static TextBlockSetCollection MergeTextGroupByVertical(TextBlockCollection sources, bool isBottomFirst = true, double sizeRatio = SIZE_RATIO, double alignRatio = ALIGN_RATIO, double spacingRatio = SPACING_RATIO) { if (sources == null) { throw new NullReferenceException(); } TextBlockSetCollection groupTexts = new TextBlockSetCollection(); TextBlockCollection texts, textsOrder; Dictionary <int, int> duplicateDic = new Dictionary <int, int>(); foreach (TextBlock source in sources) { if (!duplicateDic.ContainsKey(source.Index)) { texts = new TextBlockCollection(); texts.Add(source); duplicateDic.Add(source.Index, source.Index); FindVerticalTextsRecursive(current: source, sizeRatio: sizeRatio, alignRatio: alignRatio, spacingRatio: spacingRatio, texts: ref texts, sources: ref sources, duplicateDic: ref duplicateDic); textsOrder = isBottomFirst ? new TextBlockCollection(texts.OrderBy(text => text.BottomY)) : new TextBlockCollection(texts.OrderBy(text => text.TopY)); groupTexts.Add(new TextBlockSet(index: groupTexts.Count, textBlocks: textsOrder)); } } return(groupTexts); }
static void FindVerticalTextsRecursive(TextBlock current, double sizeRatio, double alignRatio, double spacingRatio, ref TextBlockCollection texts, ref TextBlockCollection sources, ref Dictionary <int, int> duplicateDic) { int size = (int)Math.Round(current.Width * sizeRatio, MidpointRounding.AwayFromZero); int align = (int)Math.Round(current.Width * alignRatio, MidpointRounding.AwayFromZero); int spacing = (int)Math.Round(current.Width * spacingRatio, MidpointRounding.AwayFromZero); foreach (TextBlock source in sources) { if (!duplicateDic.ContainsKey(source.Index) && IsSameFontSizeByVertical(blockA: current, blockB: source, size: size) && IsAlignByVertical(blockA: current, blockB: source, align: align) && IsCloseTextByVertical(blockA: current, blockB: source, spacing: spacing)) { texts.Add(source); duplicateDic.Add(source.Index, source.Index); FindVerticalTextsRecursive(current: source, sizeRatio: sizeRatio, alignRatio: alignRatio, spacingRatio: spacingRatio, texts: ref texts, sources: ref sources, duplicateDic: ref duplicateDic); } } }