textout.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {$mode objfpc}{$h+}
  2. {$CODEPAGE UTF8}
  3. program textout;
  4. uses
  5. {$IFDEF UNIX}cwstring, {$ENDIF} classes, sysutils, FPImage, FPCanvas, FPImgCanv, ftFont, FPWritePNG, freetype;
  6. const
  7. MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque);
  8. procedure DoDraw(FN, fnChinese : String);
  9. var
  10. canvas : TFPcustomCAnvas;
  11. image : TFPCustomImage;
  12. writer : TFPCustomImageWriter;
  13. f : TFreeTypeFont;
  14. S : String;
  15. U : UnicodeString;
  16. begin
  17. f:=Nil;
  18. image := TFPMemoryImage.Create (256,256);
  19. Canvas := TFPImageCanvas.Create (image);
  20. Writer := TFPWriterPNG.Create;
  21. InitEngine;
  22. with TFPWriterPNG(Writer) do
  23. begin
  24. indexed := false;
  25. wordsized := false;
  26. UseAlpha := false;
  27. GrayScale := false;
  28. end;
  29. try
  30. with Canvas as TFPImageCanvas do
  31. begin
  32. // Clear background
  33. brush.FPcolor:=colwhite;
  34. brush.style:=bsSolid;
  35. pen.mode := pmCopy;
  36. pen.style := psSolid;
  37. pen.width := 1;
  38. pen.FPColor := colWhite;
  39. FillRect(0,0,255,255);
  40. // Set font
  41. F:=TFreeTypeFont.Create;
  42. Font:=F;
  43. Font.Name:=FN;
  44. Font.Size:=14;
  45. Font.FPColor:=colBlack;
  46. S:='Hello, world!';
  47. Canvas.TextOut(20,20,S);
  48. U:=UTF8Decode('привет, Мир!');
  49. Font.FPColor:=colBlue;
  50. Canvas.TextOut(50,50,U);
  51. if (FNChinese<>'') then
  52. begin
  53. Font.Name:=FNChinese;
  54. U:=UTF8Decode('你好,世界!');
  55. Font.FPColor:=colRed;
  56. Canvas.TextOut(20,100,U);
  57. end
  58. else
  59. begin
  60. Font.Size:=10;
  61. Canvas.TextOut(20,100,'No chinese font available.');
  62. end;
  63. U:=UTF8Decode('non-ASCII chars: ßéùµàçè§âêû');
  64. Font.Size:=10;
  65. Canvas.TextOut(20,180,U);
  66. end;
  67. writeln ('Saving to "TextTest.png" for inspection !');
  68. Image.SaveToFile ('TextTest.png', writer);
  69. finally
  70. F.Free;
  71. Canvas.Free;
  72. image.Free;
  73. writer.Free;
  74. end;
  75. end;
  76. Var
  77. D,FontFile, FontFileChinese : String;
  78. Info : TSearchRec;
  79. begin
  80. // Initialize font search path;
  81. {$IFDEF UNIX}
  82. {$IFNDEF DARWIN}
  83. D := '/usr/share/fonts/truetype/';
  84. DefaultSearchPath:=D;
  85. if FindFirst(DefaultSearchPath+AllFilesMask,faDirectory,Info)=0 then
  86. try
  87. repeat
  88. if (Info.Attr and faDirectory)<>0 then
  89. if (Info.Name<>'.') and (info.name<>'..') then
  90. DefaultSearchPath:=DefaultSearchPath+';'+D+Info.Name;
  91. Until FindNext(Info)<>0;
  92. finally
  93. FindClose(Info);
  94. end;
  95. {$ENDIF}
  96. {$ENDIF}
  97. FontFile:=ParamStr(1);
  98. if FontFile='' then
  99. FontFile:='LiberationSans-Regular.ttf';
  100. FontFileChinese:=ParamStr(2);
  101. if FontFileChinese='' then
  102. With TFontManager.Create do
  103. try
  104. FontFileChinese:=SearchFont('wqy-microhei.ttc',False);
  105. finally
  106. Free;
  107. end;
  108. DoDraw(FontFile,FontFileChinese);
  109. end.