summaryrefslogtreecommitdiff
path: root/otherlibs/graph/text.c
blob: 3f2032a12b2ce8c92d84f7782af0b83a2617e30e (plain)
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
/***********************************************************************/
/*                                                                     */
/*                           Objective Caml                            */
/*                                                                     */
/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1996 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License.         */
/*                                                                     */
/***********************************************************************/

/* $Id$ */

#include "libgraph.h"
#include <alloc.h>

XFontStruct * grfont = NULL;

static void gr_font(char *fontname)
{
  XFontStruct * font = XLoadQueryFont(grdisplay, fontname);
  if (font == NULL) gr_fail("cannot find font %s", fontname);
  if (grfont != NULL) XFreeFont(grdisplay, grfont);
  grfont = font;
  XSetFont(grdisplay, grwindow.gc, grfont->fid);
  XSetFont(grdisplay, grbstore.gc, grfont->fid);
}

value gr_set_font(value fontname)
{
  gr_check_open();
  gr_font(String_val(fontname));
  return Val_unit;
}

value gr_set_text_size (value sz)
{
  return Val_unit;
}

static void gr_draw_text(char *txt, int len)
{
  if (grfont == NULL) gr_font(DEFAULT_FONT);
  XDrawString(grdisplay, grwindow.win, grwindow.gc,
              grx, Wcvt(gry) - grfont->descent + 1, txt, len);
  XDrawString(grdisplay, grbstore.win, grbstore.gc,
              grx, Bcvt(gry) - grfont->descent + 1, txt, len);
  grx += XTextWidth(grfont, txt, len);
  XFlush(grdisplay);
}

value gr_draw_char(value chr)
{
  char str[1];
  gr_check_open();
  str[0] = Int_val(chr);
  gr_draw_text(str, 1);
  return Val_unit;
}
  
value gr_draw_string(value str)
{
  gr_check_open();
  gr_draw_text(String_val(str), string_length(str));
  return Val_unit;
}

value gr_text_size(value str)
{
  int width;
  value res;
  gr_check_open();
  if (grfont == NULL) gr_font(DEFAULT_FONT);
  width = XTextWidth(grfont, String_val(str), string_length(str));
  res = alloc_small(2, 0);
  Field(res, 0) = Val_int(width);
  Field(res, 1) = Val_int(grfont->ascent + grfont->descent);
  return res;
}