@@ -0,0 +1,158 @@ |
| 1 | +module font_mod |
| 2 | + use, intrinsic :: iso_c_binding |
| 3 | + use glyph_mod |
| 4 | + implicit none |
| 5 | + private |
| 6 | + |
| 7 | + public :: font_t |
| 8 | + public :: font_load, font_destroy, font_render_glyph |
| 9 | + |
| 10 | + ! Font handle and metrics |
| 11 | + type :: font_t |
| 12 | + type(c_ptr) :: ft_library = c_null_ptr |
| 13 | + type(c_ptr) :: ft_face = c_null_ptr |
| 14 | + integer :: size_px = 0 |
| 15 | + integer :: cell_width = 0 |
| 16 | + integer :: cell_height = 0 |
| 17 | + integer :: ascender = 0 |
| 18 | + integer :: descender = 0 |
| 19 | + logical :: loaded = .false. |
| 20 | + end type font_t |
| 21 | + |
| 22 | + ! C interface to FreeType helpers |
| 23 | + interface |
| 24 | + integer(c_int) function fortty_ft_init(lib) bind(C, name="fortty_ft_init") |
| 25 | + import :: c_int, c_ptr |
| 26 | + type(c_ptr), intent(out) :: lib |
| 27 | + end function fortty_ft_init |
| 28 | + |
| 29 | + subroutine fortty_ft_done(lib) bind(C, name="fortty_ft_done") |
| 30 | + import :: c_ptr |
| 31 | + type(c_ptr), value :: lib |
| 32 | + end subroutine fortty_ft_done |
| 33 | + |
| 34 | + integer(c_int) function fortty_ft_load_font(lib, path, size_px, face) & |
| 35 | + bind(C, name="fortty_ft_load_font") |
| 36 | + import :: c_int, c_ptr, c_char |
| 37 | + type(c_ptr), value :: lib |
| 38 | + character(kind=c_char), intent(in) :: path(*) |
| 39 | + integer(c_int), value :: size_px |
| 40 | + type(c_ptr), intent(out) :: face |
| 41 | + end function fortty_ft_load_font |
| 42 | + |
| 43 | + subroutine fortty_ft_done_face(face) bind(C, name="fortty_ft_done_face") |
| 44 | + import :: c_ptr |
| 45 | + type(c_ptr), value :: face |
| 46 | + end subroutine fortty_ft_done_face |
| 47 | + |
| 48 | + subroutine fortty_ft_get_metrics(face, cell_width, cell_height, & |
| 49 | + ascender, descender) & |
| 50 | + bind(C, name="fortty_ft_get_metrics") |
| 51 | + import :: c_int, c_ptr |
| 52 | + type(c_ptr), value :: face |
| 53 | + integer(c_int), intent(out) :: cell_width, cell_height |
| 54 | + integer(c_int), intent(out) :: ascender, descender |
| 55 | + end subroutine fortty_ft_get_metrics |
| 56 | + |
| 57 | + integer(c_int) function fortty_ft_render_glyph(face, codepoint, & |
| 58 | + bitmap, width, height, bearing_x, bearing_y, advance) & |
| 59 | + bind(C, name="fortty_ft_render_glyph") |
| 60 | + import :: c_int, c_ptr |
| 61 | + type(c_ptr), value :: face |
| 62 | + integer(c_int), value :: codepoint |
| 63 | + type(c_ptr), intent(out) :: bitmap |
| 64 | + integer(c_int), intent(out) :: width, height |
| 65 | + integer(c_int), intent(out) :: bearing_x, bearing_y, advance |
| 66 | + end function fortty_ft_render_glyph |
| 67 | + end interface |
| 68 | + |
| 69 | +contains |
| 70 | + |
| 71 | + ! Load a font from file |
| 72 | + function font_load(path, size_px) result(font) |
| 73 | + character(len=*), intent(in) :: path |
| 74 | + integer, intent(in) :: size_px |
| 75 | + type(font_t) :: font |
| 76 | + character(len=256) :: c_path |
| 77 | + integer(c_int) :: err |
| 78 | + integer(c_int) :: cw, ch, asc, desc |
| 79 | + |
| 80 | + ! Initialize FreeType |
| 81 | + err = fortty_ft_init(font%ft_library) |
| 82 | + if (err /= 0) then |
| 83 | + print *, "Error: Failed to initialize FreeType, error ", err |
| 84 | + return |
| 85 | + end if |
| 86 | + |
| 87 | + ! Load font face |
| 88 | + c_path = trim(path) // c_null_char |
| 89 | + err = fortty_ft_load_font(font%ft_library, c_path, int(size_px, c_int), font%ft_face) |
| 90 | + if (err /= 0) then |
| 91 | + print *, "Error: Failed to load font '", trim(path), "', error ", err |
| 92 | + call fortty_ft_done(font%ft_library) |
| 93 | + font%ft_library = c_null_ptr |
| 94 | + return |
| 95 | + end if |
| 96 | + |
| 97 | + ! Get metrics |
| 98 | + call fortty_ft_get_metrics(font%ft_face, cw, ch, asc, desc) |
| 99 | + font%size_px = size_px |
| 100 | + font%cell_width = cw |
| 101 | + font%cell_height = ch |
| 102 | + font%ascender = asc |
| 103 | + font%descender = desc |
| 104 | + font%loaded = .true. |
| 105 | + |
| 106 | + end function font_load |
| 107 | + |
| 108 | + ! Destroy font and free resources |
| 109 | + subroutine font_destroy(font) |
| 110 | + type(font_t), intent(inout) :: font |
| 111 | + |
| 112 | + if (c_associated(font%ft_face)) then |
| 113 | + call fortty_ft_done_face(font%ft_face) |
| 114 | + font%ft_face = c_null_ptr |
| 115 | + end if |
| 116 | + |
| 117 | + if (c_associated(font%ft_library)) then |
| 118 | + call fortty_ft_done(font%ft_library) |
| 119 | + font%ft_library = c_null_ptr |
| 120 | + end if |
| 121 | + |
| 122 | + font%loaded = .false. |
| 123 | + end subroutine font_destroy |
| 124 | + |
| 125 | + ! Render a glyph and return its data |
| 126 | + ! Returns bitmap pointer (valid until next render_glyph call) |
| 127 | + function font_render_glyph(font, codepoint, bitmap_ptr) result(glyph) |
| 128 | + type(font_t), intent(in) :: font |
| 129 | + integer, intent(in) :: codepoint |
| 130 | + type(c_ptr), intent(out) :: bitmap_ptr |
| 131 | + type(glyph_t) :: glyph |
| 132 | + integer(c_int) :: err, w, h, bx, by, adv |
| 133 | + |
| 134 | + call glyph_init(glyph) |
| 135 | + |
| 136 | + if (.not. font%loaded) then |
| 137 | + bitmap_ptr = c_null_ptr |
| 138 | + return |
| 139 | + end if |
| 140 | + |
| 141 | + err = fortty_ft_render_glyph(font%ft_face, int(codepoint, c_int), & |
| 142 | + bitmap_ptr, w, h, bx, by, adv) |
| 143 | + if (err /= 0) then |
| 144 | + bitmap_ptr = c_null_ptr |
| 145 | + return |
| 146 | + end if |
| 147 | + |
| 148 | + glyph%codepoint = codepoint |
| 149 | + glyph%width = w |
| 150 | + glyph%height = h |
| 151 | + glyph%bearing_x = bx |
| 152 | + glyph%bearing_y = by |
| 153 | + glyph%advance = adv |
| 154 | + glyph%valid = .true. |
| 155 | + |
| 156 | + end function font_render_glyph |
| 157 | + |
| 158 | +end module font_mod |