/**@file *@brief Fontforge Plugin: Mapping of JIS X 0212/0213-1/0213-2 to ISO-2022-JP GL *@date Sat,30 May,2009 - Mon,01 Jun,2009 *@date Thu,07 Oct,2010 - Fri,08 Oct,2010 *@author Copyright(C)2009-2010 G-HAL *@verbatim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. @endverbatim */ #include #include #include #if !defined(__STDC_LIMIT_MACROS) # define __STDC_LIMIT_MACROS #endif #include # include # include #include #include "plugins.h" #define UCS_LENGTH 4 #define EUC_IN_BUF_SIZE 3 #define UCS_IN_BUF_SIZE UCS_LENGTH #define EUC_OUT_BUF_SIZE (EUC_IN_BUF_SIZE * 32) #define UCS_OUT_BUF_SIZE (UCS_IN_BUF_SIZE * 32) static iconv_t cd_eucjisx0212_to_ucs = 0; static iconv_t cd_ucs_to_eucjisx0212 = 0; static iconv_t cd_eucjisx0213_to_ucs = 0; static iconv_t cd_ucs_to_eucjisx0213 = 0; static int jisx0212_to_ucs( int enc ) { if (0x7F7FUL <= enc) { return -1; } iconv( cd_eucjisx0212_to_ucs, NULL, NULL, NULL, NULL ); unsigned char in_buf[EUC_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[UCS_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; in_buf[0] = '\x8F'; in_buf[1] = ((enc >> 8) & 0xFFUL); in_buf[2] = ((enc ) & 0xFFUL); in_left = 3; if ((in_buf[1] <= 0x20) || (0x7F <= in_buf[1])) { return -1; } if ((in_buf[2] <= 0x20) || (0x7F <= in_buf[2])) { return -1; } in_buf[1] |= 0x80UL; in_buf[2] |= 0x80UL; const ssize_t result = iconv( cd_eucjisx0212_to_ucs, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. jisx0212_to_ucs: %d, %p, %02X%02X%02X\n", errno, cd_eucjisx0212_to_ucs, in_buf[0], in_buf[1], in_buf[2] ); } return -1; } iconv( cd_eucjisx0212_to_ucs, NULL, NULL, &out_ptr, &out_left ); if (UCS_LENGTH != (out_bufsize - out_left)) { /* printf("Combining Character. jisx0212_to_ucs: %p, %zd %02X%02X%02X\n", cd_eucjisx0212_to_ucs, (out_bufsize - out_left), in_buf[0], in_buf[1], in_buf[2] ); */ printf("JISX0212_to_UCS: Combining Character. Data was lost.(EUC:%02X%02X%02X)\n", in_buf[0], in_buf[1], in_buf[2] ); return -1; } return ntohl( *((uint32_t*)out_buf) ); } static int ucs_to_jisx0212( int u ) { if (u < 0x80) { return -1; } iconv( cd_ucs_to_eucjisx0212, NULL, NULL, NULL, NULL ); unsigned char in_buf[UCS_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[EUC_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; *((uint32_t*)in_buf) = htonl( u ); in_left = 4; const ssize_t result = iconv( cd_ucs_to_eucjisx0212, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. ucs_to_jisx0212: %d, %p, %08X\n", errno, cd_ucs_to_eucjisx0212, u ); } return -1; } iconv( cd_ucs_to_eucjisx0212, NULL, NULL, &out_ptr, &out_left ); switch (out_bufsize - out_left) { case 1: { printf("Why ISO8859 in JISX0212? ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); return -1; /* return ((uint8_t)(out_buf[0])); */ } case 2: { if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0212? ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return -1; /* printf("Broken G3 in JISX0212. ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); */ } return -1; /* printf("Why JISX0208 in JISX0212? ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); */ } case 3: if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0212? ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return (((uint16_t)(out_buf[1]) & 0x7F) << 8) | ((uint16_t)(out_buf[2]) & 0x7F); } break; default: break; } printf("Unknown. ucs_to_jisx0212: %p, %zd %08X\n", cd_ucs_to_eucjisx0212, (out_bufsize - out_left), u ); return u; } static int jisx0213_1_to_ucs( int enc ) { if (0x7F7FUL <= enc) { return -1; } iconv( cd_eucjisx0213_to_ucs, NULL, NULL, NULL, NULL ); unsigned char in_buf[EUC_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[UCS_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; in_buf[0] = ((enc >> 8) & 0xFFUL); in_buf[1] = ((enc ) & 0xFFUL); in_left = 2; if ((in_buf[0] <= 0x20) || (0x7F <= in_buf[0])) { return -1; } if ((in_buf[1] <= 0x20) || (0x7F <= in_buf[1])) { return -1; } in_buf[0] |= 0x80UL; in_buf[1] |= 0x80UL; const ssize_t result = iconv( cd_eucjisx0213_to_ucs, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. jisx0213_1_to_ucs: %d, %p, %02X%02X\n", errno, cd_eucjisx0213_to_ucs, in_buf[0], in_buf[1] ); } return -1; } iconv( cd_eucjisx0213_to_ucs, NULL, NULL, &out_ptr, &out_left ); if (UCS_LENGTH != (out_bufsize - out_left)) { /* printf("Combining Character. jisx0213_1_to_ucs: %p, %zd %02X%02X\n", cd_eucjisx0213_to_ucs, (out_bufsize - out_left), in_buf[0], in_buf[1] ); */ printf("JISX0213-1_to_UCS: Combining Character. Data was lost.(EUC:%02X%02X)\n", in_buf[0], in_buf[1] ); return -1; } return ntohl( *((uint32_t*)out_buf) ); } static int ucs_to_jisx0213_1( int u ) { if (u < 0x80) { return -1; } iconv( cd_ucs_to_eucjisx0213, NULL, NULL, NULL, NULL ); unsigned char in_buf[UCS_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[EUC_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; *((uint32_t*)in_buf) = htonl( u ); in_left = 4; const ssize_t result = iconv( cd_ucs_to_eucjisx0213, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. ucs_to_jisx0213_1: %d, %p, %08X\n", errno, cd_ucs_to_eucjisx0213, u ); } return -1; } iconv( cd_ucs_to_eucjisx0213, NULL, NULL, &out_ptr, &out_left ); switch (out_bufsize - out_left) { case 1: { printf("Why ISO8859 in JISX0213-1? ucs_to_jisx0213_1: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); return -1; /* return ((uint8_t)(out_buf[0])); */ } case 2: { if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0213-1? ucs_to_jisx0213: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return -1; /* printf("Broken G3 in JISX0213-1. ucs_to_jisx0213: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); */ } return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); } case 3: if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0213-1? ucs_to_jisx0213_1: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return -1; /* printf("Why JISX0213-2 in JISX0213-1? ucs_to_jisx0213_1: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[1]) & 0x7F) << 8) | ((uint16_t)(out_buf[2]) & 0x7F); */ } break; default: break; } printf("Unknown. ucs_to_jisx0213_1: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); return u; } static int jisx0213_2_to_ucs( int enc ) { if (0x7F7FUL <= enc) { return -1; } iconv( cd_eucjisx0213_to_ucs, NULL, NULL, NULL, NULL ); unsigned char in_buf[EUC_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[UCS_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; in_buf[0] = '\x8F'; in_buf[1] = ((enc >> 8) & 0xFFUL); in_buf[2] = ((enc ) & 0xFFUL); in_left = 3; if ((in_buf[1] <= 0x20) || (0x7F <= in_buf[1])) { return -1; } if ((in_buf[2] <= 0x20) || (0x7F <= in_buf[2])) { return -1; } in_buf[1] |= 0x80UL; in_buf[2] |= 0x80UL; const ssize_t result = iconv( cd_eucjisx0213_to_ucs, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. jisx0213_2_to_ucs: %d, %p, %02X%02X%02X\n", errno, cd_eucjisx0213_to_ucs, in_buf[0], in_buf[1], in_buf[2] ); } return -1; } iconv( cd_eucjisx0213_to_ucs, NULL, NULL, &out_ptr, &out_left ); if (UCS_LENGTH != (out_bufsize - out_left)) { /* printf("Combining Character. jisx0213_2_to_ucs: %p, %zd %02X%02X%02X\n", cd_eucjisx0213_to_ucs, (out_bufsize - out_left), in_buf[0], in_buf[1], in_buf[2] ); */ printf("JISX0213-2_to_UCS: Combining Character. Data was lost.(EUC:%02X%02X%02X)\n", in_buf[0], in_buf[1], in_buf[2] ); return -1; } return ntohl( *((uint32_t*)out_buf) ); } static int ucs_to_jisx0213_2( int u ) { if (u < 0x80) { return -1; } iconv( cd_ucs_to_eucjisx0213, NULL, NULL, NULL, NULL ); unsigned char in_buf[UCS_IN_BUF_SIZE]; size_t in_left; const char* in_ptr = in_buf; unsigned char out_buf[EUC_OUT_BUF_SIZE]; const size_t out_bufsize = sizeof(out_buf); size_t out_left = out_bufsize; char* out_ptr = out_buf; *((uint32_t*)in_buf) = htonl( u ); in_left = 4; const ssize_t result = iconv( cd_ucs_to_eucjisx0213, &in_ptr, &in_left, &out_ptr, &out_left ); if (result < 0) { if (EILSEQ != errno) { printf("Failed. ucs_to_jisx0213_2: %d, %p, %08X\n", errno, cd_ucs_to_eucjisx0213, u ); } return -1; } iconv( cd_ucs_to_eucjisx0213, NULL, NULL, &out_ptr, &out_left ); switch (out_bufsize - out_left) { case 1: { printf("Why ISO8859 in JISX0213? ucs_to_jisx0213_2: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); return -1; /* return ((uint8_t)(out_buf[0])); */ } case 2: { if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0213-2? ucs_to_jisx0213: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return -1; /* printf("Broken G3 in JISX0213-2. ucs_to_jisx0213: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); */ } return -1; /* printf("Why JISX0213-1 in JISX0213-2? ucs_to_jisx0213_2: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return (((uint16_t)(out_buf[0]) & 0x7F) << 8) | ((uint16_t)(out_buf[1]) & 0x7F); */ } case 3: if (0x8E == out_buf[0]) { return -1; /* printf("Why JISX0201 in JISX0213-2? ucs_to_jisx0213_2: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); */ /* return ((uint8_t)(out_buf[1])); */ } else if (0x8F == out_buf[0]) { return (((uint16_t)(out_buf[1]) & 0x7F) << 8) | ((uint16_t)(out_buf[2]) & 0x7F); } break; default: break; } printf("Unknown. ucs_to_jisx0213_2: %p, %zd %08X\n", cd_ucs_to_eucjisx0213, (out_bufsize - out_left), u ); return u; } int FontForgeInit( void ) { { cd_eucjisx0212_to_ucs = iconv_open( "UCS-4BE", "EUC-JP" ); cd_ucs_to_eucjisx0212 = iconv_open( "EUC-JP", "UCS-4BE" ); if (((iconv_t)(-1) == cd_eucjisx0212_to_ucs) || ((iconv_t)(-1) == cd_ucs_to_eucjisx0212) ) { LogError("Failed to init JISX0212" ); return 0; } if ( !AddEncoding("JISX0212.1990-0", jisx0212_to_ucs, ucs_to_jisx0212, 0x7F7F) ) { LogError("Failed to add JISX0212.1990-0" ); return 0; } } { cd_eucjisx0213_to_ucs = iconv_open( "UCS-4BE", "EUC-JISX0213" ); cd_ucs_to_eucjisx0213 = iconv_open( "EUC-JISX0213", "UCS-4BE" ); if (((iconv_t)(-1) == cd_eucjisx0213_to_ucs) || ((iconv_t)(-1) == cd_ucs_to_eucjisx0213) ) { LogError("Failed to init JISX0213" ); return 0; } if ( !AddEncoding("JISX0213.2000-1", jisx0213_1_to_ucs, ucs_to_jisx0213_1, 0x7F7F) ) { LogError("Failed to add JISX0213.2000-1" ); return 0; } if ( !AddEncoding("JISX0213.2000-2", jisx0213_2_to_ucs, ucs_to_jisx0213_2, 0x7F7F) ) { LogError("Failed to add JISX0213.2000-2" ); return 0; } if ( !AddEncoding("JISX0213.2004-1", jisx0213_1_to_ucs, ucs_to_jisx0213_1, 0x7F7F) ) { LogError("Failed to add JISX0213.2004-1" ); return 0; } if ( !AddEncoding("JISX0213.2004-2", jisx0213_2_to_ucs, ucs_to_jisx0213_2, 0x7F7F) ) { LogError("Failed to add JISX0213.2004-2" ); return 0; } } return 1; } /* [ End of File ] */