iso88598.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #include <config.h>
  17. #include "iso88598.h"
  18. #include "univalues.h"
  19. #define ISO88598_HEB_ALEF 0xE0
  20. #define ISO88598_HEB_TAV 0xFA
  21. // The following 7 codes were taken from FriBiDi's fribidi_char_sets_iso8859_8.c.
  22. // These are "proposed extensions to ISO-8859-8."
  23. #define ISO88598_LRM 0xFD
  24. #define ISO88598_RLM 0xFE
  25. #define ISO88598_LRE 0xFB
  26. #define ISO88598_RLE 0xFC
  27. #define ISO88598_PDF 0xDD
  28. #define ISO88598_LRO 0xDB
  29. #define ISO88598_RLO 0xDC
  30. // int unicode_to_iso88598(unichar ch)
  31. //
  32. // This is a fallback function used for saving files when no iconv
  33. // implementation is present.
  34. //
  35. // It returns EOF when the unicode character can't be represented in
  36. // ISO-8859-9.
  37. int unicode_to_iso88598(unichar ch)
  38. {
  39. if (ch <= 0xA0)
  40. return ch;
  41. if (ch >= UNI_HEB_ALEF && ch <= UNI_HEB_TAV)
  42. return ISO88598_HEB_ALEF + (ch - UNI_HEB_ALEF);
  43. switch (ch) {
  44. case 0x00D7: return 0xAA; // MULTIPLICATION SIGN
  45. case 0x00F7: return 0xBA; // DIVISION SIGN
  46. case 0x2017: return 0xDF; // DOUBLE LOW LINE
  47. }
  48. if (ch >= 0xA2 && ch <= 0xBE)
  49. return ch;
  50. // The following are non-standard conversions.
  51. switch (ch) {
  52. case UNI_LRM: return ISO88598_LRM;
  53. case UNI_RLM: return ISO88598_RLM;
  54. case UNI_LRE: return ISO88598_LRE;
  55. case UNI_RLE: return ISO88598_RLE;
  56. case UNI_PDF: return ISO88598_PDF;
  57. case UNI_LRO: return ISO88598_LRO;
  58. case UNI_RLO: return ISO88598_RLO;
  59. }
  60. return EOF;
  61. }
  62. // unichar iso88598_to_unicode(unsigned char ch)
  63. //
  64. // This is a fallback function used for loading files when no iconv
  65. // implementation is present.
  66. //
  67. // It returns the Unicode Replacement Character when it encounters a
  68. // character which is illegal in ISO-8859-9.
  69. unichar iso88598_to_unicode(unsigned char ch)
  70. {
  71. if (ch <= 0xA0)
  72. return ch;
  73. if (ch >= ISO88598_HEB_ALEF && ch <= ISO88598_HEB_TAV)
  74. return UNI_HEB_ALEF + (ch - ISO88598_HEB_ALEF);
  75. switch (ch) {
  76. case 0xAA: return 0x00D7; // MULTIPLICATION SIGN
  77. case 0xBA: return 0x00F7; // DIVISION SIGN
  78. case 0xDF: return 0x2017; // DOUBLE LOW LINE
  79. }
  80. if (ch >= 0xA2 && ch <= 0xBE)
  81. return ch;
  82. // The following are non-standard conversions.
  83. switch (ch) {
  84. case ISO88598_LRM: return UNI_LRM;
  85. case ISO88598_RLM: return UNI_RLM;
  86. case ISO88598_LRE: return UNI_LRE;
  87. case ISO88598_RLE: return UNI_RLE;
  88. case ISO88598_PDF: return UNI_PDF;
  89. case ISO88598_LRO: return UNI_LRO;
  90. case ISO88598_RLO: return UNI_RLO;
  91. }
  92. return UNI_REPLACEMENT;
  93. }