手写Symbian文字显示排版代码

 由于老大又给我下了任务 时间很紧 又想记录下自己写的第一个真正的Symbian代码 希望能帮到大家 程序还是很弱 也不健壮 海涵

 

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

void CMyTextShowAppView::Draw ( const TRect & aRect) const

    {

    TBuf16 <800> iRealText ;                // 测试内容

    TBuf16 <400> iShowText;                 // 显示当前内容

    iRealText = _L ( "ARMV5 builds give the best performance and smallest code compared to "

           "other alternatives, and are typically used primarily for ROM builds. "

           "The compiler is not distributed as part of the SDK: it can be obtained "

           "as part of the RealView Developer Suite 2.2 from ARM (http://www.arm.com/)." );

    iRealText. Append ( ' ' );                 // 格式需要,通过空格来结束

    _LIT ( KFontName, "Sans MT 936_S60" );   // 定义 Font 的名称

    TInt KTextHight = 150;                 // 定义字体的大小

    TInt KLineHight = 20;                  // 配合上面的定义大小

    TInt KLineWidth = aRect. Width ();       // 获得当前设备屏幕的宽

    TInt KSpaceWidth = 4;                  // 当前设备空格的像素 ** 此处最好有函数得到 **

/*========================================================================================*/

    TInt iNowWidth = 0;                // 目前显示的宽剩余量

    TInt iStoreNextBegin = 0;              // 记录下次开始的位子

    TInt iStoreNowEnd = 0;                 // 记录当前结束的

    TInt iStoreNowBegin = 0;           // 记录本行开始的字符

    TBool iNewLine = ETrue ;            // 标志是否是新的一行

    TInt iLineNum = 0;                  // 记录显示的行数

    TInt spaceArray[16];                // 记录每行的空格数,用于空格间距调整

    TInt iSpaceNum = 0;                    // 记录单行空格数

    // Get the standard graphics context

    CWindowGc & gc = SystemGc ();

    // Gets the control's extent

    TRect drawRect(aRect);

    // Clears the screen

    gc. Clear (drawRect);

    // 设置并运用字体

    CFont *iFont;

    iCoeEnv -> ScreenDevice ()-> GetNearestFontInTwips (iFont, TFontSpec (KFontName,KTextHight)); // 按要求设置字体

    gc. UseFont (iFont);

    /*

      * 文本要求,没有中文,没有连续空格,单个字符快(空格分隔)不能超过 30

      * 判断是否是新的一行开始

      *         :: 得到上次遗留字符的像素需求

      *               需要更新剩余显示的宽度

      *              设置新的一行开始标志

      *              得到新的一行开始的位置

      * 获得一个新的字符

      * 判断是空格?是   :: 把空格的 I 变量记录为这行的结束,结束空格直接输出,循环完毕

      * 判断是换行?是 :: 设置新行 I - 1 位置的记录为本行结束 , 并把 I 的下个位置认为是下一行的开始

      */

    for ( TInt i = 0;i != iRealText. Length (); ++i)

    {

       if (iNewLine)

       {

           iSpaceNum = 0;

           TPtrC16 tempPtr(iRealText. Ptr ()+ iStoreNextBegin,i - iStoreNextBegin); // 获得上次遗留字符

           TInt iRemainderWidth = iFont-> TextWidthInPixels (tempPtr);

           iNowWidth = KLineWidth - iRemainderWidth;

           iNewLine = EFalse ;

           iStoreNowBegin = iStoreNextBegin; // 上次循环记录的 Begin 赋值

       }

       TPtrC16 ptr(iRealText. Ptr ()+ i,1); // 获得 Text 中的一个字符

       if (*(iRealText. Ptr ()+ i) == ' ' )

       {

           iStoreNowEnd = i; // 记录空格

           spaceArray[++iSpaceNum] = i;   //0 单元不使用, Num 直接是本行空格数

           if (i + 1 == iRealText. Length ())

           {

              gc. SetPenColor ( TRgb (0x008eff));    // 改变颜色

              gc. SetUnderlineStyle ( EUnderlineOn ); // 添加下划线,显示网址

              iNewLine = ETrue ;

           }

       }  

       else if ((*(iRealText. Ptr ()+ i) == '/n' )) //|| (*(iRealText.Ptr()+ i) == '#'))

       {

           iNewLine = ETrue ;

           iStoreNowEnd = i-1;

           iStoreNextBegin = i + 1;

       }

       if (!iNewLine) // 如果上文得到是换行,无需再计算,直接输出

       {

           TInt iCharWidth = iFont-> TextWidthInPixels (ptr);

           iNowWidth -=iCharWidth; // 减去当前的像素需求

           if (iNowWidth < 0) // 本行无法满足需求

           {

              if (iStoreNowEnd - iStoreNowBegin == 0)

                  iStoreNowEnd = i-1;

              iStoreNextBegin = iStoreNowEnd + 1;  // 记录本单词的开始位置

              /*

                * 判断是否需要拉伸操作

                */    

              if (iSpaceNum > 4) // 空格也大于 5 的时候 才开始调整

              {

                  TPtrC16 iRemainderPtr(iRealText. Ptr ()+ iStoreNowEnd,i - iStoreNowEnd);

                  TInt iRemainderWidth = iFont-> TextWidthInPixels (iRemainderPtr); // 显示这一行   没有拉伸操作时不须恢复记录                

                  if (iRemainderWidth > 16)  // 当剩余的宽度大于 16

                  {

                     // 下面的数据减一是为了显示的时候不会太挤

                     TInt adjustSpaceWidth = (iSpaceNum * KSpaceWidth + iRemainderWidth)/(iSpaceNum - 1) - 1;

                     TInt tempPtrWidth = 0;  // 当前显示段字符的像素

                     for ( TInt i = 0; i < iSpaceNum; i++)

                     {

                         TPtrC16 tempPtr(iRealText. Ptr ()+ iStoreNowBegin,spaceArray[i + 1] - iStoreNowBegin);

                         iShowText. Append (tempPtr);

                         gc. DrawText (iShowText, TPoint ( Rect (). iTl . iX + tempPtrWidth + i * adjustSpaceWidth, Rect (). iTl . iY + KLineHight + KLineHight * iLineNum)); // 打印

                         iShowText. Zero (); // 清空

                         tempPtrWidth += iFont-> TextWidthInPixels (tempPtr);

                         iStoreNowBegin = spaceArray[i + 1] + 1; // 下次打印的开始是本次空格的后个位置

                     }

                     iNewLine = ETrue ;

                     ++iLineNum;

                     continue ;     // 打印完毕,不要做下次处理

                  }

              }

              iNewLine = ETrue ; // 开始打印

           }

       }

       if (iNewLine)

       {

           TPtrC16 tempPtr(iRealText. Ptr ()+ iStoreNowBegin,iStoreNowEnd - iStoreNowBegin + 1); // 得到本行的开始于长度数据

           iShowText. Append (tempPtr);

           gc. DrawText (iShowText, TPoint ( Rect (). iTl . iX , Rect (). iTl . iY + KLineHight + KLineHight * iLineNum)); // 打印

           iShowText. Zero (); // 清空

           ++iLineNum; // 行数加 为接下来的打印做准备

       }

    }

    gc. DiscardFont (); // 释放 font

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值