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.

411 regels
11 KiB

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