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.

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