private int AlignCenterLineTextTokens(ref Vector2f cursor, List <TextToken2D> tokensInLine, int offsetLine) { cursor.X += offsetLine / 2; int maxHeight = 0; IntRect tokenCanevas = new IntRect(); int i = 0; foreach (TextToken2D textToken2D in tokensInLine) { tokenCanevas = textToken2D.Canevas; textToken2D.Position = cursor; cursor.X += tokenCanevas.Width; if (tokenCanevas.Height > maxHeight) { maxHeight = tokenCanevas.Height; } if (i < tokensInLine.Count - 1) { cursor.X += AObject2DFactory.GetWidthFromTextToken(textToken2D); } i++; } return(maxHeight); }
private void AlignTextTokens() { FloatRect bounds = this.Bounds; Vector2f startCursor = new Vector2f(bounds.Left, bounds.Top); Vector2f maxCursor = new Vector2f(bounds.Left + bounds.Width, bounds.Top + bounds.Height); Vector2f currentCursor = startCursor; int maxHeight = 0; List <TextToken2D> tokensInLine = new List <TextToken2D>(); IEnumerator <TextToken2D> tokenEnumerator = this.textToken2Ds.GetEnumerator(); int lineWidth = 0; int maxWidth = (int)(maxCursor.X - startCursor.X); bool notReachEnd = true; IntRect tokenCanevas = new IntRect(); TextLineHandler LineHandler = this.AlignLeftLineTextTokens; switch (this.ParagraphAlignment) { case Alignment.LEFT: LineHandler = this.AlignLeftLineTextTokens; break; case Alignment.RIGHT: LineHandler = this.AlignRightLineTextTokens; break; case Alignment.CENTER: LineHandler = this.AlignCenterLineTextTokens; break; case Alignment.JUSTIFY: LineHandler = this.AlignJustifyLineTextTokens; break; } while (notReachEnd) { while ((notReachEnd = tokenEnumerator.MoveNext()) && tokenEnumerator.Current is ParameterTextToken2D) { ; } //notReachEnd = tokenEnumerator.MoveNext(); if (notReachEnd) { tokenCanevas = tokenEnumerator.Current.Canevas; } if (notReachEnd == false || lineWidth + tokenCanevas.Width > maxWidth || tokenEnumerator.Current.FullText == "\n") { int diffWidth = maxWidth - lineWidth; lineWidth = tokenCanevas.Width; if (notReachEnd) { lineWidth += AObject2DFactory.GetWidthFromTextToken(tokenEnumerator.Current); } if (tokensInLine.Count > 0) { diffWidth += AObject2DFactory.GetWidthFromTextToken(tokensInLine.Last()); maxHeight = LineHandler(ref currentCursor, tokensInLine, diffWidth); } else { maxHeight = (int)this.characterSize; } currentCursor.X = startCursor.X; currentCursor.Y += maxHeight; tokensInLine.Clear(); } else { lineWidth += tokenCanevas.Width + AObject2DFactory.GetWidthFromTextToken(tokenEnumerator.Current); } if (notReachEnd && tokenEnumerator.Current.FullText != "\n") { tokensInLine.Add(tokenEnumerator.Current); } } }