glcompfont.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <glcomp/glcompfont.h>
  11. #include <glcomp/glcompset.h>
  12. #include <glcomp/glpangofont.h>
  13. #include <glcomp/glcomptexture.h>
  14. #include <glcomp/glutils.h>
  15. #include <GL/glut.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include <util/alloc.h>
  19. static void print_bitmap_string(void *font, char *s)
  20. {
  21. if (s && strlen(s)) {
  22. while (*s) {
  23. glutBitmapCharacter(font, *s);
  24. s++;
  25. }
  26. }
  27. }
  28. void glprintfglut(void *font, float xpos, float ypos, float zpos, char *bf) {
  29. glRasterPos3f(xpos, ypos, zpos + 0.001f);
  30. print_bitmap_string(font, bf);
  31. }
  32. void glDeleteFont(glCompFont * f)
  33. {
  34. free(f->fontdesc);
  35. if (f->tex)
  36. glCompDeleteTexture(f->tex);
  37. *f = (glCompFont){0};
  38. }
  39. glCompFont glNewFont(glCompSet *s, char *text, glCompColor *c, char *fontdesc,
  40. int fs, bool is2D) {
  41. glCompFont font = {0};
  42. font.color.R = c->R;
  43. font.color.G = c->G;
  44. font.color.B = c->B;
  45. font.color.A = c->A;
  46. font.justify.VJustify = glFontVJustifyNone;
  47. font.justify.HJustify = glFontHJustifyNone;
  48. font.is2D = is2D;
  49. font.glutfont = NULL;
  50. font.fontdesc = gv_strdup(fontdesc);
  51. font.size = fs;
  52. font.transparent = 1;
  53. if (text)
  54. font.tex =
  55. glCompSetAddNewTexLabel(s, font.fontdesc, font.size, text,
  56. is2D);
  57. return font;
  58. }
  59. glCompFont glNewFontFromParent(glCompObj *o, char *text) {
  60. glCompCommon *parent;
  61. glCompFont font = {0};
  62. parent = o->common.parent;
  63. if (parent) {
  64. parent = o->common.parent;
  65. font.color.R = parent->font.color.R;
  66. font.color.G = parent->font.color.G;
  67. font.color.B = parent->font.color.B;
  68. font.color.A = parent->font.color.A;
  69. font.glutfont = parent->font.glutfont;
  70. font.fontdesc = gv_strdup(parent->font.fontdesc);
  71. font.size = parent->font.size;
  72. font.transparent = parent->font.transparent;
  73. font.justify.VJustify = parent->font.justify.VJustify;
  74. font.justify.HJustify = parent->font.justify.HJustify;
  75. font.is2D = parent->font.is2D;
  76. if (text) {
  77. if (strlen(text))
  78. font.tex =
  79. glCompSetAddNewTexLabel(parent->compset,
  80. font.fontdesc, font.size,
  81. text, parent->font.is2D);
  82. }
  83. } else { /*no parent */
  84. glCompColor c;
  85. c.R = GLCOMPSET_FONT_COLOR_R;
  86. c.G = GLCOMPSET_FONT_COLOR_G;
  87. c.B = GLCOMPSET_FONT_COLOR_B;
  88. c.A = GLCOMPSET_FONT_COLOR_ALPHA;
  89. font = glNewFont(o->common.compset, text, &c,
  90. GLCOMPSET_FONT_DESC, GLCOMPSET_FONT_SIZE, true);
  91. }
  92. return font;
  93. }
  94. /*texture base 3d text rendering*/
  95. void glCompDrawText3D(glCompFont f, float x, float y, double z, float w,
  96. float h) {
  97. glEnable(GL_BLEND); // Turn Blending On
  98. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  99. glEnable(GL_TEXTURE_2D);
  100. glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  101. glBindTexture(GL_TEXTURE_2D, f.tex->id);
  102. glBegin(GL_QUADS);
  103. glTexCoord2d(0.0f, 1.0f);glVertex3d(x,y,z);
  104. glTexCoord2d(1.0f, 1.0f);glVertex3d(x+w,y,z);
  105. glTexCoord2d(1.0f, 0.0f);glVertex3d(x+w,y+h,z);
  106. glTexCoord2d(0.0f, 0.0f);glVertex3d(x,y+h,z);
  107. glEnd();
  108. glDisable(GL_TEXTURE_2D);
  109. glEnable(GL_BLEND);
  110. }
  111. void glCompDrawText(glCompFont f, float x, float y) {
  112. glRasterPos2f(x, y);
  113. glDrawPixels(f.tex->width, f.tex->height, GL_RGBA, GL_UNSIGNED_BYTE, f.tex->data);
  114. }
  115. /*text rendering functions, depends on a globject to retrieve stats*/
  116. void glCompRenderText(glCompFont f, glCompObj *parentObj) {
  117. if (!f.tex)
  118. return;
  119. float x = 0;
  120. float y = 0;
  121. glCompCommon ref = parentObj->common;
  122. switch (f.justify.HJustify) {
  123. case glFontHJustifyNone:
  124. x = ref.refPos.x;
  125. break;
  126. case glFontHJustifyCenter:
  127. x = ref.refPos.x + (ref.width - (float)f.tex->width) / 2.0f;
  128. break;
  129. }
  130. switch (f.justify.VJustify) {
  131. case glFontVJustifyNone:
  132. y = ref.pos.y;
  133. break;
  134. case glFontVJustifyCenter:
  135. y = ref.refPos.y + (ref.height - (float)f.tex->height) / 2.0f;
  136. break;
  137. }
  138. glCompSetColor(f.color);
  139. glCompDrawText(f, x, y);
  140. }