bestlong 怕失憶論壇's Archiver

bestlong 發表於 2006-6-29 18:28

FreeReport 中文折行及亂碼問題

修改 FR_Class.pas
[code]
procedure TfrMemoView.WrapMemo;
...略
  procedure WrapLine(const s: String);
  begin
...略
        if WCanvas.TextWidth(Copy(s, beg, cur - beg + 1)) > maxwidth then
        begin
          WasBreak := False;
...略  
  end;
[/code]
改為
[code]
procedure TfrMemoView.WrapMemo;
...略
  procedure WrapLine(const s: String);
  begin
...略
        if WCanvas.TextWidth(Copy(s, beg, cur - beg + 1)) > maxwidth then
        begin

          if ByteType(s, cur) = mbLeadByte  then cur := cur - 1;

          WasBreak := False;
...略  
  end;
[/code]
使用此一方法,只有解決在計算最大顯示寬度時不會在中文字的中間問題。但是折行點的選擇還是使用英文格式,如果全部沒有空格字就都不會折行。

bestlong 發表於 2006-6-29 18:30

將函數直接替換
[code]
procedure WrapLine(const s:WideString);
var
  i, cur, beg, last, LoopPos: Integer;
  WasBreak, CRLF: Boolean;
  s1:string;
begin
  CRLF := False;
  LoopPos := 0;
  s1 := s;
  for i := 1 to Length(s1) do
    if s1[i] in [#10, #13] then
    begin
      CRLF := True;
      break;
    end;
  last := 1; beg := 1;
  if not CRLF and ((Length(s) <= 1) or (WCanvas.TextWidth(s) <= maxwidth)) then
    OutLine(s + #1)
  else
  begin
    cur := 1;
    while cur <= Length(s) do
    begin
      if s1[cur] in [#10, #13] then
      begin
        OutLine(Copy(s1, beg, cur - beg) + #1);
        while (cur < Length(s1)) and (s1[cur] in [#10, #13]) do Inc(cur);
        beg := cur; last := beg;
        if s1[cur] in [#13, #10] then
          Exit else
          continue;
      end;
      if s[cur] <> ' ' then
      if WCanvas.TextWidth(Copy(s, beg, cur - beg + 1)) > maxwidth then
      begin
      
      
      
        WasBreak := False;
        if (Flags and flWordBreak) <> 0 then
        begin
          i := cur;
          while (i <= Length(s1)) and not (s1[i] in spaces) do
            Inc(i);
          b := BreakWord(Copy(s, last + 1, i - last - 1));
          if Length(b) > 0 then
          begin
            i := 1;
            cur := last;
            while (i <= Length(b)) and
              (WCanvas.TextWidth(Copy(s, beg, last - beg + 1 + Ord(b[i])) + '-') <= maxwidth) do
            begin
              WasBreak := True;
              cur := last + Ord(b[i]);
              Inc(i);
            end;
            last := cur;
          end;
        end
        else
          if last = beg then last := cur;
        if WasBreak then
          OutLine(Copy(s, beg, last - beg + 1) + '-')
        else if s[last] = ' ' then
          OutLine(Copy(s, beg, last - beg)) else
          OutLine(Copy(s, beg, last - beg + 1));
        if ((Flags and flWordBreak) <> 0) and not WasBreak and (last = cur) then
          if LoopPos = cur then
          begin
            beg := cur + 1;
            cur := Length(s);
            break;
          end
          else
            LoopPos := cur;
        beg := last + 1; last := beg;
      end;
//    if s[cur] in spaces then last := cur;
      if s[cur] = ' ' then last := cur;
      Inc(cur);
    end;
    if beg <> cur then OutLine(Copy(s, beg, cur - beg + 1) + #1);
  end;
end;
[/code]

資料來源:http://www.delphibbs.com/delphibbs/dispq.asp?lid=2059269
頁: [1]

Powered by Discuz! X1.5 Archiver   © 2001-2010 Comsenz Inc.