| 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 <assert.h> |
| 14 |
#include <glcomp/glcomptexture.h> |
| 15 |
#include <glcomp/glpangofont.h> |
| 16 |
#include <stddef.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <util/alloc.h> |
| 19 |
#include <util/streq.h> |
| 20 |
|
| 21 |
static glCompTex *glCompSetAddNewTexture(glCompSet *s, int width, int height, |
| 22 |
const unsigned char *data, bool is2D) { |
| 23 |
int offset, ind; |
| 24 |
unsigned char *tarData; |
| 25 |
|
| 26 |
if (!data) |
| 27 |
return NULL; |
| 28 |
|
| 29 |
glCompTex *t = gv_alloc(sizeof(glCompTex)); |
| 30 |
if (!is2D) { /*use opengl texture */ |
| 31 |
glEnable(GL_TEXTURE_2D); |
| 32 |
glShadeModel(GL_FLAT); |
| 33 |
glEnable(GL_DEPTH_TEST); |
| 34 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 35 |
glGenTextures(1, &t->id); //get next id |
| 36 |
if (glGetError() != GL_NO_ERROR) { /*for some opengl based error , texture couldnt be created */ |
| 37 |
/* drain the OpenGL error queue */ |
| 38 |
while (glGetError() != GL_NO_ERROR); |
| 39 |
free(t); |
| 40 |
return NULL; |
| 41 |
} else { |
| 42 |
glBindTexture(GL_TEXTURE_2D, t->id); |
| 43 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 44 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 45 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, |
| 46 |
GL_NEAREST); |
| 47 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 48 |
GL_NEAREST); |
| 49 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 50 |
GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 51 |
glDisable(GL_TEXTURE_2D); |
| 52 |
} |
| 53 |
} |
| 54 |
if (is2D) { |
| 55 |
assert(width >= 0); |
| 56 |
assert(height >= 0); |
| 57 |
t->data = gv_calloc(4 * (unsigned)width * (unsigned)height, |
| 58 |
sizeof(unsigned char)); |
| 59 |
offset = 4; //RGBA mod,TO DO implement other modes |
| 60 |
/*data upside down because of pango gl coord system */ |
| 61 |
for (ind = 0; ind < height; ind++) { |
| 62 |
const unsigned char *srcData = data + (height - 1 - ind) * offset * width; |
| 63 |
tarData = t->data + ind * offset * width; |
| 64 |
memcpy(tarData, srcData, 4 * (unsigned)width); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
t->userCount = 1; |
| 69 |
t->width = width; |
| 70 |
t->height = height; |
| 71 |
if(s) |
| 72 |
{ |
| 73 |
s->textures = |
| 74 |
gv_recalloc(s->textures, s->textureCount, s->textureCount + 1, sizeof(glCompTex *)); |
| 75 |
s->textureCount++; |
| 76 |
s->textures[s->textureCount - 1] = t; |
| 77 |
} |
| 78 |
return t; |
| 79 |
|
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
glCompTex *glCompSetAddNewTexImage(glCompSet *s, int width, int height, |
| 84 |
const unsigned char *data, bool is2D) { |
| 85 |
|
| 86 |
glCompTex *t; |
| 87 |
if (!data) |
| 88 |
return NULL; |
| 89 |
t = glCompSetAddNewTexture(s, width, height, data, is2D); |
| 90 |
if (!t) |
| 91 |
return NULL; |
| 92 |
t->type = glTexImage; |
| 93 |
return t; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
glCompTex *glCompSetAddNewTexLabel(glCompSet *s, char *def, int fs, |
| 98 |
const char *text, bool is2D) { |
| 99 |
int width, height; |
| 100 |
glCompTex *t; |
| 101 |
cairo_surface_t *surface = NULL; |
| 102 |
unsigned char *data = NULL; |
| 103 |
if (!def) |
| 104 |
return NULL; |
| 105 |
/*first check if the same label with same font def created before |
| 106 |
if it was , return its id |
| 107 |
*/ |
| 108 |
for (size_t ind = 0; ind < s->textureCount; ind++) { |
| 109 |
if (s->textures[ind]->type == glTexLabel) { |
| 110 |
if (streq(def, s->textures[ind]->def) |
| 111 |
&& s->textures[ind]->type == glTexLabel |
| 112 |
&& streq(text, s->textures[ind]->text) |
| 113 |
&& s->textures[ind]->fontSize==fs) { |
| 114 |
s->textures[ind]->userCount++; |
| 115 |
return s->textures[ind]; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
data = glCompCreatePangoTexture(def, fs, text, &surface, &width, &height); |
| 122 |
if (!data) // pango error |
| 123 |
return NULL; |
| 124 |
t = glCompSetAddNewTexture(s, width, height, data, is2D); |
| 125 |
cairo_surface_destroy(surface); |
| 126 |
if (!t) { |
| 127 |
free(data); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
t->def = gv_strdup(def); |
| 132 |
t->text = gv_strdup(text); |
| 133 |
t->type = glTexLabel; |
| 134 |
return t; |
| 135 |
} |
| 136 |
|
| 137 |
void glCompDeleteTexture(glCompTex * t) |
| 138 |
{ |
| 139 |
if (!t) |
| 140 |
return; |
| 141 |
t->userCount--; |
| 142 |
if (!t->userCount) { |
| 143 |
free(t->data); |
| 144 |
free(t->def); |
| 145 |
free(t->text); |
| 146 |
free(t); |
| 147 |
} |
| 148 |
} |