473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Drawing italic text

(C#, Microsoft .NET Framework 3.5)

Hello!

I'm in the process of creating a custom text control and I want to add functionality for superscripts and subscripts. I'm using TextRenderer for drawing and measuring text because Graphics.DrawString and Graphics.MeasureString does not come with a "NoPadding" option.

When drawing superscripts and subscripts, I'll specify the x-position as the width of the normal text. This works fine for regular and bold text, however, a problem arises when drawing italic text. Italic letters overlap eachother and so the width of an italic character returned by TextRenderer.MeasureText is shorter than what is actually drawn. So when drawing superscripts, the beginning of the superscript text crashes into the ending part of the normal text.

I can illustrate this with the following code:

private void Form1_Paint(object sender, PaintEventArgs e)
{

// The font we'll be using
Font font = new Font("Times New Roman", 200, FontStyle.Italic);

// The string we'll be measuring
string s = "d";

// Measure the text
Size size = TextRenderer.MeasureText(e.Graphics, s, font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding);

// Draw the text at x- and y-coordinates 0 and 0, respectively.
TextRenderer.DrawText(e.Graphics, "d", font, new Point(0, 0), Color.Blue, TextFormatFlags.NoPadding);
// Now the x-coordinate at the end of the text should be size.Width

// Draw a vertical line at the end of the text, with length 1000
e.Graphics.DrawLine(Pens.Red, new Point(size.Width, 0), new Point(size.Width, 1000));

// Notice that the red line goes straight through the d-character which will cause a problem
// when adding superscripts (if I was to place the superscript x-coordinate at size.Width)
}

I'm looking for some value to compensate for this. Have I overlooked a function in the .NET Framework? Is there some API function?

Thanks in advance!
Any help is appreciated!

Sincerely yours, Emil
Dec 25 '07 #1
0 1371

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: London Geek | last post by:
Hello, I'm having a rendering problem with italics in Safari 1.2. The following displays correctly and is consistent with other Mac and non-Mac browsers: ..Title { font-family: Impact;...
2
by: Adam Honek | last post by:
Okay, Assinging the font and its style (bold, italic etc.) using the font dialog is easy such as: FontDialog1.ShowDialog() txtEmailBody.SelectionFont = FontDialog1.Font But how does one...
4
by: paraidy | last post by:
Hi, i'm using a richtextbox named r1, and i want to write in Bold and italic, i tryed this for bold and it work Dim f As New Font("Verdana", 30, FontStyle.Bold) r1.Font = f r1.Text = "Hello" ...
0
by: gkostova | last post by:
Hi , Can someone one help me please.I'm begginer in C#, and I try to learn it , but I need help. I have in my form 2 spliterPamels, in one of them I want to display text when form is load. The...
5
by: Thirunavukarasu | last post by:
hi! i'm using textarea to type the contents. inwhich i need to make the text as italic if i use the following code i'm not getting the italic text instead i'm getting the html tags...
3
by: sudhivns | last post by:
Hi, following is the part of my xml file. data.xml <Value> This is normal text This has to be printed in bold. This has to be printed in Italic. This has to be printed in bold/italic.
2
by: r_ahimsa_m | last post by:
Hello, I am learning CSS and other WWW technologies. In the folowing definition italic doesn't work: h2 { color: Brown; text-decoration: italic; }
8
by: Rip van Winkle | last post by:
Here's a strange thing which took me hours to track down. I'd have thought that 'font-style: italic;' was generally harmless, but apparently not to IE7 and/or Vista. The stripped-down version of...
0
by: bettiahamit | last post by:
here i created sequence no. with every paragraph line .. like... <p local_id = P-100>aaaaaaaaaaaaaaaaaaaaaaaaa<\p> <p local_id = P-101>bbbbbbbbbbbbbbbbbbbbbbbbb<\p> <p local_id =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.