bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi FastReport 報表元件 FastReport 3.X 在 Win9X 下的中文折行亂碼問題 ...
查看: 5635|回復: 0
go

FastReport 3.X 在 Win9X 下的中文折行亂碼問題 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2006-6-23 13:18 |只看該作者 |倒序瀏覽 |打印
FastReport 3.X 在 Win9X 下的中文折行亂碼問題

修改 frxGraphicUtils.pas
  1. procedure TfrxDrawText.WrapTextLine(s: string; Width, FirstLineWidth,
  2.   CharSpacing: Integer);
  3. var
  4.   n, i, Offset, LineBegin, LastSpace, BreakPos: Integer;
  5.   sz: TSize;
  6.   TheWord: string;
  7.   WasBreak: Boolean;

  8.   //俄文換行
  9.   function BreakWord(const s: string; LineBegin, CurPos, LineEnd: Integer):
  10.       string;
  11.   var
  12.     i, BreakPos: Integer;
  13.     TheWord, Breaks: string;
  14.   begin
  15.     // get the whole word
  16.     i := CurPos;
  17.     while (i <= LineEnd) and (Pos(s[i], ' .,-;') = 0) do
  18.       Inc(i);

  19.     TheWord := Copy(s, LineBegin, i - LineBegin);
  20.     // get available break positions
  21.     Breaks := BreakRussianWord(AnsiUpperCase(TheWord));
  22.     // find the closest position
  23.     BreakPos := CurPos - LineBegin;
  24.     for i := Length(Breaks) downto 1 do
  25.       if Ord(Breaks[i]) < BreakPos then
  26.       begin
  27.         BreakPos := Ord(Breaks[i]);
  28.         break;
  29.       end;
  30.     if BreakPos <> CurPos - LineBegin then
  31.       Result := Copy(TheWord, 1, BreakPos) else
  32.       Result := '';
  33.   end;

  34. begin
  35.   // remove all HTML tags and build the tag list
  36.   FHTMLTags.NewLine;
  37.   FHTMLTags.ExpandHTMLTags(s);
  38.   FHTMLTags.FPosition := FHTMLTags.FPosition + 2;

  39.   n := Length(s);
  40.   if (n < 2) or not FWordWrap then // no need to wrap a string with 0 or 1 symbol
  41.   begin
  42.     FText.Add(s);
  43.     Exit;
  44.   end;
  45.   // 字符間空隙和計算寬度
  46.   // get the intercharacter spacing table and calculate the width
  47.   sz.cx := FHTMLTags.FillCharSpacingArray(FTempArray, s, FCanvas,
  48.     FHTMLTags.Count - 1, CharSpacing, True);

  49.   // 不需換行
  50.   // text fits, no need to wrap it
  51.   if sz.cx < FirstLineWidth then
  52.   begin
  53.     FText.Add(s);
  54.     Exit;
  55.   end;

  56.   Offset := 0; // 偏移量
  57.   i := 1;
  58.   LineBegin := 1; // index of the first symbol in the current line
  59.   LastSpace := 1; // index of the last space symbol in the current line


  60.   while i <= n do
  61.   begin
  62.     if s[i] = ' ' then
  63.       LastSpace := i;

  64.     if FTempArray[i] - Offset > FirstLineWidth then // need wrap
  65.     begin
  66.       if LastSpace = LineBegin then // there is only one word without spaces...
  67.       begin
  68.         if i <> LineBegin then // ... and it has more than 1 symbol
  69.         begin
  70.           if FWordBreak then   // 俄文換行
  71.           begin
  72.             TheWord := BreakWord(s, LineBegin, i, n); //俄文換行
  73.             WasBreak := TheWord <> '';
  74.             if not WasBreak then
  75.               TheWord := Copy(s, LineBegin, i - LineBegin);
  76.             if WasBreak then
  77.               FText.Add(TheWord + '-') else
  78.               FText.Add(TheWord);
  79.             BreakPos := Length(TheWord);
  80.             FHTMLTags.Wrap(BreakPos, WasBreak);
  81.             LastSpace := LineBegin + BreakPos - 1;
  82.           end
  83.           else
  84.           begin
  85.             // mbSingleByte  表示了一個完整的字符時,
  86.             // mbLeadByte    表示一個雙字節字符的頭一個字節
  87.             // mbTrailByte   表示一個雙字節字符的第二個字節

  88.             //todo: [2005-07-29] 狂迷修改中文换行
  89.             if ByteType(s, i - 1) = mbLeadByte then //判断是否是漢字字節
  90.             begin
  91.               FText.Add(Copy(s, LineBegin, i - LineBegin -1));
  92.               FHTMLTags.Wrap(i - LineBegin -1, False);
  93.               LastSpace := i - 2;
  94.             end
  95.             else
  96.             begin
  97.               FText.Add(Copy(s, LineBegin, i - LineBegin));
  98.               FHTMLTags.Wrap(i - LineBegin, False);
  99.               LastSpace := i - 1;
  100.             end;
  101.           end;
  102.         end
  103.         else
  104.         begin
  105.           FText.Add(s[LineBegin]);
  106.           // can't wrap 1 symbol, just add it to the new line
  107.           FHTMLTags.Wrap(1, False);
  108.         end;
  109.       end
  110.       else // we have a space symbol inside
  111.       begin
  112.         if FWordBreak then
  113.         begin
  114.           TheWord := BreakWord(s, LastSpace + 1, i, n);
  115.           WasBreak := TheWord <> '';
  116.           if WasBreak then
  117.             FText.Add(Copy(s, LineBegin, LastSpace - LineBegin + 1) + TheWord +
  118.               '-') else
  119.             FText.Add(Copy(s, LineBegin, LastSpace - LineBegin));
  120.           BreakPos := LastSpace - LineBegin + Length(TheWord) + 1;
  121.           FHTMLTags.Wrap(BreakPos, WasBreak);
  122.           if WasBreak then
  123.             LastSpace := LineBegin + BreakPos - 1;
  124.         end
  125.         else
  126.         begin
  127.           FText.Add(Copy(s, LineBegin, LastSpace - LineBegin));
  128.           FHTMLTags.Wrap(LastSpace - LineBegin + 1, False);
  129.         end;
  130.       end;

  131.       Offset := FTempArray[LastSpace]; // starting a new line
  132.       Inc(LastSpace);
  133.       LineBegin := LastSpace;
  134.       FirstLineWidth := Width; // this line is not first, so use Width
  135.     end;

  136.     Inc(i);
  137.   end;

  138.   if n - LineBegin + 1 > 0 then // put the rest of line to FText
  139.     FText.Add(Copy(s, LineBegin, n - LineBegin + 1));
  140. end;
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-4-19 12:48 , Processed in 0.029761 second(s), 11 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.