View Raw SPL
/*****************************************************************************
*                                                                            *
*   TEXSYM.SPL  Copyright (C) 2020 DSP Development Corporation               *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:     Randy Race                                                   *
*                                                                            *
*   Synopsis:   Displays a unicode symbol using TeX                          *
*                                                                            *
*   Revisions:  21 Aug 2020  RRR  Creation                                   *
*                                                                            *
*****************************************************************************/


#if @HELP_TEXSYM

    TEXSYM  

    Purpose: Specifies a Unicode symbol as a TeX expression.
                                                                        
    Syntax:  TEXSYM(sym, color, size, font)

               sym - An integer. The Unicode code for the symbol.
                
              color - Optional. An integer, the color of the symbol.
                      Defaults to -1, use the current font color.

              size - Optional. An integer, the point size of the symbol.
                     Defaults to 0, use the current font point size.

              font - Optional. A string. The Unicode font. Defaults to
                     "Segoe UI Symbol" for Windows 7 and higher systems,
                     and "Lucida Sans Unicode" for all others.

    Returns: A string, the TeX expression that displays the symbol.

    Example:
             texsym(0x2600)

             returns '$\fontname{Segoe UI Symbol}\char9728$', the
             TeX expression that specifes the Unicode solid sun symbol
             in decimal for Windows 7 and higher systems.

    Example:
             W1: 1..10;text(5, 8, PAPER, blue, texsym(0x2600))

             W1 contains a 10 sample series. The Unicode symbol for a solid
             sun is displayed in dark blue at coordinate (5, 8).

    Example:
             W2: 1..10;text(5, 8, PAPER, blue, texsym(0x2600, lred, 36))

             Similar to above except the symbol is displayed in light
             red at a 36 point size.

    Example:
             W3: 1..10;text(5, 8, PAPER, blue, "Have a " + texsym(0x2600, lred, 36) + " Day")

             Similar to above except the symbol is displayed between the 
             dark blue text "Have a" and "Day". The symbol is displayed in 
             light red at a 36 point size.

    Example:
             W4: 1..10;text(5, 8, PAPER, lred, texsym(0x2600, -1, 36)+texsym(0x2602, green, 18))

             Similar to above except a dark green, 18 point sized
             umbrella symbol is displayed after the light red, 36 point
             sun symbol.

    Remarks:
             TEXSYM returns the TeX expression to display the specified 
             Unicode symbol.

             If specified, the font must be capable of displaying
             Unicode characters.

             When using the GUI Drawing Toolbar to enter text, wrap the
             TEXSYM expression in {} to force evaluation. For example:

                   Have a {texsym(0x2600)} Day

            If COLOR is unspecified or -1, the color can be modified by
            right clicking the symbol and selecting "Properties".

            If SIZE is unspecified or 0, the size can be modified by
            right clicking the symbol and selecting "Properties".

    See Also:
             TeX
             Text
#endif


/* return TeX expression for Unicode symbol */
texsym(sym, color, size, font)
{
        /* parse args */
        (sym, color, size, font) = texsym_parse_args(sym, color, size, font);

        /* TeX symbol in decimal */
        sym = sprintf('\fontname{%s}\char%d', font, sym);

        /* color */
        if (color >= 0)
        {
                sym = sprintf('\textcolor{%d}%s', color, sym);
        }

        /* point size */
        if (not(size == 0))
        {
                sym = sprintf('\fontsize{%d}%s', -size, sym);
        }

        /* embedded TeX form */
        sym = sprintf("$%s$", sym);

        return(sym);
}


/* parse args */                
texsym_parse_args(sym, color, size, font)
{
        local sym_arg, color_arg, size_arg, font_arg = {};

        /* default to sun symbol */
        sym_arg = 0x2600;        

        if (isstring(sym))
        {
                font_arg = sym;
        }
        else if (not(isunspecified(sym)))
        {
                sym_arg = sym;
        }

        /* color */
        color_arg = -1;

        if (isstring(color))
        {
                font_arg = color;
        }
        else if (not(isunspecified(color)))
        {
                color_arg = color;
        }

        /* size */
        size_arg = 0;

        if (isstring(size))
        {
                font_arg = size;
        }
        else if (not(isunspecified(size)))
        {
                size_arg = size;
        }

        /* font */
        if (isempty(font_arg))
        {
                /* default Unicode, Windows 7 and above vs all others */
                font_arg = (getcomputer(1) >= 601) ? "Segoe UI Symbol" 
                                                   : "Lucida Sans Unicode";

                if (not(isunspecified(font)))
                {
                        font_arg = font;
                }
        }

        return(sym_arg, color_arg, size_arg, font_arg);
}