Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the glyphs line up if they're in the same Graphics?

I'm trying to overlay glyphs from different fonts, but it's hard to get them to line up. I'm sure they ought to line up better than this. How can I do that?

Graphics[
   {Opacity[0.1],
      {Text[Style["a", FontFamily -> "Helvetica", 240]],
       Text[Style["a", FontFamily -> "Arial", 240]]}
   }]

Also, I'm interested in drawing the outline - here I've selected them, but I'd like to draw them.

Screenshot

Edit: Thanks! With your help I was able to achieve most of what I was trying to:

Manipulate[
 Graphics[{{{Opacity[opacity], 
     Text[Style["Greats", Red, FontFamily -> "Helvetica",  180], {0, 
       0}, {Center, Baseline}]}, {Opacity[1 - opacity], 
     Text[Style["Greats", Blue, FontFamily -> "Arial", 180],  {0, 
       0}, {Center, Baseline}]}}}], {opacity, 0.1, 1, 0.1}]

Screenshot

like image 474
cormullion Avatar asked Nov 04 '11 20:11

cormullion


2 Answers

By default the text is centered (horizontally and vertically) at a point, and the fonts have characters of different heights, resulting in shifted text. For example compare the following:

Graphics[{Opacity[
   0.1], {Text[
    Style["agÄ", FontFamily -> "Helvetica", 240, 
     Background -> RGBColor[0.8, 0, 0, 0.2]]], 
   Text[Style["agÄ", FontFamily -> "Arial", 240, 
     Background -> RGBColor[0, 0, 0.8, 0.2]]]}
  }]
Graphics[{Opacity[
   0.1], {Text[
    Style["agA", FontFamily -> "Helvetica", 240, 
     Background -> RGBColor[0.8, 0, 0, 0.2]]], 
   Text[Style["agA", FontFamily -> "Arial", 240, 
     Background -> RGBColor[0, 0, 0.8, 0.2]]]}
  }]

enter image description here

If you position the text along an edge, it might work better for you, although it's by no means guaranteed:

Graphics[{Opacity[
   0.1], {Text[
    Style["a", FontFamily -> "Helvetica", 240], {0, 0}, {Center, 
     Bottom}], 
   Text[Style["a", FontFamily -> "Arial", 240], {0, 0}, {Center, 
     Bottom}]}}]

enter image description here

Edit Using Baseline as suggested by Heike:

Graphics[{Opacity[
   0.1], {Text[
    Style["a", FontFamily -> "Helvetica", 240], {0, 0}, {Center, 
     Baseline}], 
   Text[Style["a", FontFamily -> "Arial", 240], {0, 0}, {Center, 
     Baseline}]}}]

enter image description here

like image 133
Brett Champion Avatar answered Nov 02 '22 04:11

Brett Champion


Regarding your first question, welcome to the world of different fonts. They have different metrics and that is just how it is. You could use Overlay or one of the graphics options like ImagePadding to shift one over the other.

On your second question, the second "neat example" in the documentation on JoinedCurve shows how to do this using ExportString and ImportString.

like image 4
Verbeia Avatar answered Nov 02 '22 03:11

Verbeia