drawing strings and character position problem

For general discussion related FlowStone
Post Reply
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

drawing strings and character position problem

Post 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.)
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: drawing strings and character position problem

Post 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
Attachments
char_width_line_height.fsm
correct version. use this.
(774 Bytes) Downloaded 867 times
char_width_line_height.fsm
wrong version
(899 Bytes) Downloaded 853 times
"There lies the dog buried" (German saying translated literally)
Post Reply