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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
|
/*
* OGLFT: A library for drawing text with OpenGL using the FreeType library
* Copyright (C) 2002 lignum Computing, Inc. <oglft@lignumcomputing.com>
* $Id: OGLFT.cpp,v 1.11 2003/10/01 14:21:18 allen Exp $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <utility>
#include "OGLFT.h"
int wstrlen(const wchar_t * s)
{
int r = 0;
while (*s++) r++;
return r;
}
namespace OGLFT
{
FT_Library ft_library;
bool Init_FT(void)
{
FT_Error error = FT_Init_FreeType(&ft_library);
if(error != 0) std::cerr << "[OGLFT] Could not initialize the FreeType library." << std::endl;
return (error == 0);
}
bool Uninit_FT(void)
{
FT_Error error = FT_Done_FreeType(ft_library);
if(error != 0) std::cerr << "[OGLFT] Could not terminate the FreeType library." << std::endl;
return (error == 0);
}
// Load a new face
Face::Face (const char* filename, float point_size, FT_UInt resolution)
: point_size_(point_size), resolution_(resolution)
{
valid_ = true;
FT_Face ft_face;
FT_Error error = FT_New_Face(ft_library, filename, 0, &ft_face);
if(error != 0)
{
valid_ = false;
return;
}
// As of FreeType 2.1: only a UNICODE charmap is automatically activated.
// If no charmap is activated automatically, just use the first one.
if(ft_face->charmap == 0 && ft_face->num_charmaps > 0) FT_Select_Charmap(ft_face, ft_face->charmaps[0]->encoding);
faces_.push_back(FaceData(ft_face));
init();
}
// Go with a face that the user has already opened.
Face::Face (FT_Face face, float point_size, FT_UInt resolution)
: point_size_(point_size), resolution_(resolution)
{
valid_ = true;
// As of FreeType 2.1: only a UNICODE charmap is automatically activated.
// If no charmap is activated automatically, just use the first one.
if(face->charmap == 0 && face->num_charmaps > 0) FT_Select_Charmap(face, face->charmaps[0]->encoding);
faces_.push_back(FaceData(face, false));
init();
}
// Standard initialization behavior once the font file is opened.
void Face::init (void)
{
// By default, each glyph is compiled into a display list the first
// time it is encountered
compile_mode_ = COMPILE;
// By default, all drawing is wrapped with push/pop matrix so that the
// MODELVIEW matrix is not modified. If advance_ is set, then subsequent
// drawings follow from the advance of the last glyph rendered.
advance_ = false;
// Initialize the default colors
foreground_color_[R] = 0.; foreground_color_[G] = 0.; foreground_color_[B] = 0.; foreground_color_[A] = 1.;
background_color_[R] = 1.; background_color_[G] = 1.; background_color_[B] = 1.; background_color_[A] = 0.;
// The default positioning of the text is at the origin of the first glyph
horizontal_justification_ = ORIGIN;
vertical_justification_ = BASELINE;
// By default, strings are rendered in their nominal direction
string_rotation_ = 0;
// setCharacterRotationReference calls the virtual function clearCaches()
// so it is up to a subclass to set the real default
rotation_reference_glyph_ = 0;
rotation_reference_face_ = 0;
rotation_offset_y_ = 0.;
}
Face::~Face (void)
{
for(unsigned int i=0; i<faces_.size(); i++)
if(faces_[i].free_on_exit_)
FT_Done_Face(faces_[i].face_);
}
// Add another Face to select characters from
bool Face::addAuxiliaryFace (const char* filename)
{
FT_Face ft_face;
FT_Error error = FT_New_Face(ft_library, filename, 0, &ft_face);
if(error != 0) return false;
faces_.push_back(FaceData(ft_face));
setCharSize();
return true;
}
// Add another Face to select characters from
bool Face::addAuxiliaryFace (FT_Face face)
{
faces_.push_back(FaceData(face, false));
setCharSize();
return true;
}
// Note: Changing the point size also clears the display list cache
void Face::setPointSize (float point_size)
{
if(point_size != point_size_)
{
point_size_ = point_size;
clearCaches();
setCharSize();
}
}
// Note: Changing the resolution also clears the display list cache
void Face::setResolution (FT_UInt resolution)
{
if(resolution != resolution_)
{
resolution_ = resolution;
clearCaches();
setCharSize();
}
}
// Note: Changing the background color also clears the display list cache.
void Face::setBackgroundColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
{
if(background_color_[R] != red||background_color_[G] != green||background_color_[B] != blue||background_color_[A] != alpha)
{
background_color_[R] = red;
background_color_[G] = green;
background_color_[B] = blue;
background_color_[A] = alpha;
}
}
// Note: Changing the foreground color also clears the display list cache.
void Face::setForegroundColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
{
if(foreground_color_[R] != red||foreground_color_[G] != green||foreground_color_[B] != blue||foreground_color_[A] != alpha)
{
foreground_color_[R] = red;
foreground_color_[G] = green;
foreground_color_[B] = blue;
foreground_color_[A] = alpha;
}
}
// Note: Changing the foreground color also clears the display list cache.
void Face::setForegroundColor (const GLfloat foreground_color[4])
{
foreground_color_[R] = foreground_color[R];
foreground_color_[G] = foreground_color[G];
foreground_color_[B] = foreground_color[B];
foreground_color_[A] = foreground_color[A];
}
// Note: Changing the background color also clears the display list cache.
void Face::setBackgroundColor (const GLfloat background_color[4])
{
background_color_[R] = background_color[R];
background_color_[G] = background_color[G];
background_color_[B] = background_color[B];
background_color_[A] = background_color[A];
}
// Note: Changing the string rotation angle clears the display list cache
void Face::setStringRotation (GLfloat string_rotation)
{
if(string_rotation != string_rotation_)
{
string_rotation_ = string_rotation;
clearCaches();
// Note that this affects ALL glyphs accessed through
// the Face, both the vector and the raster glyphs. Very nice!
if (string_rotation_ != 0)
{
float angle;
if (string_rotation_<0)
angle = 360.0f - fmod(fabs(string_rotation_), 360.f);
else
angle = fmod(string_rotation_, 360.f);
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit(&sinus, (FT_Angle)(angle * 0x10000L));
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for(unsigned int i=0; i<faces_.size(); i++) FT_Set_Transform(faces_[i].face_, &rotation_matrix, 0);
}
else for(unsigned int i=0; i<faces_.size(); i++) FT_Set_Transform(faces_[i].face_, 0, 0);
}
}
// Note: Changing the rotation reference character clears the display list cache.
void Face::setCharacterRotationReference (unsigned char c)
{
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(f<faces_.size() && glyph_index != rotation_reference_glyph_)
{
FT_Error error = FT_Load_Glyph(faces_[f].face_, glyph_index, FT_LOAD_DEFAULT);
if(error != 0) return;
rotation_reference_glyph_ = glyph_index;
rotation_reference_face_ = faces_[f].face_;
setRotationOffset();
clearCaches();
}
}
BBox Face::measure (const char* s)
{
BBox bbox;
char c;
if((c = *s++) != 0)
{
bbox = measure((unsigned char)c);
for(c = *s; c != 0; c = *++s)
{
BBox char_bbox = measure((unsigned char)c);
bbox += char_bbox;
}
}
// make sure the origin is at 0,0
if (bbox.x_min_ != 0)
{
bbox.x_max_ -= bbox.x_min_;
bbox.x_min_ = 0;
}
if (bbox.y_min_ != 0)
{
bbox.y_max_ -= bbox.y_min_;
bbox.y_min_ = 0;
}
return bbox;
}
BBox Face::measureRaw (const char* s)
{
BBox bbox;
for(char c = *s; c != 0; c = *++s)
{
BBox char_bbox;
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0) continue;
FT_Error error = FT_Load_Glyph(faces_[f].face_, glyph_index, FT_LOAD_DEFAULT);
if(error != 0) continue;
FT_Glyph glyph;
error = FT_Get_Glyph(faces_[f].face_->glyph, &glyph);
if(error != 0) continue;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &ft_bbox);
FT_Done_Glyph(glyph);
char_bbox = ft_bbox;
char_bbox.advance_ = faces_[f].face_->glyph->advance;
bbox += char_bbox;
}
return bbox;
}
BBox Face::measure (const wchar_t* s)
{
BBox bbox;
int i;
if(wstrlen(s) > 0)
{
bbox = measure(s[0]);
for(i = 1; i < wstrlen(s); i++)
{
BBox char_bbox = measure(s[i]);
bbox += char_bbox;
}
}
// make sure the origin is at 0,0
if (bbox.x_min_ != 0)
{
bbox.x_max_ -= bbox.x_min_;
bbox.x_min_ = 0;
}
if (bbox.y_min_ != 0)
{
bbox.y_max_ -= bbox.y_min_;
bbox.y_min_ = 0;
}
return bbox;
}
BBox Face::measureRaw (const wchar_t* s)
{
BBox bbox;
int i;
for(i = 0; i < wstrlen(s); i++)
{
BBox char_bbox;
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, s[i]);
if(glyph_index != 0) break;
}
if(glyph_index == 0)
{
continue;
}
FT_Error error = FT_Load_Glyph(faces_[f].face_, glyph_index, FT_LOAD_DEFAULT);
if(error != 0) continue;
FT_Glyph glyph;
error = FT_Get_Glyph(faces_[f].face_->glyph, &glyph);
if(error != 0) continue;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &ft_bbox);
FT_Done_Glyph(glyph);
char_bbox = ft_bbox;
char_bbox.advance_ = faces_[f].face_->glyph->advance;
bbox += char_bbox;
}
return bbox;
}
// Measure the bounding box as if the (latin1) string were not rotated
BBox Face::measure_nominal (const char* s)
{
if(string_rotation_ == 0.) return measure(s);
for(unsigned int f=0; f<faces_.size(); f++) FT_Set_Transform(faces_[f].face_, 0, 0);
BBox bbox = measure(s);
float angle;
if(string_rotation_<0.)
angle = 360.0f - fmod(fabs(string_rotation_), 360.f);
else
angle = fmod(string_rotation_, 360.f);
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit(&sinus, (FT_Angle)(angle * 0x10000L));
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for(unsigned int f=0; f<faces_.size(); f++) FT_Set_Transform(faces_[f].face_, &rotation_matrix, 0);
return bbox;
}
// Measure the bounding box as if the (UNICODE) string were not rotated
BBox Face::measure_nominal (const wchar_t* s)
{
if(string_rotation_ == 0.)return measure(s);
for(unsigned int f=0; f<faces_.size(); f++)FT_Set_Transform(faces_[f].face_, 0, 0);
BBox bbox = measure(s);
float angle;
if(string_rotation_<0.0)
angle = 360.0f - fmod(fabs(string_rotation_), 360.f);
else
angle = fmod(string_rotation_, 360.f);
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit(&sinus, (FT_Angle)(angle * 0x10000L));
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for(unsigned int f=0; f<faces_.size(); f++)FT_Set_Transform(faces_[f].face_, &rotation_matrix, 0);
return bbox;
}
// Compile a (latin1) character glyph into a display list and cache
// it for later
GLuint Face::compile (unsigned char c)
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find(c);
if(fgi != glyph_dlists_.end())return fgi->second;
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0)return 0;
GLuint dlist = compileGlyph(faces_[f].face_, glyph_index);
glyph_dlists_[ c ] = dlist;
return dlist;
}
// Compile a (UNICODE) character glyph into a display list and cache
// it for later
GLuint Face::compile (const wchar_t c)
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find(c);
if(fgi != glyph_dlists_.end())return fgi->second;
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0)return 0;
GLuint dlist = compileGlyph(faces_[f].face_, glyph_index);
glyph_dlists_[ c ] = dlist;
return dlist;
}
// Assume the MODELVIEW matrix is already set and draw the (latin1)
// string. Note: this routine now ignores almost all settings:
// including the position (both modelview and raster), color,
// justification and advance settings. Consider this to be the raw
// drawing routine for which you are responsible for most of the
// setup.
void Face::draw (const char* s)
{
DLCI character_display_list = character_display_lists_.begin();
for(char c = *s; c != 0; c = *++s)
{
if(character_display_list != character_display_lists_.end())
{
glCallList(*character_display_list);
character_display_list++;
}
draw((unsigned char)c);
}
}
// Assume the MODELVIEW matrix is already set and draw the (UNICODE)
// string. Note: this routine now ignores almost all settings:
// including the position (both modelview and raster), color,
// justification and advance settings. Consider this to be the raw
// drawing routine for which you are responsible for most of the
// setup.
void Face::draw (const wchar_t* s)
{
DLCI character_display_list = character_display_lists_.begin();
int i;
for(i = 0; i < wstrlen(s); i++)
{
if(character_display_list != character_display_lists_.end())
{
glCallList(*character_display_list);
character_display_list++;
}
draw(s[i]);
}
}
// Assume the MODELVIEW matrix is already setup and draw the
// (latin1) character.
void Face::draw (unsigned char c)
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find(c);
if(fgi != glyph_dlists_.end())
{
glCallList(fgi->second);
return;
}
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0) return;
if(compile_mode_ == COMPILE)
{
GLuint dlist = compile(c);
glCallList(dlist);
}
else renderGlyph(faces_[f].face_, glyph_index);
}
// Assume the MODELVIEW matrix is already setup and draw the
// (UNICODE) character.
void Face::draw (const wchar_t c)
{
// See if we've done it already
GDLCI fgi = glyph_dlists_.find(c);
if(fgi != glyph_dlists_.end())
{
glCallList(fgi->second);
return;
}
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0) return;
if(compile_mode_ == COMPILE)
{
GLuint dlist = compile(c);
glCallList(dlist);
}
else renderGlyph(faces_[f].face_, glyph_index);
}
// Draw the (latin1) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw (GLfloat x, GLfloat y, unsigned char c)
{
glTranslatef(x, y, 0.);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(c);
}
// Draw the (latin1) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw (GLfloat x, GLfloat y, GLfloat z, unsigned char c)
{
glTranslatef(x, y, z);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(c);
}
// Draw the (UNICODE) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw (GLfloat x, GLfloat y, wchar_t c)
{
glTranslatef(x, y, 0.);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(c);
}
// Draw the (UNICODE) character at the given position. The MODELVIEW
// matrix is modified by the glyph advance.
void Face::draw (GLfloat x, GLfloat y, GLfloat z, wchar_t c)
{
glTranslatef(x, y, z);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(c);
}
// Draw the (latin1) string at the given position.
void Face::draw (GLfloat x, GLfloat y, const char* s, float *sizebox)
{
// sizebox is xmin,ymin, xmax,ymax
if(!advance_) glPushMatrix();
if(horizontal_justification_ != ORIGIN || vertical_justification_ != BASELINE)
{
glPushMatrix();
GLfloat dx = 0, dy = 0;
switch (horizontal_justification_)
{
case LEFT: dx = -sizebox[0] + 1; break;
case CENTER: dx = -(sizebox[0] + sizebox[2])/ 2.0f; break;
case RIGHT: dx = -sizebox[2] - 1; break;
default: break;
}
switch (vertical_justification_)
{
case BOTTOM: dy = -sizebox[1] + 1; break;
case MIDDLE: dy = -(sizebox[1] + sizebox[3])/ 2.0f; break;
case TOP: dy = -sizebox[3] - 1; break;
default: break;
}
// There is probably a less expensive way to compute this
glRotatef(string_rotation_, 0., 0., 1.);
glTranslatef(dx, dy, 0);
glRotatef(-string_rotation_, 0., 0., 1.);
}
glTranslatef(x, y, 0.);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(s);
if(horizontal_justification_ != ORIGIN || vertical_justification_ != BASELINE) glPopMatrix();
if(!advance_) glPopMatrix();
}
// Draw the (latin1) string at the given position.
void Face::draw (GLfloat x, GLfloat y, GLfloat z, const char* s)
{
if(!advance_) glPushMatrix();
if(horizontal_justification_ != ORIGIN || vertical_justification_ != BASELINE)
{
glPushMatrix();
BBox bbox = measure_nominal(s);
GLfloat dx = 0, dy = 0;
switch (horizontal_justification_)
{
case LEFT: dx = -bbox.x_min_; break;
case CENTER: dx = -(bbox.x_min_ + bbox.x_max_)/ 2.0f; break;
case RIGHT: dx = -bbox.x_max_; break;
default: break;
}
switch (vertical_justification_)
{
case BOTTOM: dy = -bbox.y_min_; break;
case MIDDLE: dy = -(bbox.y_min_ + bbox.y_max_)/ 2.0f; break;
case TOP: dy = -bbox.y_max_; break;
default: break;
}
// There is probably a less expensive way to compute this
glRotatef(string_rotation_, 0., 0., 1.);
glTranslatef(dx, dy, 0);
glRotatef(-string_rotation_, 0., 0., 1.);
}
glTranslatef(x, y, z);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(s);
if(horizontal_justification_ != ORIGIN || vertical_justification_ != BASELINE) glPopMatrix();
if(!advance_) glPopMatrix();
}
// Draw the (UNICODE) string at the given position.
void Face::draw (GLfloat x, GLfloat y, const wchar_t* s)
{
if(!advance_)
glPushMatrix();
if(horizontal_justification_!=ORIGIN||vertical_justification_!=BASELINE)
{
glPushMatrix();
BBox bbox = measure_nominal(s);
GLfloat dx = 0, dy = 0;
switch (horizontal_justification_)
{
case LEFT:
dx = -bbox.x_min_; break;
case CENTER:
dx = -(bbox.x_min_ + bbox.x_max_)/ 2.0f; break;
case RIGHT:
dx = -bbox.x_max_; break;
default:
break;
}
switch (vertical_justification_)
{
case BOTTOM:
dy = -bbox.y_min_; break;
case MIDDLE:
dy = -(bbox.y_min_ + bbox.y_max_)/ 2.0f; break;
case TOP:
dy = -bbox.y_max_; break;
default:
break;
}
// There is probably a less expensive way to compute this
glRotatef(string_rotation_, 0., 0., 1.);
glTranslatef(dx, dy, 0);
glRotatef(-string_rotation_, 0., 0., 1.);
}
glTranslatef(x, y, 0.);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B],
foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(s);
if(horizontal_justification_ != ORIGIN ||
vertical_justification_ != BASELINE)
glPopMatrix();
if(!advance_)
glPopMatrix();
}
// Draw the (UNICODE) string at the given position.
void Face::draw (GLfloat x, GLfloat y, GLfloat z, const wchar_t* s)
{
if(!advance_) glPushMatrix();
if(horizontal_justification_!= ORIGIN||vertical_justification_!= BASELINE)
{
glPushMatrix();
// In 3D, we need to exert more care in the computation of the
// bounding box of the text. NOTE: Needs to be fixed up for
// polygonal faces, too...
BBox bbox;
// Code from measure_nominal, but changed to use measureRaw instead
if(string_rotation_ == 0.) bbox = measureRaw(s);
else
{
for(unsigned int f=0; f<faces_.size(); f++)
FT_Set_Transform(faces_[f].face_, 0, 0);
bbox = measureRaw(s);
float angle;
if(string_rotation_<0.0)
angle = 360.0f - fmod(fabs(string_rotation_), 360.f);
else
angle = fmod(string_rotation_, 360.f);
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit(&sinus, (FT_Angle)(angle * 0x10000L));
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
for(unsigned int f=0; f<faces_.size(); f++)
FT_Set_Transform(faces_[f].face_, &rotation_matrix, 0);
}
GLfloat dx = 0, dy = 0;
switch (horizontal_justification_)
{
case LEFT:
dx = bbox.x_min_; break;
case CENTER:
dx = (bbox.x_min_ + bbox.x_max_)/ 2; break;
case RIGHT:
dx = bbox.x_max_; break;
default:
break;
}
switch (vertical_justification_)
{
case BOTTOM:
dy = bbox.y_min_; break;
case MIDDLE:
dy = (bbox.y_min_ + bbox.y_max_)/2; break;
case TOP:
dy = bbox.y_max_; break;
default:
break;
}
GLint viewport[4];
GLdouble modelview[16], projection[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLdouble x0, y0, z0;
gluUnProject(0, 0, 0, modelview, projection, viewport, &x0, &y0, &z0);
GLdouble dx_m, dy_m, dz_m;
gluUnProject(dx, dy, 0., modelview, projection, viewport,&dx_m,&dy_m,&dz_m);
glTranslated(x0-dx_m, y0-dy_m, z0-dz_m);
}
glTranslatef(x, y, z);
glColor4f(foreground_color_[R], foreground_color_[G], foreground_color_[B], foreground_color_[A]);
glRasterPos3i(0, 0, 0);
draw(s);
if(horizontal_justification_!=ORIGIN||vertical_justification_!= BASELINE)
glPopMatrix();
if(!advance_)
glPopMatrix();
}
Raster::Raster (const char* filename, float point_size, FT_UInt resolution)
: Face(filename, point_size, resolution)
{
if(!isValid()) return;
init();
}
Raster::Raster (FT_Face face, float point_size, FT_UInt resolution)
: Face(face, point_size, resolution)
{
init();
}
void Raster::init (void)
{
character_rotation_z_ = 0;
setCharSize();
setCharacterRotationReference('o');
}
Raster::~Raster (void)
{
clearCaches();
}
void Raster::setCharacterRotationZ (GLfloat character_rotation_z)
{
if(character_rotation_z != character_rotation_z_)
{
character_rotation_z_ = character_rotation_z;
clearCaches();
}
}
double Raster::height (void)const
{
if(faces_[0].face_->height > 0) return faces_[0].face_->height / 64.;
else return faces_[0].face_->size->metrics.y_ppem;
}
BBox Raster::measure (unsigned char c)
{
BBox bbox;
// For starters, just get the unscaled glyph bounding box
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0) return bbox;
FT_Error error = FT_Load_Glyph(faces_[f].face_, glyph_index, FT_LOAD_DEFAULT);
if(error != 0) return bbox;
FT_Glyph glyph;
error = FT_Get_Glyph(faces_[f].face_->glyph, &glyph);
if(error != 0) return bbox;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &ft_bbox);
FT_Done_Glyph(glyph);
bbox = ft_bbox;
bbox.advance_ = faces_[f].face_->glyph->advance;
// In order to be accurate regarding the placement of text not
// aligned at the glyph's origin (CENTER/MIDDLE), the bounding box
// of the raster format has to be projected back into the
// view's coordinates
GLint viewport[4];
GLdouble modelview[16], projection[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
// Well, first we have to get the Origin, since that is the basis
// of the bounding box
GLdouble x0, y0, z0;
gluUnProject(0., 0., 0., modelview, projection, viewport, &x0, &y0, &z0);
GLdouble x, y, z;
gluUnProject(bbox.x_min_, bbox.y_min_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.x_min_ = (float) (x - x0);
bbox.y_min_ = (float) (y - y0);
gluUnProject(bbox.x_max_, bbox.y_max_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.x_max_ = (float) (x - x0);
bbox.y_max_ = (float) (y - y0);
gluUnProject(bbox.advance_.dx_, bbox.advance_.dy_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.advance_.dx_ = (float) (x - x0);
bbox.advance_.dy_ = (float) (y - y0);
return bbox;
}
BBox Raster::measure (wchar_t c)
{
BBox bbox;
// For starters, just get the unscaled glyph bounding box
unsigned int f;
FT_UInt glyph_index = 0;
for(f=0; f<faces_.size(); f++)
{
glyph_index = FT_Get_Char_Index(faces_[f].face_, c);
if(glyph_index != 0) break;
}
if(glyph_index == 0) return bbox;
FT_Error error = FT_Load_Glyph(faces_[f].face_, glyph_index,
FT_LOAD_DEFAULT);
if(error != 0) return bbox;
FT_Glyph glyph;
error = FT_Get_Glyph(faces_[f].face_->glyph, &glyph);
if(error != 0) return bbox;
FT_BBox ft_bbox;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &ft_bbox);
FT_Done_Glyph(glyph);
bbox = ft_bbox;
bbox.advance_ = faces_[f].face_->glyph->advance;
// In order to be accurate regarding the placement of text not
// aligned at the glyph's origin (CENTER/MIDDLE), the bounding box
// of the raster format has to be projected back into the
// view's coordinates
GLint viewport[4];
GLdouble modelview[16], projection[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
// Well, first we have to get the Origin, since that is the basis
// of the bounding box
GLdouble x0, y0, z0;
gluUnProject(0., 0., 0., modelview, projection, viewport, &x0, &y0, &z0);
GLdouble x, y, z;
gluUnProject(bbox.x_min_, bbox.y_min_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.x_min_ = (float) (x - x0);
bbox.y_min_ = (float) (y - y0);
gluUnProject(bbox.x_max_, bbox.y_max_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.x_max_ = (float) (x - x0);
bbox.y_max_ = (float) (y - y0);
gluUnProject(bbox.advance_.dx_, bbox.advance_.dy_, 0., modelview, projection, viewport, &x, &y, &z);
bbox.advance_.dx_ = (float) (x - x0);
bbox.advance_.dy_ = (float) (y - y0);
return bbox;
}
GLuint Raster::compileGlyph (FT_Face face, FT_UInt glyph_index)
{
GLuint dlist = glGenLists(1);
glNewList(dlist, GL_COMPILE);
renderGlyph(face, glyph_index);
glEndList();
return dlist;
}
void Raster::setCharSize (void)
{
FT_Error error;
for(unsigned int i=0; i<faces_.size(); i++)
{
error = FT_Set_Char_Size(faces_[i].face_,(FT_F26Dot6)(point_size_ * 64),(FT_F26Dot6)(point_size_ * 64),resolution_,resolution_);
if(error != 0) return;
}
if(rotation_reference_glyph_ != 0) setRotationOffset();
}
void Raster::setRotationOffset (void)
{
FT_Error error = FT_Load_Glyph(rotation_reference_face_, rotation_reference_glyph_, FT_LOAD_RENDER);
if(error != 0) return;
rotation_offset_y_ = rotation_reference_face_->glyph->bitmap.rows / 2.0f;
}
void Raster::clearCaches (void)
{
GDLI fgi = glyph_dlists_.begin();
for(; fgi != glyph_dlists_.end(); ++fgi)
{
glDeleteLists(fgi->second, 1);
}
glyph_dlists_.clear();
}
Monochrome::Monochrome (const char* filename, float point_size, FT_UInt resolution)
: Raster(filename, point_size, resolution)
{
return;
}
Monochrome::Monochrome (FT_Face face, float point_size, FT_UInt resolution)
: Raster(face, point_size, resolution)
{
return;
}
Monochrome::~Monochrome (void)
{
return;
}
GLubyte* Monochrome::invertBitmap (const FT_Bitmap& bitmap)
{
// In FreeType 2.0.9, the pitch of bitmaps was rounded up to an
// even number. In general, this disagrees with what we had been
// using for OpenGL.
int width = bitmap.width / 8 + ((bitmap.width & 7)> 0 ? 1 : 0);
GLubyte* inverse = new GLubyte[ bitmap.rows * width ];
GLubyte* inverse_ptr = inverse;
for(unsigned int r=0; r<bitmap.rows; r++)
{
GLubyte* bitmap_ptr = &bitmap.buffer[bitmap.pitch * (bitmap.rows - r - 1)];
memmove(inverse_ptr, bitmap_ptr, width);
inverse_ptr += width;
bitmap_ptr += width;
}
return inverse;
}
void Monochrome::renderGlyph (FT_Face face, FT_UInt glyph_index)
{
// Start by retrieving the glyph's data.
FT_Error error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if(error != 0) return;
FT_Glyph original_glyph;
FT_Glyph glyph;
error = FT_Get_Glyph(face->glyph, &original_glyph);
if(error != 0) return;
error = FT_Glyph_Copy(original_glyph, &glyph);
FT_Done_Glyph(original_glyph);
if(error != 0) return;
// If the individual characters are rotated (as distinct from string
// rotation), then apply that extra rotation here. This is equivalent
// to the sequence
// glTranslate(x_center,y_center);
// glRotate(angle);
// glTranslate(-x_center,-y_center);
// which is used for the polygonal styles. The deal with the raster
// styles is that you must retain the advance from the string rotation
// so that the glyphs are laid out properly. So, we make a copy of
// the string rotated glyph, and then rotate that and add back an
// additional offset to (in effect) restore the proper origin and
// advance of the glyph.
if(character_rotation_z_ != 0.)
{
FT_Matrix rotation_matrix;
FT_Vector sinus;
FT_Vector_Unit(&sinus, (FT_Angle)(character_rotation_z_ * 0x10000L));
rotation_matrix.xx = sinus.x;
rotation_matrix.xy = -sinus.y;
rotation_matrix.yx = sinus.y;
rotation_matrix.yy = sinus.x;
FT_Vector original_offset, rotation_offset;
original_offset.x = (face->glyph->metrics.width / 2 + face->glyph->metrics.horiBearingX)/ 64 * 0x10000L;
original_offset.y = (FT_Pos)(rotation_offset_y_ * 0x10000L);
rotation_offset = original_offset;
FT_Vector_Rotate(&rotation_offset, (FT_Angle)(character_rotation_z_ * 0x10000L));
rotation_offset.x = original_offset.x - rotation_offset.x;
rotation_offset.y = original_offset.y - rotation_offset.y;
rotation_offset.x /= 1024;
rotation_offset.y /= 1024;
error = FT_Glyph_Transform(glyph, &rotation_matrix, &rotation_offset);
}
error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_MONO, 0, 1);
if(error != 0)
{
FT_Done_Glyph(glyph);
return;
}
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) glyph;
// Evidently, in FreeType2, you can only get "upside-down" bitmaps and
// OpenGL won't invert a bitmap with PixelZoom, so we have to invert the
// glyph's bitmap ourselves.
GLubyte* inverted_bitmap = invertBitmap(bitmap_glyph->bitmap);
glBitmap(bitmap_glyph->bitmap.width, bitmap_glyph->bitmap.rows,
(GLfloat) -bitmap_glyph->left,
(GLfloat) (bitmap_glyph->bitmap.rows - bitmap_glyph->top),
face->glyph->advance.x / 64.0f,
face->glyph->advance.y / 64.0f,
inverted_bitmap);
FT_Done_Glyph(glyph);
delete[] inverted_bitmap;
}
} // close OGLFT namespace
|