123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- * guilib - a minimal pixel framework
- *
- * Copyright (c) 2009 Openmoko Inc.
- *
- * Authors Daniel Mack <daniel@caiaq.de>
- * Holger Hans Peter Freyther <zecke@openmoko.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <string.h>
- /* wiki-lib includes */
- #include <wikilib.h>
- #include <file-io.h>
- /* gui-lib includes */
- #include "guilib.h"
- #include "glyph.h"
- #include <regs.h>
- #include <lcd.h>
- #include <samo.h>
- #include "lcd_buf_draw.h"
- #include "search.h"
- #include "msg.h"
- #define DBG_GLYPH 0
- void render_glyph(int start_x, int start_y, const struct glyph *glyph, char *buf)
- {
- int x, y, w, bit = 0;
- const char *d = glyph->data;
- for (y = start_y; y < start_y + glyph->height; y++) {
- for (x = start_x, w = glyph->width; w > 0;) {
- int use;
- unsigned byte = (x + LCD_VRAM_WIDTH_PIXELS * y) / 8;
- if (byte >= LCD_BUF_HEIGHT_PIXELS * LCD_BUF_WIDTH_BYTES)
- return;
- use = MIN(8 - (x % 8) , w);
- use = MIN(8 - bit, use);
- #ifdef DISPLAY_INVERTED
- buf[byte] &= ~((*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8));
- #else
- buf[byte] |= (*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8);
- #endif
- bit += use;
- x += use;
- w -= use;
- if (bit == 8) {
- bit = 0;
- d++;
- }
- }
- }
- }
- int render_string(const int font, int start_x,
- int start_y, char *string, int text_length, int inverted)
- {
- int x;
- int width;
- long len = text_length;
- long lenLast = 0;
- long widthLast = 0;
- char *p = (char *)string;
- unsigned char *q;
- int nCharBytes;
- ucs4_t c;
- if (start_x < 0)
- width = 0;
- else
- width = start_x;
- while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
- {
- lenLast = len;
- widthLast = width;
- width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
- }
- if (width > LCD_BUF_WIDTH_PIXELS)
- {
- text_length -= lenLast;
- width = widthLast;
- }
- if (start_x < 0) // to be centered
- {
- start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
- if (start_x < 0)
- start_x = 0;
- }
- x = start_x;
- q = (unsigned char *)string;
- while (*q) {
- c = UTF8_to_UCS4(&q);
- x = draw_bmf_char(c,font-1,x,start_y, inverted, 0);
- if(x<0)
- return 0;
- }
- return x;
- }
- int render_string_and_clear(const int font, int start_x,
- int start_y, char *string, int text_length, int inverted,
- int clear_start_x, int clear_start_y, int clear_end_x, int clear_end_y)
- {
- int x;
- int width;
- int height;
- long len = text_length;
- long lenLast = 0;
- long widthLast = 0;
- char *p = (char *)string;
- unsigned char *q;
- int nCharBytes;
- ucs4_t c;
- if (clear_start_x >= 0 && clear_start_y < start_y)
- {
- if (clear_end_y < start_y)
- {
- guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
- clear_start_x = -1; // no more area to clear
- }
- else
- {
- guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, start_y - 1);
- clear_start_y = start_y;
- }
- }
- if (start_x < 0)
- width = 0;
- else
- width = start_x;
- height = GetFontLinespace(font);
- while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
- {
- lenLast = len;
- widthLast = width;
- width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
- }
- if (width > LCD_BUF_WIDTH_PIXELS)
- {
- text_length -= lenLast;
- width = widthLast;
- }
- if (start_x < 0) // to be centered
- {
- start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
- if (start_x < 0)
- start_x = 0;
- }
- if (clear_start_y < start_y + height)
- {
- if (clear_start_x < start_x)
- guilib_clear_area(clear_start_x, clear_start_y, start_x - 1,
- clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
- if (clear_end_x >= width)
- guilib_clear_area(width, clear_start_y, clear_end_x,
- clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
- if (clear_end_y >= start_y + height)
- clear_start_y = start_x + height;
- else
- clear_start_x = -1;
- }
- if (clear_start_x >= 0)
- guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
- x = start_x;
- q = (unsigned char *)string;
- while (*q) {
- c = UTF8_to_UCS4(&q);
- x = draw_bmf_char(c,font-1,x,start_y, inverted, 1);
- if(x<0)
- return 0;
- }
- return x;
- }
- // if search string is longer than the LCD width, keep the right of it to fit
- int render_string_right(const int font, int start_x,
- int start_y, char *string, int text_length, int inverted)
- {
- int i;
- int x;
- int utf8_chars = 0;
- int widths[MAX_TITLE_SEARCH];
- int lens[MAX_TITLE_SEARCH];
- int width = start_x;
- long len = text_length;
- char *p = (char *)string;
- unsigned char *q;
- int nCharBytes;
- int rc;
- ucs4_t c;
- while (len > 0 && utf8_chars < MAX_TITLE_SEARCH)
- {
- lens[utf8_chars] = len;
- widths[utf8_chars] = get_UTF8_char_width(font, &p, &len, &nCharBytes);
- width += widths[utf8_chars];
- utf8_chars++;
- }
- rc = width;
- if (width > LCD_BUF_WIDTH_PIXELS)
- {
- int width_to_descrease = width - LCD_BUF_WIDTH_PIXELS;
- width = 0;
- for (i = 0; i < utf8_chars && width < width_to_descrease; i++)
- width += widths[i];
- if (i < utf8_chars)
- {
- string = &string[text_length - lens[i]];
- text_length = lens[i];
- }
- }
- x = start_x;
- q = (unsigned char *)string;
- while (*q) {
- c = UTF8_to_UCS4(&q);
- x = draw_bmf_char(c,font-1,x,start_y, inverted, 0);
- if(x<0)
- return 0;
- }
- return rc;
- }
|