Example #1
0
    // Chapter: Graphics / Text
    // Short Description: Set font independent of OS
    public void set_display_font(object hv_WindowHandle, object hv_Size, object hv_Font,
                                 object hv_Bold, object hv_Slant)
    {
        HOperatorSetX    Op    = new HOperatorSetX();
        HTupleX          Tuple = new HTupleX();
        HDevWindowStackX COMExpWinHandleStack = new HDevWindowStackX();
        HSystemX         sys = new HSystemX();

        // Local control variables

        object hv_OS, hv_Exception = null, hv_AllowedFontSizes = null;
        object hv_Distances = null, hv_Indices = null;

        // Initialize local and output iconic variables

        //This procedure sets the text font of the current window with
        //the specified attributes.
        //It is assumed that following fonts are installed on the system:
        //Windows: Courier New, Arial Times New Roman
        //Linux: courier, helvetica, times
        //Because fonts are displayed smaller on Linux than on Windows,
        //a scaling factor of 1.25 is used the get comparable results.
        //For Linux, only a limited number of font sizes is supported,
        //to get comparable results, it is recommended to use one of the
        //following sizes: 9, 11, 14, 16, 20, 27
        //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
        //
        //input parameters:
        //WindowHandle: The graphics window for which the font will be set
        //Size: The font size. If Size=-1, the default of 16 is used.
        //Bold: If set to 'true', a bold font is used
        //Slant: If set to 'true', a slanted font is used
        //
        Op.GetSystem("operating_system", out hv_OS);
        if (Convert.ToInt32(Tuple.TupleOr(Tuple.TupleEqual(hv_Size, null), Tuple.TupleEqual(
                                              hv_Size, -1))) != 0)
        {
            hv_Size = 16;
        }
        if (Convert.ToInt32(Tuple.TupleEqual(Tuple.TupleStrLastN(Tuple.TupleStrFirstN(
                                                                     hv_OS, 2), 0), "Win")) != 0)
        {
            //set font on Windows systems
            if (Convert.ToInt32(Tuple.TupleOr(Tuple.TupleOr(Tuple.TupleEqual(hv_Font, "mono"),
                                                            Tuple.TupleEqual(hv_Font, "Courier")), Tuple.TupleEqual(hv_Font, "courier"))) != 0)
            {
                hv_Font = "Courier New";
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Font, "sans")) != 0)
            {
                hv_Font = "Arial";
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Font, "serif")) != 0)
            {
                hv_Font = "Times New Roman";
            }
            if (Convert.ToInt32(Tuple.TupleEqual(hv_Bold, "true")) != 0)
            {
                hv_Bold = 1;
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Bold, "false")) != 0)
            {
                hv_Bold = 0;
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HException(hv_Exception);
            }
            if (Convert.ToInt32(Tuple.TupleEqual(hv_Slant, "true")) != 0)
            {
                hv_Slant = 1;
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Slant, "false")) != 0)
            {
                hv_Slant = 0;
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HException(hv_Exception);
            }
            try
            {
                Op.SetFont(hv_WindowHandle, Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(
                                                                              Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(
                                                                                                                                              "-", hv_Font), "-"), hv_Size), "-*-"), hv_Slant), "-*-*-"), hv_Bold), "-"));
            }
            // catch (Exception)
            catch (COMException HDevExpDefaultException1)
            {
                ExceptionToTuple(HDevExpDefaultException1, (int)HDevExpDefaultException1.ErrorCode - sys.ErrorBaseHalcon, out hv_Exception);
                throw new HException(hv_Exception);
            }
        }
        else
        {
            //set font for UNIX systems
            hv_Size             = Tuple.TupleMult(hv_Size, 1.25);
            hv_AllowedFontSizes = Tuple.TupleConcat(Tuple.TupleConcat(Tuple.TupleConcat(
                                                                          Tuple.TupleConcat(Tuple.TupleConcat(11, 14), 17), 20), 25), 34);
            if (Convert.ToInt32(Tuple.TupleEqual(Tuple.TupleFind(hv_AllowedFontSizes, hv_Size),
                                                 -1)) != 0)
            {
                hv_Distances = Tuple.TupleAbs(Tuple.TupleSub(hv_AllowedFontSizes, hv_Size));
                Op.TupleSortIndex(hv_Distances, out hv_Indices);
                hv_Size = Tuple.TupleSelect(hv_AllowedFontSizes, Tuple.TupleSelect(hv_Indices,
                                                                                   0));
            }
            if (Convert.ToInt32(Tuple.TupleOr(Tuple.TupleEqual(hv_Font, "mono"), Tuple.TupleEqual(
                                                  hv_Font, "Courier"))) != 0)
            {
                hv_Font = "courier";
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Font, "sans")) != 0)
            {
                hv_Font = "helvetica";
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Font, "serif")) != 0)
            {
                hv_Font = "times";
            }
            if (Convert.ToInt32(Tuple.TupleEqual(hv_Bold, "true")) != 0)
            {
                hv_Bold = "bold";
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Bold, "false")) != 0)
            {
                hv_Bold = "medium";
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HException(hv_Exception);
            }
            if (Convert.ToInt32(Tuple.TupleEqual(hv_Slant, "true")) != 0)
            {
                if (Convert.ToInt32(Tuple.TupleEqual(hv_Font, "times")) != 0)
                {
                    hv_Slant = "i";
                }
                else
                {
                    hv_Slant = "o";
                }
            }
            else if (Convert.ToInt32(Tuple.TupleEqual(hv_Slant, "false")) != 0)
            {
                hv_Slant = "r";
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HException(hv_Exception);
            }
            try
            {
                Op.SetFont(hv_WindowHandle, Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(
                                                                              Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(Tuple.TupleAdd(
                                                                                                                                              "-adobe-", hv_Font), "-"), hv_Bold), "-"), hv_Slant), "-normal-*-"), hv_Size),
                                                           "-*-*-*-*-*-*-*"));
            }
            // catch (Exception)
            catch (COMException HDevExpDefaultException1)
            {
                ExceptionToTuple(HDevExpDefaultException1, (int)HDevExpDefaultException1.ErrorCode - sys.ErrorBaseHalcon, out hv_Exception);
                throw new HException(hv_Exception);
            }
        }

        return;
    }