Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

413 řádky
11 KiB

  1. unit utsTypes;
  2. {$IFDEF FPC}
  3. {$mode delphi}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. Classes, SysUtils;
  8. type
  9. TtsCodePage = (
  10. tsUTF8,
  11. tsISO_8859_1,
  12. tsISO_8859_2,
  13. tsISO_8859_3,
  14. tsISO_8859_4,
  15. tsISO_8859_5,
  16. tsISO_8859_6,
  17. tsISO_8859_7,
  18. tsISO_8859_8,
  19. tsISO_8859_9,
  20. tsISO_8859_10,
  21. tsISO_8859_11,
  22. tsISO_8859_13,
  23. tsISO_8859_14,
  24. tsISO_8859_15,
  25. tsISO_8859_16,
  26. tsISO_037,
  27. tsISO_437,
  28. tsISO_500,
  29. tsISO_737,
  30. tsISO_775,
  31. tsISO_850,
  32. tsISO_852,
  33. tsISO_855,
  34. tsISO_857,
  35. tsISO_860,
  36. tsISO_861,
  37. tsISO_862,
  38. tsISO_863,
  39. tsISO_864,
  40. tsISO_865,
  41. tsISO_866,
  42. tsISO_869,
  43. tsISO_874,
  44. tsISO_875,
  45. tsISO_1026,
  46. tsISO_1250,
  47. tsISO_1251,
  48. tsISO_1252,
  49. tsISO_1253,
  50. tsISO_1254,
  51. tsISO_1255,
  52. tsISO_1256,
  53. tsISO_1257,
  54. tsISO_1258);
  55. TtsFontStyle = (
  56. tsStyleBold,
  57. tsStyleItalic,
  58. tsStyleUnderline,
  59. tsStyleStrikeout);
  60. TtsFontStyles = set of TtsFontStyle;
  61. TtsVertAlignment = (
  62. tsVertAlignTop,
  63. tsVertAlignCenter,
  64. tsVertAlignBottom);
  65. TtsHorzAlignment = (
  66. tsHorzAlignLeft,
  67. tsHorzAlignCenter,
  68. tsHorzAlignRight,
  69. tsHorzAlignJustify);
  70. TtsFormat = (
  71. tsFormatEmpty,
  72. tsFormatRGBA8,
  73. tsFormatLumAlpha8,
  74. tsFormatAlpha8,
  75. tsFormatLum8);
  76. TtsAntiAliasing = (
  77. tsAANone,
  78. tsAANormal);
  79. TtsColorChannel = (
  80. tsChannelRed,
  81. tsChannelGreen,
  82. tsChannelBlue,
  83. tsChannelAlpha);
  84. TtsColorChannels = set of TtsColorChannel;
  85. TtsImageMode = (
  86. tsModeIgnore,
  87. tsModeReplace,
  88. tsModeModulate);
  89. TtsImageModes = array[TtsColorChannel] of TtsImageMode;
  90. TtsImageModeFunc = function(const aSource, aDest: Single): Single;
  91. TtsFontProperties = packed record
  92. Fontname: String;
  93. Copyright: String;
  94. FaceName: String;
  95. StyleName: String;
  96. FullName: String;
  97. Size: Integer;
  98. Style: TtsFontStyles;
  99. AntiAliasing: TtsAntiAliasing;
  100. DefaultChar: WideChar;
  101. Ascent: Integer;
  102. Descent: Integer;
  103. ExternalLeading: Integer;
  104. BaseLineOffset: Integer;
  105. UnderlinePos: Integer;
  106. UnderlineSize: Integer;
  107. StrikeoutPos: Integer;
  108. StrikeoutSize: Integer;
  109. end;
  110. TtsPosition = packed record
  111. x, y: Integer;
  112. end;
  113. PtsPosition = ^TtsPosition;
  114. TtsPositionF = packed record
  115. x, y: Single;
  116. end;
  117. PtsPositionF = ^TtsPositionF;
  118. TtsRect = packed record
  119. case Byte of
  120. 0: (TopLeft: TtsPosition; BottomRight: TtsPosition);
  121. 1: (Left, Top, Right, Bottom: Integer);
  122. end;
  123. PtsRect = ^TtsRect;
  124. TtsRectF = packed record
  125. case Byte of
  126. 0: (TopLeft: TtsPositionF; BottomRight: TtsPositionF);
  127. 1: (Left, Top, Right, Bottom: Single);
  128. end;
  129. PtsRectF = ^TtsRectF;
  130. TtsColor4f = packed record
  131. case Boolean of
  132. true: (r, g, b, a: Single);
  133. false: (arr: array[0..3] of Single);
  134. end;
  135. PtsColor4f = ^TtsColor4f;
  136. TtsColor4ub = packed record
  137. case Boolean of
  138. true: (r, g, b, a: Byte);
  139. false: (arr: array[0..3] of Byte);
  140. end;
  141. PtsColor4ub = ^TtsColor4ub;
  142. TtsVector4f = array[0..3] of Single;
  143. TtsMatrix4f = array[0..3] of TtsVector4f;
  144. TtsTextMetric = packed record
  145. Ascent: Integer;
  146. Descent: Integer;
  147. ExternalLeading: Integer;
  148. BaseLineOffset: Integer;
  149. CharSpacing: Integer;
  150. LineHeight: Integer;
  151. LineSpacing: Integer;
  152. end;
  153. TtsBlendFunc = function(const aSrc, aDst: TtsColor4f): TtsColor4f;
  154. const
  155. TS_CHANNELS_RGB: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue];
  156. TS_CHANNELS_RGBA: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue, tsChannelAlpha];
  157. TS_MODES_REPLACE_ALL: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeReplace);
  158. TS_MODES_MODULATE_ALL: TtsImageModes = (tsModeModulate, tsModeModulate, tsModeModulate, tsModeModulate);
  159. TS_MODES_MODULATE_ALPHA: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeModulate);
  160. TS_MATRIX_IDENTITY: TtsMatrix4f = ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1));
  161. function tsColor4f(r, g, b, a: Single): TtsColor4f;
  162. function tsModes(r, g, b, a: TtsImageMode): TtsImageModes;
  163. function tsRect(const l, t, r, b: Integer): TtsRect;
  164. function tsPosition(const x, y: Integer): TtsPosition;
  165. function tsPositionF(const x, y: Single): TtsPositionF;
  166. function tsVector4f(X, Y, Z, W: Single): TtsVector4f;
  167. function tsMatrix4f(X, Y, Z, P: TtsVector4f): TtsMatrix4f;
  168. function tsFormatSize(const aFormat: TtsFormat): Integer;
  169. procedure tsFormatMap(const aFormat: TtsFormat; var aData: PByte; const aColor: TtsColor4f);
  170. procedure tsFormatUnmap(const aFormat: TtsFormat; var aData: PByte; out aColor: TtsColor4f);
  171. function tsImageModeFuncIgnore(const aSource, aDest: Single): Single;
  172. function tsImageModeFuncReplace(const aSource, aDest: Single): Single;
  173. function tsImageModeFuncModulate(const aSource, aDest: Single): Single;
  174. function tsBlendFundAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  175. function tsBlendFundAdditive(const aSrc, aDst: TtsColor4f): TtsColor4f;
  176. function tsBlendFundAdditiveAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  177. implementation
  178. uses
  179. Math;
  180. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. function tsColor4f(r, g, b, a: Single): TtsColor4f;
  182. begin
  183. result.r := r;
  184. result.g := g;
  185. result.b := b;
  186. result.a := a;
  187. end;
  188. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  189. function tsModes(r, g, b, a: TtsImageMode): TtsImageModes;
  190. begin
  191. result[tsChannelRed] := r;
  192. result[tsChannelGreen] := g;
  193. result[tsChannelBlue] := b;
  194. result[tsChannelAlpha] := a;
  195. end;
  196. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. function tsRect(const l, t, r, b: Integer): TtsRect;
  198. begin
  199. result.Left := l;
  200. result.Top := t;
  201. result.Right := r;
  202. result.Bottom := b;
  203. end;
  204. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205. function tsPosition(const x, y: Integer): TtsPosition;
  206. begin
  207. result.x := x;
  208. result.y := y;
  209. end;
  210. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. function tsPositionF(const x, y: Single): TtsPositionF;
  212. begin
  213. result.x := x;
  214. result.y := y;
  215. end;
  216. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  217. function tsVector4f(X, Y, Z, W: Single): TtsVector4f;
  218. begin
  219. result[0] := X;
  220. result[1] := Y;
  221. result[2] := Z;
  222. result[3] := W;
  223. end;
  224. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. function tsMatrix4f(X, Y, Z, P: TtsVector4f): TtsMatrix4f;
  226. begin
  227. result[0] := X;
  228. result[1] := Y;
  229. result[2] := Z;
  230. result[3] := P;
  231. end;
  232. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  233. function tsFormatSize(const aFormat: TtsFormat): Integer;
  234. begin
  235. case aFormat of
  236. tsFormatRGBA8: result := 4;
  237. tsFormatLumAlpha8: result := 2;
  238. tsFormatAlpha8: result := 1;
  239. tsFormatLum8: result := 1;
  240. else
  241. result := 0;
  242. end;
  243. end;
  244. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  245. procedure tsFormatMap(const aFormat: TtsFormat; var aData: PByte; const aColor: TtsColor4f);
  246. var
  247. i: Integer;
  248. s: Single;
  249. begin
  250. case aFormat of
  251. tsFormatRGBA8: begin
  252. for i := 0 to 3 do begin
  253. aData^ := Trunc($FF * min(aColor.arr[i], 1.0));
  254. inc(aData);
  255. end;
  256. end;
  257. tsFormatLumAlpha8: begin
  258. s := 0.30 * min(aColor.r, 1.0) +
  259. 0.59 * min(aColor.g, 1.0) +
  260. 0.11 * min(aColor.b, 1.0);
  261. aData^ := Trunc($FF * s);
  262. inc(aData);
  263. aData^ := Trunc($FF * min(aColor.a, 1.0));
  264. inc(aData);
  265. end;
  266. tsFormatAlpha8: begin
  267. aData^ := Trunc($FF * min(aColor.a, 1.0));
  268. inc(aData);
  269. end;
  270. tsFormatLum8: begin
  271. s := 0.30 * min(aColor.r, 1.0) +
  272. 0.59 * min(aColor.g, 1.0) +
  273. 0.11 * min(aColor.b, 1.0);
  274. aData^ := Trunc($FF * s);
  275. inc(aData);
  276. end;
  277. end;
  278. end;
  279. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  280. procedure tsFormatUnmap(const aFormat: TtsFormat; var aData: PByte; out aColor: TtsColor4f);
  281. var
  282. i: Integer;
  283. begin
  284. case aFormat of
  285. tsFormatRGBA8: begin
  286. for i := 0 to 3 do begin
  287. aColor.arr[i] := aData^ / $FF;
  288. inc(aData);
  289. end;
  290. end;
  291. tsFormatLumAlpha8: begin
  292. aColor.r := aData^ / $FF;
  293. aColor.g := aData^ / $FF;
  294. aColor.b := aData^ / $FF;
  295. inc(aData);
  296. aColor.a := aData^ / $FF;
  297. inc(aData);
  298. end;
  299. tsFormatAlpha8: begin
  300. aColor.r := 1.0;
  301. aColor.g := 1.0;
  302. aColor.b := 1.0;
  303. aColor.a := aData^ / $FF;
  304. inc(aData);
  305. end;
  306. tsFormatLum8: begin
  307. aColor.r := aData^ / $FF;
  308. aColor.g := aData^ / $FF;
  309. aColor.b := aData^ / $FF;
  310. aColor.a := 1.0;
  311. inc(aData);
  312. end;
  313. end;
  314. end;
  315. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  316. function tsImageModeFuncIgnore(const aSource, aDest: Single): Single;
  317. begin
  318. result := aDest;
  319. end;
  320. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  321. function tsImageModeFuncReplace(const aSource, aDest: Single): Single;
  322. begin
  323. result := aSource;
  324. end;
  325. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  326. function tsImageModeFuncModulate(const aSource, aDest: Single): Single;
  327. begin
  328. result := aSource * aDest;
  329. end;
  330. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. function tsBlendFundAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  332. var
  333. i: Integer;
  334. begin
  335. for i := 0 to 2 do
  336. result.arr[i] := aSrc.arr[i] * aSrc.a + aDst.arr[i] * (1 - aSrc.a);
  337. result.a := aSrc.a + aDst.a * (1 - aSrc.a);
  338. end;
  339. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  340. function tsBlendFundAdditive(const aSrc, aDst: TtsColor4f): TtsColor4f;
  341. var
  342. i: Integer;
  343. begin
  344. for i := 0 to 3 do
  345. result.arr[i] := aSrc.arr[i] + aDst.arr[i];
  346. end;
  347. function tsBlendFundAdditiveAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  348. var
  349. i: Integer;
  350. begin
  351. for i := 0 to 2 do
  352. result.arr[i] := aSrc.arr[i] * aSrc.a + aDst.arr[i];
  353. result.a := aDst.a;
  354. end;
  355. end.