public SizeF MeasureString(Graphics g, string str) { using (GdiObjectSelector selector = new GdiObjectSelector(g, _font)) { Size size = new Size(0, 0); Win32FontSystem.GetTextExtentPoint(selector.HDC, str, str.Length, ref size); return(new SizeF(size.Width, size.Height)); } }
public float GetBaselinePos(Graphics g, FontStyle style) { using (GdiObjectSelector selector = new GdiObjectSelector(g, _font)) { Win32FontSystem.TEXTMETRIC metrics; Win32FontSystem.GetTextMetrics(selector.HDC, out metrics); return(((float)metrics.tmAscent / metrics.tmHeight) * _logFont.lfHeight); } }
public float[] GetCharacterXPositions(Graphics g, string str) { #if true // Make sure our _charWidths list has all the necessary characters. using (GdiObjectSelector selector = new GdiObjectSelector(g, _font)) { UpdateCharWidths(selector.HDC, str); } float[] xCoords = new float[str.Length]; float curXOffset = 0; for (int i = 0; i < str.Length; i++) { xCoords[i] = curXOffset; Win32FontSystem.ABCFLOAT abc = _charWidths[(uint)str[i]]; curXOffset += abc.abcfA + abc.abcfB + abc.abcfC; } return(xCoords); #else // This method gives us spacing between character cells, but it doesn't tell us what the cells are! Win32FontSystem.GCP_RESULTS results = new Win32FontSystem.GCP_RESULTS(); results.StructSize = Marshal.SizeOf(typeof(Win32FontSystem.GCP_RESULTS)); // Setup the int array for them to write results into. int[] dx = new int[str.Length]; GCHandle handle = GCHandle.Alloc(dx, GCHandleType.Pinned); results.Dx = Marshal.UnsafeAddrOfPinnedArrayElement(dx, 0); using (GdiObjectSelector selector = new GdiObjectSelector(g, _font)) { // Call GetCharacterPlacement Win32FontSystem.GetCharacterPlacementW( selector.HDC, str, str.Length, 0, // max extent (ignored) ref results, (uint)Win32FontSystem.GCPFlags.GCP_USEKERNING); } // Unpin the array. handle.Free(); // Convert to floats for output. return(dx.Select(x => (float)x).ToArray()); #endif }
public void DrawString(Graphics g, string str, Brush brush, Point location) { SolidBrush solidBrush = brush as SolidBrush; if (solidBrush == null) { throw new Exception("Win32Font.DrawString only supports SolidBrush"); } using (GdiObjectSelector selector = new GdiObjectSelector(g, _font)) { // Make a pen to represent the color. uint crColor = (uint)(solidBrush.Color.R << 0) | (uint)(solidBrush.Color.G << 8) | (uint)(solidBrush.Color.B << 16); uint prevTextColor = Win32FontSystem.SetTextColor(selector.HDC, crColor); // Set background mode to transparent. int oldBKMode = Win32FontSystem.SetBkMode(selector.HDC, Win32FontSystem.BKMODE_TRANSPARENT); Win32FontSystem.TextOut(selector.HDC, location.X, location.Y, str, str.Length); Win32FontSystem.SetBkMode(selector.HDC, oldBKMode); Win32FontSystem.SetTextColor(selector.HDC, prevTextColor); } }
public float GetBaselinePos( Graphics g, FontStyle style ) { using ( GdiObjectSelector selector = new GdiObjectSelector( g, _font ) ) { Win32FontSystem.TEXTMETRIC metrics; Win32FontSystem.GetTextMetrics( selector.HDC, out metrics ); return ( ( (float)metrics.tmAscent / metrics.tmHeight ) * _logFont.lfHeight ); } }
public float[] GetCharacterXPositions( Graphics g, string str ) { #if true // Make sure our _charWidths list has all the necessary characters. using ( GdiObjectSelector selector = new GdiObjectSelector( g, _font ) ) { UpdateCharWidths( selector.HDC, str ); } float[] xCoords = new float[ str.Length ]; float curXOffset = 0; for ( int i=0; i < str.Length; i++ ) { xCoords[i] = curXOffset; Win32FontSystem.ABCFLOAT abc = _charWidths[ (uint)str[i] ]; curXOffset += abc.abcfA + abc.abcfB + abc.abcfC; } return xCoords; #else // This method gives us spacing between character cells, but it doesn't tell us what the cells are! Win32FontSystem.GCP_RESULTS results = new Win32FontSystem.GCP_RESULTS(); results.StructSize = Marshal.SizeOf( typeof( Win32FontSystem.GCP_RESULTS ) ); // Setup the int array for them to write results into. int[] dx = new int[ str.Length ]; GCHandle handle = GCHandle.Alloc( dx, GCHandleType.Pinned ); results.Dx = Marshal.UnsafeAddrOfPinnedArrayElement( dx, 0 ); using ( GdiObjectSelector selector = new GdiObjectSelector( g, _font ) ) { // Call GetCharacterPlacement Win32FontSystem.GetCharacterPlacementW( selector.HDC, str, str.Length, 0, // max extent (ignored) ref results, (uint)Win32FontSystem.GCPFlags.GCP_USEKERNING ); } // Unpin the array. handle.Free(); // Convert to floats for output. return dx.Select( x => (float)x ).ToArray(); #endif }
public void DrawString( Graphics g, string str, Brush brush, Point location ) { SolidBrush solidBrush = brush as SolidBrush; if ( solidBrush == null ) throw new Exception( "Win32Font.DrawString only supports SolidBrush" ); using ( GdiObjectSelector selector = new GdiObjectSelector( g, _font ) ) { // Make a pen to represent the color. uint crColor = (uint)( solidBrush.Color.R << 0 ) | (uint)( solidBrush.Color.G << 8 ) | (uint)( solidBrush.Color.B << 16 ); uint prevTextColor = Win32FontSystem.SetTextColor( selector.HDC, crColor ); // Set background mode to transparent. int oldBKMode = Win32FontSystem.SetBkMode( selector.HDC, Win32FontSystem.BKMODE_TRANSPARENT ); Win32FontSystem.TextOut( selector.HDC, location.X, location.Y, str, str.Length ); Win32FontSystem.SetBkMode( selector.HDC, oldBKMode ); Win32FontSystem.SetTextColor( selector.HDC, prevTextColor ); } }
public SizeF MeasureString( Graphics g, string str ) { using ( GdiObjectSelector selector = new GdiObjectSelector( g, _font ) ) { Size size = new Size( 0, 0 ); Win32FontSystem.GetTextExtentPoint( selector.HDC, str, str.Length, ref size ); return new SizeF( size.Width, size.Height ); } }