📄 font.c
字号:
/*
* @(#)font.c
*
* Copyright 1997-1999, Wes Cherry (mailto:wesc@technosis.com)
* 2000-2001, Aaron Ardiri (mailto:aaron@ardiri.com)
* All rights reserved.
*
* 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 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, please write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Revisions:
* ==========
*
* pre 18-Jun-2000 <numerous developers>
* creation
* 18-Jun-2000 Aaron Ardiri
* GNU GPL documentation additions
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <ctype.h>
#include "RCP2C.h"
#include "util.h"
/*
* Header definitions and token types
*/
#define h_fontType 0 /* font type */
#define h_firstChar 1 /* ASCII code of first character */
#define h_lastChar 2 /* ASCII code of last character */
#define h_maxWidth 3 /* maximum character width */
#define h_kernMax 4 /* negative of maximum character kern */
#define h_nDescent 5 /* negative of descent */
#define h_fRectWidth 6 /* width of font rectangle */
#define h_fRectHeight 7 /* height of font rectangle */
#define h_owTLoc 8 /* offset to offset/width table */
#define h_ascent 9 /* ascent */
#define h_descent 10 /* descent */
#define h_leading 11 /* leading */
#define h_rowWords 12 /* row width of bit image in words (16bits) */
#define g_glyph 20 /* new glyph token */
#define g_offset 21 /* offset token */
#define g_width 22 /* width token */
#define g_bitmap 99 /* bitmap data */
/*
* some foreign font handing function
*/
int (*pfnChkCode) (unsigned char *cp,
int *pdx);
/*
* Some globals to keep track of things for error reporting
*/
static char *filename;
static unsigned int lineno;
int vfontType;
/*
* Global to hold the font widths and offsets
*/
typedef struct
{
char offset;
char width;
}
FontCharInfoType;
FontCharInfoType *fntOW[256];
unsigned int fntH[256];
int
IsDBCSNone(unsigned char *cp,
int *pdxChar)
{
return 0;
}
/*-----------------------------------------------------------------------------
| IsBIG5
|
| Check the double byte char is BIG5 coded
-------------------------------------------------------------DLIN------------*/
int
IsBIG5(unsigned char *cp,
int *pdxChar)
{
/*
* BIG-5 code rule
* first byte range 0x81..0xfe (high byte)
* second byte range 0x40..0x7e, 0xa1..0xfe (low byte)
*
* default symbols range 0xa140..0xa3bf
* often used word range 0xa440..0xc67e
* seldom 0xc940..0xf9d5
*
* full typed 0..9 0xa2af..0xa2b8
* A..Z 0xa2cf..0xa2e8
* a..z 0xa2e9..0xa343
* sound symbol b..u 0xa374..0xa3ba
* sound level .1234 0xa3bb..0xa3bf
*
* full typed space 0xa140
* cama 0xa141
*
*/
if ((*cp >= 0x81 && *cp <= 0xfe)
&& ((*(cp + 1) >= 0x40 && *(cp + 1) <= 0x7e)
|| (*(cp + 1) >= 0xa1 && *(cp + 1) <= 0xfe)))
{
*pdxChar = 13;
return 2;
}
return 0;
}
/*-----------------------------------------------------------------------------
| IsJapanese
|
| Check the double byte char is Japanese coded
-------------------------------------------------------------DLIN------------*/
int
IsJapanese(unsigned char *cp,
int *pdxChar)
{
if ((*cp >= 0xa1 && *cp <= 0xdf))
{
*pdxChar = 5;
return 1;
}
else
if ((((*cp >= 0x81) && (*cp <= 0x9f)) ||
((*cp >= 0xe0) && (*cp <= 0xef))) &&
(((*(cp + 1) >= 0x40) && (*(cp + 1) <= 0x7e)) ||
((*(cp + 1) >= 0x80) && (*(cp + 1) <= 0xfc))))
{
*pdxChar = 9; /* not sure about this */
return 2;
}
return 0;
}
/*-----------------------------------------------------------------------------
| IsKoreanHanme
|
| Check the double byte char is Korean coded(Large Font, for HanMe)
-----------------------------------------------------------------------------*/
int
IsKoreanHanme(unsigned char *cp,
int *pdxChar)
{
/*
* Korean code rule (by JaeMok Jeong)
* first byte range 0xb0..0xc8 (high byte)
* second byte range 0xa1..0xfe (low byte)
*/
if ((*cp >= 0xb0 && *cp <= 0xc8)
&& (*(cp + 1) >= 0xa1 && *(cp + 1) <= 0xfe))
{
*pdxChar = 11; /* not sure about this, hanme font width */
return 2;
}
return 0;
}
/*-----------------------------------------------------------------------------
| IsKoreanHantip
|
| Check the double byte char is Korean coded(Small Font, for Hantiip)
-----------------------------------------------------------------------------*/
int
IsKoreanHantip(unsigned char *cp,
int *pdxChar)
{
/*
* Korean code rule (by JaeMok Jeong)
* first byte range 0xb0..0xc8 (high byte)
* second byte range 0xa1..0xfe (low byte)
*/
if ((*cp >= 0xb0 && *cp <= 0xc8)
&& (*(cp + 1) >= 0xa1 && *(cp + 1) <= 0xfe))
{
*pdxChar = 8; /* hantip font width = 8 */
return 2;
}
return 0;
}
/*
* Report an error with line number and filename
*/
static void
ErrorX(char *s)
{
char er[512];
sprintf(er, "ERROR: %s, line %u, %s", s, lineno, filename);
Error(er);
}
/*
* Report a warning with line number and filename
*/
static void
WarningX(char *s)
{
fprintf(stderr, "WARNING: %s, line %u, %s", s, lineno, filename);
}
/*
* Parse out a line and return it's token and value
*/
static void
ParseLine(char *s,
int *token,
int *value)
{
size_t i, len = strlen(s);
char *s1;
*token = g_bitmap;
*value = 0;
/*
* first check for normal "token value" line
*/
for (i = 0; i < len; i++)
{
s[i] = toupper(s[i]);
if (s[i] == ' ' || s[i] == 9)
{
s[i] = 0;
if (!strcmp(s, "FONTTYPE"))
*token = h_fontType;
if (!strcmp(s, "MAXWIDTH"))
*token = h_maxWidth;
if (!strcmp(s, "KERNMAX"))
*token = h_kernMax;
if (!strcmp(s, "NDESCENT"))
*token = h_nDescent;
if (!strcmp(s, "FRECTWIDTH"))
*token = h_fRectWidth;
if (!strcmp(s, "FRECTHEIGHT"))
*token = h_fRectHeight;
if (!strcmp(s, "ASCENT"))
*token = h_ascent;
if (!strcmp(s, "DESCENT"))
*token = h_descent;
if (!strcmp(s, "LEADING"))
*token = h_leading;
if (!strcmp(s, "GLYPH"))
*token = g_glyph;
if (!strcmp(s, "OFFSET"))
*token = g_offset;
if (!strcmp(s, "WIDTH"))
*token = g_width;
if (*token == g_bitmap)
ErrorX("Invalid Token");
break;
}
}
/*
* determe the value
*/
if (*token != g_bitmap)
{
i++;
if ((len - i) == 3 && s[i] == 39 && s[i + 2] == 39)
*value = s[i + 1];
else
{
*value = strtol(s + i, &s1, 10);
if (s1[0] != 0)
ErrorX("Invalid Value");
}
}
}
/*
* Ugly routine to call after every glyph
*/
static void
CloseGlyph(int *header,
int *width,
int *row,
int *col,
int autoWidth,
int autoRectWidth)
{
if (*width > header[h_maxWidth])
{
if (autoWidth)
header[h_maxWidth] = *width;
else
WarningX("Width exceeds maxWidth definition");
}
if (*width > header[h_fRectWidth])
{
if (autoRectWidth)
header[h_fRectWidth] = *width;
else
WarningX("Width exceeds fRectWidth definition");
}
if (!header[h_fRectHeight])
header[h_fRectHeight] = *row;
if (!(*row) || header[h_fRectHeight] != *row)
ErrorX("Invalid height on previous glyph");
*row = 0;
*col += *width;
}
/*
* The main function of this module
*/
void
DumpFont(char *pchFileName,
int fntNo)
{
#define kArraySize 257 /* RMa add 1 for handle missing char (256 +1) */
FILE *in;
char s[kArraySize], *s1, *bitmap[kArraySize];
unsigned short int coltable[kArraySize];
size_t x;
int token, value, header[13];
int curChar = -1;
int autoWidth = 1;
int autoRectWidth = 1;
int row = 0;
int col = 0;
int width = 0;
unsigned short bit[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
memset(header, 0, sizeof(header));
memset(bitmap, 0, sizeof(bitmap));
filename = FindAndOpenFile(pchFileName, "rt", &in);
lineno = 0;
if (fntOW[fntNo])
free(fntOW[fntNo]);
if (!(fntOW[fntNo] = malloc(kArraySize * sizeof(FontCharInfoType))))
Error("Out of memory");
memset(fntOW[fntNo], -1, kArraySize * sizeof(FontCharInfoType));
while (fgets(s, sizeof(s), in))
{
lineno++;
/*
* Remove leading and trailing whitespace
*/
for (s1 = s; s1[0] == ' ' || s1[0] == 9; s1++) ;
for (x = strlen(s1); x; x--)
{
if (s1[x - 1] == ' ' || s1[x - 1] == 9 || s1[x - 1] == 10
|| s1[x - 1] == 13)
s1[x - 1] = 0;
else
break;
}
if (s1[0] && (s[0] != 47 || s[1] != 47))
{ /* skip blank lines and comment lines */
ParseLine(s1, &token, &value);
if (token < g_glyph)
{
if (curChar >= 0)
ErrorX("Header must precede glyphs");
header[token] = value;
if (token == h_maxWidth)
autoWidth = 0;
if (token == h_fRectWidth)
autoRectWidth = 0;
}
/*
* Do new char processing
*/
if (token == g_glyph)
{
if (curChar >= 0)
CloseGlyph(header, &width, &row, &col, autoWidth, autoRectWidth);
if (value == -1)
{
/*
* Handle missing char
*/
curChar++;
fntOW[fntNo][curChar].offset = 0;
coltable[curChar] = col;
continue;
}
if (value < 0 || value > 255)
ErrorX("Glyph number out of range");
if (value <= curChar)
ErrorX("Glyph out of sequence");
for (x = curChar + 1; x <= (size_t) value; x++)
coltable[x] = col;
if (curChar < 0)
header[h_firstChar] = value;
curChar = value;
header[h_lastChar] = value;
fntOW[fntNo][curChar].offset = 0;
continue;
}
if (token == g_offset)
{
if (curChar < 0)
ErrorX("Unexpected OFFSET token");
fntOW[fntNo][curChar].offset = value;
continue;
}
if (token == g_width)
{
if (curChar < 0)
ErrorX("Unexpected WIDTH token");
fntOW[fntNo][curChar].width = value;
continue;
}
if (token == g_bitmap)
{
if (row == 0)
{
width = strlen(s1);
if (fntOW[fntNo][curChar].width == -1)
fntOW[fntNo][curChar].width = width;
}
else if (width != (int)strlen(s1))
ErrorX("Invalid width");
if (!bitmap[row])
{
if (!(bitmap[row] = malloc(1024)))
Error("Out of memory");
memset(bitmap[row], 0, 1024);
}
for (x = 0; x < (size_t) width; x++)
{
if (s1[x] != '-' && s1[x] != '.')
{
bitmap[row][(col + x) >> 3] |= bit[(col + x) & 0x7];
}
}
if (++row > 255)
ErrorX("Too many rows");
continue;
}
}
}
if (!feof(in))
Error2("Error reading file: ", filename);
fclose(in);
/*
* Write the data
*/
if (curChar < 0)
ErrorX("No glyphs");
CloseGlyph(header, &width, &row, &col, autoWidth, autoRectWidth);
fntH[fntNo] = header[h_fRectHeight];
header[h_rowWords] = (col + 15) / 16;
header[h_owTLoc] =
header[h_rowWords] * header[h_fRectHeight] + 8 + header[h_lastChar] -
header[h_firstChar];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -