summaryrefslogtreecommitdiffstats
path: root/src/ZYZPhoneticSection.cc
blob: 4721885aaac53d2049c4d2e879a886a0512304cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* vim:set et ts=4 sts=4:
 *
 * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus
 *
 * Copyright (c) 2014 Peng Wu <alexepico@gmail.com>
 *
 * 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, 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "ZYZPhoneticSection.h"
#include <assert.h>
#include "ZYConfig.h"
#include "ZYZhuyinProperties.h"
#include "ZYTradSimpConverter.h"

namespace ZY {

PhoneticSection::PhoneticSection (PhoneticEditor & editor,
                                  ZhuyinProperties & props) :
    m_editor (editor), m_props (props)
{
}

PhoneticSection::~PhoneticSection ()
{
}

bool
PhoneticSection::initCandidates (zhuyin_instance_t * instance,
                                 int cursor)
{
    m_instance = instance;
    m_cursor = cursor;

    size_t offset = 0;
    zhuyin_get_zhuyin_offset (instance, cursor, &offset);

    if (m_editor.m_config.candidatesAfterCursor ())
        zhuyin_guess_candidates_after_cursor (m_instance, offset);
    else
        zhuyin_guess_candidates_before_cursor (m_instance, offset);

    return true;
}

bool
PhoneticSection::fillLookupTableByPage ()
{
    LookupTable & lookup_table = getLookupTable ();

    guint len = 0;
    zhuyin_get_n_candidate (m_instance, &len);

    guint filled_nr = lookup_table.size ();
    guint page_size = lookup_table.pageSize ();

    /* fill lookup table by libzhuyin get candidates. */
    guint need_nr = MIN (page_size, len - filled_nr);
    g_assert (need_nr >=0);
    if (need_nr == 0)
        return FALSE;

    String word;
    for (guint i = filled_nr; i < filled_nr + need_nr; i++) {
        if (i >= len)  /* no more candidates */
            break;

        lookup_candidate_t * candidate = NULL;
        zhuyin_get_candidate (m_instance, i, &candidate);

        const gchar * phrase_string = NULL;
        zhuyin_get_candidate_string (m_instance, candidate, &phrase_string);

        /* show get candidates. */
        if (G_LIKELY (m_props.modeTrad ())) {
            word = phrase_string;
        } else { /* Simplified Chinese */
            word.truncate (0);
            TradSimpConverter::tradToSimp (phrase_string, word);
        }

        Text text (word);
        lookup_table.appendCandidate (text);
    }

    return TRUE;
}

int
PhoneticSection::selectCandidate (guint index)
{
    guint len = 0;
    zhuyin_get_n_candidate (m_instance, &len);

    if (index >= len)
        return 0;

    lookup_candidate_t * candidate = NULL;
    zhuyin_get_candidate (m_instance, index, &candidate);

    size_t offset = 0;
    zhuyin_get_zhuyin_offset (m_instance, m_cursor, &offset);

    offset = zhuyin_choose_candidate
        (m_instance, offset, candidate);

    return offset - m_cursor;
}

};