Пример #1
0
        public XSize MeasureString(XGraphics gfx, string text, XFont font, XStringFormat stringFormat)
        {
            TextBlock textBlock = FontHelper.CreateTextBlock(text, null, font.Size, null);

            return(new XSize(textBlock.ActualWidth, textBlock.ActualHeight));
        }
Пример #2
0
        public void DrawString(XGraphics gfx, string text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
        {
            double x = layoutRectangle.X;
            double y = layoutRectangle.Y;

            double lineSpace = font.GetHeight(gfx);
            double cyAscent  = lineSpace * font.cellAscent / font.cellSpace;
            double cyDescent = lineSpace * font.cellDescent / font.cellSpace;

            bool bold      = (font.Style & XFontStyle.Bold) != 0;
            bool italic    = (font.Style & XFontStyle.Italic) != 0;
            bool strikeout = (font.Style & XFontStyle.Strikeout) != 0;
            bool underline = (font.Style & XFontStyle.Underline) != 0;

            //FormattedText formattedText = new FormattedText(text, new CultureInfo("en-us"), // WPFHACK
            //  FlowDirection.LeftToRight, font.typeface, font.Size, brush.RealizeWpfBrush());
            TextBlock textBlock = FontHelper.CreateTextBlock(text, null, font.Size, brush.RealizeWpfBrush());

            Canvas.SetLeft(textBlock, x);
            Canvas.SetTop(textBlock, y);

            //formattedText.SetTextDecorations(TextDecorations.OverLine);
            switch (format.Alignment)
            {
            case XStringAlignment.Near:
                // nothing to do, this is the default
                //formattedText.TextAlignment = TextAlignment.Left;
                break;

            case XStringAlignment.Center:
                x += layoutRectangle.Width / 2;
                textBlock.TextAlignment = TextAlignment.Center;
                break;

            case XStringAlignment.Far:
                x += layoutRectangle.Width;
                textBlock.TextAlignment = TextAlignment.Right;
                break;
            }
            if (gfx.PageDirection == XPageDirection.Downwards)
            {
                switch (format.LineAlignment)
                {
                case XLineAlignment.Near:
                    //y += cyAscent;
                    break;

                    //case XLineAlignment.Center:
                    //  // TODO use CapHeight. PDFlib also uses 3/4 of ascent
                    //  y += -formattedText.Baseline + (cyAscent * 1 / 3) + layoutRectangle.Height / 2;
                    //  //y += -formattedText.Baseline + (font.Size * font.Metrics.CapHeight / font.unitsPerEm / 2) + layoutRectangle.Height / 2;
                    //  break;

                    //case XLineAlignment.Far:
                    //  y += -formattedText.Baseline - cyDescent + layoutRectangle.Height;
                    //  break;

                    //case XLineAlignment.BaseLine:
                    //  y -= formattedText.Baseline;
                    //  break;
                }
            }
            else
            {
                // TODOWPF: make unit test
                switch (format.LineAlignment)
                {
                case XLineAlignment.Near:
                    //y += cyDescent;
                    break;

                case XLineAlignment.Center:
                    // TODO use CapHeight. PDFlib also uses 3/4 of ascent
                    //y += -(cyAscent * 3 / 4) / 2 + rect.Height / 2;
                    break;

                case XLineAlignment.Far:
                    //y += -cyAscent + rect.Height;
                    break;

                case XLineAlignment.BaseLine:
                    // nothing to do
                    break;
                }
            }

            //if (bold && !descriptor.IsBoldFace)
            //{
            //  // TODO: emulate bold by thicker outline
            //}

            //if (italic && !descriptor.IsBoldFace)
            //{
            //  // TODO: emulate italic by shearing transformation
            //}

            //if (underline)
            //{
            //  formattedText.FontStyle.SetTextDecorations(TextDecorations.Underline);
            //  //double underlinePosition = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlinePosition / font.cellSpace;
            //  //double underlineThickness = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlineThickness / font.cellSpace;
            //  //DrawRectangle(null, brush, x, y - underlinePosition, width, underlineThickness);
            //}

            //if (strikeout)
            //{
            //  formattedText.SetTextDecorations(TextDecorations.Strikethrough);
            //  //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace;
            //  //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace;
            //  //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize);
            //}

            //formattedText
            _canvas.Children.Add(textBlock);
        }