/// <summary>Calculate the average character width of indicated font data.</summary> /// <param name="x11display">The display pointer, that specifies the connection to the X server.<see cref="IntPtr"/></param> /// <param name="x11gc">The crapchics context to use for drawing.<see cref="IntPtr"/></param> /// <param name="fontData">The font data to calculate the average character width for.<see cref="X11FontData"/></param> /// <returns>The average character width of indicated font data.<see cref="System.Int32"/></returns> public static int AverageCharacterWidth(IntPtr x11display, IntPtr x11gc, X11FontData fontData) { if (fontData == null) { SimpleLog.LogLine(TraceEventType.Error, CLASS_NAME + "::AverageCharacterWidth () Argument null: fontData"); return(0); } if (fontData.UseFontset) { X11lib.XRectangle overallInc = new X11lib.XRectangle(); X11lib.XRectangle overallLogical = new X11lib.XRectangle(); X11.TWchar[] text = new X11.TWchar[] { (X11.TWchar) 'X', (X11.TWchar) ' ', (X11.TWchar) 'i' }; X11lib.XwcTextExtents(fontData.FontResourceId, text, (X11.TInt)text.Length, ref overallInc, ref overallLogical); return((int)(((int)overallLogical.width + 2) / 3)); } else { TInt direction = 0; TInt fontAscent = 0; TInt fontDescent = 0; X11lib.XCharStruct xCharStruct = new X11lib.XCharStruct(); X11lib.XChar2b[] text = new X11lib.XChar2b[] { new X11lib.XChar2b('X'), new X11lib.XChar2b(' '), new X11lib.XChar2b('i') }; X11lib.XSetFont(x11display, x11gc, fontData.FontResourceId); X11lib.XQueryTextExtents16(x11display, fontData.FontResourceId, text, (X11.TInt)text.Length, ref direction, ref fontAscent, ref fontDescent, ref xCharStruct); return((int)(((int)xCharStruct.width + 2) / 3)); } }
/// <summary>Convert a C# string (2 byte (0...65.536) character array) into a 4 byte (0...4.294.967.296) character array.</summary> /// <param name="text">The C# string to convert.<see cref="System.String"/></param> /// <returns>The 4 byte (0...4.294.967.296) character array.<see cref="X11.TWchar[]"/></returns> /// <remarks>Used for X11lib.XwcDrawString6() and X11lib.XwcTextExtents().</remarks> public static X11.TWchar[] StringToWcharArray(string text) { if (string.IsNullOrEmpty(text)) { return(new X11.TWchar[0]); } try { byte[] unicode = System.Text.Encoding.Unicode.GetBytes(text); X11.TWchar[] result = new X11.TWchar[unicode.Length / 2]; for (int charIndex = 0; charIndex < unicode.Length; charIndex++) { // A 'byte' is a 1-byte unsigned integer (0...255). result[charIndex / 2] = (X11.TWchar)(unicode[charIndex] + unicode[++charIndex] * 256); } return(result); } catch { return(new X11.TWchar[0]); } }
/// <summary>Convert a C# character (2 byte (0...65.536) character) into a 4 byte (0...4.294.967.296) character.</summary> /// <param name="c">The C# character to convert.<see cref="System.Char"/></param> /// <returns>The 4 byte (0...4.294.967.296) character.<see cref="X11.TWchar"/></returns> /// <remarks>Used for X11lib.XwcDrawString6() and X11lib.XwcTextExtents().</remarks> public static X11.TWchar CharToWchar(char c) { try { byte[] unicode = System.Text.Encoding.Unicode.GetBytes(new char[] { c }); X11.TWchar result = (X11.TWchar)(unicode[0] + unicode[1] * 256); return(result); } catch { return((X11.TWchar) 0); } }
/// <summary>Determine the index of indicated character within a 4 byte (0...4.294.967.296) character array.</summary> /// <param name="text">The 4 byte (0...4.294.967.296) character array to find the index in.<see cref="X11.TWchar[]"/></param> /// <param name="character">The character to find the index for.<see cref="X11.TWchar"/></param> /// <returns>The zero-based index on success, or -1 otherwise.<see cref="System.Int32"/></returns> public static int IndexOfWcharArray(X11.TWchar[] text, X11.TWchar character) { if (text.Length == 0) { return(-1); } for (int charIndex = 0; charIndex < text.Length; charIndex++) { if (text[charIndex] == character) { return(charIndex); } } return(-1); }
/// <summary>Extract a sub-array from a 4 byte (0...4.294.967.296) character array.</summary> /// <param name="text">The 4 byte (0...4.294.967.296) character array to extract a sub-array from.<see cref="X11.TWchar[]"/></param> /// <param name="start">The index of the first character to extract.<see cref="System.Int32"/></param> /// <param name="length">The number of characters to extract, or -1 for all characters up to the end.<see cref="System.Int32"/></param> /// <returns>The extracted sub-array on success, or an empty array otherwise.<see cref="X11.TWchar[]"/></returns> public static X11.TWchar[] SubWcharArray(X11.TWchar[] text, int start, int length) { if (start >= text.Length) { return(new X11.TWchar[0]); } if (length <= 0) { length = text.Length; } int realLength = (length > 0 && start + length < text.Length ? length : text.Length - start); X11.TWchar[] result = new X11.TWchar[realLength]; for (int charIndex = 0; charIndex < realLength; charIndex++) { result[charIndex] = text[start + charIndex]; } return(result); }
/// <summary>Concatenate two 4 byte (0...4.294.967.296) character array.</summary> /// <param name="text1">The first 4 byte (0...4.294.967.296) character array to concatenate.<see cref="X11.TWchar[]"/></param> /// <param name="text2">The second 4 byte (0...4.294.967.296) character array to concatenate.<see cref="X11.TWchar[]"/></param> /// <returns>The concatenated array on success, or an empty array otherwise.<see cref="X11.TWchar[]"/></returns> /// <remarks>A tailing null character of text1 will be overwritten by the forst character of text2.</remarks> /// <remarks>Not required character of the result will be set to null characters.</remarks> public static X11.TWchar[] AddWcharArray(X11.TWchar[] text1, X11.TWchar[] text2) { if (text1.Length == 0) { return(text2); } if (text2.Length == 0) { return(text1); } X11.TWchar[] result = new X11.TWchar[text1.Length + text2.Length]; int charIndex1; for (charIndex1 = 0; charIndex1 < text1.Length; charIndex1++) { if (charIndex1 == text1.Length && text1[charIndex1] == (TWchar)0) { charIndex1++; break; } else { result[charIndex1] = text1[charIndex1]; } } for (int charIndex2 = 0; charIndex1 + charIndex2 < result.Length; charIndex2++) { if (charIndex2 < text2.Length) { result[charIndex1 + charIndex2] = text2[charIndex2]; } else { result[charIndex1 + charIndex2] = (TWchar)0; } } return(result); }
/// <summary>Initialize a new X11.Text.StyleChar instance.</summary> /// <param name="c32">The character of a styled character.<see cref="X11.TWchar"/></param> /// <param name="styleIndex">The style of a styled character.<see cref="System.Int32"/></param> public TStyleChar(X11.TWchar c32, int styleIndex) { _c = c32; _styleIndex = styleIndex; }
/// <summary>Initialize a new X11.Text.StyleChar instance.</summary> /// <param name="c16">The character of a styled character.<see cref="X11lib.XChar2b"/></param> /// <param name="styleIndex">The style of a styled character.<see cref="System.Int32"/></param> public TStyleChar(X11lib.XChar2b c16, int styleIndex) { _c = X11.X11Utils.CharToWchar(X11.X11Utils.XChar2bToChar(c16)); _styleIndex = styleIndex; }
// ############################################################################### // ### C O N S T R U C T I O N A N D I N I T I A L I Z A T I O N // ############################################################################### #region Construction /// <summary>Initialize a new X11.Text.StyleChar instance.</summary> /// <param name="c">The character of a styled character.<see cref="System.Char"/></param> /// <param name="styleIndex">The style of a styled character.<see cref="System.Int32"/></param> public TStyleChar(char c, int styleIndex) { _c = X11.X11Utils.CharToWchar(c); _styleIndex = styleIndex; }
/// <summary>Test whether 'text', starting at 'startIndex', matches the 'predicate'.</summary> //// <param name="text">The string to test.<see cref="X11.TWchar[]"/></param> /// <param name="startIndex">The index to start the test.<see cref="System.Int32"/></param> /// <param name="predicate">The mask string to compare to. Accepts '?' as single-char joker and '*' as multi-char joker.<see cref="X11.TWchar[]"/></param> /// <returns>The matching string if substring matches predicate, or string.Empty otherwise.<see cref="X11.TWchar[]"/></returns> public static X11.TWchar[] SubstringMatch(X11.TWchar[] text, int startIndex, X11.TWchar[] predicate) { if (text == null) { return(new X11.TWchar[0]); } if (predicate == null) { return(new X11.TWchar[0]); } if (text.Length - startIndex < predicate.Length) { return(new X11.TWchar[0]); } List <X11.TWchar> result = new List <X11.TWchar> (); X11.TWchar asteric = (X11.TWchar) '*'; X11.TWchar qmark = (X11.TWchar) '?'; for (int index = 0; index < predicate.Length; index++) { // [1] Either current char matches the predicate's current mask char. if (text[startIndex + index] == predicate[index] || predicate[index] == asteric /* '*' = Multi-char joker! */ || predicate[index] == qmark /* '?' = Single-char joker! */) { result.Add(text[startIndex + index]); } // [2] Or the predicate's current mask char is a multi-char joker. else if (index > 0 && predicate[index - 1] == asteric /* '*' = Multi-char joker! */) { // [A] Either multi-char joker represents a sub-string length of 0 characters. if (text[startIndex + index - 1] == predicate[index]) { index--; } // [B] Or the multi-char joker represents a sub-string length of >= 1 character(s). else { do { // [a] Either current char matches the predicate's current mask char. if (text[startIndex + index] == predicate[index]) { result.Add(text[startIndex + index]); break; } // [b] Or the current char isn't the last char and matches the predicate's last joker. if (startIndex + index < text.Length - 1) { result.Add(text[startIndex + index]); startIndex++; } // [c] Or the current char don't match at all. else { return(new X11.TWchar[0]); } } while (true); } } // [3] Or the current char don't match at all. else { return(new X11.TWchar[0]); } } return(result.ToArray()); }
/// <summary>Convert a 4 byte (0...4.294.967.296) character into a C# char (2 byte (0...65.536) character).</summary> /// <param name="c">Then 4 byte (0...4.294.967.296) character to convert.<see cref="X11.TWchar"/></param> /// <returns>The C# char (2 byte (0...65.536) character).<see cref="System.Char"/></returns> /// <remarks>This conversion might be lossy.</remarks> public static X11lib.XChar2b WcharToXChar2b(X11.TWchar c) { return(new X11lib.XChar2b((X11.TUchar)(((uint)c) & 255), (X11.TUchar)(((uint)c) >> 8))); }
/// <summary>Convert a 4 byte (0...4.294.967.296) character into a C# char (2 byte (0...65.536) character).</summary> /// <param name="c">Then 4 byte (0...4.294.967.296) character to convert.<see cref="X11.TWchar"/></param> /// <returns>The C# char (2 byte (0...65.536) character).<see cref="System.Char"/></returns> /// <remarks>This conversion might be lossy.</remarks> public static char WcharToChar(X11.TWchar c) { return(System.Text.Encoding.Unicode.GetChars(new byte[] { (byte)(((int)c) & 0x00FF), (byte)((((int)c) & 0xFF00) / 256) })[0]); }