You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2253 line
69 KiB

  1. unit utsTextSuite;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, contnrs, math, syncobjs,
  6. utsTypes, utsUtils;
  7. type
  8. TtsImage = class;
  9. TtsFont = class;
  10. TtsFontGenerator = class;
  11. TtsRenderer = class;
  12. TtsContext = class;
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. TtsKernel1DItem = packed record
  15. Offset: Integer;
  16. Value: Single;
  17. end;
  18. TtsKernel1D = class
  19. public
  20. Size: Integer;
  21. Items: array of TtsKernel1DItem;
  22. ItemCount: Integer;
  23. constructor Create(const aRadius, aStrength: Single);
  24. end;
  25. TtsKernel2DItem = packed record
  26. OffsetX: Integer;
  27. OffsetY: Integer;
  28. Value: Double;
  29. DataOffset: Integer;
  30. end;
  31. TtsKernel2D = class
  32. public
  33. SizeX: Integer;
  34. SizeY: Integer;
  35. MidSizeX: Integer;
  36. MidSizeY: Integer;
  37. ValueSum: Double;
  38. Items: array of TtsKernel2DItem;
  39. ItemCount: Integer;
  40. constructor Create(const aRadius, aStrength: Single);
  41. end;
  42. TtsImageFunc = procedure(const aImage: TtsImage; X, Y: Integer; var aPixel: TtsColor4f; aArgs: Pointer);
  43. TtsImage = class(TObject)
  44. private
  45. fWidth: Integer;
  46. fHeight: Integer;
  47. fDataSize: Integer;
  48. fLineSize: Integer;
  49. fFormat: TtsFormat;
  50. fData: Pointer;
  51. fHasScanlines: Boolean;
  52. fScanlines: array of Pointer;
  53. function GetScanline(const aIndex: Integer): Pointer;
  54. function GetIsEmpty: Boolean;
  55. procedure SetData(const aData: Pointer; const aFormat: TtsFormat = tsFormatEmpty;
  56. const aWidth: Integer = 0; const aHeight: Integer = 0;
  57. const aLineSize: Integer = 0; const aDataSize: Integer = 0);
  58. procedure UpdateScanlines;
  59. public
  60. property IsEmpty: Boolean read GetIsEmpty;
  61. property Width: Integer read fWidth;
  62. property Height: Integer read fHeight;
  63. property LineSize: Integer read fLineSize;
  64. property DataSize: Integer read fDataSize;
  65. property Format: TtsFormat read fFormat;
  66. property Data: Pointer read fData;
  67. property Scanline[const aIndex: Integer]: Pointer read GetScanline;
  68. function GetPixelAt(const x, y: Integer; out aColor: TtsColor4f): Boolean;
  69. procedure Assign(const aImage: TtsImage);
  70. procedure CreateEmpty(const aFormat: TtsFormat; const aWidth, aHeight: Integer);
  71. procedure LoadFromFunc(const aFunc: TtsImageFunc; const aArgs: Pointer);
  72. procedure Resize(const aNewWidth, aNewHeight, X, Y: Integer);
  73. procedure FindMinMax(out aRect: TtsRect);
  74. procedure FillColor(const aColor: TtsColor4f; const aChannelMask: TtsColorChannels; const aModes: TtsImageModes);
  75. procedure FillPattern(const aPattern: TtsImage; X, Y: Integer; const aChannelMask: TtsColorChannels; const aModes: TtsImageModes);
  76. procedure Blend(const aImage: TtsImage; const X, Y: Integer; const aFunc: TtsBlendFunc);
  77. procedure Blur(const aHorzKernel, aVertKernel: TtsKernel1D; const aChannelMask: TtsColorChannels);
  78. constructor Create;
  79. destructor Destroy; override;
  80. end;
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. TtsCharRenderRef = class(TObject);
  83. TtsChar = class(TObject)
  84. private
  85. fCharCode: WideChar;
  86. fGlyphOrigin: TtsPosition;
  87. fGlyphRect: TtsRect;
  88. fAdvance: Integer;
  89. fRenderRef: TtsCharRenderRef;
  90. public
  91. property CharCode: WideChar read fCharCode;
  92. property GlyphOrigin: TtsPosition read fGlyphOrigin write fGlyphOrigin;
  93. property GlyphRect: TtsRect read fGlyphRect write fGlyphRect;
  94. property Advance: Integer read fAdvance write fAdvance;
  95. property RenderRef: TtsCharRenderRef read fRenderRef write fRenderRef;
  96. constructor Create(const aCharCode: WideChar);
  97. end;
  98. TtsFontCharArray = packed record
  99. Chars: array [Byte] of TtsChar;
  100. CharCount: Byte;
  101. end;
  102. PtsFontCharArray = ^TtsFontCharArray;
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. TtsFont = class(TObject)
  105. private
  106. fRenderer: TtsRenderer;
  107. fGenerator: TtsFontGenerator;
  108. fProperties: TtsFontProperties;
  109. fCharSpacing: Integer;
  110. fTabWidth: Integer;
  111. fLineSpacing: Single;
  112. fChars: array[Byte] of PtsFontCharArray;
  113. fCreateChars: Boolean;
  114. function HasChar(const aCharCode: WideChar): Boolean;
  115. function GetChar(const aCharCode: WideChar): TtsChar;
  116. function GetCharCreate(const aCharCode: WideChar): TtsChar;
  117. procedure AddChar(const aCharCode: WideChar; const aChar: TtsChar); overload;
  118. protected
  119. constructor Create(const aRenderer: TtsRenderer; const aGenerator: TtsFontGenerator; const aProperties: TtsFontProperties);
  120. public
  121. property CreateChars: Boolean read fCreateChars write fCreateChars;
  122. property Char[const aCharCode: WideChar]: TtsChar read GetChar;
  123. property Renderer: TtsRenderer read fRenderer;
  124. property Generator: TtsFontGenerator read fGenerator;
  125. property Properties: TtsFontProperties read fProperties;
  126. property CharSpacing: Integer read fCharSpacing write fCharSpacing;
  127. property TabWidth: Integer read fTabWidth write fTabWidth;
  128. property LineSpacing: Single read fLineSpacing write fLineSpacing;
  129. function AddChar(const aCharCode: WideChar): TtsChar; overload;
  130. procedure AddCharRange(const aCharCodeBeg, aCharCodeEnd: WideChar);
  131. procedure RemoveChar(const aCharCode: WideChar);
  132. procedure ClearChars;
  133. function GetTextWidthW(aText: PWideChar): Integer;
  134. function GetTextWidthA(aText: PAnsiChar): Integer;
  135. procedure GetTextMetric(out aMetric: TtsTextMetric);
  136. destructor Destroy; override;
  137. end;
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. TtsPostProcessStepRange = record
  140. StartChar: WideChar;
  141. EndChar: WideChar;
  142. end;
  143. PtsPostProcessStepRange = ^TtsPostProcessStepRange;
  144. TtsFontProcessStepUsage = (
  145. tsUsageInclude,
  146. tsUsageExclude);
  147. TtsPostProcessStep = class(TObject)
  148. private
  149. fIncludeCharRange: TList;
  150. fExcludeCharRange: TList;
  151. procedure ClearList(const aList: TList);
  152. protected
  153. procedure Execute(const aChar: TtsChar; const aCharImage: TtsImage); virtual; abstract;
  154. public
  155. function IsInRange(const aCharCode: WideChar): Boolean;
  156. procedure AddUsageRange(const aUsage: TtsFontProcessStepUsage; const aStartChar, aEndChar: WideChar);
  157. procedure AddUsageChars(const aUsage: TtsFontProcessStepUsage; aChars: PWideChar);
  158. procedure ClearIncludeRange;
  159. procedure ClearExcludeRange;
  160. constructor Create;
  161. destructor Destroy; override;
  162. end;
  163. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. TtsFontGenerator = class(TObject)
  165. private
  166. fContext: TtsContext;
  167. fFonts: TObjectList;
  168. fPostProcessSteps: TObjectList;
  169. function GetPostProcessStepCount: Integer;
  170. function GetPostProcessStep(const aIndex: Integer): TtsPostProcessStep;
  171. procedure DrawLine(const aChar: TtsChar; const aCharImage: TtsImage; aLinePosition, aLineSize: Integer);
  172. procedure DoPostProcess(const aChar: TtsChar; const aCharImage: TtsImage);
  173. protected
  174. procedure RegisterFont(const aFont: TtsFont);
  175. procedure UnregisterFont(const aFont: TtsFont);
  176. function GetGlyphMetrics(const aFont: TtsFont; const aCharCode: WideChar; out aGlyphOrigin, aGlyphSize: TtsPosition; out aAdvance: Integer): Boolean; virtual; abstract;
  177. procedure GetCharImage(const aFont: TtsFont; const aCharCode: WideChar; const aCharImage: TtsImage); virtual; abstract;
  178. public
  179. property Context: TtsContext read fContext;
  180. property PostProcessStepCount: Integer read GetPostProcessStepCount;
  181. property PostProcessStep[const aIndex: Integer]: TtsPostProcessStep read GetPostProcessStep;
  182. function GenerateChar(const aCharCode: WideChar; const aFont: TtsFont; const aRenderer: TtsRenderer): TtsChar;
  183. function AddPostProcessStep(const aStep: TtsPostProcessStep): TtsPostProcessStep;
  184. function InsertPostProcessStep(const aIndex: Integer; const aStep: TtsPostProcessStep): TtsPostProcessStep;
  185. procedure DeletePostProcessStep(const aIndex: Integer);
  186. procedure ClearPostProcessSteps;
  187. constructor Create(const aContext: TtsContext);
  188. destructor Destroy; override;
  189. end;
  190. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  191. TtsLineItemType = (
  192. tsItemTypeUnknown,
  193. tsItemTypeFont,
  194. tsItemTypeColor,
  195. tsItemTypeText,
  196. tsItemTypeSpace,
  197. tsItemTypeLineBreak,
  198. tsItemTypeTab,
  199. tsItemTypeSpacing);
  200. PtsLineItem = ^TtsLineItem;
  201. TtsLineItem = packed record
  202. Next: PtsLineItem;
  203. Prev: PtsLineItem;
  204. ItemType: TtsLineItemType;
  205. case TtsLineItemType of
  206. tsItemTypeFont: (
  207. Font: TtsFont
  208. );
  209. tsItemTypeColor: (
  210. Color: TtsColor4f;
  211. );
  212. tsItemTypeText, tsItemTypeSpace: (
  213. Text: PWideChar; // text of this item
  214. TextWidth: Integer; // width of text (in pixel)
  215. );
  216. tsItemTypeSpacing: (
  217. Spacing: Integer;
  218. );
  219. end;
  220. TtsLineFlag = (
  221. tsLastItemIsSpace, // is set if the last item was a space item
  222. tsMetaValid, // is set if the line meta data is valid
  223. tsAutoLineBreak // is set if the linebreak was set automatically
  224. );
  225. TtsLineFlags = set of TtsLineFlag;
  226. PtsBlockLine = ^TtsBlockLine;
  227. TtsBlockLine = packed record
  228. Next: PtsBlockLine;
  229. First: PtsLineItem;
  230. Last: PtsLineItem;
  231. Flags: TtsLineFlags;
  232. meta: packed record
  233. Width: Integer; // absolut width of this line
  234. Height: Integer; // absolute height of this line
  235. Spacing: Integer; // spacing between lines
  236. Ascent: Integer; // text ascent
  237. SpaceCount: Integer; // number of words in this line
  238. end;
  239. end;
  240. TtsBlockFlag = (
  241. tsBlockFlagWordWrap
  242. );
  243. TtsBlockFlags = set of TtsBlockFlag;
  244. TtsClipping = (
  245. tsClipNone, // no clipping
  246. tsClipWordBorder, // draw all words that have at least one pixel inside the box
  247. tsClipCharBorder, // draw all chars that have at least one pixel inside the box
  248. tsClipWordComplete, // draw all words that are completly inside the box
  249. tsClipCharComplete // draw all chars that are completly inside the box
  250. );
  251. TtsTextBlock = class(TObject)
  252. private
  253. fRenderer: TtsRenderer;
  254. fTop: Integer;
  255. fLeft: Integer;
  256. fWidth: Integer;
  257. fHeight: Integer;
  258. fFlags: TtsBlockFlags;
  259. fVertAlign: TtsVertAlignment;
  260. fHorzAlign: TtsHorzAlignment;
  261. fClipping: TtsClipping;
  262. fCurrentColor: TtsColor4f;
  263. fCurrentFont: TtsFont;
  264. fFirstLine: PtsBlockLine;
  265. fLastLine: PtsBlockLine;
  266. function GetRect: TtsRect;
  267. function PushLineItem(const aItem: PtsLineItem; const aUpdateLineWidth: Boolean = true): Boolean;
  268. procedure PushSpacing(const aWidth: Integer);
  269. procedure FreeLineItem(var aItem: PtsLineItem);
  270. procedure FreeLineItems(var aItem: PtsLineItem);
  271. procedure FreeLines(var aItem: PtsBlockLine);
  272. function SplitText(aText: PWideChar): PtsLineItem;
  273. function SplitIntoLines(aItem: PtsLineItem): Boolean;
  274. procedure TrimSpaces(const aLine: PtsBlockLine);
  275. procedure UpdateLineMeta(const aLine: PtsBlockLine);
  276. protected
  277. property Lines: PtsBlockLine read fFirstLine;
  278. procedure PushNewLine;
  279. constructor Create(const aRenderer: TtsRenderer; const aTop, aLeft, aWidth, aHeight: Integer; const aFlags: TtsBlockFlags);
  280. public
  281. property Renderer: TtsRenderer read fRenderer;
  282. property CurrentColor: TtsColor4f read fCurrentColor;
  283. property CurrentFont: TtsFont read fCurrentFont;
  284. property Rect: TtsRect read GetRect;
  285. property Width: Integer read fWidth;
  286. property Height: Integer read fHeight;
  287. property Flags: TtsBlockFlags read fFlags;
  288. property Top: Integer read fTop write fTop;
  289. property Left: Integer read fLeft write fLeft;
  290. property VertAlign: TtsVertAlignment read fVertAlign write fVertAlign;
  291. property HorzAlign: TtsHorzAlignment read fHorzAlign write fHorzAlign;
  292. property Clipping: TtsClipping read fClipping write fClipping;
  293. procedure ChangeFont(const aFont: TtsFont);
  294. procedure ChangeColor(const aColor: TtsColor4f);
  295. function GetActualBlockHeight: Integer;
  296. procedure TextOutA(const aText: PAnsiChar);
  297. procedure TextOutW(const aText: PWideChar);
  298. destructor Destroy; override;
  299. end;
  300. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  301. TtsRenderer = class(TObject)
  302. private
  303. fContext: TtsContext;
  304. fFormat: TtsFormat;
  305. fSaveImages: Boolean;
  306. fRenderCS: TCriticalSection;
  307. fBlocks: TObjectList;
  308. procedure RegisterBlock(const aBlock: TtsTextBlock);
  309. procedure UnregisterBlock(const aBlock: TtsTextBlock);
  310. protected
  311. function CreateRenderRef(const aChar: TtsChar; const aCharImage: TtsImage): TtsCharRenderRef; virtual; abstract;
  312. procedure FreeRenderRef(const aCharRef: TtsCharRenderRef); virtual; abstract;
  313. procedure BeginRender; virtual;
  314. procedure EndRender; virtual;
  315. procedure SetDrawPos(const X, Y: Integer); virtual; abstract;
  316. function GetDrawPos: TtsPosition; virtual; abstract;
  317. procedure MoveDrawPos(const X, Y: Integer); virtual; abstract;
  318. procedure SetColor(const aColor: TtsColor4f); virtual; abstract;
  319. procedure Render(const aCharRef: TtsCharRenderRef); virtual; abstract;
  320. public
  321. property Context: TtsContext read fContext;
  322. property Format: TtsFormat read fFormat;
  323. property SaveImages: Boolean read fSaveImages write fSaveImages;
  324. function BeginBlock(const aTop, aLeft, aWidth, aHeight: Integer; const aFlags: TtsBlockFlags): TtsTextBlock;
  325. procedure EndBlock(var aBlock: TtsTextBlock);
  326. constructor Create(const aContext: TtsContext; const aFormat: TtsFormat);
  327. destructor Destroy; override;
  328. end;
  329. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  330. TtsContext = class(TObject)
  331. private
  332. fCodePage: TtsCodePage;
  333. fCodePageDefault: WideChar;
  334. fRenderers: TObjectList;
  335. fGenerators: TObjectList;
  336. private
  337. procedure RegisterRenderer(const aRenderer: TtsRenderer);
  338. procedure UnregisterRenderer(const aRenderer: TtsRenderer);
  339. procedure RegisterGenerator(const aGenerator: TtsFontGenerator);
  340. procedure UnregisterGenerator(const aGenerator: TtsFontGenerator);
  341. public
  342. property CodePage: TtsCodePage read fCodePage write fCodePage;
  343. property CodePageDefault: WideChar read fCodePageDefault write fCodePageDefault;
  344. function AnsiToWide(const aText: PAnsiChar): PWideChar;
  345. constructor Create;
  346. destructor Destroy; override;
  347. end;
  348. EtsException = class(Exception);
  349. EtsRenderer = class(EtsException);
  350. EtsOutOfRange = class(EtsException)
  351. public
  352. constructor Create(const aMin, aMax, aIndex: Integer);
  353. end;
  354. const
  355. IMAGE_MODES_REPLACE: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeReplace);
  356. IMAGE_MODES_NORMAL: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeModulate);
  357. COLOR_CHANNELS_RGB: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue];
  358. COLOR_CHANNELS_RGBA: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue, tsChannelAlpha];
  359. implementation
  360. const
  361. IMAGE_MODE_FUNCTIONS: array[TtsImageMode] of TtsImageModeFunc = (
  362. @tsImageModeFuncIgnore,
  363. @tsImageModeFuncReplace,
  364. @tsImageModeFuncModulate);
  365. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. //TtsKernel1D///////////////////////////////////////////////////////////////////////////////////////////////////////////
  367. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  368. constructor TtsKernel1D.Create(const aRadius, aStrength: Single);
  369. var
  370. TempRadius, SQRRadius, TempStrength, TempValue: Double;
  371. Idx: Integer;
  372. function CalcValue(const aIndex: Integer): Single;
  373. var
  374. Temp: Double;
  375. begin
  376. Temp := Max(0, Abs(aIndex) - TempStrength);
  377. Temp := Sqr(Temp * TempRadius) / SQRRadius;
  378. result := Exp(-Temp);
  379. end;
  380. begin
  381. inherited Create;
  382. // calculate new radius and strength
  383. TempStrength := Min(aRadius - 1, aRadius * aStrength);
  384. TempRadius := aRadius - TempStrength;
  385. SQRRadius := sqr(TempRadius) * sqr(TempRadius);
  386. // caluculating size of the kernel
  387. Size := Round(TempRadius);
  388. while CalcValue(Size) > 0.001 do
  389. Inc(Size);
  390. Size := Size -1;
  391. ItemCount := Size * 2 +1;
  392. SetLength(Items, ItemCount);
  393. // calculate Value (yes thats right. there is no -1)
  394. for Idx := 0 to Size do begin
  395. TempValue := CalcValue(Idx);
  396. with Items[Size + Idx] do begin
  397. Offset := Idx;
  398. Value := TempValue;
  399. end;
  400. with Items[Size - Idx] do begin
  401. Offset := -Idx;
  402. Value := TempValue;
  403. end;
  404. end;
  405. end;
  406. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  407. //TtsKernel2D///////////////////////////////////////////////////////////////////////////////////////////////////////////
  408. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  409. constructor TtsKernel2D.Create(const aRadius, aStrength: Single);
  410. var
  411. tmpStrenght: Double;
  412. tmpRadius: Double;
  413. tmpValue: Double;
  414. sqrRadius: Double;
  415. x, y, w, h: Integer;
  416. function CalcValue(const aIndex: Double): Double;
  417. begin
  418. result := max(0, Abs(aIndex) - tmpStrenght);
  419. result := Sqr(result * tmpRadius) / sqrRadius;
  420. result := Exp(-result);
  421. end;
  422. procedure CalcSize(var aSize, aMidSize: Integer);
  423. begin
  424. aSize := 0;
  425. aMidSize := 0;
  426. while CalcValue(aSize) > 0.5 do begin
  427. inc(aSize, 1);
  428. inc(aMidSize, 1);
  429. end;
  430. while CalcValue(aSize) > 0.001 do
  431. Inc(aSize, 1);
  432. end;
  433. procedure SetItem(const x, y: Integer);
  434. begin
  435. with Items[(SizeY + y) * w + (SizeX + x)] do begin
  436. OffsetX := x;
  437. OffsetY := y;
  438. Value := tmpValue;
  439. end;
  440. end;
  441. procedure QuickSort(l, r: Integer);
  442. var
  443. _l, _r: Integer;
  444. p, t: TtsKernel2DItem;
  445. begin
  446. repeat
  447. _l := l;
  448. _r := r;
  449. p := Items[(l + r) shr 1];
  450. repeat
  451. while (Items[_l].Value > p.Value) do
  452. inc(_l, 1);
  453. while (Items[_r].Value < p.Value) do
  454. dec(_r, 1);
  455. if (_l <= _r) then begin
  456. t := Items[_l];
  457. Items[_l] := Items[_r];
  458. Items[_r] := t;
  459. inc(_l, 1);
  460. dec(_r, 1);
  461. end;
  462. until (_l > _r);
  463. if (l < _r) then
  464. QuickSort(l, _r);
  465. l := _l;
  466. until (_l >= r);
  467. end;
  468. begin
  469. inherited Create;
  470. tmpStrenght := Min(aRadius - 1.0, aRadius * aStrength);
  471. tmpRadius := aRadius - tmpStrenght;
  472. sqrRadius := sqr(tmpRadius) * sqr(tmpRadius);
  473. CalcSize(SizeX, MidSizeX);
  474. CalcSize(SizeY, MidSizeY);
  475. ValueSum := 0.0;
  476. w := 2 * SizeX + 1;
  477. h := 2 * SizeY + 1;
  478. ItemCount := w * h;
  479. SetLength(Items, ItemCount);
  480. for y := 0 to SizeY do begin
  481. for x := 0 to SizeX do begin
  482. tmpValue := CalcValue(sqrt(Sqr(x) + Sqr(y)));
  483. SetItem( x, y);
  484. SetItem( x, -y);
  485. SetItem(-x, -y);
  486. SetItem(-x, y);
  487. ValueSum := ValueSum + tmpValue;
  488. if (x > 0) and (y > 0) then
  489. ValueSum := ValueSum + tmpValue;
  490. end;
  491. end;
  492. QuickSort(0, ItemCount-1);
  493. while (Items[ItemCount-1].Value < 0.001) do
  494. dec(ItemCount, 1);
  495. SetLength(Items, ItemCount);
  496. end;
  497. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  498. //TtsImage//////////////////////////////////////////////////////////////////////////////////////////////////////////////
  499. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  500. function TtsImage.GetScanline(const aIndex: Integer): Pointer;
  501. begin
  502. if not fHasScanlines then
  503. UpdateScanlines;
  504. if fHasScanlines and (aIndex >= 0) and (aIndex <= High(fScanlines)) then
  505. result := fScanlines[aIndex]
  506. else
  507. result := nil;
  508. end;
  509. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  510. function TtsImage.GetIsEmpty: Boolean;
  511. begin
  512. result := not Assigned(fData);
  513. end;
  514. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  515. procedure TtsImage.SetData(const aData: Pointer; const aFormat: TtsFormat; const aWidth: Integer;
  516. const aHeight: Integer; const aLineSize: Integer; const aDataSize: Integer);
  517. begin
  518. fHasScanlines := false;
  519. if Assigned(fData) then
  520. FreeMemory(fData);
  521. fData := aData;
  522. if Assigned(fData) then begin
  523. fWidth := aWidth;
  524. fHeight := aHeight;
  525. fFormat := aFormat;
  526. fLineSize := aLineSize;
  527. fDataSize := aDataSize;
  528. end else begin
  529. fWidth := 0;
  530. fHeight := 0;
  531. fLineSize := 0;
  532. fDataSize := 0;
  533. fFormat := tsFormatEmpty;
  534. end;
  535. end;
  536. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  537. procedure TtsImage.UpdateScanlines;
  538. var
  539. i: Integer;
  540. tmp: PByte;
  541. begin
  542. SetLength(fScanlines, fHeight);
  543. for i := 0 to fHeight-1 do begin
  544. tmp := fData;
  545. inc(tmp, i * fLineSize);
  546. fScanlines[i] := tmp;
  547. end;
  548. fHasScanlines := true;
  549. end;
  550. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  551. function TtsImage.GetPixelAt(const x, y: Integer; out aColor: TtsColor4f): Boolean;
  552. var
  553. p: PByte;
  554. begin
  555. result := (x >= 0) and (x < Width) and (y >= 0) and (y < Height);
  556. if result then begin
  557. p := Scanline[y];
  558. inc(p, x * tsFormatSize(Format));
  559. tsFormatUnmap(Format, p, aColor);
  560. end;
  561. end;
  562. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  563. procedure TtsImage.Assign(const aImage: TtsImage);
  564. var
  565. ImgData: Pointer;
  566. begin
  567. GetMem(ImgData, aImage.DataSize);
  568. if Assigned(ImgData) then
  569. Move(aImage.Data^, ImgData^, aImage.DataSize);
  570. SetData(ImgData, aImage.Format, aImage.Width, aImage.Height, aImage.LineSize, aImage.DataSize);
  571. end;
  572. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  573. procedure TtsImage.CreateEmpty(const aFormat: TtsFormat; const aWidth, aHeight: Integer);
  574. var
  575. ImgData: PByte;
  576. lSize, dSize: Integer;
  577. begin
  578. lSize := aWidth * tsFormatSize(aFormat);
  579. lSize := lSize + ((4 - (lSize mod 4)) mod 4);
  580. dSize := aHeight * lSize;
  581. ImgData := AllocMem(dSize);
  582. FillByte(ImgData^, dSize, 0);
  583. SetData(ImgData, aFormat, aWidth, aHeight, lSize, dSize);
  584. end;
  585. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  586. procedure TtsImage.LoadFromFunc(const aFunc: TtsImageFunc; const aArgs: Pointer);
  587. var
  588. X, Y: Integer;
  589. c: TtsColor4f;
  590. p, tmp: PByte;
  591. begin
  592. for Y := 0 to Height - 1 do begin
  593. p := ScanLine[Y];
  594. for X := 0 to Width - 1 do begin
  595. tmp := p;
  596. tsFormatUnmap(fFormat, tmp, c);
  597. aFunc(Self, X, Y, c, aArgs);
  598. tsFormatMap(fFormat, p, c);
  599. end;
  600. end;
  601. end;
  602. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  603. procedure TtsImage.Resize(const aNewWidth, aNewHeight, X, Y: Integer);
  604. var
  605. ImgData: PByte;
  606. pSize, lSize, dSize: Integer;
  607. src, dst: PByte;
  608. YStart, YEnd, YPos, XStart, XEnd: Integer;
  609. begin
  610. if (aNewHeight = 0) or (aNewWidth = 0) then begin
  611. SetData(nil);
  612. exit;
  613. end;
  614. pSize := tsFormatSize(Format);
  615. lSize := pSize * aNewWidth;
  616. lSize := lSize + ((4 - (lSize mod 4)) mod 4);
  617. dSize := lSize * aNewHeight;
  618. GetMem(ImgData, dSize);
  619. try
  620. FillChar(ImgData^, dSize, 0);
  621. // positions
  622. YStart := Max(0, Y);
  623. YEnd := Min(aNewHeight, Y + Height);
  624. XStart := Max(0, X);
  625. XEnd := Min(aNewWidth, X + Width);
  626. // copy data
  627. for YPos := YStart to YEnd -1 do begin
  628. dst := ImgData;
  629. Inc(dst, lSize * YPos + pSize * XStart);
  630. src := fData;
  631. Inc(src, fLineSize * (YPos - Y) + pSize * (XStart - X));
  632. Move(src^, dst^, (XEnd - XStart) * pSize);
  633. end;
  634. // assign
  635. SetData(ImgData, Format, aNewWidth, aNewHeight, lSize, dSize);
  636. except
  637. FreeMem(ImgData);
  638. end;
  639. end;
  640. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  641. procedure TtsImage.FindMinMax(out aRect: TtsRect);
  642. var
  643. X, Y: Integer;
  644. c: TtsColor4f;
  645. p: PByte;
  646. begin
  647. aRect.Top := -1;
  648. aRect.Left := -1;
  649. aRect.Right := -1;
  650. aRect.Bottom := -1;
  651. // Search for MinMax
  652. for Y := 0 to Height-1 do begin
  653. p := ScanLine[Y];
  654. for X := 0 to Width-1 do begin
  655. tsFormatUnmap(Format, p, c);
  656. if c.a > 0 then begin
  657. if (X < aRect.Left) or (aRect.Left = -1) then
  658. aRect.Left := X;
  659. if (X+1 > aRect.Right) or (aRect.Right = -1) then
  660. aRect.Right := X+1;
  661. if (Y < aRect.Top) or (aRect.Top = -1) then
  662. aRect.Top := Y;
  663. if (Y+1 > aRect.Bottom) or (aRect.Bottom = -1) then
  664. aRect.Bottom := Y+1;
  665. end;
  666. end;
  667. end;
  668. end;
  669. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  670. procedure TtsImage.FillColor(const aColor: TtsColor4f; const aChannelMask: TtsColorChannels; const aModes: TtsImageModes);
  671. var
  672. x, y: Integer;
  673. rp, wp: PByte;
  674. c: TtsColor4f;
  675. ch: TtsColorChannel;
  676. i: Integer;
  677. begin
  678. for y := 0 to Height-1 do begin
  679. rp := Scanline[y];
  680. wp := rp;
  681. for x := 0 to Width-1 do begin
  682. tsFormatUnmap(Format, rp, c);
  683. for i := 0 to 3 do begin
  684. ch := TtsColorChannel(i);
  685. if (ch in aChannelMask) then
  686. c.arr[i] := IMAGE_MODE_FUNCTIONS[aModes[ch]](aColor.arr[i], c.arr[i]);
  687. end;
  688. tsFormatMap(Format, wp, c);
  689. end;
  690. end;
  691. end;
  692. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  693. procedure TtsImage.FillPattern(const aPattern: TtsImage; X, Y: Integer; const aChannelMask: TtsColorChannels; const aModes: TtsImageModes);
  694. var
  695. _x, _y, posX, i: Integer;
  696. src, dst, tmp: PByte;
  697. cSrc, cDst: TtsColor4f;
  698. ch: TtsColorChannel;
  699. begin
  700. if x < 0 then
  701. x := Random(aPattern.Width);
  702. if y < 0 then
  703. y := Random(aPattern.Height);
  704. for _y := 0 to Height-1 do begin
  705. src := aPattern.Scanline[(y + _y) mod aPattern.Height];
  706. dst := Scanline[_y];
  707. inc(src, x);
  708. posX := x;
  709. for _x := 0 to Width-1 do begin
  710. if (posX >= aPattern.Width) then begin
  711. src := aPattern.Scanline[(y + _y) mod aPattern.Height];
  712. posX := 0;
  713. end;
  714. tmp := dst;
  715. tsFormatUnmap(aPattern.Format, src, cSrc);
  716. tsFormatUnmap(Format, tmp, cDst);
  717. for i := 0 to 3 do begin
  718. ch := TtsColorChannel(i);
  719. if (ch in aChannelMask) then
  720. cDst.arr[i] := IMAGE_MODE_FUNCTIONS[aModes[ch]](cSrc.arr[i], cDst.arr[i]);
  721. end;
  722. tsFormatMap(Format, dst, cDst);
  723. inc(posX);
  724. end;
  725. end;
  726. end;
  727. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  728. procedure TtsImage.Blend(const aImage: TtsImage; const X, Y: Integer; const aFunc: TtsBlendFunc);
  729. var
  730. _x, _y, x1, x2, y1, y2: Integer;
  731. src, dst, tmp: PByte;
  732. srcColor, dstColor: TtsColor4f;
  733. srcPixelSize, dstPixelSize: Integer;
  734. begin
  735. x1 := Max(X, 0);
  736. x2 := Min(X + aImage.Width , Width);
  737. y1 := Max(Y, 0);
  738. y2 := Min(Y + aImage.Height, Height);
  739. srcPixelSize := tsFormatSize(aImage.Format);
  740. dstPixelSize := tsFormatSize(Format);
  741. for _y := y1 to y2-1 do begin
  742. src := aImage.Scanline[_y - min(y1, y)];
  743. dst := Scanline[_y];
  744. inc(src, (x1 - x) * srcPixelSize);
  745. inc(dst, x1 * dstPixelSize);
  746. tmp := dst;
  747. for _x := x1 to x2-1 do begin
  748. tsFormatUnmap(aImage.Format, src, srcColor);
  749. tsFormatUnmap( Format, dst, dstColor);
  750. tsFormatMap(aImage.Format, tmp, aFunc(srcColor, dstColor));
  751. end;
  752. end;
  753. end;
  754. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  755. procedure TtsImage.Blur(const aHorzKernel, aVertKernel: TtsKernel1D; const aChannelMask: TtsColorChannels);
  756. var
  757. tmpImage: TtsImage;
  758. procedure DoBlur(const aSrc, aDst: TtsImage; const aKernel: TtsKernel1D; const ShiftX, ShiftY: Integer);
  759. var
  760. x, y, i, j: Integer;
  761. src, dst: PByte;
  762. v: Single;
  763. c, tmp: TtsColor4f;
  764. begin
  765. for y := 0 to Height-1 do begin
  766. src := aSrc.Scanline[y];
  767. dst := aDst.Scanline[y];
  768. for x := 0 to Width-1 do begin
  769. // read color and clear channels
  770. v := 0;
  771. tsFormatUnmap(aSrc.Format, src, c);
  772. for j := 0 to 3 do
  773. if (TtsColorChannel(j) in aChannelMask) then
  774. c.arr[j] := 0;
  775. // do blur
  776. for i := 0 to aKernel.ItemCount-1 do with aKernel.Items[i] do begin
  777. if aSrc.GetPixelAt(x + Offset * ShiftX, y + Offset * ShiftY, tmp) then begin
  778. for j := 0 to 3 do begin
  779. if (TtsColorChannel(j) in aChannelMask) then
  780. c.arr[j] := c.arr[j] + tmp.arr[j] * Value;
  781. end;
  782. v := v + Value;
  783. end;
  784. end;
  785. // calc final color and write
  786. for j := 0 to 3 do
  787. if (TtsColorChannel(i) in aChannelMask) then
  788. c.arr[j] := c.arr[j] / v;
  789. tsFormatMap(aDst.Format, dst, c);
  790. end;
  791. end;
  792. end;
  793. begin
  794. tmpImage := TtsImage.Create;
  795. try
  796. tmpImage.CreateEmpty(Format, Width, Height);
  797. tmpImage.FillColor(tsColor4f(1, 1, 1, 0), COLOR_CHANNELS_RGBA, IMAGE_MODES_REPLACE);
  798. DoBlur(self, tmpImage, aHorzKernel, 1, 0);
  799. DoBlur(tmpImage, self, aVertKernel, 0, 1);
  800. finally
  801. FreeAndNil(tmpImage);
  802. end;
  803. end;
  804. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  805. constructor TtsImage.Create;
  806. begin
  807. inherited Create;
  808. SetData(nil);
  809. end;
  810. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  811. destructor TtsImage.Destroy;
  812. begin
  813. SetData(nil);
  814. inherited Destroy;
  815. end;
  816. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  817. //TtsChar///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  818. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  819. constructor TtsChar.Create(const aCharCode: WideChar);
  820. begin
  821. inherited Create;
  822. fCharCode := aCharCode;
  823. fGlyphOrigin := tsPosition(0, 0);
  824. fGlyphRect := tsRect(0, 0, 0, 0);
  825. fAdvance := 0;
  826. fRenderRef := nil;
  827. end;
  828. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  829. //TtsFont///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  830. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  831. function TtsFont.HasChar(const aCharCode: WideChar): Boolean;
  832. begin
  833. result := Assigned(GetChar(aCharCode));
  834. end;
  835. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  836. function TtsFont.GetChar(const aCharCode: WideChar): TtsChar;
  837. var
  838. Chars: PtsFontCharArray;
  839. begin
  840. Chars := fChars[(Ord(aCharCode) shr 8) and $FF];
  841. if Assigned(Chars) then
  842. result := Chars^.Chars[Ord(aCharCode) and $FF]
  843. else
  844. result := nil;
  845. end;
  846. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  847. function TtsFont.GetCharCreate(const aCharCode: WideChar): TtsChar;
  848. begin
  849. result := GetChar(aCharCode);
  850. if not Assigned(result) then
  851. result := AddChar(aCharCode);
  852. end;
  853. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  854. procedure TtsFont.AddChar(const aCharCode: WideChar; const aChar: TtsChar);
  855. var
  856. h, l: Integer;
  857. Chars: PtsFontCharArray;
  858. begin
  859. h := (Ord(aCharCode) shr 8) and $FF;
  860. Chars := fChars[h];
  861. if not Assigned(Chars) then begin
  862. New(Chars);
  863. FillChar(Chars^, SizeOf(Chars^), 0);
  864. fChars[h] := Chars;
  865. end;
  866. if Assigned(Chars) then begin
  867. l := Ord(aCharCode) and $FF;
  868. Chars^.Chars[l] := aChar;
  869. inc(Chars^.CharCount);
  870. end;
  871. end;
  872. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  873. constructor TtsFont.Create(const aRenderer: TtsRenderer; const aGenerator: TtsFontGenerator; const aProperties: TtsFontProperties);
  874. begin
  875. inherited Create;
  876. fRenderer := aRenderer;
  877. fGenerator := aGenerator;
  878. fProperties := aProperties;
  879. fCharSpacing := 0;
  880. fTabWidth := 0;
  881. fLineSpacing := 0.0;
  882. fCreateChars := true;
  883. fGenerator.RegisterFont(self);
  884. end;
  885. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  886. function TtsFont.AddChar(const aCharCode: WideChar): TtsChar;
  887. begin
  888. result := GetChar(aCharCode);
  889. if not Assigned(result) and fCreateChars then begin
  890. result := fGenerator.GenerateChar(aCharCode, self, fRenderer);
  891. if Assigned(result) then
  892. AddChar(aCharCode, result);
  893. end;
  894. end;
  895. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  896. procedure TtsFont.AddCharRange(const aCharCodeBeg, aCharCodeEnd: WideChar);
  897. var
  898. c: WideChar;
  899. begin
  900. for c := aCharCodeBeg to aCharCodeEnd do
  901. AddChar(c);
  902. end;
  903. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  904. procedure TtsFont.RemoveChar(const aCharCode: WideChar);
  905. var
  906. h, l: Integer;
  907. Chars: PtsFontCharArray;
  908. c: TtsChar;
  909. begin
  910. // find char array
  911. h := (Ord(aCharCode) shr 8) and $FF;
  912. Chars := fChars[h];
  913. if not Assigned(Chars) then
  914. exit;
  915. // find char
  916. l := Ord(aCharCode) and $FF;
  917. c := Chars^.Chars[l];
  918. if not Assigned(c) then
  919. exit;
  920. // remove char
  921. Chars^.Chars[l] := nil;
  922. dec(Chars^.CharCount);
  923. if (Chars^.CharCount <= 0) then begin
  924. fChars[h] := nil;
  925. Dispose(Chars);
  926. end;
  927. if Assigned(c.RenderRef) then begin
  928. fRenderer.FreeRenderRef(c.RenderRef);
  929. c.RenderRef := nil;
  930. end;
  931. FreeAndNil(c);
  932. end;
  933. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  934. procedure TtsFont.ClearChars;
  935. var
  936. h, l: Integer;
  937. Chars: PtsFontCharArray;
  938. c: TtsChar;
  939. begin
  940. for h := Low(fChars) to High(fChars) do begin
  941. Chars := fChars[h];
  942. if Assigned(Chars) then begin
  943. for l := Low(Chars^.Chars) to High(Chars^.Chars) do begin
  944. c := Chars^.Chars[l];
  945. if Assigned(c) then begin
  946. if Assigned(c.RenderRef) then
  947. fRenderer.FreeRenderRef(c.RenderRef);
  948. FreeAndNil(c);
  949. end;
  950. end;
  951. Dispose(Chars);
  952. fChars[h] := nil;
  953. end;
  954. end;
  955. end;
  956. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  957. function TtsFont.GetTextWidthW(aText: PWideChar): Integer;
  958. var
  959. c: TtsChar;
  960. begin
  961. result := 0;
  962. if not Assigned(aText) then
  963. exit;
  964. while (aText^ <> #0) do begin
  965. c := AddChar(aText^);
  966. if not Assigned(c) then
  967. c := AddChar(fProperties.DefaultChar);
  968. if Assigned(c) then begin
  969. if (result > 0) then
  970. result := result + CharSpacing;
  971. result := result + c.Advance;
  972. end;
  973. inc(aText);
  974. end;
  975. end;
  976. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  977. function TtsFont.GetTextWidthA(aText: PAnsiChar): Integer;
  978. var
  979. tmp: PWideChar;
  980. begin
  981. tmp := fGenerator.Context.AnsiToWide(aText);
  982. try
  983. result := GetTextWidthW(tmp);
  984. finally
  985. tsStrDispose(tmp);
  986. end;
  987. end;
  988. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  989. procedure TtsFont.GetTextMetric(out aMetric: TtsTextMetric);
  990. begin
  991. aMetric.Ascent := fProperties.Ascent;
  992. aMetric.Descent := fProperties.Descent;
  993. aMetric.ExternalLeading := fProperties.ExternalLeading;
  994. aMetric.BaseLineOffset := fProperties.BaseLineOffset;
  995. aMetric.CharSpacing := CharSpacing;
  996. aMetric.LineHeight := fProperties.Ascent + fProperties.Descent + fProperties.ExternalLeading;
  997. aMetric.LineSpacing := Trunc(fProperties.Size * fLineSpacing);
  998. end;
  999. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1000. destructor TtsFont.Destroy;
  1001. begin
  1002. fGenerator.UnregisterFont(self);
  1003. ClearChars;
  1004. inherited Destroy;
  1005. end;
  1006. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1007. //TtsPostProcessStep////////////////////////////////////////////////////////////////////////////////////////////////////
  1008. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1009. procedure TtsPostProcessStep.ClearList(const aList: TList);
  1010. var
  1011. i: Integer;
  1012. p: PtsPostProcessStepRange;
  1013. begin
  1014. for i := 0 to aList.Count-1 do begin
  1015. p := aList[i];
  1016. Dispose(p);
  1017. end;
  1018. aList.Clear;
  1019. end;
  1020. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1021. function TtsPostProcessStep.IsInRange(const aCharCode: WideChar): Boolean;
  1022. var
  1023. i: Integer;
  1024. p: PtsPostProcessStepRange;
  1025. begin
  1026. result := (fIncludeCharRange.Count = 0);
  1027. if not result then for i := 0 to fIncludeCharRange.Count-1 do begin
  1028. p := fIncludeCharRange[i];
  1029. if (aCharCode >= p^.StartChar) and (aCharCode <= p^.EndChar) then begin
  1030. result := true;
  1031. break;
  1032. end;
  1033. end;
  1034. if result then for i := 0 to fExcludeCharRange.Count-1 do begin
  1035. p := fExcludeCharRange[i];
  1036. if (aCharCode >= p^.StartChar) and (aCharCode <= p^.EndChar) then begin
  1037. result := false;
  1038. break;
  1039. end;
  1040. end;
  1041. end;
  1042. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1043. procedure TtsPostProcessStep.AddUsageRange(const aUsage: TtsFontProcessStepUsage; const aStartChar, aEndChar: WideChar);
  1044. var
  1045. p: PtsPostProcessStepRange;
  1046. begin
  1047. New(p);
  1048. p^.StartChar := aStartChar;
  1049. p^.EndChar := aEndChar;
  1050. case aUsage of
  1051. tsUsageInclude:
  1052. fIncludeCharRange.Add(p);
  1053. tsUsageExclude:
  1054. fExcludeCharRange.Add(p);
  1055. end;
  1056. end;
  1057. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1058. procedure TtsPostProcessStep.AddUsageChars(const aUsage: TtsFontProcessStepUsage; aChars: PWideChar);
  1059. begin
  1060. if Assigned(aChars) then
  1061. while (aChars^ <> #0) do begin
  1062. AddUsageRange(aUsage, aChars^, aChars^);
  1063. inc(aChars);
  1064. end;
  1065. end;
  1066. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1067. procedure TtsPostProcessStep.ClearIncludeRange;
  1068. begin
  1069. ClearList(fIncludeCharRange);
  1070. end;
  1071. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1072. procedure TtsPostProcessStep.ClearExcludeRange;
  1073. begin
  1074. ClearList(fExcludeCharRange);
  1075. end;
  1076. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1077. constructor TtsPostProcessStep.Create;
  1078. begin
  1079. inherited Create;
  1080. fIncludeCharRange := TList.Create;
  1081. fExcludeCharRange := TList.Create;
  1082. end;
  1083. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1084. destructor TtsPostProcessStep.Destroy;
  1085. begin
  1086. ClearList(fIncludeCharRange);
  1087. ClearList(fExcludeCharRange);
  1088. FreeAndNil(fIncludeCharRange);
  1089. FreeAndNil(fExcludeCharRange);
  1090. inherited Destroy;
  1091. end;
  1092. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1093. //TtsFontGenerator//////////////////////////////////////////////////////////////////////////////////////////////////////////
  1094. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1095. function TtsFontGenerator.GetPostProcessStepCount: Integer;
  1096. begin
  1097. result := fPostProcessSteps.Count;
  1098. end;
  1099. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1100. function TtsFontGenerator.GetPostProcessStep(const aIndex: Integer): TtsPostProcessStep;
  1101. begin
  1102. if (aIndex >= 0) and (aIndex < fPostProcessSteps.Count) then
  1103. Result := TtsPostProcessStep(fPostProcessSteps[aIndex])
  1104. else
  1105. raise EtsOutOfRange.Create(0, fPostProcessSteps.Count-1, aIndex);
  1106. end;
  1107. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1108. procedure TtsFontGenerator.DrawLine(const aChar: TtsChar; const aCharImage: TtsImage; aLinePosition, aLineSize: Integer);
  1109. var
  1110. NewSize, NewPos: TtsPosition;
  1111. YOffset, y: Integer;
  1112. procedure FillLine(aData: PByte);
  1113. var
  1114. w, i: Integer;
  1115. c: TtsColor4f;
  1116. tmp: PByte;
  1117. begin
  1118. w := NewSize.x;
  1119. while (w > 0) do begin
  1120. tmp := aData;
  1121. tsFormatUnmap(aCharImage.Format, tmp, c);
  1122. for i := 0 to 3 do
  1123. c.arr[i] := 1.0;
  1124. tsFormatMap(aCharImage.Format, aData, c);
  1125. dec(w);
  1126. end;
  1127. end;
  1128. begin
  1129. if aLineSize <= 0 then
  1130. exit;
  1131. aLinePosition := aLinePosition - aLineSize;
  1132. // calculate width and height
  1133. NewPos.x := 0;
  1134. NewPos.y := 0;
  1135. NewSize.x := aCharImage.Width;
  1136. NewSize.y := aCharImage.Height;
  1137. // expand image to the full advance
  1138. if aChar.Advance > aCharImage.Width then
  1139. NewSize.x := aChar.Advance;
  1140. // add glyph position to image width and set position
  1141. if aChar.GlyphOrigin.x > aChar.GlyphRect.Left then begin
  1142. NewSize.x := NewSize.x + aChar.GlyphOrigin.x;
  1143. NewPos.x := aChar.GlyphOrigin.x;
  1144. end;
  1145. if (aChar.GlyphOrigin.x < 0) then
  1146. NewSize.x := NewSize.x - aChar.GlyphOrigin.x;
  1147. // line is under the image
  1148. if aLinePosition < (aChar.GlyphOrigin.y - aCharImage.Height) then
  1149. NewSize.y := NewSize.y + (aChar.GlyphOrigin.y - aCharImage.Height - aLinePosition);
  1150. // line is above the image
  1151. if aLinePosition + aLineSize > aChar.GlyphOrigin.y then begin
  1152. NewPos.y := ((aLinePosition + aLineSize) - aChar.GlyphOrigin.y);
  1153. NewSize.y := NewSize.y + NewPos.y;
  1154. end;
  1155. // resize
  1156. aCharImage.Resize(NewSize.x, NewSize.y, NewPos.x, NewPos.y);
  1157. // draw lines
  1158. YOffset := (aChar.GlyphOrigin.y + NewPos.y) - aLinePosition;
  1159. for y := 1 to aLineSize do
  1160. FillLine(aCharImage.ScanLine[YOffset - y]);
  1161. // move glyph rect
  1162. aChar.GlyphRect := tsRect(
  1163. aChar.GlyphRect.Left + NewPos.x,
  1164. aChar.GlyphRect.Top + NewPos.y,
  1165. aChar.GlyphRect.Right + NewPos.x,
  1166. aChar.GlyphRect.Bottom + NewPos.y);
  1167. end;
  1168. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1169. procedure TtsFontGenerator.DoPostProcess(const aChar: TtsChar; const aCharImage: TtsImage);
  1170. var
  1171. i: Integer;
  1172. step: TtsPostProcessStep;
  1173. begin
  1174. if not aCharImage.IsEmpty then begin
  1175. for i := 0 to fPostProcessSteps.Count-1 do begin
  1176. step := TtsPostProcessStep(fPostProcessSteps[i]);
  1177. if step.IsInRange(aChar.CharCode) then
  1178. step.Execute(aChar, aCharImage);
  1179. end;
  1180. end;
  1181. end;
  1182. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1183. procedure TtsFontGenerator.RegisterFont(const aFont: TtsFont);
  1184. begin
  1185. fFonts.Add(aFont);
  1186. end;
  1187. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1188. procedure TtsFontGenerator.UnregisterFont(const aFont: TtsFont);
  1189. begin
  1190. if Assigned(fFonts) then
  1191. fFonts.Remove(aFont);
  1192. end;
  1193. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1194. function TtsFontGenerator.GenerateChar(const aCharCode: WideChar; const aFont: TtsFont; const aRenderer: TtsRenderer): TtsChar;
  1195. var
  1196. GlyphOrigin, GlyphSize: TtsPosition;
  1197. Advance: Integer;
  1198. CharImage: TtsImage;
  1199. begin
  1200. result := nil;
  1201. if not GetGlyphMetrics(aFont, aCharCode, GlyphOrigin, GlyphSize, Advance) or
  1202. not ((GlyphOrigin.x <> 0) or (GlyphOrigin.y <> 0) or (GlyphSize.x <> 0) or (GlyphSize.y <> 0) or (Advance <> 0)) then
  1203. exit;
  1204. CharImage := TtsImage.Create;
  1205. try
  1206. if aRenderer.SaveImages then begin
  1207. if (GlyphSize.x > 0) and (GlyphSize.y > 0) then
  1208. GetCharImage(aFont, aCharCode, CharImage);
  1209. if CharImage.IsEmpty and ([tsStyleUnderline, tsStyleStrikeout] * aFont.Properties.Style <> []) then begin
  1210. CharImage.CreateEmpty(aRenderer.Format, max(Advance, 1), 1);
  1211. GlyphOrigin.y := 1;
  1212. end;
  1213. end;
  1214. result := TtsChar.Create(aCharCode);
  1215. try
  1216. result.GlyphOrigin := GlyphOrigin;
  1217. result.GlyphRect := tsRect(0, 0, CharImage.Width, CharImage.Height);
  1218. result.Advance := Advance;
  1219. if (aRenderer.SaveImages) then begin
  1220. try
  1221. if (tsStyleUnderline in aFont.Properties.Style) then
  1222. DrawLine(result, CharImage, aFont.Properties.UnderlinePos, aFont.Properties.UnderlineSize);
  1223. if (tsStyleStrikeout in aFont.Properties.Style) then
  1224. DrawLine(result, CharImage, aFont.Properties.StrikeoutPos, aFont.Properties.StrikeoutSize);
  1225. except
  1226. CharImage.FillColor(tsColor4f(1, 0, 0, 0), COLOR_CHANNELS_RGB, IMAGE_MODES_NORMAL);
  1227. end;
  1228. DoPostProcess(result, CharImage);
  1229. result.RenderRef := aRenderer.CreateRenderRef(result, CharImage);
  1230. end;
  1231. except
  1232. FreeAndNil(result);
  1233. end;
  1234. finally
  1235. FreeAndNil(CharImage);
  1236. end;
  1237. end;
  1238. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1239. function TtsFontGenerator.AddPostProcessStep(const aStep: TtsPostProcessStep): TtsPostProcessStep;
  1240. begin
  1241. result := aStep;
  1242. fPostProcessSteps.Add(aStep);
  1243. end;
  1244. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1245. function TtsFontGenerator.InsertPostProcessStep(const aIndex: Integer; const aStep: TtsPostProcessStep): TtsPostProcessStep;
  1246. begin
  1247. result := aStep;
  1248. fPostProcessSteps.Insert(aIndex, aStep);
  1249. end;
  1250. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1251. procedure TtsFontGenerator.DeletePostProcessStep(const aIndex: Integer);
  1252. begin
  1253. if (aIndex >= 0) and (aIndex < fPostProcessSteps.Count) then
  1254. fPostProcessSteps.Delete(aIndex)
  1255. else
  1256. raise EtsOutOfRange.Create(0, fPostProcessSteps.Count-1, aIndex);
  1257. end;
  1258. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1259. procedure TtsFontGenerator.ClearPostProcessSteps;
  1260. begin
  1261. fPostProcessSteps.Clear;
  1262. end;
  1263. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1264. constructor TtsFontGenerator.Create(const aContext: TtsContext);
  1265. begin
  1266. inherited Create;
  1267. fContext := aContext;
  1268. fFonts := TObjectList.Create(false);
  1269. fPostProcessSteps := TObjectList.Create(true);
  1270. fContext.RegisterGenerator(self);
  1271. end;
  1272. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1273. destructor TtsFontGenerator.Destroy;
  1274. begin
  1275. ClearPostProcessSteps;
  1276. fContext.UnregisterGenerator(self);
  1277. fFonts.OwnsObjects := true;
  1278. FreeAndNil(fFonts);
  1279. FreeAndNil(fPostProcessSteps);
  1280. inherited Destroy;
  1281. end;
  1282. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1283. //TtsTextBlock//////////////////////////////////////////////////////////////////////////////////////////////////////////
  1284. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1285. function TtsTextBlock.GetRect: TtsRect;
  1286. begin
  1287. result.Left := fLeft;
  1288. result.Top := fTop;
  1289. result.Right := fLeft + fWidth;
  1290. result.Bottom := fTop + fHeight;
  1291. end;
  1292. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1293. function TtsTextBlock.PushLineItem(const aItem: PtsLineItem; const aUpdateLineWidth: Boolean): Boolean;
  1294. begin
  1295. result := false;
  1296. if not Assigned(fLastLine) then
  1297. PushNewLine;
  1298. if not Assigned(fLastLine^.First) and
  1299. (aItem^.ItemType in [tsItemTypeSpace, tsItemTypeSpacing]) then
  1300. exit; // di not add line space or line spacing if line is empty
  1301. if Assigned(fLastLine^.Last) then begin
  1302. aItem^.Prev := fLastLine^.Last;
  1303. aItem^.Next := nil;
  1304. fLastLine^.Last^.Next := aItem;
  1305. fLastLine^.Last := aItem;
  1306. end;
  1307. if not Assigned(fLastLine^.First) then begin
  1308. fLastLine^.First := aItem;
  1309. fLastLine^.Last := aItem;
  1310. end;
  1311. case aItem^.ItemType of
  1312. tsItemTypeSpace, tsItemTypeText:
  1313. fLastLine^.meta.Width := fLastLine^.meta.Width + aItem^.TextWidth;
  1314. tsItemTypeSpacing:
  1315. fLastLine^.meta.Width := fLastLine^.meta.Width + aItem^.Spacing;
  1316. end;
  1317. result := true;
  1318. end;
  1319. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1320. procedure TtsTextBlock.PushSpacing(const aWidth: Integer);
  1321. var
  1322. p: PtsLineItem;
  1323. begin
  1324. if (aWidth <= 0) then
  1325. exit;
  1326. new(p);
  1327. FillByte(p^, SizeOf(p^), 0);
  1328. p^.ItemType := tsItemTypeSpacing;
  1329. p^.Spacing := aWidth;
  1330. PushLineItem(p);
  1331. end;
  1332. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1333. procedure TtsTextBlock.FreeLineItem(var aItem: PtsLineItem);
  1334. begin
  1335. if Assigned(aItem^.Prev) then
  1336. aItem^.Prev^.Next := aItem^.Next;
  1337. if Assigned(aItem^.Next) then
  1338. aItem^.Next^.Prev := aItem^.Prev;
  1339. case aItem^.ItemType of
  1340. tsItemTypeText, tsItemTypeSpace:
  1341. tsStrDispose(aItem^.Text);
  1342. end;
  1343. Dispose(aItem);
  1344. aItem := nil;
  1345. end;
  1346. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1347. procedure TtsTextBlock.FreeLineItems(var aItem: PtsLineItem);
  1348. var
  1349. p: PtsLineItem;
  1350. begin
  1351. while Assigned(aItem) do begin
  1352. p := aItem;
  1353. aItem := aItem^.Next;
  1354. FreeLineItem(p);
  1355. end;
  1356. end;
  1357. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1358. procedure TtsTextBlock.FreeLines(var aItem: PtsBlockLine);
  1359. var
  1360. p: PtsBlockLine;
  1361. begin
  1362. while Assigned(aItem) do begin
  1363. p := aItem;
  1364. aItem := aItem^.Next;
  1365. FreeLineItems(p^.First);
  1366. p^.Last := nil;
  1367. Dispose(p);
  1368. end;
  1369. end;
  1370. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1371. function TtsTextBlock.SplitText(aText: PWideChar): PtsLineItem;
  1372. var
  1373. TextBegin: PWideChar;
  1374. TextLength: Integer;
  1375. State: TtsLineItemType;
  1376. LastItem: PtsLineItem;
  1377. procedure AddItem(const aItem: PtsLineItem);
  1378. begin
  1379. if Assigned(result) then begin
  1380. LastItem^.Next := aItem;
  1381. aItem^.Prev := LastItem;
  1382. aItem^.Next := nil;
  1383. LastItem := aItem;
  1384. end;
  1385. if not Assigned(result) then begin
  1386. result := aItem;
  1387. LastItem := aItem;
  1388. end;
  1389. end;
  1390. procedure ExtractWord;
  1391. var
  1392. p: PtsLineItem;
  1393. Text: PWideChar;
  1394. begin
  1395. if (State = tsItemTypeUnknown) then
  1396. exit;
  1397. new(p);
  1398. FillByte(p^, SizeOf(p^), 0);
  1399. p^.ItemType := State;
  1400. case State of
  1401. tsItemTypeText, tsItemTypeSpace: begin
  1402. p^.Text := tsStrAlloc(TextLength);
  1403. TextLength := 0;
  1404. Text := p^.Text;
  1405. while (TextBegin <> aText) do begin
  1406. Text^ := TextBegin^;
  1407. inc(Text, 1);
  1408. inc(TextBegin, 1);
  1409. end;
  1410. AddItem(p);
  1411. end;
  1412. tsItemTypeLineBreak: begin
  1413. AddItem(p);
  1414. TextBegin := aText;
  1415. end;
  1416. tsItemTypeTab: begin
  1417. AddItem(p);
  1418. end;
  1419. else
  1420. Dispose(p);
  1421. p := nil;
  1422. end;
  1423. end;
  1424. begin
  1425. result := nil;
  1426. LastItem := nil;
  1427. TextBegin := aText;
  1428. TextLength := 0;
  1429. State := tsItemTypeUnknown;
  1430. if not Assigned(aText) then
  1431. exit;
  1432. while (aText^ <> #0) do begin
  1433. case aText^ of
  1434. // line breaks
  1435. #$000D, #$000A: begin
  1436. if (State <> tsItemTypeLineBreak) then begin
  1437. ExtractWord;
  1438. State := tsItemTypeLineBreak;
  1439. end else if (TextBegin^ <> #13) or (aText^ <> #10) or (TextBegin + 1 < aText) then
  1440. ExtractWord;
  1441. end;
  1442. // spaces
  1443. #$0020: begin
  1444. if (State <> tsItemTypeSpace) then
  1445. ExtractWord;
  1446. State := tsItemTypeSpace;
  1447. end;
  1448. // tabulator
  1449. #$0009: begin
  1450. if (State <> tsItemTypeTab) then
  1451. ExtractWord;
  1452. State := tsItemTypeTab;
  1453. end;
  1454. else
  1455. if (State <> tsItemTypeText) then
  1456. ExtractWord;
  1457. State := tsItemTypeText;
  1458. end;
  1459. inc(aText, 1);
  1460. inc(TextLength, 1);
  1461. end;
  1462. if (TextBegin <> aText) then
  1463. ExtractWord;
  1464. end;
  1465. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1466. function TtsTextBlock.SplitIntoLines(aItem: PtsLineItem): Boolean;
  1467. var
  1468. p: PtsLineItem;
  1469. begin
  1470. result := false;
  1471. if not Assigned(fCurrentFont) then
  1472. exit;
  1473. result := true;
  1474. while Assigned(aItem) do begin
  1475. p := aItem;
  1476. aItem := aItem^.Next;
  1477. p^.Next := nil;
  1478. p^.Prev := nil;
  1479. if not Assigned(fLastLine) then
  1480. PushNewLine;
  1481. case p^.ItemType of
  1482. tsItemTypeText, tsItemTypeSpace: begin
  1483. // increment word counter
  1484. if (p^.ItemType = tsItemTypeSpace) then begin
  1485. if not (tsLastItemIsSpace in fLastLine^.Flags) then
  1486. inc(fLastLine^.meta.SpaceCount, 1);
  1487. Include(fLastLine^.Flags, tsLastItemIsSpace);
  1488. end else
  1489. Exclude(fLastLine^.Flags, tsLastItemIsSpace);
  1490. // update and check line width
  1491. p^.TextWidth := fCurrentFont.GetTextWidthW(p^.Text);
  1492. if (tsBlockFlagWordWrap in fFlags) and
  1493. (fLastLine^.meta.Width + p^.TextWidth > fWidth) then
  1494. begin
  1495. if (fLastLine^.meta.Width = 0) then begin
  1496. if not PushLineItem(p, false) then // if is first word, than add anyway
  1497. FreeLineItem(p);
  1498. p := nil;
  1499. end;
  1500. include(fLastLine^.Flags, tsAutoLineBreak);
  1501. PushNewLine;
  1502. end;
  1503. // add item
  1504. if Assigned(p) then begin
  1505. if not PushLineItem(p) then
  1506. FreeLineItem(p);
  1507. PushSpacing(fCurrentFont.CharSpacing);
  1508. end;
  1509. end;
  1510. tsItemTypeLineBreak: begin
  1511. if not PushLineItem(p) then
  1512. FreeLineItem(p);
  1513. PushNewLine;
  1514. end;
  1515. tsItemTypeTab: begin
  1516. if not PushLineItem(p) then
  1517. FreeLineItem(p);
  1518. end;
  1519. else
  1520. raise EtsException.Create('unexpected line item');
  1521. end;
  1522. end;
  1523. end;
  1524. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1525. procedure TtsTextBlock.TrimSpaces(const aLine: PtsBlockLine);
  1526. procedure Trim(var aItem: PtsLineItem; const aMoveNext: Boolean);
  1527. var
  1528. tmp, p: PtsLineItem;
  1529. IsFirst: Boolean;
  1530. begin
  1531. IsFirst := true;
  1532. p := aItem;
  1533. while Assigned(p) do begin
  1534. tmp := p;
  1535. if aMoveNext then
  1536. p := p^.Next
  1537. else
  1538. p := p^.Prev;
  1539. case tmp^.ItemType of
  1540. tsItemTypeText: begin //done
  1541. break;
  1542. end;
  1543. tsItemTypeSpace,
  1544. tsItemTypeSpacing: begin
  1545. // update line meta
  1546. if (tmp^.ItemType = tsItemTypeSpace) then begin
  1547. aLine^.meta.Width := aLine^.meta.Width - tmp^.TextWidth;
  1548. dec(aLine^.meta.SpaceCount, 1);
  1549. end else
  1550. aLine^.meta.Width := aLine^.meta.Width - tmp^.Spacing;
  1551. FreeLineItem(tmp);
  1552. if IsFirst then
  1553. aItem := p;
  1554. end;
  1555. else
  1556. IsFirst := false;
  1557. end;
  1558. end;
  1559. end;
  1560. begin
  1561. if not Assigned(aLine) then
  1562. exit;
  1563. Trim(aLine^.First, true);
  1564. Trim(aLine^.Last, false);
  1565. end;
  1566. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1567. procedure TtsTextBlock.UpdateLineMeta(const aLine: PtsBlockLine);
  1568. var
  1569. metric: TtsTextMetric;
  1570. begin
  1571. if not Assigned(fCurrentFont) or
  1572. not Assigned(aLine) then
  1573. exit;
  1574. fCurrentFont.GetTextMetric(metric);
  1575. if (tsMetaValid in aLine^.Flags) then begin
  1576. aLine^.meta.Height := max(
  1577. aLine^.meta.Height,
  1578. metric.LineHeight);
  1579. aLine^.meta.Spacing := max(
  1580. aLine^.meta.Spacing,
  1581. metric.LineSpacing);
  1582. aLine^.meta.Ascent := max(
  1583. aLine^.meta.Ascent,
  1584. metric.Ascent);
  1585. end else begin
  1586. Include(aLine^.Flags, tsMetaValid);
  1587. aLine^.meta.Height := metric.LineHeight;
  1588. aLine^.meta.Spacing := metric.LineSpacing;
  1589. aLine^.meta.Ascent := metric.Ascent;
  1590. end;
  1591. end;
  1592. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1593. procedure TtsTextBlock.PushNewLine;
  1594. var
  1595. p: PtsBlockLine;
  1596. begin
  1597. TrimSpaces(fLastLine);
  1598. new(p);
  1599. FillByte(p^, SizeOf(p^), 0);
  1600. UpdateLineMeta(p);
  1601. if Assigned(fLastLine) then begin
  1602. fLastLine^.Next := p;
  1603. fLastLine := p;
  1604. end;
  1605. if not Assigned(fFirstLine) then begin
  1606. fFirstLine := p;
  1607. fLastLine := p;
  1608. end;
  1609. end;
  1610. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1611. constructor TtsTextBlock.Create(const aRenderer: TtsRenderer; const aTop, aLeft, aWidth, aHeight: Integer; const aFlags: TtsBlockFlags);
  1612. begin
  1613. inherited Create;
  1614. fRenderer := aRenderer;
  1615. fTop := aTop;
  1616. fLeft := aLeft;
  1617. fWidth := aWidth;
  1618. fHeight := aHeight;
  1619. fFlags := aFlags;
  1620. fVertAlign := tsVertAlignTop;
  1621. fHorzAlign := tsHorzAlignLeft;
  1622. fRenderer.RegisterBlock(self);
  1623. PushNewLine;
  1624. end;
  1625. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1626. procedure TtsTextBlock.ChangeFont(const aFont: TtsFont);
  1627. var
  1628. p: PtsLineItem;
  1629. begin
  1630. if not Assigned(aFont) then
  1631. exit;
  1632. New(p);
  1633. FillByte(p^, SizeOf(p^), 0);
  1634. fCurrentFont := aFont;
  1635. p^.ItemType := tsItemTypeFont;
  1636. p^.Font := fCurrentFont;
  1637. PushLineItem(p);
  1638. UpdateLineMeta(fLastLine);
  1639. fRenderer.UnregisterBlock(self);
  1640. end;
  1641. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1642. procedure TtsTextBlock.ChangeColor(const aColor: TtsColor4f);
  1643. var
  1644. p: PtsLineItem;
  1645. begin
  1646. New(p);
  1647. FillByte(p^, SizeOf(p^), 0);
  1648. p^.ItemType := tsItemTypeColor;
  1649. p^.Color := aColor;
  1650. PushLineItem(p);
  1651. fCurrentColor := aColor;
  1652. end;
  1653. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1654. function TtsTextBlock.GetActualBlockHeight: Integer;
  1655. var
  1656. line: PtsBlockLine;
  1657. begin
  1658. result := 0;
  1659. line := fFirstLine;
  1660. while Assigned(line) do begin
  1661. result := result + line^.meta.Height;
  1662. line := line^.Next;
  1663. end;
  1664. end;
  1665. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1666. procedure TtsTextBlock.TextOutA(const aText: PAnsiChar);
  1667. var
  1668. tmp: PWideChar;
  1669. begin
  1670. tmp := Renderer.Context.AnsiToWide(aText);
  1671. try
  1672. TextOutW(tmp);
  1673. finally
  1674. tsStrDispose(tmp);
  1675. end;
  1676. end;
  1677. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1678. procedure TtsTextBlock.TextOutW(const aText: PWideChar);
  1679. var
  1680. p: PtsLineItem;
  1681. begin
  1682. p := SplitText(aText);
  1683. if not SplitIntoLines(p) then
  1684. FreeLineItems(p);
  1685. end;
  1686. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1687. destructor TtsTextBlock.Destroy;
  1688. begin
  1689. FreeLines(fFirstLine);
  1690. fLastLine := nil;
  1691. inherited Destroy;
  1692. end;
  1693. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1694. //TtsRenderer///////////////////////////////////////////////////////////////////////////////////////////////////////////
  1695. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1696. procedure TtsRenderer.RegisterBlock(const aBlock: TtsTextBlock);
  1697. begin
  1698. fBlocks.Add(aBlock);
  1699. end;
  1700. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1701. procedure TtsRenderer.UnregisterBlock(const aBlock: TtsTextBlock);
  1702. begin
  1703. if Assigned(fBlocks) then
  1704. fBlocks.Remove(aBlock);
  1705. end;
  1706. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1707. procedure TtsRenderer.BeginRender;
  1708. begin
  1709. fRenderCS.Enter;
  1710. end;
  1711. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1712. procedure TtsRenderer.EndRender;
  1713. begin
  1714. fRenderCS.Leave;
  1715. end;
  1716. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1717. function TtsRenderer.BeginBlock(const aTop, aLeft, aWidth, aHeight: Integer; const aFlags: TtsBlockFlags): TtsTextBlock;
  1718. begin
  1719. result := TtsTextBlock.Create(self, aTop, aLeft, aWidth, aHeight, aFlags);
  1720. end;
  1721. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1722. procedure TtsRenderer.EndBlock(var aBlock: TtsTextBlock);
  1723. var
  1724. c: PWideChar;
  1725. pos: TtsPosition;
  1726. x, y, tmp, tab: Integer;
  1727. ExtraSpaceTotal, ExtraSpaceActual: Single;
  1728. rect: TtsRect;
  1729. line: PtsBlockLine;
  1730. item: PtsLineItem;
  1731. font: TtsFont;
  1732. char: TtsChar;
  1733. metric: TtsTextMetric;
  1734. draw: Boolean;
  1735. function GetChar(const aCharCode: WideChar): TtsChar;
  1736. begin
  1737. result := font.AddChar(aCharCode);
  1738. if not Assigned(result) then
  1739. result := font.AddChar(font.Properties.DefaultChar);
  1740. end;
  1741. procedure DrawItem;
  1742. begin
  1743. case item^.ItemType of
  1744. tsItemTypeFont: begin
  1745. font := item^.Font;
  1746. font.GetTextMetric(metric);
  1747. end;
  1748. tsItemTypeColor: begin
  1749. SetColor(item^.Color);
  1750. end;
  1751. tsItemTypeText: begin
  1752. if draw and Assigned(font) then begin
  1753. c := item^.Text;
  1754. while (c^ <> #0) do begin
  1755. char := GetChar(c^);
  1756. if Assigned(char) then begin
  1757. MoveDrawPos(Char.GlyphOrigin.x, -metric.BaseLineOffset);
  1758. Render(char.RenderRef);
  1759. MoveDrawPos(char.Advance - char.GlyphOrigin.x + font.CharSpacing, metric.BaseLineOffset);
  1760. end;
  1761. inc(c);
  1762. end;
  1763. end;
  1764. end;
  1765. tsItemTypeSpace: begin
  1766. if draw and Assigned(font) then begin
  1767. ExtraSpaceActual := ExtraSpaceActual + ExtraSpaceTotal;
  1768. c := item^.Text;
  1769. while (c^ <> #0) do begin
  1770. char := GetChar(c^);
  1771. if Assigned(char) then begin
  1772. if (font.Properties.Style * [tsStyleUnderline, tsStyleStrikeout] <> []) then begin
  1773. MoveDrawPos(char.GlyphOrigin.x, -metric.BaseLineOffset);
  1774. Render(char.RenderRef);
  1775. MoveDrawPos(char.Advance - char.GlyphOrigin.x + font.CharSpacing, metric.BaseLineOffset);
  1776. end else begin
  1777. MoveDrawPos(char.Advance + font.CharSpacing, 0);
  1778. end;
  1779. end;
  1780. inc(c);
  1781. end;
  1782. tmp := Trunc(ExtraSpaceActual);
  1783. ExtraSpaceActual := ExtraSpaceActual - tmp;
  1784. if (font.Properties.Style * [tsStyleUnderline, tsStyleStrikeout] <> []) then begin
  1785. // TODO draw lines; maybe with a temporary created fake char or something like an empty char?
  1786. end;
  1787. MoveDrawPos(tmp, 0);
  1788. end;
  1789. end;
  1790. tsItemTypeLineBreak: begin
  1791. // because this should be the last item in a line, we have nothing to do here
  1792. end;
  1793. tsItemTypeTab: begin
  1794. // get current x pos and round it to TabWidth
  1795. pos := GetDrawPos;
  1796. tab := font.TabWidth * font.Properties.Size;
  1797. pos.x := Ceil(pos.x * tab) div tab;
  1798. SetDrawPos(pos.x, pos.y);
  1799. end;
  1800. tsItemTypeSpacing: begin
  1801. MoveDrawPos(item^.Spacing, 0);
  1802. end;
  1803. end;
  1804. end;
  1805. procedure DrawLine;
  1806. begin
  1807. // check vertical clipping
  1808. case aBlock.Clipping of
  1809. tsClipCharBorder, tsClipWordBorder:
  1810. draw := (y + line^.meta.Height > rect.Top) and (y < rect.Bottom);
  1811. tsClipCharComplete, tsClipWordComplete:
  1812. draw := (y > rect.Top) and (y + line^.meta.Height < rect.Bottom);
  1813. else
  1814. draw := true;
  1815. end;
  1816. // check horizontal alignment
  1817. x := rect.Left;
  1818. ExtraSpaceTotal := 0;
  1819. ExtraSpaceActual := 0;
  1820. case aBlock.HorzAlign of
  1821. tsHorzAlignCenter:
  1822. x := rect.Left + (aBlock.Width div 2) - (line^.meta.Width div 2);
  1823. tsHorzAlignRight:
  1824. x := rect.Right - line^.meta.Width;
  1825. tsHorzAlignJustify:
  1826. if (tsAutoLineBreak in line^.Flags) then
  1827. ExtraSpaceTotal := (aBlock.Width - line^.meta.Width) / line^.meta.SpaceCount;
  1828. end;
  1829. if draw then
  1830. SetDrawPos(x, y + line^.meta.Ascent);
  1831. inc(y, line^.meta.Height + line^.meta.Spacing);
  1832. item := line^.First;
  1833. while Assigned(item) do begin
  1834. DrawItem;
  1835. item := item^.Next;
  1836. end;
  1837. end;
  1838. begin
  1839. if (aBlock.Renderer <> self) then
  1840. EtsException.Create('text block was created by other renderer');
  1841. BeginRender;
  1842. try
  1843. // init variables
  1844. y := aBlock.Top;
  1845. font := nil;
  1846. line := aBlock.Lines;
  1847. rect := aBlock.Rect;
  1848. // check vertical alignment
  1849. case aBlock.VertAlign of
  1850. tsVertAlignCenter:
  1851. y := y + (aBlock.Height div 2 - aBlock.GetActualBlockHeight div 2);
  1852. tsVertAlignBottom:
  1853. y := y + (aBlock.Height - aBlock.GetActualBlockHeight);
  1854. end;
  1855. while Assigned(line) do begin
  1856. DrawLine;
  1857. line := line^.Next;
  1858. end;
  1859. finally
  1860. EndRender;
  1861. FreeAndNil(aBlock);
  1862. end;
  1863. end;
  1864. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1865. constructor TtsRenderer.Create(const aContext: TtsContext; const aFormat: TtsFormat);
  1866. begin
  1867. inherited Create;
  1868. fContext := aContext;
  1869. fFormat := aFormat;
  1870. fSaveImages := true;
  1871. fBlocks := TObjectList.Create(false);
  1872. fRenderCS := TCriticalSection.Create;
  1873. fContext.RegisterRenderer(self);
  1874. end;
  1875. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1876. destructor TtsRenderer.Destroy;
  1877. begin
  1878. fContext.UnregisterRenderer(self);
  1879. fBlocks.OwnsObjects := true;
  1880. FreeAndNil(fBlocks);
  1881. FreeAndNil(fRenderCS);
  1882. inherited Destroy;
  1883. end;
  1884. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1885. //TtsContext////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1886. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1887. procedure TtsContext.RegisterRenderer(const aRenderer: TtsRenderer);
  1888. begin
  1889. fRenderers.Add(aRenderer);
  1890. end;
  1891. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1892. procedure TtsContext.UnregisterRenderer(const aRenderer: TtsRenderer);
  1893. begin
  1894. if Assigned(fRenderers) then
  1895. fRenderers.Remove(aRenderer);
  1896. end;
  1897. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1898. procedure TtsContext.RegisterGenerator(const aGenerator: TtsFontGenerator);
  1899. begin
  1900. fGenerators.Add(aGenerator);
  1901. end;
  1902. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1903. procedure TtsContext.UnregisterGenerator(const aGenerator: TtsFontGenerator);
  1904. begin
  1905. if Assigned(fGenerators) then
  1906. fGenerators.Remove(aGenerator);
  1907. end;
  1908. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1909. function TtsContext.AnsiToWide(const aText: PAnsiChar): PWideChar;
  1910. var
  1911. len: Integer;
  1912. begin
  1913. result := nil;
  1914. if not Assigned(aText) then
  1915. exit;
  1916. len := Length(aText);
  1917. result := tsStrAlloc(len);
  1918. tsAnsiToWide(result, len, aText, fCodePage, fCodePageDefault);
  1919. end;
  1920. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1921. constructor TtsContext.Create;
  1922. begin
  1923. inherited Create;
  1924. fCodePage := tsUTF8;
  1925. fCodePageDefault := WideChar('?');
  1926. fRenderers := TObjectList.Create(false);
  1927. fGenerators := TObjectList.Create(false);
  1928. end;
  1929. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1930. destructor TtsContext.Destroy;
  1931. begin
  1932. fGenerators.OwnsObjects := true;
  1933. fRenderers.OwnsObjects := true;
  1934. FreeAndNil(fGenerators);
  1935. FreeAndNil(fRenderers);
  1936. inherited Destroy;
  1937. end;
  1938. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1939. //Exceptions////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1940. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1941. constructor EtsOutOfRange.Create(const aMin, aMax, aIndex: Integer);
  1942. begin
  1943. inherited Create(Format('index (%d) is out of range (%d - %d)', [aIndex, aMin, aMax]));
  1944. end;
  1945. initialization
  1946. Randomize;
  1947. end.