@@ -138,13 +138,17 @@ public void SetAlignment(TextFormatAlignment alignments)
138138 /// <param name="font">The font.</param>
139139 /// <param name="brush">The text brush.</param>
140140 /// <param name="layoutRectangle">The layout rectangle.</param>
141+ /// <param name="lineHeight">The line height.</param>
141142 public void DrawString ( string text , XFont font , XBrush brush , XRect layoutRectangle , XUnit ? lineHeight = null )
142143 {
143- DrawString ( text , font , brush , layoutRectangle , XStringFormats . TopLeft , lineHeight ) ;
144+ DrawString ( text , font , brush , layoutRectangle , new TextFormatAlignment ( )
145+ {
146+ Horizontal = XParagraphAlignment . Justify , Vertical = XVerticalAlignment . Top
147+ } , lineHeight ) ;
144148 }
145149
146150 /// <summary>
147- /// Draws the text .
151+ /// Get the layout rectangle required .
148152 /// </summary>
149153 /// <param name="text">The text to be drawn.</param>
150154 /// <param name="font">The font.</param>
@@ -184,24 +188,20 @@ public XRect GetLayout(string text, XFont font, XBrush brush, XRect layoutRectan
184188 /// <param name="font">The font.</param>
185189 /// <param name="brush">The text brush.</param>
186190 /// <param name="layoutRectangle">The layout rectangle.</param>
187- /// <param name="format ">The format. Must be <c>XStringFormat.TopLeft </c></param>
188- /// <param name="lineHeight">The height of each line</param>
189- public void DrawString ( string text , XFont font , XBrush brush , XRect layoutRectangle , XStringFormat format ,
191+ /// <param name="alignments ">The alignments. </c></param>
192+ /// <param name="lineHeight">The height of each line. </param>
193+ public void DrawString ( string text , XFont font , XBrush brush , XRect layoutRectangle , TextFormatAlignment alignments ,
190194 XUnit ? lineHeight = null )
191195 {
192- if ( format == null )
193- throw new ArgumentNullException ( nameof ( format ) ) ;
196+ if ( alignments == null )
197+ throw new ArgumentNullException ( nameof ( alignments ) ) ;
194198
195199 if ( text . Length == 0 )
196200 return ;
197201
198202 GetLayout ( text , font , brush , layoutRectangle , lineHeight ) ;
199203
200- SetAlignment ( new TextFormatAlignment ( )
201- {
202- Horizontal = GetHorizontalAlignmentFromFormat ( format ) ,
203- Vertical = GetVerticalAlignmentFromFormat ( format )
204- } ) ;
204+ SetAlignment ( alignments ) ;
205205
206206 double dx = layoutRectangle . Location . X ;
207207 double dy = layoutRectangle . Location . Y ;
@@ -210,7 +210,7 @@ public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectan
210210
211211 if ( VerticalAlignment == XVerticalAlignment . Middle )
212212 {
213- dy += ( layoutRectangle . Height - _layoutRectangle . Height + _lineHeight ) / 2 ;
213+ dy += ( layoutRectangle . Height - _layoutRectangle . Height ) / 2 ;
214214 }
215215 else if ( VerticalAlignment == XVerticalAlignment . Bottom )
216216 {
@@ -221,16 +221,26 @@ public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectan
221221 int count = _blocks . Count ;
222222 foreach ( var line in lines )
223223 {
224- if ( Alignment != XParagraphAlignment . Justify )
224+ var lineBlocks = line as Block [ ] ?? line . ToArray ( ) ;
225+ if ( Alignment == XParagraphAlignment . Justify )
226+ {
227+ var locationX = dx ;
228+ var gapSize = ( layoutRectangle . Width - lineBlocks . Select ( l => l . Width ) . Sum ( ) ) / ( lineBlocks . Count ( ) - 1 ) ;
229+ foreach ( var block in lineBlocks )
230+ {
231+ _gfx . DrawString ( block . Text . Trim ( ) , font , brush , locationX , dy + lineBlocks . First ( ) . Location . Y , XStringFormats . TopLeft ) ;
232+ locationX += block . Width + gapSize ;
233+ }
234+ }
235+ else
225236 {
226- var enumerable = line as Block [ ] ?? line . ToArray ( ) ;
227- var lineText = string . Join ( " " , enumerable . Select ( l => l . Text ) ) ;
237+ var lineText = string . Join ( " " , lineBlocks . Select ( l => l . Text ) ) ;
228238 var locationX = dx ;
229- if ( format . Alignment == XStringAlignment . Center )
239+ if ( Alignment == XParagraphAlignment . Center )
230240 locationX = dx + layoutRectangle . Width / 2 ;
231- if ( format . Alignment == XStringAlignment . Far )
241+ if ( Alignment == XParagraphAlignment . Right )
232242 locationX += layoutRectangle . Width ;
233- _gfx . DrawString ( lineText , font , brush , locationX , dy + enumerable . First ( ) . Location . Y , format ) ;
243+ _gfx . DrawString ( lineText , font , brush , locationX , dy + lineBlocks . First ( ) . Location . Y , GetXStringFormat ( ) ) ;
234244 }
235245 }
236246 }
@@ -420,33 +430,19 @@ void HorizontalAlignLine(int firstIndex, int lastIndex, double layoutWidth)
420430 // - super- and sub-script
421431 // - ...
422432
423- private static XParagraphAlignment GetHorizontalAlignmentFromFormat ( XStringFormat format )
424- {
425- switch ( format . Alignment )
426- {
427- case XStringAlignment . Center :
428- return XParagraphAlignment . Center ;
429- case XStringAlignment . Near :
430- return XParagraphAlignment . Left ;
431- case XStringAlignment . Far :
432- return XParagraphAlignment . Right ;
433- }
434-
435- return XParagraphAlignment . Justify ;
436- }
437-
438- private static XVerticalAlignment GetVerticalAlignmentFromFormat ( XStringFormat format )
433+ private XStringFormat GetXStringFormat ( )
439434 {
440- switch ( format . LineAlignment )
435+ switch ( Alignment )
441436 {
442- case XLineAlignment . Center :
443- return XVerticalAlignment . Middle ;
444- case XLineAlignment . BaseLine :
445- case XLineAlignment . Far :
446- return XVerticalAlignment . Bottom ;
447- case XLineAlignment . Near :
437+ case XParagraphAlignment . Center :
438+ return XStringFormats . TopCenter ;
439+ case XParagraphAlignment . Right :
440+ return XStringFormats . TopRight ;
441+ case XParagraphAlignment . Default :
442+ case XParagraphAlignment . Justify :
443+ case XParagraphAlignment . Left :
448444 default :
449- return XVerticalAlignment . Top ;
445+ return XStringFormats . TopLeft ;
450446 }
451447 }
452448 }
0 commit comments