Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

378 righe
9.5 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. TtsTextMetric = packed record
  141. Ascent: Integer;
  142. Descent: Integer;
  143. ExternalLeading: Integer;
  144. BaseLineOffset: Integer;
  145. CharSpacing: Integer;
  146. LineHeight: Integer;
  147. LineSpacing: Integer;
  148. end;
  149. TtsBlendFunc = function(const aSrc, aDst: TtsColor4f): TtsColor4f;
  150. const
  151. TS_CHANNELS_RGB: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue];
  152. TS_CHANNELS_RGBA: TtsColorChannels = [tsChannelRed, tsChannelGreen, tsChannelBlue, tsChannelAlpha];
  153. TS_MODES_REPLACE_ALL: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeReplace);
  154. TS_MODES_MODULATE_ALL: TtsImageModes = (tsModeModulate, tsModeModulate, tsModeModulate, tsModeModulate);
  155. TS_MODES_MODULATE_ALPHA: TtsImageModes = (tsModeReplace, tsModeReplace, tsModeReplace, tsModeModulate);
  156. function tsColor4f(r, g, b, a: Single): TtsColor4f;
  157. function tsModes(r, g, b, a: TtsImageMode): TtsImageModes;
  158. function tsRect(const l, t, r, b: Integer): TtsRect;
  159. function tsPosition(const x, y: Integer): TtsPosition;
  160. function tsFormatSize(const aFormat: TtsFormat): Integer;
  161. procedure tsFormatMap(const aFormat: TtsFormat; var aData: PByte; const aColor: TtsColor4f);
  162. procedure tsFormatUnmap(const aFormat: TtsFormat; var aData: PByte; out aColor: TtsColor4f);
  163. function tsImageModeFuncIgnore(const aSource, aDest: Single): Single;
  164. function tsImageModeFuncReplace(const aSource, aDest: Single): Single;
  165. function tsImageModeFuncModulate(const aSource, aDest: Single): Single;
  166. function tsBlendFundAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  167. function tsBlendFundAdditive(const aSrc, aDst: TtsColor4f): TtsColor4f;
  168. function tsBlendFundAdditiveAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  169. implementation
  170. uses
  171. Math;
  172. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  173. function tsColor4f(r, g, b, a: Single): TtsColor4f;
  174. begin
  175. result.r := r;
  176. result.g := g;
  177. result.b := b;
  178. result.a := a;
  179. end;
  180. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. function tsModes(r, g, b, a: TtsImageMode): TtsImageModes;
  182. begin
  183. result[tsChannelRed] := r;
  184. result[tsChannelGreen] := g;
  185. result[tsChannelBlue] := b;
  186. result[tsChannelAlpha] := a;
  187. end;
  188. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  189. function tsRect(const l, t, r, b: Integer): TtsRect;
  190. begin
  191. result.Left := l;
  192. result.Top := t;
  193. result.Right := r;
  194. result.Bottom := b;
  195. end;
  196. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. function tsPosition(const x, y: Integer): TtsPosition;
  198. begin
  199. result.x := x;
  200. result.y := y;
  201. end;
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. function tsFormatSize(const aFormat: TtsFormat): Integer;
  204. begin
  205. case aFormat of
  206. tsFormatRGBA8: result := 4;
  207. tsFormatLumAlpha8: result := 2;
  208. tsFormatAlpha8: result := 1;
  209. tsFormatLum8: result := 1;
  210. else
  211. result := 0;
  212. end;
  213. end;
  214. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  215. procedure tsFormatMap(const aFormat: TtsFormat; var aData: PByte; const aColor: TtsColor4f);
  216. var
  217. i: Integer;
  218. s: Single;
  219. begin
  220. case aFormat of
  221. tsFormatRGBA8: begin
  222. for i := 0 to 3 do begin
  223. aData^ := Trunc($FF * min(aColor.arr[i], 1.0));
  224. inc(aData);
  225. end;
  226. end;
  227. tsFormatLumAlpha8: begin
  228. s := 0.30 * min(aColor.r, 1.0) +
  229. 0.59 * min(aColor.g, 1.0) +
  230. 0.11 * min(aColor.b, 1.0);
  231. aData^ := Trunc($FF * s);
  232. inc(aData);
  233. aData^ := Trunc($FF * min(aColor.a, 1.0));
  234. inc(aData);
  235. end;
  236. tsFormatAlpha8: begin
  237. aData^ := Trunc($FF * min(aColor.a, 1.0));
  238. inc(aData);
  239. end;
  240. tsFormatLum8: begin
  241. s := 0.30 * min(aColor.r, 1.0) +
  242. 0.59 * min(aColor.g, 1.0) +
  243. 0.11 * min(aColor.b, 1.0);
  244. aData^ := Trunc($FF * s);
  245. inc(aData);
  246. end;
  247. end;
  248. end;
  249. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  250. procedure tsFormatUnmap(const aFormat: TtsFormat; var aData: PByte; out aColor: TtsColor4f);
  251. var
  252. i: Integer;
  253. begin
  254. case aFormat of
  255. tsFormatRGBA8: begin
  256. for i := 0 to 3 do begin
  257. aColor.arr[i] := aData^ / $FF;
  258. inc(aData);
  259. end;
  260. end;
  261. tsFormatLumAlpha8: begin
  262. aColor.r := aData^ / $FF;
  263. aColor.g := aData^ / $FF;
  264. aColor.b := aData^ / $FF;
  265. inc(aData);
  266. aColor.a := aData^ / $FF;
  267. inc(aData);
  268. end;
  269. tsFormatAlpha8: begin
  270. aColor.r := 1.0;
  271. aColor.g := 1.0;
  272. aColor.b := 1.0;
  273. aColor.a := aData^ / $FF;
  274. inc(aData);
  275. end;
  276. tsFormatLum8: begin
  277. aColor.r := aData^ / $FF;
  278. aColor.g := aData^ / $FF;
  279. aColor.b := aData^ / $FF;
  280. aColor.a := 1.0;
  281. inc(aData);
  282. end;
  283. end;
  284. end;
  285. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  286. function tsImageModeFuncIgnore(const aSource, aDest: Single): Single;
  287. begin
  288. result := aDest;
  289. end;
  290. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  291. function tsImageModeFuncReplace(const aSource, aDest: Single): Single;
  292. begin
  293. result := aSource;
  294. end;
  295. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  296. function tsImageModeFuncModulate(const aSource, aDest: Single): Single;
  297. begin
  298. result := aSource * aDest;
  299. end;
  300. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  301. function tsBlendFundAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  302. var
  303. i: Integer;
  304. begin
  305. for i := 0 to 2 do
  306. result.arr[i] := aSrc.arr[i] * aSrc.a + aDst.arr[i] * (1 - aSrc.a);
  307. result.a := aSrc.a + aDst.a * (1 - aSrc.a);
  308. end;
  309. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  310. function tsBlendFundAdditive(const aSrc, aDst: TtsColor4f): TtsColor4f;
  311. var
  312. i: Integer;
  313. begin
  314. for i := 0 to 3 do
  315. result.arr[i] := aSrc.arr[i] + aDst.arr[i];
  316. end;
  317. function tsBlendFundAdditiveAlpha(const aSrc, aDst: TtsColor4f): TtsColor4f;
  318. var
  319. i: Integer;
  320. begin
  321. for i := 0 to 2 do
  322. result.arr[i] := aSrc.arr[i] * aSrc.a + aDst.arr[i];
  323. result.a := aDst.a;
  324. end;
  325. end.