Non-hacked version of CJK handling. Should switch to CJK rendering only if a particular string needs it, instead of based on locale file.
authornemo
Tue, 05 Jan 2010 21:26:38 +0000
changeset 2677 83ad68ceef72
parent 2676 03bf7a96400a
child 2678 334016e8d895
Non-hacked version of CJK handling. Should switch to CJK rendering only if a particular string needs it, instead of based on locale file.
hedgewars/hwengine.pas
hedgewars/uChat.pas
hedgewars/uConsts.pas
hedgewars/uStore.pas
--- a/hedgewars/hwengine.pas	Tue Jan 05 19:20:44 2010 +0000
+++ b/hedgewars/hwengine.pas	Tue Jan 05 21:26:38 2010 +0000
@@ -633,9 +633,6 @@
 begin
 WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
 GetParams;
-// FIXME -  hack in font with support for CJK
-if (cLocaleFName = 'zh_CN.txt') or (cLocaleFName = 'zh_TW.txt') or (cLocaleFName = 'ja.txt') then
-    Fontz:= FontzCJK;
 
 Randomize;
 
--- a/hedgewars/uChat.pas	Tue Jan 05 19:20:44 2010 +0000
+++ b/hedgewars/uChat.pas	Tue Jan 05 21:26:38 2010 +0000
@@ -59,6 +59,7 @@
 var strSurface, resSurface: PSDL_Surface;
 	w, h: LongInt;
 	color: TSDL_Color;
+    font: THWFont;
 begin
 if cl.Tex <> nil then
 	FreeTexture(cl.Tex);
@@ -81,12 +82,12 @@
 	delete(str, 1, 1)
 end;
 
-
-TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h);
+font:= CheckCJKFont(str, fnt16);
+TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), w, h);
 
 resSurface:= SDL_CreateRGBSurface(0, toPowerOf2(w), toPowerOf2(h), 32, RMask, GMask, BMask, AMask);
 
-strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), color);
+strSurface:= TTF_RenderUTF8_Solid(Fontz[font].Handle, Str2PChar(str), color);
 cl.Width:= w + 4;
 SDL_UpperBlit(strSurface, nil, resSurface, nil);
 SDL_FreeSurface(strSurface);
--- a/hedgewars/uConsts.pas	Tue Jan 05 19:20:44 2010 +0000
+++ b/hedgewars/uConsts.pas	Tue Jan 05 21:26:38 2010 +0000
@@ -110,7 +110,7 @@
 			amRCPlane, amLowGravity, amExtraDamage, amInvulnerable, amExtraTime,
 			amLaserSight, amVampiric, amSniperRifle, amJetpack, amMolotov);
 
-	THWFont = (fnt16, fntBig, fntSmall);
+	THWFont = (fnt16, fntBig, fntSmall, CJKfnt16, CJKfntBig, CJKfntSmall);
 
 	TCapGroup = (capgrpGameState, capgrpAmmoinfo, capgrpVolume,
 			capgrpMessage, capgrpAmmostate);
@@ -370,10 +370,7 @@
 			(Handle: nil;
 			Height: 10;
 			style: TTF_STYLE_NORMAL;
-			Name: 'DejaVuSans-Bold.ttf')
-			);
-
-	FontzCJK: array[THWFont] of THHFont = (
+			Name: 'DejaVuSans-Bold.ttf'),
 			(Handle: nil;
 			Height: 12;
 			style: TTF_STYLE_NORMAL;
--- a/hedgewars/uStore.pas	Tue Jan 05 19:20:44 2010 +0000
+++ b/hedgewars/uStore.pas	Tue Jan 05 21:26:38 2010 +0000
@@ -45,6 +45,7 @@
 procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
 procedure DrawFillRect(r: TSDL_Rect);
+function  CheckCJKFont(s: string; font: THWFont): THWFont;
 function  RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture;
 function  RenderSpeechBubbleTex(s: string; SpeechType: Longword; font: THWFont): PTexture;
 procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
@@ -753,11 +754,40 @@
 FreeTexture(HHTexture)
 end;
 
+
+function CheckCJKFont(s: string; font: THWFont): THWFont;
+var l, i, u : LongInt;
+    tmpstr: array[0..256] of WideChar;
+begin
+if font >= CJKfntSmall then exit(font);
+
+l:= Utf8ToUnicode(@tmpstr, Str2PChar(s), 255);
+i:= 0;
+while i < l do
+    begin
+    u:= LongInt(tmpstr[i]);
+    //AddFileLog(IntToStr(u));
+    if (($2E80  <= u) and (u >= $2FDF))  or // CJK Radicals Supplement / Kangxi Radicals
+       (($2FF0  <= u) and (u >= $303F))  or // Ideographic Description Characters / CJK Radicals Supplement
+       (($31C0  <= u) and (u >= $31EF))  or // CJK Strokes
+       (($3200  <= u) and (u >= $4DBF))  or // Enclosed CJK Letters and Months / CJK Compatibility / CJK Unified Ideographs Extension A
+       (($4E00  <= u) and (u >= $9FFF))  or // CJK Unified Ideographs
+       (($F900  <= u) and (u >= $FAFF))  or // CJK Compatibility Ideographs
+       (($FE30  <= u) and (u >= $FE4F))  or // CJK Compatibility Forms
+       (($20000 <= u) and (u >= $2A6DF)) or // CJK Unified Ideographs Extension B
+       (($2F800 <= u) and (u >= $2FA1F))    // CJK Compatibility Ideographs Supplement
+       then exit(THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) ));
+    inc(i)
+    end;
+exit(font);
+end;
+
 function  RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture;
-var w, h: LongInt;
+var w, h : LongInt;
     Result: PSDL_Surface;
 begin
 if length(s) = 0 then s:= ' ';
+font:= CheckCJKFont(s, font);
 TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h);
 
 Result:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2,
@@ -812,6 +842,7 @@
 numLines:= 0;
 
 if length(s) = 0 then s:= '...';
+font:= CheckCJKFont(s, font);
 TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h);
 if w<8 then w:= 8;
 j:= 0;