bestlong 怕失憶論壇's Archiver

bestlong 發表於 2006-6-23 13:18

FastReport 3.X 在 Win9X 下的中文折行亂碼問題

FastReport 3.X 在 Win9X 下的中文折行亂碼問題

修改 frxGraphicUtils.pas
[code]
procedure TfrxDrawText.WrapTextLine(s: string; Width, FirstLineWidth,
  CharSpacing: Integer);
var
  n, i, Offset, LineBegin, LastSpace, BreakPos: Integer;
  sz: TSize;
  TheWord: string;
  WasBreak: Boolean;

  //俄文換行
  function BreakWord(const s: string; LineBegin, CurPos, LineEnd: Integer):
      string;
  var
    i, BreakPos: Integer;
    TheWord, Breaks: string;
  begin
    // get the whole word
    i := CurPos;
    while (i <= LineEnd) and (Pos(s[i], ' .,-;') = 0) do
      Inc(i);

    TheWord := Copy(s, LineBegin, i - LineBegin);
    // get available break positions
    Breaks := BreakRussianWord(AnsiUpperCase(TheWord));
    // find the closest position
    BreakPos := CurPos - LineBegin;
    for i := Length(Breaks) downto 1 do
      if Ord(Breaks[i]) < BreakPos then
      begin
        BreakPos := Ord(Breaks[i]);
        break;
      end;
    if BreakPos <> CurPos - LineBegin then
      Result := Copy(TheWord, 1, BreakPos) else
      Result := '';
  end;

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

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

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

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


  while i <= n do
  begin
    if s[i] = ' ' then
      LastSpace := i;

    if FTempArray[i] - Offset > FirstLineWidth then // need wrap
    begin
      if LastSpace = LineBegin then // there is only one word without spaces...
      begin
        if i <> LineBegin then // ... and it has more than 1 symbol
        begin
          if FWordBreak then   // 俄文換行
          begin
            TheWord := BreakWord(s, LineBegin, i, n); //俄文換行
            WasBreak := TheWord <> '';
            if not WasBreak then
              TheWord := Copy(s, LineBegin, i - LineBegin);
            if WasBreak then
              FText.Add(TheWord + '-') else
              FText.Add(TheWord);
            BreakPos := Length(TheWord);
            FHTMLTags.Wrap(BreakPos, WasBreak);
            LastSpace := LineBegin + BreakPos - 1;
          end
          else
          begin
            // mbSingleByte  表示了一個完整的字符時,
            // mbLeadByte    表示一個雙字節字符的頭一個字節
            // mbTrailByte   表示一個雙字節字符的第二個字節

            //todo: [2005-07-29] 狂迷修改中文换行
            if ByteType(s, i - 1) = mbLeadByte then //判断是否是漢字字節
            begin
              FText.Add(Copy(s, LineBegin, i - LineBegin -1));
              FHTMLTags.Wrap(i - LineBegin -1, False);
              LastSpace := i - 2;
            end
            else
            begin
              FText.Add(Copy(s, LineBegin, i - LineBegin));
              FHTMLTags.Wrap(i - LineBegin, False);
              LastSpace := i - 1;
            end;
          end;
        end
        else
        begin
          FText.Add(s[LineBegin]);
          // can't wrap 1 symbol, just add it to the new line
          FHTMLTags.Wrap(1, False);
        end;
      end
      else // we have a space symbol inside
      begin
        if FWordBreak then
        begin
          TheWord := BreakWord(s, LastSpace + 1, i, n);
          WasBreak := TheWord <> '';
          if WasBreak then
            FText.Add(Copy(s, LineBegin, LastSpace - LineBegin + 1) + TheWord +
              '-') else
            FText.Add(Copy(s, LineBegin, LastSpace - LineBegin));
          BreakPos := LastSpace - LineBegin + Length(TheWord) + 1;
          FHTMLTags.Wrap(BreakPos, WasBreak);
          if WasBreak then
            LastSpace := LineBegin + BreakPos - 1;
        end
        else
        begin
          FText.Add(Copy(s, LineBegin, LastSpace - LineBegin));
          FHTMLTags.Wrap(LastSpace - LineBegin + 1, False);
        end;
      end;

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

    Inc(i);
  end;

  if n - LineBegin + 1 > 0 then // put the rest of line to FText
    FText.Add(Copy(s, LineBegin, n - LineBegin + 1));
end;
[/code]
頁: [1]

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