# Any automated system to compute the linear length of a letter(s) in a word or phrase?

I may be way out of league asking this question but is there a way to get the linear length of a letter or words. That is, if you were to take the word "super" and measured the outside border of the letters you'd get a measurement. Is there something out there that would tell me this measurement if I give the font and the font size? I'm a rookie here so be gentle. thanks in advance.

-

This is trivial to do using TeX/LaTeX (though you'll want to use a newer version such as xetex or luatex so as to be able to easily access OpenType and TrueType fonts):

\documentclass{standalone}
\usepackage{fontspec}
\setmainfont{Myriad Pro Black Condensed}% <<-- change to desired font
\usepackage[nomessages]{fp}
\makeatletter
\newcommand{\printer}[3][in]{%
\FPeval\temp{round(\expandafter\strip@pt\csname#2\endcsname/72.27:#3)}%
\temp#1}
\makeatother
\begin{document}
\newlength{\samplewidth}
\settowidth{\samplewidth}{Test} % <<-- change to the text which you wish to measure
\printer{samplewidth}{4}
\end{document}


which will output: 0.2060in

OpenType features are supported:

\settowidth{\samplewidth}{Test 0123456789}
\printer{samplewidth}{4}\par
\settowidth{\samplewidth}{Test 0123456789}
\printer{samplewidth}{4}


which outputs:

0.8425in

0.7911in

-
Itneresting, thanks. I'll check that out. –  mrbungle May 12 at 17:31
When it comes to contextual alternates, discretionary ligatures and the gamut of OpenType features/substitutions, I don't know that "trivial" is quite the word I'd use... :) [BTW: Haven't we met on the InDesign listserv, or are you a different LaTeX aficionado of the same name?] –  Alan Gilbertson May 12 at 17:37
Which OpenType feature would you like to turn on --- they all work and are accessible (enabling options in a pre-written program qualifies as trivial, no). Yes, I'm the willadams from the InDesign list. –  WillAdams May 12 at 17:39
<grinning widely> Only a true-blue, fully-paid-up, geek's geek could call 14 lines of code "trivial" in the first place, so I suppose on that basis that adding the necessary OTF parameters would similarly qualify. –  Alan Gilbertson May 12 at 17:46
Great to see you here, though. You have several areas of knowledge that aren't much represented here otherwise (said LaTeX being a good case in point). –  Alan Gilbertson May 12 at 17:48

In terms of commonly-available tools, no, there's nothing that will take [random-font] [size] and [characters] as inputs and spit out a length. There are several reasons why this will always be true, but the main one is that every normal (not monospaced) font has its own kerning table that can and often does vary somewhat from one release of the font to the next, not to mention the fact that different type foundries publish their own versions of popular typefaces (Futura, Times, Gill Sans, etc.), with their own variations in character outlines and kerning. All that, and we haven't even mentioned ligatures, tracking, custom kerning or automatic character alternates or swashes such as are commonly available in OpenType fonts.

So although a measuring tool of the kind you're asking about could in theory be made, the practicalities involved in making it actually useful would probably make it too expensive to be worth the effort.

All is not lost, though. Every design application has some form of measuring tool (even, as WillAdams points out, specifically for type in the case of LaTeX) that can be used to measure anything on the page. In the Adobe applications it's found nested with the Eyedropper tool. It's manual, but it works.

-
Thanks for the detailed answer. Although over my head it's not completly difficult to understand what you're getting at. –  mrbungle May 12 at 17:34
If it seems over your head, just pick up the individual bits of terminology you're not familiar with and google them. Terminology is 90% of the battle in getting comfortable with and being able to become facile with any subject. –  Alan Gilbertson May 12 at 17:41
Indeed. I'm a dba/report writer thrown into the postion. This topic however is just for a personal project. :-) –  mrbungle May 12 at 17:47
In that case, you'll want to take a look at: pytex.org/texd –  WillAdams May 12 at 18:42
I'll check that out as well, looks interesting and something I'll probably end knowing too much of just for a personal side project, lol. Always learning! –  mrbungle May 12 at 19:23

There are many ways this could be done. I'm showing here how a engineer would approach the problem. The point is to show that the approach is in many ways analogous to many other fields. Enumerating it aloud seems boring and in simple cases like this its done silently in the head. This just illustrates the method.

Warning long explanation.

### Stage 1: Define solution

• Get width of string.
• Using specific font.
• Preferably in a technically usable way so minimal human intervention.
• Using font encoding: Unknown ask clarification, affects solution
• Purpose: Unknown ask clarification, affects solution
• How often is the tool needed: Unknown ask clarification, affects solution if often
• need batch processing: Unknown ask clarification, affects solution if needed
• user input: string plus font? Disambiguate
• output: for a automated process or for manual use?
• use environment: What operating system? Unknown ask clarification, strongly affects solution
• unit: Pixels, pt, inches or mm? Unknown ask clarification, affects solution

As you can see there's a lot of unknowns that need to be answered. Especially if one wants to have a good solution that could be shipped to client. Always strive to understand the problem.

### Stage 2: Search for methods in literature (or brainstorm)

First stage is to list applications and methods that could yield answers:

Graphics applications (adobe has scripting api OK)
Page setters (TEX solution above)
Operating system (windows has methods for font metrics)
Manually parse fontfile (Infeasible, many formats in one need to implement a lot of routines)
postscript interpretters (solution known)
GUI toolkits (QT can do this)
Other Tookints (Graphing tools like Scipy have this info)


The list is sufficent go test

### Stage 3: Prototype (sketch)

using postscript

Postscript is a known method, problem with postscript is what engine do you use to do this as they have slightly different effects. Using gohostscript following batch file works:

:: usage stringWidth string systemfont
set "ps=(%1) /%2 findfont 12 scalefont setfont 0 0 moveto true charpath pathbbox pop exch pop exch sub pstack"
gswin64c -dQUIET -dBATCH -r1200 -dNODISPLAY -sFONTPATH=%windir%\fonts -c %ps%


Works on windows, returns point width, probably also works linux with a few changes. Border constraints unknown. Ask clarification. Problems: need to fix char encoding or use intermediary ps file. Otherwise no spaced string works. Nasty encoding issues, manageable if using western languages.

Using Illustrator and javascipt

using illustrator manually: If you outline the path you can read the width of the text in the width box in the gui. Once this is understood it can be scripted.

This can be codified in javascript:

#target illustrator

text.contents = "test";
text.textRange.characterAttributes.size = 12;
text.textRange.characterAttributes.textFont = textFonts.getByName("Onyx");

var outline = text.createOutline();
var bounds = outline.geometricBounds
var width = bounds[1]-bounds[3]