DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/misc/shiftjis.cpp
00001 
00002 #include "shiftjis.h"
00003 
00004 ShiftJISDecoder::ShiftJISDecoder() {
00005     reset();
00006 }
00007 
00008 bool ShiftJISDecoder::leadByteWaitingForSecondByte(void) {
00009     return fullwidth;
00010 }
00011 
00012 void ShiftJISDecoder::reset(void) {
00013     doublewide = false;
00014     fullwidth = false;
00015     b1 = b2 = 0;
00016 }
00017 
00018 bool ShiftJISDecoder::take(unsigned char c) {
00019     /* JIS sequence j1, j2
00020      * ShiftJIS bytes s1, s2
00021      *
00022      * if (33 <= j1 && j1 <= 94)
00023      *   s1 = ((j1 + 1) / 2) + 112
00024      * else if (95 <= j1 && j1 <= 126)
00025      *   s1 = ((j1 + 1) / 2) + 176
00026      * 
00027      * if (j1 & 1)    <- is odd
00028      *   s2 = j2 + 31 + (j2 / 96)
00029      * else           <- is even
00030      *   s2 = j2 + 126
00031      */ 
00032     if (!fullwidth) {
00033         doublewide = false;
00034         if (c >= 0x81 && c <= 0x9F) {
00035             /* Reverse:
00036              *
00037              *    s1 = ((j1 + 1) / 2) + 112
00038              *    s1 - 112 = (j1 + 1) / 2
00039              *    (s1 - 112) * 2 = j1 + 1
00040              *    ((s1 - 112) * 2) - 1 = j1 */
00041             doublewide = fullwidth = true;
00042             b1 = (c - 112) * 2;
00043             return false;
00044         }
00045         else if (c >= 0xE0 && c <= 0xEF) {
00046             /* Reverse:
00047              *
00048              *    s1 = ((j1 + 1) / 2) + 176
00049              *    s1 - 176 = (j1 + 1) / 2
00050              *    (s1 - 176) * 2 = j1 + 1
00051              *    ((s1 - 176) * 2) - 1 = j1 */
00052             doublewide = fullwidth = true;
00053             b1 = (c - 176) * 2;
00054             return false;
00055         }
00056         else {
00057             b2 = 0;
00058             b1 = c;
00059         }
00060     }
00061     else { // fullwidth, 2nd byte
00062         if (c >= 0x9F) { /* j1 is even */
00063             b2 = c - 126;
00064         }
00065         else if (c >= 0x40 && c != 0x7F) { /* j1 is odd */
00066             b1--; /* (j1 + 1) / 2 */
00067             b2 = c - 31;
00068             if (c >= 0x80) b2--; /* gap at 0x7F */
00069         }
00070         else {
00071             // ???
00072             b1 = 0x7F;
00073             b2 = 0x7F;
00074         }
00075 
00076         // some character combinations are actually single-wide such as the
00077         // proprietary non-standard box drawing characters on PC-98 systems.
00078         if ((b1 & 0xFC) == 0x28) /* 0x28-0x2B */
00079             doublewide = false;
00080 
00081         fullwidth = false;
00082     }
00083 
00084     return true;
00085 }
00086