Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRenderingWpf/Texts/WpfVertTextRenderer.cs @ 12762

Last change on this file since 12762 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 32.6 KB
Line 
1using System;
2using System.Xml;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6using System.Collections.Generic;
7
8using System.Windows;
9using System.Windows.Media;
10
11using SharpVectors.Dom.Css;
12using SharpVectors.Dom.Svg;
13
14using SharpVectors.Renderers.Wpf;
15
16namespace SharpVectors.Renderers.Texts
17{
18    public sealed class WpfVertTextRenderer : WpfTextRenderer
19    {
20        #region Private Fields
21
22        #endregion
23
24        #region Constructors and Destructor
25
26        public WpfVertTextRenderer(SvgTextElement textElement, WpfTextRendering textRendering)
27            : base(textElement, textRendering)
28        {
29        }
30
31        #endregion
32
33        #region Public Methods
34
35        #region RenderSingleLineText Method
36
37        public override void RenderSingleLineText(SvgTextContentElement element,
38            ref Point ctp, string text, double rotate, WpfTextPlacement placement)
39        {
40            if (String.IsNullOrEmpty(text))
41                return;
42
43            int vertOrientation    = -1;
44            int horzOrientation    = -1;
45            string orientationText = element.GetPropertyValue("glyph-orientation-vertical");
46            if (!String.IsNullOrEmpty(orientationText))
47            {
48                double orientationValue = 0;
49                if (Double.TryParse(orientationText, out orientationValue))
50                {
51                    vertOrientation = (int)orientationValue;
52                }
53            }
54            orientationText = element.GetPropertyValue("glyph-orientation-horizontal");
55            if (!String.IsNullOrEmpty(orientationText))
56            {
57                double orientationValue = 0;
58                if (Double.TryParse(orientationText, out orientationValue))
59                {
60                    horzOrientation = (int)orientationValue;
61                }
62            }
63
64            Point startPoint      = ctp;
65            IList<WpfTextRun> textRunList = WpfTextRun.BreakWords(text,
66                vertOrientation, horzOrientation);
67
68            for (int tr = 0; tr < textRunList.Count; tr++)
69            {
70                // For unknown reasons, FormattedText will split a text like "-70%" into two parts "-"
71                // and "70%". We provide a shift to account for the split...
72                double baselineShiftX = 0;
73                double baselineShiftY = 0;
74
75                WpfTextRun textRun = textRunList[tr];
76
77                DrawingGroup verticalGroup = new DrawingGroup();
78
79                DrawingContext verticalContext = verticalGroup.Open();
80                DrawingContext currentContext  = _textContext;
81
82                _textContext = verticalContext;
83
84                this.DrawSingleLineText(element, ref ctp, textRun, rotate, placement);
85
86                verticalContext.Close();
87
88                _textContext = currentContext;
89
90                if (verticalGroup.Children.Count == 1)
91                {
92                    DrawingGroup textGroup = verticalGroup.Children[0] as DrawingGroup;
93                    if (textGroup != null)
94                    {
95                        verticalGroup = textGroup;
96                    }
97                }
98
99                string runText = textRun.Text;
100                int charCount = runText.Length;
101
102                double totalHeight = 0;
103                DrawingCollection drawings = verticalGroup.Children;
104                int itemCount = drawings != null ? drawings.Count : 0;
105                for (int i = 0; i < itemCount; i++)
106                {
107                    Drawing textDrawing = drawings[i];
108                    DrawingGroup textGroup = textDrawing as DrawingGroup;
109
110                    if (vertOrientation == -1)
111                    {
112                        if (textGroup != null)
113                        {
114                            for (int j = 0; j < textGroup.Children.Count; j++)
115                            {
116                                GlyphRunDrawing glyphDrawing = textGroup.Children[j] as GlyphRunDrawing;
117                                if (glyphDrawing != null)
118                                {
119                                    if (textRun.IsLatin)
120                                    {
121                                        GlyphRun glyphRun = glyphDrawing.GlyphRun;
122
123                                        IList<UInt16> glyphIndices = glyphRun.GlyphIndices;
124                                        IDictionary<ushort, double> allGlyphWeights = glyphRun.GlyphTypeface.AdvanceWidths;
125                                        double lastAdvanceWeight =
126                                            allGlyphWeights[glyphIndices[glyphIndices.Count - 1]] * glyphRun.FontRenderingEmSize;
127
128                                        totalHeight += glyphRun.ComputeAlignmentBox().Width + lastAdvanceWeight / 2d;
129                                    }
130                                    else
131                                    {
132                                        totalHeight += ChangeGlyphOrientation(glyphDrawing,
133                                            baselineShiftX, baselineShiftY, false);
134                                    }
135                                }
136                            }
137                        }
138                        else
139                        {
140                            GlyphRunDrawing glyphDrawing = textDrawing as GlyphRunDrawing;
141                            if (glyphDrawing != null)
142                            {
143                                if (textRun.IsLatin)
144                                {
145                                    GlyphRun glyphRun = glyphDrawing.GlyphRun;
146
147                                    IList<UInt16> glyphIndices = glyphRun.GlyphIndices;
148                                    IDictionary<ushort, double> allGlyphWeights = glyphRun.GlyphTypeface.AdvanceWidths;
149                                    double lastAdvanceWeight =
150                                        allGlyphWeights[glyphIndices[glyphIndices.Count - 1]] * glyphRun.FontRenderingEmSize;
151
152                                    totalHeight += glyphRun.ComputeAlignmentBox().Width + lastAdvanceWeight / 2d;
153                                }
154                                else
155                                {
156                                    totalHeight += ChangeGlyphOrientation(glyphDrawing,
157                                        baselineShiftX, baselineShiftY, false);
158                                }
159                            }
160                        }
161                    }
162                    else if (vertOrientation == 0)
163                    {
164                        if (textGroup != null)
165                        {
166                            for (int j = 0; j < textGroup.Children.Count; j++)
167                            {
168                                GlyphRunDrawing glyphDrawing = textGroup.Children[j] as GlyphRunDrawing;
169                                if (glyphDrawing != null)
170                                {
171                                    baselineShiftX = ChangeGlyphOrientation(glyphDrawing,
172                                        baselineShiftX, baselineShiftY, textRun.IsLatin);
173                                    totalHeight += baselineShiftX;
174                                }
175                            }
176                        }
177                        else
178                        {
179                            GlyphRunDrawing glyphDrawing = textDrawing as GlyphRunDrawing;
180                            if (textDrawing != null)
181                            {
182                                totalHeight += ChangeGlyphOrientation(glyphDrawing,
183                                    baselineShiftX, baselineShiftY, textRun.IsLatin);
184                            }
185                        }
186                    }
187                    else
188                    {
189                        throw new NotImplementedException();
190                    }
191                }
192
193                if (!this.IsMeasuring)
194                {
195                    _textContext.DrawDrawing(verticalGroup);
196                }
197
198                if (tr < textRunList.Count)
199                {
200                    ctp.X = startPoint.X;
201                    ctp.Y = startPoint.Y + totalHeight;
202                    startPoint.Y += totalHeight;
203                }
204            }
205        }
206
207        #endregion
208
209        #region RenderTextRun Method
210
211        public override void RenderTextRun(SvgTextContentElement element,
212            ref Point ctp, string text, double rotate, WpfTextPlacement placement)
213        {
214            if (String.IsNullOrEmpty(text))
215                return;
216
217            int vertOrientation = -1;
218            int horzOrientation = -1;
219
220            string orientationText = element.GetPropertyValue("glyph-orientation-vertical");
221            if (!String.IsNullOrEmpty(orientationText))
222            {
223                double orientationValue = 0;
224                if (Double.TryParse(orientationText, out orientationValue))
225                {
226                    vertOrientation = (int)orientationValue;
227                }
228            }
229            orientationText = element.GetPropertyValue("glyph-orientation-horizontal");
230            if (!String.IsNullOrEmpty(orientationText))
231            {
232                double orientationValue = 0;
233                if (Double.TryParse(orientationText, out orientationValue))
234                {
235                    horzOrientation = (int)orientationValue;
236                }
237            }
238
239            Point startPoint      = ctp;
240
241            IList<WpfTextRun> textRunList = WpfTextRun.BreakWords(text,
242                vertOrientation, horzOrientation);
243
244            for (int tr = 0; tr < textRunList.Count; tr++)
245            {
246                // For unknown reasons, FormattedText will split a text like "-70%" into two parts "-"
247                // and "70%". We provide a shift to account for the split...
248                double baselineShiftX = 0;
249                double baselineShiftY = 0;
250                WpfTextRun textRun = textRunList[tr];
251
252                if (!textRun.IsLatin)
253                {
254                    textRun = textRunList[tr];
255                }
256
257                DrawingGroup verticalGroup = new DrawingGroup();
258
259                DrawingContext verticalContext = verticalGroup.Open();
260                DrawingContext currentContext  = _textContext;
261
262                _textContext = verticalContext;
263
264                this.DrawTextRun(element, ref ctp, textRun, rotate, placement);
265
266                verticalContext.Close();
267
268                _textContext = currentContext;
269
270                if (verticalGroup.Children.Count == 1)
271                {
272                    DrawingGroup textGroup = verticalGroup.Children[0] as DrawingGroup;
273                    if (textGroup != null)
274                    {
275                        verticalGroup = textGroup;
276                    }
277                }
278
279                double totalHeight = 0;
280                DrawingCollection drawings = verticalGroup.Children;
281                int itemCount = drawings != null ? drawings.Count : 0;
282                for (int i = 0; i < itemCount; i++)
283                {
284                    Drawing textDrawing = drawings[i];
285                    DrawingGroup textGroup = textDrawing as DrawingGroup;
286
287                    if (vertOrientation == -1)
288                    {
289                        if (textGroup != null)
290                        {
291                            for (int j = 0; j < textGroup.Children.Count; j++)
292                            {
293                                GlyphRunDrawing glyphDrawing = textGroup.Children[j] as GlyphRunDrawing;
294                                if (glyphDrawing != null)
295                                {
296                                    if (textRun.IsLatin)
297                                    {
298                                        GlyphRun glyphRun = glyphDrawing.GlyphRun;
299
300                                        IList<UInt16> glyphIndices = glyphRun.GlyphIndices;
301                                        IDictionary<ushort, double> allGlyphWeights = glyphRun.GlyphTypeface.AdvanceWidths;
302                                        double lastAdvanceWeight =
303                                            allGlyphWeights[glyphIndices[glyphIndices.Count - 1]] * glyphRun.FontRenderingEmSize;
304
305                                        totalHeight += glyphRun.ComputeAlignmentBox().Width + lastAdvanceWeight / 2d;
306                                    }
307                                    else
308                                    {
309                                        totalHeight += ChangeGlyphOrientation(glyphDrawing,
310                                            baselineShiftX, baselineShiftY, false);
311                                    }
312                                }
313                            }
314                        }
315                        else
316                        {
317                            GlyphRunDrawing glyphDrawing = textDrawing as GlyphRunDrawing;
318                            if (glyphDrawing != null)
319                            {
320                                if (textRun.IsLatin)
321                                {
322                                    GlyphRun glyphRun = glyphDrawing.GlyphRun;
323
324                                    IList<UInt16> glyphIndices = glyphRun.GlyphIndices;
325                                    IDictionary<ushort, double> allGlyphWeights = glyphRun.GlyphTypeface.AdvanceWidths;
326                                    double lastAdvanceWeight =
327                                        allGlyphWeights[glyphIndices[glyphIndices.Count - 1]] * glyphRun.FontRenderingEmSize;
328
329                                    totalHeight += glyphRun.ComputeAlignmentBox().Width + lastAdvanceWeight / 2d;
330                                }
331                                else
332                                {
333                                    totalHeight += ChangeGlyphOrientation(glyphDrawing,
334                                        baselineShiftX, baselineShiftY, false);
335                                }
336                            }
337                        }
338                    }
339                    else if (vertOrientation == 0)
340                    {
341                        if (textGroup != null)
342                        {
343                            for (int j = 0; j < textGroup.Children.Count; j++)
344                            {
345                                GlyphRunDrawing glyphDrawing = textGroup.Children[j] as GlyphRunDrawing;
346                                if (glyphDrawing != null)
347                                {
348                                    baselineShiftX = ChangeGlyphOrientation(glyphDrawing,
349                                        baselineShiftX, baselineShiftY, textRun.IsLatin);
350                                    totalHeight += baselineShiftX;
351                                }
352                            }
353                        }
354                        else
355                        {
356                            GlyphRunDrawing glyphDrawing = textDrawing as GlyphRunDrawing;
357                            if (glyphDrawing != null)
358                            {
359                                totalHeight += ChangeGlyphOrientation(glyphDrawing,
360                                    baselineShiftX, baselineShiftY, textRun.IsLatin);
361                            }
362                        }
363                    }
364                    else
365                    {
366                        throw new NotImplementedException();
367                    }
368                }
369
370                if (!this.IsMeasuring)
371                {
372                    _textContext.DrawDrawing(verticalGroup);
373                }
374
375                if (tr < textRunList.Count)
376                {
377                    ctp.X = startPoint.X;
378                    ctp.Y = startPoint.Y + totalHeight;
379                    startPoint.Y += totalHeight;
380                }
381            }
382        }
383
384        #endregion
385
386        #endregion
387
388        #region Private Methods
389
390        #region DrawSingleLineText Method
391
392        private void DrawSingleLineText(SvgTextContentElement element, ref Point ctp,
393            WpfTextRun textRun, double rotate, WpfTextPlacement placement)
394        {
395            if (textRun == null || textRun.IsEmpty)
396                return;
397
398            string text           = textRun.Text;
399            double emSize         = GetComputedFontSize(element);
400            FontFamily fontFamily = GetTextFontFamily(element, emSize);
401
402            FontStyle fontStyle   = GetTextFontStyle(element);
403            FontWeight fontWeight = GetTextFontWeight(element);
404
405            FontStretch fontStretch = GetTextFontStretch(element);
406
407            WpfTextStringFormat stringFormat = GetTextStringFormat(element);
408
409            // Fix the use of Postscript fonts...
410            WpfFontFamilyVisitor fontFamilyVisitor = _drawContext.FontFamilyVisitor;
411            if (!String.IsNullOrEmpty(_actualFontName) && fontFamilyVisitor != null)
412            {
413                WpfFontFamilyInfo currentFamily = new WpfFontFamilyInfo(fontFamily, fontWeight,
414                    fontStyle, fontStretch);
415                WpfFontFamilyInfo familyInfo = fontFamilyVisitor.Visit(_actualFontName,
416                    currentFamily,_drawContext);
417                if (familyInfo != null && !familyInfo.IsEmpty)
418                {
419                    fontFamily  = familyInfo.Family;
420                    fontWeight  = familyInfo.Weight;
421                    fontStyle   = familyInfo.Style;
422                    fontStretch = familyInfo.Stretch;
423                }
424            }
425
426            WpfSvgPaint fillPaint = new WpfSvgPaint(_drawContext, element, "fill");
427            Brush textBrush = fillPaint.GetBrush();
428
429            WpfSvgPaint strokePaint = new WpfSvgPaint(_drawContext, element, "stroke");
430            Pen textPen = strokePaint.GetPen();
431
432            if (textBrush == null && textPen == null)
433            {
434                return;
435            }
436            else if (textBrush == null)
437            {
438                // If here, then the pen is not null, and so the fill cannot be null.
439                // We set this to transparent for stroke only text path...
440                textBrush = Brushes.Transparent;
441            }
442
443            TextDecorationCollection textDecors = GetTextDecoration(element);
444            TextAlignment alignment = stringFormat.Alignment;
445
446            string letterSpacing = element.GetAttribute("letter-spacing");
447            if (String.IsNullOrEmpty(letterSpacing))
448            {   
449                FormattedText formattedText = new FormattedText(text,
450                    textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
451                    stringFormat.Direction, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
452                    emSize, textBrush);
453
454                if (this.IsMeasuring)
455                {
456                    this.AddTextWidth(formattedText.WidthIncludingTrailingWhitespace);
457                    return;
458                }
459
460                formattedText.TextAlignment = stringFormat.Alignment;
461                formattedText.Trimming      = stringFormat.Trimming;
462
463                if (textDecors != null && textDecors.Count != 0)
464                {
465                    formattedText.SetTextDecorations(textDecors);
466                }
467
468                //float xCorrection = 0;
469                //if (alignment == TextAlignment.Left)
470                //    xCorrection = emSize * 1f / 6f;
471                //else if (alignment == TextAlignment.Right)
472                //    xCorrection = -emSize * 1f / 6f;
473
474                double yCorrection = formattedText.Baseline;
475                //float yCorrection = 0;
476
477                if (textRun.IsLatin && textRun.VerticalOrientation == -1)
478                {
479                    yCorrection = yCorrection + formattedText.OverhangAfter * 1.5;
480                }
481
482                Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);
483
484                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
485                _textContext.PushTransform(rotateAt);
486
487                _textContext.DrawText(formattedText, textPoint);
488
489                //float bboxWidth = (float)formattedText.Width;
490                double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
491                if (alignment == TextAlignment.Center)
492                    bboxWidth /= 2f;
493                else if (alignment == TextAlignment.Right)
494                    bboxWidth = 0;
495
496                //ctp.X += bboxWidth + emSize / 4;
497                ctp.X += bboxWidth;
498
499                if (rotateAt != null)
500                {
501                    _textContext.Pop();
502                }
503            }
504            else
505            {
506                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
507                _textContext.PushTransform(rotateAt);
508
509                double spacing = Convert.ToDouble(letterSpacing);
510                for (int i = 0; i < text.Length; i++)
511                {
512                    FormattedText formattedText = new FormattedText(new string(text[i], 1),
513                        textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
514                        stringFormat.Direction,
515                        new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
516                        emSize, textBrush);
517
518                    if (this.IsMeasuring)
519                    {
520                        this.AddTextWidth(formattedText.WidthIncludingTrailingWhitespace);
521                        continue;
522                    }
523
524                    formattedText.TextAlignment = stringFormat.Alignment;
525                    formattedText.Trimming = stringFormat.Trimming;
526
527                    if (textDecors != null && textDecors.Count != 0)
528                    {
529                        formattedText.SetTextDecorations(textDecors);
530                    }
531
532                    //float xCorrection = 0;
533                    //if (alignment == TextAlignment.Left)
534                    //    xCorrection = emSize * 1f / 6f;
535                    //else if (alignment == TextAlignment.Right)
536                    //    xCorrection = -emSize * 1f / 6f;
537
538                    double yCorrection = formattedText.Baseline;
539
540                    Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);
541
542                    _textContext.DrawText(formattedText, textPoint);
543
544                    //float bboxWidth = (float)formattedText.Width;
545                    double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
546                    if (alignment == TextAlignment.Center)
547                        bboxWidth /= 2f;
548                    else if (alignment == TextAlignment.Right)
549                        bboxWidth = 0;
550
551                    //ctp.X += bboxWidth + emSize / 4 + spacing;
552                    ctp.X += bboxWidth + spacing;
553                }
554
555                if (rotateAt != null)
556                {
557                    _textContext.Pop();
558                }
559            }
560        }
561
562        #endregion
563
564        #region DrawTextRun Method
565
566        private void DrawTextRun(SvgTextContentElement element, ref Point ctp,
567            WpfTextRun textRun, double rotate, WpfTextPlacement placement)
568        {
569            if (textRun == null || textRun.IsEmpty)
570                return;
571
572            string text           = textRun.Text;
573            double emSize         = GetComputedFontSize(element);
574            FontFamily fontFamily = GetTextFontFamily(element, emSize);
575
576            FontStyle fontStyle   = GetTextFontStyle(element);
577            FontWeight fontWeight = GetTextFontWeight(element);
578
579            FontStretch fontStretch = GetTextFontStretch(element);
580
581            WpfTextStringFormat stringFormat = GetTextStringFormat(element);
582
583            // Fix the use of Postscript fonts...
584            WpfFontFamilyVisitor fontFamilyVisitor = _drawContext.FontFamilyVisitor;
585            if (!String.IsNullOrEmpty(_actualFontName) && fontFamilyVisitor != null)
586            {
587                WpfFontFamilyInfo currentFamily = new WpfFontFamilyInfo(fontFamily, fontWeight,
588                    fontStyle, fontStretch);
589                WpfFontFamilyInfo familyInfo = fontFamilyVisitor.Visit(_actualFontName,
590                    currentFamily, _drawContext);
591                if (familyInfo != null && !familyInfo.IsEmpty)
592                {
593                    fontFamily  = familyInfo.Family;
594                    fontWeight  = familyInfo.Weight;
595                    fontStyle   = familyInfo.Style;
596                    fontStretch = familyInfo.Stretch;
597                }
598            }
599
600            WpfSvgPaint fillPaint = new WpfSvgPaint(_drawContext, element, "fill");
601            Brush textBrush       = fillPaint.GetBrush();
602
603            WpfSvgPaint strokePaint = new WpfSvgPaint(_drawContext, element, "stroke");
604            Pen textPen = strokePaint.GetPen();
605
606            if (textBrush == null && textPen == null)
607            {
608                return;
609            }
610            else if (textBrush == null)
611            {
612                // If here, then the pen is not null, and so the fill cannot be null.
613                // We set this to transparent for stroke only text path...
614                textBrush = Brushes.Transparent;
615            }
616
617            TextDecorationCollection textDecors = GetTextDecoration(element);
618            TextAlignment alignment = stringFormat.Alignment;
619
620            string letterSpacing = element.GetAttribute("letter-spacing");
621            if (String.IsNullOrEmpty(letterSpacing))
622            {   
623                FormattedText formattedText = new FormattedText(text,
624                    textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
625                    stringFormat.Direction, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
626                    emSize, textBrush);
627
628                formattedText.TextAlignment = stringFormat.Alignment;
629                formattedText.Trimming      = stringFormat.Trimming;
630
631                if (textDecors != null && textDecors.Count != 0)
632                {
633                    formattedText.SetTextDecorations(textDecors);
634                }
635
636                //float xCorrection = 0;
637                //if (alignment == TextAlignment.Left)
638                //    xCorrection = emSize * 1f / 6f;
639                //else if (alignment == TextAlignment.Right)
640                //    xCorrection = -emSize * 1f / 6f;
641
642                double yCorrection = formattedText.Baseline;
643
644                Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);
645
646                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
647                _textContext.PushTransform(rotateAt);
648
649                _textContext.DrawText(formattedText, textPoint);
650
651                //float bboxWidth = (float)formattedText.Width;
652                double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
653                if (alignment == TextAlignment.Center)
654                    bboxWidth /= 2f;
655                else if (alignment == TextAlignment.Right)
656                    bboxWidth = 0;
657
658                //ctp.X += bboxWidth + emSize / 4;
659                ctp.X += bboxWidth;
660
661                if (rotateAt != null)
662                {
663                    _textContext.Pop();
664                }
665            }
666            else
667            {
668                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
669                _textContext.PushTransform(rotateAt);
670
671                float spacing = Convert.ToSingle(letterSpacing);
672                for (int i = 0; i < text.Length; i++)
673                {
674                    FormattedText formattedText = new FormattedText(new string(text[i], 1),
675                        textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
676                        stringFormat.Direction,
677                        new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
678                        emSize, textBrush);
679
680                    formattedText.Trimming      = stringFormat.Trimming;
681                    formattedText.TextAlignment = stringFormat.Alignment;
682
683                    if (textDecors != null && textDecors.Count != 0)
684                    {
685                        formattedText.SetTextDecorations(textDecors);
686                    }
687
688                    //float xCorrection = 0;
689                    //if (alignment == TextAlignment.Left)
690                    //    xCorrection = emSize * 1f / 6f;
691                    //else if (alignment == TextAlignment.Right)
692                    //    xCorrection = -emSize * 1f / 6f;
693
694                    double yCorrection = formattedText.Baseline;
695
696                    Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);
697
698                    _textContext.DrawText(formattedText, textPoint);
699
700                    //float bboxWidth = (float)formattedText.Width;
701                    double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
702                    if (alignment == TextAlignment.Center)
703                        bboxWidth /= 2f;
704                    else if (alignment == TextAlignment.Right)
705                        bboxWidth = 0;
706
707                    //ctp.X += bboxWidth + emSize / 4 + spacing;
708                    ctp.X += bboxWidth + spacing;
709                }
710
711                if (rotateAt != null)
712                {
713                    _textContext.Pop();
714                }
715            }
716        }
717
718        #endregion
719
720        #region ChangeGlyphOrientation Method
721
722        private double ChangeGlyphOrientation(GlyphRunDrawing glyphDrawing,
723            double baselineShiftX, double baselineShiftY, bool isLatin)
724        {
725            if (glyphDrawing == null)
726            {
727                return 0;
728            }
729            GlyphRun glyphRun = glyphDrawing.GlyphRun;
730
731            GlyphRun verticalRun = new GlyphRun();
732            ISupportInitialize glyphInit = verticalRun;
733            glyphInit.BeginInit();
734
735            verticalRun.IsSideways = true;
736
737            List<double> advancedHeights = null;
738
739            double totalHeight = 0;
740
741            IList<UInt16> glyphIndices = glyphRun.GlyphIndices;
742            int advancedCount = glyphIndices.Count;
743            //{
744            //    double textHeight = glyphRun.ComputeInkBoundingBox().Height + glyphRun.ComputeAlignmentBox().Height;
745            //    textHeight = textHeight / 2.0d;
746
747            //    totalHeight = advancedCount * textHeight;
748            //    advancedHeights = new List<double>(advancedCount);
749            //    for (int k = 0; k < advancedCount; k++)
750            //    {
751            //        advancedHeights.Add(textHeight);
752            //    }
753            //}
754            advancedHeights = new List<double>(advancedCount);
755            IDictionary<ushort, double> allGlyphHeights = glyphRun.GlyphTypeface.AdvanceHeights;
756            double fontSize = glyphRun.FontRenderingEmSize;
757            for (int k = 0; k < advancedCount; k++)
758            {
759                double tempValue = allGlyphHeights[glyphIndices[k]] * fontSize;
760                advancedHeights.Add(tempValue);
761                totalHeight += tempValue;
762            }
763
764            Point baselineOrigin = glyphRun.BaselineOrigin;
765            if (isLatin)
766            {
767                baselineOrigin.X += baselineShiftX;
768                baselineOrigin.Y += baselineShiftY;
769            }
770
771            //verticalRun.AdvanceWidths     = glyphRun.AdvanceWidths;
772            verticalRun.AdvanceWidths       = advancedHeights;
773            verticalRun.BaselineOrigin      = baselineOrigin;
774            verticalRun.BidiLevel           = glyphRun.BidiLevel;
775            verticalRun.CaretStops          = glyphRun.CaretStops;
776            verticalRun.Characters          = glyphRun.Characters;
777            verticalRun.ClusterMap          = glyphRun.ClusterMap;
778            verticalRun.DeviceFontName      = glyphRun.DeviceFontName;
779            verticalRun.FontRenderingEmSize = glyphRun.FontRenderingEmSize;
780            verticalRun.GlyphIndices        = glyphRun.GlyphIndices;
781            verticalRun.GlyphOffsets        = glyphRun.GlyphOffsets;
782            verticalRun.GlyphTypeface       = glyphRun.GlyphTypeface;
783            verticalRun.Language            = glyphRun.Language;
784
785            glyphInit.EndInit();
786
787            glyphDrawing.GlyphRun = verticalRun;
788
789            return totalHeight;
790        }
791
792        #endregion
793
794        #endregion
795    }
796}
Note: See TracBrowser for help on using the repository browser.