Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file tsdl_ttf.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364openCtypesopenForeignopenTsdlmoduleTtf=structtype'aresult='aSdl.resultletdebug=false(* set this to false before release *)leterror()=Error(`Msg(Sdl.get_error()))letzero_to_ok=letread=function0->Ok()|_err->error()inview~read~write:(fun_->assertfalse)intletbool=letread=function0->false|_->trueinletwrite=functiontrue->1|false->0inview~read~writeintletint32_as_uint32_t=view~read:Unsigned.UInt32.to_int32~write:Unsigned.UInt32.of_int32uint32_tletint64_as_long=view~read:Signed.Long.to_int64~write:Signed.Long.of_int64long(* let surface =
* view ~read:Sdl.unsafe_surface_of_ptr ~write:Sdl.unsafe_ptr_of_surface nativeint *)letsurface_result=letreadv=ifNativeint.(comparevzero)=0thenerror()elseOk(Sdl.unsafe_surface_of_ptrv)andwrite=function|Error_->raw_address_of_ptr@@null|Oks->Sdl.unsafe_ptr_of_surfacesinview~read~writenativeintletrw_ops=view~read:Sdl.unsafe_rw_ops_of_ptr~write:Sdl.unsafe_ptr_of_rw_opsnativeinttype_fonttypefont=_fontstructureptrletfont_struct:_fontstructuretyp=structure"TTF_Font"letfont:_fontstructureptrtyp=ptrfont_structletfont_opt:_fontstructureptroptiontyp=ptr_optfont_structletfont_result=letread=functionNone->error()|Somev->Okvandwrite=functionError_->None|Oks->Somesinview~read~writefont_opt(* pkg-config --variable=libdir SDL2_ttf *)(* Use Configurator.V1.Pkg_config instead? *)letpkg_config()=tryletic=Unix.open_process_in"pkg-config --variable=libdir SDL2_ttf"inletdir=input_lineicinclose_inic;Somedirwith_->None(* This "hack" seems to be necessary for linux if you want to use
#require "tsdl-ttf"
in the toplevel, see
https://github.com/ocamllabs/ocaml-ctypes/issues/70 *)letfrom:Dl.libraryoption=(ifdebugthenSdl.(log_infoLog.category_system"Loading Sdl_ttf, Target = %s"Build_config.system));letenv=trySys.getenv"LIBSDL2_PATH"withNot_found->""inletfilename,path=matchBuild_config.systemwith|"macosx"->("libSDL2_ttf-2.0.0.dylib",["/opt/homebrew/lib/"])|"win32"|"win64"->(* On native Windows DLLs are loaded from the PATH *)("SDL2_ttf.dll",[""])|"cygwin"|"mingw"->(* For Windows POSIX emulators (Cygwin and MSYS2), hardcoded
locations are available in addition to the PATH *)("SDL2_ttf.dll",["";"/usr/x86_64-w64-mingw32/sys-root/mingw/bin";"/usr/i686-w64-mingw32/sys-root/mingw/bin";"/clangarm64/bin";"/clang64/bin";"/clang32/bin";"/ucrt64/bin";"/mingw64/bin";"/mingw32/bin";])|_->("libSDL2_ttf-2.0.so.0",["/usr/lib/x86_64-linux-gnu/";"/usr/local/lib"])inletrecloop=function|[]->None|dir::rest->(letfilename=ifdir=""thenfilenameelseFilename.concatdirfilenameintrySomeDl.(dlopen~filename~flags:[RTLD_NOW])with_->looprest)inmatchloop(env::path)with|Somef->Somef|None->((* We execute pkg_config only if everything else failed. *)matchpkg_config()with|Somedir->loop[dir]|None->print_endline("Cannot find "^filename^", please set LIBSDL2_PATH");None)letforeign=foreign?fromletinit=foreign"TTF_Init"(void@->returningzero_to_ok)letopen_font=foreign"TTF_OpenFont"(string@->int@->returningfont_result)letopen_font_index=foreign"TTF_OpenFontIndex"(string@->int@->int64_as_long@->returningfont_result)letopen_font_rw=foreign"TTF_OpenFontRW"(rw_ops@->int@->int@->returningfont_result)letopen_font_index_rw=foreign"TTF_OpenFontIndexRW"(rw_ops@->int@->int@->int64_as_long@->returningfont_result)(* let byte_swapped_unicode =
* foreign "TTF_ByteSwappedUNICODE" (int @-> returning void) *)moduleStyle=structtypet=Unsigned.uint32leti=Unsigned.UInt32.of_intlet(+)=Unsigned.UInt32.logorlet(-)stflag=Unsigned.UInt32.(logandst(lognotflag))lettestfm=Unsigned.UInt32.(compare(logandfm)zero<>0)leteqff'=Unsigned.UInt32.(compareff'=0)letnormal=i0letbold=i1letitalic=i2letunderline=i4letstrikethrough=i8endletget_font_style=foreign"TTF_GetFontStyle"(font@->returninguint32_t)letset_font_style=foreign"TTF_SetFontStyle"(font@->uint32_t@->returningvoid)letget_font_outline=foreign"TTF_GetFontOutline"(font@->returningint)letset_font_outline=foreign"TTF_SetFontOutline"(font@->int@->returningvoid)moduleHinting=structtypet=Normal|Light|Mono|Nonelett=letread=function|0->Normal|1->Light|2->Mono|3->None|_->failwith"Unexpected value"inletwrite=functionNormal->0|Light->1|Mono->2|None->3inview~read~writeintendletget_font_hinting=foreign"TTF_GetFontHinting"(font@->returningHinting.t)letset_font_hinting=foreign"TTF_SetFontHinting"(font@->Hinting.t@->returningvoid)letfont_height=foreign"TTF_FontHeight"(font@->returningint)letfont_ascent=foreign"TTF_FontAscent"(font@->returningint)letfont_descent=foreign"TTF_FontDescent"(font@->returningint)letfont_line_skip=foreign"TTF_FontLineSkip"(font@->returningint)letget_font_kerning=foreign"TTF_GetFontKerning"(font@->returningbool)letset_font_kerning=foreign"TTF_SetFontKerning"(font@->bool@->returningvoid)letfont_faces=foreign"TTF_FontFaces"(font@->returningint64_as_long)letfont_face_is_fixed_width=foreign"TTF_FontFaceIsFixedWidth"(font@->returningint)letfont_face_family_name=foreign"TTF_FontFaceFamilyName"(font@->returningstring)letfont_face_style_name=foreign"TTF_FontFaceStyleName"(font@->returningstring)letglyph_ucs2=view~read:Unsigned.UInt16.to_int~write:Unsigned.UInt16.of_intuint16_tletglyph_is_provided=foreign"TTF_GlyphIsProvided"(font@->glyph_ucs2@->returningbool)moduleGlyphMetrics=structtypet={min_x:int;max_x:int;min_y:int;max_y:int;advance:int;}endletglyph_metrics=foreign"TTF_GlyphMetrics"(font@->glyph_ucs2@->ptrint@->ptrint@->ptrint@->ptrint@->ptrint@->returningint)letglyph_metricsfg=letmin_x,max_x,min_y,max_y,advance=(allocateint0,allocateint0,allocateint0,allocateint0,allocateint0)inif0=glyph_metricsfgmin_xmax_xmin_ymax_yadvancethenOkGlyphMetrics.{min_x=!@min_x;max_x=!@max_x;min_y=!@min_y;max_y=!@max_y;advance=!@advance;}elseerror()letsize_text=foreign"TTF_SizeText"(font@->string@->ptrint@->ptrint@->returningint)letsize_textfs=letw,h=(allocateint0,allocateint0)inif0=size_textfswhthenOk(!@w,!@h)elseerror()letsize_utf8=foreign"TTF_SizeUTF8"(font@->string@->ptrint@->ptrint@->returningint)letsize_utf8fs=letw,h=(allocateint0,allocateint0)inif0=size_utf8fswhthenOk(!@w,!@h)elseerror()(* let size_unicode =
* foreign "TTF_SizeUNICODE" (font @-> ptr glyph_ucs2 @-> ptr int @-> ptr int @-> returning int) *)type_colortypecolor=_colorstructureletcolor:colortyp=structure"SDL_Color"letcolor_r=fieldcolor"r"uint8_tletcolor_g=fieldcolor"g"uint8_tletcolor_b=fieldcolor"b"uint8_tletcolor_a=fieldcolor"a"uint8_tlet()=sealcolorletcolor=letreadv=letr,g,b,a=Unsigned.UInt8.(to_int@@getfvcolor_r,to_int@@getfvcolor_g,to_int@@getfvcolor_b,to_int@@getfvcolor_a)inSdl.Color.create~r~g~b~ainletwritev=letc=makecolorinsetfccolor_r(Unsigned.UInt8.of_int(Sdl.Color.rv));setfccolor_g(Unsigned.UInt8.of_int(Sdl.Color.gv));setfccolor_b(Unsigned.UInt8.of_int(Sdl.Color.bv));setfccolor_a(Unsigned.UInt8.of_int(Sdl.Color.av));cinview~read~writecolorletrender_text_solid=foreign"TTF_RenderText_Solid"(font@->string@->color@->returningsurface_result)letrender_utf8_solid=foreign"TTF_RenderUTF8_Solid"(font@->string@->color@->returningsurface_result)(* let render_unicode_solid = foreign "TTF_RenderUNICODE_Solid" (font @-> ptr glyph_ucs2 @-> color @-> returning surface_result) *)letrender_glyph_solid=foreign"TTF_RenderGlyph_Solid"(font@->glyph_ucs2@->color@->returningsurface_result)letrender_text_shaded=foreign"TTF_RenderText_Shaded"(font@->string@->color@->color@->returningsurface_result)letrender_utf8_shaded=foreign"TTF_RenderUTF8_Shaded"(font@->string@->color@->color@->returningsurface_result)(* let render_unicode_shaded = foreign "TTF_RenderUNICODE_Shaded" (font @-> ptr glyph_ucs2 @-> color @-> color @-> returning surface_result) *)letrender_glyph_shaded=foreign"TTF_RenderGlyph_Shaded"(font@->glyph_ucs2@->color@->color@->returningsurface_result)letrender_text_blended=foreign"TTF_RenderText_Blended"(font@->string@->color@->returningsurface_result)letrender_utf8_blended=foreign"TTF_RenderUTF8_Blended"(font@->string@->color@->returningsurface_result)(* let render_unicode_blended = foreign "TTF_RenderUNICODE_Blended" (font @-> ptr glyph_ucs2 @-> color @-> returning surface_result) *)letrender_text_blended_wrapped=foreign"TTF_RenderText_Blended_Wrapped"(font@->string@->color@->int32_as_uint32_t@->returningsurface_result)letrender_utf8_blended_wrapped=foreign"TTF_RenderUTF8_Blended_Wrapped"(font@->string@->color@->int32_as_uint32_t@->returningsurface_result)(* let render_unicode_blended_wrapped = foreign "TTF_RenderUNICODE_Blended_Wrapped" (font @-> ptr glyph_ucs2 @-> color @-> int32_as_uint32_t @-> returning surface_result) *)letrender_glyph_blended=foreign"TTF_RenderGlyph_Blended"(font@->glyph_ucs2@->color@->returningsurface_result)letclose_font=foreign"TTF_CloseFont"(font@->returningvoid)letquit=foreign"TTF_Quit"(void@->returningvoid)letwas_init=foreign"TTF_WasInit"(void@->returningbool)letget_font_kerning_size=foreign"TTF_GetFontKerningSize"(font@->int@->int@->returningint)end