00001 /* F O N T . C 00002 * BRL-CAD 00003 * 00004 * Copyright (c) 2004-2012 United States Government as represented by 00005 * the U.S. Army Research Laboratory. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * version 2.1 as published by the Free Software Foundation. 00010 * 00011 * This library is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this file; see the file named COPYING for more 00018 * information. 00019 */ 00020 /** @addtogroup vlist */ 00021 /** @{ */ 00022 /** @file libbn/font.c 00023 * 00024 */ 00025 00026 #include "common.h" 00027 00028 #include <stdio.h> 00029 #include <math.h> 00030 #include <string.h> 00031 00032 #include "vmath.h" 00033 #include "bu.h" 00034 #include "bn.h" 00035 #include "vectfont.h" 00036 00037 /* References tp_xxx symbols from libbn/vectfont.c */ 00038 00039 /** 00040 * B N _ V L I S T _ 3 S T R I N G 00041 *@brief 00042 * Convert a string to a vlist. 00043 * 00044 * 'scale' is the width, in mm, of one character. 00045 * 00046 * @param vhead 00047 * @param free_hd source of free vlists 00048 * @param string string of chars to be plotted 00049 * @param origin lower left corner of 1st char 00050 * @param rot Transform matrix (WARNING: may xlate) 00051 * @param scale scale factor to change 1x1 char sz 00052 * 00053 */ 00054 void 00055 bn_vlist_3string(struct bu_list *vhead, 00056 struct bu_list *free_hd, /**< source of free vlists */ 00057 const char *string, /**< string of chars to be plotted */ 00058 const vect_t origin, /**< lower left corner of 1st char */ 00059 const mat_t rot, /**< Transform matrix (WARNING: may xlate) */ 00060 double scale) /**< scale factor to change 1x1 char sz */ 00061 00062 00063 { 00064 register unsigned char *cp; 00065 double offset; /* offset of char from given x, y */ 00066 int ysign; /* sign of y motion, either +1 or -1 */ 00067 vect_t temp; 00068 vect_t loc; 00069 mat_t xlate_to_origin; 00070 mat_t mat; 00071 00072 if ( string == NULL || *string == '\0' ) 00073 return; /* done before begun! */ 00074 00075 /* 00076 * The point "origin" will be the center of the axis rotation. 00077 * The text is located in a local coordinate system with the 00078 * lower left corner of the first character at (0, 0, 0), with 00079 * the text proceeding onward towards +X. 00080 * We need to rotate the text around its local (0, 0, 0), 00081 * and then translate to the user's designated "origin". 00082 * If the user provided translation or 00083 * scaling in his matrix, it will *also* be applied. 00084 */ 00085 MAT_IDN( xlate_to_origin ); 00086 MAT_DELTAS_VEC( xlate_to_origin, origin ); 00087 bn_mat_mul( mat, xlate_to_origin, rot ); 00088 00089 /* Check to see if initialization is needed */ 00090 if ( tp_cindex[040] == 0 ) tp_setup(); 00091 00092 /* Draw each character in the input string */ 00093 offset = 0; 00094 for ( cp = (unsigned char *)string; *cp; cp++, offset += scale ) { 00095 register int *p; /* pointer to stroke table */ 00096 register int stroke; 00097 00098 VSET( temp, offset, 0, 0 ); 00099 MAT4X3PNT( loc, mat, temp ); 00100 BN_ADD_VLIST(free_hd, vhead, loc, BN_VLIST_LINE_MOVE ); 00101 00102 for ( p = tp_cindex[*cp]; ((stroke= *p)) != LAST; p++ ) { 00103 int draw; 00104 00105 if ( (stroke)==NEGY ) { 00106 ysign = (-1); 00107 stroke = *++p; 00108 } else 00109 ysign = 1; 00110 00111 /* Detect & process pen control */ 00112 if ( stroke < 0 ) { 00113 stroke = -stroke; 00114 draw = 0; 00115 } else 00116 draw = 1; 00117 00118 /* stroke co-ordinates in string coord system */ 00119 VSET( temp, (stroke/11) * 0.1 * scale + offset, 00120 (ysign * (stroke%11)) * 0.1 * scale, 0 ); 00121 MAT4X3PNT( loc, mat, temp ); 00122 if ( draw ) { 00123 BN_ADD_VLIST( free_hd, vhead, loc, BN_VLIST_LINE_DRAW ); 00124 } else { 00125 BN_ADD_VLIST( free_hd, vhead, loc, BN_VLIST_LINE_MOVE ); 00126 } 00127 } 00128 } 00129 } 00130 00131 00132 /** 00133 * B N _ V L I S T _ 2 S T R I N G 00134 * @brief 00135 * Convert string to vlist in 2D 00136 * 00137 * A simpler interface, for those cases where the text lies 00138 * in the X-Y plane. 00139 * 00140 * @param vhead 00141 * @param free_hd source of free vlists 00142 * @param string string of chars to be plotted 00143 * @param x lower left corner of 1st char 00144 * @param y lower left corner of 1st char 00145 * @param scale scale factor to change 1x1 char sz 00146 * @param theta degrees ccw from X-axis 00147 * 00148 */ 00149 void 00150 bn_vlist_2string(struct bu_list *vhead, 00151 struct bu_list *free_hd, 00152 const char *string, 00153 double x, 00154 double y, 00155 double scale, 00156 double theta) 00157 { 00158 mat_t mat; 00159 vect_t p; 00160 00161 bn_mat_angles( mat, 0.0, 0.0, theta ); 00162 VSET( p, x, y, 0 ); 00163 bn_vlist_3string( vhead, free_hd, string, p, mat, scale ); 00164 } 00165 /** @} */ 00166 /* 00167 * Local Variables: 00168 * mode: C 00169 * tab-width: 8 00170 * indent-tabs-mode: t 00171 * c-file-style: "stroustrup" 00172 * End: 00173 * ex: shiftwidth=4 tabstop=8 00174 */