glcomptexture.c 4.1 KB

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