コード例 #1
0
 /// <summary>
 /// Adds a text string to this path.
 /// </summary>
 public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XRect layoutRect, XStringFormat format)
 {
   this.gdipPath.AddString(s, family.family, (int)style, (float)emSize, layoutRect.ToRectangleF(), format.RealizeGdiStringFormat());
 }
コード例 #2
0
ファイル: XGraphics.cs プロジェクト: Davincier/openpetra
    /// <summary>
    /// Draws the specified text string.
    /// </summary>
    public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
    {
      if (text == null)
        throw new ArgumentNullException("text");
      if (font == null)
        throw new ArgumentNullException("font");
      if (brush == null)
        throw new ArgumentNullException("brush");

      if (format != null && format.LineAlignment == XLineAlignment.BaseLine && layoutRectangle.Height != 0)
        throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0.");

      if (text.Length == 0)
        return;

      if (format == null)
        format = XStringFormats.Default;

      if (this.drawGraphics)
      {
#if GDI
        if (this.targetContext == XGraphicTargetContext.GDI)
        {
          RectangleF rect = layoutRectangle.ToRectangleF();
          if (format.LineAlignment == XLineAlignment.BaseLine)
          {
            double lineSpace = font.GetHeight(this);
            int cellSpace = font.FontFamily.GetLineSpacing(font.Style);
            int cellAscent = font.FontFamily.GetCellAscent(font.Style);
            int cellDescent = font.FontFamily.GetCellDescent(font.Style);
            double cyAscent = lineSpace * cellAscent / cellSpace;
            cyAscent = lineSpace * font.cellAscent / font.cellSpace;
            rect.Offset(0, (float)-cyAscent);
          }
          this.gfx.DrawString(text, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect,
            format != null ? format.RealizeGdiStringFormat() : null);
        }
#endif
#if WPF
        if (this.targetContext == XGraphicTargetContext.WPF)
        {
#if !SILVERLIGHT
          double x = layoutRectangle.X;
          double y = layoutRectangle.Y;

          double lineSpace = font.GetHeight(this);
          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());
          FormattedText formattedText = FontHelper.CreateFormattedText(text, font.typeface, font.Size, brush.RealizeWpfBrush());

          //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;
              formattedText.TextAlignment = TextAlignment.Center;
              break;

            case XStringAlignment.Far:
              x += layoutRectangle.Width;
              formattedText.TextAlignment = TextAlignment.Right;
              break;
          }
          if (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.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);
          }

          //this.dc.DrawText(formattedText, layoutRectangle.Location.ToPoint());
          this.dc.DrawText(formattedText, new System.Windows.Point(x, y));
#else
          dc.DrawString(this, text, font, brush, layoutRectangle, format);
#endif
        }
#endif
      }

      if (this.renderer != null)
        this.renderer.DrawString(text, font, brush, layoutRectangle, format);
    }
コード例 #3
0
 /// <summary>
 /// Adds an elliptical arc to the current figure.
 /// </summary>
 public void AddArc(XRect rect, double startAngle, double sweepAngle)
 {
   this.items.Add(new XGraphicsPathItem(XGraphicsPathItemType.Arc, new XPoint(rect.X, rect.Y), new XPoint(rect.Width, rect.Height),
     new XPoint(startAngle, sweepAngle)));
   this.dirty = true;
   this.gdipPath.AddArc(rect.ToRectangleF(), (float)startAngle, (float)sweepAngle);
 }
コード例 #4
0
 /// <summary>
 /// Adds an ellipse to the current path.
 /// </summary>
 public void AddEllipse(XRect rect)
 {
   this.items.Add(new XGraphicsPathItem(XGraphicsPathItemType.Ellipse, new XPoint(rect.X, rect.Y), new XPoint(rect.Width, rect.Height)));
   this.dirty = true;
   this.gdipPath.AddEllipse(rect.ToRectangleF());
 }
コード例 #5
0
ファイル: XGraphicsPath.cs プロジェクト: Core2D/PDFsharp
        /// <summary>
        /// Adds a text string to this path.
        /// </summary>
        public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XRect layoutRect,
            XStringFormat format)
        {
            if (s == null)
                throw new ArgumentNullException("s");

            if (family == null)
                throw new ArgumentNullException("family");

            if (format == null)
                format = XStringFormats.Default;

            if (format.LineAlignment == XLineAlignment.BaseLine && layoutRect.Height != 0)
                throw new InvalidOperationException(
                    "DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0.");

            if (s.Length == 0)
                return;

            XFont font = new XFont(family.Name, emSize, style);
#if CORE
            DiagnosticsHelper.HandleNotImplemented("XGraphicsPath.AddString");
#endif
#if (GDI || CORE_) && !WPF
            //Gfx.DrawString(text, font.Realize_GdiFont(), brush.RealizeGdiBrush(), rect,
            //  format != null ? format.RealizeGdiStringFormat() : null);

            if (family.GdiFamily == null)
                throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name));

            RectangleF rect = layoutRect.ToRectangleF();
            rect.Offset(new PointF(0, SimulateBaselineOffset(family, style, emSize, format)));

            try
            {
                Lock.EnterGdiPlus();
                _gdipPath.AddString(s, family.GdiFamily, (int)style, (float)emSize, rect, format.RealizeGdiStringFormat());
            }
            finally { Lock.ExitGdiPlus(); }
#endif
#if WPF && !GDI
            if (family.WpfFamily == null)
                throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name));
#if !SILVERLIGHT
            // Just a first sketch, but currently we do not need it and there is enough to do...
            double x = layoutRect.X;
            double y = layoutRect.Y;

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

            //double cyAscent = family.GetCellAscent(style) * family.GetLineSpacing(style) / family.getl; //fontlineSpace * font.cellAscent / font.cellSpace;
            //double cyDescent =family.GetCellDescent(style); // lineSpace * font.cellDescent / font.cellSpace;
            double lineSpace = font.GetHeight();
            double cyAscent = lineSpace * font.CellAscent / font.CellSpace;
            double cyDescent = lineSpace * font.CellDescent / font.CellSpace;

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

            Typeface typeface = FontHelper.CreateTypeface(family.WpfFamily, style);
            //FormattedText formattedText = new FormattedText(s, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize, WpfBrushes.Black);
            FormattedText formattedText = FontHelper.CreateFormattedText(s, typeface, emSize, WpfBrushes.Black);

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

                case XStringAlignment.Center:
                    x += layoutRect.Width / 2;
                    formattedText.TextAlignment = TextAlignment.Center;
                    break;

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

                case XLineAlignment.Center:
                    // TO/DO use CapHeight. PDFlib also uses 3/4 of ascent
                    //y += -formattedText.Baseline + (cyAscent * 2 / 4) + layoutRect.Height / 2;

                    // GDI seems to make it this simple:
                    // TODO: Check WPF's vertical alignment and make all implementations fit. $MaOs
                    y += layoutRect.Height / 2 - lineSpace / 2;
                    break;

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

                case XLineAlignment.BaseLine:
                    y -= formattedText.Baseline;
                    break;
            }
            //}
            //else
            //{
            //  // TODOWPF
            //  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.IsItalicFace)
            //{
            //  // TODO: emulate italic by shearing transformation
            //}

            if (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)
            {
                //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);
            }

            //dc.DrawText(formattedText, layoutRectangle.Location.ToPoint());
            //dc.DrawText(formattedText, new SysPoint(x, y));

            Geometry geo = formattedText.BuildGeometry(new Point(x, y));
            _pathGeometry.AddGeometry(geo);
#else
            // AG-HACK
            throw new InvalidOperationException("Silverlight cannot create geometry of glyphs.");
            // TODO: no, yagni
#endif
#endif
        }
コード例 #6
0
ファイル: XGraphics.cs プロジェクト: BackupTheBerlios/zp7-svn
    /// <summary>
    /// Draws the specified text string.
    /// </summary>
    public void DrawString(string s, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
    {
      if (s == null)
        throw new ArgumentNullException("s");
      if (font == null)
        throw new ArgumentNullException("font");
      if (brush == null)
        throw new ArgumentNullException("brush");

      if (format.LineAlignment == XLineAlignment.BaseLine && layoutRectangle.Height != 0)
        throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0.");

      if (s.Length == 0)
        return;

      if (format == null)
        format = XStringFormat.Default;

      if (this.drawGraphics)
      {
        RectangleF rect = layoutRectangle.ToRectangleF();
        if (format.LineAlignment == XLineAlignment.BaseLine)
        {
          // TODO optimze
          double lineSpace = font.GetHeight(this);
          int cellSpace = font.FontFamily.GetLineSpacing(font.Style);
          int cellAscent = font.FontFamily.GetCellAscent(font.Style);
          int cellDescent = font.FontFamily.GetCellDescent(font.Style);
          double cyAscent = lineSpace * cellAscent / cellSpace;
          cyAscent = lineSpace * font.cellAscent / font.cellSpace;
          rect.Offset(0, (float)-cyAscent);
        }
        this.gfx.DrawString(s, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect,
          format != null ? format.RealizeGdiStringFormat() : null);
      }

      if (this.renderer != null)
        this.renderer.DrawString(s, font, brush, layoutRectangle, format);
    }
コード例 #7
0
    /// <summary>
    /// Adds a rectangle to this path.
    /// </summary>
    public void AddRectangle(XRect rect)
    {
#if GDI
      this.gdipPath.AddRectangle(rect.ToRectangleF());
#endif
#if WPF
      StartFigure();
      PathFigure figure = CurrentPathFigure;
      figure.StartPoint = new System.Windows.Point(rect.x, rect.y);
      figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y), true));
      figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y + rect.height), true));
      figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x, rect.y + rect.height), true));
      CloseFigure();
#endif
    }
コード例 #8
0
    /// <summary>
    /// Adds a text string to this path.
    /// </summary>
    public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XRect layoutRect, XStringFormat format)
    {
      if (s == null)
        throw new ArgumentNullException("s");
      if (family == null)
        throw new ArgumentNullException("family");

      if (format.LineAlignment == XLineAlignment.BaseLine && layoutRect.Height != 0)
        throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0.");

      if (s.Length == 0)
        return;

      if (format == null)
        format = XStringFormats.Default;

      XFont font = new XFont(family.Name, emSize, style);
#if GDI && !WPF
          RectangleF rc = layoutRect.ToRectangleF();
          if (format.LineAlignment == XLineAlignment.BaseLine)
          {
            double lineSpace = font.GetHeight();
            int cellSpace = font.FontFamily.GetLineSpacing(font.Style);
            int cellAscent = font.FontFamily.GetCellAscent(font.Style);
            int cellDescent = font.FontFamily.GetCellDescent(font.Style);
            double cyAscent = lineSpace * cellAscent / cellSpace;
            cyAscent = lineSpace * font.cellAscent / font.cellSpace;
            rc.Offset(0, (float)-cyAscent);
          }
          //this.gfx.DrawString(text, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect,
          //  format != null ? format.RealizeGdiStringFormat() : null);
      this.gdipPath.AddString(s, family.gdiFamily, (int)style, (float)emSize, rc, format.RealizeGdiStringFormat());
#endif
#if WPF && !GDI
      // Just a first sketch, but currently we do not need it and there is enough to do...
      double x = layoutRect.X;
      double y = layoutRect.Y;

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

      //double cyAscent = family.GetCellAscent(style) * family.GetLineSpacing(style) / family.getl; //fontlineSpace * font.cellAscent / font.cellSpace;
      //double cyDescent =family.GetCellDescent(style); // lineSpace * font.cellDescent / font.cellSpace;
      double lineSpace = font.GetHeight();
      double cyAscent = lineSpace * font.cellAscent / font.cellSpace;
      double cyDescent = lineSpace * font.cellDescent / font.cellSpace;

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

      Typeface typeface = FontHelper.CreateTypeface(family, style);
      FormattedText formattedText = new FormattedText(s, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize,
        System.Windows.Media.Brushes.Black);

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

        case XStringAlignment.Center:
          x += layoutRect.Width / 2;
          formattedText.TextAlignment = TextAlignment.Center;
          break;

        case XStringAlignment.Far:
          x += layoutRect.Width;
          formattedText.TextAlignment = TextAlignment.Right;
          break;
      }
      //if (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 * 2 / 4) + layoutRect.Height / 2;
          break;

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

        case XLineAlignment.BaseLine:
          y -= formattedText.Baseline;
          break;
      }
      //}
      //else
      //{
      //  // TODOWPF
      //  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)
      {
        //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)
      {
        //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);
      }

      //this.dc.DrawText(formattedText, layoutRectangle.Location.ToPoint());
      //this.dc.DrawText(formattedText, new System.Windows.Point(x, y));

      Geometry geo = formattedText.BuildGeometry(new Point(x, y));
      this.pathGeometry.AddGeometry(geo);
#endif
    }
コード例 #9
0
    /// <summary>
    /// Adds a rectangle to this path.
    /// </summary>
    public void AddRectangle(XRect rect)
    {
#if GDI
      this.gdipPath.AddRectangle(rect.ToRectangleF());
#endif
#if WPF
      StartFigure();
      PathFigure figure = CurrentPathFigure;
      figure.StartPoint = new System.Windows.Point(rect.x, rect.y);

      // figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y), true));
      // figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y + rect.height), true));
      // figure.Segments.Add(new LineSegment(new System.Windows.Point(rect.x, rect.y + rect.height), true));
#if !SILVERLIGHT
      LineSegment lineSegment1 = new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y), true);
      LineSegment lineSegment2 = new LineSegment(new System.Windows.Point(rect.x + rect.width, rect.y + rect.height), true);
      LineSegment lineSegment3 = new LineSegment(new System.Windows.Point(rect.x, rect.y + rect.height), true);
#else
      LineSegment lineSegment1 = new LineSegment();
      lineSegment1.Point = new Point(rect.x + rect.width, rect.y);
      LineSegment lineSegment2 = new LineSegment();
      lineSegment2.Point = new Point(rect.x + rect.width, rect.y + rect.height);
      LineSegment lineSegment3 = new LineSegment();
      lineSegment3.Point = new Point(rect.x, rect.y + rect.height);
#endif
      figure.Segments.Add(lineSegment1);
      figure.Segments.Add(lineSegment2);
      figure.Segments.Add(lineSegment3);
      CloseFigure();
#endif
    }