u8g2_kerning.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. u8g2_kerning.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. */
  27. #include "u8g2.h"
  28. /* this function is used as "u8g2_get_kerning_cb" */
  29. /*
  30. uint8_t u8g2_GetNullKerning(u8g2_t *u8g2, uint16_t e1, uint16_t e2)
  31. {
  32. return 0;
  33. }
  34. */
  35. /* this function is used as "u8g2_get_kerning_cb" */
  36. uint8_t u8g2_GetKerning(U8X8_UNUSED u8g2_t *u8g2, u8g2_kerning_t *kerning, uint16_t e1, uint16_t e2)
  37. {
  38. uint16_t i1, i2, cnt, end;
  39. if ( kerning == NULL )
  40. return 0;
  41. /* search for the encoding in the first table */
  42. cnt = kerning->first_table_cnt;
  43. cnt--; /* ignore the last element of the table, which is 0x0ffff */
  44. for( i1 = 0; i1 < cnt; i1++ )
  45. {
  46. if ( kerning->first_encoding_table[i1] == e1 )
  47. break;
  48. }
  49. if ( i1 >= cnt )
  50. return 0; /* e1 not part of the kerning table, return 0 */
  51. /* get the upper index for i2 */
  52. end = kerning->index_to_second_table[i1+1];
  53. for( i2 = kerning->index_to_second_table[i1]; i2 < end; i2++ )
  54. {
  55. if ( kerning->second_encoding_table[i2] == e2 )
  56. break;
  57. }
  58. if ( i2 >= end )
  59. return 0; /* e2 not part of any pair with e1, return 0 */
  60. return kerning->kerning_values[i2];
  61. }
  62. uint8_t u8g2_GetKerningByTable(U8X8_UNUSED u8g2_t *u8g2, const uint16_t *kt, uint16_t e1, uint16_t e2)
  63. {
  64. uint16_t i;
  65. i = 0;
  66. if ( kt == NULL )
  67. return 0;
  68. for(;;)
  69. {
  70. if ( kt[i] == 0x0ffff )
  71. break;
  72. if ( kt[i] == e1 && kt[i+1] == e2 )
  73. return kt[i+2];
  74. i+=3;
  75. }
  76. return 0;
  77. }