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.

414 line
11 KiB

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