Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**************************************************************************
  2. *
  3. * Copyright 2010 LunarG, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  20. * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  21. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. **************************************************************************/
  26. #include "util/u_memory.h"
  27. #include "cso_cache/cso_hash.h"
  28. #include "text.h"
  29. #include "image.h"
  30. #include "path.h"
  31. #ifdef OPENVG_VERSION_1_1
  32. struct vg_font {
  33. struct vg_object base;
  34. struct cso_hash *glyphs;
  35. };
  36. struct vg_glyph {
  37. struct vg_object *object; /* it could be NULL */
  38. VGboolean is_hinted;
  39. VGfloat glyph_origin[2];
  40. VGfloat escapement[2];
  41. };
  42. static VGboolean del_glyph(struct vg_font *font,
  43. VGuint glyphIndex)
  44. {
  45. struct vg_glyph *glyph;
  46. glyph = (struct vg_glyph *)
  47. cso_hash_take(font->glyphs, (unsigned) glyphIndex);
  48. if (glyph)
  49. FREE(glyph);
  50. return (glyph != NULL);
  51. }
  52. static void add_glyph(struct vg_font *font,
  53. VGuint glyphIndex,
  54. struct vg_object *obj,
  55. VGboolean isHinted,
  56. const VGfloat glyphOrigin[2],
  57. const VGfloat escapement[2])
  58. {
  59. struct vg_glyph *glyph;
  60. /* remove the existing one */
  61. del_glyph(font, glyphIndex);
  62. glyph = CALLOC_STRUCT(vg_glyph);
  63. glyph->object = obj;
  64. glyph->is_hinted = isHinted;
  65. memcpy(glyph->glyph_origin, glyphOrigin, sizeof(glyphOrigin));
  66. memcpy(glyph->escapement, escapement, sizeof(escapement));
  67. cso_hash_insert(font->glyphs, (unsigned) glyphIndex, glyph);
  68. }
  69. static struct vg_glyph *get_glyph(struct vg_font *font,
  70. VGuint glyphIndex)
  71. {
  72. struct cso_hash_iter iter;
  73. iter = cso_hash_find(font->glyphs, (unsigned) glyphIndex);
  74. return (struct vg_glyph *) cso_hash_iter_data(iter);
  75. }
  76. static void vg_render_glyph(struct vg_context *ctx,
  77. struct vg_glyph *glyph,
  78. VGbitfield paintModes,
  79. VGboolean allowAutoHinting)
  80. {
  81. if (glyph->object && paintModes) {
  82. struct vg_state *state = &ctx->state.vg;
  83. struct matrix m;
  84. m = state->glyph_user_to_surface_matrix;
  85. matrix_translate(&m,
  86. state->glyph_origin[0].f - glyph->glyph_origin[0],
  87. state->glyph_origin[1].f - glyph->glyph_origin[1]);
  88. if (glyph->object->type == VG_OBJECT_PATH) {
  89. path_render((struct path *) glyph->object, paintModes, &m);
  90. }
  91. else {
  92. assert(glyph->object->type == VG_OBJECT_IMAGE);
  93. image_draw((struct vg_image *) glyph->object, &m);
  94. }
  95. }
  96. }
  97. static void vg_advance_glyph(struct vg_context *ctx,
  98. struct vg_glyph *glyph,
  99. VGfloat adjustment_x,
  100. VGfloat adjustment_y,
  101. VGboolean last)
  102. {
  103. struct vg_value *glyph_origin = ctx->state.vg.glyph_origin;
  104. glyph_origin[0].f += glyph->escapement[0] + adjustment_x;
  105. glyph_origin[1].f += glyph->escapement[1] + adjustment_y;
  106. if (last) {
  107. glyph_origin[0].i = float_to_int_floor(glyph_origin[0].f);
  108. glyph_origin[1].i = float_to_int_floor(glyph_origin[1].f);
  109. }
  110. }
  111. struct vg_font *font_create(VGint glyphCapacityHint)
  112. {
  113. struct vg_context *ctx = vg_current_context();
  114. struct vg_font *font;
  115. font = CALLOC_STRUCT(vg_font);
  116. vg_init_object(&font->base, ctx, VG_OBJECT_FONT);
  117. font->glyphs = cso_hash_create();
  118. vg_context_add_object(ctx, &font->base);
  119. return font;
  120. }
  121. void font_destroy(struct vg_font *font)
  122. {
  123. struct vg_context *ctx = vg_current_context();
  124. struct cso_hash_iter iter;
  125. vg_context_remove_object(ctx, &font->base);
  126. iter = cso_hash_first_node(font->glyphs);
  127. while (!cso_hash_iter_is_null(iter)) {
  128. struct vg_glyph *glyph = (struct vg_glyph *) cso_hash_iter_data(iter);
  129. FREE(glyph);
  130. iter = cso_hash_iter_next(iter);
  131. }
  132. cso_hash_delete(font->glyphs);
  133. FREE(font);
  134. }
  135. void font_set_glyph_to_path(struct vg_font *font,
  136. VGuint glyphIndex,
  137. struct path *path,
  138. VGboolean isHinted,
  139. const VGfloat glyphOrigin[2],
  140. const VGfloat escapement[2])
  141. {
  142. add_glyph(font, glyphIndex, (struct vg_object *) path,
  143. isHinted, glyphOrigin, escapement);
  144. }
  145. void font_set_glyph_to_image(struct vg_font *font,
  146. VGuint glyphIndex,
  147. struct vg_image *image,
  148. const VGfloat glyphOrigin[2],
  149. const VGfloat escapement[2])
  150. {
  151. add_glyph(font, glyphIndex, (struct vg_object *) image,
  152. VG_TRUE, glyphOrigin, escapement);
  153. }
  154. void font_clear_glyph(struct vg_font *font,
  155. VGuint glyphIndex)
  156. {
  157. if (!del_glyph(font, glyphIndex)) {
  158. struct vg_context *ctx = vg_current_context();
  159. vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
  160. }
  161. }
  162. void font_draw_glyph(struct vg_font *font,
  163. VGuint glyphIndex,
  164. VGbitfield paintModes,
  165. VGboolean allowAutoHinting)
  166. {
  167. struct vg_context *ctx = vg_current_context();
  168. struct vg_glyph *glyph;
  169. glyph = get_glyph(font, glyphIndex);
  170. if (!glyph) {
  171. vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
  172. return;
  173. }
  174. vg_render_glyph(ctx, glyph, paintModes, allowAutoHinting);
  175. vg_advance_glyph(ctx, glyph, 0.0f, 0.0f, VG_TRUE);
  176. }
  177. void font_draw_glyphs(struct vg_font *font,
  178. VGint glyphCount,
  179. const VGuint *glyphIndices,
  180. const VGfloat *adjustments_x,
  181. const VGfloat *adjustments_y,
  182. VGbitfield paintModes,
  183. VGboolean allowAutoHinting)
  184. {
  185. struct vg_context *ctx = vg_current_context();
  186. VGint i;
  187. for (i = 0; i < glyphCount; ++i) {
  188. if (!get_glyph(font, glyphIndices[i])) {
  189. vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
  190. return;
  191. }
  192. }
  193. for (i = 0; i < glyphCount; ++i) {
  194. struct vg_glyph *glyph;
  195. VGfloat adj_x, adj_y;
  196. glyph = get_glyph(font, glyphIndices[i]);
  197. vg_render_glyph(ctx, glyph, paintModes, allowAutoHinting);
  198. adj_x = (adjustments_x) ? adjustments_x[i] : 0.0f;
  199. adj_y = (adjustments_y) ? adjustments_y[i] : 0.0f;
  200. vg_advance_glyph(ctx, glyph, adj_x, adj_y, (i == glyphCount - 1));
  201. }
  202. }
  203. VGint font_num_glyphs(struct vg_font *font)
  204. {
  205. return cso_hash_size(font->glyphs);
  206. }
  207. #endif /* OPENVG_VERSION_1_1 */