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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2003 by Warren Lyford Delano of DeLano Scientific.
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information.
H* --------------------------------------------------\-----------------
I* Additional authors of this source file include:
-*
-*
-*
Z* -------------------------------------------------------------------
*/
#include"os_python.h"
#include"MemoryDebug.h"
#include"Text.h"
#include"Font.h"
#include"FontGLUT.h"
#include"FontType.h"
#include"Color.h"
#include"Vector.h"
#ifdef _PYMOL_FREETYPE
#include "FontTTF.h"
#include "FontTTF2.h"
#endif
#define FONT_NAME_MAX 255
#define TEXT_DEFAULT_SIZE 12.0F
static const float _255 = 255.0F;
static const float _499 = 0.4999F;
typedef struct {
int Src;
int Code;
char Name[FONT_NAME_MAX];
int Mode;
int Style;
CFont *Font;
} ActiveRec;
struct _CText {
int NActive;
ActiveRec *Active;
float Pos[4];
float WorldPos[4];
float ScreenWorldOffset[3];
float Color[4];
unsigned char UColor[4];
unsigned char OutlineColor[4];
int Default_ID;
int Flat;
};
static void TextUpdateUColor(CText * I)
{
I->UColor[0] = (unsigned char) (_255 * I->Color[0] + _499);
I->UColor[1] = (unsigned char) (_255 * I->Color[1] + _499);
I->UColor[2] = (unsigned char) (_255 * I->Color[2] + _499);
I->UColor[3] = (unsigned char) (_255 * I->Color[3] + _499);
}
void TextSetPosNColor(PyMOLGlobals * G, float *pos, float *color)
{
CText *I = G->Text;
copy3f(pos, I->Pos);
copy3f(color, I->Color);
I->Flat = false;
I->Pos[3] = 1.0F;
I->Color[3] = 1.0F;
TextUpdateUColor(I);
}
void TextAdvance(PyMOLGlobals * G, float advance)
{
G->Text->Pos[0] += advance;
}
void TextSetLabPos(PyMOLGlobals * G, float *pos, LabPosType * labpos, const char *text)
{
if((!labpos) || (!labpos->mode))
TextSetPos(G, pos);
else {
CText *I = G->Text;
switch (labpos->mode) {
default:
copy3f(pos, I->Pos);
add3f(labpos->offset, I->Pos, I->Pos);
break;
}
}
}
void TextIndent(PyMOLGlobals * G, float x, float y)
{
CText *I = G->Text;
I->Pos[0] -= x;
I->Pos[1] -= y;
}
void TextSetPos(PyMOLGlobals * G, float *pos)
{
CText *I = G->Text;
copy3f(pos, I->Pos);
I->Pos[3] = 1.0F;
}
void TextSetWorldPos(PyMOLGlobals * G, float *pos)
{
CText *I = G->Text;
copy3f(pos, I->WorldPos);
I->WorldPos[3] = 1.0F;
}
float *TextGetWorldPos(PyMOLGlobals * G){
CText *I = G->Text;
return I->WorldPos;
}
void TextSetScreenWorldOffset(PyMOLGlobals * G, float *pos)
{
CText *I = G->Text;
I->ScreenWorldOffset[0] = -pos[0];
I->ScreenWorldOffset[1] = -pos[1];
I->ScreenWorldOffset[2] = -pos[2];
}
float *TextGetScreenWorldOffset(PyMOLGlobals * G){
CText *I = G->Text;
return I->ScreenWorldOffset;
}
void TextDrawSubStrFast(PyMOLGlobals * G, const char *c, int x, int y, int start, int n ORTHOCGOARG)
{
c += start;
TextSetPos2i(G, x, y);
if(n)
while(*c) {
n--;
TextDrawChar(G, *(c++) ORTHOCGOARGVAR);
if(n <= 0)
break;
}
}
void TextDrawCharRepeat(PyMOLGlobals * G, char c, int x, int y, int start, int n ORTHOCGOARG)
{
c += start;
TextSetPos2i(G, x, y);
while(n) {
n--;
TextDrawChar(G, c ORTHOCGOARGVAR);
}
}
void TextSetPos2i(PyMOLGlobals * G, int x, int y)
{
CText *I = G->Text;
I->Pos[0] = (float) x;
I->Pos[1] = (float) y;
I->Pos[2] = 0.0F;
I->Pos[3] = 1.0F;
}
static void TextSetPos3f(PyMOLGlobals * G, float x, float y, float z)
{
CText *I = G->Text;
I->Pos[0] = x;
I->Pos[1] = y;
I->Pos[2] = z;
I->Pos[3] = 1.0F;
}
void TextSetColor(PyMOLGlobals * G, float *color)
{
CText *I = G->Text;
copy3f(color, I->Color);
I->Color[3] = 1.0F;
I->Flat = false;
TextUpdateUColor(I);
}
void TextSetColor3f(PyMOLGlobals * G, float red, float green, float blue)
{
CText *I = G->Text;
I->Flat = false;
I->Color[0] = red;
I->Color[1] = green;
I->Color[2] = blue;
I->Color[3] = 1.0F;
TextUpdateUColor(I);
}
void TextSetOutlineColor(PyMOLGlobals * G, int color)
{
CText *I = G->Text;
if(color >= 0) {
float *fcolor = ColorGet(G, color);
I->OutlineColor[0] = (unsigned char) (_255 * fcolor[0]);
I->OutlineColor[1] = (unsigned char) (_255 * fcolor[1]);
I->OutlineColor[2] = (unsigned char) (_255 * fcolor[2]);
I->OutlineColor[3] = 0xFF;
} else {
I->OutlineColor[3] = 0;
}
}
static const float _inv255 = 1.0F / 255.0F;
void TextSetPickColor(PyMOLGlobals * G, int first_pass, int index)
{
CText *I = G->Text;
if(!first_pass)
index = (index >> 12); /* high order bits */
I->Flat = true;
I->UColor[0] = ((unsigned char) ((index & 0xF) << 4));
I->UColor[1] = ((unsigned char) ((index & 0xF0) | 0x8));
I->UColor[2] = ((unsigned char) ((index & 0xF00) >> 4));
I->UColor[3] = 0xFF;
I->Color[0] = I->UColor[0] * _inv255;
I->Color[1] = I->UColor[1] * _inv255;
I->Color[2] = I->UColor[2] * _inv255;
I->Color[3] = 1.0F;
}
float *TextGetPos(PyMOLGlobals * G)
{
CText *I = G->Text;
return I->Pos;
}
float *TextGetColor(PyMOLGlobals * G)
{
CText *I = G->Text;
return I->Color;
}
void TextGetColorUChar(PyMOLGlobals * G, unsigned char *red,
unsigned char *green, unsigned char *blue, unsigned char *alpha)
{
CText *I = G->Text;
*red = I->UColor[0];
*green = I->UColor[1];
*blue = I->UColor[2];
*alpha = I->UColor[3];
}
void TextGetOutlineColor(PyMOLGlobals * G,
unsigned char *red,
unsigned char *green, unsigned char *blue, unsigned char *alpha)
{
CText *I = G->Text;
*red = I->OutlineColor[0];
*green = I->OutlineColor[1];
*blue = I->OutlineColor[2];
*alpha = I->OutlineColor[3];
}
const char *TextRenderOpenGL(PyMOLGlobals * G, RenderInfo * info, int text_id,
const char *st, float size, float *rpos, CGO *shaderCGO)
{
CText *I = G->Text;
CFont *font;
FontRenderOpenGLFn *fn;
if((text_id < 0) || (text_id >= I->NActive))
text_id = 0;
if(st && (*st)) {
if((text_id >= 0) && (text_id < I->NActive)) {
font = I->Active[text_id].Font;
if(I->Flat)
fn = font->fRenderOpenGLFlat;
else
fn = font->fRenderOpenGL;
if(fn)
return fn(info, font, st, size, rpos SHADERCGOARGVAR);
}
/* make sure we got to end of string */
if(*st)
while(*(st++));
}
return st;
}
void TextDrawStrAt(PyMOLGlobals * G, const char *st, int x, int y ORTHOCGOARG)
{
CText *I = G->Text;
TextSetPos3f(G, (float) x, (float) y, 0.0F);
TextRenderOpenGL(G, NULL, I->Default_ID, st, TEXT_DEFAULT_SIZE, NULL ORTHOCGOARGVAR);
}
void TextDrawStr(PyMOLGlobals * G, const char *st ORTHOCGOARG)
{
CText *I = G->Text;
TextRenderOpenGL(G, NULL, I->Default_ID, st, TEXT_DEFAULT_SIZE, NULL ORTHOCGOARGVAR);
}
void TextDrawChar(PyMOLGlobals * G, char ch ORTHOCGOARG)
{
char st[2] = { 0, 0 };
CText *I = G->Text;
st[0] = ch;
TextRenderOpenGL(G, NULL, I->Default_ID, st, TEXT_DEFAULT_SIZE, NULL ORTHOCGOARGVAR);
}
const char *TextRenderRay(PyMOLGlobals * G, CRay * ray, int text_id,
const char *st, float size, float *rpos)
{
CText *I = G->Text;
CFont *font;
FontRenderRayFn *fn;
if((text_id < 0) || (text_id >= I->NActive))
text_id = 0;
if(st && (*st)) {
if((text_id >= 0) && (text_id < I->NActive)) {
font = I->Active[text_id].Font;
if(size >= 0.0F)
size *= ray->Magnified;
fn = font->fRenderRay;
if(fn)
return fn(ray, font, st, size, rpos);
}
/* make sure we got to end of string */
if(*st)
while(*(st++));
}
return st;
}
int TextInit(PyMOLGlobals * G)
{
CText *I = NULL;
if((I = (G->Text = Calloc(CText, 1)))) {
I->NActive = 0;
I->Active = VLACalloc(ActiveRec, 10);
I->Default_ID = 0;
I->Flat = false;
/* font 0 is old reliable GLUT 8x13 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, cFontGLUT8x13);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = cFontGLUT8x13;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* font 1 is GLUT 9x15 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, cFontGLUT9x15);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = cFontGLUT9x15;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* font 2 is GLUT Helvetica10 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, cFontGLUTHel10);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = cFontGLUTHel10;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* font 3 is GLUT Helvetica12 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, cFontGLUTHel12);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = cFontGLUTHel12;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* font 4 is GLUT Helvetica18 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, cFontGLUTHel18);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = cFontGLUTHel18;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
#ifdef _PYMOL_FREETYPE
/* 5 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontTypeNew(G, TTF_DejaVuSans_dat, TTF_DejaVuSans_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 6 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSans_Oblique_dat, TTF_DejaVuSans_Oblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 7 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSans_Bold_dat, TTF_DejaVuSans_Bold_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 8 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSans_BoldOblique_dat, TTF_DejaVuSans_BoldOblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 9 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontTypeNew(G, TTF_DejaVuSerif_dat, TTF_DejaVuSerif_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 10 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSerif_Bold_dat, TTF_DejaVuSerif_Bold_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 11 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSansMono_dat, TTF_DejaVuSansMono_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 12 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSansMono_Oblique_dat, TTF_DejaVuSansMono_Oblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 13 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSansMono_Bold_dat, TTF_DejaVuSansMono_Bold_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 14 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSansMono_BoldOblique_dat,
TTF_DejaVuSansMono_BoldOblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* Gentium */
/* 15 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontTypeNew(G, TTF_GenR102_dat, TTF_GenR102_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 16 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontTypeNew(G, TTF_GenI102_dat, TTF_GenI102_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* back to DejaVu for the last two */
/* 17 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSerif_Oblique_dat, TTF_DejaVuSerif_Oblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
/* 18 */
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font =
FontTypeNew(G, TTF_DejaVuSerif_BoldOblique_dat, TTF_DejaVuSerif_BoldOblique_len);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcFreeType;
I->Active[I->NActive].Font->TextID = I->NActive;
I->NActive++;
}
#endif
return 1;
} else
return 0;
}
int TextGetFontID(PyMOLGlobals * G, int src, int code, const char *name, int mode, int style)
{
/* first, return the font code if it is already active */
CText *I = G->Text;
{
int a;
ActiveRec *rec = I->Active;
for(a = 0; I->NActive; a++) {
if((src == rec->Src) &&
(code == rec->Code) && (mode == rec->Mode) && (style == rec->Style))
if(((!name) && (!rec->Name[0])) || (name && (strcmp(name, rec->Name) == 0))) {
return a;
}
rec++;
}
}
switch (src) {
case cTextSrcGLUT:
VLACheck(I->Active, ActiveRec, I->NActive);
I->Active[I->NActive].Font = FontGLUTNew(G, code);
if(I->Active[I->NActive].Font) {
I->Active[I->NActive].Src = cTextSrcGLUT;
I->Active[I->NActive].Code = code;
I->NActive++;
}
break;
case cTextSrcFreeType:
break;
}
return -1;
}
void TextFree(PyMOLGlobals * G)
{
CText *I = G->Text;
int a;
CFont *fp;
for(a = 0; a < I->NActive; a++) {
fp = I->Active[a].Font;
if(fp && fp->fFree)
fp->fFree(fp);
}
VLAFreeP(I->Active);
FreeP(G->Text);
}
|