Calculating Text baselines in pixels

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Calculating Text baselines in pixels

Post by capellan » Mon Nov 19, 2018 1:27 am

Hi All,

How could we calculate text baselines in pixels
starting from the top of the stack?
(Number 5 in the image)

Livecode offers closely related properties.
For example:

formattedLeft measures the left of the
character starting from stack's left.

formattedTop measures the top of the
character starting from stack's top.

formattedRect measures the enclosing rectangle
of the character starting from stack's topleft.

Thanks in advance!

Al
LiveCode text baseline.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Calculating Text baselines in pixels

Post by dunbarx » Mon Nov 19, 2018 2:39 am

Capellan.

But you have answered this yourself. If you have, say, the formattedRect of line 2 of fld "yourField", then the last item of that function is the baseline of that line, no?

Perhaps you have to make sure the bottom margin is "0"?

Craig Newman

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Calculating Text baselines in pixels

Post by capellan » Mon Nov 19, 2018 2:46 am

Hi Craig,
But you have answered this yourself. If you have, say, the formattedRect of line 2 of fld "yourField", then the last item of that function is the baseline of that line, no?
No, is not.

The last item of the formattedrect is 359 pixels from top (the bottom of the green rectangle) while the text baseline is at 346 pixels from top (the orange line)

Al
Last edited by capellan on Mon Nov 19, 2018 4:53 am, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Calculating Text baselines in pixels

Post by dunbarx » Mon Nov 19, 2018 2:53 am

Hmmm.

As usual, a bit more complicated than first appears. I tested with a field, and the fourth item of the formattedRect of a line is exactly where the baseline should be

If I set the hGrid, the baseLine moves. If the margin bottom is not "0", there is yet another displacement. I assume all these are able to be taken into account.

But I feel that Capellan has more in mind than these simplistic musings.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Calculating Text baselines in pixels

Post by dunbarx » Mon Nov 19, 2018 2:55 am

Hi.

We are both on line, I see.

Starting with a simple field with margins set to 0, are you able to verify that the baseline of any arbitrary line is at item 4 of the formattedRect?

I have a "mouseMove" handler in the card script that sets the cursor to arrow and reports the mouseLoc.

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Calculating Text baselines in pixels

Post by bogs » Mon Nov 19, 2018 2:57 am

Not sure I understand what your looking for Al, if your looking for the bottom of the text, wouldn't that just be the item of the rect of the text for the bottom?

If your looking for something else, take another shot at it, or hopefully someone else knows what your asking for.

Edit - DOH! I see Craig and I were answering at similar times, and thinking similar thoughts :D
Image

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Calculating Text baselines in pixels

Post by capellan » Mon Nov 19, 2018 3:35 am

Hi All,

Probably a screenshot and scripts for finding a pixel difference
could make the job. By the way, looks like the font baseline
rest 1 pixel above the line visible after setting the showlines
property.

http://livecode.wikia.com/wiki/ShowLines

The purpose of getting the exact x,y coordinates of the text baseline
is to save the text fields in svg format that requires this info.

For example:
https://developer.mozilla.org/en-US/doc ... ement/text

Al
LiveCode text baseline 2.png
LiveCode text baseline 3.png

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Calculating Text baselines in pixels

Post by jacque » Mon Nov 19, 2018 5:59 pm

dunbarx wrote:
Mon Nov 19, 2018 2:53 am
Hmmm.

As usual, a bit more complicated than first appears. I tested with a field, and the fourth item of the formattedRect of a line is exactly where the baseline should be

If I set the hGrid, the baseLine moves. If the margin bottom is not "0", there is yet another displacement. I assume all these are able to be taken into account.

But I feel that Capellan has more in mind than these simplistic musings.

Craig
In typography the baseline is not the bottom of the text, it is the imaginary line where the main part of the character sits. The capital D does sit directly on the baseline, whereas the descender of a lower case p will hang below it.

I'm not sure how to calculate the percentage for the height of the baseline, but in the past I've guessed at 2/3 the textheight and then adjusted it visually with trial and error. There's probably a formula for it somewhere.

I suspect LC adds a pixel when drawing gridlines just for esthetics.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Calculating Text baselines in pixels

Post by capellan » Mon Nov 19, 2018 11:00 pm

Hi All,

After consulting other sources, the most accurate method is
read truetype or opentype font and get the value directly
from the font binary data.

I will try this now.

Update: This works!

Code: Select all

put url ("binfile:" & specialFolderPath("fonts") & slash & "arial.ttf")
Now, I just have to convert the binary data in hexadecimal to
study TTF and OpenType internal structure.

Al

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Calculating Text baselines in pixels

Post by [-hh] » Tue Nov 20, 2018 5:40 pm

Did you already try mode "bounds" of measureText(text,objectReference,[mode])?
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Calculating Text baselines in pixels

Post by dunbarx » Tue Nov 20, 2018 8:49 pm

Hermann.

"measureText"

I am always surprised that there are native gadgets in LC that I have never heard of.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Calculating Text baselines in pixels

Post by [-hh] » Tue Nov 20, 2018 10:03 pm

Hi Craig.

MeasureText and its sister measureUnicodeText probably do in mode "bounds" what Al intends to do.
• In LC Script I used these very seldom.
• In LC Builder there is also "measure <text>" and "layout bounds" and "image bounds" of <text>. We can't draw text exactly on a canvas without using one of these.
shiftLock happens

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Calculating Text baselines in pixels

Post by bn » Tue Nov 20, 2018 10:17 pm

Hi Alejandro,

here is a very rough stack that determines a lot of measures for fonts. It is not cleaned up but maybe it helps.

It uses measureText and formattedText, lets you choose text size and font interactively and indicates with graphics the different values.

Kind regard

Bernd
HowFontsMeasure.livecode.zip
(9.51 KiB) Downloaded 192 times
measureText.png

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Calculating Text baselines in pixels

Post by [-hh] » Tue Nov 20, 2018 10:42 pm

Hi Bernd,

if I remember right, the formatted-"thingies" and measureText work correctly only if the margins are the default value (I think now 8 and 7 in the past). I had a long time ago an info from Peter about this bug.
May be this explains (part of) the offsets in your demo?
shiftLock happens

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Calculating Text baselines in pixels

Post by bn » Tue Nov 20, 2018 10:48 pm

Hi Hermann,

I think most of the variability is the fonts. They vary widely in what they report as baseline, upstroke and downstroke. This affects very much Asian fonts that have other requirements to display and when they display roman/ASCII text this shows.

One of the wildest outliers is Zapfino. Which is handcrafted and has weird metrics.

But I also added a snapshot of the text to determine the visible top of the text. Works quite well.

Kind regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”