Newer
Older
scim-wnn / scim-wnn / src / wnnconversion.cpp
/***************************************************************************
 *   Copyright (C) 2004 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "wnnconversion.h"

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#ifdef HAVE_GETTEXT
  #include <libintl.h>
  #define _(String) dgettext(GETTEXT_PACKAGE,String)
  #define N_(String) (String)
#else
  #define _(String) (String)
  #define N_(String) (String)
  #define bindtextdomain(Package,Directory)
  #define textdomain(domain)
  #define bind_textdomain_codeset(domain,codeset)
#endif

WnnConversion::WnnConversion()
{
    // とりあえず決めうちでいきます。
    m_iconv.set_encoding ("EUC-JP");
    pos = 0;
    bunsetu = 0;
    wnn = 0;
}


WnnConversion::~WnnConversion()
{
}


    /* w_charからEUCに変換するアレ */
void WnnConversion::wstostr(unsigned char *e,w_char *u) {
    w_char x;
    for(;*u;) {
        x = *u ++;
        if (x & 0x8000) {
            *e ++ = x >> 8;
            *e ++ = x;
        } else
        if (x & 0x80) {
            *e ++ = 0x8e;
            *e ++ = x & 0xff;
        } else
            *e++ = x;
    }
    *e ++ = 0;
}

    /* EUCからw_charに変換するナニ */
void WnnConversion::strtows(w_char *u,unsigned char *e) {
    w_char  x;
    for(;*e;) {
        x = *e ++;
        if (x == 0x8e) x = *e ++ | 0x80;
        if (x & 0x80)
        x = ((x << 8) & 0xff00)  | *e ++;
        *u ++ = x;
    }
    *u = 0;
}


/*!
    \fn WnnConversion::wnnConnect(String name,String host,String rc,int timeout)
 */
bool WnnConversion::wnnConnect(String name,String host,String rc,int timeout)
{
    wnn = jl_open_lang((char *)name.data(),(char *)host.data(),"jp_JP",(char *)rc.data(),wnn_error,wnn_message,timeout);
    //wnn = jl_open_lang("test","localhost","jp_JP","/usr/lib/wnn7/ja_JP/wnnenvrc",wnn_error,wnn_message,10);
    if (!wnn) return(false);
    return(true);
}


/*!
    \fn WnnConversion::isConnected()
 */
bool WnnConversion::isConnected()
{
    if (wnn) return(true);
    else (false);
}




/*!
    \fn WnnConversion::wnn_message (char *s)
 */
int WnnConversion::wnn_message (char *s)
{

    SCIM_DEBUG_IMENGINE(1) << s << "\n";
}

/*!
    \fn WnnConversion::wnn_error (char *s)
 */
int WnnConversion::wnn_error (char *s)
{

    SCIM_DEBUG_IMENGINE(1) << s << "\n";
}




/*!
    \fn WnnConversion::reset()
 */
void WnnConversion::reset()
{
    yomiText.clear();
    bunList.clear();
    yomiList.clear();
    text.clear();
    attr.clear();
    pos = 0;
    jl_kill(wnn,0,-1);
}


/*!
    \fn WnnConversion::setYomiText(WideString yomi)
 */
void WnnConversion::setYomiText(WideString yomi)
{
    yomiText = yomi;
}


/*!
    \fn WnnConversion::ren_conversion()
 */
int WnnConversion::ren_conversion()
{
    convList.Yomi.clear();
    convList.kouho.clear();
    bunList.clear();
    yomiList.clear();
    pos = 0;
    w_char ws[1024];
    char c[2048];
    String y;
    m_iconv.convert(y,yomiText);
    strtows(ws,(unsigned char*)y.data());
    bunsetu = jl_ren_conv(wnn,ws,0,-1,WNN_NO_USE);
    bunList.resize(bunsetu);
    yomiList.resize(bunsetu);
    for(unsigned int i = 0;i < bunsetu;i ++) {
        WideString w;
        wnn_get_area(wnn,i,i + 1,ws,1);
        wstostr((unsigned char*)c,ws);
        m_iconv.convert(w,c,strlen(c));
        bunList[i] = w;
        wnn_get_area(wnn,i,i + 1,ws,0);
        wstostr((unsigned char*)c,ws);
        m_iconv.convert(w,c,strlen(c));
        yomiList[i] = w;
    }
    createText();
    return(bunsetu);
}


/*!
    \fn WnnConversion::resizeRegion(int w)
 */
bool WnnConversion::resizeRegion(int w)
{
    if (w == 0) return(false);
    if ((yomiList[pos].length() + w) <= 0)  return(false);
    if (((pos + 1) >= yomiList.size()) && (w > 0)) return(false);

    w_char ws[1024];
    char c[2048];
    int s;
    int h = WNN_NO_USE;
    if (pos > 0) h = WNN_USE_MAE;
    else if (pos < bunsetu - 1) h = WNN_USE_ATO;
    bunsetu = jl_nobi_conv(wnn,pos,yomiList[pos].length() + w,-1,h,WNN_SHO);
    convList.kouho.clear();
    bunList.clear();
    bunList.resize(bunsetu);
    yomiList.resize(bunsetu);
    for(unsigned int i = 0;i < bunsetu;i ++) {
        WideString w;
        wnn_get_area(wnn,i,i + 1,ws,1);
        wstostr((unsigned char*)c,ws);
        m_iconv.convert(w,c,strlen(c));
        bunList[i] = w;
        wnn_get_area(wnn,i,i + 1,ws,0);
        wstostr((unsigned char*)c,ws);
        m_iconv.convert(w,c,strlen(c));
        yomiList[i] = w;
    }
    createText();
    return(true);
}


/*!
    \fn WnnConversion::createText()
 */
void WnnConversion::createText()
{
    WideString w;
    caretPos = 0;
    for(unsigned int i = 0;i < bunsetu;i ++) {
        if (pos == i) {
            caretPos = w.length();
            attr.clear();
            Attribute a(w.length(),bunList[i].length(),SCIM_ATTR_DECORATE,SCIM_ATTR_DECORATE_REVERSE);
            attr.push_back(a);
        }
        w = w + bunList[i];
    }
    text = w;
}

/*!
    \fn WnnConversion::getText()
 */
WideString WnnConversion::getText()
{
    return(text);
}


/*!
    \fn WnnConversion::getAttributeList()
 */
AttributeList WnnConversion::getAttributeList()
{
    return(attr);
}


/*!
    \fn WnnConversion::setPos(int p)
 */
int WnnConversion::setPos(int p)
{
    if (p >= bunsetu) p = bunsetu - 1;
    else if (p < 0) p = 0;

    pos = p;
    createText();
    return(pos);
}


/*!
    \fn WnnConversion::getPos()
 */
int WnnConversion::getPos()
{
    return(pos);
}


/*!
    \fn WnnConversion::getConversionList(int p)
 */
WnnConversionList WnnConversion::getConversionList(int p)
{
    convList.kouho.clear();
    convList.pos = 0;
    convList.count = 0;
    if (p == -1) p = pos;
    if ((p >= bunsetu) || (p < 0)) return(convList);
    pos = p;
    convList.pos = jl_zenkouho(wnn,pos,WNN_USE_ZENGO,WNN_UNIQ);
    convList.count = jl_zenkouho_suu(wnn);
    convList.kouho.resize(convList.count);

    for (unsigned int i = 0;i < convList.count; i ++) {
        w_char k[1024];
        char buf[2048];
        WideString u;
        jl_get_zenkouho_kanji(wnn,i,k);
        wstostr((unsigned char*)buf,k);
        m_iconv.convert(u,buf,strlen(buf));
        convList.kouho[i] = u;
    }
    select(convList.pos);
    createText();
    return(convList);

}


/*!
    \fn WnnConversion::select(int p)
 */
bool WnnConversion::select(int p)
{
    if (p > convList.count) p = 0;
    convList.pos = p;
    jl_set_jikouho(wnn,p);

    bunList.at(pos) = convList.kouho.at(p);
    createText();

    return(true);

}






/*!
    \fn WnnConversion::updateFrequency()
 */
void WnnConversion::updateFrequency()
{
    if (bunsetu)
        jl_update_hindo(wnn,0,-1);
}



/*!
    \fn WnnConversion::getCaretPos()
 */
int WnnConversion::getCaretPos()
{
    return(caretPos);
}