| 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 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <glcomp/glcompfont.h> |
| 14 |
#include <glcomp/glcompset.h> |
| 15 |
#include <glcomp/glpangofont.h> |
| 16 |
#include <glcomp/glcomptexture.h> |
| 17 |
#include <glcomp/glutils.h> |
| 18 |
#include <GL/glut.h> |
| 19 |
#include <stdbool.h> |
| 20 |
#include <stddef.h> |
| 21 |
#include <util/alloc.h> |
| 22 |
|
| 23 |
static void print_bitmap_string(void *font, char *s) |
| 24 |
{ |
| 25 |
if (s && strlen(s)) { |
| 26 |
while (*s) { |
| 27 |
glutBitmapCharacter(font, *s); |
| 28 |
s++; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
void glprintfglut(void *font, float xpos, float ypos, float zpos, char *bf) { |
| 34 |
glRasterPos3f(xpos, ypos, zpos + 0.001f); |
| 35 |
print_bitmap_string(font, bf); |
| 36 |
|
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
void glDeleteFont(glCompFont * f) |
| 41 |
{ |
| 42 |
free(f->fontdesc); |
| 43 |
if (f->tex) |
| 44 |
glCompDeleteTexture(f->tex); |
| 45 |
*f = (glCompFont){0}; |
| 46 |
} |
| 47 |
|
| 48 |
glCompFont glNewFont(glCompSet *s, const char *text, glCompColor *c, |
| 49 |
char *fontdesc, int fs, bool is2D) { |
| 50 |
glCompFont font = {0}; |
| 51 |
font.color.R = c->R; |
| 52 |
font.color.G = c->G; |
| 53 |
font.color.B = c->B; |
| 54 |
font.color.A = c->A; |
| 55 |
font.justify.VJustify = glFontVJustifyNone; |
| 56 |
font.justify.HJustify = glFontHJustifyNone; |
| 57 |
font.is2D = is2D; |
| 58 |
|
| 59 |
font.glutfont = NULL; |
| 60 |
|
| 61 |
font.fontdesc = gv_strdup(fontdesc); |
| 62 |
font.size = fs; |
| 63 |
font.transparent = 1; |
| 64 |
if (text) |
| 65 |
font.tex = |
| 66 |
glCompSetAddNewTexLabel(s, font.fontdesc, font.size, text, |
| 67 |
is2D); |
| 68 |
return font; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
glCompFont glNewFontFromParent(glCompObj *o, const char *text) { |
| 73 |
glCompCommon *parent; |
| 74 |
glCompFont font = {0}; |
| 75 |
parent = o->common.parent; |
| 76 |
if (parent) { |
| 77 |
parent = o->common.parent; |
| 78 |
font.color.R = parent->font.color.R; |
| 79 |
font.color.G = parent->font.color.G; |
| 80 |
font.color.B = parent->font.color.B; |
| 81 |
font.color.A = parent->font.color.A; |
| 82 |
|
| 83 |
font.glutfont = parent->font.glutfont; |
| 84 |
font.fontdesc = gv_strdup(parent->font.fontdesc); |
| 85 |
font.size = parent->font.size; |
| 86 |
font.transparent = parent->font.transparent; |
| 87 |
font.justify.VJustify = parent->font.justify.VJustify; |
| 88 |
font.justify.HJustify = parent->font.justify.HJustify; |
| 89 |
font.is2D = parent->font.is2D; |
| 90 |
if (text) { |
| 91 |
if (strlen(text)) |
| 92 |
font.tex = |
| 93 |
glCompSetAddNewTexLabel(parent->compset, |
| 94 |
font.fontdesc, font.size, |
| 95 |
text, parent->font.is2D); |
| 96 |
} |
| 97 |
} else { /*no parent */ |
| 98 |
|
| 99 |
glCompColor c; |
| 100 |
c.R = GLCOMPSET_FONT_COLOR_R; |
| 101 |
c.G = GLCOMPSET_FONT_COLOR_G; |
| 102 |
c.B = GLCOMPSET_FONT_COLOR_B; |
| 103 |
c.A = GLCOMPSET_FONT_COLOR_ALPHA; |
| 104 |
font = glNewFont(o->common.compset, text, &c, |
| 105 |
GLCOMPSET_FONT_DESC, GLCOMPSET_FONT_SIZE, true); |
| 106 |
} |
| 107 |
return font; |
| 108 |
} |
| 109 |
|
| 110 |
/*texture base 3d text rendering*/ |
| 111 |
void glCompDrawText3D(glCompFont f, float x, float y, double z, float w, |
| 112 |
float h) { |
| 113 |
glEnable(GL_BLEND); // Turn Blending On |
| 114 |
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
| 115 |
glEnable(GL_TEXTURE_2D); |
| 116 |
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 117 |
glBindTexture(GL_TEXTURE_2D, f.tex->id); |
| 118 |
glBegin(GL_QUADS); |
| 119 |
glTexCoord2d(0.0f, 1.0f);glVertex3d(x,y,z); |
| 120 |
glTexCoord2d(1.0f, 1.0f);glVertex3d(x+w,y,z); |
| 121 |
glTexCoord2d(1.0f, 0.0f);glVertex3d(x+w,y+h,z); |
| 122 |
glTexCoord2d(0.0f, 0.0f);glVertex3d(x,y+h,z); |
| 123 |
glEnd(); |
| 124 |
|
| 125 |
glDisable(GL_TEXTURE_2D); |
| 126 |
glEnable(GL_BLEND); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
void glCompDrawText(glCompFont f, double x, double y) { |
| 131 |
glRasterPos2d(x, y); |
| 132 |
glDrawPixels(f.tex->width, f.tex->height, GL_RGBA, GL_UNSIGNED_BYTE, f.tex->data); |
| 133 |
} |
| 134 |
|
| 135 |
/*text rendering functions, depends on a globject to retrieve stats*/ |
| 136 |
void glCompRenderText(glCompFont f, glCompObj *parentObj) { |
| 137 |
if (!f.tex) |
| 138 |
return; |
| 139 |
float x = 0; |
| 140 |
float y = 0; |
| 141 |
glCompCommon ref = parentObj->common; |
| 142 |
switch (f.justify.HJustify) { |
| 143 |
case glFontHJustifyNone: |
| 144 |
x = ref.refPos.x; |
| 145 |
break; |
| 146 |
case glFontHJustifyCenter: |
| 147 |
x = ref.refPos.x + (ref.width - (float)f.tex->width) / 2.0f; |
| 148 |
break; |
| 149 |
} |
| 150 |
switch (f.justify.VJustify) { |
| 151 |
case glFontVJustifyNone: |
| 152 |
y = ref.pos.y; |
| 153 |
break; |
| 154 |
case glFontVJustifyCenter: |
| 155 |
y = ref.refPos.y + (ref.height - (float)f.tex->height) / 2.0f; |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
glCompSetColor(f.color); |
| 160 |
glCompDrawText(f, x, y); |
| 161 |
} |