BRL-CAD
font.c
Go to the documentation of this file.
1 /* F O N T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <math.h>
25 #include <string.h>
26 
27 #include "bu/malloc.h"
28 #include "bu/list.h"
29 #include "bu/log.h"
30 #include "vmath.h"
31 #include "bn/mat.h"
32 #include "bn/vectfont.h"
33 #include "bn/vlist.h"
34 #include "vectfont.h"
35 
36 
37 void
38 bn_vlist_3string(struct bu_list *vhead,
39  struct bu_list *free_hd, /* source of free vlists */
40  const char *string, /* string of chars to be plotted */
41  const vect_t origin, /* lower left corner of 1st char */
42  const mat_t rot, /* Transform matrix (WARNING: may xlate) */
43  double scale) /* scale factor to change 1x1 char sz */
44 {
45  register unsigned char *cp;
46  double offset; /* offset of char from given x, y */
47  int ysign; /* sign of y motion, either +1 or -1 */
48  vect_t temp;
49  vect_t loc;
50  mat_t xlate_to_origin;
51  mat_t mat;
52 
53  if ( string == NULL || *string == '\0' )
54  return; /* done before begun! */
55 
56  /*
57  * The point "origin" will be the center of the axis rotation.
58  * The text is located in a local coordinate system with the
59  * lower left corner of the first character at (0, 0, 0), with
60  * the text proceeding onward towards +X.
61  * We need to rotate the text around its local (0, 0, 0),
62  * and then translate to the user's designated "origin".
63  * If the user provided translation or
64  * scaling in his matrix, it will *also* be applied.
65  */
66  MAT_IDN( xlate_to_origin );
67  MAT_DELTAS_VEC( xlate_to_origin, origin );
68  bn_mat_mul( mat, xlate_to_origin, rot );
69 
70  /* Draw each character in the input string */
71  offset = 0;
72  for ( cp = (unsigned char *)string; *cp; cp++, offset += scale ) {
73  register int *p; /* pointer to stroke table */
74  register int stroke;
75 
76  VSET( temp, offset, 0, 0 );
77  MAT4X3PNT( loc, mat, temp );
78  BN_ADD_VLIST(free_hd, vhead, loc, BN_VLIST_LINE_MOVE );
79 
80  for ( p = tp_getchar(cp); ((stroke= *p)) != LAST; p++ ) {
81  int draw;
82 
83  if ( (stroke)==NEGY ) {
84  ysign = (-1);
85  stroke = *++p;
86  } else
87  ysign = 1;
88 
89  /* Detect & process pen control */
90  if ( stroke < 0 ) {
91  stroke = -stroke;
92  draw = 0;
93  } else
94  draw = 1;
95 
96  /* stroke co-ordinates in string coord system */
97  VSET( temp, (stroke/11) * 0.1 * scale + offset,
98  (ysign * (stroke%11)) * 0.1 * scale, 0 );
99  MAT4X3PNT( loc, mat, temp );
100  if ( draw ) {
101  BN_ADD_VLIST( free_hd, vhead, loc, BN_VLIST_LINE_DRAW );
102  } else {
103  BN_ADD_VLIST( free_hd, vhead, loc, BN_VLIST_LINE_MOVE );
104  }
105  }
106  }
107 }
108 
109 
110 void
111 bn_vlist_2string(struct bu_list *vhead,
112  struct bu_list *free_hd,
113  const char *string,
114  double x,
115  double y,
116  double scale,
117  double theta)
118 {
119  mat_t mat;
120  vect_t p;
121 
122  bn_mat_angles( mat, 0.0, 0.0, theta );
123  VSET( p, x, y, 0 );
124  bn_vlist_3string( vhead, free_hd, string, p, mat, scale );
125 }
126 
127 /*
128  * Local Variables:
129  * mode: C
130  * tab-width: 8
131  * indent-tabs-mode: t
132  * c-file-style: "stroustrup"
133  * End:
134  * ex: shiftwidth=4 tabstop=8
135  */
Definition: list.h:118
#define VSET(a, b, c, d)
Definition: color.c:53
Header file for the BRL-CAD common definitions.
int * tp_getchar(const unsigned char *c)
Definition: vectfont.c:1104
void bn_vlist_2string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, double x, double y, double scale, double theta)
Definition: font.c:111
#define BN_VLIST_LINE_MOVE
Definition: vlist.h:82
#define BN_VLIST_LINE_DRAW
Definition: vlist.h:83
#define BN_ADD_VLIST(_free_hd, _dest_hd, pnt, draw)
Definition: vlist.h:123
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
void bn_mat_angles(mat_t mat, double alpha, double beta, double ggamma)
void bn_vlist_3string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, const vect_t origin, const mat_t rot, double scale)
Definition: font.c:38