123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
- //
- // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
- // All rights reserved.
- //
- // This file is part of SDLPAL.
- //
- // SDLPAL 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 3 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, see <http://www.gnu.org/licenses/>.
- //
- // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
- //
- #include "font.h"
- #include "util.h"
- #if defined(PAL_UNICODE)
- #include "fontglyph.h"
- INT
- PAL_InitFont(
- VOID
- )
- /*++
- Purpose:
- None.
- Parameters:
- None.
- Return value:
- Always return 0.
- --*/
- {
- return 0;
- }
- VOID
- PAL_FreeFont(
- VOID
- )
- /*++
- Purpose:
- None.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- }
- VOID
- PAL_DrawCharOnSurface(
- WORD wChar,
- SDL_Surface *lpSurface,
- PAL_POS pos,
- BYTE bColor
- )
- /*++
- Purpose:
- Draw a Unicode character on a surface.
- Parameters:
- [IN] wChar - the unicode character to be drawn.
- [OUT] lpSurface - the destination surface.
- [IN] pos - the destination location of the surface.
- [IN] bColor - the color of the character.
- Return value:
- None.
- --*/
- {
- int i, j;
- int x = PAL_X(pos), y = PAL_Y(pos);
- //
- // Check for NULL pointer & invalid char code.
- //
- if (lpSurface == NULL || (wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
- {
- return;
- }
- //
- // Locate for this character in the font lib.
- //
- if (wChar >= unicode_upper_base)
- {
- wChar -= (unicode_upper_base - 0xd800);
- }
- //
- // Draw the character to the surface.
- //
- LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
- if (font_width[wChar] == 32)
- {
- for (i = 0; i < 32; i += 2, dest += lpSurface->pitch)
- {
- for (j = 0; j < 8; j++)
- {
- if (unicode_font[wChar][i] & (1 << (7 - j)))
- {
- dest[j] = bColor;
- }
- }
- for (j = 0; j < 8; j++)
- {
- if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
- {
- dest[j + 8] = bColor;
- }
- }
- }
- }
- else
- {
- for (i = 0; i < 16; i++, dest += lpSurface->pitch)
- {
- for (j = 0; j < 8; j++)
- {
- if (unicode_font[wChar][i] & (1 << (7 - j)))
- {
- dest[j] = bColor;
- }
- }
- }
- }
- }
- INT
- PAL_CharWidth(
- WORD wChar
- )
- /*++
- Purpose:
- Get the text width of a character.
- Parameters:
- [IN] wChar - the unicode character for width calculation.
- Return value:
- The width of the character, 16 for full-width char and 8 for half-width char.
- --*/
- {
- if ((wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
- {
- return 0;
- }
- //
- // Locate for this character in the font lib.
- //
- if (wChar >= unicode_upper_base)
- {
- wChar -= (unicode_upper_base - 0xd800);
- }
- return font_width[wChar] >> 1;
- }
- #elif defined(PAL_WIN95)
- /*
- * Portions based on:
- *
- * YH - Console Chinese Environment -
- * Copyright (C) 1999 Red Flag Linux (office@sonata.iscas.ac.cn)
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY RED FLAG LINUX ``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 TERRENCE R. LAMBERT 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.
- *
- */
- /*
- * Portions based on:
- *
- * KON2 - Kanji ON Console -
- * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
- *
- * CCE - Console Chinese Environment -
- * Copyright (C) 1998-1999 Rui He (herui@cs.duke.edu)
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY TAKASHI MANABE ``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 TERRENCE R. LAMBERT 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.
- *
- */
- #include "gbfont.h"
- #include "big5font.h"
- BOOL fIsBig5 = FALSE;
- INT
- PAL_InitFont(
- VOID
- )
- /*++
- Purpose:
- Load the font files.
- Parameters:
- None.
- Return value:
- 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
- --*/
- {
- FILE *fp;
- fp = fopen(PAL_PREFIX "word.dat", "rb");
- if (!fp)
- {
- return 0;
- }
- fseek(fp, 0x1E, SEEK_SET);
- if (fgetc(fp) == 0xAA)
- {
- fIsBig5 = TRUE;
- }
- fclose(fp);
- return 0;
- }
- VOID
- PAL_FreeFont(
- VOID
- )
- /*++
- Purpose:
- Free the memory used for fonts.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- }
- static BOOL is_gb(unsigned char b1, unsigned char b2)
- {
- if (b1 < 0xa1 || b1 > 0xfe)
- return FALSE;
- if (b2 < 0xa1 || b2 > 0xfe)
- return FALSE;
- return TRUE;
- }
- VOID
- PAL_DrawCharOnSurface(
- WORD wChar,
- SDL_Surface *lpSurface,
- PAL_POS pos,
- BYTE bColor
- )
- /*++
- Purpose:
- Draw a BIG-5 Chinese character on a surface.
- Parameters:
- [IN] wChar - the character to be drawn (in GB2312/BIG5).
- [OUT] lpSurface - the destination surface.
- [IN] pos - the destination location of the surface.
- [IN] bColor - the color of the character.
- Return value:
- None.
- --*/
- {
- int i, j, dx;
- int x = PAL_X(pos), y = PAL_Y(pos);
- LPBYTE pChar;
- BYTE ch1, ch2;
- //
- // Check for NULL pointer.
- //
- if (lpSurface == NULL)
- {
- return;
- }
- //
- // Locate for this character in the font lib.
- //
- ch1 = wChar & 0xff;
- ch2 = wChar >> 8;
- if (fIsBig5)
- {
- if (ch2 < 0xa1)
- pChar = &big5font[((ch1 - 0xA1) * 157 + ch2 - 0x40) << 5] + 8;
- else
- pChar = &big5font[((ch1 - 0xA1) * 157 + 63 + ch2 - 0xA1) << 5] + 8;
- }
- else
- {
- if (!is_gb(ch1, ch2))
- {
- return;
- }
- pChar = &gbfont[((ch1 - 0xa1) * 94 + (ch2 - 0xa1)) * 32];
- }
- if (pChar == NULL) return;
- //
- // Draw the character to the surface.
- //
- if (y >= lpSurface->h) return;
- y *= lpSurface->pitch;
- for (i = 0; i < 32; i++)
- {
- dx = x + ((i & 1) << 3);
- for (j = 0; j < 8; j++)
- {
- if (pChar[i] & (1 << (7 - j)))
- {
- if (dx < lpSurface->w)
- {
- ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
- }
- else
- {
- break;
- }
- }
- dx++;
- }
- y += (i & 1) * lpSurface->pitch;
- if (y / lpSurface->pitch >= lpSurface->h)
- {
- break;
- }
- }
- }
- VOID
- PAL_DrawASCIICharOnSurface(
- BYTE bChar,
- SDL_Surface *lpSurface,
- PAL_POS pos,
- BYTE bColor
- )
- /*++
- Purpose:
- Draw a ASCII character on a surface.
- Parameters:
- [IN] bChar - the character to be drawn.
- [OUT] lpSurface - the destination surface.
- [IN] pos - the destination location of the surface.
- [IN] bColor - the color of the character.
- Return value:
- None.
- --*/
- {
- int i, j, dx;
- int x = PAL_X(pos), y = PAL_Y(pos);
- LPBYTE pChar;
- pChar = &iso_font[(int)(bChar & ~128) * 15];
- //
- // Check for NULL pointer.
- //
- if (lpSurface == NULL)
- {
- return;
- }
- //
- // Draw the character to the surface.
- //
- if (y >= lpSurface->h) return;
- y *= lpSurface->pitch;
- for (i = 0; i < 15; i++)
- {
- dx = x;
- for (j = 0; j < 8; j++)
- {
- if (pChar[i] & (1 << j))
- {
- if (dx < lpSurface->w)
- {
- ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
- }
- else
- {
- break;
- }
- }
- dx++;
- }
- y += lpSurface->pitch;
- if (y / lpSurface->pitch >= lpSurface->h)
- {
- break;
- }
- }
- }
- #else
- typedef struct tagFont
- {
- LPWORD lpBufChar;
- LPBYTE lpBufGlyph;
- INT nChar;
- } FONT, *LPFONT;
- static LPFONT gpFont = NULL;
- INT
- PAL_InitFont(
- VOID
- )
- /*++
- Purpose:
- Load the font files.
- Parameters:
- None.
- Return value:
- 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
- --*/
- {
- FILE *fp;
- if (gpFont != NULL)
- {
- //
- // Already initialized
- //
- return 0;
- }
- gpFont = (LPFONT)calloc(1, sizeof(FONT));
- if (gpFont == NULL)
- {
- return -1;
- }
- //
- // Load the wor16.asc file.
- //
- fp = UTIL_OpenRequiredFile("wor16.asc");
- //
- // Get the size of wor16.asc file.
- //
- fseek(fp, 0, SEEK_END);
- gpFont->nChar = ftell(fp);
- gpFont->nChar /= 2;
- //
- // Read all the character codes.
- //
- gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
- if (gpFont->lpBufChar == NULL)
- {
- free(gpFont);
- gpFont = NULL;
- return -1;
- }
- fseek(fp, 0, SEEK_SET);
- fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
- //
- // Close wor16.asc file.
- //
- fclose(fp);
- //
- // Read all bitmaps from wor16.fon file.
- //
- fp = UTIL_OpenRequiredFile("wor16.fon");
- gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
- if (gpFont->lpBufGlyph == NULL)
- {
- free(gpFont->lpBufChar);
- free(gpFont);
- gpFont = NULL;
- return -1;
- }
- //
- // The font glyph data begins at offset 0x682 in wor16.fon.
- //
- fseek(fp, 0x682, SEEK_SET);
- fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
- fclose(fp);
- return 0;
- }
- VOID
- PAL_FreeFont(
- VOID
- )
- /*++
- Purpose:
- Free the memory used for fonts.
- Parameters:
- None.
- Return value:
- None.
- --*/
- {
- if (gpFont != NULL)
- {
- free(gpFont->lpBufChar);
- free(gpFont->lpBufGlyph);
- free(gpFont);
- }
- gpFont = NULL;
- }
- VOID
- PAL_DrawCharOnSurface(
- WORD wChar,
- SDL_Surface *lpSurface,
- PAL_POS pos,
- BYTE bColor
- )
- /*++
- Purpose:
- Draw a BIG-5 Chinese character on a surface.
- Parameters:
- [IN] wChar - the character to be drawn (in BIG-5).
- [OUT] lpSurface - the destination surface.
- [IN] pos - the destination location of the surface.
- [IN] bColor - the color of the character.
- Return value:
- None.
- --*/
- {
- int i, j, dx;
- int x = PAL_X(pos), y = PAL_Y(pos);
- LPBYTE pChar;
- //
- // Check for NULL pointer.
- //
- if (lpSurface == NULL || gpFont == NULL)
- {
- return;
- }
- //
- // Locate for this character in the font lib.
- //
- for (i = 0; i < gpFont->nChar; i++)
- {
- if (gpFont->lpBufChar[i] == wChar)
- {
- break;
- }
- }
- if (i >= gpFont->nChar)
- {
- //
- // This character does not exist in the font lib.
- //
- return;
- }
- pChar = gpFont->lpBufGlyph + i * 30;
- //
- // Draw the character to the surface.
- //
- y *= lpSurface->pitch;
- for (i = 0; i < 30; i++)
- {
- dx = x + ((i & 1) << 3);
- for (j = 0; j < 8; j++)
- {
- if (pChar[i] & (1 << (7 - j)))
- {
- ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
- }
- dx++;
- }
- y += (i & 1) * lpSurface->pitch;
- }
- }
- VOID
- PAL_DrawASCIICharOnSurface(
- BYTE bChar,
- SDL_Surface *lpSurface,
- PAL_POS pos,
- BYTE bColor
- )
- /*++
- Purpose:
- Draw a ASCII character on a surface.
- Parameters:
- [IN] bChar - the character to be drawn.
- [OUT] lpSurface - the destination surface.
- [IN] pos - the destination location of the surface.
- [IN] bColor - the color of the character.
- Return value:
- None.
- --*/
- {
- int i, j, dx;
- int x = PAL_X(pos), y = PAL_Y(pos);
- LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
- //
- // Check for NULL pointer.
- //
- if (lpSurface == NULL)
- {
- return;
- }
- //
- // Draw the character to the surface.
- //
- y *= lpSurface->pitch;
- for (i = 0; i < 15; i++)
- {
- dx = x;
- for (j = 0; j < 8; j++)
- {
- if (pChar[i] & (1 << j))
- {
- ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
- }
- dx++;
- }
- y += lpSurface->pitch;
- }
- }
- #endif
|