newgrf_text.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_text.cpp 15718 2009-03-15 00:32:18Z rubidium $ */
00002 
00012 #include "stdafx.h"
00013 #include "newgrf.h"
00014 #include "strings_func.h"
00015 #include "newgrf_storage.h"
00016 #include "string_func.h"
00017 #include "date_type.h"
00018 
00019 #include "table/strings.h"
00020 #include "table/control_codes.h"
00021 
00022 #define GRFTAB  28
00023 #define TABSIZE 11
00024 
00030 enum GRFBaseLanguages {
00031   GRFLB_AMERICAN    = 0x01,
00032   GRFLB_ENGLISH     = 0x02,
00033   GRFLB_GERMAN      = 0x04,
00034   GRFLB_FRENCH      = 0x08,
00035   GRFLB_SPANISH     = 0x10,
00036   GRFLB_GENERIC     = 0x80,
00037 };
00038 
00039 enum GRFExtendedLanguages {
00040   GRFLX_AMERICAN    = 0x00,
00041   GRFLX_ENGLISH     = 0x01,
00042   GRFLX_GERMAN      = 0x02,
00043   GRFLX_FRENCH      = 0x03,
00044   GRFLX_SPANISH     = 0x04,
00045   GRFLX_UNSPECIFIED = 0x7F,
00046 };
00047 
00053 struct GRFText {
00054   public:
00055     static GRFText* New(byte langid, const char* text)
00056     {
00057       return new(strlen(text) + 1) GRFText(langid, text);
00058     }
00059 
00060   private:
00061     GRFText(byte langid_, const char* text_) : next(NULL), langid(langid_)
00062     {
00063       strcpy(text, text_);
00064     }
00065 
00066     void *operator new(size_t size, size_t extra)
00067     {
00068       return ::operator new(size + extra);
00069     }
00070 
00071 public:
00072     /* dummy operator delete to silence VC8:
00073      * 'void *GRFText::operator new(size_t,size_t)' : no matching operator delete found;
00074      *     memory will not be freed if initialization throws an exception */
00075     void operator delete(void *p, size_t extra)
00076     {
00077       return ::operator delete(p);
00078     }
00079 
00080   public:
00081     GRFText *next;
00082     byte langid;
00083     char text[VARARRAY_SIZE];
00084 };
00085 
00086 
00092 struct GRFTextEntry {
00093   uint32 grfid;
00094   uint16 stringid;
00095   StringID def_string;
00096   GRFText *textholder;
00097 };
00098 
00099 
00100 static uint _num_grf_texts = 0;
00101 static GRFTextEntry _grf_text[(1 << TABSIZE) * 3];
00102 static byte _currentLangID = GRFLX_ENGLISH;  
00103 
00104 
00105 char *TranslateTTDPatchCodes(uint32 grfid, const char *str)
00106 {
00107   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
00108   char *d = tmp;
00109   bool unicode = false;
00110   WChar c;
00111   size_t len = Utf8Decode(&c, str);
00112 
00113   if (c == 0x00DE) {
00114     /* The thorn ('รพ') indicates a unicode string to TTDPatch */
00115     unicode = true;
00116     str += len;
00117   }
00118 
00119   for (;;) {
00120     if (unicode && Utf8EncodedCharLen(*str) != 0) {
00121       c = Utf8Consume(&str);
00122       /* 'Magic' range of control codes. */
00123       if (GB(c, 8, 8) == 0xE0) c = GB(c, 0, 8);
00124     } else {
00125       c = (byte)*str++;
00126     }
00127     if (c == 0) break;
00128 
00129     switch (c) {
00130       case 0x01:
00131         d += Utf8Encode(d, SCC_SETX);
00132         *d++ = *str++;
00133         break;
00134       case 0x0A: break;
00135       case 0x0D: *d++ = 0x0A; break;
00136       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
00137       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
00138       case 0x1F:
00139         d += Utf8Encode(d, SCC_SETXY);
00140         *d++ = *str++;
00141         *d++ = *str++;
00142         break;
00143       case 0x7B:
00144       case 0x7C:
00145       case 0x7D:
00146       case 0x7E:
00147       case 0x7F:
00148       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
00149       case 0x81: {
00150         StringID string;
00151         string  = ((uint8)*str++);
00152         string |= ((uint8)*str++) << 8;
00153         d += Utf8Encode(d, SCC_STRING_ID);
00154         d += Utf8Encode(d, MapGRFStringID(grfid, string));
00155         break;
00156       }
00157       case 0x82:
00158       case 0x83:
00159       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_SPEED + c - 0x82); break;
00160       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
00161       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
00162       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_LITRES);  break;
00163       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
00164       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
00165       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
00166       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
00167       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
00168       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
00169       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
00170       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
00171       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
00172       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
00173       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
00174       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
00175       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
00176       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
00177       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
00178       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
00179       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
00180       case 0x9A:
00181         switch (*str++) {
00182           case 0: // FALL THROUGH
00183           case 1:
00184             d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY);
00185             break;
00186           case 3: {
00187             uint16 tmp  = ((uint8)*str++);
00188             tmp        |= ((uint8)*str++) << 8;
00189             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
00190             d += Utf8Encode(d, tmp);
00191           } break;
00192           case 4:
00193             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
00194             d += Utf8Encode(d, *str++);
00195             break;
00196           case 6:
00197             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);
00198             break;
00199           case 7:
00200             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);
00201             break;
00202           case 8:
00203             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);
00204             break;
00205 
00206           default:
00207             grfmsg(1, "missing handler for extended format code");
00208             break;
00209         }
00210         break;
00211 
00212       case 0x9E: d += Utf8Encode(d, 0x20AC); break; // Euro
00213       case 0x9F: d += Utf8Encode(d, 0x0178); break; // Y with diaeresis
00214       case 0xA0: d += Utf8Encode(d, SCC_UPARROW); break;
00215       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW); break;
00216       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK); break;
00217       case 0xAD: d += Utf8Encode(d, SCC_CROSS); break;
00218       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW); break;
00219       case 0xB4: d += Utf8Encode(d, SCC_TRAIN); break;
00220       case 0xB5: d += Utf8Encode(d, SCC_LORRY); break;
00221       case 0xB6: d += Utf8Encode(d, SCC_BUS); break;
00222       case 0xB7: d += Utf8Encode(d, SCC_PLANE); break;
00223       case 0xB8: d += Utf8Encode(d, SCC_SHIP); break;
00224       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
00225       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW); break;
00226       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
00227       default:
00228         /* Validate any unhandled character */
00229         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00230         d += Utf8Encode(d, c);
00231         break;
00232     }
00233   }
00234 
00235   *d = '\0';
00236   tmp = ReallocT(tmp, strlen(tmp) + 1);
00237   return tmp;
00238 }
00239 
00240 
00244 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
00245 {
00246   char *translatedtext;
00247   uint id;
00248 
00249   /* When working with the old language scheme (grf_version is less than 7) and
00250    * English or American is among the set bits, simply add it as English in
00251    * the new scheme, i.e. as langid = 1.
00252    * If English is set, it is pretty safe to assume the translations are not
00253    * actually translated.
00254    */
00255   if (!new_scheme) {
00256     if (HASBITS(langid_to_add, GRFLB_AMERICAN | GRFLB_ENGLISH)) {
00257       langid_to_add = GRFLX_ENGLISH;
00258     } else {
00259       StringID ret = STR_EMPTY;
00260       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
00261       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
00262       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
00263       return ret;
00264     }
00265   }
00266 
00267   for (id = 0; id < _num_grf_texts; id++) {
00268     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00269       break;
00270     }
00271   }
00272 
00273   /* Too many strings allocated, return empty */
00274   if (id == lengthof(_grf_text)) return STR_EMPTY;
00275 
00276   translatedtext = TranslateTTDPatchCodes(grfid, text_to_add);
00277 
00278   GRFText *newtext = GRFText::New(langid_to_add, translatedtext);
00279 
00280   free(translatedtext);
00281 
00282   /* If we didn't find our stringid and grfid in the list, allocate a new id */
00283   if (id == _num_grf_texts) _num_grf_texts++;
00284 
00285   if (_grf_text[id].textholder == NULL) {
00286     _grf_text[id].grfid      = grfid;
00287     _grf_text[id].stringid   = stringid;
00288     _grf_text[id].def_string = def_string;
00289     _grf_text[id].textholder = newtext;
00290   } else {
00291     GRFText **ptext, *text;
00292     bool replaced = false;
00293 
00294     /* Loop through all languages and see if we can replace a string */
00295     for (ptext = &_grf_text[id].textholder; (text = *ptext) != NULL; ptext = &text->next) {
00296       if (text->langid != langid_to_add) continue;
00297       newtext->next = text->next;
00298       *ptext = newtext;
00299       delete text;
00300       replaced = true;
00301       break;
00302     }
00303 
00304     /* If a string wasn't replaced, then we must append the new string */
00305     if (!replaced) *ptext = newtext;
00306   }
00307 
00308   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
00309 
00310   return (GRFTAB << TABSIZE) + id;
00311 }
00312 
00313 /* Used to remember the grfid that the last retrieved string came from */
00314 static uint32 _last_grfid = 0;
00315 
00319 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
00320 {
00321   uint id;
00322 
00323   /* grfid is zero when we're being called via an include */
00324   if (grfid == 0) grfid = _last_grfid;
00325 
00326   for (id = 0; id < _num_grf_texts; id++) {
00327     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00328       return (GRFTAB << TABSIZE) + id;
00329     }
00330   }
00331 
00332   return STR_UNDEFINED;
00333 }
00334 
00335 
00336 const char *GetGRFStringPtr(uint16 stringid)
00337 {
00338   const GRFText *default_text = NULL;
00339   const GRFText *search_text;
00340 
00341   assert(_grf_text[stringid].grfid != 0);
00342 
00343   /* Remember this grfid in case the string has included text */
00344   _last_grfid = _grf_text[stringid].grfid;
00345 
00346   /* Search the list of lang-strings of this stringid for current lang */
00347   for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
00348     if (search_text->langid == _currentLangID) {
00349       return search_text->text;
00350     }
00351 
00352     /* If the current string is English or American, set it as the
00353      * fallback language if the specific language isn't available. */
00354     if (search_text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (search_text->langid == GRFLX_ENGLISH || search_text->langid == GRFLX_AMERICAN))) {
00355       default_text = search_text;
00356     }
00357   }
00358 
00359   /* If there is a fallback string, return that */
00360   if (default_text != NULL) return default_text->text;
00361 
00362   /* Use the default string ID if the fallback string isn't available */
00363   return GetStringPtr(_grf_text[stringid].def_string);
00364 }
00365 
00374 void SetCurrentGrfLangID(byte language_id)
00375 {
00376   _currentLangID = language_id;
00377 }
00378 
00379 bool CheckGrfLangID(byte lang_id, byte grf_version)
00380 {
00381   if (grf_version < 7) {
00382     switch (_currentLangID) {
00383       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
00384       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
00385       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
00386       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
00387     }
00388   }
00389 
00390   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
00391 }
00392 
00397 void CleanUpStrings()
00398 {
00399   uint id;
00400 
00401   for (id = 0; id < _num_grf_texts; id++) {
00402     GRFText *grftext = _grf_text[id].textholder;
00403     while (grftext != NULL) {
00404       GRFText *grftext2 = grftext->next;
00405       delete grftext;
00406       grftext = grftext2;
00407     }
00408     _grf_text[id].grfid      = 0;
00409     _grf_text[id].stringid   = 0;
00410     _grf_text[id].textholder = NULL;
00411   }
00412 
00413   _num_grf_texts = 0;
00414 }
00415 
00416 struct TextRefStack {
00417   byte stack[0x30];
00418   byte position;
00419   bool used;
00420 
00421   TextRefStack() : used(false) {}
00422 
00423   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
00424   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
00425 
00426   uint16 PopUnsignedWord()
00427   {
00428     uint16 val = this->PopUnsignedByte();
00429     return val | (this->PopUnsignedByte() << 8);
00430   }
00431   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
00432 
00433   uint32 PopUnsignedDWord()
00434   {
00435     uint32 val = this->PopUnsignedWord();
00436     return val | (this->PopUnsignedWord() << 16);
00437   }
00438   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
00439 
00440   uint64 PopUnsignedQWord()
00441   {
00442     uint64 val = this->PopUnsignedDWord();
00443     return val | (((uint64)this->PopUnsignedDWord()) << 32);
00444   }
00445   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
00446 
00448   void RotateTop4Words()
00449   {
00450     byte tmp[2];
00451     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
00452     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
00453     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
00454   }
00455 
00456   void PushWord(uint16 word)
00457   {
00458     if (this->position >= 2) {
00459       this->position -= 2;
00460     } else {
00461       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
00462         this->stack[i] = this->stack[i - 2];
00463       }
00464     }
00465     this->stack[this->position]     = GB(word, 0, 8);
00466     this->stack[this->position + 1] = GB(word, 8, 8);
00467   }
00468 
00469   void ResetStack()  { this->position = 0; this->used = true; }
00470   void RewindStack() { this->position = 0; }
00471 };
00472 
00473 static TextRefStack _newgrf_normal_textrefstack;
00474 static TextRefStack _newgrf_error_textrefstack;
00475 
00477 static TextRefStack *_newgrf_textrefstack = &_newgrf_normal_textrefstack;
00478 
00483 void PrepareTextRefStackUsage(byte numEntries)
00484 {
00485   extern TemporaryStorageArray<uint32, 0x110> _temp_store;
00486 
00487   _newgrf_textrefstack->ResetStack();
00488 
00489   byte *p = _newgrf_textrefstack->stack;
00490   for (uint i = 0; i < numEntries; i++) {
00491     for (uint j = 0; j < 32; j += 8) {
00492       *p = GB(_temp_store.Get(0x100 + i), j, 8);
00493       p++;
00494     }
00495   }
00496 }
00497 
00499 void StopTextRefStackUsage() { _newgrf_textrefstack->used = false; }
00500 
00501 void SwitchToNormalRefStack()
00502 {
00503   _newgrf_textrefstack = &_newgrf_normal_textrefstack;
00504 }
00505 
00506 void SwitchToErrorRefStack()
00507 {
00508   _newgrf_textrefstack = &_newgrf_error_textrefstack;
00509 }
00510 
00511 void RewindTextRefStack()
00512 {
00513   _newgrf_textrefstack->RewindStack();
00514 }
00515 
00522 uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv)
00523 {
00524   if (_newgrf_textrefstack->used) {
00525     switch (scc) {
00526       default: NOT_REACHED();
00527       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
00528       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
00529       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack->PopUnsignedQWord(); break;
00530 
00531       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
00532       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack->PopSignedDWord();   break;
00533 
00534       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack->PopUnsignedByte();  break;
00535       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack->PopUnsignedDWord(); break;
00536 
00537       case SCC_NEWGRF_PRINT_HEX_WORD:
00538       case SCC_NEWGRF_PRINT_WORD_SPEED:
00539       case SCC_NEWGRF_PRINT_WORD_LITRES:
00540       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack->PopUnsignedWord();  break;
00541 
00542       case SCC_NEWGRF_PRINT_DATE:
00543       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
00544 
00545       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
00546 
00547       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
00548       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
00549       case SCC_NEWGRF_UNPRINT:              *buff -= Utf8Consume(str); break;
00550 
00551       case SCC_NEWGRF_PRINT_STRING_ID:
00552         *argv = _newgrf_textrefstack->PopUnsignedWord();
00553         if (*argv == STR_NULL) *argv = STR_EMPTY;
00554         break;
00555     }
00556   }
00557 
00558   switch (scc) {
00559     default: NOT_REACHED();
00560     case SCC_NEWGRF_PRINT_DWORD:
00561     case SCC_NEWGRF_PRINT_SIGNED_WORD:
00562     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
00563     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
00564       return SCC_COMMA;
00565 
00566     case SCC_NEWGRF_PRINT_HEX_BYTE:
00567     case SCC_NEWGRF_PRINT_HEX_WORD:
00568     case SCC_NEWGRF_PRINT_HEX_DWORD:
00569       return SCC_HEX;
00570 
00571     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
00572     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
00573       return SCC_CURRENCY;
00574 
00575     case SCC_NEWGRF_PRINT_STRING_ID:
00576       return SCC_STRING1;
00577 
00578     case SCC_NEWGRF_PRINT_DATE:
00579       return SCC_DATE_LONG;
00580 
00581     case SCC_NEWGRF_PRINT_MONTH_YEAR:
00582       return SCC_DATE_TINY;
00583 
00584     case SCC_NEWGRF_PRINT_WORD_SPEED:
00585       return SCC_VELOCITY;
00586 
00587     case SCC_NEWGRF_PRINT_WORD_LITRES:
00588       return SCC_VOLUME;
00589 
00590     case SCC_NEWGRF_DISCARD_WORD:
00591     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
00592     case SCC_NEWGRF_PUSH_WORD:
00593     case SCC_NEWGRF_UNPRINT:
00594       return 0;
00595   }
00596 }

Generated on Mon May 11 15:48:05 2009 for OpenTTD by  doxygen 1.5.6