textout.pp 3.3 KB

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