Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

2264 rader
69 KiB

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