newgrf_text.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_text.cpp 16423 2009-05-25 17:15:15Z 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) {
00124         c = GB(c, 0, 8);
00125       } else if (c >= 0x20) {
00126         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00127         d += Utf8Encode(d, c);
00128         continue;
00129       }
00130     } else {
00131       c = (byte)*str++;
00132     }
00133     if (c == 0) break;
00134 
00135     switch (c) {
00136       case 0x01:
00137         d += Utf8Encode(d, SCC_SETX);
00138         *d++ = *str++;
00139         break;
00140       case 0x0A: break;
00141       case 0x0D: *d++ = 0x0A; break;
00142       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
00143       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
00144       case 0x1F:
00145         d += Utf8Encode(d, SCC_SETXY);
00146         *d++ = *str++;
00147         *d++ = *str++;
00148         break;
00149       case 0x7B:
00150       case 0x7C:
00151       case 0x7D:
00152       case 0x7E:
00153       case 0x7F:
00154       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
00155       case 0x81: {
00156         StringID string;
00157         string  = ((uint8)*str++);
00158         string |= ((uint8)*str++) << 8;
00159         d += Utf8Encode(d, SCC_STRING_ID);
00160         d += Utf8Encode(d, MapGRFStringID(grfid, string));
00161         break;
00162       }
00163       case 0x82:
00164       case 0x83:
00165       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_SPEED + c - 0x82); break;
00166       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
00167       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
00168       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_LITRES);  break;
00169       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
00170       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
00171       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
00172       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
00173       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
00174       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
00175       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
00176       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
00177       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
00178       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
00179       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
00180       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
00181       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
00182       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
00183       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
00184       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
00185       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
00186       case 0x9A:
00187         switch (*str++) {
00188           case 0: // FALL THROUGH
00189           case 1:
00190             d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY);
00191             break;
00192           case 3: {
00193             uint16 tmp  = ((uint8)*str++);
00194             tmp        |= ((uint8)*str++) << 8;
00195             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
00196             d += Utf8Encode(d, tmp);
00197           } break;
00198           case 4:
00199             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
00200             d += Utf8Encode(d, *str++);
00201             break;
00202           case 6:
00203             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);
00204             break;
00205           case 7:
00206             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);
00207             break;
00208           case 8:
00209             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);
00210             break;
00211 
00212           default:
00213             grfmsg(1, "missing handler for extended format code");
00214             break;
00215         }
00216         break;
00217 
00218       case 0x9E: d += Utf8Encode(d, 0x20AC); break; // Euro
00219       case 0x9F: d += Utf8Encode(d, 0x0178); break; // Y with diaeresis
00220       case 0xA0: d += Utf8Encode(d, SCC_UPARROW); break;
00221       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW); break;
00222       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK); break;
00223       case 0xAD: d += Utf8Encode(d, SCC_CROSS); break;
00224       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW); break;
00225       case 0xB4: d += Utf8Encode(d, SCC_TRAIN); break;
00226       case 0xB5: d += Utf8Encode(d, SCC_LORRY); break;
00227       case 0xB6: d += Utf8Encode(d, SCC_BUS); break;
00228       case 0xB7: d += Utf8Encode(d, SCC_PLANE); break;
00229       case 0xB8: d += Utf8Encode(d, SCC_SHIP); break;
00230       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
00231       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW); break;
00232       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
00233       default:
00234         /* Validate any unhandled character */
00235         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00236         d += Utf8Encode(d, c);
00237         break;
00238     }
00239   }
00240 
00241   *d = '\0';
00242   tmp = ReallocT(tmp, strlen(tmp) + 1);
00243   return tmp;
00244 }
00245 
00246 
00250 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
00251 {
00252   char *translatedtext;
00253   uint id;
00254 
00255   /* When working with the old language scheme (grf_version is less than 7) and
00256    * English or American is among the set bits, simply add it as English in
00257    * the new scheme, i.e. as langid = 1.
00258    * If English is set, it is pretty safe to assume the translations are not
00259    * actually translated.
00260    */
00261   if (!new_scheme) {
00262     if (HASBITS(langid_to_add, GRFLB_AMERICAN | GRFLB_ENGLISH)) {
00263       langid_to_add = GRFLX_ENGLISH;
00264     } else {
00265       StringID ret = STR_EMPTY;
00266       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
00267       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
00268       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
00269       return ret;
00270     }
00271   }
00272 
00273   for (id = 0; id < _num_grf_texts; id++) {
00274     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00275       break;
00276     }
00277   }
00278 
00279   /* Too many strings allocated, return empty */
00280   if (id == lengthof(_grf_text)) return STR_EMPTY;
00281 
00282   translatedtext = TranslateTTDPatchCodes(grfid, text_to_add);
00283 
00284   GRFText *newtext = GRFText::New(langid_to_add, translatedtext);
00285 
00286   free(translatedtext);
00287 
00288   /* If we didn't find our stringid and grfid in the list, allocate a new id */
00289   if (id == _num_grf_texts) _num_grf_texts++;
00290 
00291   if (_grf_text[id].textholder == NULL) {
00292     _grf_text[id].grfid      = grfid;
00293     _grf_text[id].stringid   = stringid;
00294     _grf_text[id].def_string = def_string;
00295     _grf_text[id].textholder = newtext;
00296   } else {
00297     GRFText **ptext, *text;
00298     bool replaced = false;
00299 
00300     /* Loop through all languages and see if we can replace a string */
00301     for (ptext = &_grf_text[id].textholder; (text = *ptext) != NULL; ptext = &text->next) {
00302       if (text->langid != langid_to_add) continue;
00303       newtext->next = text->next;
00304       *ptext = newtext;
00305       delete text;
00306       replaced = true;
00307       break;
00308     }
00309 
00310     /* If a string wasn't replaced, then we must append the new string */
00311     if (!replaced) *ptext = newtext;
00312   }
00313 
00314   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
00315 
00316   return (GRFTAB << TABSIZE) + id;
00317 }
00318 
00319 /* Used to remember the grfid that the last retrieved string came from */
00320 static uint32 _last_grfid = 0;
00321 
00325 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
00326 {
00327   uint id;
00328 
00329   /* grfid is zero when we're being called via an include */
00330   if (grfid == 0) grfid = _last_grfid;
00331 
00332   for (id = 0; id < _num_grf_texts; id++) {
00333     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00334       return (GRFTAB << TABSIZE) + id;
00335     }
00336   }
00337 
00338   return STR_UNDEFINED;
00339 }
00340 
00341 
00342 const char *GetGRFStringPtr(uint16 stringid)
00343 {
00344   const GRFText *default_text = NULL;
00345   const GRFText *search_text;
00346 
00347   assert(_grf_text[stringid].grfid != 0);
00348 
00349   /* Remember this grfid in case the string has included text */
00350   _last_grfid = _grf_text[stringid].grfid;
00351 
00352   /* Search the list of lang-strings of this stringid for current lang */
00353   for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
00354     if (search_text->langid == _currentLangID) {
00355       return search_text->text;
00356     }
00357 
00358     /* If the current string is English or American, set it as the
00359      * fallback language if the specific language isn't available. */
00360     if (search_text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (search_text->langid == GRFLX_ENGLISH || search_text->langid == GRFLX_AMERICAN))) {
00361       default_text = search_text;
00362     }
00363   }
00364 
00365   /* If there is a fallback string, return that */
00366   if (default_text != NULL) return default_text->text;
00367 
00368   /* Use the default string ID if the fallback string isn't available */
00369   return GetStringPtr(_grf_text[stringid].def_string);
00370 }
00371 
00380 void SetCurrentGrfLangID(byte language_id)
00381 {
00382   _currentLangID = language_id;
00383 }
00384 
00385 bool CheckGrfLangID(byte lang_id, byte grf_version)
00386 {
00387   if (grf_version < 7) {
00388     switch (_currentLangID) {
00389       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
00390       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
00391       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
00392       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
00393     }
00394   }
00395 
00396   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
00397 }
00398 
00403 void CleanUpStrings()
00404 {
00405   uint id;
00406 
00407   for (id = 0; id < _num_grf_texts; id++) {
00408     GRFText *grftext = _grf_text[id].textholder;
00409     while (grftext != NULL) {
00410       GRFText *grftext2 = grftext->next;
00411       delete grftext;
00412       grftext = grftext2;
00413     }
00414     _grf_text[id].grfid      = 0;
00415     _grf_text[id].stringid   = 0;
00416     _grf_text[id].textholder = NULL;
00417   }
00418 
00419   _num_grf_texts = 0;
00420 }
00421 
00422 struct TextRefStack {
00423   byte stack[0x30];
00424   byte position;
00425   bool used;
00426 
00427   TextRefStack() : used(false) {}
00428 
00429   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
00430   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
00431 
00432   uint16 PopUnsignedWord()
00433   {
00434     uint16 val = this->PopUnsignedByte();
00435     return val | (this->PopUnsignedByte() << 8);
00436   }
00437   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
00438 
00439   uint32 PopUnsignedDWord()
00440   {
00441     uint32 val = this->PopUnsignedWord();
00442     return val | (this->PopUnsignedWord() << 16);
00443   }
00444   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
00445 
00446   uint64 PopUnsignedQWord()
00447   {
00448     uint64 val = this->PopUnsignedDWord();
00449     return val | (((uint64)this->PopUnsignedDWord()) << 32);
00450   }
00451   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
00452 
00454   void RotateTop4Words()
00455   {
00456     byte tmp[2];
00457     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
00458     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
00459     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
00460   }
00461 
00462   void PushWord(uint16 word)
00463   {
00464     if (this->position >= 2) {
00465       this->position -= 2;
00466     } else {
00467       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
00468         this->stack[i] = this->stack[i - 2];
00469       }
00470     }
00471     this->stack[this->position]     = GB(word, 0, 8);
00472     this->stack[this->position + 1] = GB(word, 8, 8);
00473   }
00474 
00475   void ResetStack()  { this->position = 0; this->used = true; }
00476   void RewindStack() { this->position = 0; }
00477 };
00478 
00479 static TextRefStack _newgrf_normal_textrefstack;
00480 static TextRefStack _newgrf_error_textrefstack;
00481 
00483 static TextRefStack *_newgrf_textrefstack = &_newgrf_normal_textrefstack;
00484 
00489 void PrepareTextRefStackUsage(byte numEntries)
00490 {
00491   extern TemporaryStorageArray<uint32, 0x110> _temp_store;
00492 
00493   _newgrf_textrefstack->ResetStack();
00494 
00495   byte *p = _newgrf_textrefstack->stack;
00496   for (uint i = 0; i < numEntries; i++) {
00497     for (uint j = 0; j < 32; j += 8) {
00498       *p = GB(_temp_store.Get(0x100 + i), j, 8);
00499       p++;
00500     }
00501   }
00502 }
00503 
00505 void StopTextRefStackUsage() { _newgrf_textrefstack->used = false; }
00506 
00507 void SwitchToNormalRefStack()
00508 {
00509   _newgrf_textrefstack = &_newgrf_normal_textrefstack;
00510 }
00511 
00512 void SwitchToErrorRefStack()
00513 {
00514   _newgrf_textrefstack = &_newgrf_error_textrefstack;
00515 }
00516 
00517 void RewindTextRefStack()
00518 {
00519   _newgrf_textrefstack->RewindStack();
00520 }
00521 
00528 uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv)
00529 {
00530   if (_newgrf_textrefstack->used) {
00531     switch (scc) {
00532       default: NOT_REACHED();
00533       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
00534       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
00535       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack->PopUnsignedQWord(); break;
00536 
00537       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
00538       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack->PopSignedDWord();   break;
00539 
00540       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack->PopUnsignedByte();  break;
00541       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack->PopUnsignedDWord(); break;
00542 
00543       case SCC_NEWGRF_PRINT_HEX_WORD:
00544       case SCC_NEWGRF_PRINT_WORD_SPEED:
00545       case SCC_NEWGRF_PRINT_WORD_LITRES:
00546       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack->PopUnsignedWord();  break;
00547 
00548       case SCC_NEWGRF_PRINT_DATE:
00549       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
00550 
00551       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
00552 
00553       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
00554       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
00555       case SCC_NEWGRF_UNPRINT:              *buff -= Utf8Consume(str); break;
00556 
00557       case SCC_NEWGRF_PRINT_STRING_ID:
00558         *argv = _newgrf_textrefstack->PopUnsignedWord();
00559         if (*argv == STR_NULL) *argv = STR_EMPTY;
00560         break;
00561     }
00562   }
00563 
00564   switch (scc) {
00565     default: NOT_REACHED();
00566     case SCC_NEWGRF_PRINT_DWORD:
00567     case SCC_NEWGRF_PRINT_SIGNED_WORD:
00568     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
00569     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
00570       return SCC_COMMA;
00571 
00572     case SCC_NEWGRF_PRINT_HEX_BYTE:
00573     case SCC_NEWGRF_PRINT_HEX_WORD:
00574     case SCC_NEWGRF_PRINT_HEX_DWORD:
00575       return SCC_HEX;
00576 
00577     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
00578     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
00579       return SCC_CURRENCY;
00580 
00581     case SCC_NEWGRF_PRINT_STRING_ID:
00582       return SCC_STRING1;
00583 
00584     case SCC_NEWGRF_PRINT_DATE:
00585       return SCC_DATE_LONG;
00586 
00587     case SCC_NEWGRF_PRINT_MONTH_YEAR:
00588       return SCC_DATE_TINY;
00589 
00590     case SCC_NEWGRF_PRINT_WORD_SPEED:
00591       return SCC_VELOCITY;
00592 
00593     case SCC_NEWGRF_PRINT_WORD_LITRES:
00594       return SCC_VOLUME;
00595 
00596     case SCC_NEWGRF_DISCARD_WORD:
00597     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
00598     case SCC_NEWGRF_PUSH_WORD:
00599     case SCC_NEWGRF_UNPRINT:
00600       return 0;
00601   }
00602 }

Generated on Sun Sep 13 08:19:17 2009 for OpenTTD by  doxygen 1.5.6