public SimpleDocument(Size size, Font font, String text) { this.size = size; this.font = font; this.sections = new List<SimpleSection>(); this.sections.Add(new SimpleSection()); this.insertPos = this.sections.Last().End; Insert(text); DrawText(); }
public void Delete(SimpleChar sc) { var oldText = Lines.SelectMany(x => x.Line); List<SimpleChar> newText = oldText.Where(x => x != sc && !x.IsLineEnd).ToList(); var lines = new List<SimpleLine>(); bool isLoose = true; do { var line = new SimpleLine(this); line.Fill(newText, isLoose = !isLoose); lines.Add(line); } while (newText.Count > 0); this.Lines = lines; }
public void Insert(SimpleChar position, List<SimpleChar> insertChars) { var oldText = Lines.SelectMany(x => x.Line).ToList(); int insertPos = oldText.IndexOf(position); var leftText = oldText.Take(insertPos); var rightText = oldText.Skip(insertPos); List<SimpleChar> newText= leftText.Concat(insertChars).Concat(rightText).Where(x => !x.IsLineEnd).ToList(); var lines = new List<SimpleLine>(); bool isLoose = true; do { var line = new SimpleLine(this); line.Fill(newText, isLoose=!isLoose); lines.Add(line); } while (newText.Count > 0); this.Lines = lines; }
public void DeleteLeft() { var currentSec = this.insertPos.Line.Section; if (this.insertPos == currentSec.Home) { // 段首 int currentSecIndex = sections.IndexOf(currentSec); if (currentSecIndex > 0) { var previousSec = sections[currentSecIndex - 1]; previousSec.Merge(currentSec); sections.Remove(currentSec); if (!this.insertPos.IsPrintableChar()) { // 空段落 this.insertPos = previousSec.End; } } } else { var deleteChar = this.insertPos; do { deleteChar = PreviousChar(deleteChar); } while (!deleteChar.IsPrintableChar()); var nextChar = this.insertPos; if (this.insertPos == currentSec.End) { nextChar = null; } else { while (!nextChar.IsPrintableChar()) { nextChar = NextChar(nextChar); } } currentSec.Delete(deleteChar); if (!this.insertPos.IsPrintableChar()) { if (nextChar != null) { this.insertPos = nextChar; } else { this.insertPos = currentSec.End; } } } DrawText(); }
private SimpleChar PreviousChar(SimpleChar c) { int index = c.Line.Line.IndexOf(c); if (index > 0) { return c.Line.Line[index - 1]; } else { int lineIndex = c.Line.Section.Lines.IndexOf(c.Line); if (lineIndex > 0) { return c.Line.Section.Lines[lineIndex - 1].End; } else { int secIndex = sections.IndexOf(c.Line.Section); if (secIndex > 0) { return sections[secIndex - 1].End; } else { return sections[0].Home; } } } }
private SimpleChar NextChar(SimpleChar c) { int index = c.Line.Line.IndexOf(c); if (index < c.Line.Line.Count - 1) { return c.Line.Line[index + 1]; } else { int lineIndex = c.Line.Section.Lines.IndexOf(c.Line); if (lineIndex < c.Line.Section.Lines.Count - 1) { return c.Line.Section.Lines[lineIndex + 1].Home; } else { int secIndex = sections.IndexOf(c.Line.Section); if (secIndex < sections.Count - 1) { return sections[secIndex + 1].Home; } else { return sections.Last().End; } } } }
private SimpleChar HomeChar(SimpleChar c) { if (c.Line.Line.Contains(c)) { return c.Line.Line.First(); } else { return c; } }
private SimpleChar EndChar(SimpleChar c) { if (c.Line.Line.Contains(c)) { return c.Line.End; } else { return c; } }
public void SetInsertPosByMove(MoveOperation op) { switch (op) { case MoveOperation.Left: this.insertPos = PreviousChar(this.insertPos); // 前一个字符是行末,并且不是段末,再往前找一格 if (!this.insertPos.IsPrintableChar() && this.insertPos != this.insertPos.Line.Section.End) { this.insertPos = PreviousChar(this.insertPos); } break; case MoveOperation.Right: // 字符是行末,并且不是段末,多往后找一格 if (!this.insertPos.IsPrintableChar() && this.insertPos != this.insertPos.Line.Section.End) { this.insertPos = NextChar(this.insertPos); } this.insertPos = NextChar(this.insertPos); break; case MoveOperation.Up: PointF pUp = new PointF(this.insertPos.X, this.insertPos.Y - this.font.Height - ROW_SPACING); SetInsertPosByLocation(pUp); break; case MoveOperation.Down: PointF pDown = new PointF(this.insertPos.X, this.insertPos.Y + this.font.Height + ROW_SPACING); SetInsertPosByLocation(pDown); break; case MoveOperation.Home: this.insertPos = HomeChar(this.insertPos); break; case MoveOperation.End: this.insertPos = EndChar(this.insertPos); break; default: break; } }
public void SetInsertPosByLocation(PointF location) { this.insertPos = LocateChar(location); }
public void Insert(String text) { text = text.Replace("\r\n", "\n").Replace("\r", "\n"); var secs = ConvertChars(text); var currentSec = this.insertPos.Line.Section; int currentSecIndex = sections.IndexOf(currentSec); if (secs.Count > 1) { var newSec = currentSec.Split(this.insertPos); this.sections.Insert(currentSecIndex + 1, newSec); } currentSec.Insert(this.insertPos, secs[0]); var lastInsertChar = secs[0].Count > 0 ? secs[0].Last() : currentSec.End; for (int i = 1; i < secs.Count-1; i++) { SimpleSection sec = new SimpleSection(); sec.Insert(sec.End, secs[i]); this.sections.Insert(currentSecIndex + i, sec); lastInsertChar = sec.End; } if (secs.Count > 1) { var nextSec = sections[currentSecIndex + secs.Count - 1]; nextSec.Insert(nextSec.Home, secs[secs.Count - 1]); if (secs[secs.Count - 1].Count > 0) { lastInsertChar = secs[secs.Count - 1].Last(); } else { lastInsertChar = null; this.insertPos = nextSec.Home; } } if (lastInsertChar != null) { this.insertPos = NextChar(lastInsertChar); } DrawText(); }
public void DeleteRight() { var currentSec = this.insertPos.Line.Section; if (this.insertPos == currentSec.End) { // 段末 int currentSecIndex = sections.IndexOf(currentSec); if (currentSecIndex < sections.Count - 1) { var nextSec = sections[currentSecIndex + 1]; this.insertPos = nextSec.Home; currentSec.Merge(nextSec); sections.Remove(nextSec); if (!this.insertPos.IsPrintableChar()) { // 空段落 this.insertPos = currentSec.End; } } } else { var deleteChar = this.insertPos; while (!deleteChar.IsPrintableChar()) { deleteChar = NextChar(deleteChar); } var nextChar = NextChar(deleteChar); currentSec.Delete(deleteChar); this.insertPos = nextChar; } DrawText(); }
public SimpleSection Split(SimpleChar position) { var oldText = Lines.SelectMany(x => x.Line).ToList(); int splitPos = oldText.IndexOf(position); var leftText = oldText.Take(splitPos).Where(x => !x.IsLineEnd).ToList(); var rightText = oldText.Skip(splitPos).Where(x => !x.IsLineEnd).ToList(); var lines = new List<SimpleLine>(); bool isLoose = true; do { var line = new SimpleLine(this); line.Fill(leftText, isLoose = !isLoose); lines.Add(line); } while (leftText.Count > 0); this.Lines = lines; var splitOne = new SimpleSection(); splitOne.Insert(splitOne.End, rightText); return splitOne; }