u8g2_hvline.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. u8g2_hvline.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. Calltree
  27. void u8g2_DrawHVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  28. u8g2->cb->draw_l90
  29. u8g2_draw_hv_line_2dir
  30. u8g2->ll_hvline(u8g2, x, y, len, dir);
  31. */
  32. #include "u8g2.h"
  33. #include <assert.h>
  34. /*==========================================================*/
  35. /* intersection procedure */
  36. /*
  37. Description:
  38. clip range from pos a (included) with line len (a+len excluded) agains c (included) to d (excluded)
  39. Assumptions:
  40. len > 0
  41. c <= d (this is not checked)
  42. will return 0 if there is no intersection and if a > b
  43. */
  44. static uint8_t u8g2_clip_intersection2(u8g2_uint_t *ap, u8g2_uint_t *len, u8g2_uint_t c, u8g2_uint_t d)
  45. {
  46. u8g2_uint_t a = *ap;
  47. u8g2_uint_t b;
  48. b = a;
  49. b += *len;
  50. /*
  51. Description:
  52. clip range from a (included) to b (excluded) agains c (included) to d (excluded)
  53. Assumptions:
  54. a <= b (violation is checked and handled correctly)
  55. c <= d (this is not checked)
  56. will return 0 if there is no intersection and if a > b
  57. optimized clipping: c is set to 0 --> 27 Oct 2018: again removed the c==0 assumption
  58. replaced by uint8_t u8g2_clip_intersection2
  59. */
  60. /* handle the a>b case correctly. If code and time is critical, this could */
  61. /* be removed completly (be aware about memory curruption for wrong */
  62. /* arguments) or return 0 for a>b (will lead to skipped lines for wrong */
  63. /* arguments) */
  64. /* removing the following if clause completly may lead to memory corruption of a>b */
  65. if ( a > b )
  66. {
  67. /* replacing this if with a simple "return 0;" will not handle the case with negative a */
  68. if ( a < d )
  69. {
  70. b = d;
  71. b--;
  72. }
  73. else
  74. {
  75. a = c;
  76. }
  77. }
  78. /* from now on, the asumption a <= b is ok */
  79. if ( a >= d )
  80. return 0;
  81. if ( b <= c )
  82. return 0;
  83. if ( a < c )
  84. a = c;
  85. if ( b > d )
  86. b = d;
  87. *ap = a;
  88. b -= a;
  89. *len = b;
  90. return 1;
  91. }
  92. /*==========================================================*/
  93. /* draw procedures */
  94. /*
  95. x,y Upper left position of the line within the pixel buffer
  96. len length of the line in pixel, len must not be 0
  97. dir 0: horizontal line (left to right)
  98. 1: vertical line (top to bottom)
  99. This function first adjusts the y position to the local buffer. Then it
  100. will clip the line and call u8g2_draw_low_level_hv_line()
  101. */
  102. void u8g2_draw_hv_line_2dir(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  103. {
  104. /* clipping happens before the display rotation */
  105. /* transform to pixel buffer coordinates */
  106. y -= u8g2->pixel_curr_row;
  107. u8g2->ll_hvline(u8g2, x, y, len, dir);
  108. }
  109. /*
  110. This is the toplevel function for the hv line draw procedures.
  111. This function should be called by the user.
  112. "dir" may have 4 directions: 0 (left to right), 1, 2, 3 (down up)
  113. */
  114. void u8g2_DrawHVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  115. {
  116. /* Make a call to the callback function (e.g. u8g2_draw_l90_r0). */
  117. /* The callback may rotate the hv line */
  118. /* after rotation this will call u8g2_draw_hv_line_4dir() */
  119. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  120. if ( u8g2->is_page_clip_window_intersection != 0 )
  121. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  122. if ( len != 0 )
  123. {
  124. /* convert to two directions */
  125. if ( len > 1 )
  126. {
  127. if ( dir == 2 )
  128. {
  129. x -= len;
  130. x++;
  131. }
  132. else if ( dir == 3 )
  133. {
  134. y -= len;
  135. y++;
  136. }
  137. }
  138. dir &= 1;
  139. /* clip against the user window */
  140. if ( dir == 0 )
  141. {
  142. if ( y < u8g2->user_y0 )
  143. return;
  144. if ( y >= u8g2->user_y1 )
  145. return;
  146. if ( u8g2_clip_intersection2(&x, &len, u8g2->user_x0, u8g2->user_x1) == 0 )
  147. return;
  148. }
  149. else
  150. {
  151. if ( x < u8g2->user_x0 )
  152. return;
  153. if ( x >= u8g2->user_x1 )
  154. return;
  155. if ( u8g2_clip_intersection2(&y, &len, u8g2->user_y0, u8g2->user_y1) == 0 )
  156. return;
  157. }
  158. u8g2->cb->draw_l90(u8g2, x, y, len, dir);
  159. }
  160. }
  161. void u8g2_DrawHLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len)
  162. {
  163. // #ifdef U8G2_WITH_INTERSECTION
  164. // if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 )
  165. // return;
  166. // #endif /* U8G2_WITH_INTERSECTION */
  167. u8g2_DrawHVLine(u8g2, x, y, len, 0);
  168. }
  169. void u8g2_DrawVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len)
  170. {
  171. // #ifdef U8G2_WITH_INTERSECTION
  172. // if ( u8g2_IsIntersection(u8g2, x, y, x+1, y+len) == 0 )
  173. // return;
  174. // #endif /* U8G2_WITH_INTERSECTION */
  175. u8g2_DrawHVLine(u8g2, x, y, len, 1);
  176. }
  177. void u8g2_DrawPixel(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y)
  178. {
  179. #ifdef U8G2_WITH_INTERSECTION
  180. if ( y < u8g2->user_y0 )
  181. return;
  182. if ( y >= u8g2->user_y1 )
  183. return;
  184. if ( x < u8g2->user_x0 )
  185. return;
  186. if ( x >= u8g2->user_x1 )
  187. return;
  188. #endif /* U8G2_WITH_INTERSECTION */
  189. u8g2_DrawHVLine(u8g2, x, y, 1, 0);
  190. }
  191. /*
  192. Assign the draw color for all drawing functions.
  193. color may be 0 or 1. The actual color is defined by the display.
  194. With color = 1 the drawing function will set the display memory to 1.
  195. For OLEDs this ususally means, that the pixel is enabled and the LED
  196. at the pixel is turned on.
  197. On an LCD it usually means that the LCD segment of the pixel is enabled,
  198. which absorbs the light.
  199. For eInk/ePaper it means black ink.
  200. 7 Jan 2017: Allow color value 2 for XOR operation.
  201. */
  202. void u8g2_SetDrawColor(u8g2_t *u8g2, uint8_t color)
  203. {
  204. u8g2->draw_color = color; /* u8g2_SetDrawColor: just assign the argument */
  205. if ( color >= 3 )
  206. u8g2->draw_color = 1; /* u8g2_SetDrawColor: make color as one if arg is invalid */
  207. }