選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

161 行
4.5 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains a small framework for canvas frame management.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. /** \brief Get the number of frames in a canvas.
  26. *
  27. * This function returns the current canvas frame count.
  28. *
  29. * \param cv A libcucul canvas
  30. * \return The frame count
  31. */
  32. unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
  33. {
  34. return cv->framecount;
  35. }
  36. /** \brief Activate a given canvas frame.
  37. *
  38. * This function sets the active canvas frame. All subsequent drawing
  39. * operations will be performed on that frame. The current painting
  40. * context set by cucul_set_color() or cucul_set_truecolor() is inherited.
  41. *
  42. * If the frame index is outside the canvas' frame range, nothing happens.
  43. *
  44. * \param cv A libcucul canvas
  45. * \param frame The canvas frame to activate
  46. */
  47. void cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  48. {
  49. if(frame >= cv->framecount)
  50. return;
  51. cv->frame = frame;
  52. cv->chars = cv->allchars[cv->frame];
  53. cv->attr = cv->allattr[cv->frame];
  54. }
  55. /** \brief Add a frame to a canvas.
  56. *
  57. * This function creates a new frame within the given canvas. Its contents
  58. * are copied from the currently active frame.
  59. *
  60. * The frame index indicates where the frame should be inserted. Valid
  61. * values range from 0 to the current canvas frame count. If the frame
  62. * index is greater the or equals the current canvas frame count, the new
  63. * frame is appended at the end of the canvas.
  64. *
  65. * The active frame does not change, but its index may be renumbered due
  66. * to the insertion.
  67. *
  68. * \param cv A libcucul canvas
  69. * \param frame The index where to insert the new frame
  70. */
  71. void cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  72. {
  73. unsigned int size = cv->width * cv->height * sizeof(uint32_t);
  74. unsigned int f;
  75. if(frame > cv->framecount)
  76. frame = cv->framecount;
  77. cv->framecount++;
  78. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  79. cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
  80. for(f = cv->framecount - 1; f > frame; f--)
  81. {
  82. cv->allchars[f] = cv->allchars[f - 1];
  83. cv->allattr[f] = cv->allattr[f - 1];
  84. }
  85. cv->allchars[frame] = malloc(size);
  86. memcpy(cv->allchars[frame], cv->chars, size);
  87. cv->allattr[frame] = malloc(size);
  88. memcpy(cv->allattr[frame], cv->attr, size);
  89. if(cv->frame >= frame)
  90. cv->frame++;
  91. cv->chars = cv->allchars[cv->frame];
  92. cv->attr = cv->allattr[cv->frame];
  93. }
  94. /** \brief Remove a frame from a canvas.
  95. *
  96. * This function deletes a frame from a given canvas.
  97. *
  98. * It is not legal to remove the last frame from a canvas. Such a request
  99. * will be ignored by cucul_free_canvas_frame().
  100. *
  101. * The frame index indicates the frame to delete. Valid values range from
  102. * 0 to the current canvas frame count minus 1. If the frame index is
  103. * greater the or equals the current canvas frame count, the last frame
  104. * is deleted.
  105. *
  106. * If the active frame is deleted, frame 0 becomes the new active frame.
  107. * Otherwise, the active frame does not change, but its index may be
  108. * renumbered due to the deletion.
  109. *
  110. * \param cv A libcucul canvas
  111. * \param frame The index of the frame to delete
  112. */
  113. void cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  114. {
  115. unsigned int f;
  116. if(frame >= cv->framecount)
  117. return;
  118. if(cv->framecount == 1)
  119. return;
  120. free(cv->allchars[frame]);
  121. free(cv->allattr[frame]);
  122. for(f = frame + 1; f < cv->framecount; f++)
  123. {
  124. cv->allchars[f - 1] = cv->allchars[f];
  125. cv->allattr[f - 1] = cv->allattr[f];
  126. }
  127. cv->framecount--;
  128. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  129. cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
  130. if(cv->frame > frame)
  131. cv->frame--;
  132. else if(cv->frame == frame)
  133. cv->frame = 0;
  134. cv->chars = cv->allchars[cv->frame];
  135. cv->attr = cv->allattr[cv->frame];
  136. }