~ubuntu-branches/debian/squeeze/camlimages/squeeze

« back to all changes in this revision

Viewing changes to src/fttext.ml

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Le Gall, Ralf Treinen, Sylvain Le Gall
  • Date: 2009-03-05 00:19:32 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090305001932-f0hstlmun8hxvs0r
Tags: 1:3.0.1-1
[ Ralf Treinen ]
* Updated debian/watch.

[ Sylvain Le Gall ]
* New upstream version:
  * Remove useless patches
  * Adapt debian/rules and other debhelper files
  * Add debian/patches/fix_3_0_1 to fix various problem (probably due to
    OCaml 3.11 migration)
* Depends on version 2.12 of lablgtk2
* Add dh-ocaml build-dependency (rules/ocaml.mk)
* Add ${misc:Depends} to dependencies
* Update Homepage field into debian/control and debian/copyright
* Add license version for debian packaging
* Directly use eng.html rather than creating a linked index.html file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(***********************************************************************)
 
2
(*                                                                     *)
 
3
(*                           Objective Caml                            *)
 
4
(*                                                                     *)
 
5
(*            Jun Furuse, projet Cristal, INRIA Rocquencourt           *)
 
6
(*                                                                     *)
 
7
(*  Copyright 1999-2004                                                *)
 
8
(*  Institut National de Recherche en Informatique et en Automatique.  *)
 
9
(*  Distributed only by permission.                                    *)
 
10
(*                                                                     *)
 
11
(***********************************************************************)
 
12
 
 
13
(* $Id: fttext.ml,v 1.1 2007/01/18 10:29:57 rousse Exp $ *)
 
14
 
 
15
open Images;;
 
16
open Freetype;;
 
17
 
 
18
type 'a drawer = 'a -> int -> 'a;;
 
19
 
 
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 };;
 
25
 
 
26
let func_red_only org level = { r = 255; g = 0; b = 0 };;
 
27
 
 
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]
 
32
  done;
 
33
  ary;;
 
34
 
 
35
let unicode_of_euc_japan s = Jis_unicode.encode s;;
 
36
 
 
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
 
40
 
 
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
 
45
 
 
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
 
49
        let level = 
 
50
          if z < 0 then 0 else 
 
51
          if z > 255 then 255 else z
 
52
        in
 
53
        try
 
54
          let px = px + binfo.bitmap_left + x
 
55
          and py = py - (binfo.bitmap_top - binfo.bitmap_height + y)
 
56
(*
 
57
            in
 
58
            and py = py + (binfo.bitmap_top + binfo.bitmap_height - y)
 
59
*)
 
60
          in
 
61
          func px py level
 
62
        with
 
63
          Out_of_image -> ()
 
64
      done;
 
65
    done;
 
66
    curx := !curx +. advx;
 
67
    cury := !cury +. advy;
 
68
  done;;
 
69
 
 
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;;
 
74
 
 
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;;
 
79
 
 
80
 
 
81
module type T = sig
 
82
  type t
 
83
  type elt
 
84
 
 
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
 
91
end;;
 
92
 
 
93
module Make(T : T) = struct
 
94
 
 
95
  let putpixel f bitmap = fun px py level ->
 
96
    try
 
97
      let orgcolor = T.get bitmap px py in
 
98
      T.set bitmap px py (f orgcolor level) 
 
99
    with
 
100
      Out_of_image -> ()
 
101
 
 
102
  let draw_rotated_text face func bitmap px py rot string =
 
103
    draw_rotated_text rot (putpixel func bitmap) face px py string
 
104
 
 
105
  let draw_rotated_glyphs face func bitmap px py rot string =
 
106
    draw_rotated_glyphs rot (putpixel func bitmap) face px py string
 
107
 
 
108
  let draw_text face func bitmap px py string =
 
109
    draw_text (putpixel func bitmap) face px py string
 
110
 
 
111
  let draw_glyphs face func bitmap px py string =
 
112
    draw_glyphs (putpixel func bitmap) face px py string
 
113
 
 
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
 
116
 
 
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
 
119
 
 
120
  let draw_mono_text face func bitmap px py string =
 
121
    draw_mono_text (putpixel func bitmap) face px py string
 
122
 
 
123
  let draw_mono_glyphs face func bitmap px py string =
 
124
    draw_mono_glyphs (putpixel func bitmap) face px py string
 
125
 
 
126
end;;
 
127
 
 
128
let size_gen face loadf string =
 
129
  let curx = ref 0.0
 
130
  and leftmost = ref None
 
131
  and rightmost = ref None
 
132
  and upmost = ref None
 
133
  and downmost = ref None
 
134
  in
 
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
 
142
    in
 
143
    begin match !leftmost with
 
144
    | None -> leftmost := Some left
 
145
    | Some x when x > left -> leftmost := Some left 
 
146
    | _ -> () end;
 
147
    begin match !rightmost with
 
148
    | None -> rightmost := Some right 
 
149
    | Some x when x < right -> rightmost := Some right 
 
150
    | _ -> () end;
 
151
    begin match !upmost with
 
152
    | None   -> upmost := Some up 
 
153
    | Some x when x < up -> upmost := Some up 
 
154
    | _ -> () end;
 
155
    begin match !downmost with
 
156
    | None   -> downmost := Some down 
 
157
    | Some x when x > down -> downmost := Some down 
 
158
    | _ -> () end;
 
159
    curx := !curx +. metrics.gm_hori.advance
 
160
  done;
 
161
  match !leftmost, !downmost, !rightmost, !upmost with
 
162
    Some l, Some d, Some r, Some u -> l,d,r,u
 
163
  | _ -> assert false;;
 
164
 
 
165
let size face string = size_gen face load_char string;;
 
166
let size_of_glyphs face string = size_gen face load_glyph string;;
 
167
 
 
168
let vector_gen loadf turn_y rot func face px py string =
 
169
  let matrix = matrix_rotate rot in
 
170
  let matrix = 
 
171
    if turn_y then 
 
172
      { matrix with ft_xy = -. matrix.ft_xy;
 
173
        ft_yy = -. matrix.ft_yy; }
 
174
    else matrix
 
175
  in 
 
176
  let curx = ref px and cury = ref py in
 
177
 
 
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
 
184
  done;;
 
185
 
 
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;;
 
188
  
 
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;;