BIDI_L_Rules.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:19k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *    bidi_l_rules.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui_Software
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *   bidirectional algorithm engine 
  48.  *
  49.  *
  50.  * Author:
  51.  * -------
  52.  * -------
  53.  *
  54.  *============================================================================
  55.  *             HISTORY
  56.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  57.  *------------------------------------------------------------------------------
  58.  * removed!
  59.  *
  60.  *------------------------------------------------------------------------------
  61.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  62.  *============================================================================
  63.  *****************************************************************************/
  64. /*
  65.  * Copyright Notice
  66.  * ?2002 - 2003, Pixtel Communications, Inc., 1489 43rd Ave. W.,
  67.  * Vancouver, B.C. V6M 4K8 Canada. All Rights Reserved.
  68.  *  (It is illegal to remove this copyright notice from this software or any
  69.  *  portion of it)
  70.  */
  71. /**************************************************************
  72.    FILENAME : BIDI_L_Rules.c
  73.    PURPOSE     : Main functions of Rule L1 to L4 of BIDI Algorithm
  74.    REMARKS     : nil
  75.    AUTHOR      : Yogesh
  76.    DATE     : Aug-23-2004
  77. **************************************************************/
  78. #include "BidiDef.h"
  79. #include "BidiProt.h"
  80. #ifdef __BIDI_CONSOLE__
  81. #include "CommonProts.h"
  82. #else 
  83. #include "Unicodexdcl.h"
  84. #endif 
  85. /*****************************************************************************
  86.  * FUNCTION
  87.  *  resolve_lines
  88.  * DESCRIPTION
  89.  *  
  90.  * PARAMETERS
  91.  *  paragraphEmbeddingLevel     [IN]        
  92.  *  inputString                 [?]         
  93.  *  paraCount                   [IN]        
  94.  *  mirrorFlag                  [IN]        
  95.  *  pInput_Types                [?]         
  96.  *  pInput_Levels               [?]         
  97.  * RETURNS
  98.  *  void
  99.  *****************************************************************************/
  100. void resolve_lines(
  101.         U8 paragraphEmbeddingLevel,
  102.         U8 *inputString,
  103.         U16 paraCount,
  104.         pBOOL mirrorFlag,
  105.         U8 *pInput_Types,
  106.         U8 *pInput_Levels)
  107. {
  108.     /*----------------------------------------------------------------*/
  109.     /* Local Variables                                                */
  110.     /*----------------------------------------------------------------*/
  111.     U16 lineCount = 0;
  112.     U16 *pBreak = 0;
  113.     /*----------------------------------------------------------------*/
  114.     /* Code Body                                                      */
  115.     /*----------------------------------------------------------------*/
  116.     do
  117.     {
  118.         /* break lines at LS */
  119.         lineCount = bidi_resolve_lines(inputString, paraCount, pBreak);
  120.         /* resolve whitespace */
  121.         bidi_resolve_white_space(paragraphEmbeddingLevel, pInput_Types, pInput_Levels, lineCount);
  122.         if (mirrorFlag)
  123.         {
  124.             get_mirror(inputString, pInput_Levels, lineCount);
  125.         }
  126.         /* reorder each line in place */
  127.         bidi_reorder(paragraphEmbeddingLevel, inputString, pInput_Levels, lineCount);
  128.         //Changed by Yogesh 20041224
  129.         //              inputString += lineCount;
  130.         inputString += lineCount * BIDI_ENCODING_LENGTH;
  131.         pInput_Levels += lineCount;
  132.         pBreak += pBreak ? lineCount : 0;
  133.         pInput_Types += lineCount;
  134.         paraCount -= lineCount;
  135.     } while (paraCount);
  136. }
  137. /*****************************************************************************
  138.  * FUNCTION
  139.  *  bidi_resolve_white_space
  140.  * DESCRIPTION
  141.  *  
  142.  * PARAMETERS
  143.  *  base_level          [IN]        
  144.  *  pInput_Types        [IN]        
  145.  *  pInput_Level        [?]         
  146.  *  lineCount           [IN]        
  147.  * RETURNS
  148.  *  void
  149.  *****************************************************************************/
  150. void bidi_resolve_white_space(U8 base_level, const U8 *pInput_Types, U8 *pInput_Level, U16 lineCount)
  151. {
  152.     /*----------------------------------------------------------------*/
  153.     /* Local Variables                                                */
  154.     /*----------------------------------------------------------------*/
  155.     U8 cur_level = 0, old_level = base_level;
  156.     S16 iCount;
  157.     /*----------------------------------------------------------------*/
  158.     /* Code Body                                                      */
  159.     /*----------------------------------------------------------------*/
  160.     for (iCount = 0; iCount < lineCount; iCount++)
  161.     {
  162.         switch (pInput_Types[iCount])
  163.         {
  164.             case WS:
  165.                 //START TARUN 20041204
  166.                 //cur_level++;
  167.                 //break;
  168.                 //END TARUN 20041204
  169.             case RLE:
  170.             case LRE:
  171.             case LRO:
  172.             case RLO:
  173.             case PDF:
  174.             case BN:
  175.                 pInput_Level[iCount] = old_level;
  176.                 cur_level++;
  177.                 break;
  178.             case S:
  179.             case BIDI_B:
  180.                 bidi_set_level_run(pInput_Level, cur_level, iCount, base_level);
  181.                 cur_level = 0;
  182.                 pInput_Level[iCount] = base_level;
  183.                 break;
  184.             default:
  185.                 cur_level = 0;
  186.                 break;
  187.         }
  188.         old_level = pInput_Level[iCount];
  189.     }
  190.     bidi_set_level_run(pInput_Level, cur_level, iCount, base_level);
  191. }
  192. /*****************************************************************************
  193.  * FUNCTION
  194.  *  bidi_set_level_run
  195.  * DESCRIPTION
  196.  *  
  197.  * PARAMETERS
  198.  *  pinput_level        [?]         
  199.  *  cur_level           [IN]        
  200.  *  iStart              [IN]        
  201.  *  base_level          [IN]        
  202.  * RETURNS
  203.  *  void
  204.  *****************************************************************************/
  205. void bidi_set_level_run(U8 *pinput_level, U8 cur_level, U16 iStart, U8 base_level)
  206. {
  207.     /*----------------------------------------------------------------*/
  208.     /* Local Variables                                                */
  209.     /*----------------------------------------------------------------*/
  210.     S16 iCount;
  211.     /*----------------------------------------------------------------*/
  212.     /* Code Body                                                      */
  213.     /*----------------------------------------------------------------*/
  214.     for (iCount = iStart - 1; iCount >= iStart - cur_level; iCount--)
  215.     {
  216.         pinput_level[iCount] = base_level;
  217.     }
  218. }
  219. /*****************************************************************************
  220.  * FUNCTION
  221.  *  bidi_resolve_lines
  222.  * DESCRIPTION
  223.  *  
  224.  * PARAMETERS
  225.  *  pinput      [?]         
  226.  *  cCount      [IN]        
  227.  *  pBreak      [?]         
  228.  * RETURNS
  229.  *  
  230.  *****************************************************************************/
  231. U16 bidi_resolve_lines(U8 *pinput, U16 cCount, U16 *pBreak)
  232. {
  233.     /*----------------------------------------------------------------*/
  234.     /* Local Variables                                                */
  235.     /*----------------------------------------------------------------*/
  236.     S16 iCount;
  237.     /*----------------------------------------------------------------*/
  238.     /* Code Body                                                      */
  239.     /*----------------------------------------------------------------*/
  240.     /* skip characters not of type LS */
  241.     for (iCount = 0; iCount < (cCount); iCount++)
  242.     {
  243.         //START TARUN PMT 20041214
  244.         //START Changed by Yogesh 20041224
  245.         //              if ((pinput[iCount] == LS)||(pBreak && pBreak[iCount])||(pinput[iCount] == NL) )
  246.         if (((pinput[iCount * BIDI_ENCODING_LENGTH] == LS) && (pinput[(iCount * BIDI_ENCODING_LENGTH) + 1] == 0)) ||
  247.             (pBreak && pBreak[iCount]) || ((pinput[iCount * BIDI_ENCODING_LENGTH] == NL) &&
  248.                                            (pinput[(iCount * BIDI_ENCODING_LENGTH) + 1] == 0)))
  249.             //END Changed by Yogesh 20041224
  250.             //END TARUN PMT 20041214
  251.         {
  252.             iCount++;
  253.             break;
  254.         }
  255.     }
  256.     return iCount;
  257. }
  258. /*****************************************************************************
  259.  * FUNCTION
  260.  *  get_mirror
  261.  * DESCRIPTION
  262.  *  
  263.  * PARAMETERS
  264.  *  inputString         [?]         
  265.  *  input_levels        [?]         
  266.  *  lineCount           [IN]        
  267.  * RETURNS
  268.  *  void
  269.  *****************************************************************************/
  270. void get_mirror(U8 *inputString, U8 *input_levels, U16 lineCount)
  271. {
  272.     /*----------------------------------------------------------------*/
  273.     /* Local Variables                                                */
  274.     /*----------------------------------------------------------------*/
  275.     S16 iCount;
  276.     /*----------------------------------------------------------------*/
  277.     /* Code Body                                                      */
  278.     /*----------------------------------------------------------------*/
  279.     for (iCount = 0; iCount < lineCount; iCount++)
  280.     {
  281.         if (!ODD(input_levels[iCount]))
  282.         {
  283.             continue;
  284.         }
  285.         get_mirror_char(&inputString[iCount * BIDI_ENCODING_LENGTH]);
  286.     }
  287. }
  288. /*****************************************************************************
  289.  * FUNCTION
  290.  *  get_mirror_char
  291.  * DESCRIPTION
  292.  *  
  293.  * PARAMETERS
  294.  *  pinput      [?]     
  295.  * RETURNS
  296.  *  
  297.  *****************************************************************************/
  298. pBOOL get_mirror_char(U8 *pinput)
  299. {
  300.     /*----------------------------------------------------------------*/
  301.     /* Local Variables                                                */
  302.     /*----------------------------------------------------------------*/
  303.     U16 charInput = 0;
  304.     S32 nFirst = 0;
  305.     S32 nLast = nMaxIndexBidiMirror;
  306.     S32 nMid;
  307. #ifdef __UCS2_ENCODING
  308.     U8 charLen;
  309. #endif 
  310.     /*----------------------------------------------------------------*/
  311.     /* Code Body                                                      */
  312.     /*----------------------------------------------------------------*/
  313. #ifdef __ASCII
  314.     charInput = (U16) (*pinput);
  315. #endif 
  316. #ifdef __UCS2_ENCODING
  317.     pfnEncodingSchemeToUnicode((U16*) & charInput, pinput);
  318. #endif 
  319.     while (nLast >= nFirst)
  320.     {
  321.         nMid = (nFirst + nLast) / 2;
  322.         if (bidiCharMirror[nMid].bidiChar == charInput)
  323.         {
  324.         #ifdef __ASCII
  325.             *pinput = (U8) (bidiCharMirror[nMid].bidiMirror);
  326.         #endif 
  327.         #ifdef __UCS2_ENCODING
  328.             pfnUnicodeToEncodingScheme(bidiCharMirror[nMid].bidiMirror, &charLen, pinput);
  329.         #endif 
  330.             return TRUE;
  331.         }
  332.         else if (bidiCharMirror[nMid].bidiMirror == charInput)
  333.         {
  334.         #ifdef __ASCII
  335.             *pinput = (U8) (bidiCharMirror[nMid].bidiChar);
  336.         #endif 
  337.         #ifdef __UCS2_ENCODING
  338.             pfnUnicodeToEncodingScheme(bidiCharMirror[nMid].bidiChar, &charLen, pinput);
  339.         #endif 
  340.             return TRUE;
  341.         }
  342.         else
  343.         {
  344.             //START YOGESH 20041122        
  345.             //if(bidiCharMirror[nMid].bidiChar>charInput)
  346.             if (bidiCharMirror[nMid].bidiChar < charInput)
  347.                 /* END YOGESH 20041122 */
  348.             {
  349.                 nFirst = nMid + 1;
  350.             }
  351.             else
  352.             {
  353.                 nLast = nMid - 1;
  354.             }
  355.         }
  356.     }
  357.     return FALSE;
  358. }
  359. /*****************************************************************************
  360.  * FUNCTION
  361.  *  bidi_reorder
  362.  * DESCRIPTION
  363.  *  
  364.  * PARAMETERS
  365.  *  base_level      [IN]        
  366.  *  pinput          [?]         
  367.  *  input_level     [?]         
  368.  *  cCount          [IN]        
  369.  * RETURNS
  370.  *  
  371.  *****************************************************************************/
  372. U16 bidi_reorder(U8 base_level, U8 *pinput, U8 *input_level, U16 cCount)
  373. {
  374.     /*----------------------------------------------------------------*/
  375.     /* Local Variables                                                */
  376.     /*----------------------------------------------------------------*/
  377.     U16 iCount = 0;
  378.     /*----------------------------------------------------------------*/
  379.     /* Code Body                                                      */
  380.     /*----------------------------------------------------------------*/
  381.     while (iCount < cCount)
  382.     {
  383.         /* By Yogesh on 20040904 */
  384.         iCount += bidi_reorder_level(
  385.                     base_level,
  386.                     pinput + iCount * BIDI_ENCODING_LENGTH,
  387.                     input_level + iCount,
  388.                     (U16) (cCount - iCount),
  389.                     FALSE);
  390.     }
  391.     return iCount;
  392. }
  393. /*****************************************************************************
  394.  * FUNCTION
  395.  *  bidi_reorder_level
  396.  * DESCRIPTION
  397.  *  
  398.  * PARAMETERS
  399.  *  level               [IN]        
  400.  *  pinput              [?]         
  401.  *  input_level         [?]         
  402.  *  cCount              [IN]        
  403.  *  reverse_flag        [IN]        
  404.  * RETURNS
  405.  *  
  406.  *****************************************************************************/
  407. U16 bidi_reorder_level(U8 level, U8 *pinput, U8 *input_level, U16 cCount, pBOOL reverse_flag)
  408. {
  409.     /*----------------------------------------------------------------*/
  410.     /* Local Variables                                                */
  411.     /*----------------------------------------------------------------*/
  412.     U16 iCount;
  413.     /*----------------------------------------------------------------*/
  414.     /* Code Body                                                      */
  415.     /*----------------------------------------------------------------*/
  416.     reverse_flag = reverse_flag || ODD(level);
  417.     for (iCount = 0; iCount < cCount; iCount++)
  418.     {
  419.         if (input_level[iCount] < level)
  420.         {
  421.             break;
  422.         }
  423.         else if (input_level[iCount] > level)
  424.         {
  425.             /* By Yogesh on 20040904 */
  426.             iCount += bidi_reorder_level(
  427.                         (U8) (level + 1),
  428.                         pinput + iCount * BIDI_ENCODING_LENGTH,
  429.                         input_level + iCount,
  430.                         (U16) (cCount - iCount),
  431.                         reverse_flag) - 1;
  432.         }
  433.     }
  434.     //START Changed by Yogesh 20041224
  435.     //    if (reverse_flag)
  436.     if (reverse_flag && iCount)
  437.         /* END Changed by Yogesh 20041224 */
  438.     {
  439.         bidi_reverse(pinput, iCount);
  440.     }
  441.     /* START Changed by Yogesh 20041224 */
  442.     if (!iCount)
  443.     {
  444.         iCount = 1;
  445.     }
  446.     /* END Changed by Yogesh 20041224 */
  447.     return iCount;
  448. }
  449. /*****************************************************************************
  450.  * FUNCTION
  451.  *  bidi_reverse
  452.  * DESCRIPTION
  453.  *  
  454.  * PARAMETERS
  455.  *  pinput      [?]         
  456.  *  cCount      [IN]        
  457.  * RETURNS
  458.  *  void
  459.  *****************************************************************************/
  460. void bidi_reverse(U8 *pinput, U16 cCount)
  461. {
  462.     /*----------------------------------------------------------------*/
  463.     /* Local Variables                                                */
  464.     /*----------------------------------------------------------------*/
  465.     U8 cTemp;
  466.     /* START Changed by Yogesh 20041224 : Function body changed */
  467.     S16 iCount = 0, jCount = 0, kCount = 0;
  468.     pBOOL bFlag;
  469.     /*----------------------------------------------------------------*/
  470.     /* Code Body                                                      */
  471.     /*----------------------------------------------------------------*/
  472.     while (iCount < cCount)
  473.     {
  474.         bFlag = FALSE;
  475.         for (jCount = iCount; jCount < cCount; jCount++)
  476.         {
  477.             if (pinput[jCount * BIDI_ENCODING_LENGTH] == 0x0a && pinput[jCount * BIDI_ENCODING_LENGTH + 1] == 0x00)
  478.             {
  479.                 /* jCount--; */
  480.                 bFlag = TRUE;
  481.                 break;
  482.             }
  483.         }
  484.         for (kCount = iCount; kCount < (iCount + (jCount - iCount) / 2); kCount++)
  485.         {
  486.         #ifdef __ASCII
  487.             cTemp = pinput[kCount];
  488.             pinput[kCount] = pinput[jCount - 1 - kCount];
  489.             pinput[jCount - 1 - kCount] = cTemp;
  490.         #endif /* __ASCII */ 
  491.         #ifdef __UCS2_ENCODING
  492.             cTemp = pinput[kCount * BIDI_ENCODING_LENGTH];
  493.             pinput[kCount * BIDI_ENCODING_LENGTH] = pinput[(jCount + iCount - 1 - kCount) * BIDI_ENCODING_LENGTH];
  494.             pinput[(jCount + iCount - 1 - kCount) * BIDI_ENCODING_LENGTH] = cTemp;
  495.             cTemp = pinput[kCount * BIDI_ENCODING_LENGTH + 1];
  496.             pinput[kCount * BIDI_ENCODING_LENGTH + 1] =
  497.                 pinput[(jCount + iCount - 1 - kCount) * BIDI_ENCODING_LENGTH + 1];
  498.             pinput[(jCount + iCount - 1 - kCount) * BIDI_ENCODING_LENGTH + 1] = cTemp;
  499.         #endif /* __UCS2_ENCODING */ 
  500.         }
  501.         if (bFlag)
  502.         {
  503.             jCount++;
  504.         }
  505.         iCount = jCount;
  506.     }
  507.     /* END Changed by Yogesh 20041224 : Function body changed */
  508. }