// Get the line spacing for the font selected into this graphics object. public override int GetLineSpacing() { Xsharp.Font xfont = (font as DrawingFont).xfont; FontExtents extents = xfont.GetFontExtents(graphics); return(extents.Ascent + extents.Descent); }
// Draw a string using the current font and brush. public override void DrawString (String s, int x, int y, StringFormat format) { Xsharp.Font xfont = (font as DrawingFont).xfont; FontExtents extents = xfont.GetFontExtents(graphics); xfont.DrawString(graphics, RestrictXY(x), RestrictXY(y) + extents.Ascent, s, 0, s.Length); }
// Select this font into a graphics object. public void Select(IToolkitGraphics _graphics) { DrawingGraphics graphics = (_graphics as DrawingGraphics); if(graphics != null) { lock(this) { if(xfont == null) { xfont = Xsharp.Font.CreateFont (properties.Name, (int)(properties.SizeInPoints * (10.0f * (PointSizeConversion/dpi))), (Xsharp.FontStyle)(properties.Style)); } graphics.Font = this; } } }
// Select this font into a graphics object. public void Select(IToolkitGraphics _graphics) { DrawingGraphics graphics = (_graphics as DrawingGraphics); if (graphics != null) { lock (this) { if (xfont == null) { xfont = Xsharp.Font.CreateFont (properties.Name, (int)(properties.SizeInPoints * (10.0f * (PointSizeConversion / dpi))), (Xsharp.FontStyle)(properties.Style)); } graphics.Font = this; } } }
// Measure a string using the current font and a given layout rectangle. public override Size MeasureString (String s, System.Drawing.Point[] layoutRectangle, StringFormat format, out int charactersFitted, out int linesFilled, bool ascentOnly) { // TODO: line wrapping, etc int width, ascent, descent; charactersFitted = 0; linesFilled = 0; Xsharp.Font xfont = (font as DrawingFont).xfont; xfont.MeasureString (graphics, s, 0, s.Length, out width, out ascent, out descent); if (!ascentOnly) { return(new Size(width, ascent + descent)); } else { return(new Size(width, ascent)); } }
// Constructor. public DrawingFont(System.Drawing.Font properties, float dpi) { this.properties = properties; this.dpi = dpi; this.xfont = null; }
/// <summary> /// <para>Construct a new application object, process command-line /// options, and open the display.</para> /// </summary> /// /// <param name="name"> /// <para>The resource name and class for the application.</para> /// </param> /// /// <param name="args"> /// <para>The arguments that came from the application's "Main" /// method.</para> /// </param> /// /// <exception cref="T:Xsharp.XCannotConnectException"> /// <para>A connection to the X display server could not /// be established.</para> /// </exception> public Application(String name, String[] args) { String[] envCmdLine; int firstArg = 0; String fontName; bool synchronous = false; // Set this as the primary application object if necessary. lock(typeof(Application)) { if(primary == null) { primary = this; } } // Choose defaults for the parameters. try { envCmdLine = Environment.GetCommandLineArgs(); } catch(NotSupportedException) { envCmdLine = null; } if(envCmdLine != null && envCmdLine.Length > 0) { programName = envCmdLine[0]; } else { programName = "Xsharp-application"; } if(name == null) { // Strip the path from the program name to // get the default resource name to use. int index = programName.LastIndexOf('/'); if(index == -1) { index = programName.LastIndexOf('\\'); } if(index != -1) { name = programName.Substring(index + 1); } else { name = programName; } int len = name.Length; if(len > 4 && name[len - 4] == '.' && (name[len - 3] == 'e' || name[len - 3] == 'E') && (name[len - 2] == 'x' || name[len - 2] == 'X') && (name[len - 1] == 'e' || name[len - 1] == 'E')) { name = name.Substring(0, len - 4); } } if(args == null) { if(envCmdLine != null && envCmdLine.Length > 0) { args = envCmdLine; firstArg = 1; } else { args = new String [0]; } } // Initialize the application state. resourceName = name; resourceClass = name; displayName = null; startIconic = false; title = null; geometry = null; fontName = null; // Process the standard Xt command-line options. ArrayList newArgs = new ArrayList(); while(firstArg < args.Length) { switch(args[firstArg]) { case "-display": { ++firstArg; if(firstArg < args.Length) { displayName = args[firstArg]; } } break; case "-iconic": { startIconic = true; } break; case "-name": { ++firstArg; if(firstArg < args.Length) { resourceName = args[firstArg]; } } break; case "-title": { ++firstArg; if(firstArg < args.Length) { title = args[firstArg]; } } break; case "-fn": case "-font": { ++firstArg; if(firstArg < args.Length) { fontName = args[firstArg]; } } break; case "-geometry": { ++firstArg; if(firstArg < args.Length) { geometry = args[firstArg]; } } break; case "+synchronous": case "-synchronous": { // Turn on synchronous processing to the X server. synchronous = true; } break; // Ignore other Xt toolkit options that aren't // relevant to us. We may add some of these later. case "-reverse": case "-rv": case "+rv": break; case "-bg": case "-background": case "-bw": case "-borderwidth": case "-bd": case "-bordercolor": case "-fg": case "-foreground": case "-selectionTimeout": case "-xnllanguage": case "-xrm": case "-xtsessionID": ++firstArg; break; default: { // Unknown option - copy it to "newArgs". newArgs.Add(args[firstArg]); } break; } ++firstArg; } cmdLineArgs = (String[])(newArgs.ToArray(typeof(String))); // Connect to the display. if(displayName == null) { // Xlib will figure it by itself, but classes using displayName can get broken is it's null displayName = Environment.GetEnvironmentVariable("DISPLAY"); } display = Xsharp.Display.Open(displayName, this, synchronous); // Create the default font. defaultFont = Font.CreateFromXLFD(fontName); defaultFont.GetFontSet(display); }
// Get the caption font. private static Font GetCaptionFont() { lock(typeof(CaptionWidget)) { if(captionFont == null) { captionFont = Font.CreateFont (Font.DefaultSansSerif, 120, FontStyle.Bold); } return captionFont; } }
/// <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) { if(font == null) { throw new ArgumentNullException("font"); } if(str == null || (str.Length == 0)) { width = 0; ascent = 0; descent = 0; return; } font.MeasureString(this, str, 0, str.Length, out width, out ascent, out descent); }
/// <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"); } return font.GetFontExtents(this); }
/// <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) { if(font == null) { throw new ArgumentNullException("font"); } if(str == null || (str.Length == 0)) { return; } font.DrawString(this, x, y, str, 0, str.Length); }