fpdpansi.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. {
  2. This file is part of text IDE
  3. Copyright (c) 2000 by Pierre Muller
  4. Unit to export current screen buffer to an ansi file
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit fpdpansi;
  12. interface
  13. uses
  14. objects,
  15. video;
  16. function ExportBufferToAnsiFile(var Buffer : TVideoBuf;xmin,xmax,ymin,ymax,linesize : sw_integer;
  17. SaveAsText : boolean;var f : text) : boolean;
  18. implementation
  19. uses
  20. wutils;
  21. const
  22. { Foreground and background color constants }
  23. Black = 0;
  24. Blue = 1;
  25. Green = 2;
  26. Cyan = 3;
  27. Red = 4;
  28. Magenta = 5;
  29. Brown = 6;
  30. LightGray = 7;
  31. const ColorTab : array[0..7] of byte =
  32. (Black,Red,Green,Brown,Blue,Magenta,Cyan,LightGray);
  33. {$i-}
  34. function ExportBufferToAnsiFile(var Buffer : TVideoBuf;xmin,xmax,ymin,ymax,linesize : sw_integer;
  35. SaveAsText : boolean;var f : text) : boolean;
  36. var
  37. CurColor : byte;
  38. CurForColor, CurBackColor : byte;
  39. CurIsBold, CurIsBlinking : boolean;
  40. procedure ChangeColor(NewColor : byte);
  41. var
  42. ForColor, BackColor : byte;
  43. IsBold, IsBlinking : boolean;
  44. begin
  45. ForColor:=NewColor and 7;
  46. BackColor:=(NewColor and $70) shr 4;
  47. IsBold:=(NewColor and 8) <> 0;
  48. IsBlinking:=(NewColor and $80) <> 0;
  49. if IsBlinking<>CurIsBlinking then
  50. begin
  51. if IsBlinking then
  52. Write(f,#27'[5m')
  53. else
  54. Write(f,#27'[25m');
  55. CurIsBlinking:=IsBlinking;
  56. end;
  57. if IsBold<>CurIsBold then
  58. begin
  59. if IsBold then
  60. Write(f,#27'[1m')
  61. else
  62. Write(f,#27'[21m');
  63. CurIsBold:=IsBold;
  64. end;
  65. if CurForColor<>ForColor then
  66. begin
  67. Write(f,#27'['+inttostr(ColorTab[ForColor]+30)+'m');
  68. CurForColor:=ForColor;
  69. end;
  70. if CurBackColor<>BackColor then
  71. begin
  72. Write(f,#27'['+inttostr(ColorTab[BackColor]+40)+'m');
  73. CurBackColor:=BackColor;
  74. end;
  75. CurColor:=NewColor;
  76. end;
  77. var
  78. Ch : char;
  79. textAttr : byte;
  80. i, j : sw_integer;
  81. begin
  82. CurColor:=0;
  83. for i:=ymin to ymax do
  84. begin
  85. for j:=xmin to xmax do
  86. begin
  87. ch:=chr(Buffer[i*linesize+j] and $ff);
  88. textattr:=Buffer[i*linesize+j] shr 8;
  89. if (textattr<>CurColor) and not SaveAsText then
  90. ChangeColor(textattr);
  91. { Escape escape, by printing two #27 PM }
  92. if (ch=#27) or (ord(ch)<=16) then
  93. Write(f,#27);
  94. Write(f,ch);
  95. end;
  96. writeln(f);
  97. end;
  98. ExportBufferToAnsiFile:=(IOResult=0);
  99. end;
  100. end.