1
(***********************************************************************)
5
(* Jun Furuse, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 1999-2004 *)
8
(* Institut National de Recherche en Informatique et en Automatique. *)
9
(* Distributed only by permission. *)
11
(***********************************************************************)
13
(* $Id: fttext.ml,v 1.1 2007/01/18 10:29:57 rousse Exp $ *)
18
type 'a drawer = 'a -> int -> 'a;;
20
let func_darken_only org level =
21
let level = 255 - level in
22
{ r = if org.r > level then level else org.r;
23
g = if org.g > level then level else org.g;
24
b = if org.b > level then level else org.b };;
26
let func_red_only org level = { r = 255; g = 0; b = 0 };;
28
let unicode_of_latin s =
29
let ary = Array.create (String.length s) 0 in
30
for i = 0 to String.length s - 1 do
31
ary.(i) <- Char.code s.[i]
35
let unicode_of_euc_japan s = Jis_unicode.encode s;;
37
let draw_gen render_mode renderf rot func face px py string =
38
let matrix = matrix_rotate rot in
39
let curx = ref (0.0) and cury = ref (0.0) in
41
for i = 0 to Array.length string - 1 do
42
set_transform face matrix {ft_x = !curx; ft_y = !cury};
43
let advx, advy = renderf face string.(i) [] render_mode in
44
let binfo = get_bitmap_info face in
46
for y = 0 to binfo.bitmap_height - 1 do
47
for x = 0 to binfo.bitmap_width - 1 do
48
let z = read_bitmap face x y in
51
if z > 255 then 255 else z
54
let px = px + binfo.bitmap_left + x
55
and py = py - (binfo.bitmap_top - binfo.bitmap_height + y)
58
and py = py + (binfo.bitmap_top + binfo.bitmap_height - y)
66
curx := !curx +. advx;
67
cury := !cury +. advy;
70
let draw_rotated_text = draw_gen Render_Normal render_char;;
71
let draw_rotated_glyphs = draw_gen Render_Normal render_glyph;;
72
let draw_text = draw_rotated_text 0.0;;
73
let draw_glyphs = draw_rotated_glyphs 0.0;;
75
let draw_mono_rotated_text = draw_gen Render_Mono render_char;;
76
let draw_mono_rotated_glyphs = draw_gen Render_Mono render_glyph;;
77
let draw_mono_text = draw_mono_rotated_text 0.0;;
78
let draw_mono_glyphs = draw_mono_rotated_glyphs 0.0;;
85
val create : int -> int -> t
86
val destroy : t -> unit
87
val get : t -> int -> int -> elt
88
val set : t -> int -> int -> elt -> unit
89
val unsafe_get : t -> int -> int -> elt
90
val unsafe_set : t -> int -> int -> elt -> unit
93
module Make(T : T) = struct
95
let putpixel f bitmap = fun px py level ->
97
let orgcolor = T.get bitmap px py in
98
T.set bitmap px py (f orgcolor level)
102
let draw_rotated_text face func bitmap px py rot string =
103
draw_rotated_text rot (putpixel func bitmap) face px py string
105
let draw_rotated_glyphs face func bitmap px py rot string =
106
draw_rotated_glyphs rot (putpixel func bitmap) face px py string
108
let draw_text face func bitmap px py string =
109
draw_text (putpixel func bitmap) face px py string
111
let draw_glyphs face func bitmap px py string =
112
draw_glyphs (putpixel func bitmap) face px py string
114
let draw_mono_rotated_text face func bitmap px py rot string =
115
draw_mono_rotated_text rot (putpixel func bitmap) face px py string
117
let draw_mono_rotated_glyphs face func bitmap px py rot string =
118
draw_mono_rotated_glyphs rot (putpixel func bitmap) face px py string
120
let draw_mono_text face func bitmap px py string =
121
draw_mono_text (putpixel func bitmap) face px py string
123
let draw_mono_glyphs face func bitmap px py string =
124
draw_mono_glyphs (putpixel func bitmap) face px py string
128
let size_gen face loadf string =
130
and leftmost = ref None
131
and rightmost = ref None
132
and upmost = ref None
133
and downmost = ref None
135
for i = 0 to Array.length string - 1 do
136
let _advx, _advy = loadf face string.(i) [] in
137
let metrics = get_glyph_metrics face in
138
let left = metrics.gm_hori.bearingx +. !curx
139
and right = metrics.gm_hori.bearingx +. metrics.gm_width +. !curx
140
and up = metrics.gm_hori.bearingy
141
and down = metrics.gm_hori.bearingy -. metrics.gm_height
143
begin match !leftmost with
144
| None -> leftmost := Some left
145
| Some x when x > left -> leftmost := Some left
147
begin match !rightmost with
148
| None -> rightmost := Some right
149
| Some x when x < right -> rightmost := Some right
151
begin match !upmost with
152
| None -> upmost := Some up
153
| Some x when x < up -> upmost := Some up
155
begin match !downmost with
156
| None -> downmost := Some down
157
| Some x when x > down -> downmost := Some down
159
curx := !curx +. metrics.gm_hori.advance
161
match !leftmost, !downmost, !rightmost, !upmost with
162
Some l, Some d, Some r, Some u -> l,d,r,u
163
| _ -> assert false;;
165
let size face string = size_gen face load_char string;;
166
let size_of_glyphs face string = size_gen face load_glyph string;;
168
let vector_gen loadf turn_y rot func face px py string =
169
let matrix = matrix_rotate rot in
172
{ matrix with ft_xy = -. matrix.ft_xy;
173
ft_yy = -. matrix.ft_yy; }
176
let curx = ref px and cury = ref py in
178
for i = 0 to Array.length string - 1 do
179
set_transform face matrix {ft_x = !curx; ft_y = !cury};
180
let advx, advy = loadf face string.(i) [] in
181
func (get_outline_contents face);
182
curx := !curx +. advx;
183
cury := !cury +. advy
186
let vector_text turn_y func face px py rot string =
187
vector_gen load_char turn_y rot func face px py string;;
189
let vector_glyphs turn_y func face px py rot string =
190
vector_gen load_glyph turn_y rot func face px py string;;