2
0

SDL2.Ttf.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. unit SDL2.Ttf;
  2. (*******************************************************************************
  3. SDL2_Ttf.pas v1.0 29/07/2013 first version for DelphiXE
  4. v1.1 27/08/2013 add MACOS compability
  5. v1.2 31/05/2014 delete sdl2.inc
  6. Simple DirectMedia Layer
  7. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  8. Pascal-Header-Conversion SDL from the JEDI-Team written by Domenique Louis and others.
  9. convert SDL/SDL2 to SDL2 for DelphiXE by Kotai 2013/2014 www.remakesonline.com
  10. The initial developer of this Pascal code was :
  11. Dominqiue Louis <[email protected]>
  12. *******************************************************************************)
  13. interface
  14. uses
  15. {$IFDEF MSWINDOWS}
  16. Winapi.Windows,
  17. {$ENDIF}
  18. SDL2.Import;
  19. const
  20. {$IFDEF MSWINDOWS}
  21. SDL_ttfLibName = 'SDL2_ttf.dll';
  22. {$ENDIF}
  23. {$IFDEF ANDROID}
  24. SDL_ttfLibName = 'libSDL2_ttf.so';
  25. {$ENDIF}
  26. {$IFDEF MACOS}
  27. {$IFDEF IOS}
  28. SDL_ttfLibName = 'libSDL2_ttf.a';
  29. {$ELSE}
  30. SDL_ttfLibName = 'SDL2_ttf';
  31. // SDL_ttfLibName = '../Frameworks/SDL2_ttf.framework/Versions/A/SDL2_ttf';
  32. {$ENDIF}
  33. {$ENDIF}
  34. {* Set up for C function definitions, even when using C++ *}
  35. {* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL *}
  36. const
  37. SDL_TTF_MAJOR_VERSION = 2;
  38. SDL_TTF_MINOR_VERSION = 0;
  39. SDL_TTF_PATCHLEVEL = 12;
  40. {* Backwards compatibility *}
  41. const
  42. TTF_MAJOR_VERSION = SDL_TTF_MAJOR_VERSION;
  43. TTF_MINOR_VERSION = SDL_TTF_MINOR_VERSION;
  44. TTF_PATCHLEVEL = SDL_TTF_PATCHLEVEL;
  45. //TTF_VERSION(X) = SDL_TTF_VERSION(X);
  46. {* This function gets the version of the dynamically linked SDL_ttf library.
  47. it should NOT be used to fill a version structure, instead you should
  48. use the SDL_TTF_VERSION() macro.
  49. *}
  50. function TTF_Linked_Version(): TSDL_Version;
  51. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_Linked_Version' {$ENDIF} {$ENDIF};
  52. {* ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark) *}
  53. const
  54. UNICODE_BOM_NATIVE = $FEFF;
  55. UNICODE_BOM_SWAPPED = $FFFE;
  56. {* This function tells the library whether UNICODE text is generally
  57. byteswapped. A UNICODE BOM character in a string will override
  58. this setting for the remainder of that string.
  59. *}
  60. procedure TTF_ByteSwappedUNICODE(swapped: Integer);
  61. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_ByteSwappedUNICODE' {$ENDIF} {$ENDIF};
  62. {* The internal structure containing font information *}
  63. type
  64. PTTF_Font = ^TTTF_Font;
  65. TTTF_Font = record end; //todo?
  66. {* Initialize the TTF engine - returns 0 if successful, -1 on error *}
  67. function TTF_Init(): Integer;
  68. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_Init' {$ENDIF} {$ENDIF};
  69. {* Open a font file and create a font of the specified point size.
  70. * Some .fon fonts will have several sizes embedded in the file, so the
  71. * point size becomes the index of choosing which size. If the value
  72. * is too high, the last indexed size will be the default. *}
  73. function TTF_OpenFont(_file: PChar; ptsize: Integer): PTTF_Font;
  74. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_OpenFont' {$ENDIF} {$ENDIF};
  75. function TTF_OpenFontIndex(_file: PChar; ptsize: Integer; index: LongInt): PTTF_Font;
  76. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_OpenFontIndex' {$ENDIF} {$ENDIF};
  77. function TTF_OpenFontRW(src: PSDL_RWops; freesrc: Integer; ptsize: LongInt): PTTF_Font;
  78. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_OpenFontRW' {$ENDIF} {$ENDIF};
  79. function TTF_OpenFontIndexRW(src: PSDL_RWops; freesrc: Integer; ptsize: Integer; index: LongInt): PTTF_Font;
  80. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_OpenFontIndexRW' {$ENDIF} {$ENDIF};
  81. {* Set and retrieve the font style *}
  82. const
  83. TTF_STYLE_NORMAL = $00;
  84. TTF_STYLE_BOLD = $01;
  85. TTF_STYLE_ITALIC = $02;
  86. TTF_STYLE_UNDERLINE = $04;
  87. TTF_STYLE_STRIKETHROUGH = $08;
  88. function TTF_GetFontStyle(font: PTTF_Font): Integer;
  89. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GetFontStyle' {$ENDIF} {$ENDIF};
  90. procedure TTF_SetFontStyle(font: PTTF_Font; style: Integer);
  91. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SetFontStyle' {$ENDIF} {$ENDIF};
  92. function TTF_GetFontOutline(font: PTTF_Font): Integer;
  93. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GetFontOutline' {$ENDIF} {$ENDIF};
  94. procedure TTF_SetFontOutline(font: PTTF_Font; outline: Integer);
  95. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SetFontOutline' {$ENDIF} {$ENDIF};
  96. {* Set and retrieve FreeType hinter settings *}
  97. const
  98. TTF_HINTING_NORMAL = 0;
  99. TTF_HINTING_LIGHT = 1;
  100. TTF_HINTING_MONO = 2;
  101. TTF_HINTING_NONE = 3;
  102. function TTF_GetFontHinting(font: PTTF_Font): Integer;
  103. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GetFontHinting' {$ENDIF} {$ENDIF};
  104. procedure TTF_SetFontHinting(font: PTTF_Font; hinting: Integer);
  105. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SetFontHinting' {$ENDIF} {$ENDIF};
  106. {* Get the total height of the font - usually equal to point size *}
  107. function TTF_FontHeight(font: PTTF_Font): Integer;
  108. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontHeight' {$ENDIF} {$ENDIF};
  109. {* Get the offset from the baseline to the top of the font
  110. This is a positive value, relative to the baseline.
  111. *}
  112. function TTF_FontAscent(font: PTTF_Font): Integer;
  113. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontAscent' {$ENDIF} {$ENDIF};
  114. {* Get the offset from the baseline to the bottom of the font
  115. This is a negative value, relative to the baseline.
  116. *}
  117. function TTF_FontDescent(font: PTTF_Font): Integer;
  118. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontDescent' {$ENDIF} {$ENDIF};
  119. {* Get the recommended spacing between lines of text for this font *}
  120. function TTF_FontLineSkip(font: PTTF_Font): Integer;
  121. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontLineSkip' {$ENDIF} {$ENDIF};
  122. {* Get/Set whether or not kerning is allowed for this font *}
  123. function TTF_GetFontKerning(font: PTTF_Font): Integer;
  124. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GetFontKerning' {$ENDIF} {$ENDIF};
  125. procedure TTF_SetFontKerning(font: PTTF_Font; allowed: Integer);
  126. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SetFontKerning' {$ENDIF} {$ENDIF};
  127. {* Get the number of faces of the font *}
  128. function TTF_FontFaces(font: PTTF_Font): LongInt;
  129. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontFaces' {$ENDIF} {$ENDIF};
  130. {* Get the font face attributes, if any *}
  131. function TTF_FontFaceIsFixedWidth(font: PTTF_Font): Integer;
  132. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontFaceIsFixedWidth' {$ENDIF} {$ENDIF};
  133. function TTF_FontFaceFamilyName(font: PTTF_Font): PChar;
  134. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontFaceFamilyName' {$ENDIF} {$ENDIF};
  135. function TTF_FontFaceStyleName(font: PTTF_Font): PChar;
  136. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_FontFaceStyleName' {$ENDIF} {$ENDIF};
  137. {* Check wether a glyph is provided by the font or not *}
  138. function TTF_GlyphIsProvided(font: PTTF_Font; ch: UInt16): Integer;
  139. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GlyphIsProvided' {$ENDIF} {$ENDIF};
  140. {* Get the metrics (dimensions) of a glyph
  141. To understand what these metrics mean, here is a useful link:
  142. http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
  143. *}
  144. function TTF_GlyphMetrics(font: PTTF_Font; ch: UInt16; minx, maxx: PInt; miny, maxy: PInt; advance: PInt): Integer;
  145. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GlyphMetrics' {$ENDIF} {$ENDIF};
  146. {* Get the dimensions of a rendered string of text *}
  147. function TTF_SizeText(font: PTTF_Font; text: PChar; w, h: PInt): Integer;
  148. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SizeText' {$ENDIF} {$ENDIF};
  149. function TTF_SizeUTF8(font: PTTF_Font; text: PChar; w, h: PInt): Integer;
  150. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SizeUTF8' {$ENDIF} {$ENDIF};
  151. function TTF_SizeUNICODE(font: PTTF_Font; text: PUInt16; w, h: PInt): Integer;
  152. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_SizeUNICODE' {$ENDIF} {$ENDIF};
  153. {* Create an 8-bit palettized surface and render the given text at
  154. fast quality with the given font and color. The 0 pixel is the
  155. colorkey, giving a transparent background, and the 1 pixel is set
  156. to the text color.
  157. This function returns the new surface, or NULL if there was an error.
  158. *}
  159. function TTF_RenderText_Solid(font: PTTF_Font; text: PChar; fg: TSDL_Color): PSDL_Surface;
  160. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderText_Solid' {$ENDIF} {$ENDIF};
  161. function TTF_RenderUTF8_Solid(font: PTTF_Font; text: PChar; fg: TSDL_Color): PSDL_Surface;
  162. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUTF8_Solid' {$ENDIF} {$ENDIF};
  163. function TTF_RenderUNICODE_Solid(font: PTTF_Font; text: PUInt16; fg: TSDL_Color): PSDL_Surface;
  164. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUNICODE_Solid' {$ENDIF} {$ENDIF};
  165. {* Create an 8-bit palettized surface and render the given glyph at
  166. fast quality with the given font and color. The 0 pixel is the
  167. colorkey, giving a transparent background, and the 1 pixel is set
  168. to the text color. The glyph is rendered without any padding or
  169. centering in the X direction, and aligned normally in the Y direction.
  170. This function returns the new surface, or NULL if there was an error.
  171. *}
  172. function TTF_RenderGlyph_Solid(font: PTTF_Font; ch: UInt16; fg: TSDL_Color): PSDL_Surface;
  173. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderGlyph_Solid' {$ENDIF} {$ENDIF};
  174. {* Create an 8-bit palettized surface and render the given text at
  175. high quality with the given font and colors. The 0 pixel is background,
  176. while other pixels have varying degrees of the foreground color.
  177. This function returns the new surface, or NULL if there was an error.
  178. *}
  179. function TTF_RenderText_Shaded(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  180. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderText_Shaded' {$ENDIF} {$ENDIF};
  181. function TTF_RenderUTF8_Shaded(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  182. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUTF8_Shaded' {$ENDIF} {$ENDIF};
  183. function TTF_RenderUNICODE_Shaded(font: PTTF_Font; text: PUInt16; fg, bg: TSDL_Color): PSDL_Surface;
  184. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUNICODE_Shaded' {$ENDIF} {$ENDIF};
  185. {* Create an 8-bit palettized surface and render the given glyph at
  186. high quality with the given font and colors. The 0 pixel is background,
  187. while other pixels have varying degrees of the foreground color.
  188. The glyph is rendered without any padding or centering in the X
  189. direction, and aligned normally in the Y direction.
  190. This function returns the new surface, or NULL if there was an error.
  191. *}
  192. function TTF_RenderGlyph_Shaded(font: PTTF_Font; ch: UInt16; fg, bg: TSDL_Color): PSDL_Surface;
  193. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderGlyph_Shaded' {$ENDIF} {$ENDIF};
  194. {* Create a 32-bit ARGB surface and render the given text at high quality,
  195. using alpha blending to dither the font with the given color.
  196. This function returns the new surface, or NULL if there was an error.
  197. *}
  198. function TTF_RenderText_Blended(font: PTTF_Font; text: PChar; fg: TSDL_Color): PSDL_Surface;
  199. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderText_Blended' {$ENDIF} {$ENDIF};
  200. function TTF_RenderUTF8_Blended(font: PTTF_Font; text: PChar; fg: TSDL_Color): PSDL_Surface;
  201. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUTF8_Blended' {$ENDIF} {$ENDIF};
  202. function TTF_RenderUNICODE_Blended(font: PTTF_Font; text: UInt16; fg: TSDL_Color): PSDL_Surface;
  203. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUNICODE_Blended' {$ENDIF} {$ENDIF};
  204. {* Create a 32-bit ARGB surface and render the given text at high quality,
  205. using alpha blending to dither the font with the given color.
  206. Text is wrapped to multiple lines on line endings and on word boundaries
  207. if it extends beyond wrapLength in pixels.
  208. This function returns the new surface, or NULL if there was an error.
  209. *}
  210. function TTF_RenderText_Blended_Wrapped(font: PTTF_Font; text: PChar; fg: TSDL_Color; wrapLength: UInt32): PSDL_Surface;
  211. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderText_Blended_Wrapped' {$ENDIF} {$ENDIF};
  212. function TTF_RenderUTF8_Blended_Wrapped(font: PTTF_Font; text: PChar; fg: TSDL_Color; wrapLength: UInt32): PSDL_Surface;
  213. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUTF8_Blended_Wrapped' {$ENDIF} {$ENDIF};
  214. function TTF_RenderUNICODE_Blended_Wrapped(font: PTTF_Font; text: PUInt16; fg: TSDL_Color; wrapLength: UInt32): PSDL_Surface;
  215. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderUNICODE_Blended_Wrapped' {$ENDIF} {$ENDIF};
  216. {* Create a 32-bit ARGB surface and render the given glyph at high quality,
  217. using alpha blending to dither the font with the given color.
  218. The glyph is rendered without any padding or centering in the X
  219. direction, and aligned normally in the Y direction.
  220. This function returns the new surface, or NULL if there was an error.
  221. *}
  222. function TTF_RenderGlyph_Blended(font: PTTF_Font; ch: UInt16; fg: TSDL_Color): PSDL_Surface;
  223. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_RenderGlyph_Blended' {$ENDIF} {$ENDIF};
  224. {* For compatibility with previous versions, here are the old functions *}
  225. function TTF_RenderText(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  226. function TTF_RenderUTF8(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  227. function TTF_RenderUNICODE(font: PTTF_Font; text: PUInt16; fg, bg: TSDL_Color): PSDL_Surface;
  228. {* Close an opened font file *}
  229. procedure TTF_CloseFont(font: PTTF_Font);
  230. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_CloseFont' {$ENDIF} {$ENDIF};
  231. {* De-initialize the TTF engine *}
  232. procedure TTF_Quit();
  233. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_Quit' {$ENDIF} {$ENDIF};
  234. {* Check if the TTF engine is initialized *}
  235. function TTF_WasInit(): Boolean;
  236. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_WasInit' {$ENDIF} {$ENDIF};
  237. {* Get the kerning size of two glyphs *}
  238. function TTF_GetFontKerningSize(font: PTTF_Font; prev_index, index: Integer): Integer;
  239. cdecl; external SDL_ttfLibName {$IFDEF MACOS} {$IFNDEF IOS} name '_TTF_GetFontKerningSize' {$ENDIF} {$ENDIF};
  240. {* We'll use SDL for reporting errors *}
  241. function TTF_SetError(const fmt: PChar): SInt32;
  242. function TTF_GetError: PChar;
  243. //******************************************************************************
  244. //******************************************************************************
  245. //******************************************************************************
  246. //******************************************************************************
  247. //******************************************************************************
  248. implementation
  249. //******************************************************************************
  250. function TTF_SetError(const fmt: PChar): SInt32;
  251. begin
  252. Result := SDL_SetError(fmt);
  253. end;
  254. //******************************************************************************
  255. function TTF_GetError: PChar;
  256. begin
  257. Result := SDL_GetError;
  258. end;
  259. //******************************************************************************
  260. function TTF_RenderText(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  261. begin
  262. Result := TTF_RenderText_Shaded(font, text, fg, bg);
  263. end;
  264. //******************************************************************************
  265. function TTF_RenderUTF8(font: PTTF_Font; text: PChar; fg, bg: TSDL_Color): PSDL_Surface;
  266. begin
  267. Result := TTF_RenderUTF8_Shaded(font, text, fg, bg);
  268. end;
  269. //******************************************************************************
  270. function TTF_RenderUNICODE(font: PTTF_Font; text: PUInt16; fg, bg: TSDL_Color): PSDL_Surface;
  271. begin
  272. Result := TTF_RenderUNICODE_Shaded(font, text, fg, bg);
  273. end;
  274. end.