1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
open Images
open Freetype
type 'a drawer = 'a -> int -> 'a
let func_darken_only org level =
let level = 255 - level in
{ r = if org.r > level then level else org.r;
g = if org.g > level then level else org.g;
b = if org.b > level then level else org.b }
let func_red_only _org _level = { r = 255; g = 0; b = 0 }
let unicode_of_latin s =
Array.init (String.length s) @@ fun i -> Char.code s.[i]
let unicode_of_euc_japan s = Jis_unicode.encode s
let draw_gen render_mode renderf rot func face px py string =
let matrix = matrix_rotate rot in
let curx = ref (0.0) and cury = ref (0.0) in
for i = 0 to Array.length string - 1 do
set_transform face matrix {ft_x = !curx; ft_y = !cury};
let advx, advy = renderf face string.(i) [] render_mode in
let binfo = get_bitmap_info face in
for y = 0 to binfo.bitmap_height - 1 do
for x = 0 to binfo.bitmap_width - 1 do
let z = read_bitmap face x y in
let level =
if z < 0 then 0 else
if z > 255 then 255 else z
in
try
let px = px + binfo.bitmap_left + x
and py = py - (binfo.bitmap_top - binfo.bitmap_height + y)
in
func px py level
with
Out_of_image -> ()
done;
done;
curx := !curx +. advx;
cury := !cury +. advy;
done
let draw_rotated_text = draw_gen Render_Normal render_char
let draw_rotated_glyphs = draw_gen Render_Normal render_glyph
let draw_text = draw_rotated_text 0.0
let draw_glyphs = draw_rotated_glyphs 0.0
let draw_mono_rotated_text = draw_gen Render_Mono render_char
let draw_mono_rotated_glyphs = draw_gen Render_Mono render_glyph
let draw_mono_text = draw_mono_rotated_text 0.0
let draw_mono_glyphs = draw_mono_rotated_glyphs 0.0
module type T = sig
type t
type elt
val create : int -> int -> t
val destroy : t -> unit
val get : t -> int -> int -> elt
val set : t -> int -> int -> elt -> unit
val unsafe_get : t -> int -> int -> elt
val unsafe_set : t -> int -> int -> elt -> unit
end
module Make(T : T) = struct
let putpixel f bitmap = fun px py level ->
try
let orgcolor = T.get bitmap px py in
T.set bitmap px py (f orgcolor level)
with
Out_of_image -> ()
let draw_rotated_text face func bitmap px py rot string =
draw_rotated_text rot (putpixel func bitmap) face px py string
let draw_rotated_glyphs face func bitmap px py rot string =
draw_rotated_glyphs rot (putpixel func bitmap) face px py string
let draw_text face func bitmap px py string =
draw_text (putpixel func bitmap) face px py string
let draw_glyphs face func bitmap px py string =
draw_glyphs (putpixel func bitmap) face px py string
let draw_mono_rotated_text face func bitmap px py rot string =
draw_mono_rotated_text rot (putpixel func bitmap) face px py string
let draw_mono_rotated_glyphs face func bitmap px py rot string =
draw_mono_rotated_glyphs rot (putpixel func bitmap) face px py string
let draw_mono_text face func bitmap px py string =
draw_mono_text (putpixel func bitmap) face px py string
let draw_mono_glyphs face func bitmap px py string =
draw_mono_glyphs (putpixel func bitmap) face px py string
end
let size_gen face loadf string =
let curx = ref 0.0
and leftmost = ref None
and rightmost = ref None
and upmost = ref None
and downmost = ref None
in
for i = 0 to Array.length string - 1 do
let _advx, _advy = loadf face string.(i) [] in
let metrics = get_glyph_metrics face in
let left = metrics.gm_hori.bearingx +. !curx
and right = metrics.gm_hori.bearingx +. metrics.gm_width +. !curx
and up = metrics.gm_hori.bearingy
and down = metrics.gm_hori.bearingy -. metrics.gm_height
in
begin match !leftmost with
| None -> leftmost := Some left
| Some x when x > left -> leftmost := Some left
| _ -> () end;
begin match !rightmost with
| None -> rightmost := Some right
| Some x when x < right -> rightmost := Some right
| _ -> () end;
begin match !upmost with
| None -> upmost := Some up
| Some x when x < up -> upmost := Some up
| _ -> () end;
begin match !downmost with
| None -> downmost := Some down
| Some x when x > down -> downmost := Some down
| _ -> () end;
curx := !curx +. metrics.gm_hori.advance
done;
match !leftmost, !downmost, !rightmost, !upmost with
Some l, Some d, Some r, Some u -> l,d,r,u
| _ -> assert false
let size face string = size_gen face load_char string
let size_of_glyphs face string = size_gen face load_glyph string
let vector_gen loadf turn_y rot func face px py string =
let matrix = matrix_rotate rot in
let matrix =
if turn_y then
{ matrix with ft_xy = -. matrix.ft_xy;
ft_yy = -. matrix.ft_yy; }
else matrix
in
let curx = ref px and cury = ref py in
for i = 0 to Array.length string - 1 do
set_transform face matrix {ft_x = !curx; ft_y = !cury};
let advx, advy = loadf face string.(i) [] in
func (get_outline_contents face);
curx := !curx +. advx;
cury := !cury +. advy
done
let vector_text turn_y func face px py rot string =
vector_gen load_char turn_y rot func face px py string
let vector_glyphs turn_y func face px py rot string =
vector_gen load_glyph turn_y rot func face px py string