home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / c / samples / bidi / shape.c__ / shape.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  12.8 KB  |  390 lines

  1. /*static char *SCCSID = "@(#)shape.c    6.1 92/02/19";*/
  2. /*************************************************************************
  3.  * File Name     : SHAPE.C
  4.  *
  5.  * Description   : This sample program demonstrates how to use the
  6.  *                 Bidirectional (Bidi) API NlsShapeBidiString.
  7.  *                 This an Arabic specific API and is used to Shape a
  8.  *                 string according to the value of the CSD byte in
  9.  *                 the Bidi attributes given to the API.
  10.  *
  11.  * Concepts      : The orientation byte in the Bidi attributes should
  12.  *                 describe the way the input string was logically sotred,
  13.  *                 i.e RTL or LTR. This is mandatory because the Character
  14.  *                 Shape Determination is dependant on the string
  15.  *                 orientation. If a wrong orientation was specified, a
  16.  *                 wrong output is expected
  17.  *
  18.  * API's         : NlsSetBidiAtt
  19.  *                 NlsQueryBidiAtt
  20.  *                 NlsShapeBidiString
  21.  *                 VioWrtCharStr
  22.  *                 VioScrollUp
  23.  *                 VioSetCurType
  24.  *
  25.  * Required files: To build and run this sample code the following files
  26.  *                 are needed:
  27.  *                 - SHAPE.C
  28.  *                 - SHAPE.DEF
  29.  *                 - SHAPE.MAK
  30.  *                 - SHAPE.LNK
  31.  *
  32.  *  Copyright (C) 1991 IBM Corporation
  33.  *
  34.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  35.  *      sample code created by IBM Corporation. This sample code is not
  36.  *      part of any standard or IBM product and is provided to you solely
  37.  *      for  the purpose of assisting you in the development of your
  38.  *      applications.  The code is provided "AS IS", without
  39.  *      warranty of any kind.  IBM shall not be liable for any damages
  40.  *      arising out of your use of the sample code, even if they have been
  41.  *      advised of the possibility of such damages.                                                    *
  42.  ************************************************************************/
  43. #define INCL_BASE
  44. /* To include the neccessary header files for Bidi */
  45. #define INCL_DOSERRORS
  46. #define INCL_BDCALLS
  47.  
  48. #include <string.h>
  49. #include <os2.h>
  50. #include <conio.h>
  51.  
  52. VOID Initialize(VOID);
  53. VOID ShapeAndDisplay(VOID);
  54. VOID SetCur(INT);
  55.  
  56. /* Define a variable to save the session Bidi Attributes in it */
  57. ULONG ulOriginalBidiAttr;
  58.  
  59. /* Define the area where we will get the result
  60.  * of what we want to query from the session   */
  61. BDKVCB bdkvcbSetAttr;
  62.  
  63. /* Define the parameter to Hide and Restore the cursor */
  64. #define HIDE   -1
  65. #define RESTORE 0
  66.  
  67. /* Define the buffer for the string to be displayed */
  68. CHAR cString[50];
  69.  
  70. /*************************************************************************
  71.  * Name          : Main
  72.  *
  73.  * Description   : This is the main part of the program, it first clear
  74.  *                 the screen and then hide the cursor. The string to be
  75.  *                 shaped will be displayed before and after the API
  76.  *                 NlsShapeBidiString is called, with different modes of
  77.  *                 shaping.
  78.  *
  79.  * API's         : NlsSetBidiAtt
  80.  *                 VioScrollUp
  81.  *
  82.  * Parameters    : This program does not take any command line parameters
  83.  *
  84.  * Returns       : None
  85.  *
  86.  ************************************************************************/
  87.  
  88. INT main(SHORT sArgc,CHAR **ppArgv)
  89. {
  90.    APIRET RC;
  91.  
  92.    /* clear the screen  */
  93.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  94.  
  95.    /* hide the cursor */
  96.    SetCur(HIDE);
  97.  
  98.    /* Write the static text to the screen and the string that
  99.     * is needed to be shaped */
  100.    Initialize();
  101.  
  102.    /* Call the API to shape the string and wirte it to the screen */
  103.    ShapeAndDisplay();
  104.  
  105.    /* Wait for Enter key to end the program */
  106.  
  107.    VioWrtCharStr("Press Enter to end the program", 32, 23, 28, 0l);
  108.    getchar();
  109.  
  110.    /* Restore the session Bidi attributes  */
  111.    bdkvcbSetAttr.BDAtts = ulOriginalBidiAttr ;
  112.  
  113.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  114.    if( RC != 0)
  115.    {
  116.      printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  117.      exit(1);
  118.    }
  119.  
  120.    /* clear the screen before exit */
  121.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  122.  
  123.    /* restore the cursor position */
  124.    SetCur(RESTORE);
  125.  
  126.    return (0);
  127. }
  128. /*************************************************************************
  129.  * Name          : INITIALIZE
  130.  *
  131.  * Description   : This function does save the current Bidi attributes of
  132.  *                 the session in a global variable, and set session with
  133.  *                 its own Bidi attributes to protect the displayed shaped
  134.  *                 string from been processed again according to the session
  135.  *                 Bidi attributes.
  136.  *                 Then the function displays some static text and also the
  137.  *                 String to be shaped before any shaping processing.
  138.  *
  139.  * API's         : NlsQueryBidiAtt
  140.  *                 NlsSetBidiAtt
  141.  *                 VioWrtCharStr
  142.  *
  143.  * Parameters    : This program does not take any command line parameters
  144.  *
  145.  * Returns       : None
  146.  *
  147.  ************************************************************************/
  148. VOID Initialize(VOID)
  149. {
  150.    INT iI, iRow, iCol;
  151.  
  152.    APIRET  RC;
  153.  
  154.    /* define structures to be array of strings to contain the static
  155.     * text to be wirtten on the screen  */
  156.    struct {
  157.           CHAR cScreen[40];
  158.           }mssg1[4];
  159.  
  160.    struct {
  161.            CHAR cScreen[12];
  162.            }mssg2[8];
  163.  
  164.    /* First we need to set the session Bidi attributes to have the
  165.     * shaping mode as Passthru. This will let the Bidi support
  166.     * to display the characters on the screen as they are.
  167.     * i.e without any additional shape processing.
  168.     * This way, when the application does its own shaping processing
  169.     * the output characters to the screen will not be corrupted. */
  170.  
  171.    /* The length here is 8 to query only on the Bidi attribute   */
  172.  
  173.    bdkvcbSetAttr.BDLength = 8;
  174.  
  175.    RC = NlsQueryBidiAtt( 0L,(PRETINFO)&bdkvcbSetAttr);
  176.  
  177.    if(RC != 0)
  178.    {
  179.       printf("\n\nError in query the Bidi Attributes RC=%ld\n",RC);
  180.       exit(1);
  181.    }
  182.  
  183.    /* Now we save the initial value of the Bidi attributes so that
  184.     * so that we set back the session with it before the program terminates*/
  185.  
  186.    ulOriginalBidiAttr = bdkvcbSetAttr.BDAtts;
  187.  
  188.   /* Here we set the shaping byte value to passthru */
  189.  
  190.    bdkvcbSetAttr.BDAtts = BD_LEVEL |
  191.                           BD_SUPPORT |
  192.                           BDORIENT_LTR |
  193.                           BDNUM_PASSTHRU |
  194.                           BDCSD_PASSTHRU;
  195.  
  196.   /* Now let's set the session Bidi attributes with the new value  */
  197.  
  198.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  199.    if( RC != 0)
  200.    {
  201.        printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  202.        exit(1);
  203.    }
  204.  
  205.    /* Initialize the structures with the characters to be displayed */
  206.    strcpy(mssg1[0].cScreen, "Calling NlsShapeBidiString you can");
  207.    strcpy(mssg1[1].cScreen, "  Change the following String to  ");
  208.    strcpy(mssg1[2].cScreen, "    any of the different Arabic   ");
  209.    strcpy(mssg1[3].cScreen, "             Shapes               ");
  210.  
  211.    /* In this for loop, we increment the iRow after the display of each
  212.     * string in the structure to jump to another line, keeping the value
  213.     * of the iCol value fixed to have the strings indented */
  214.    for(iI = 0, iRow = 2, iCol = 23; iI < 4; iI++, iRow++)
  215.  
  216.    VioWrtCharStr(mssg1[iI].cScreen, strlen(mssg1[iI].cScreen), iRow, iCol, 0l);
  217.  
  218.  
  219.    /* Initialize the second structure with the characters to be displayed */
  220.    strcpy(mssg2[0].cScreen, "String    :");
  221.    strcpy(mssg2[1].cScreen, "Base      :");
  222.    strcpy(mssg2[2].cScreen, "Initial   :");
  223.    strcpy(mssg2[3].cScreen, "Middle    :");
  224.    strcpy(mssg2[4].cScreen, "Final     :");
  225.    strcpy(mssg2[5].cScreen, "Isolated  :");
  226.    strcpy(mssg2[6].cScreen, "Shaped LTR:");
  227.    strcpy(mssg2[7].cScreen, "Shaped RTL:");
  228.  
  229.  
  230.    VioWrtCharStr(mssg2[0].cScreen, strlen(mssg2[0].cScreen), 8, 5, 0l);
  231.  
  232.    for(iI = 1, iRow = 11, iCol = 5; iI <= 7; iI++, iRow++)
  233.  
  234.    VioWrtCharStr(mssg2[iI].cScreen, strlen(mssg2[iI].cScreen), iRow, iCol, 0l);
  235.  
  236.    /* Display the string before it is shaped.
  237.     * Note that this is a Left To Right string, because the logical
  238.     * order of the characters stored is from the end of the buffer
  239.     * to the begining, i.e from high to low memory */
  240.  
  241.    strcpy(cString,"╔ß√¬»∩Σ╟ ╔Ω⌐╤┘Σ╟ ║Φ╤«Σ╟ Σ╟ⁿ╘├ σ╧╬╩╝Ω ¡∩╟≥╤╚Σ Σ¿╦∩");
  242.    VioWrtCharStr(cString, strlen(cString), 8, 18, 0l);
  243. }
  244. /*************************************************************************
  245.  * Name          : SHAPEANDDISPLAY
  246.  *
  247.  * Description   : This function will show how to use the Bidi API
  248.  *                 NslShapeBidiString, and what are the different shaping
  249.  *                 modes available and how to pass them to the API.
  250.  *                 The function will display the string after each call to
  251.  *                 NslShapeBidiString, according to different shaping
  252.  *                 attributes.
  253.  *
  254.  * API's         : NlsShapeBidiString
  255.  *                 VioWrtCharStr
  256.  *
  257.  * Parameters    : This program does not take any command line parameters
  258.  *
  259.  * Returns       : None
  260.  *
  261.  ************************************************************************/
  262. VOID ShapeAndDisplay(VOID)
  263. {
  264.    APIRET RC;
  265.    INT   iRow, iCol;
  266.    ULONG ulBidiAttr,ulBidiAttrIso;
  267.    ULONG ulEffect ;
  268.    CHAR  cTarget[50];
  269.    ULONG ulLength;
  270.    ULONG ulIncrement ;
  271.  
  272.    /* The Effect paramter is reserved to be zero */
  273.    ulEffect = 0;
  274.  
  275.    ulLength = strlen(cString);
  276.  
  277.    /* The string contains successive characters, i.e the cell here is
  278.     * only one character, i.e Increment =1 */
  279.    ulIncrement = 1;
  280.  
  281.    iCol = 18;
  282.  
  283.    /* using the OR operator, we set the bits in the Bidi attributes
  284.     * to describe the orientation of the buffer and the shaping mode
  285.     * that we want as output */
  286.  
  287.    ulBidiAttr= BD_LEVEL |
  288.                BD_SUPPORT |
  289.                BDORIENT_LTR |
  290.                BDNUM_PASSTHRU |
  291.                BDCSD_BASE ;
  292.  
  293.     /* In this for loop, we depend on the fact that if we increment
  294.      * the Bidi attributes by 1, starting from the value of the
  295.      * BASE shapes, we get another shaping attribute, until we reach
  296.      * the ISOLATED shapes mode, which is the value in ulBidiAttrIso.
  297.      * We also increment the iRow value by one, to get a new line to
  298.      * display each mode  */
  299.  
  300.    ulBidiAttrIso = ulBidiAttr | BDCSD_ISOLATED;
  301.  
  302.    for(iRow=11; ulBidiAttr <= ulBidiAttrIso ; ulBidiAttr++, iRow++)
  303.  
  304.    {
  305.        RC=NlsShapeBidiString(ulBidiAttr,ulEffect, cString,
  306.                              cTarget, ulLength, ulIncrement);
  307.  
  308.        /* If there is an error returned */
  309.        if(RC != 0)
  310.        {
  311.        printf ("RC1 =%ld\n",RC);
  312.            exit(1);
  313.        }
  314.  
  315.        /* terminate the string with a NULL */
  316.        cTarget[strlen(cString)] = '\0';
  317.  
  318.        /* display it on the screen */
  319.        VioWrtCharStr(cTarget, strlen(cTarget), iRow, iCol, 0l);
  320.     }
  321.  
  322.    /* Now let's put the shaping mode to be Automatic character determination
  323.     * (shaping) ON, keeping the orientation to be Left To Right. */
  324.  
  325.    ulBidiAttr= BD_LEVEL |
  326.                BD_SUPPORT |
  327.                BDORIENT_LTR |
  328.                BDNUM_PASSTHRU |
  329.                BDCSD_AUTOMATIC;
  330.  
  331.     RC=NlsShapeBidiString(ulBidiAttr ,ulEffect,cString,
  332.                           cTarget, ulLength, ulIncrement);
  333.  
  334.     if(RC != 0)
  335.     {
  336.        printf ("RC2 =%ld\n",RC);
  337.        exit(1);
  338.     }
  339.  
  340.     cTarget[strlen(cTarget)] = '\0';
  341.     VioWrtCharStr(cTarget, strlen(cTarget), iRow, iCol, 0l);
  342.  
  343.    /* Just for the sake of demonstration, let's intentionally set the
  344.     * the orientation bit to Right To Left, which is the wrong orientation
  345.     * of the original string, keeping the shaping mode unchanged */
  346.  
  347.    ulBidiAttr= ulBidiAttr | BDORIENT_RTL;
  348.  
  349.     RC=NlsShapeBidiString(ulBidiAttr ,ulEffect,cString,
  350.                           cTarget, ulLength, ulIncrement);
  351.  
  352.     if(RC != 0)
  353.     {
  354.        printf ("RC3 =%ld\n",RC);
  355.        exit(1);
  356.     }
  357.     cTarget[strlen(cTarget)] = '\0';
  358.  
  359.     VioWrtCharStr(cTarget, strlen(cTarget), iRow+1, iCol, 0l);
  360. }
  361.  
  362. /*************************************************************************
  363.  * Name          : SET_CUR
  364.  *
  365.  * Description   : This function hides or restores the cursor position.
  366.  *
  367.  * API's         : VioSetCurType
  368.  *
  369.  * Parameters    : (INT flag)
  370.  *                 flag indicates whether we want ot hide or restore
  371.  *                 the cursor.
  372.  *
  373.  * Returns       : None
  374.  *
  375.  ************************************************************************/
  376.  
  377. VOID SetCur(INT iFlag)
  378. {
  379.    HVIO HVIOhandle = 0;
  380.    VIOCURSORINFO cursor;
  381.  
  382.    cursor.yStart  = 3;
  383.    cursor.cEnd    = 5;
  384.    cursor.cx      = 0;
  385.    cursor.attr    = iFlag;
  386.  
  387.    VioSetCurType(&cursor, HVIOhandle);
  388. }
  389.  
  390.