Exemplo n.º 1
0
void QinPinyin::update_preedit_string(const IPreeditString& preedit)
{
  const int len = preedit.size();
  if (len > 0) {
    preeditStr = TWCHAR2str(preedit.string(), preedit.size());
  } else {
    preeditStr.clear();
  }
}
Exemplo n.º 2
0
void
CIMIClassicView::getPreeditString(IPreeditString& ps)
{
    ps.clear();

    wstring &wstr = ps.getString ();
    IPreeditString::CCharTypeVec& charTypes = ps.getCharTypeVec ();

    m_pIC->getBestSentence (wstr, 0, m_candiFrIdx);

    int caret = wstr.size ();
    charTypes.reserve (caret);
    for (int i = 0; i < caret; ++i)
        charTypes.push_back (IPreeditString::HANZI_CHAR | IPreeditString::USER_CHOICE);

    const wstring& pystr = m_pPySegmentor->getInputBuffer ();
    IPySegmentor::TSegmentVec& segments = m_pPySegmentor->getSegments ();
    IPySegmentor::TSegmentVec::const_iterator it  = segments.begin ();
    IPySegmentor::TSegmentVec::const_iterator ite = segments.end ();
    
    CLattice& lattice = m_pIC->getLattice ();
    unsigned i = 0, l = 0;
    for (; it != ite; ++it, i+=l) {
        l = it->m_len;

        if (i+l <= m_candiFrIdx)
            continue;

        if (i < m_cursorFrIdx && m_cursorFrIdx <= i+l)
            caret = wstr.size() +  (m_cursorFrIdx-i);

        CLatticeFrame &fr = lattice [i+l];
        int ct = IPreeditString::PINYIN_CHAR;
        if (fr.isSyllableSepFrame()) {
            ct = IPreeditString::BOUNDARY | IPreeditString::USER_CHOICE;
        } else if (fr.m_type == CLatticeFrame::ASCII) {
            ct = IPreeditString::ASCII_CHAR;
        } else if (fr.m_type == CLatticeFrame::SYMBOL) {
            ct = IPreeditString::SYMBOL_CHAR;
        }

        wstr.insert (wstr.end(), pystr.begin()+i, pystr.begin()+i+l);
        for (int c=0; c<l; ++c)
            charTypes.push_back (ct);

        if (fr.isSyllableFrame() && !fr.isSyllableSepFrame()) {
            if ( it != ite -1 && !lattice[i+l+1].isSyllableSepFrame ()) { 
                wstr.push_back (' ');
                charTypes.push_back (IPreeditString::BOUNDARY);
            }
        }
    }
    
    ps.setCaret (caret);
}
Exemplo n.º 3
0
void decorate_preedit_string_using_caret_pos(IBusText *text, const IPreeditString& preedit, int caret)
{
    if (caret < preedit.size()) {
        // add underline, otherwise gtk app won't have the same color scheme with that of x11 apps
        ibus_text_append_attribute(text, IBUS_ATTR_TYPE_UNDERLINE, IBUS_ATTR_UNDERLINE_SINGLE,
                                   caret, preedit.size());
        ibus_text_append_attribute(text, IBUS_ATTR_TYPE_FOREGROUND, WHITE,
                                   caret, preedit.size());
        ibus_text_append_attribute(text, IBUS_ATTR_TYPE_BACKGROUND, GRAY_BLUE,
                                   caret, preedit.size());
    }
}
Exemplo n.º 4
0
void
SunPinyinEngine::update_preedit_string(const IPreeditString& preedit)
{
    const int len = preedit.size();
    if (len > 0) {
        IBusText *text = ibus_text_new_from_ucs4((const gunichar*) preedit.string());
        
        
        const int caret = preedit.caret();
        if (caret < len) {
            decorate_preedit_string_using_caret_pos(text, preedit, caret);
        } else {
            decorate_preedit_string_using_char_type(text, preedit);
        }
        
        ibus_engine_update_preedit_text(m_engine, text, caret, TRUE);
    } else {
        ibus_engine_hide_preedit_text(m_engine);
    }
}
Exemplo n.º 5
0
int decorate_preedit_char(IBusText *text, const IPreeditString& preedit,
                          int begin, int end, unsigned type,
                          unsigned long fg_color)
{
    int i = begin;
    while (i < end && (preedit.charTypeAt(i) & type) == type)
        ++i;
    end = i;
    int len = end - begin;
    if (len > 0) {
        ibus_text_append_attribute(text, IBUS_ATTR_TYPE_FOREGROUND,
                                   fg_color, begin, end);
    }
    return len;
}
Exemplo n.º 6
0
void decorate_preedit_string_using_char_type(IBusText *text, const IPreeditString& preedit)
{
    for (int i = 0, size = preedit.charTypeSize(); i < size; ) {
        int len = 0;
        if ((len = decorate_preedit_char(text, preedit, i, size, preedit.PINYIN_CHAR, 
                                         GRAY_BLUE)) > 0) {
            i += len;
        } else if ((len = decorate_preedit_char(text, preedit, i, size,
                                                preedit.BOUNDARY,
                                                GRAY_BLUE)) > 0) {
            i += len;
        } else {
            ++i;
        }
    }
}
Exemplo n.º 7
0
void
CIMIClassicView::getPreeditString(IPreeditString& ps)
{
    ps.clear();

    wstring &wstr = ps.getString ();
    IPreeditString::CCharTypeVec& charTypes = ps.getCharTypeVec ();

    m_pIC->getBestSentence (wstr, 0, m_candiFrIdx);

    int caret = wstr.size ();
    charTypes.reserve (caret);
    for (int i = 0; i < caret; ++i)
        charTypes.push_back (IPreeditString::HANZI_CHAR | IPreeditString::USER_CHOICE);

    const wstring& pystr = m_pPySegmentor->getInputBuffer ();
    std::vector<unsigned>& seg_path = m_pIC->getBestSegPath();

    if (pystr.empty())
        return;

    /* FIXME:
     * CIMIContext would fail to backTrace the bestPathes when there are no latticeStates
     * on frame e.g., 'yiden' in Quanpin mode, need a better solution later!
     */
    if (seg_path.empty()) { 
        IPySegmentor::TSegmentVec& segments = m_pPySegmentor->getSegments ();
        IPySegmentor::TSegmentVec::const_iterator it  = segments.begin ();
        IPySegmentor::TSegmentVec::const_iterator ite = segments.end ();

        CLattice& lattice = m_pIC->getLattice ();
        unsigned i = 0, l = 0;
        for (; it != ite; ++it, i+=l) {
            l = it->m_len;

            if (i+l <= m_candiFrIdx)
                continue;

            if (i < m_cursorFrIdx && m_cursorFrIdx <= i+l)
                caret = wstr.size() +  (m_cursorFrIdx-i);

            CLatticeFrame &fr = lattice [i+l];
            int ct = IPreeditString::PINYIN_CHAR;
            if (fr.isSyllableSepFrame()) {
                ct = IPreeditString::BOUNDARY | IPreeditString::USER_CHOICE;
            } else if (fr.m_type == CLatticeFrame::ASCII) {
                ct = IPreeditString::ASCII_CHAR;
            } else if (fr.m_type == CLatticeFrame::SYMBOL) {
                ct = IPreeditString::SYMBOL_CHAR;
            }

            wstr.insert (wstr.end(), pystr.begin()+i, pystr.begin()+i+l);
            for (int c=0; c<l; ++c)
                charTypes.push_back (ct);

            if (fr.isSyllableFrame() && !fr.isSyllableSepFrame()) {
                if ( it != ite -1 && !lattice[i+l+1].isSyllableSepFrame ()) {
                    wstr.push_back (' ');
                    charTypes.push_back (IPreeditString::BOUNDARY);
                }
            }
        }
    } else {
        std::vector<unsigned>::iterator it  = seg_path.begin() + 1;
        std::vector<unsigned>::iterator ite = seg_path.end();

        CLattice& lattice = m_pIC->getLattice ();
        unsigned i = 0, l = 0;
        for (; it != ite; i=*(it++)) {
            l = *it - i;

            if (*it <= m_candiFrIdx)
                continue;

            if (i < m_cursorFrIdx && m_cursorFrIdx <= i+l)
                caret = wstr.size() +  (m_cursorFrIdx-i);

            CLatticeFrame &fr = lattice [i+l];
            int ct = IPreeditString::PINYIN_CHAR;
            if (fr.isSyllableSepFrame()) {
                ct = IPreeditString::BOUNDARY | IPreeditString::USER_CHOICE;
            } else if (fr.m_type == CLatticeFrame::ASCII) {
                ct = IPreeditString::ASCII_CHAR;
            } else if (fr.m_type == CLatticeFrame::SYMBOL) {
                ct = IPreeditString::SYMBOL_CHAR;
            }

            wstr.insert (wstr.end(), pystr.begin()+i, pystr.begin()+i+l);
            for (int c=0; c<l; ++c)
                charTypes.push_back (ct);

            if (fr.isSyllableFrame() && !fr.isSyllableSepFrame()) {
                if ( it != ite -1 && !lattice[i+l+1].isSyllableSepFrame ()) { 
                    wstr.push_back (' ');
                    charTypes.push_back (IPreeditString::BOUNDARY);
                }
            }
        }
    }

    ps.setCaret (caret);
}