static bool nsfont_position_in_string(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int x, size_t *char_offset, int *actual_x)
{
        uint32_t ucs4;
        size_t nxtchr = 0;
        FT_Glyph glyph;
        int prev_x = 0;

        *actual_x = 0;
        while (nxtchr < length) {
                ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);

                glyph = fb_getglyph(fstyle, ucs4);
                if (glyph == NULL)
                        continue;

                *actual_x += glyph->advance.x >> 16;
                if (*actual_x > x)
                        break;

                prev_x = *actual_x;
                nxtchr = utf8_next(string, length, nxtchr);
        }

        /* choose nearest of previous and last x */
        if (abs(*actual_x - x) > abs(prev_x - x))
                *actual_x = prev_x;

        *char_offset = nxtchr;
	return true;
}
Example #2
0
vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
{
	if (utf8str.empty())
		return vector<char_type>();

	return utf8_to_ucs4(&utf8str[0], utf8str.size());
}
Example #3
0
int console_print(struct console * console, const char * fmt, ...)
{
	va_list ap;
	u32_t code;
	char *p, *buf;
	int len;

	if(!console || !console->putcode)
		return 0;

	va_start(ap, fmt);
	len = vsnprintf(NULL, 0, fmt, ap);
	if(len < 0)
		return 0;
	buf = malloc(len + 1);
	if(!buf)
		return 0;
	len = vsnprintf(buf, len + 1, fmt, ap);
	va_end(ap);

	for(p = buf; utf8_to_ucs4(&code, 1, p, -1, (const char **)&p) > 0; )
	{
		console->putcode(console, code);
	}

	free(buf);
	return len;
}
Example #4
0
static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd,
		const char *chunk, size_t inlen,
		char **out, size_t *outlen)
{
	size_t ret, esclen;
	uint32_t ucs4;
	char *pescape, escape[11];

	while (inlen > 0) {
		ret = iconv(cd, (void *) &chunk, &inlen, (void *) out, outlen);
		if (ret != (size_t) -1)
			break;

		if (errno != EILSEQ)
			return UTF8_CONVERT_NOMEM;

		ucs4 = utf8_to_ucs4(chunk, inlen);
		esclen = snprintf(escape, sizeof(escape), "&#x%06x;", ucs4);
		pescape = escape;
		ret = iconv(cd, (void *) &pescape, &esclen,
				(void *) out, outlen);
		if (ret == (size_t) -1)
			return UTF8_CONVERT_NOMEM;

		esclen = utf8_next(chunk, inlen, 0);
		chunk += esclen;
		inlen -= esclen;
	}

	return UTF8_CONVERT_OK;
}
Example #5
0
int utf8_gb(const char *utf8, char *gb, size_t cnt)
{
        int wc = 0; /* word count */
        const char *putf = utf8;
        uint32_t ucs4; /* UCS-4 data */
        uint16_t utf16; /* UTF-16 data */
        uint16_t gb2; /*  GB 2-byte data */
        size_t max = sizeof(GB_UCS) / 4 - 1; /* high index */

        while(cnt > 0) {
                cnt -= utf8_to_ucs4(&putf, &ucs4);
                if(0x00000000 != (ucs4 & 0xFFFF0000)) {
                        fprintf(stderr, "UCS data(0x%08X) is bigger than 0xFFFF!\n", (unsigned int)ucs4);
                        break;
                }
                utf16 = (uint16_t)ucs4;

                if(utf16 == 0x0000) {
                        break;
                }
                else if(utf16 <= 0x007F) {
                        *gb++ = (char)utf16;
                }
                else {
                        gb2 = half_search(utf16, DFLT_GB, max, UCS_GB);
                        *gb++ = (char)(gb2 >> 8);
                        *gb++ = (char)(gb2 >> 0);
                }
                wc++;
        }
        *gb = '\0';

        return wc;
}
static int collate(void *unused,int l1,const void *s1,int l2,const void *s2)
{
	int len1;
	int len2;
	unsigned char *str1;
	unsigned char *str2;

	len1=l1;
	len2=l2;
	str1=(unsigned char *)(s1);
	str2=(unsigned char *)(s2);
	while(len1&&len2)
	{
		int res;
		unsigned int val1;
		unsigned int val2;

		if(!(res=utf8_to_ucs4(str1,len1,&val1)))goto utf8cmp;
		len1-=res;
		str1+=res;
		if(!(res=utf8_to_ucs4(str2,len2,&val2)))goto utf8cmp;
		len2-=res;
		str2+=res;
		val1=ucs4_normalize(val1);
		val2=ucs4_normalize(val2);
		if(val1<val2)return -1;
		if(val1>val2)return 1;
	}
	if(len1)return 1;
	if(len2)return -1;

utf8cmp:len1=l1;
	len2=l2;
	str1=(unsigned char *)(s1);
	str2=(unsigned char *)(s2);
	while(len1&&len2)
	{
		if(*str1<*str2)return -1;
		if(*str1++>*str2++)return 1;
		len1--;
		len2--;
	}
	if(len1)return 1;
	if(len2)return -1;
	return 0;
}
Example #7
0
TextView::TextView( Program& _program, int _recursion_depth, Text& _text )
	: Mode(_program, _recursion_depth), word_screen(SCREEN_MAIN), text_screen(SCREEN_SUB), text(_text),
		word_browser(button_provider_list, *_program.ft, current_words, text_screen, *_program.library),
		y_offset(16), v_y(0), sub_frame_count(0), current_highlight(0),
		current_highlight_x(0), current_highlight_y(0), context_mode(CONTEXT_WORDS_BY_CONTEXT),
		context_render_char(0),
		settings_button(text_screen,"",SpriteSize_16x16,text_screen.res_x-16,text_screen.res_y-16,_program.ft->latin_face,10,1,1),
		loading_symbol(text_screen,"",SpriteSize_32x32,text_screen.res_x/2-16,text_screen.res_y/2-16,program.ft->han_face,14,0,1),
		lookup_from_current_lesson(true), lookup_from_previous_lessons(true), 
		lookup_from_upcoming_lessons(true), lookup_from_other_books(true),
		old_y_offset(0), old_abs_y_diff(0), pixels_scrolled(0), render_info( 0, 0, 0, 0 ),
		prev_size(0), org_size(0)
{
	this->init_mode();
	
	// FIXME: settings dialog item ordering relies on std::map implementation for now; don't know if this is portable
	this->settings.add_setting( new SettingsLabel("0_lookup_label","Lookup selected words...") );
	this->settings.add_setting( new BooleanSetting("1_lookup_from_current_lesson","from current Lesson",this->lookup_from_current_lesson) );
	this->settings.add_setting( new BooleanSetting("2_lookup_from_previous_lessons","from previous Lessons",this->lookup_from_previous_lessons) );
	this->settings.add_setting( new BooleanSetting("3_lookup_from_upcoming_lessons","from upcoming Lessons",this->lookup_from_upcoming_lessons) );
	this->settings.add_setting( new BooleanSetting("4_lookup_from_other_books","from other Books",this->lookup_from_other_books) );
	
	this->text_buttons.push_back( &this->settings_button );
	this->text_buttons.push_back( &this->loading_symbol );
	
	// disable child mode buttons when recursion limit is reached:
	if( this->recursion_depth>=Mode::MAX_RECURSION_DEPTH )
	{
		this->word_browser.as_text_tab.hidden = this->word_browser.as_text_tab.disabled = true;
		this->word_browser.search_button.hidden = this->word_browser.search_button.disabled = true;
		this->word_browser.stroke_order_tab.hidden = this->word_browser.stroke_order_tab.disabled = true;
	}
	// ignore touch events on loading_symbol:
	this->loading_symbol.disabled = true;
	
	// load word lookup settings from config:
	this->lookup_from_current_lesson = this->program.config->get( "text_view.lookup_from_current_lesson", this->lookup_from_current_lesson );
	this->lookup_from_previous_lessons = this->program.config->get( "text_view.lookup_from_previous_lessons", this->lookup_from_previous_lessons );
	this->lookup_from_upcoming_lessons = this->program.config->get( "text_view.lookup_from_upcoming_lessons", this->lookup_from_upcoming_lessons );
	this->lookup_from_other_books = this->program.config->get( "text_view.lookup_from_other_books", this->lookup_from_other_books );
	this->restore_init_settings();
	
	if( !utf8_to_ucs4((unsigned char*)this->text.c_str(), this->char_list) )
    {
        WARN( "error in utf-8 input (non fatal)" );
    }
	this->org_size = this->char_list.size();
	
	this->init_vram();
}
Example #8
0
static void
nsgtk_tree_window_input_method_commit(GtkIMContext *ctx,
		const gchar *str, gpointer data)
{
	struct nsgtk_treeview *tw = (struct nsgtk_treeview *) data;
	size_t len = strlen(str), offset = 0;

	while (offset < len) {
		uint32_t nskey = utf8_to_ucs4(str + offset, len - offset);

		tree_keypress(tw->tree, nskey);

		offset = utf8_next(str, len, offset);
	}
}
Example #9
0
static bool 
framebuffer_plot_text(int x, int y, const char *text, size_t length,
		const plot_font_style_t *fstyle)
{
        uint32_t ucs4;
        size_t nxtchr = 0;
        FT_Glyph glyph;
        FT_BitmapGlyph bglyph;
        nsfb_bbox_t loc;

        while (nxtchr < length) {
                ucs4 = utf8_to_ucs4(text + nxtchr, length - nxtchr);
                nxtchr = utf8_next(text, length, nxtchr);

                glyph = fb_getglyph(fstyle, ucs4);
                if (glyph == NULL)
                        continue;

                if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
                        bglyph = (FT_BitmapGlyph)glyph;

                        loc.x0 = x + bglyph->left;
                        loc.y0 = y - bglyph->top;
                        loc.x1 = loc.x0 + bglyph->bitmap.width;
                        loc.y1 = loc.y0 + bglyph->bitmap.rows;

                        /* now, draw to our target surface */
                        if (bglyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {
                            nsfb_plot_glyph1(nsfb, 
                                             &loc, 
                                             bglyph->bitmap.buffer, 
                                             bglyph->bitmap.pitch, 
                                             fstyle->foreground);
                        } else {
                            nsfb_plot_glyph8(nsfb, 
                                             &loc, 
                                             bglyph->bitmap.buffer, 
                                             bglyph->bitmap.pitch, 
                                             fstyle->foreground);
                        }
                }
                x += glyph->advance.x >> 16;

        }
        return true;

}
Example #10
0
/*
---------------------------------------
    检查是否是 UTF-8
---------------------------------------
*/
static bool_t is_utf8_file (const ansi_t *str)
{
    leng_t temp;
    int32u ucs4;

    /* BOM 标志检查 */
    if (chr_cmpA(str, BOM_UTF8, 3) == 0)
        str += 3;

    /* 逐个字符检查 */
    while (*str != NIL) {
        temp = utf8_to_ucs4(&ucs4, str);
        if (temp == 0)
            return (FALSE);
        str += temp;
    }
    return (TRUE);
}
Example #11
0
int utf8_utf16(const char *utf8, uint16_t *utf16, size_t cnt, int endian)
{
        int wc = 0; /* word count */
        const char *putf = utf8;
        uint16_t *putf16 = utf16;
        uint32_t ucs4; /* UCS-4 data */

        while(cnt > 0) {
                cnt -= utf8_to_ucs4(&putf, &ucs4);
                if(0x00000000 == ucs4) {
                        break;
                }
                ucs4_to_utf16(ucs4, &putf16, endian);
                wc++;
        }
        *putf16 = 0x0000;

        return wc;
}
Example #12
0
bool_t console_stdout_putc(char c)
{
	static size_t size = 0;
	static char buf[6];
	char * rest;
	u32_t code;

	if(!console_stdout || !console_stdout->putcode)
		return FALSE;

	buf[size++] = c;
	while(utf8_to_ucs4(&code, 1, buf, size, (const char **)&rest) > 0)
	{
		led_console_trigger_activity();

		size -= rest - buf;
		memmove(buf, rest, size);
		console_stdout->putcode(console_stdout, code);
	}
	return TRUE;
}
Example #13
0
static bool nsfont_split(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int x, size_t *char_offset, int *actual_x)
{
        uint32_t ucs4;
        size_t nxtchr = 0;
        int last_space_x = 0;
        int last_space_idx = 0;
        FT_Glyph glyph;

        *actual_x = 0;
        while (nxtchr < length) {
                ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);

                glyph = fb_getglyph(fstyle, ucs4);
                if (glyph == NULL)
                        continue;

                if (ucs4 == 0x20) {
                        last_space_x = *actual_x;
                        last_space_idx = nxtchr;
                }

                *actual_x += glyph->advance.x >> 16;
                if (*actual_x > x) {
                        /* string has exceeded available width return previous
                         * space 
                         */
                        *actual_x = last_space_x;
                        *char_offset = last_space_idx;
                        return true;
                }

                nxtchr = utf8_next(string, length, nxtchr);
        }

        *char_offset = nxtchr;

	return true;
}
Example #14
0
//#else
static bool framebuffer_plot_text_internal(int x, int y, const char *text, size_t length,
        const plot_font_style_t *fstyle)
{
    enum fb_font_style style = fb_get_font_style(fstyle);
    int size = fb_get_font_size(fstyle);
    const uint8_t *chrp;
    size_t nxtchr = 0;
    nsfb_bbox_t loc;
    uint32_t ucs4;
    int p = FB_FONT_PITCH * size;
    int w = FB_FONT_WIDTH * size;
    int h = FB_FONT_HEIGHT * size;

    y -= ((h * 3) / 4);
    /* the coord is the bottom-left of the pixels offset by 1 to make
     * it work since fb coords are the top-left of pixels */
    y += 1;

    while (nxtchr < length) {
        ucs4 = utf8_to_ucs4(text + nxtchr, length - nxtchr);
        nxtchr = utf8_next(text, length, nxtchr);

        if (!codepoint_displayable(ucs4))
            continue;

        loc.x0 = x;
        loc.y0 = y;
        loc.x1 = loc.x0 + w;
        loc.y1 = loc.y0 + h;

        chrp = fb_get_glyph_internal(ucs4, style, size);
        nsfb_plot_glyph1(nsfb, &loc, chrp, p, fstyle->foreground);

        x += w;

    }

    return true;
}
Example #15
0
/**
 * Measure the width of a string.
 *
 * \param  fstyle  style for this text
 * \param  string  UTF-8 string to measure
 * \param  length  length of string
 * \param  width   updated to width of string[0..length)
 * \return  true on success, false on error and error reported
 */
static bool nsfont_width(const plot_font_style_t *fstyle,
                         const char *string, size_t length,
                         int *width)
{
        uint32_t ucs4;
        size_t nxtchr = 0;
        FT_Glyph glyph;

        *width = 0;
        while (nxtchr < length) {
                ucs4 = utf8_to_ucs4(string + nxtchr, length - nxtchr);
                nxtchr = utf8_next(string, length, nxtchr);

                glyph = fb_getglyph(fstyle, ucs4);
                if (glyph == NULL)
                        continue;

                *width += glyph->advance.x >> 16;
        }

	return true;
}
Example #16
0
char * opencc_convert_utf8(opencc_t t_opencc, const char * inbuf, size_t length)
{
    if (!lib_initialized)
        lib_initialize();

    if (length == (size_t) -1 || length > strlen(inbuf))
        length = strlen(inbuf);

    /* 將輸入數據轉換爲ucs4_t字符串 */
    ucs4_t * winbuf = utf8_to_ucs4(inbuf, length);
    if (winbuf == (ucs4_t *) -1)
    {
        /* 輸入數據轉換失敗 */
        errnum = OPENCC_ERROR_ENCODIND;
        return (char *) -1;
    }

    /* 設置輸出UTF8文本緩衝區空間 */
    size_t outbuf_len = length;
    size_t outsize = outbuf_len;
    char * original_outbuf = (char *) malloc(sizeof(char) * (outbuf_len + 1));
    char * outbuf = original_outbuf;
    original_outbuf[0] = '\0';

    /* 設置轉換緩衝區空間 */
    size_t wbufsize = length + 64;
    ucs4_t * woutbuf = (ucs4_t *) malloc(sizeof(ucs4_t) * (wbufsize + 1));

    ucs4_t * pinbuf = winbuf;
    ucs4_t * poutbuf = woutbuf;
    size_t inbuf_left, outbuf_left;

    inbuf_left = ucs4len(winbuf);
    outbuf_left = wbufsize;

    while (inbuf_left > 0)
    {
        size_t retval = opencc_convert(t_opencc, &pinbuf, &inbuf_left, &poutbuf, &outbuf_left);
        if (retval == (size_t) -1)
        {
            free(outbuf);
            free(winbuf);
            free(woutbuf);
            return (char *) -1;
        }

        *poutbuf = L'\0';

        char * ubuff = ucs4_to_utf8(woutbuf, (size_t) -1);

        if (ubuff == (char *) -1)
        {
            free(outbuf);
            free(winbuf);
            free(woutbuf);
            errnum = OPENCC_ERROR_ENCODIND;
            return (char *) -1;
        }

        size_t ubuff_len = strlen(ubuff);

        while (ubuff_len > outsize)
        {
            size_t outbuf_offset = outbuf - original_outbuf;
            outsize += outbuf_len;
            outbuf_len += outbuf_len;
            original_outbuf = (char *) realloc(original_outbuf, sizeof(char) * outbuf_len);
            outbuf = original_outbuf + outbuf_offset;
        }

        strncpy(outbuf, ubuff, ubuff_len);
        free(ubuff);

        outbuf += ubuff_len;
        *outbuf = '\0';

        outbuf_left = wbufsize;
        poutbuf = woutbuf;
    }

    free(winbuf);
    free(woutbuf);

    original_outbuf = (char *) realloc(original_outbuf,
                                       sizeof(char) * (strlen(original_outbuf) + 1));

    return original_outbuf;
}
Example #17
0
File: text.c Project: Axure/librime
int parse_entry(const char* buff, TextEntry* entry_i) {
  size_t length;
  const char* pbuff;

  /* 解析鍵 */
  for (pbuff = buff; *pbuff != '\t' && *pbuff != '\0'; ++pbuff) {}

  if (*pbuff == '\0') {
    return -1;
  }
  length = pbuff - buff;

  ucs4_t* ucs4_buff;
  ucs4_buff = utf8_to_ucs4(buff, length);

  if (ucs4_buff == (ucs4_t*)-1) {
    return -1;
  }
  entry_i->key = (ucs4_t*)malloc((length + 1) * sizeof(ucs4_t));
  ucs4cpy(entry_i->key, ucs4_buff);
  free(ucs4_buff);

  /* 解析值 */
  size_t value_i, value_count = INITIAL_DICTIONARY_SIZE;
  entry_i->value = (ucs4_t**)malloc(value_count * sizeof(ucs4_t*));

  for (value_i = 0; *pbuff != '\0' && *pbuff != '\n'; ++value_i) {
    if (value_i >= value_count) {
      value_count += value_count;
      entry_i->value = (ucs4_t**)realloc(
        entry_i->value,
        value_count * sizeof(ucs4_t*)
        );
    }

    for (buff = ++pbuff;
         *pbuff != ' ' && *pbuff != '\0' && *pbuff != '\n' && *pbuff != '\r';
         ++pbuff) {}
    length = pbuff - buff;
    ucs4_buff = utf8_to_ucs4(buff, length);

    if (ucs4_buff == (ucs4_t*)-1) {
      /* 發生錯誤 回退內存申請 */
      ssize_t i;

      for (i = value_i - 1; i >= 0; --i) {
        free(entry_i->value[i]);
      }
      free(entry_i->value);
      free(entry_i->key);
      return -1;
    }

    entry_i->value[value_i] = (ucs4_t*)malloc((length + 1) * sizeof(ucs4_t));
    ucs4cpy(entry_i->value[value_i], ucs4_buff);
    free(ucs4_buff);
  }

  entry_i->value = (ucs4_t**)realloc(
    entry_i->value,
    value_count * sizeof(ucs4_t*)
    );
  entry_i->value[value_i] = NULL;

  return 0;
}
Example #18
0
/*
 * handle X11 events
 * here we handle key, mouse, repaint and window sizing events
 */
static void X11_HandleEvents(GF_VideoOutput *vout)
{
	GF_Event evt;
	Window the_window;
	XComposeStatus state;
	X11VID();
	unsigned char keybuf[32];
	XEvent xevent;
	the_window = xWindow->fullscreen ? xWindow->full_wnd : xWindow->wnd;
	XSync(xWindow->display, False);

	while (X11_Pending(xWindow->display)) {
		  XNextEvent(xWindow->display, &xevent);
		  if (xevent.xany.window!=the_window) continue;
		  switch (xevent.type) {
		    /*
		     * X11 window resized event
		     * must inform GPAC
		     */
		  case ConfigureNotify:
		    if ((unsigned int) xevent.xconfigure.width != xWindow->w_width
			|| (unsigned int) xevent.xconfigure.height != xWindow->w_height)
		      {
			evt.type = GF_EVENT_SIZE;
			xWindow->w_width = evt.size.width = xevent.xconfigure.width;
			xWindow->w_height = evt.size.height = xevent.xconfigure.height;
			vout->on_event(vout->evt_cbk_hdl, &evt);
		      } else {
			evt.type = GF_EVENT_MOVE;
			evt.move.x = xevent.xconfigure.x;
			evt.move.y = xevent.xconfigure.y;
			vout->on_event(vout->evt_cbk_hdl, &evt);
		      }
		    break;
		    /*
		     * Windows need repaint
		     */
		  case Expose:
		    if (xevent.xexpose.count > 0)	break;
		    evt.type = GF_EVENT_REFRESH;
		    vout->on_event (vout->evt_cbk_hdl, &evt);
		    break;
		    
		    /* Have we been requested to quit (or another client message?) */
		  case ClientMessage:
		    if ( (xevent.xclient.format == 32) && (xevent.xclient.data.l[0] == xWindow->WM_DELETE_WINDOW) ) {
		      evt.type = GF_EVENT_QUIT;
		      vout->on_event(vout->evt_cbk_hdl, &evt);
		    }
		    break;
		  	
		      case KeyPress:
		      case KeyRelease:
		    x11_translate_key(XKeycodeToKeysym (xWindow->display, xevent.xkey.keycode, 0), &evt.key);
			evt.type = (xevent.type ==KeyPress) ? GF_EVENT_KEYDOWN : GF_EVENT_KEYUP;
			vout->on_event (vout->evt_cbk_hdl, &evt);
		    
			if (xevent.type ==KeyPress) {
				s32 len;
				len = XLookupString (&xevent.xkey, (char *) keybuf, sizeof(keybuf), NULL, &state);
				if ((len>0) && (len<5)) {
					utf8_to_ucs4 (& evt.character.unicode_char, len, keybuf);
					evt.type = GF_EVENT_TEXTINPUT;
					vout->on_event (vout->evt_cbk_hdl, &evt);
				}
			}
			break;
		    
		  case ButtonPress:
			  if (!xWindow->fullscreen && !xWindow->has_focus) {
				  xWindow->has_focus = 1;
				  XSetInputFocus(xWindow->display, xWindow->wnd, RevertToParent, CurrentTime);
			  }
		  case ButtonRelease:
		    //				last_mouse_move = xevent.xbutton.time;
		    evt.mouse.x = xevent.xbutton.x;
		    evt.mouse.y = xevent.xbutton.y;
			evt.type = (xevent.type == ButtonRelease) ? GF_EVENT_MOUSEUP : GF_EVENT_MOUSEDOWN;
		    
		    switch (xevent.xbutton.button) {
		    case Button1:
		      evt.mouse.button = GF_MOUSE_LEFT;
		      vout->on_event (vout->evt_cbk_hdl, &evt);
		      break;
		    case Button2:
		      evt.mouse.button = GF_MOUSE_MIDDLE;
		      vout->on_event (vout->evt_cbk_hdl, &evt);
		      break;
		    case Button3:
		      evt.mouse.button = GF_MOUSE_RIGHT;
		      vout->on_event (vout->evt_cbk_hdl, &evt);
		      break;
		    case Button4:
		      evt.type = GF_EVENT_MOUSEWHEEL;
		      evt.mouse.wheel_pos = FIX_ONE;
		      vout->on_event(vout->evt_cbk_hdl, &evt);
		      break;
		    case Button5:
		      evt.type = GF_EVENT_MOUSEWHEEL;
		      evt.mouse.wheel_pos = -FIX_ONE;
		      vout->on_event(vout->evt_cbk_hdl, &evt);
		      break;
		    }
		    if (!xWindow->fullscreen && (xevent.type==ButtonPress) ) 
		      XSetInputFocus(xWindow->display, xWindow->wnd, RevertToNone, CurrentTime);
		    break;
		    
		  case MotionNotify:
		    evt.type = GF_EVENT_MOUSEMOVE;
		    evt.mouse.x = xevent.xmotion.x;
		    evt.mouse.y = xevent.xmotion.y;
		    vout->on_event (vout->evt_cbk_hdl, &evt);
		    break;
		  case PropertyNotify:
		    break;
		  case MapNotify:
		    break;
		  case CirculateNotify:
		    break;
		  case UnmapNotify:
		    break;
		  case ReparentNotify:
		    break;
		  case FocusOut:
			if (!xWindow->fullscreen) xWindow->has_focus = 0;
		    break;
		  case FocusIn:
			if (!xWindow->fullscreen) xWindow->has_focus = 1;
		    break;
		    
		  case DestroyNotify:
		      evt.type = GF_EVENT_QUIT;
		      vout->on_event(vout->evt_cbk_hdl, &evt);
		    break;
		    
		  default:
		    break;
		  }
	 }

}
Example #19
0
Bool SDLVid_ProcessMessageQueue(SDLVidCtx *ctx, GF_VideoOutput *dr)
{
	SDL_Event sdl_evt;
	GF_Event gpac_evt;

	while (SDL_PollEvent(&sdl_evt)) {
		switch (sdl_evt.type) {
		case SDL_VIDEORESIZE:
		  	gpac_evt.type = GF_EVENT_SIZE;
			gpac_evt.size.width = sdl_evt.resize.w;
			gpac_evt.size.height = sdl_evt.resize.h;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			break;
		case SDL_QUIT:
			gpac_evt.type = GF_EVENT_QUIT;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			return 0;

		case SDL_VIDEOEXPOSE:
			gpac_evt.type = GF_EVENT_REFRESH;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			break;
#ifdef SDL_TEXTINPUTEVENT_TEXT_SIZE
		/*keyboard*/
		case SDL_TEXTINPUT: /* Since SDL 1.3, text-input is handled in a specific event */
		{
			u32 len = strlen( sdl_evt.text.text);
			u32 ucs4_len;
			assert( len < 5 );
			ucs4_len = utf8_to_ucs4 (&(gpac_evt.character.unicode_char), len, sdl_evt.text.text);
			gpac_evt.type = GF_EVENT_TEXTINPUT;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);	
			break;
		}
#endif /* SDL_TEXTINPUTEVENT_TEXT_SIZE */
		case SDL_KEYDOWN:
		case SDL_KEYUP:
			sdl_translate_key(sdl_evt.key.keysym.sym, &gpac_evt.key);
			gpac_evt.type = (sdl_evt.key.type==SDL_KEYDOWN) ? GF_EVENT_KEYDOWN : GF_EVENT_KEYUP;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			if ((sdl_evt.key.type==SDL_KEYDOWN)
				&& sdl_evt.key.keysym.unicode
#ifdef SDL_TEXTINPUTEVENT_TEXT_SIZE
				&& ((sdl_evt.key.keysym.unicode=='\r') || (sdl_evt.key.keysym.unicode=='\n')  || (sdl_evt.key.keysym.unicode=='\b') || (sdl_evt.key.keysym.unicode=='\t') )
#endif
			) {
				gpac_evt.character.unicode_char = sdl_evt.key.keysym.unicode;
				gpac_evt.type = GF_EVENT_TEXTINPUT;
				dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			}
			break;

		/*mouse*/
		case SDL_MOUSEMOTION:
			ctx->last_mouse_move = SDL_GetTicks();
			gpac_evt.type = GF_EVENT_MOUSEMOVE;
			gpac_evt.mouse.x = sdl_evt.motion.x;
			gpac_evt.mouse.y = sdl_evt.motion.y;
			dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
			break;
		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			ctx->last_mouse_move = SDL_GetTicks();
			gpac_evt.mouse.x = sdl_evt.motion.x;
			gpac_evt.mouse.y = sdl_evt.motion.y;
			gpac_evt.type = (sdl_evt.type==SDL_MOUSEBUTTONUP) ? GF_EVENT_MOUSEUP : GF_EVENT_MOUSEDOWN;
			switch (sdl_evt.button.button) {
			case SDL_BUTTON_LEFT: 
				gpac_evt.mouse.button = GF_MOUSE_LEFT;
				dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
				break;
			case SDL_BUTTON_MIDDLE: 
				gpac_evt.mouse.button = GF_MOUSE_MIDDLE;
				dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
				break;
			case SDL_BUTTON_RIGHT: 
				gpac_evt.mouse.button = GF_MOUSE_RIGHT;
				dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
				break;
#ifdef SDL_BUTTON_WHEELUP
			case SDL_BUTTON_WHEELUP:
			case SDL_BUTTON_WHEELDOWN:
				/*SDL handling is not perfect there, it just says up/down but no info on how much
				the wheel was rotated...*/
				gpac_evt.mouse.wheel_pos = (sdl_evt.button.button==SDL_BUTTON_WHEELUP) ? FIX_ONE : -FIX_ONE;
				gpac_evt.type = GF_EVENT_MOUSEWHEEL;
				dr->on_event(dr->evt_cbk_hdl, &gpac_evt);
				break;
#endif
			}
			break;
		}
	}
	return 1;
}
Example #20
0
static mdn_result_t
amcaceo_encode(const char *from, size_t fromlen, char *to, size_t tolen) {
	ucsbuf_t ucsb;
	amcaceo_encode_ctx ctx;
	size_t len;
	mdn_result_t r;
	int literal_mode = 0;
	int i;

	/*
	 * Convert to UCS-4.
	 */
	ucsbuf_init(&ucsb);
	if ((r = utf8_to_ucs4(from, fromlen, &ucsb)) != mdn_success)
		return (r);

	/*
	 * Verify that all the code points are within 0-0x10FFFF range.
	 */
	for (i = 0; i < ucsb.len; i++) {
		if (ucsb.ucs[i] > 0x10FFFF) {
			ucsbuf_free(&ucsb);
			return (mdn_invalid_encoding);
		}
	}

	init_encode_ctx(&ctx);
	ctx.input = ucsb.ucs;
	ctx.input_len = ucsb.len;
	choose_refpoints(&ctx);
	if ((len = encode_refpoints(&ctx, to, tolen)) == 0)
		goto overflow;

	to += len;
	tolen -= len;

	for (i = 0; i < ctx.input_len; i++) {
		unsigned long v = ctx.input[i];

		if (v == '-') {
			/*
			 * Convert "-" to "--".
			 */
			if (tolen < 2)
				goto overflow;
			to[0] = to[1] = '-';
			to += 2;
			tolen -= 2;
		} else if (is_ldh(v)) {
			/*
			 * LDH characters.
			 */
			if (literal_mode == 0) {
				/*
				 * Go into literal mode.
				 */
				if (tolen < 1)
					goto overflow;
				*to++ = '-';
				tolen--;
				literal_mode = 1;
			}
			if (tolen < 1)
				goto overflow;
			*to++ = v;
			tolen--;
		} else {
			/*
			 * Non-LDH characters.
			 */
			if (literal_mode != 0) {
				/*
				 * Get out of literal mode.
				 */
				if (tolen < 1)
					goto overflow;
				*to++ = '-';
				tolen--;
				literal_mode = 0;
			}
			len = encode_point(ctx.refpoint, v, to, tolen);
			if (len == 0)
				goto overflow;
			to += len;
			tolen -= len;
		}
	}

	/*
	 * Terminate with NUL.
	 */
	if (tolen < 1)
		return (mdn_buffer_overflow);

	to[0] = '\0';

	ucsbuf_free(&ucsb);
	return (mdn_success);

 overflow:
	ucsbuf_free(&ucsb);
	return (mdn_buffer_overflow);
}
Example #21
0
char* opencc_convert_utf8(opencc_t t_opencc, const char* inbuf, size_t length) {
  if (!lib_initialized) {
    lib_initialize();
  }
	size_t actual_length = strlen(inbuf);
  if ((length == (size_t)-1) || (length > actual_length)) {
    length = actual_length;
  }
  ucs4_t* winbuf = utf8_to_ucs4(inbuf, length);
  if (winbuf == (ucs4_t*)-1) {
    /* Can not convert input UTF8 to UCS4 */
    errnum = OPENCC_ERROR_ENCODING;
    return (char*)-1;
  }
  /* Set up UTF8 buffer */
  size_t outbuf_len = length;
  size_t outsize = outbuf_len;
  char* original_outbuf = (char*)malloc(sizeof(char) * (outbuf_len + 1));
  char* outbuf = original_outbuf;
  original_outbuf[0] = '\0';
  /* Set conversion buffer */
  size_t wbufsize = length + 64;
  ucs4_t* woutbuf = (ucs4_t*)malloc(sizeof(ucs4_t) * (wbufsize + 1));
  ucs4_t* pinbuf = winbuf;
  ucs4_t* poutbuf = woutbuf;
  size_t inbuf_left, outbuf_left;
  inbuf_left = ucs4len(winbuf);
  outbuf_left = wbufsize;
  while (inbuf_left > 0) {
    size_t retval = opencc_convert(t_opencc,
                                   &pinbuf,
                                   &inbuf_left,
                                   &poutbuf,
                                   &outbuf_left);
    if (retval == (size_t)-1) {
      free(outbuf);
      free(winbuf);
      free(woutbuf);
      return (char*)-1;
    }
    *poutbuf = L'\0';
    char* ubuff = ucs4_to_utf8(woutbuf, (size_t)-1);
    if (ubuff == (char*)-1) {
      free(outbuf);
      free(winbuf);
      free(woutbuf);
      errnum = OPENCC_ERROR_ENCODING;
      return (char*)-1;
    }
    size_t ubuff_len = strlen(ubuff);
    while (ubuff_len > outsize) {
      size_t outbuf_offset = outbuf - original_outbuf;
      outsize += outbuf_len;
      outbuf_len += outbuf_len;
      original_outbuf =
        (char*)realloc(original_outbuf, sizeof(char) * outbuf_len);
      outbuf = original_outbuf + outbuf_offset;
    }
    strncpy(outbuf, ubuff, ubuff_len);
    free(ubuff);
    outbuf += ubuff_len;
    *outbuf = '\0';
    outbuf_left = wbufsize;
    poutbuf = woutbuf;
  }
  free(winbuf);
  free(woutbuf);
  original_outbuf = (char*)realloc(original_outbuf,
                                   sizeof(char) * (strlen(original_outbuf) + 1));
  return original_outbuf;
}
Example #22
0
void nsbeos_window_keypress_event(BView *view, gui_window *g, BMessage *event)
{
	const char *bytes;
	char buff[6];
	int numbytes = 0;
	uint32 mods;
	uint32 key;
	uint32 raw_char;
	uint32_t nskey;
	int i;

	if (event->FindInt32("modifiers", (int32 *)&mods) < B_OK)
		mods = modifiers();
	if (event->FindInt32("key", (int32 *)&key) < B_OK)
		key = 0;
	if (event->FindInt32("raw_char", (int32 *)&raw_char) < B_OK)
		raw_char = 0;
	/* check for byte[] first, because C-space gives bytes="" (and byte[0] = '\0') */
	for (i = 0; i < 5; i++) {
		buff[i] = '\0';
		if (event->FindInt8("byte", i, (int8 *)&buff[i]) < B_OK)
			break;
	}

	if (i) {
		bytes = buff;
		numbytes = i;
	} else if (event->FindString("bytes", &bytes) < B_OK)
		bytes = "";

	if (!numbytes)
		numbytes = strlen(bytes);

	LOG(("mods 0x%08lx key %ld raw %ld byte[0] %d", mods, key, raw_char, buff[0]));

	char byte;
	if (numbytes == 1) {
		byte = bytes[0];
		if (mods & B_CONTROL_KEY)
			byte = (char)raw_char;
		if (byte >= '!' && byte <= '~')
			nskey = (uint32_t)byte;
		else {
			switch (byte) {
				case B_BACKSPACE:	nskey = KEY_DELETE_LEFT; break;
				case B_TAB:	nskey = KEY_TAB; break;
				/*case XK_Linefeed:	return QKlinefeed;*/
				case B_ENTER:	nskey = (uint32_t)10; break;
				case B_ESCAPE:	nskey = (uint32_t)'\033'; break;
				case B_SPACE:	nskey = (uint32_t)' '; break;
				case B_DELETE:	nskey = KEY_DELETE_RIGHT; break;
				/*
				case B_INSERT:	nskey = KEYSYM("insert"); break;
				*/
				case B_HOME:	nskey = KEY_LINE_START; break; // XXX ?
				case B_END:	nskey = KEY_LINE_END; break; // XXX ?
				case B_PAGE_UP:	nskey = KEY_PAGE_UP; break;
				case B_PAGE_DOWN:	nskey = KEY_PAGE_DOWN; break;
				case B_LEFT_ARROW:	nskey = KEY_LEFT; break;
				case B_RIGHT_ARROW:	nskey = KEY_RIGHT; break;
				case B_UP_ARROW:	nskey = KEY_UP; break;
				case B_DOWN_ARROW:	nskey = KEY_DOWN; break;
				/*
				case B_FUNCTION_KEY:
					switch (scancode) {
						case B_F1_KEY: nskey = KEYSYM("f1"); break;
						case B_F2_KEY: nskey = KEYSYM("f2"); break;
						case B_F3_KEY: nskey = KEYSYM("f3"); break;
						case B_F4_KEY: nskey = KEYSYM("f4"); break;
						case B_F5_KEY: nskey = KEYSYM("f5"); break;
						case B_F6_KEY: nskey = KEYSYM("f6"); break;
						case B_F7_KEY: nskey = KEYSYM("f7"); break;
						case B_F8_KEY: nskey = KEYSYM("f8"); break;
						case B_F9_KEY: nskey = KEYSYM("f9"); break;
						case B_F10_KEY: nskey = KEYSYM("f10"); break;
						case B_F11_KEY: nskey = KEYSYM("f11"); break;
						case B_F12_KEY: nskey = KEYSYM("f12"); break;
						case B_PRINT_KEY: nskey = KEYSYM("print"); break;
						case B_SCROLL_KEY: nskey = KEYSYM("scroll-lock"); break;
						case B_PAUSE_KEY: nskey = KEYSYM("pause"); break;
					}
				*/
				case 0:
					nskey = (uint32_t)0;
				default:
					nskey = (uint32_t)raw_char;
					/*if (simple_p)
						nskey = (uint32_t)0;*/
					break;
			}
		}
	} else {
		nskey = utf8_to_ucs4(bytes, numbytes);
	}

	bool done = browser_window_key_press(g->bw, nskey);
	LOG(("nskey %d %d", nskey, done));
	//if (browser_window_key_press(g->bw, nskey))
		return;
	
}
Example #23
0
static int
iconv_ucs_conv(void *d2p, const char **inbuf,
	size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
	int convchar, int casetype)
{
	struct iconv_ucs *dp = (struct iconv_ucs*)d2p;
	int ret = 0, i;
	size_t in, on, ir, or, inlen, outlen, ucslen;
	const char *src, *p;
	char *dst;
	u_char ucs[4], *q;
	uint32_t code;

	if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
		return 0;
	ir = in = *inbytesleft;
	or = on = *outbytesleft;
	src = *inbuf;
	dst = *outbuf;

	while (ir > 0 && or > 0) {

		/*
		 * The first half of conversion.
		 * (convert any code into ENCODING_UNICODE)
		 */
		code = 0;
		p = src;
		if (dp->convtype & KICONV_UCS_FROM_UTF8) {
			/* convert UTF-8 to ENCODING_UNICODE */
			inlen = 0;
			code = utf8_to_ucs4(p, &inlen, ir);
			if (code == 0) {
				ret = -1;
				break;
			}

			if (casetype == KICONV_FROM_LOWER && dp->ctype) {
				code = towlower(code, dp->ctype);
			} else if (casetype == KICONV_FROM_UPPER && dp->ctype) {
				code = towupper(code, dp->ctype);
			}

			if ((code >= 0xd800 && code < 0xe000) || code >= 0x110000 ) {
				/* reserved for utf-16 surrogate pair */
				/* invalid unicode */
				ret = -1;
				break;
			}

			if (inlen == 4) {
				if (dp->convtype & KICONV_UCS_UCS4) {
					ucslen = 4;
					code = encode_surrogate(code);
				} else {
					/* can't handle with ucs-2 */
					ret = -1;
					break;
				}
			} else {
				ucslen = 2;
			}

			/* save UCS-4 into ucs[] */
			for (q = ucs, i = ucslen - 1 ; i >= 0 ; i--)
				*q++ = (code >> (i << 3)) & 0xff;

		} else if (dp->convtype & KICONV_UCS_COMBINE && dp->f_ctp) {
Example #24
0
static void console_fb_write_byte(struct console_fb_data_t * dat, unsigned char c)
{
	u32_t cp;
	char * rest;
	int t;

	switch(dat->state)
	{
	case ESC_STATE_NORMAL:
		switch(c)
		{
		case '\e':	/* ESC state */
			dat->state = ESC_STATE_ESC;
			break;

		default:
			dat->utf8[dat->usize++] = c;
			if(utf8_to_ucs4(&cp, 1, (const char *)dat->utf8, dat->usize, (const char **)&rest) > 0)
			{
				dat->usize -= rest - dat->utf8;
				memmove(dat->utf8, rest, dat->usize);
				console_fb_putcode(dat, cp);
			}
			break;
		}
		break;

	case ESC_STATE_ESC:
		switch(c)
		{
		case 'c':	/* Reset */
			console_fb_show_cursor(dat, 1);
			console_fb_set_color_bright(dat, 0);
			console_fb_set_color(dat, TCOLOR_WHITE, TCOLOR_BLACK);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'D':	/* Scroll display down one line */
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'M':	/* Scroll display up one line */
			dat->state = ESC_STATE_NORMAL;
			break;

		case '7':	/* Save cursor position and attrs */
			console_fb_save_cursor(dat);
			console_fb_save_color(dat);
			dat->state = ESC_STATE_NORMAL;
			break;

		case '8':	/* Restore cursor position and attrs */
			console_fb_restore_cursor(dat);
			console_fb_restore_color(dat);
			dat->state = ESC_STATE_NORMAL;
			break;

		case '[':	/* CSI codes */
			dat->csize = 0;
			dat->abuf[0] = 0;
			dat->asize = 0;
			dat->state = ESC_STATE_CSI;
			break;

		default:
			dat->state = ESC_STATE_NORMAL;
			break;
		}
		break;

	case ESC_STATE_CSI:
		dat->cbuf[dat->csize++] = c;

		switch(dat->cbuf[dat->csize - 1])
		{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			dat->abuf[dat->asize] *= 10;
			dat->abuf[dat->asize] += (dat->cbuf[dat->csize - 1] - '0');
			break;

		case ';':
			dat->asize++;
			dat->abuf[dat->asize] = 0;
			break;

		case 'A':	/* Move the cursor up */
			t = dat->abuf[0];
			t = (t) ? t : 1;
			console_fb_cursor_gotoxy(dat, dat->x, dat->y - t);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'B':	/* Move the cursor down */
			t = dat->abuf[0];
			t = (t) ? t : 1;
			console_fb_cursor_gotoxy(dat, dat->x, dat->y + t);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'C':	/* Move the cursor right */
			t = dat->abuf[0];
			t = (t) ? t : 1;
			console_fb_cursor_gotoxy(dat, dat->x + t, dat->y);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'D':	/* Move the cursor left */
			t = dat->abuf[0];
			t = (t) ? t : 1;
			console_fb_cursor_gotoxy(dat, dat->x - t, dat->y);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 's':	/* Save cursor position */
			console_fb_save_cursor(dat);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'u':	/* Restore cursor position */
			console_fb_restore_cursor(dat);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'J':	/* Clear the screen */
			console_fb_clear_screen(dat);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'H':	/* Cursor home */
		case 'f':	/* Force cursor position */
			if(dat->asize == 0)
				console_fb_cursor_gotoxy(dat, 0, dat->y);
			else
				console_fb_cursor_gotoxy(dat, dat->abuf[1], dat->abuf[0]);
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'c':		/* Request terminal Type */
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'n':
			switch(dat->abuf[0])
			{
			case 5:		/* Request terminal status */
				break;
			case 6:		/* Request cursor position */
				break;
			};
			dat->state = ESC_STATE_NORMAL;
			break;

		case 'm':		/* Set Display Attributes */
			for(t = 0; t <= dat->asize; t++)
			{
				switch(dat->abuf[t])
				{
				case 0:		/* Reset all attrs */
					console_fb_set_color_bright(dat, 0);
					console_fb_set_color(dat, TCOLOR_WHITE, TCOLOR_BLACK);
					break;
				case 1:		/* Bright */
					console_fb_set_color_bright(dat, 1);
					console_fb_set_color(dat, dat->f, dat->b);
					break;
				case 2:		/* Dim */
					break;
				case 4:		/* Underscore */
					break;
				case 5:		/* Blink */
					break;
				case 7:		/* Reverse */
					console_fb_set_color(dat, dat->b, dat->f);
					break;
				case 8:		/* Hidden */
					break;

				case 30:	/* Set foreground color */
				case 31:
				case 32:
				case 33:
				case 34:
				case 35:
				case 36:
				case 37:
					console_fb_set_color(dat, dat->abuf[t] - 30, dat->b);
					break;

				case 40:	/* Set background color */
				case 41:
				case 42:
				case 43:
				case 44:
				case 45:
				case 46:
				case 47:
					console_fb_set_color(dat, dat->f, dat->abuf[t] - 40);
					break;

				default:
					break;
				}
			}
			dat->state = ESC_STATE_NORMAL;
			break;

		default:
			dat->state = ESC_STATE_NORMAL;
			break;
		}
		break;

	default:
		dat->state = ESC_STATE_NORMAL;
		break;
	}
}