textout.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. F.Size := 14.5;
  49. Canvas.TextOut(20,30,S);
  50. U:=UTF8Decode('привет, Мир!');
  51. Font.FPColor:=colBlue;
  52. Canvas.TextOut(50,50,U);
  53. if (FNChinese<>'') then
  54. begin
  55. Font.Name:=FNChinese;
  56. U:=UTF8Decode('你好,世界!');
  57. Font.FPColor:=colRed;
  58. Canvas.TextOut(20,100,U);
  59. end
  60. else
  61. begin
  62. Font.Size:=10;
  63. Canvas.TextOut(20,100,'No chinese font available.');
  64. end;
  65. U:=UTF8Decode('non-ASCII chars: ßéùµàçè§âêû');
  66. Font.Size:=10;
  67. Canvas.TextOut(20,180,U);
  68. end;
  69. writeln ('Saving to "TextTest.png" for inspection !');
  70. Image.SaveToFile ('TextTest.png', writer);
  71. finally
  72. F.Free;
  73. Canvas.Free;
  74. image.Free;
  75. writer.Free;
  76. end;
  77. end;
  78. Var
  79. D,FontFile, FontFileChinese : String;
  80. Info : TSearchRec;
  81. begin
  82. // Initialize font search path;
  83. {$IFDEF UNIX}
  84. {$IFNDEF DARWIN}
  85. D := '/usr/share/fonts/truetype/';
  86. DefaultSearchPath:=D;
  87. if FindFirst(DefaultSearchPath+AllFilesMask,faDirectory,Info)=0 then
  88. try
  89. repeat
  90. if (Info.Attr and faDirectory)<>0 then
  91. if (Info.Name<>'.') and (info.name<>'..') then
  92. DefaultSearchPath:=DefaultSearchPath+';'+D+Info.Name;
  93. Until FindNext(Info)<>0;
  94. finally
  95. FindClose(Info);
  96. end;
  97. {$ENDIF}
  98. {$ENDIF}
  99. FontFile:=ParamStr(1);
  100. if FontFile='' then
  101. FontFile:='LiberationSans-Regular.ttf';
  102. FontFileChinese:=ParamStr(2);
  103. if FontFileChinese='' then
  104. With TFontManager.Create do
  105. try
  106. FontFileChinese:=SearchFont('wqy-microhei.ttc',False);
  107. finally
  108. Free;
  109. end;
  110. DoDraw(FontFile,FontFileChinese);
  111. end.