/// <summary> /// Applies centered alignment to the text on the linebox /// </summary> /// <param name="g"></param> /// <param name="lineBox"></param> private static void ApplyJustifyAlignment(RGraphics g, CssLineBox lineBox) { if (lineBox.Equals(lineBox.OwnerBox.LineBoxes[lineBox.OwnerBox.LineBoxes.Count - 1])) { return; } double indent = lineBox.Equals(lineBox.OwnerBox.LineBoxes[0]) ? lineBox.OwnerBox.ActualTextIndent : 0f; double textSum = 0f; double words = 0f; double availWidth = lineBox.OwnerBox.ClientRectangle.Width - indent; // Gather text sum foreach (CssRect w in lineBox.Words) { textSum += w.Width; words += 1f; } if (words <= 0f) { return; //Avoid Zero division } double spacing = (availWidth - textSum) / words; //Spacing that will be used double curx = lineBox.OwnerBox.ClientLeft + indent; foreach (CssRect word in lineBox.Words) { word.Left = curx; curx = word.Right + spacing; if (word == lineBox.Words[lineBox.Words.Count - 1]) { word.Left = lineBox.OwnerBox.ClientRight - word.Width; } } }