ttf.nim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. {.deadCodeElim: on.}
  2. when not defined(SDL_Static):
  3. when defined(windows):
  4. const LibName* = "SDL2_ttf.dll"
  5. elif defined(macosx):
  6. const LibName = "libSDL2_ttf.dylib"
  7. else:
  8. const LibName = "libSDL2_ttf(|-2.0).so(|.0)"
  9. else:
  10. static: echo "SDL_Static option is deprecated and will soon be removed. Instead please use --dynlibOverride:SDL2."
  11. import ../sdl2
  12. type
  13. FontPtr*{.pure.} = ptr object
  14. # Set up for C function definitions, even when using C++
  15. # Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
  16. #/*
  17. ##define SDL_TTF_MAJOR_VERSION 2
  18. ##define SDL_TTF_MINOR_VERSION 0
  19. ##define SDL_TTF_PATCHLEVEL 12
  20. #
  21. # This macro can be used to fill a version structure with the compile-time
  22. # version of the SDL_ttf library.
  23. #
  24. ##define SDL_TTF_VERSION(X) \
  25. #{ \
  26. # (X)->major = SDL_TTF_MAJOR_VERSION; \
  27. # (X)->minor = SDL_TTF_MINOR_VERSION; \
  28. # (X)->patch = SDL_TTF_PATCHLEVEL; \
  29. #}
  30. # Backwards compatibility
  31. ##define TTF_MAJOR_VERSION SDL_TTF_MAJOR_VERSION
  32. ##define TTF_MINOR_VERSION SDL_TTF_MINOR_VERSION
  33. #//#define TTF_PATCHLEVEL SDL_TTF_PATCHLEVEL
  34. ##define TTF_VERSION(X) SDL_TTF_VERSION(X)
  35. when not defined(SDL_Static):
  36. {.push callConv:cdecl, dynlib:LibName.}
  37. proc ttfLinkedVersion*(): ptr SDL_version {.importc: "TTF_Linked_Version".}
  38. # ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark)
  39. const
  40. UNICODE_BOM_NATIVE* = 0x0000FEFF
  41. UNICODE_BOM_SWAPPED* = 0x0000FFFE
  42. # This function tells the library whether UNICODE text is generally
  43. # byteswapped. A UNICODE BOM character in a string will override
  44. # this setting for the remainder of that string.
  45. #
  46. proc ttfByteSwappedUnicode*(swapped: cint) {.importc: "TTF_ByteSwappedUNICODE".}
  47. # Initialize the TTF engine - returns 0 if successful, -1 on error
  48. proc ttfInit*(): SDL_Return {.importc: "TTF_Init", discardable.}
  49. # Open a font file and create a font of the specified point size.
  50. # Some .fon fonts will have several sizes embedded in the file, so the
  51. # point size becomes the index of choosing which size. If the value
  52. # is too high, the last indexed size will be the default.
  53. proc openFont*(file: cstring; ptsize: cint): FontPtr {.importc: "TTF_OpenFont".}
  54. proc openFontIndex*(file: cstring; ptsize: cint; index: clong): FontPtr {.importc: "TTF_OpenFontIndex".}
  55. proc openFontRW*(src: ptr RWops; freesrc: cint; ptsize: cint): FontPtr {.importc: "TTF_OpenFontRW".}
  56. proc openFontIndexRW*(src: ptr RWops; freesrc: cint; ptsize: cint;
  57. index: clong): FontPtr {.importc: "TTF_OpenFontIndexRW".}
  58. # Set and retrieve the font style
  59. const
  60. TTF_STYLE_NORMAL* = 0x00000000
  61. TTF_STYLE_BOLD* = 0x00000001
  62. TTF_STYLE_ITALIC* = 0x00000002
  63. TTF_STYLE_UNDERLINE* = 0x00000004
  64. TTF_STYLE_STRIKETHROUGH* = 0x00000008
  65. proc getFontStyle*(font: FontPtr): cint {.importc: "TTF_GetFontStyle".}
  66. proc setFontStyle*(font: FontPtr; style: cint) {.importc: "TTF_SetFontStyle".}
  67. proc getFontOutline*(font: FontPtr): cint {.importc: "TTF_GetFontOutline".}
  68. proc setFontOutline*(font: FontPtr; outline: cint) {.importc: "TTF_SetFontOutline".}
  69. # Set and retrieve FreeType hinter settings
  70. const
  71. TTF_HINTING_NORMAL* = 0
  72. TTF_HINTING_LIGHT* = 1
  73. TTF_HINTING_MONO* = 2
  74. TTF_HINTING_NONE* = 3
  75. proc getFontHinting*(font: FontPtr): cint {.importc: "TTF_GetFontHinting".}
  76. proc setFontHinting*(font: FontPtr; hinting: cint) {.importc: "TTF_SetFontHinting".}
  77. # Get the total height of the font - usually equal to point size
  78. proc fontHeight*(font: FontPtr): cint {.importc: "TTF_FontHeight".}
  79. # Get the offset from the baseline to the top of the font
  80. # This is a positive value, relative to the baseline.
  81. #
  82. proc fontAscent*(font: FontPtr): cint {.importc: "TTF_FontAscent".}
  83. # Get the offset from the baseline to the bottom of the font
  84. # This is a negative value, relative to the baseline.
  85. #
  86. proc fontDescent*(font: FontPtr): cint {.importc: "TTF_FontDescent".}
  87. # Get the recommended spacing between lines of text for this font
  88. proc fontLineSkip*(font: FontPtr): cint {.importc: "TTF_FontLineSkip".}
  89. # Get/Set whether or not kerning is allowed for this font
  90. proc getFontKerning*(font: FontPtr): cint {.importc: "TTF_GetFontKerning".}
  91. proc setFontKerning*(font: FontPtr; allowed: cint) {.importc: "TTF_SetFontKerning".}
  92. # Get the number of faces of the font
  93. proc fontFaces*(font: FontPtr): clong {.importc: "TTF_FontFaces".}
  94. # Get the font face attributes, if any
  95. proc fontFaceIsFixedWidth*(font: FontPtr): cint {.importc: "TTF_FontFaceIsFixedWidth".}
  96. proc fontFaceFamilyName*(font: FontPtr): cstring {.importc: "TTF_FontFaceFamilyName".}
  97. proc fontFaceStyleName*(font: FontPtr): cstring {.importc: "TTF_FontFaceStyleName".}
  98. # Check wether a glyph is provided by the font or not
  99. proc glyphIsProvided*(font: FontPtr; ch: uint16): cint {.importc: "TTF_GlyphIsProvided".}
  100. # Get the metrics (dimensions) of a glyph
  101. # To understand what these metrics mean, here is a useful link:
  102. # http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
  103. #
  104. proc glyphMetrics*(font: FontPtr; ch: uint16; minx: ptr cint;
  105. maxx: ptr cint; miny: ptr cint; maxy: ptr cint;
  106. advance: ptr cint): cint {.importc: "TTF_GlyphMetrics".}
  107. # Get the dimensions of a rendered string of text
  108. proc sizeText*(font: FontPtr; text: cstring; w: ptr cint; h: ptr cint): cint{.
  109. importc: "TTF_SizeText".}
  110. proc sizeUtf8*(font: FontPtr; text: cstring; w: ptr cint; h: ptr cint): cint{.
  111. importc: "TTF_SizeUTF8".}
  112. proc sizeUnicode*(font: FontPtr; text: ptr uint16; w, h: ptr cint): cint{.
  113. importc: "TTF_SizeUNICODE".}
  114. # Create an 8-bit palettized surface and render the given text at
  115. # fast quality with the given font and color. The 0 pixel is the
  116. # colorkey, giving a transparent background, and the 1 pixel is set
  117. # to the text color.
  118. # This function returns the new surface, or NULL if there was an error.
  119. #
  120. proc renderTextSolid*(font: FontPtr; text: cstring; fg: Color): SurfacePtr{.
  121. importc: "TTF_RenderText_Solid".}
  122. proc renderUtf8Solid*(font: FontPtr; text: cstring; fg: Color): SurfacePtr{.
  123. importc: "TTF_RenderUTF8_Solid".}
  124. proc renderUnicodeSolid*(font: FontPtr; text: ptr uint16;
  125. fg: Color): SurfacePtr {.importc: "TTF_RenderUNICODE_Solid".}
  126. # Create an 8-bit palettized surface and render the given glyph at
  127. # fast quality with the given font and color. The 0 pixel is the
  128. # colorkey, giving a transparent background, and the 1 pixel is set
  129. # to the text color. The glyph is rendered without any padding or
  130. # centering in the X direction, and aligned normally in the Y direction.
  131. # This function returns the new surface, or NULL if there was an error.
  132. #
  133. proc renderGlyphSolid*(font: FontPtr; ch: uint16; fg: Color): SurfacePtr {.
  134. importc: "TTF_RenderGlyph_Solid".}
  135. proc renderTextShaded*(font: FontPtr; text: cstring; fg, bg: Color): SurfacePtr {.
  136. importc: "TTF_RenderText_Shaded".}
  137. proc renderUtf8Shaded*(font: FontPtr; text: cstring; fg, bg: Color): SurfacePtr {.
  138. importc: "TTF_RenderUTF8_Shaded".}
  139. proc renderUnicodeShaded*(font: FontPtr; text: ptr uint16;
  140. fg, bg: Color): SurfacePtr {.importc: "TTF_RenderUNICODE_Shaded".}
  141. # Create an 8-bit palettized surface and render the given glyph at
  142. # high quality with the given font and colors. The 0 pixel is background,
  143. # while other pixels have varying degrees of the foreground color.
  144. # The glyph is rendered without any padding or centering in the X
  145. # direction, and aligned normally in the Y direction.
  146. # This function returns the new surface, or NULL if there was an error.
  147. #
  148. proc renderGlyphShaded*(font: FontPtr; ch: uint16; fg, bg: Color): SurfacePtr {.
  149. importc: "TTF_RenderGlyph_Shaded".}
  150. # Create a 32-bit ARGB surface and render the given text at high quality,
  151. # using alpha blending to dither the font with the given color.
  152. # This function returns the new surface, or NULL if there was an error.
  153. #
  154. proc renderTextBlended*(font: FontPtr; text: cstring; fg: Color): SurfacePtr {.
  155. importc: "TTF_RenderText_Blended".}
  156. proc renderUtf8Blended*(font: FontPtr; text: cstring; fg: Color): SurfacePtr {.
  157. importc: "TTF_RenderUTF8_Blended".}
  158. proc renderUnicodeBlended*(font: FontPtr; text: ptr uint16;
  159. fg: Color): SurfacePtr {.importc: "TTF_RenderUNICODE_Blended".}
  160. # Create a 32-bit ARGB surface and render the given text at high quality,
  161. # using alpha blending to dither the font with the given color.
  162. # Text is wrapped to multiple lines on line endings and on word boundaries
  163. # if it extends beyond wrapLength in pixels.
  164. # This function returns the new surface, or NULL if there was an error.
  165. #
  166. proc renderTextBlendedWrapped*(font: FontPtr; text: cstring; fg: Color; wrapLength: uint32):
  167. SurfacePtr {.importc: "TTF_RenderText_Blended_Wrapped".}
  168. proc renderUtf8BlendedWrapped*(font: FontPtr; text: cstring; fg: Color;
  169. wrapLength: uint32): SurfacePtr {.importc: "TTF_RenderUTF8_Blended_Wrapped".}
  170. proc renderUnicodeBlendedWrapped*(font: FontPtr; text: ptr uint16; fg: Color;
  171. wrapLength: uint32): SurfacePtr {.importc: "TTF_RenderUNICODE_Blended_Wrapped".}
  172. # Create a 32-bit ARGB surface and render the given glyph at high quality,
  173. # using alpha blending to dither the font with the given color.
  174. # The glyph is rendered without any padding or centering in the X
  175. # direction, and aligned normally in the Y direction.
  176. # This function returns the new surface, or NULL if there was an error.
  177. #
  178. proc renderGlyphBlended*(font: FontPtr; ch: uint16; fg: Color): SurfacePtr {.
  179. importc: "TTF_RenderGlyph_Blended".}
  180. #
  181. #/* Close an opened font file
  182. proc close*(font: FontPtr) {.importc: "TTF_CloseFont".}
  183. # De-initialize the TTF engine
  184. proc ttfQuit*() {.importc: "TTF_Quit".}
  185. # Check if the TTF engine is initialized
  186. proc ttfWasInit*(): bool {.importc: "TTF_WasInit".}
  187. # Get the kerning size of two glyphs
  188. proc getFontKerningSize*(font: FontPtr; prev_index, indx: cint): cint {.
  189. importc: "TTF_GetFontKerningSize".}
  190. when not defined(SDL_Static):
  191. {.pop.}
  192. # For compatibility with previous versions, here are the old functions
  193. ##define TTF_RenderText(font, text, fg, bg) \
  194. # TTF_RenderText_Shaded(font, text, fg, bg)
  195. ##define TTF_RenderUTF8(font, text, fg, bg) \
  196. # TTF_RenderUTF8_Shaded(font, text, fg, bg)
  197. ##define TTF_RenderUNICODE(font, text, fg, bg) \
  198. # TTF_RenderUNICODE_Shaded(font, text, fg, bg)
  199. proc renderText*(font: FontPtr; text: cstring;
  200. fg, bg: Color): SurfacePtr = renderTextShaded(font, text, fg, bg)