Confusing font information from CurrentValue

5

3

I'm trying to programmatically get the width of a character in Mathematica's default fixed-width font, Courier. I believe CurrentValue holds the answer but I'm having trouble understanding correct usage.

Four examples:

In[1]:= CurrentValue[$FrontEnd, FontFamily]
Out[1]= "Times"

Example 1: Expected Courier, since input is typed in Courier.

In[2]:= s = Style["Confusion", FontFamily -> "Times"]; CurrentValue[s, FontFamily]
Out[2]= "Courier"

Example 2: Expected Times, since I specifically chose that font family for s.

In[3]:= CurrentValue[s, "FontMWidth"] / CurrentValue[s, "FontNWidth"]
Out[3]= 2.

Example 3: Expected exactly 1, since since all letters should be equally wide in Courier.

In[4]:= CurrentValue[$FrontEnd, "FontMWidth"] / CurrentValue[$FrontEnd, "FontNWidth"]
Out[4]= 2.

Example 4: Expected about 1.2, based on a quick measurement of the width ratio of Times' M to N.

A prior question about fonts and CurrentValue has an example of using DynamicWrapper to get information out of dynamic objects, but I'm still curious how to use CurrentValue correctly in non-dynamic settings, and why the ratio of FontMWidth/FontNWidth is always 2.

I'm on Mac OS X 10.8.5 and Mathematica 9.0.1.0.

duozmo

Posted 2013-11-04T03:40:24.260

Reputation: 327

discussion here may be relevant: http://mathematica.stackexchange.com/questions/17795/grid-layout-problems-different-sizes-when-rendering-on-mac-and-windows

– Mike Honeychurch – 2013-11-04T03:54:32.117

@Mike Thanks, right on. – duozmo – 2013-11-06T02:34:47.560

Answers

5

After additional research, I believe I can (mostly) explain all four examples.

Example 1: CurrentValue is returning the default font for the front end, not the font you're typing in. It's the same as looking at Options Inspector→Formatting→Font→FontFamily, or evaluating:

Options[$FrontEnd, FontFamily]
(*
  {FontFamily -> "Times"}
*)

Example 2: CurrentValue is returning the font of the input cell. You can change the cell font and get different results.

CurrentValue output from cell font change

CurrentValue is likely falling back on a default response for cases where it's given an object it doesn't have specific behavior for, although this is conjecture*. Broadly speaking, CurrentValue is for interacting with the front end, so it's little surprise that s, or any such symbol, is not among by CurrentValue's repertoire. The objects CurrentValue can report on are in the reference documentation and the M.SE question "Items known by CurrentValue."

* Tracing CurrentValue reveals a MathLink call to the front end, at which point my investigative skills diminish.

Examples 3 & 4: These appear to be related to a genuine bug with calculating character width on OS X.

duozmo

Posted 2013-11-04T03:40:24.260

Reputation: 327