Skip to content
Permalink
Browse files

temp

  • Loading branch information...
youknowone committed Jun 11, 2014
1 parent 6824236 commit 45f999a17afb43efa22b37f13c86bd8dadef6aff

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,188 @@
//
// KSX5002.cpp
// hangeul
//
// Created by Jeong YunWon on 2014. 7. 5..
// Copyright (c) 2014년 youknowone.org. All rights reserved.
//

#include "KSX5002.h"

namespace hangeul {

namespace KSX5002 {
UnicodeVector Decoder::decode(State state) {
UnicodeVector unicodes;
if (state['a'] && state['b']) {
auto v = 0xac00;
v += (Initial::FromConsonant[state['a']] - 1) * 21 * 28;
v += (state['b'] - 1) * 28;
if (state['c']) {
v += Final::FromConsonant[state['c']];
}
unicodes.push_back(v);
}
else if (state['a'] && !state['b']) {
auto v = 0x3131 + state['a'] - 1;
unicodes.push_back(v);
}
else if (!state['a'] && !state['b']) {
auto v = 0x314f + state['b'] - 1;
unicodes.push_back(v);
}
else {
unicodes.push_back(state[0]);
}
return unicodes;
}

PhaseResult KeyStrokeToAnnotationPhase::put(StateList states) {
#define A(C) { AnnotationClass::ASCII, C }
#define F(C) { AnnotationClass::Function, KeyPosition ## C }
#define C(C) { AnnotationClass::Consonant, Consonant:: C }
#define V(C) { AnnotationClass::Vowel, Vowel:: C }
#define E() { AnnotationClass::Function, 0 }

static Annotation map[] = {
A('`'), A('1'), A('2'), A('3'), A('4'), A('5'), A('6'), A('7'), A('8'), A('9'), A('0'), A('-'), A('='), A('\\'), F(Backspace), E(),
A('\t'), C(B), C(J), C(D), C(G), C(S), V(Yo), V(Yeo), V(Ya), V(Ae), V(E), A('['), A(']'), E(), E(), E(),
E(), C(M), C(N), C(NG), C(R), C(H), V(O), V(Eo), V(A), V(I), A(';'), A('\''), F(Enter), E(), E(), E(),
E(), C(K), C(T), C(CH), C(P), V(Yu), V(U), V(Eu), A(','), A('.'), A('/'), E(), E(), E(), E(), E(),
};

#undef A
#undef F
#undef C
#undef V

auto& state = states.front();
auto input = state[2];
Annotation stroke = map[input & 0xff];
switch (stroke.type) {
case AnnotationClass::Consonant:
state['a'] = stroke.data;
break;
case AnnotationClass::Vowel:
state['b'] = stroke.data;
break;
case AnnotationClass::ASCII:
state[0] = stroke.data;
break;
case AnnotationClass::Function:
state[2] = stroke.data;
break;
default:
break;
}

return PhaseResult::Make(states, true);
}

PhaseResult JasoCombinationPhase::put(StateList states) {
if (states.size() == 1) {
return PhaseResult::Make(states, false);
}
auto iter = states.begin();
auto& state = *iter;
iter++;
auto& secondary = *iter;
bool combined = false;
if (state['a'] && secondary['c']) {
#define R(A, B, X) {Consonant::A, Consonant::B, Consonant::X}
const Consonant::Type table[][3] = {
R(G, S, GS),
R(N, J, NJ),
R(N, H, NH),
R(R, G, RG),
R(R, M, RM),
R(R, B, RB),
R(R, S, RS),
R(R, T, RT),
R(R, P, RP),
R(R, H, RH),
};
#undef R
auto c1 = state['a'];
auto c2 = secondary['c'];
for (auto& rule: table) {
if (c1 == rule[0] && c2 == rule[1]) {
secondary['c'] = rule[2];
combined = true;
break;
}
}
}
else if (state['b'] && secondary['b']) {
#define R(A, B, X) {Vowel::A, Vowel::B, Vowel::X}
const Vowel::Type table[][3] = {
R(O, A, Wa),
R(O, Ae, Wae),
R(O, I, Oe),
R(U, Eo, Weo),
R(U, E, We),
R(U, I, Wi),
R(Eu, I, Ui),
};
#undef R
auto c1 = state['b'];
auto c2 = secondary['b'];
for (auto& rule: table) {
if (c1 == rule[0] && c2 == rule[1]) {
secondary['b'] = rule[2];
combined = true;
break;
}
}
}
if (combined) {
states.erase(states.begin());
return PhaseResult::Make(states, true);
}
return PhaseResult::Make(states, true);
}

PhaseResult AnnotationToCombinationPhase::put(StateList states) {
auto iter = states.begin();
auto& state = states.front();
if (!state['a'] && !state['b'] && !state['c']) {
return PhaseResult::Make(states, true);
}
if (states.size() == 1) {
return PhaseResult::Make(states, false);
}
iter++;
auto& secondary = *iter;
if (state['a']) {
if (secondary['b'] && !secondary['c']) {
secondary['c'] = state['a'];
states.erase(states.begin());
return PhaseResult::Make(states, true);
}
else {
return PhaseResult::Make(states, false);
}
}
else if (state['b']) {
if (!secondary['b']) {
secondary['b'] = state['b'];
states.erase(states.begin());
return PhaseResult::Make(states, true);
}
else if (!state['a'] && secondary['c']) {
state['a'] = secondary['c'];
secondary['c'] = 0;
return PhaseResult::Make(states, true);
}
else {
return PhaseResult::Make(states, false);
}
}
else {
assert(false);
}
return PhaseResult::Make(states, false);
}

}

}
@@ -0,0 +1,76 @@
//
// KSX5002.h
// hangeul
//
// Created by Jeong YunWon on 2014. 7. 5..
// Copyright (c) 2014년 youknowone.org. All rights reserved.
//

#ifndef __hangeul__KSX5002__
#define __hangeul__KSX5002__

#include <hangeul/phase.h>
#include <hangeul/decoder.h>

namespace hangeul { namespace KSX5002 {

namespace AnnotationClass {
enum Type {
ASCII,
Function,
Consonant,
Vowel,
};
}

struct Annotation {
AnnotationClass::Type type;
uint32_t data;
};

class Decoder: public hangeul::Decoder {
virtual UnicodeVector decode(State state);
};

//! state[2]->state['0', 'a', 'b', 'c'] - 날개셋 호환용
class KeyStrokeToAnnotationPhase: public AnnotationPhase {
public:
virtual PhaseResult put(StateList state);

static std::string InputType() { assert(false); return "keyposition"; }
static std::string OutputType() { assert(false); return "annotation-ksx5002"; }
};

class JasoCombinationPhase: Phase {
public:
virtual PhaseResult put(StateList state);

static std::string InputType() { assert(false); return "annotation-ksx5002"; }
static std::string OutputType() { assert(false); return "annotation-ksx5002"; }
};

class AnnotationToCombinationPhase: Phase {
public:
virtual PhaseResult put(StateList state);

static std::string InputType() { assert(false); return "annotation-ksx5002"; }
static std::string OutputType() { assert(false); return "combination-ksx5002"; }
};


class FromQwertyPhase: public CombinedPhase {
public:
static std::string InputType() { assert(false); return "inputsource-qwerty"; }
static std::string OutputType() { assert(false); return "combination-ksx5002"; }

FromQwertyPhase() : CombinedPhase() {
this->phases.push_back((Phase *)new QwertyToKeyStrokePhase());
this->phases.push_back((Phase *)new KeyStrokeToAnnotationPhase());
this->phases.push_back((Phase *)new JasoCombinationPhase());
this->phases.push_back((Phase *)new AnnotationToCombinationPhase());
}
};

} }

#endif /* defined(__hangeul__KSX5002__) */
@@ -0,0 +1,9 @@
//
// context.cpp
// hangeul
//
// Created by Jeong YunWon on 2014. 6. 9..
// Copyright (c) 2014년 youknowone.org. All rights reserved.
//

#include <stdio.h>
@@ -0,0 +1,63 @@
//
// context.h
// hangeul
//
// Created by Jeong YunWon on 2014. 6. 9..
// Copyright (c) 2014년 youknowone.org. All rights reserved.
//

#ifndef __HANGEUL_CONTEXT__
#define __HANGEUL_CONTEXT__

#include <vector>
#include <hangeul/phase.h>

namespace hangeul {
class Context {
protected:
StateList states;
Phase *processor;
Phase *decoder;

public:
Context(Phase *processor, Phase *decoder) { this->processor = processor; this->decoder = decoder; }

void put(InputSource input) {
State state;
state[1] = input;
this->states.push_front(state);
auto res = this->processor->put(this->states);
this->states = res.states;
}

void flush() {
this->states.front()[-1] = 1;
}

UnicodeVector commited() {
UnicodeVector result;
while (this->states.size() > 0) {
// FIXME: not a STL way yet
result.push_back(this->states.back()[0]);
this->states.pop_back();
}
if (this->states.size() && this->states.back()[-1]) {
result.push_back(this->states.back()[0]);
this->states.pop_back();
}
return result;
}

UnicodeVector state() {
StateList copied = states;
copied.reverse();
UnicodeVector result;
for (auto state: copied) {
result.push_back(state[0]);
}
return result;
}
};
}

#endif
Oops, something went wrong.

0 comments on commit 45f999a

Please sign in to comment.
You can’t perform that action at this time.