Subversion Repositories libOLED

Rev

Rev 17 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // font class library
  2. #pragma once
  3. #include <cstdint>
  4.  
  5. class font_t
  6. {
  7. public:
  8.  
  9.  
  10.   /// \param height - pixel character height
  11.   /// \param width  - pixel character width
  12.   /// \param spacing - character to character spacing
  13.   /// \param chars  - number of characters in character set
  14.   /// \param data - constant data
  15.   /// \param mult - coordinate multiplied by before sampling bitmap
  16.   /// \param shift- coordinate right shifted by after multiplication : mult=1, shift=1 means half sized
  17.   font_t(uint8_t const height, uint8_t const width,
  18.          uint8_t const spacing, uint8_t const chars,
  19.          char const *data,
  20.          uint8_t const mult, uint8_t const shift)
  21.       : m_height(height),
  22.         m_width(width),
  23.         m_mapheight(PIXELS(height,mult,shift)),
  24.         m_mapwidth(PIXELS(width,mult,shift)),
  25.         m_spacing(PIXELS(spacing,mult,shift)),
  26.         m_chars(chars),
  27.         m_data(data),
  28.         m_mult(mult),
  29.         m_shift(shift),
  30.         m_round(ROUNDING(shift))
  31.   {
  32.   }
  33.  
  34.   virtual char
  35.   getPixel(char c, int x, int y) = 0;
  36.  
  37.   // character width
  38.   uint8_t
  39.   width()
  40.   {
  41.     return m_mapwidth;
  42.   }
  43.  
  44.   // character height
  45.   uint8_t
  46.   height()
  47.   {
  48.     return m_mapheight;
  49.   }
  50.  
  51.   uint8_t
  52.   chars()
  53.   {
  54.     return m_chars;
  55.   }
  56.  
  57.   uint8_t
  58.   spacing()
  59.   {
  60.     return m_spacing;
  61.   }
  62.  
  63. private:
  64.  
  65.   /// @brief Convert from real bitmap dimension to virtual dimension given
  66.   /// @param dimension original dimension
  67.   /// @param mult  scale up multiplier
  68.   /// @param shift bit shift right after scaling
  69.   /// @return virtual dimension
  70.   static constexpr unsigned PIXELS(uint8_t dimension,uint8_t mult, uint8_t shift) { return (dimension << shift )/mult; };
  71.  
  72.   /// @brief Calculate a rounding value that is 0.5 << shift
  73.   /// @param val shift value
  74.   /// @return 0.5 << shift or 0
  75.   static constexpr uint8_t ROUNDING(uint8_t shift) { return (shift>0) ? (0) : 1<<(shift-1);};
  76.  
  77.  
  78. protected:
  79.   /// @brief Pixel height
  80.   uint8_t const m_height;
  81.   /// @brief Bitmap width
  82.   uint8_t const m_width;
  83.  
  84.   /// @brief bitmap virtual height
  85.   uint8_t const m_mapheight;
  86.   /// @brief Bitmap virtual width  
  87.   uint8_t const m_mapwidth;
  88.  
  89.   /// @brief Spacing between characters
  90.   uint8_t const m_spacing;
  91.   /// @brief Number of characters in the character set bit map
  92.  
  93.  
  94.   uint8_t const m_chars;
  95.   char const *m_data;
  96.  
  97.   unsigned char const m_mult;  //
  98.   unsigned char const m_shift; // bit shift after multiplication
  99.   unsigned char const m_round; // round up value 0.5 << shift
  100. };
  101.  
  102. class font5x7_t : public font_t
  103. {
  104. public:
  105.   // one byte of pixels per character row.
  106.   font5x7_t(unsigned char_count, char const *data) : font_t(7, 5, 6, char_count, data, 1, 0){};
  107.  
  108.   char
  109.   getPixel(char c, int x, int y) override;
  110. };
  111.  
  112. class font10x18_t : public font_t
  113. {
  114. public:
  115.  
  116.   // XBM format with optional multiply, right shift scaling of coordinates
  117.   font10x18_t(unsigned char_count, char const *data, unsigned char mult=1, unsigned char shift=0) : font_t(18,10,10, char_count, data, mult, shift){};
  118.  
  119.   char
  120.   getPixel(char c, int x, int y) override;
  121. };
  122.  
  123. // defined fonts
  124. // original 5x7
  125. extern font5x7_t small_font;
  126.  
  127.  
  128. // lucida font
  129. // in 10x18
  130. extern font10x18_t large_font;
  131.  
  132. // 4/5 scaled down lucida font
  133. //
  134. extern font10x18_t medium_font;
  135.  
  136. /// half sized lucida font
  137. extern font10x18_t half_font;