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

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

[Dotgnu-pnet-commits] CVS: pnetlib/Xsharp FontExtents.cs,NONE,1.1 Displ


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/Xsharp FontExtents.cs,NONE,1.1 Display.cs,1.4,1.5 Font.cs,1.1,1.2 Graphics.cs,1.3,1.4 Xlib.cs.in,1.6,1.7 XsharpSupport.c,1.1,1.2
Date: Sat, 07 Jun 2003 03:40:18 -0400

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

Modified Files:
        Display.cs Font.cs Graphics.cs Xlib.cs.in XsharpSupport.c 
Added Files:
        FontExtents.cs 
Log Message:


Add font handling code to Xsharp.


--- NEW FILE ---
/*
 * FontExtents.cs - Extent information for a font.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace Xsharp
{

using System;
using Xsharp.Types;

/// <summary>
/// <para>The <see cref="T:Xsharp.FontExtents"/> class encapsulates
/// extent information for a font, including ascent, descent,
/// and maximum character width information.</para>
/// </summary>
public sealed class FontExtents
{
        // Internal state.
        private int ascent;
        private int descent;
        private int maxWidth;

        // Constructor.
        internal FontExtents(Font font, IntPtr fontSet)
                        {
                                XRectangle max_ink;
                                XRectangle max_logical;

                                // Get the extent information for the font.
                                Xlib.XSharpFontExtents(fontSet, out max_ink, 
out max_logical);

                                // Convert the extent information into values 
that make sense.
                                ascent = -(max_logical.y);
                                descent = max_logical.height + max_logical.y;
                                maxWidth = max_ink.width;

                                // 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;
                                        }
                                }
                        }

        /// <summary>
        /// <para>Get the font's ascent, in pixels.</para>
        /// </summary>
        public int Ascent
                        {
                                get
                                {
                                        return ascent;
                                }
                        }

        /// <summary>
        /// <para>Get the font's desscent, in pixels.</para>
        /// </summary>
        public int Descent
                        {
                                get
                                {
                                        return descent;
                                }
                        }

        /// <summary>
        /// <para>Get the font's maximum character width, in pixels.</para>
        /// </summary>
        public int MaxCharWidth
                        {
                                get
                                {
                                        return maxWidth;
                                }
                        }

} // class FontExtents

} // namespace Xsharp

Index: Display.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Display.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** Display.cs  7 Jun 2003 05:40:07 -0000       1.4
--- Display.cs  7 Jun 2003 07:40:16 -0000       1.5
***************
*** 224,230 ****
  
                                                        // Disassociate the 
fonts from this display.
!                                                       foreach(Font font in 
fonts)
                                                        {
!                                                               
font.Disassociate(this);
                                                        }
                                                        fonts.Clear();
--- 224,231 ----
  
                                                        // Disassociate the 
fonts from this display.
!                                                       IDictionaryEnumerator e 
= fonts.GetEnumerator();
!                                                       while(e.MoveNext())
                                                        {
!                                                               
((Font)(e.Value)).Disassociate(this);
                                                        }
                                                        fonts.Clear();

Index: Font.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Font.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** Font.cs     28 May 2003 04:17:53 -0000      1.1
--- Font.cs     7 Jun 2003 07:40:16 -0000       1.2
***************
*** 34,37 ****
--- 34,38 ----
        {
                public FontInfo next;
+               public FontExtents extents;
                public Display dpy;
                public IntPtr fontSet;
***************
*** 275,278 ****
--- 276,284 ----
        internal IntPtr GetFontSet(Display dpy)
                        {
+                               FontExtents extents;
+                               return GetFontSet(dpy, out extents);
+                       }
+       internal IntPtr GetFontSet(Display dpy, out FontExtents extents)
+                       {
                                lock(typeof(Font))
                                {
***************
*** 283,286 ****
--- 289,293 ----
                                        {
                                                font = this;
+                                               dpy.fonts[this] = this;
                                        }
  
***************
*** 291,294 ****
--- 298,302 ----
                                                if(info.dpy == dpy)
                                                {
+                                                       extents = info.extents;
                                                        return info.fontSet;
                                                }
***************
*** 296,304 ****
                                        }
  
!                                       // TODO: create a new font set.
! 
!                                       // TODO: associate the font set with 
the display.
  
!                                       return IntPtr.Zero;
                                }
                        }
--- 304,336 ----
                                        }
  
!                                       // Create a new font set.
!                                       IntPtr fontSet;
!                                       try
!                                       {
!                                               IntPtr display = dpy.Lock();
!                                               fontSet = Xlib.XSharpCreateFont
!                                                       (display, family, 
pointSize, (int)style);
!                                               if(fontSet == IntPtr.Zero)
!                                               {
!                                                       extents = null;
!                                                       return IntPtr.Zero;
!                                               }
!                                       }
!                                       finally
!                                       {
!                                               dpy.Unlock();
!                                       }
  
!                                       // Associate the font set with the 
display.
!                                       info = new FontInfo();
!                                       info.next = font.infoList;
!                                       info.extents = new FontExtents(font, 
fontSet);
!                                       info.dpy = dpy;
!                                       info.fontSet = fontSet;
!                                       font.infoList = info;
! 
!                                       // Return the font set to the caller.
!                                       extents = info.extents;
!                                       return fontSet;
                                }
                        }

Index: Graphics.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Graphics.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** Graphics.cs 7 Jun 2003 05:40:07 -0000       1.3
--- Graphics.cs 7 Jun 2003 07:40:16 -0000       1.4
***************
*** 2114,2117 ****
--- 2114,2288 ----
                        }
  
+       /// <summary>
+       /// <para>Draw a string at a particular position using a
+       /// specified font.</para>
+       /// </summary>
+       ///
+       /// <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="font">
+       /// <para>The font to use to draw the string.</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="font"/> is <see langword="null"/>.
+       /// </para>
+       /// </exception>
+       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)
+                               {
+                                       throw new ArgumentNullException("font");
+                               }
+                               if(str == null || str == String.Empty)
+                               {
+                                       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));
+                               }
+                               finally
+                               {
+                                       dpy.Unlock();
+                               }
+                       }
+ 
+       /// <summary>
+       /// <para>Get extent information for a particular font, when drawing
+       /// onto this graphics context.</para>
+       /// </summary>
+       ///
+       /// <param name="font">
+       /// <para>The font to obtain extents for.</para>
+       /// </param>
+       ///
+       /// <returns>
+       /// <para>Returns the extent information.</para>
+       /// </returns>
+       ///
+       /// <exception cref="T:System.ArgumentNullException">
+       /// <para>Raised if <paramref name="font"/> is <see langword="null"/>.
+       /// </para>
+       /// </exception>
+       public FontExtents GetFontExtents(Font font)
+                       {
+                               if(font == null)
+                               {
+                                       throw new ArgumentNullException("font");
+                               }
+                               FontExtents extents = null;
+                               font.GetFontSet(dpy, out extents);
+                               return extents;
+                       }
+ 
+       /// <summary>
+       /// <para>Measure the width, ascent, and descent of a string,
+       /// to calculate its extents when drawn on this graphics context
+       /// using a specific font.</para>
+       /// </summary>
+       ///
+       /// <param name="str">
+       /// <para>The string to be measured.</para>
+       /// </param>
+       ///
+       /// <param name="font">
+       /// <para>The font to use to measure the string.</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="font"/> is <see langword="null"/>.
+       /// </para>
+       /// </exception>
+       public void MeasureString(String str, Font font, out int width,
+                                                         out int ascent, out 
int descent)
+                       {
+                               // Validate the parameters.
+                               if(font == null)
+                               {
+                                       throw new ArgumentNullException("font");
+                               }
+                               if(str == null || str == String.Empty)
+                               {
+                                       width = 0;
+                                       ascent = 0;
+                                       descent = 0;
+                                       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;
+                               Xlib.XSharpTextExtents
+                                       (fontSet, str, out overall_ink, out 
overall_logical);
+                               width = overall_ink.width;
+                               ascent = -(overall_ink.y);
+                               descent = overall_ink.height + overall_ink.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;
+                                       }
+                               }
+                       }
+ 
        // Draw a radio button.
        private void DrawRadio(IntPtr display,

Index: Xlib.cs.in
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/Xlib.cs.in,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** Xlib.cs.in  7 Jun 2003 05:40:07 -0000       1.6
--- Xlib.cs.in  7 Jun 2003 07:40:16 -0000       1.7
***************
*** 495,498 ****
--- 495,520 ----
                        (IntPtr display, out XEvent xevent, @X_int@ timeout);
  
+       [DllImport("XsharpSupport")]
+       extern public static IntPtr XSharpCreateFont
+                       (IntPtr display, String family, @X_int@ pointSize, 
@X_int@ style);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static void XSharpDrawString
+                       (IntPtr display, Drawable drawable, IntPtr gc,
+                        IntPtr fontSet, @X_int@ x, @X_int@ y,
+                        String str, @X_int@ style);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static void XSharpTextExtents
+                       (IntPtr fontSet, String str,
+                        out XRectangle overall_ink_return,
+                        out XRectangle overall_logical_return);
+ 
+       [DllImport("XsharpSupport")]
+       extern public static void XSharpFontExtents
+                       (IntPtr fontSet,
+                        out XRectangle max_ink_return,
+                        out XRectangle max_logical_return);
+ 
        // Helper functions from "libImlib.so" for loading images.
  

Index: XsharpSupport.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/Xsharp/XsharpSupport.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** XsharpSupport.c     7 Jun 2003 01:01:52 -0000       1.1
--- XsharpSupport.c     7 Jun 2003 07:40:16 -0000       1.2
***************
*** 44,47 ****
--- 44,49 ----
        #include <unistd.h>
  #endif
+ #include <stdlib.h>
+ #include <stdio.h>
  
  int XNextEventWithTimeout(Display *dpy, XEvent *event, int timeout)
***************
*** 79,87 ****
--- 81,282 ----
  }
  
+ /*
+  * Font style flags.
+  */
+ #define       FontStyle_Normal                0
+ #define       FontStyle_Bold                  1
+ #define       FontStyle_Italic                2
+ #define       FontStyle_Underline             4
+ 
+ /*
+  * Try to create a font with specific parameters.
+  */
+ static XFontSet TryCreateFont(Display *dpy, const char *family,
+                                                         int pointSize, int 
style)
+ {
+       XFontSet fontSet;
+       char *name;
+       const char *weight;
+       const char *slant;
+       char **missingCharsets;
+       int missingCharsetCount;
+       char *defStringReturn;
+ 
+       /* Create a buffer big enough to hold the base XLFD */
+       name = (char *)malloc(strlen(family) + 128);
+       if(!name)
+       {
+               return 0;
+       }
+ 
+       /* Determine the weight and slant to use */
+       if((style & FontStyle_Bold) != 0)
+       {
+               weight = "bold";
+       }
+       else
+       {
+               weight = "medium";
+       }
+       if((style & FontStyle_Italic) != 0)
+       {
+               slant = "i";
+       }
+       else
+       {
+               slant = "r";
+       }
+ 
+       /* Create the base XLFD */
+       if(pointSize != -1)
+       {
+               sprintf(name, "-*-%s-%s-%s-*-*-*-%d-*-*-*-*-*-*",
+                               (family ? family : "*"), weight, slant, 
pointSize);
+       }
+       else
+       {
+               sprintf(name, "-*-%s-%s-%s-*-*-*-*-*-*-*-*-*-*",
+                               (family ? family : "*"), weight, slant);
+       }
+ 
+       /* Try to create the font set using just the base name */
+       missingCharsets = 0;
+       missingCharsetCount = 0;
+       defStringReturn = 0;
+       fontSet = XCreateFontSet(dpy, name, &missingCharsets,
+                                                        &missingCharsetCount, 
&defStringReturn);
+       if(fontSet)
+       {
+               free(name);
+               return fontSet;
+       }
+       /* TODO: process the missing charsets */
+ 
+       /* Give up - we couldn't create this variant */
+       free(name);
+       return 0;
+ }
+ 
+ /*
+  * Create a font from a description.
+  */
+ XFontSet XSharpCreateFont(Display *dpy, const char *family,
+                                                 int pointSize, int style)
+ {
+       XFontSet fontSet;
+ 
+       /* Try with the actual parameters first */
+       fontSet = TryCreateFont(dpy, family, pointSize, style);
+       if(fontSet)
+       {
+               return fontSet;
+       }
+ 
+       /* Remove the style, but keep the family */
+       fontSet = TryCreateFont(dpy, family, pointSize, FontStyle_Normal);
+       if(fontSet)
+       {
+               return fontSet;
+       }
+ 
+       /* Remove the family, but keep the style */
+       fontSet = TryCreateFont(dpy, 0, pointSize, style);
+       if(fontSet)
+       {
+               return fontSet;
+       }
+ 
+       /* Remove the point size and the style, but keep the family */
+       fontSet = TryCreateFont(dpy, family, -1, FontStyle_Normal);
+       if(fontSet)
+       {
+               return fontSet;
+       }
+ 
+       /* Remove everything - this will succeed unless X has no fonts at all! 
*/
+       return TryCreateFont(dpy, 0, -1, FontStyle_Normal);
+ }
+ 
+ /*
+  * Draw a string using a font set.
+  */
+ void XSharpDrawString(Display *dpy, Drawable drawable, GC gc,
+                                         XFontSet fontSet, int x, int y,
+                                         const char *str, int style)
+ {
+       XmbDrawString(dpy, drawable, fontSet, gc, x, y,
+                                 str, strlen(str));
+       if((style & FontStyle_Underline) != 0)
+       {
+               XRectangle overall_ink_return;
+               XRectangle overall_logical_return;
+               XmbTextExtents(fontSet, str, strlen(str),
+                                          &overall_ink_return, 
&overall_logical_return);
+               XSetLineAttributes(dpy, gc, 1, LineSolid, CapNotLast, 
JoinMiter);
+               XDrawLine(dpy, drawable, gc, x, y + 2,
+                                 x + overall_ink_return.width, y + 2);
+       }
+ }
+ 
+ /*
+  * Calculate the extent information for a string.
+  */
+ void XSharpTextExtents(XFontSet fontSet, const char *str,
+                                          XRectangle *overall_ink_return,
+                                          XRectangle *overall_logical_return)
+ {
+       XmbTextExtents(fontSet, str, strlen(str),
+                                  overall_ink_return, overall_logical_return);
+ }
+ 
+ /*
+  * Calculate the extent information for a font.
+  */
+ void XSharpFontExtents(XFontSet fontSet,
+                                          XRectangle *max_ink_return,
+                                          XRectangle *max_logical_return)
+ {
+       XFontSetExtents *extents;
+       extents = XExtentsOfFontSet(fontSet);
+       if(extents)
+       {
+               *max_ink_return = extents->max_ink_extent;
+               *max_logical_return = extents->max_logical_extent;
+       }
+ }
+ 
  #else /* X_DISPLAY_MISSING || !HAVE_SELECT */
  
  int XNextEventWithTimeout(void *dpy, void *event, int timeout)
  {
+       /* Nothing to do here */
        return -1;
+ }
+ 
+ void *XSharpCreateFont(void *dpy, const char *family, int pointSize, int 
style)
+ {
+       /* Nothing to do here */
+       return 0;
+ }
+ 
+ void XSharpDrawString(void *dpy, unsigned long drawable, void *gc,
+                                         void *fontSet, int x, int y,
+                                         const char *str, int style)
+ {
+       /* Nothing to do here */
+ }
+ 
+ void XSharpTextExtents(void *fontSet, const char *str,
+                                          void *overall_ink_return,
+                                          void *overall_logical_return)
+ {
+       /* Nothing to do here */
+ }
+ 
+ void XSharpFontExtents(void *fontSet,
+                                          void *max_ink_return,
+                                          void *max_logical_return)
+ {
+       /* Nothing to do here */
  }
  





reply via email to

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