Browse Source

basic unicode bdf support

Pal Lockheart 6 years ago
parent
commit
d3f03aeff0
7 changed files with 123 additions and 11 deletions
  1. 97 8
      font.c
  2. 1 1
      macos/Pal.xcodeproj/project.pbxproj
  3. 2 1
      palcommon.h
  4. 12 0
      text.c
  5. 6 0
      util.c
  6. 3 1
      win32/pal_config.h
  7. 2 0
      winrt/pal_config.h

+ 97 - 8
font.c

@@ -40,6 +40,40 @@ static uint8_t reverseBits(uint8_t x) {
     }
     return y;
 }
+static uint16_t reverseBits16(uint16_t x) {
+   //specified to unifont structure; not common means
+   uint8_t l=reverseBits(x);
+   uint8_t h=reverseBits(x>>8);
+   return h<<8|l;
+}
+
+#ifdef DEBUG
+void dump_font(BYTE *buf,int rows, int cols)
+{
+   for(int row=0;row<rows;row++)
+   {
+      for( int bit=0;bit<cols;bit++)
+         if( buf[row] & (uint8_t)pow(2,bit) )
+            printf("*");
+         else
+            printf(" ");
+      printf("\n");
+   }
+}
+
+void dump_font16(WORD *buf,int rows, int cols)
+{
+   for(int row=0;row<rows;row++)
+   {
+      for( int bit=0;bit<cols;bit++)
+         if( reverseBits16(buf[row]) & (uint16_t)pow(2,bit) )
+            printf("*");
+         else
+            printf(" ");
+      printf("\n");
+   }
+}
+#endif
 
 static void PAL_LoadISOFont(void)
 {
@@ -167,12 +201,13 @@ PAL_LoadUserFont(
 --*/
 {
    char buf[4096];
-   int state = 0;
+   int state = 0, fstate = 0;
    int codepage = -1;
 
    DWORD dwEncoding = 0;
    BYTE bFontGlyph[32] = {0};
    int iCurHeight = 0;
+   int bbw = 0, bbh = 0, bbox, bboy;
 
    FILE *fp = UTIL_OpenFileForMode(pszBdfFileName, "r");
 
@@ -187,27 +222,65 @@ PAL_LoadUserFont(
       {
          if (strncmp(buf, "CHARSET_REGISTRY", 16) == 0)
          {
-            if (strstr(buf, "Big5") != NULL)
+            if (strcasestr(buf, "Big5") != NULL)
             {
                codepage = CP_BIG5;
             }
-            else if (strstr(buf, "BIG5") != NULL)
+            else if (strcasestr(buf, "GBK") != NULL)
             {
-               codepage = CP_BIG5;
+               codepage = CP_GBK;
+            }
+            else if (strcasestr(buf, "ISO10646") != NULL)
+            {
+               codepage = CP_UCS;
             }
             //else if (strstr(buf, "JISX0208") != NULL)
             //
             //  codepage = CP_JISX0208;
             //}
+            else
+            {
+               TerminateOnError("Charset of %s is %s, which is not a supported yet.",pszBdfFileName,buf);
+            }
          }
          else if (strncmp(buf, "ENCODING", 8) == 0)
          {
             dwEncoding = atoi(buf + 8);
          }
+         else if (strncmp(buf, "SIZE", 3) == 0)
+         {
+            int bytes_consumed = 0, bytes_now;
+            int got_size;
+            BOOL got_expected = FALSE;
+            char size[10];
+            sscanf(buf+bytes_consumed,"%s%n",size,&bytes_now);bytes_consumed += bytes_now;
+            while(sscanf(buf+bytes_consumed,"%d%n",&got_size,&bytes_now))
+            {
+               bytes_consumed += bytes_now;
+               if( got_size == 16 )
+                  got_expected = TRUE;
+            }
+            if(!got_expected)
+               TerminateOnError("%s not contains expected font size 16!",pszBdfFileName);
+         }
+         else if (strncmp(buf, "BBX", 3) == 0)
+         {
+            int bytes_consumed = 0, bytes_now;
+            char bbx[10];
+            sscanf(buf+bytes_consumed,"%s%n",bbx,&bytes_now);bytes_consumed += bytes_now;
+            sscanf(buf+bytes_consumed,"%d%n",&bbw,&bytes_now);bytes_consumed += bytes_now;
+            sscanf(buf+bytes_consumed,"%d%n",&bbh,&bytes_now);bytes_consumed += bytes_now;
+            sscanf(buf+bytes_consumed,"%d%n",&bbox,&bytes_now);bytes_consumed += bytes_now;
+            sscanf(buf+bytes_consumed,"%d%n",&bboy,&bytes_now);bytes_consumed += bytes_now;
+         }
          else if (strncmp(buf, "BITMAP", 6) == 0)
          {
             state = 1;
             iCurHeight = 0;
+            for(iCurHeight=0;iCurHeight<bboy;iCurHeight++){
+               bFontGlyph[iCurHeight * 2] = 0;
+               bFontGlyph[iCurHeight * 2 + 1] = 0;
+            }
             memset(bFontGlyph, 0, sizeof(bFontGlyph));
          }
       }
@@ -237,12 +310,28 @@ PAL_LoadUserFont(
          }
          else
          {
-            if (iCurHeight < 16)
+            if (iCurHeight < bbh )
             {
                WORD wCode = strtoul(buf, NULL, 16);
-               bFontGlyph[iCurHeight * 2] = (wCode >> 8);
-               bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
-               iCurHeight++;
+               if(bbw <= 8)
+               {
+                  switch(fstate)
+                  {
+                     case 0:
+                        bFontGlyph[iCurHeight * 2] = wCode;
+                        fstate = 1;
+                        break;
+                     case 1:
+                        bFontGlyph[iCurHeight * 2+1] = wCode;
+                        fstate = 0;
+                        iCurHeight++;
+                        break;
+                  }
+               }else{
+                  bFontGlyph[iCurHeight * 2] = (wCode >> 8);
+                  bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
+                  iCurHeight++;
+               }
             }
          }
       }

+ 1 - 1
macos/Pal.xcodeproj/project.pbxproj

@@ -261,7 +261,7 @@
 		7104FD3A0D772F6300A97E53 /* battle.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = battle.c; sourceTree = "<group>"; };
 		7104FD3B0D772F6300A97E53 /* battle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = battle.h; sourceTree = "<group>"; };
 		7104FD3C0D772F6300A97E53 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = "<group>"; };
-		7104FD3D0D772F6300A97E53 /* font.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = font.c; sourceTree = "<group>"; };
+		7104FD3D0D772F6300A97E53 /* font.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; path = font.c; sourceTree = "<group>"; tabWidth = 3; };
 		7104FD3E0D772F6300A97E53 /* font.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = font.h; sourceTree = "<group>"; };
 		7104FD3F0D772F6300A97E53 /* game.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = game.c; sourceTree = "<group>"; };
 		7104FD400D772F6300A97E53 /* game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = game.h; sourceTree = "<group>"; };

+ 2 - 1
palcommon.h

@@ -115,7 +115,8 @@ typedef enum tagCODEPAGE {
 	//CP_SHIFTJIS = 2,
 	//CP_JISX0208 = 3,
 	CP_MAX = CP_GBK + 1,
-	CP_UTF_8 = CP_MAX + 1
+	CP_UTF_8 = CP_MAX + 1,
+    CP_UCS = CP_UTF_8 + 1,
 } CODEPAGE;
 
 typedef enum tagPALFILE {

+ 12 - 0
text.c

@@ -1556,6 +1556,7 @@ PAL_GetInvalidChar(
 	case CP_GBK:      return 0x3f;
 		//case CP_SHIFTJIS: return 0x30fb;
 	case CP_UTF_8:    return 0x3f;
+	case CP_UCS:      return 0x3f;
 	default:          return 0;
 	}
 }
@@ -1752,6 +1753,10 @@ PAL_MultiByteToWideCharCP(
 				}
 			}
 			break;
+        case CP_UCS:
+            i = mbslength;
+            wlen = mbslength/2;
+            break;
 		default:
 			return -1;
 		}
@@ -1883,6 +1888,13 @@ PAL_MultiByteToWideCharCP(
 				}
 			}
 			break;
+        case CP_UCS:
+            for (i = 0; i < mbslength && wlen < wcslength; i+=2){
+                uint8_t *ptr = (uint8_t*)&wcs[wlen++];
+                *(ptr+1)=mbs[i];
+                *ptr    =mbs[i+1];
+            }
+            break;
 		default:
 			return -1;
 		}

+ 6 - 0
util.c

@@ -324,12 +324,18 @@ TerminateOnError(
 	  SDL_MessageBoxButtonData buttons[2] = { { 0, 0, "Yes" },{ 0, 1, "No" } };
 	  SDL_MessageBoxData mbd = { SDL_MESSAGEBOX_ERROR, gpWindow, "FATAL ERROR", buffer, 2, buttons, NULL };
 	  int btnid;
+#if PAL_HAS_CONFIG_PAGE
 	  sprintf(buffer, "%sLaunch setting dialog on next start?\n", string);
 	  if (SDL_ShowMessageBox(&mbd, &btnid) == 0 && btnid == 0)
 	  {
 		  gConfig.fLaunchSetting = TRUE;
 		  PAL_SaveConfig();
 	  }
+#else
+	  sprintf(buffer, "%s\n", string);
+	  mbd.numbuttons=1;
+	  SDL_ShowMessageBox(&mbd, &btnid);
+#endif
 	  PAL_Shutdown(255);
    }
 #else

+ 3 - 1
win32/pal_config.h

@@ -63,4 +63,6 @@
 #ifndef __MINGW__
 #define strtok_r strtok_s
 #define strncasecmp _strnicmp
-#endif
+#endif
+
+#define strcasestr(a,b) strstr(_strupr((a)),_strupr((b)))

+ 2 - 0
winrt/pal_config.h

@@ -60,6 +60,8 @@
 #define strtok_r strtok_s
 #define strncasecmp _strnicmp
 
+#define strcasestr(a,b) strstr(_strupr((a)),_strupr((b)))
+
 PAL_C_LINKAGE_BEGIN
 
 LPCSTR