ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <pragma@wxs.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: \getscaledglyph for width-only
Date: Sat, 20 Aug 2016 13:53:42 +0200	[thread overview]
Message-ID: <44a49f25-4093-7209-6ca4-0d3ace4d244b@wxs.nl> (raw)
In-Reply-To: <alpine.BSO.2.20.1608180755280.18513@firefly.landy.cx>

On 8/18/2016 6:30 PM, Brian R. Landy wrote:
>
>
> On Thu, 18 Aug 2016, Hans Hagen wrote:
>
>> On 8/18/2016 1:12 AM, Brian R. Landy wrote:
>>>
>>>> On Aug 17, 2016, at 5:45 PM, Hans Hagen <pragma@wxs.nl> wrote:
>>>>
>>>> On 8/17/2016 9:56 PM, Brian R. Landy wrote:
>>>>> Hi, I was wondering if there is a way to call \getscaledglyph to
>>>>> scale a
>>>>> glyph only horizontally, leaving the height untouched?  Or maybe an
>>>>> alternate command?
>>>>>
>>>>> I can sort of accomplish this with (for example)
>>>>> \scale[sx=0.75,sy=1.0]{}{A}, but that encloses the glyph in an \hbox,
>>>>> which can be problematic.  \getscaledglyph doesn't cause me any
>>>>> problems, except that it scales the height too.
>>>>
>>>> \starttext
>>>>
>>>> \definefontfeature[whatever][default][extend=2.5]
>>>>
>>>> \definedfont[Serif*whatever at 12pt]
>>>>
>>>> whatever
>>>>
>>>> \stoptext
>>>>
>>>
>>> Thank you.  One thing that \getscaledglyph does it works agnostic to
>>> the currently selected font.  For example:
>>
>> scaling glyphs in one direction is kind of bad anyway so if you want
>> just one glyph you can do
>>
>> \inframed{\scale[width=3em,height=1ex]{...}}
>>
>> or whatever suits the purpose
>
> Agreed.  Here's a little more background, so it makes some sense why I
> want to scale horizontally, and then I describe my final solution.  I
> have a specific case where I use a minus, a hyphen-minus, and a plus
> close to each other, and it's always bugged me that most fonts don't
> vertically align the hyphen-minus with the other two glyphs. An example is:
>
>   {\normalUchar"2212}100-00+
>
> I was thinking that math fonts did align these symbols, hence my
> question on Monday on how to get the hyphen-minus in math mode (and
> thanks for the quick answer on that) but unfortunately was wrong.  So
> that got me searching for a way to horizontally shrink a minus, so it
> would retain the same height and line thickness as the minus.
> Horizontal-only scaling of a hyphen/dash/minus isn't so bad.
>
> \scaled[] worked perfectly, but it breaks inside a natural table with
> character alignment enabled.  I have a commented example of this below.
> \getscaledglyph works in the table, but the vertical shrink is a
> non-starter.
>
> Here's an example:
>
> \edef\mathminus{\normalUchar"2212}
> \edef\smallminus{\getscaledglyph{0.33}{}{\normalUchar"2212}}
> \starttext
>
> This is my starting point:
>
> \bTABLE[aligncharacter=yes,alignmentcharacter={text->-}]
> \bTR \bTD \mathminus100-00+ \eTD \eTR
> \bTR \bTD           100-00  \eTD \eTR
> \bTR \bTD \mathminus 99-00+ \eTD \eTR
> \bTR \bTD \mathminus 99-00  \eTD \eTR
> \eTABLE
>
> % This table won't typeset:
> %
> % \bTABLE[aligncharacter=yes,alignmentcharacter={text->:}]
> % \bTR \bTD \mathminus100\scaled[sx=0.5,sy=1.0]{\mathminus}00:+ \eTD \eTR
> % \bTR \bTD           100\scaled[sx=0.5,sy=1.0]{\mathminus}00:  \eTD \eTR
> % \bTR \bTD \mathminus 99\scaled[sx=0.5,sy=1.0]{\mathminus}00:+ \eTD \eTR
> % \bTR \bTD \mathminus 99\scaled[sx=0.5,sy=1.0]{\mathminus}00:  \eTD \eTR
> % \eTABLE
>
> \stoptext
>
> But I came up with a solution that works.  Rather than attempt to shrink
> the minus glyph, I define a new glyph that corrects the height of the
> hyphen-minus:
>
> \startluacode
>      local function addsmallminus(tfmdata)
>          local hchar    = tfmdata.characters[0x002D]
>          local mchar    = tfmdata.characters[0x2212]
>          tfmdata.characters[0xFE000]   = {
>              width    = hchar.width,
>              height   = mchar.height,
>              commands = {
>                  { "down", hchar.height-mchar.height },
>                  { "char", 0x002D },
>              }
>          }
>      end
>
>      fonts.constructors.newfeatures("otf").register {
>          name        = "smallminus",
>          description = "small minus",
>          manipulators = {
>              base = addsmallminus,
>              node = addsmallminus,
>          }
>      }
>
> \stopluacode
>
> \definefontfeature[default][default][smallminus=yes]
> \edef\smallminus{\normalUchar"FE000}
>
> \starttext
>
> \bTABLE[aligncharacter=yes,alignmentcharacter={text->\smallminus}]
> \bTR \bTD \mathminus100\smallminus00+ \eTD \eTR
> \bTR \bTD           100\smallminus00  \eTD \eTR
> \bTR \bTD \mathminus 99\smallminus00+ \eTD \eTR
> \bTR \bTD \mathminus 99\smallminus00  \eTD \eTR
> \eTABLE
>
> \stoptext
>
> So this looks good and works with any font.  The only issue is fonts
> that use a different line thickness for the minus and hyphen-minus; this
> top-aligns the glyphs.
>
> Is there a way to make this glyph available in math-mode?  I get a "?"
> when I try:
>
> \math{\smallminus}
> \math{\mathchar"FE000}
>
> Thanks for all your help!

One problem with your approach is that you can never know if that 
private slot isn't already used. This can be avoided with

\startluacode
     local function addsmallminus(tfmdata)
         local hchar = tfmdata.characters[0x002D]
         local mchar = tfmdata.characters[0x2212]
         if hchar and mchar then
             fonts.helpers.addprivate(tfmdata, "smallminus", {
                 width    = hchar.width,
                 height   = mchar.height,
                 commands = {
                     { "down", hchar.height - mchar.height },
                     { "char", 0x002D },
                 }
             })
         end
     end
     fonts.constructors.newfeatures.otf.register {
         name        = "smallminus",
         description = "small minus",
         default     = true,
         manipulators = {
             base = addsmallminus,
             node = addsmallminus,
         }
     }
\stopluacode

The default set to true makes that you don't need to do:

% \definefontfeature[default]           [default]           [smallminus=yes]

% \definefontfeature[math-text]         [math-text]         [smallminus=yes]
% \definefontfeature[math-script]       [math-script]       [smallminus=yes]
% \definefontfeature[math-scriptscript] [math-scriptscript] [smallminus=yes]

and using \privatechar{smallminus} instead. Originally these privates 
were for internal (housekeeping and reporting) use only but i made it a 
bit more general. But to make it more convenient and to avoid too many 
such features added I'll provide

     fonts.helpers.addextraprivate("smallminus", function(tfmdata)
         local hchar = tfmdata.characters[0x002D]
         local mchar = tfmdata.characters[0x2212]
         if hchar and mchar then
             return {
                 width    = hchar.width,
                 height   = mchar.height,
                 commands = {
                     { "down", hchar.height - mchar.height },
                     { "char", 0x002D },
                 }
             }
         end
     end)

hooked into the enabled-by-default feature "extraprivates" (maybe i'll 
add a method to change these global defaults of features).

The catch with these private characters in math mode is that in math 
mode there is not really a font but families that represent sizes that 
are bound to fonts, so it's more indirect. This is why a hard coded 
number can fail as these private numbers can differ per font. But this 
works:

\def\smallminus{\privatechar{smallminus}}

\starttext

\bTABLE[aligncharacter=yes,alignmentcharacter={text->\smallminus}]
     \bTR \bTD \mathminus100\smallminus00+ \eTD \eTR
     \bTR \bTD           100\smallminus00  \eTD \eTR
     \bTR \bTD \mathminus 99\smallminus00+ \eTD \eTR
     \bTR \bTD \mathminus 99\smallminus00  \eTD \eTR
\eTABLE

[\getprivateslot{smallminus}]

[\getprivatechar{smallminus}]

$[\smallminus]$

\stoptext

Because \privatechar in text mode will expand to an utf (so in your case 
the same font is assumes because you need the slots to be the same for 
the alignment check; I might decide to share private code points for 
that purpose). In math mode you will get the right character too.

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

      parent reply	other threads:[~2016-08-20 11:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 19:56 Brian R. Landy
2016-08-17 21:45 ` Hans Hagen
2016-08-17 23:12   ` Brian R. Landy
2016-08-18  8:20     ` Hans Hagen
2016-08-18 16:30       ` Brian R. Landy
2016-08-18 22:11         ` Hans Hagen
2016-08-20 11:53         ` Hans Hagen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44a49f25-4093-7209-6ca4-0d3ace4d244b@wxs.nl \
    --to=pragma@wxs.nl \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).