dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] pnetlib/Xsharp Font.cs, 1.3, 1.4 Graphics.cs, 1.12


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] pnetlib/Xsharp Font.cs, 1.3, 1.4 Graphics.cs, 1.12, 1.13
Date: Sun, 30 Nov 2003 05:00:24 +0000

Update of /cvsroot/dotgnu-pnet/pnetlib/Xsharp
In directory subversions:/tmp/cvs-serv10791/Xsharp

Modified Files:
        Font.cs Graphics.cs 
Log Message:


Shift "MeasureString", "DrawString", and "GetFontExtents" into the
"Xsharp.Font" class so that we can subclass it for different font
rendering systems.


Index: Graphics.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Graphics.cs,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** Graphics.cs 8 Nov 2003 00:21:01 -0000       1.12
--- Graphics.cs 30 Nov 2003 05:00:21 -0000      1.13
***************
*** 57,64 ****
  {
        // Internal state.
!       private Display dpy;
        private Drawable drawable;
!       private Xlib.Drawable drawableHandle;
!       private IntPtr gc;
        private Color foreground;
        private Color background;
--- 57,64 ----
  {
        // Internal state.
!       internal Display dpy;
        private Drawable drawable;
!       internal Xlib.Drawable drawableHandle;
!       internal IntPtr gc;
        private Color foreground;
        private Color background;
***************
*** 2264,2272 ****
        public void DrawString(int x, int y, String str, Font font)
                        {
-                               // Validate the parameters.
-                               if(x < -32768 || x > 32767 || y < -32768 || y > 
32767)
-                               {
-                                       throw new 
XException(S._("X_PointCoordRange"));
-                               }
                                if(font == null)
                                {
--- 2264,2267 ----
***************
*** 2277,2301 ****
                                        return;
                                }
! 
!                               // Get the font set to use for the font.
!                               IntPtr fontSet = font.GetFontSet(dpy);
!                               if(fontSet == IntPtr.Zero)
!                               {
!                                       return;
!                               }
! 
!                               // Draw the string using the specified font set.
!                               try
!                               {
!                                       IntPtr display = dpy.Lock();
!                                       Xlib.XSharpDrawString
!                                                       (display, 
drawableHandle, gc,
!                                                        fontSet, x, y, str, 
(int)(font.Style),
!                                                        IntPtr.Zero, 
foreground.value);
!                               }
!                               finally
!                               {
!                                       dpy.Unlock();
!                               }
                        }
  
--- 2272,2276 ----
                                        return;
                                }
!                               font.DrawString(this, x, y, str, 0, str.Length);
                        }
  
***************
*** 2323,2329 ****
                                        throw new ArgumentNullException("font");
                                }
!                               FontExtents extents = null;
!                               font.GetFontSet(dpy, out extents);
!                               return extents;
                        }
  
--- 2298,2302 ----
                                        throw new ArgumentNullException("font");
                                }
!                               return font.GetFontExtents(this);
                        }
  
***************
*** 2361,2365 ****
                                                          out int ascent, out 
int descent)
                        {
-                               // Validate the parameters.
                                if(font == null)
                                {
--- 2334,2337 ----
***************
*** 2373,2415 ****
                                        return;
                                }
! 
!                               // Get the font set to use to measure the 
string.
!                               IntPtr fontSet = font.GetFontSet(dpy);
!                               if(fontSet == IntPtr.Zero)
!                               {
!                                       width = 0;
!                                       ascent = 0;
!                                       descent = 0;
!                                       return;
!                               }
! 
!                               // Get the text extents and decode them into 
useful values.
!                               XRectangle overall_ink;
!                               XRectangle overall_logical;
!                               try
!                               {
!                                       IntPtr display = dpy.Lock();
!                                       Xlib.XSharpTextExtents
!                                               (display, fontSet, str,
!                                                out overall_ink, out 
overall_logical);
!                               }
!                               finally
!                               {
!                                       dpy.Unlock();
!                               }
!                               width = overall_logical.width;
!                               ascent = -(overall_logical.y);
!                               descent = overall_logical.height + 
overall_logical.y;
! 
!                               // Increase the descent to account for 
underlining.
!                               // We always draw the underline two pixels below
!                               // the font base line.
!                               if((font.Style & FontStyle.Underlined) != 0)
!                               {
!                                       if(descent < 3)
!                                       {
!                                               descent = 3;
!                                       }
!                               }
                        }
  
--- 2345,2350 ----
                                        return;
                                }
!                               font.MeasureString(this, str, 0, str.Length,
!                                                                  out width, 
out ascent, out descent);
                        }
  

Index: Font.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Font.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Font.cs     8 Jul 2003 02:09:06 -0000       1.3
--- Font.cs     30 Nov 2003 05:00:21 -0000      1.4
***************
*** 23,26 ****
--- 23,27 ----
  
  using System;
+ using Xsharp.Types;
  
  /// <summary>
***************
*** 28,32 ****
  /// the operations on an X font.</para>
  /// </summary>
! public sealed class Font
  {
        // Internal class that keeps track of displays and fontsets.
--- 29,33 ----
  /// the operations on an X font.</para>
  /// </summary>
! public class Font
  {
        // Internal class that keeps track of displays and fontsets.
***************
*** 271,274 ****
--- 272,487 ----
                                        return new Font(name, true);
                                }
+                       }
+ 
+       /// <summary>
+       /// <para>Measure the width, ascent, and descent of a string,
+       /// to calculate its extents when drawn on a graphics context
+       /// using this font.</para>
+       /// </summary>
+       ///
+       /// <param name="graphics">
+       /// <para>The graphics context to measure with.</para>
+       /// </param>
+       ///
+       /// <param name="str">
+       /// <para>The string to be measured.</para>
+       /// </param>
+       ///
+       /// <param name="index">
+       /// <para>The starting index in <paramref name="str"/> of the first
+       /// character to be measured.</para>
+       /// </param>
+       ///
+       /// <param name="count">
+       /// <para>The number of characters <paramref name="str"/>
+       /// to be measured.</para>
+       /// </param>
+       ///
+       /// <param name="width">
+       /// <para>The width of the string, in pixels.</para>
+       /// </param>
+       ///
+       /// <param name="ascent">
+       /// <para>The ascent of the string, in pixels.</para>
+       /// </param>
+       ///
+       /// <param name="descent">
+       /// <para>The descent of the string, in pixels.</para>
+       /// </param>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>Raised if <paramref name="graphics"/> is <see 
langword="null"/>.
+       /// </para>
+       /// </exception>
+       public virtual void MeasureString
+                               (Graphics graphics, String str, int index, int 
count,
+                                out int width, out int ascent, out int descent)
+                       {
+                               // Validate the parameters.
+                               if(graphics == null)
+                               {
+                                       throw new 
ArgumentNullException("graphics");
+                               }
+                               if(str == null || count == 0)
+                               {
+                                       width = 0;
+                                       ascent = 0;
+                                       descent = 0;
+                                       return;
+                               }
+ 
+                               // Extract the substring to be measured.
+                               // TODO: make this more efficient by avoiding 
the data copy.
+                               str = str.Substring(index, count);
+ 
+                               // Get the font set to use to measure the 
string.
+                               IntPtr fontSet = GetFontSet(graphics.dpy);
+                               if(fontSet == IntPtr.Zero)
+                               {
+                                       width = 0;
+                                       ascent = 0;
+                                       descent = 0;
+                                       return;
+                               }
+ 
+                               // Get the text extents and decode them into 
useful values.
+                               XRectangle overall_ink;
+                               XRectangle overall_logical;
+                               try
+                               {
+                                       IntPtr display = graphics.dpy.Lock();
+                                       Xlib.XSharpTextExtents
+                                               (display, fontSet, str,
+                                                out overall_ink, out 
overall_logical);
+                               }
+                               finally
+                               {
+                                       graphics.dpy.Unlock();
+                               }
+                               width = overall_logical.width;
+                               ascent = -(overall_logical.y);
+                               descent = overall_logical.height + 
overall_logical.y;
+ 
+                               // Increase the descent to account for 
underlining.
+                               // We always draw the underline two pixels below
+                               // the font base line.
+                               if((style & FontStyle.Underlined) != 0)
+                               {
+                                       if(descent < 3)
+                                       {
+                                               descent = 3;
+                                       }
+                               }
+                       }
+ 
+       /// <summary>
+       /// <para>Draw a string at a particular position on a
+       /// specified graphics context.</para>
+       /// </summary>
+       ///
+       /// <param name="graphics">
+       /// <para>The graphics context to draw on.</para>
+       /// </param>
+       ///
+       /// <param name="x">
+       /// <para>The X co-ordinate of the position to start drawing 
text.</para>
+       /// </param>
+       ///
+       /// <param name="y">
+       /// <para>The Y co-ordinate of the position to start drawing 
text.</para>
+       /// </param>
+       ///
+       /// <param name="str">
+       /// <para>The string to be drawn.</para>
+       /// </param>
+       ///
+       /// <param name="index">
+       /// <para>The starting index in <paramref name="str"/> of the first
+       /// character to be measured.</para>
+       /// </param>
+       ///
+       /// <param name="count">
+       /// <para>The number of characters <paramref name="str"/>
+       /// to be measured.</para>
+       /// </param>
+       ///
+       /// <exception cref="T:Xsharp.XException">
+       /// <para>One of the co-ordinate values is out of range.</para>
+       /// </exception>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>Raised if <paramref name="graphics"/> is <see 
langword="null"/>.
+       /// </para>
+       /// </exception>
+       public virtual void DrawString
+                               (Graphics graphics, int x, int y,
+                                String str, int index, int count)
+                       {
+                               // Validate the parameters.
+                               if(x < -32768 || x > 32767 || y < -32768 || y > 
32767)
+                               {
+                                       throw new 
XException(S._("X_PointCoordRange"));
+                               }
+                               if(graphics == null)
+                               {
+                                       throw new 
ArgumentNullException("graphics");
+                               }
+                               if(str == null || count == 0)
+                               {
+                                       return;
+                               }
+ 
+                               // Extract the substring to be measured.
+                               // TODO: make this more efficient by avoiding 
the data copy.
+                               str = str.Substring(index, count);
+ 
+                               // Get the font set to use for the font.
+                               IntPtr fontSet = GetFontSet(graphics.dpy);
+                               if(fontSet == IntPtr.Zero)
+                               {
+                                       return;
+                               }
+ 
+                               // Draw the string using the specified font set.
+                               try
+                               {
+                                       IntPtr display = graphics.dpy.Lock();
+                                       Xlib.XSharpDrawString
+                                                       (display, 
graphics.drawableHandle, graphics.gc,
+                                                        fontSet, x, y, str, 
(int)style,
+                                                        IntPtr.Zero, 
graphics.Foreground.value);
+                               }
+                               finally
+                               {
+                                       graphics.dpy.Unlock();
+                               }
+                       }
+ 
+       /// <summary>
+       /// <para>Get extent information for this font, when drawing
+       /// onto a particular graphics context.</para>
+       /// </summary>
+       ///
+       /// <param name="graphics">
+       /// <para>The graphics context to get the extent information for.</para>
+       /// </param>
+       ///
+       /// <returns>
+       /// <para>Returns the extent information.</para>
+       /// </returns>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>Raised if <paramref name="graphics"/> is <see 
langword="null"/>.
+       /// </para>
+       /// </exception>
+       public virtual FontExtents GetFontExtents(Graphics graphics)
+                       {
+                               if(graphics == null)
+                               {
+                                       throw new 
ArgumentNullException("graphics");
+                               }
+                               FontExtents extents = null;
+                               GetFontSet(graphics.dpy, out extents);
+                               return extents;
                        }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]