public LineBreakElement[] FindLineBreaks(string text, int length, CultureInfo ci) { LineBreakElement[] lbElements = null; if (text != null) { List<LineBreakElement> elements = new List<LineBreakElement>(); LineBreakEventHandler LineBreakFoundInternal = (object sender, LineBreakEventArgs e) => { elements.Add(new LineBreakElement(e.Position, e.Condition)); }; LineBreakFound += LineBreakFoundInternal; FindLineBreaksInternal(text, length, ci); LineBreakFound = null; lbElements = new LineBreakElement[elements.Count]; elements.CopyTo(lbElements, 0); } return lbElements; }
public LineBreakElement[] FindLineBreaks(string text, int length, string[] breakStrings, byte[] wrapCodes = null) { LineBreakElement[] lbElements = null; if (text != null) { if (length < 0 || length > text.Length) throw new ArgumentOutOfRangeException("length"); List<LineBreakElement> elements = new List<LineBreakElement>(); for (int i = 0; i < length; i++) { LineBreakElement lbElem = new LineBreakElement(i, LineBreakCondition.Prohibited); elements.Add(lbElem); } if (breakStrings != null && breakStrings.Length > 0) { bool bWrap = (wrapCodes != null); for (int i = 0; i < breakStrings.Length; i++) { string strBreak = breakStrings[i]; if (strBreak == null) continue; int breakLength = strBreak.Length; int posBreak = 0; while (posBreak >= 0 && posBreak < length) { int pos = text.IndexOf(strBreak, posBreak); if (pos >= 0 && pos <= length) { int idx = 0; if (bWrap) { if (wrapCodes[i] == 1) idx = pos + breakLength - 1; else idx = pos - 1; } else { idx = pos + breakLength - 1; } if (idx >= 0 && idx < length) { LineBreakElement lbElem = elements[idx]; lbElem.Condition = LineBreakCondition.Allowed; elements[idx] = lbElem; } } else break; posBreak = pos + breakLength; } } } LineBreakElement lbe = elements[elements.Count - 1]; lbe.Condition = LineBreakCondition.Mandatory; elements[elements.Count - 1] = lbe; lbElements = new LineBreakElement[elements.Count]; elements.CopyTo(lbElements, 0); } return lbElements; }