Exemplo n.º 1
0
WideString
utf8_read_wstring (std::istream &is, ucs4_t delim, bool rm_delim)
{
    WideString str;
    ucs4_t wc;
    while ((wc = utf8_read_wchar (is)) > 0) {
        if (wc != delim)
            str.push_back (wc);
        else {
            if (!rm_delim)
                str.push_back (wc);
            break;
        }
    }
    return str;
}
Exemplo n.º 2
0
void
ArrayInstance::create_lookup_table_labels(int page_size) 
{
    WideString trail;
    trail.push_back(0x20);
    m_lookup_table_labels.clear();
    for (int i = 0; i < page_size; i++)
    {
        trail [0] = (ucs4_t) int_to_ascii ((i % 10) + 1);
        if ((i % 10) >= 9)
            trail [0] = (ucs4_t) int_to_ascii (0);
        m_lookup_table_labels.push_back(trail);
    }
}
Exemplo n.º 3
0
bool
ThaiInstance::process_key_event (const KeyEvent& key)
{
    if (key.is_key_release()
        || key.code == 0
        || __is_context_intact_key (key.code))
    {
        return false;
    }

    if (key.mask & (SCIM_KEY_AllMasks
                    & ~(SCIM_KEY_ShiftMask | SCIM_KEY_CapsLockMask)) ||
        __is_context_lost_key (key.code))
    {
        _forget_previous_chars ();
        return false;
    }

    KeyEvent  thai_key = m_keymap.map_key (key);
    ucs4_t    thai_uni = thai_key.get_unicode_code ();

    if (!th_wcistis (thai_uni))
        return false;

    thchar_t thai_tis = th_uni2tis (thai_uni);

    thcell_t    context_cell = _get_previous_cell ();
    thinpconv_t conv;
    if (th_validate (context_cell, thai_tis, &conv))
    {
        if (conv.offset < 0)
            if (!delete_surrounding_text (conv.offset, -conv.offset))
                return false;
        _forget_previous_chars ();
        _remember_previous_char (thai_tis);

        WideString str;
        for (int i = 0; conv.conv [i]; i++)
            str.push_back (th_tis2uni (conv.conv [i]));
        commit_string (str);
    }
    else
    {
        beep ();
    }

    return true;
}
Exemplo n.º 4
0
WideString
utf8_mbstowcs (const String & str)
{
    WideString wstr;
    ucs4_t wc;
    unsigned int sn = 0;
    int un = 0;

    const unsigned char *s = (const unsigned char *) str.c_str ();

    while (sn < str.length () && *s != 0 &&
            (un=utf8_mbtowc (&wc, s, str.length () - sn)) > 0) {
        wstr.push_back (wc);
        s += un;
        sn += un;
    }
    return wstr;
}
Exemplo n.º 5
0
void
HangulInstance::flush()
{
    SCIM_DEBUG_IMENGINE(2) << "flush.\n";

    hide_preedit_string();

    WideString wstr = m_preedit;
    const ucschar *str = hangul_ic_flush(m_hic);
    while (*str != 0)
	wstr.push_back (*str++);

    if (wstr.length())
        commit_string(wstr);

    delete_candidates ();
    m_preedit.clear();
}
Exemplo n.º 6
0
	//--------------------------------
	bool Utils::createDirectoryRecursive( const WideString &pathString )
	{
		if (pathString.length() == 0)
			return false;

		WideString path = pathString;

		if (path[path.length()-1] != '/' && path[path.length()-1] != '\\')
			path.push_back('\\');

		std::list<WideString> paths;
		size_t offset = WideString::npos;
		while ((offset != 0) && (offset = pathString.find_last_of(L"/\\", offset)) != WideString::npos)
		{
			paths.push_front(pathString.substr(0, offset + 1));
			if (offset != 0) --offset;
		}

		bool pathExists = true;
		const wchar_t* currentPath = _wgetcwd(0, 0);

		for (std::list<WideString>::const_iterator iPath = paths.begin(); iPath != paths.end(); ++iPath)
		{
			// if path exists
			if (_wchdir((*iPath).c_str()) == 0)
				continue;

			// path does not exist, try to create it
			_wmkdir((*iPath).c_str());
			
			if (_wchdir((*iPath).c_str()) != 0)
			{
				pathExists = false;
				break;
			}
		}

		// Restore current path
		_wchdir(currentPath);
		return pathExists;
	}
Exemplo n.º 7
0
WideString
utf8_mbstowcs (const char *str, int len)
{
    WideString wstr;

    if (str) {
        ucs4_t wc;
        unsigned int sn = 0;
        int un = 0;

        if (len < 0) len = strlen (str);

        while (sn < len && *str != 0 && (un=utf8_mbtowc (&wc, (const unsigned char *)str, len - sn)) > 0) {
            wstr.push_back (wc);
            str += un;
            sn += un;
    
        }
    }
    return wstr;
}
Exemplo n.º 8
0
void self_learn (GenericTableLibrary &lib, const char *file)
{
    std::vector <ucs4_t> buffer;
    std::ifstream ifs(file);

    if (!ifs) return;

    uint32 byte = 0;
    uint32 kb = 0;

    WideString str;

    std::vector <uint32> phrases;
    std::vector <uint32>::const_iterator pit;

    uint32 maxlen = lib.get_max_phrase_length ();

    ucs4_t wc;
    bool skip;
    char wheel [] = {'-', '\\', '|', '/', 0};
    int wheel_state;

    buffer.reserve (1048576*32);

    skip = false;

    wheel_state = 0;

    while (!ifs.eof()) {
        buffer.clear ();
        // Read a line
        while (!ifs.eof ()) {
            if ((wc = utf8_read_wchar (ifs)) == 0) break;
            if (wc == L'\n') break;
            else if (iswpunct (wc) || iswspace (wc) || iswdigit (wc) ) {
                if (!skip) {
                    buffer.push_back (0);
                    skip = true;
                }
            } else {
                buffer.push_back (wc);
                skip = false;
            }
        }

        buffer.push_back (0);
        for (int i=0; i<buffer.size (); i++) {
            str = WideString ();
            for (int j=0; j<maxlen; j++) {
                if (buffer [j+i] == 0)
                    break;
                str.push_back (buffer [j+i]);

                phrases.clear ();
                if (lib.find_phrase (phrases, str)) {
                    for (pit = phrases.begin (); pit != phrases.end (); ++ pit) 
                        lib.set_phrase_frequency (*pit, lib.get_phrase_frequency (*pit) + 1);
                }
            }
            byte ++;
            if (byte == 1024) {
                byte = 0;
                kb ++;
                std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
                std::cout << kb << "\tK ("
                     << wheel [wheel_state/2] << ") " << std::flush;
                wheel_state = (wheel_state+1) % 8;
            }
        }
    }

    std::cout << std::endl;
}
Exemplo n.º 9
0
bool
ComposeKeyInstance::process_key_event (const KeyEvent& key)
{
    if (key.is_key_release ()) return false;

    // Ignore modifier key presses.
    if (std::binary_search (__scim_compose_ignores, __scim_compose_ignores + SCIM_NUM_COMPOSE_IGNORES, (uint16) key.code))
        return false;

    // Ignore the key if ctrl or alt is down.
    if (key.is_control_down () || key.is_alt_down ())
        return false;

    int n_compose = 0;

    while (m_compose_buffer [n_compose] != 0 && n_compose < SCIM_MAX_COMPOSE_LEN)
        ++ n_compose;

    // The buffer is full, then reset the buffer first.
    if (n_compose == SCIM_MAX_COMPOSE_LEN) {
        reset ();
        n_compose = 0;
    }

    m_compose_buffer [n_compose] = (uint32) key.code;

    const ComposeSequence *it = std::lower_bound (__scim_compose_seqs,
                                __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS,
                                m_compose_buffer,
                                ComposeSequenceLessByKeys ());

    // Not result found, reset the buffer and return false.
    if (it == __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS) {
        reset ();
        return false;
    }

    // Check if the compose sequence is match.
    for (n_compose = 0; n_compose < SCIM_MAX_COMPOSE_LEN; ++ n_compose) {
        if (m_compose_buffer [n_compose] == 0)
            break;

        // Not match, reset the buffer and return.
        // If it's the first key press, then return false to forward it.
        // Otherwise return true to ignore it.
        if (m_compose_buffer [n_compose] != it->keys [n_compose]) {
            reset ();
            return n_compose != 0;
        }
    }

    // Match exactly, commit the result.
    if (n_compose == SCIM_MAX_COMPOSE_LEN || it->keys [n_compose] == 0) {
        WideString wstr;
        wstr.push_back (it->unicode);
        commit_string (wstr);
        reset ();
    }

    return true;
}
Exemplo n.º 10
0
int
ArrayInstance::create_lookup_table (int mapSelect)
{
    String mbs_code;
    ucs4_t ucs_code;
    WideString trail;
    WideString wstr;

    m_lookup_table.clear ();
    m_lookup_table_labels.clear ();

    trail.push_back(0x20);

    bool isHaveWildcard = false;
    isHaveWildcard = hasWildcard(m_preedit_string);

    vector<string> candidatesVec;
    int rcount = 0;

    if (isHaveWildcard)
    {
        rcount = m_factory->arrayCins[mapSelect]->getWordsVectorWithWildcard(
                utf8_wcstombs(m_preedit_string), candidatesVec);
    }
    else
    {
        rcount = m_factory->arrayCins[mapSelect]->getWordsVector(
                utf8_wcstombs(m_preedit_string), candidatesVec);
    }

    if (rcount)
    {
        for (int i = 0; i < candidatesVec.size(); i++)
        {
            trail [0] = (ucs4_t) int_to_ascii ((i % 10) + 1);
            if ((i % 10) >= 9)
                trail [0] = (ucs4_t) int_to_ascii (0);

            m_lookup_table.append_candidate(utf8_mbstowcs(candidatesVec[i]));
            m_lookup_table_labels.push_back(trail);
        }
    }
    else
    {
        trail [0] = (ucs4_t) int_to_ascii (0);
        m_lookup_table.append_candidate(utf8_mbstowcs(SCIM_ARRAY_EMPTY_CHAR));
        m_lookup_table_labels.push_back(trail);
    }

    if (m_lookup_table_labels.size() <= 10)
        m_lookup_table.set_page_size (m_lookup_table_labels.size());
    else
        m_lookup_table.set_page_size(10);

    if (mapSelect == _ScimArray::Array_Phrases)
    {
        m_lookup_table.fix_page_size(false);
    }

    create_lookup_table_labels(m_lookup_table.get_current_page_size());
    m_lookup_table.set_candidate_labels (m_lookup_table_labels);

    return m_lookup_table_labels.size ();
}
Exemplo n.º 11
0
bool
ArrayInstance::process_key_event (const KeyEvent& rawkey)
{
    KeyEvent key = rawkey.map_to_layout(SCIM_KEYBOARD_Default);

    if (key.is_key_release ()) return false;

    // English/Chinese change mode key
    if ( match_key_event(m_factory->m_ench_key, key))
    {
        trigger_property(SCIM_PROP_STATUS);
        return true;
    }

    // Full/Half width chang mode key
    if ( match_key_event(m_factory->m_full_half_key, key))
    {
        trigger_property(SCIM_PROP_LETTER);
        return true;
    }

    // if in forward mode
    if (m_forward)
    {
        if ( key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde)
        {
            if (m_full_width_letter)
            {
                char widthc = key.get_ascii_code();
                WideString outws;
                outws.push_back(scim_wchar_to_full_width(widthc));
                commit_string(outws);
                return true;
            }
            else
                return false;
        }
        else 
            return false;
    }

    //reset key
    if (key.code == SCIM_KEY_Escape && key.mask == 0) {
        // Do not catch ESC while no key input for VI users
        if (m_preedit_string.size() == 0)
            return false;
        reset ();
        return true;
    }

    //delete key
    if (key.code == SCIM_KEY_BackSpace && key.mask == 0 &&
        m_preedit_string.size () != 0) 
    {
        m_preedit_string.erase (m_preedit_string.length () - 1, 1);
        pre_update_preedit_string (m_preedit_string);
        process_preedit_string ();
        commit_press_count = 0;     // reset to 0 to avoid output error
        return true;
    }

    // valid keys
    if (((key.code >= SCIM_KEY_a && key.code <= SCIM_KEY_z) ||
         (key.code == SCIM_KEY_comma) || (key.code == SCIM_KEY_period) ||
         (key.code == SCIM_KEY_semicolon) || (key.code == SCIM_KEY_slash))
            &&
        (key.mask == 0))
    {
        if (m_preedit_string.length () >=  m_max_preedit_len)
            return true;
 
        if (commit_press_count == 1)
        {
            WideString str = m_lookup_table.get_candidate_in_current_page (0);
            if (str.length() && str.compare(utf8_mbstowcs(SCIM_ARRAY_EMPTY_CHAR)) != 0 ) {
                send_commit_string(m_preedit_string,
                        m_lookup_table.get_candidate_in_current_page(0));
            }
            else
                reset();
        }

        if (m_preedit_string.length () == 0)
        {
            hide_aux_string ();
            show_preedit_string ();
        }

        ucs4_t ascii = (ucs4_t) tolower (key.get_ascii_code ());
        m_preedit_string.push_back (ascii);
        pre_update_preedit_string (m_preedit_string);
        process_preedit_string ();

        return true;
    }

    // apostrophe key for end of phrases
    if (m_use_phrases && key.code == SCIM_KEY_apostrophe 
            && m_preedit_string.length())
    {
        phrase_key_press();
        return true;
    }
    
    // wildcard keys
    if (key.code == SCIM_KEY_question || key.code == SCIM_KEY_asterisk)
    {
        hide_lookup_table();
        ucs4_t ascii = (ucs4_t) tolower (key.get_ascii_code ());
        m_preedit_string.push_back (ascii);
        pre_update_preedit_string (m_preedit_string);
        process_preedit_string ();
        return true;
    }

    // selection keys
    if (key.code >= SCIM_KEY_0 && key.code <= SCIM_KEY_9) 
    {
        if (!m_preedit_string.length())
            return false;

        WideString inkey = m_preedit_string;

        // key "w" to enable the symbol input
        if (m_preedit_string.compare(utf8_mbstowcs("w")) == 0)
        {
            ucs4_t ascii = (ucs4_t) tolower (key.get_ascii_code ());
            m_preedit_string.push_back (ascii);
            pre_update_preedit_string (m_preedit_string);
            process_symbol_preedit_string ();

            return true;
        }

        int selectnum = (int)(key.get_ascii_code() - '0' - 1);
        if (selectnum < 0)
            selectnum = 9;


        WideString cmtstr = m_lookup_table.get_candidate_in_current_page(selectnum);

        if (cmtstr.length() && cmtstr.compare(utf8_mbstowcs(SCIM_ARRAY_EMPTY_CHAR)) != 0)
        {
            send_commit_string(inkey, cmtstr);
            return true;
        }
        else
        {
            return true;
        }

    }

    // commit key
    if (key.code == SCIM_KEY_space && key.mask == 0 && m_preedit_string.length ())
    {
        space_key_press();
        return true;
    }

    //page up key.
    if (key.code == SCIM_KEY_Page_Up && key.mask == 0)
        lookup_table_page_up ();

    //page down key.
    if (key.code == SCIM_KEY_Page_Down && key.mask == 0)
        lookup_table_page_down ();

    //other keys is not allowed when preediting
    if (m_preedit_string.length ())
        return true;

    //other keys wiil be send out if the Full width mode on
    if ( (key.code >= SCIM_KEY_space && key.code <= SCIM_KEY_asciitilde) && (key.mask == 0 || key.is_shift_down()))
    {
        WideString outws;
        char widthc = key.get_ascii_code();

        if (widthc >= 'A' && widthc <= 'Z')
            if (!key.is_caps_lock_down())
                widthc = widthc - 'A' + 'a';

        if (m_full_width_letter)
            outws.push_back(scim_wchar_to_full_width(widthc));
        else
            outws.push_back(widthc);

        commit_string(outws);
        return true;
    }

    return false;
}