hedgewars/uStore.pas
changeset 2677 83ad68ceef72
parent 2674 2fce032f2f95
child 2679 b61d25fa6c53
--- 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;