newgrf_text.cpp

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

Generated on Sun Nov 15 15:40:13 2009 for OpenTTD by  doxygen 1.5.6