delorie.com/archives/browse.cgi   search  
Mail Archives: geda-user/2013/06/28/17:51:42

X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f
X-Recipient: geda-user AT delorie DOT com
Message-ID: <51CE04B2.3090300@sonic.net>
Date: Fri, 28 Jun 2013 14:48:34 -0700
From: Dave Curtis <davecurtis AT sonic DOT net>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2
MIME-Version: 1.0
To: geda-user AT delorie DOT com
CC: Edward Hennessy <ehennes AT sbcglobal DOT net>
Subject: Re: [geda-user] gschem cairo text measure
References: <51CCD179 DOT 7010903 AT sonic DOT net> <1E0F5AC4-88AB-4CCF-ABE3-439DBE79B907 AT sbcglobal DOT net>
In-Reply-To: <1E0F5AC4-88AB-4CCF-ABE3-439DBE79B907@sbcglobal.net>
Reply-To: geda-user AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019