utooltext.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit UToolText;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, SysUtils, UTool, UToolVectorial, LCLType, Graphics, BGRABitmap, BGRABitmapTypes, BGRATextFX,
  7. BGRAGradients, LCVectorOriginal;
  8. type
  9. { TToolText }
  10. TToolText = class(TVectorialTool)
  11. protected
  12. FMatrix: TAffineMatrix;
  13. FPrevShadow: boolean;
  14. FPrevShadowOffset: TPoint;
  15. FPrevShadowRadius: single;
  16. function ShapeClass: TVectorShapeAny; override;
  17. function AlwaysRasterizeShape: boolean; override;
  18. procedure IncludeShadowBounds(var ARect: TRect);
  19. function GetCustomShapeBounds(ADestBounds: TRect; AMatrix: TAffineMatrix; ADraft: boolean): TRect; override;
  20. procedure DrawCustomShape(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean); override;
  21. procedure ShapeChange(ASender: TObject; ABounds: TRectF; ADiff: TVectorShapeDiff); override;
  22. procedure ShapeEditingChange(ASender: TObject); override;
  23. procedure AssignShapeStyle(AMatrix: TAffineMatrix; AAlwaysFit: boolean); override;
  24. procedure QuickDefineEnd; override;
  25. function RoundCoordinate(constref ptF: TPointF): TPointF; override;
  26. function DoToolKeyDown(var key: Word): TRect; override;
  27. public
  28. constructor Create(AManager: TToolManager); override;
  29. function GetContextualToolbars: TContextualToolbars; override;
  30. function ToolCommand(ACommand: TToolCommand): boolean; override;
  31. function ToolProvideCommand(ACommand: TToolCommand): boolean; override;
  32. end;
  33. implementation
  34. uses LCVectorTextShapes, BGRALayerOriginal, BGRATransform, BGRAGrayscaleMask,
  35. ugraph, math;
  36. { TToolText }
  37. function TToolText.ShapeClass: TVectorShapeAny;
  38. begin
  39. result := TTextShape;
  40. end;
  41. function TToolText.AlwaysRasterizeShape: boolean;
  42. begin
  43. Result:= Manager.TextShadow;
  44. end;
  45. procedure TToolText.IncludeShadowBounds(var ARect: TRect);
  46. var
  47. shadowRect: TRect;
  48. begin
  49. if Manager.TextShadow then
  50. begin
  51. shadowRect := ARect;
  52. shadowRect.Inflate(ceil(Manager.TextShadowBlurRadius),ceil(Manager.TextShadowBlurRadius));
  53. shadowRect.Offset(Manager.TextShadowOffset.X,Manager.TextShadowOffset.Y);
  54. ARect := RectUnion(ARect, shadowRect);
  55. end;
  56. end;
  57. function TToolText.GetCustomShapeBounds(ADestBounds: TRect; AMatrix: TAffineMatrix; ADraft: boolean): TRect;
  58. begin
  59. Result:= inherited GetCustomShapeBounds(ADestBounds, AMatrix, ADraft);
  60. IncludeShadowBounds(result);
  61. result.Intersect(ADestBounds);
  62. end;
  63. procedure TToolText.DrawCustomShape(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean);
  64. var
  65. temp: TBGRABitmap;
  66. blur, gray, grayShape: TGrayscaleMask;
  67. shapeBounds, blurBounds, r, actualShapeBounds: TRect;
  68. begin
  69. if Manager.TextShadow then
  70. begin
  71. shapeBounds := GetCustomShapeBounds(rect(0,0,ADest.Width,ADest.Height),AMatrix,ADraft);
  72. shapeBounds.Intersect(ADest.ClipRect);
  73. if (shapeBounds.Width > 0) and (shapeBounds.Height > 0) then
  74. begin
  75. temp := TBGRABitmap.Create(shapeBounds.Width,shapeBounds.Height);
  76. inherited DrawCustomShape(temp, AffineMatrixTranslation(-shapeBounds.Left,-shapeBounds.Top)*AMatrix, ADraft);
  77. actualShapeBounds := temp.GetImageBounds;
  78. if not actualShapeBounds.IsEmpty then
  79. begin
  80. actualShapeBounds.Offset(shapeBounds.Left,shapeBounds.Top);
  81. grayShape := TGrayscaleMask.Create;
  82. grayShape.CopyFrom(temp, cAlpha);
  83. blurBounds := actualShapeBounds;
  84. blurBounds.Inflate(ceil(Manager.TextShadowBlurRadius),ceil(Manager.TextShadowBlurRadius));
  85. blurBounds.Offset(Manager.TextShadowOffset.X,Manager.TextShadowOffset.Y);
  86. r := ADest.ClipRect;
  87. r.Inflate(ceil(Manager.TextShadowBlurRadius),ceil(Manager.TextShadowBlurRadius));
  88. blurBounds.Intersect(r);
  89. gray := TGrayscaleMask.Create(blurBounds.Width,blurBounds.Height);
  90. gray.PutImage(shapeBounds.Left-blurBounds.Left+Manager.TextShadowOffset.X,
  91. shapeBounds.Top-blurBounds.Top+Manager.TextShadowOffset.Y,grayShape,dmSet);
  92. grayShape.Free;
  93. blur := gray.FilterBlurRadial(Manager.TextShadowBlurRadius,Manager.TextShadowBlurRadius, rbFast) as TGrayscaleMask;
  94. gray.Free;
  95. ADest.FillMask(blurBounds.Left,blurBounds.Top,blur,BGRABlack,dmDrawWithTransparency);
  96. blur.Free;
  97. end;
  98. ADest.PutImage(shapeBounds.Left,shapeBounds.Top,temp,dmDrawWithTransparency);
  99. temp.Free;
  100. end;
  101. FPrevShadow := true;
  102. FPrevShadowRadius := Manager.TextShadowBlurRadius;
  103. FPrevShadowOffset := Manager.TextShadowOffset;
  104. end else
  105. begin
  106. inherited DrawCustomShape(ADest, AMatrix, ADraft);
  107. FPrevShadow := false;
  108. end;
  109. end;
  110. procedure TToolText.ShapeChange(ASender: TObject; ABounds: TRectF; ADiff: TVectorShapeDiff);
  111. var
  112. r: TRect;
  113. posF: TPointF;
  114. begin
  115. posF := AffineMatrixInverse(FMatrix)*(FShape as TTextShape).LightPosition;
  116. Manager.LightPosition := posF;
  117. with ABounds do r := rect(floor(Left),floor(Top),ceil(Right),ceil(Bottom));
  118. IncludeShadowBounds(r);
  119. inherited ShapeChange(ASender, RectF(r.Left,r.Top,r.Right,r.Bottom), ADiff);
  120. end;
  121. procedure TToolText.ShapeEditingChange(ASender: TObject);
  122. begin
  123. with (FShape as TTextShape) do
  124. Manager.TextAlign := ParagraphAlignment;
  125. inherited ShapeEditingChange(ASender);
  126. end;
  127. procedure TToolText.AssignShapeStyle(AMatrix: TAffineMatrix; AAlwaysFit: boolean);
  128. var
  129. r: TRect;
  130. toolDest: TBGRABitmap;
  131. zoom: Single;
  132. begin
  133. inherited AssignShapeStyle(AMatrix, AAlwaysFit);
  134. FMatrix := AMatrix;
  135. with TTextShape(FShape) do
  136. begin
  137. zoom := (VectLen(AMatrix[1,1],AMatrix[2,1])+VectLen(AMatrix[1,2],AMatrix[2,2]))/2;
  138. FontEmHeight:= zoom*Manager.TextFontSize*Manager.Image.DPI/72;
  139. FontName:= Manager.TextFontName;
  140. FontStyle:= Manager.TextFontStyle;
  141. Aliased := Manager.ShapeOptionAliasing;
  142. LightPosition := AMatrix*Manager.LightPosition;
  143. AltitudePercent:= Manager.PhongShapeAltitude;
  144. ParagraphAlignment:= Manager.TextAlign;
  145. PenPhong := Manager.TextPhong;
  146. end;
  147. if (Manager.TextShadow <> FPrevShadow) or
  148. (Manager.TextShadowBlurRadius <> FPrevShadowRadius) or
  149. (Manager.TextShadowOffset <> FPrevShadowOffset) then
  150. begin
  151. toolDest := GetToolDrawingLayer;
  152. r:= UpdateShape(toolDest);
  153. Action.NotifyChange(toolDest, r);
  154. end;
  155. end;
  156. procedure TToolText.QuickDefineEnd;
  157. begin
  158. FShape.Usermode := vsuEditText;
  159. end;
  160. function TToolText.RoundCoordinate(constref ptF: TPointF): TPointF;
  161. begin
  162. result := PointF(floor(ptF.x)+0.5,floor(ptF.y)+0.5);
  163. end;
  164. constructor TToolText.Create(AManager: TToolManager);
  165. begin
  166. inherited Create(AManager);
  167. FMatrix := AffineMatrixIdentity;
  168. end;
  169. function TToolText.GetContextualToolbars: TContextualToolbars;
  170. begin
  171. Result:= [ctPenFill,ctText,ctOutlineFill,ctOutlineWidth,ctAliasing];
  172. if Manager.TextPhong then include(result, ctAltitude);
  173. end;
  174. function TToolText.DoToolKeyDown(var key: Word): TRect;
  175. var
  176. keyUtf8: TUTF8Char;
  177. handled: Boolean;
  178. begin
  179. if Key = VK_SPACE then
  180. begin
  181. keyUtf8:= ' ';
  182. result := ToolKeyPress(keyUtf8);
  183. Key := 0;
  184. end else
  185. if (Key = VK_ESCAPE) and Assigned(FShape) then
  186. begin
  187. if FShape.Usermode = vsuEditText then
  188. FShape.Usermode := vsuEdit
  189. else
  190. result := ValidateShape;
  191. Key := 0;
  192. end else
  193. if (Key = VK_RETURN) and Assigned(FShape) then
  194. begin
  195. handled := false;
  196. FShape.KeyDown(ShiftState, skReturn, handled);
  197. if not handled then ValidateShape;
  198. Key := 0;
  199. end else
  200. Result:=inherited DoToolKeyDown(key);
  201. end;
  202. function TToolText.ToolCommand(ACommand: TToolCommand): boolean;
  203. begin
  204. if Assigned(FShape) and (FShape.Usermode = vsuEditText) then
  205. case ACommand of
  206. tcCopy: Result:= TTextShape(FShape).CopySelection;
  207. tcCut: Result:= TTextShape(FShape).CutSelection;
  208. tcPaste: Result:= TTextShape(FShape).PasteSelection;
  209. tcDelete: Result:= TTextShape(FShape).DeleteSelection;
  210. else
  211. result := inherited ToolCommand(ACommand);
  212. end
  213. else
  214. case ACommand of
  215. tcDelete:
  216. if Assigned(FShape) then
  217. begin
  218. CancelShape;
  219. result := true;
  220. end else result := false;
  221. else result := inherited ToolCommand(ACommand);
  222. end;
  223. end;
  224. function TToolText.ToolProvideCommand(ACommand: TToolCommand): boolean;
  225. begin
  226. if Assigned(FShape) and (FShape.Usermode = vsuEditText) then
  227. case ACommand of
  228. tcCopy,tcCut,tcDelete: result := TTextShape(FShape).HasSelection;
  229. tcPaste: result := true;
  230. else
  231. result := inherited ToolProvideCommand(ACommand);
  232. end
  233. else
  234. case ACommand of
  235. tcDelete: result := Assigned(FShape);
  236. else result := inherited ToolProvideCommand(ACommand);
  237. end;
  238. end;
  239. initialization
  240. RegisterTool(ptText, TToolText);
  241. end.