Mail Archives: geda-user/2013/06/28/17:51:42
On 06/27/2013 06:48 PM, Edward Hennessy wrote:
> On Jun 27, 2013, at 4:57 PM, Dave Curtis <davecurtis AT sonic DOT net> wrote:
>
>> I'm working on a symbol generator, and trying to match up text width measurement with gschem. I've dug into the gschem code a little, and gschem seems to use Cairo these days. I'm not very familiar with Cairo, but I've managed to come up with something that appears to work, but it relies heavily on a magic number that I arrived at empirically.
>>
>> What is the correct way to match up text measurement?
>>
>> Here is what I have:
>>
>> class FontInfo(object):
>> _gschemScalingConstant = 10000.0/555.0 # Magic number
>> def __init__(self):
>> blah blah blah
>> def measure(self, aString):
>> "Returns the layout length of aString in gschem distance."
>> if self._csf == None:
>> self._build_csf()
>> width = self._csf.text_extents(aString)[2] # item 2 of extents is Cairo 'width'
>> return width
>> def _build_csf(self):
>> "Builds and caches a Cairo Scaled Font."
>> fontFace = cr.ToyFontFace(self.name) # default is Arial
>> identityMatrix = cr.Matrix()
>> fontOptions = cr.FontOptions() # get defaults
>> scaling = self.size * self._gschemScalingConstant # 'size' is gschem font size from 'T' line
>> scalingMatrix = cr.Matrix(xx = scaling, yy = scaling)
>> self._csf = cr.ScaledFont(fontFace, scalingMatrix,
>> identityMatrix, fontOptions)
>>
>> Peter Clifton's name is all over the Cairo commits, but I see he hasn't posted to the list for about a week, so I'm guessing he's on holiday. Any help appreciated.
>>
>> -dave
>>
> Dave,
>
> When I worked on text rendering, I wound up with a couple magic numbers too:
>
> http://git.geda-project.org/gparts/tree/src/schgui-cairo-text.c
>
> I looked through the code and could not find the magic constant to make the
> text match up with the legacy renderer.
>
> Cheers,
> Ed
Thanks for the link. That's good reference code.
I think, for now, I'm going to call my "magic number" good enough, since
it seems to be pretty close, and for the most part I'm only using it for
layout calculations that get rounded up to grid (100 unit) boundaries.
So as long as it doesn't cause rounding to the wrong 100-unit width,
it's good. There is one place where the text length is critical to
getting text centered correctly, but again, my magic number seems close
enough for now.
I wish I understood why the magic number that I found works, but I'll
leave that mystery for now, and try to wrap up a couple of other more
pressing issues so that my symbol generator is ready for a public push.
-dave
- Raw text -