Page 1 of 1

drawing strings and character position problem

Posted: Mon Feb 16, 2015 6:02 pm
by KG_is_back
I'm attempting to make a TurboPascal-styled code editor for the Custom DSPcode project. However, I run into two problems.

The actual character height (the hight of a line - the actual size of the character is irrelevant) does not match with font size and I also can't find the width-to-height ratio for one character. I'm using Courier New (a monospaced font).

measureString method is also not reliable - it calculates rectangle where text is drawn (from first drawn pixel to the last one)- not the area that monospace characters actually occupy (including empty pixels at the bottom and sides of the characters).

My goal is to draw a text onto a grid, where raws align and then use x,y coorinates on that grid to specify cursor position. But without a way to properly align text and the grid (by knowing the space each char occupies) it is impossible.

Only workaround I can think of is to split the text into individual characters and draw them onto the grid precisely one by one. However, I'd like to not do so, because it takes quite long (cpu spikes, gui freezing etc.)

Re: drawing strings and character position problem

Posted: Fri Feb 20, 2015 12:19 am
by tulamide
Totally missed that post, sorry!

A font is constructed from measurements. It all starts with the base line, where all glyphs "sit on". From there, a bunch of values describe different heights and widths, which finally make up the complete size. There's ascent, descent, internal leading, external leading, bearing, etc. If interested, google for "font metrics" and "typography".

You could access font metrics using GDI/GDI+. However there's also a quick Ruby solution to your specific problems. Hope it helps!

EDIT: Updated file to an even better version ;)
EDIT 2: For some reason, I reuploaded the old version :roll: Hopefully this time it's the right one