# -*- coding: utf-8 -*-

# Good enough for us:
#   http://en.wikipedia.org/wiki/Mapping_of_Unicode_character_planes
#   http://www.unicode.org/Public/UNIDATA/Blocks.txt
# More specific:
#   http://www.unicode.org/Public/UNIDATA/Scripts.txt
# Also:
#   http://www.unicode.org/cldr/data/charts/supplemental/languages_and_scripts.html
#   http://www.unicode.org/cldr/data/charts/supplemental/language_territory_information.html
#
#

CHARACTER_PLANE_MAPPING = {
  0x0000..0x007F      => [ "Latin",                                   "Latn",       "Basic Latin",                                  ],
  0x0080..0x00FF      => [ "Latin",                                   "Latn",       "Latin-1 Supplement",                           ],
  0x0100..0x017F      => [ "Latin",                                   "Latn",       "Latin Extended-A",                             ],
  0x0180..0x024F      => [ "Latin",                                   "Latn",       "Latin Extended-B",                             ],
  0x0250..0x02AF      => [ "(Phonetic)",                              "_ipa",       "IPA Extensions",                               ],
  0x02B0..0x02FF      => [ "(Symbols)",                               "_sym",       "Spacing Modifier Letters",                     ],
  0x0300..0x036F      => [ "(Diacriticals)",                          "_dia",       "Combining Diacritical Marks",                  ],
  0x0370..0x03FF      => [ "Greek",                                   "Grek",       "Greek and Coptic",                             ], #### !! Combined
  0x0400..0x04FF      => [ "Cyrillic",                                "Cyrl",       "Cyrillic",                                     ],
  0x0500..0x052F      => [ "Cyrillic",                                "Cyrl",       "Cyrillic Supplement",                          ],
  0x0530..0x058F      => [ "Armenian",                                "Armn",       "Armenian",                                     ],
  0x0590..0x05FF      => [ "Hebrew",                                  "Hebr",       "Hebrew",                                       ],
  0x0600..0x06FF      => [ "Arabic",                                  "Arab",       "Arabic",                                       ],
  0x0700..0x074F      => [ "Syriac",                                  "Syrc",       "Syriac",                                       ],
  0x0750..0x077F      => [ "Arabic",                                  "Arab",       "Arabic Supplement",                            ],
  0x0780..0x07BF      => [ "Thaana",                                  "Thaa",       "Thaana",                                       ],
  0x07C0..0x07FF      => [ "N'Ko",                                    "Nkoo",       "N'Ko (Mandenkan)",                             ],
  0x0900..0x097F      => [ "Devanagari",                              "Deva",       "Devanagari",                                   ],   # Indic scripts:
  0x0980..0x09FF      => [ "Bengali",                                 "Beng",       "Bengali",                                      ],
  0x0A00..0x0A7F      => [ "Gurmukhi",                                "Guru",       "Gurmukhi",                                     ],
  0x0A80..0x0AFF      => [ "Gujarati",                                "Gujr",       "Gujarati",                                     ],
  0x0B00..0x0B7F      => [ "Oriya",                                   "Orya",       "Oriya",                                        ],
  0x0B80..0x0BFF      => [ "Tamil",                                   "Taml",       "Tamil",                                        ],
  0x0C00..0x0C7F      => [ "Telugu",                                  "Telu",       "Telugu",                                       ],
  0x0C80..0x0CFF      => [ "Kannada",                                 "Knda",       "Kannada",                                      ],
  0x0D00..0x0D7F      => [ "Malayalam",                               "Mlym",       "Malayalam",                                    ],
  0x0D80..0x0DFF      => [ "Sinhala",                                 "Sinh",       "Sinhala",                                      ],
  0x0E00..0x0E7F      => [ "Thai",                                    "Thai",       "Thai",                                         ], #
  0x0E80..0x0EFF      => [ "Lao",                                     "Laoo",       "Lao",                                          ],
  0x0F00..0x0FFF      => [ "Tibetan",                                 "Tibt",       "Tibetan",                                      ],
  0x1000..0x109F      => [ "Myanmar",                                 "Mymr",       "Burmese (Myanmar)",                            ],
  0x10A0..0x10FF      => [ "Georgian",                                "Geor",       "Georgian",                                     ],
  0x1100..0x11FF      => [ "Korean",                                  "Kore",       "Hangul Jamo",                                  ],
  0x1200..0x137F      => [ "Ethiopic",                                "Ethi",       "Ethiopic",                                     ],
  0x1380..0x139F      => [ "Ethiopic",                                "Ethi",       "Ethiopic Supplement",                          ],
  0x13A0..0x13FF      => [ "Cherokee",                                "Cher",       "Cherokee",                                     ],
  0x1400..0x167F      => [ "Unified Canadian Aboriginal Syllabics",   "Cans",       "Unified Canadian Aboriginal Syllabics",        ],
  0x1680..0x169F      => [ "Ogham",                                   "Ogam",       "Ogham",                                        ],
  0x16A0..0x16FF      => [ "Runic",                                   "Runr",       "Runic",                                        ],
  0x1700..0x171F      => [ "Tagalog",                                 "Tglg",       "Tagalog",                                      ],  # Philippine scripts:
  0x1720..0x173F      => [ "Hanunoo",                                 "Hano",       "Hanunóo",                                      ],
  0x1740..0x175F      => [ "Buhid",                                   "Buhd",       "Buhid",                                        ],
  0x1760..0x177F      => [ "Tagbanwa",                                "Tagb",       "Tagbanwa",                                     ],
  0x1780..0x17FF      => [ "Khmer",                                   "Khmr",       "Khmer",                                        ], #
  0x1800..0x18AF      => [ "Mongolian",                               "Mong",       "Mongolian",                                    ],
  0x1900..0x194F      => [ "Limbu",                                   "Limb",       "Limbu",                                        ],
  0x1950..0x197F      => [ "Tai Le",                                  "Tale",       "Tai Le",                                       ],
  0x1980..0x19DF      => [ "New Tai Lue",                             "Talu",       "New Tai Lue",                                  ],
  0x19E0..0x19FF      => [ "Khmer",                                   "Khmr",       "Khmer Symbols",                                ],
  0x1A00..0x1A1F      => [ "Buginese",                                "Bugi",       "Buginese",                                     ],
  0x1B00..0x1B7F      => [ "Balinese",                                "Bali",       "Balinese",                                     ],
  0x1B80..0x1BBF      => [ "Sundanese",                               "Sund",       "Sundanese",                                    ],
  0x1C00..0x1C4F      => [ "Lepcha",                                  "Lepc",       "Lepcha (Rong)",                                ],
  0x1C50..0x1C7F      => [ "Ol Chiki",                                "Olck",       "Ol Chiki (Santali / Ol Cemet’)",               ],
  0x1D00..0x1D7F      => [ "(Phonetic)",                              "_ipa",       "Phonetic Extensions",                          ],
  0x1D80..0x1DBF      => [ "(Phonetic)",                              "_ipa",       "Phonetic Extensions Supplement",               ],
  0x1DC0..0x1DFF      => [ "(Diacriticals)",                          "_dia",       "Combining Diacritical Marks Supplement",       ],
  0x1E00..0x1EFF      => [ "Latin",                                   "Latn",       "Latin Extended Additional",                    ],
  0x1F00..0x1FFF      => [ "Greek",                                   "Grek",       "Greek Extended",                               ],
  0x2000..0x206F      => [ "(Symbols)",                               "_sym",       "General Punctuation",                          ],  # Symbols:
  0x2070..0x209F      => [ "(Symbols)",                               "_sym",       "Superscripts and Subscripts",                  ],
  0x20A0..0x20CF      => [ "(Symbols)",                               "_sym",       "Currency Symbols",                             ],
  0x20D0..0x20FF      => [ "(Symbols)",                               "_sym",       "Combining Diacritical Marks for Symbols",      ],
  0x2100..0x214F      => [ "(Symbols)",                               "_sym",       "Letterlike Symbols",                           ],
  0x2150..0x218F      => [ "(Numbers)",                               "_num",       "Number Forms",                                 ],
  0x2190..0x21FF      => [ "(Drawing)",                               "_drw",       "Arrows",                                       ],
  0x2200..0x22FF      => [ "(Mathematical)",                          "_mth",       "Mathematical Operators",                       ],
  0x2300..0x23FF      => [ "(Mathematical)",                          "_mth",       "Miscellaneous Technical",                      ],
  0x2400..0x243F      => [ "(Symbols)",                               "_sym",       "Control Pictures",                             ],
  0x2440..0x245F      => [ "(Symbols)",                               "_sym",       "Optical Character Recognition",                ],
  0x2460..0x24FF      => [ "Latin",                                   "Latn",       "Enclosed Alphanumerics",                       ],
  0x2500..0x257F      => [ "(Drawing)",                               "_drw",       "Box Drawing",                                  ],
  0x2580..0x259F      => [ "(Drawing)",                               "_drw",       "Block Elements",                               ],
  0x25A0..0x25FF      => [ "(Drawing)",                               "_drw",       "Geometric Shapes",                             ],
  0x2600..0x26FF      => [ "(Mathematical)",                          "_mth",       "Miscellaneous Symbols",                        ],
  0x2700..0x27BF      => [ "(Symbols)",                               "_sym",       "Dingbats",                                     ],
  0x27C0..0x27EF      => [ "(Mathematical)",                          "_mth",       "Miscellaneous Mathematical Symbols-A",         ],
  0x27F0..0x27FF      => [ "(Drawing)",                               "_drw",       "Supplemental Arrows-A",                        ],
  0x2800..0x28FF      => [ "(Braille)",                               "_brl",       "Braille Patterns",                             ],
  0x2900..0x297F      => [ "(Drawing)",                               "_drw",       "Supplemental Arrows-B",                        ],
  0x2980..0x29FF      => [ "(Mathematical)",                          "_mth",       "Miscellaneous Mathematical Symbols-B",         ],
  0x2A00..0x2AFF      => [ "(Mathematical)",                          "_mth",       "Supplemental Mathematical Operators",          ],
  0x2B00..0x2BFF      => [ "(Mathematical)",                          "_mth",       "Miscellaneous Symbols and Arrows",             ],
  0x2C00..0x2C5F      => [ "Glagolitic",                              "Glag",       "Glagolitic",                                   ],  #
  0x2C60..0x2C7F      => [ "Latin",                                   "Latn",       "Latin Extended-C",                             ],
  0x2C80..0x2CFF      => [ "Coptic",                                  "Copt",       "Coptic",                                       ],
  0x2D00..0x2D2F      => [ "Georgian",                                "Geor",       "Georgian Supplement",                          ],
  0x2D30..0x2D7F      => [ "Tifinagh",                                "Tfng",       "Tifinagh",                                     ],
  0x2D80..0x2DDF      => [ "Ethiopic",                                "Ethi",       "Ethiopic Extended",                            ],
  0x2DE0..0x2DFF      => [ "Cyrillic",                                "Cyrl",       "Cyrillic Extended-A",                          ],
  0x2E00..0x2E7F      => [ "(Symbols)",                               "_sym",       "Supplemental Punctuation",                     ],
  0x2E80..0x2EFF      => [ "CJK",                                     "_cjk",       "CJK Radicals Supplement",                      ],
  0x2F00..0x2FDF      => [ "CJK",                                     "_cjk",       "Kangxi Radicals",                              ],
  0x2FF0..0x2FFF      => [ "CJK",                                     "_cjk",       "Ideographic Description Characters",           ],
  0x3000..0x303F      => [ "CJK",                                     "_cjk",       "CJK Symbols and Punctuation",                  ],
  0x3040..0x309F      => [ "Hiragana",                                "_hir",       "Hiragana",                                     ],
  0x30A0..0x30FF      => [ "Katakana",                                "Kana",       "Katakana",                                     ],  ###
  0x3100..0x312F      => [ "Bopomofo",                                "Bopo",       "Bopomofo",                                     ],
  0x3130..0x318F      => [ "Korean",                                  "Kore",       "Hangul Compatibility Jamo",                    ],
  0x3190..0x319F      => [ "(Kanbun)",                                "_kbn",       "Kanbun",                                       ],
  0x31A0..0x31BF      => [ "Bopomofo",                                "Bopo",       "Bopomofo Extended",                            ],
  0x31C0..0x31EF      => [ "CJK",                                     "_cjk",       "CJK Strokes",                                  ],
  0x31F0..0x31FF      => [ "Katakana",                                "Kana",       "Katakana Phonetic Extensions",                 ],
  0x3200..0x32FF      => [ "CJK",                                     "_cjk",       "Enclosed CJK Letters and Months",              ],
  0x3300..0x33FF      => [ "CJK",                                     "_cjk",       "CJK Compatibility",                            ],
  0x3400..0x4DBF      => [ "CJK",                                     "_cjk",       "CJK Unified Ideographs Extension A",           ],
  0x4DC0..0x4DFF      => [ "(Symbols)",                               "_sym",       "Yijing Hexagram Symbols",                      ],
  0x4E00..0x9FFF      => [ "CJK",                                     "_cjk",       "CJK Unified Ideographs",                       ],
  0xA000..0xA48F      => [ "Yi",                                      "Yiii",       "Yi Syllables",                                 ],
  0xA490..0xA4CF      => [ "Yi",                                      "Yiii",       "Yi Radicals",                                  ],
  0xA500..0xA63F      => [ "Vai",                                     "Vaii",       "Vai",                                          ],
  0xA640..0xA69F      => [ "Cyrillic",                                "Cyrl",       "Cyrillic Extended-B",                          ],
  0xA700..0xA71F      => [ "(Miscellaneous)",                         "_msc",       "Modifier Tone Letters",                        ],
  0xA720..0xA7FF      => [ "Latin",                                   "Latn",       "Latin Extended-D",                             ],
  0xA800..0xA82F      => [ "Syloti Nagri",                            "Sylo",       "Syloti Nagri",                                 ],
  0xA840..0xA87F      => [ "Phags-pa",                                "Phag",       "Phags-pa",                                     ],
  0xA880..0xA8DF      => [ "Saurashtra",                              "Saur",       "Saurashtra",                                   ],
  0xA900..0xA92F      => [ "Kayah Li",                                "Kali",       "Kayah Li",                                     ],
  0xA930..0xA95F      => [ "Rejang",                                  "Rjng",       "Rejang",                                       ],
  0xAA00..0xAA5F      => [ "Cham",                                    "Cham",       "Cham",                                         ],
  0xAC00..0xD7AF      => [ "Korean",                                  "Kore",       "Hangul Syllables",                             ],
  0xD800..0xDB7F      => [ "(Private)",                               "_prv",       "High Surrogates",                              ],
  0xDB80..0xDBFF      => [ "(Private)",                               "_prv",       "High Private Use Surrogates",                  ],
  0xDC00..0xDFFF      => [ "(Private)",                               "_prv",       "Low Surrogates",                               ],
  0xE000..0xF8FF      => [ "(Private)",                               "_prv",       "Private Use Area",                             ],
  0xF900..0xFAFF      => [ "CJK",                                     "_cjk",       "CJK Compatibility Ideographs",                 ],
  0xFB00..0xFB4F      => [ "(Miscellaneous)",                         "_msc",       "Alphabetic Presentation Forms",                ],
  0xFB50..0xFDFF      => [ "Arabic",                                  "Arab",       "Arabic Presentation Forms-A",                  ],
  0xFE00..0xFE0F      => [ "(Miscellaneous)",                         "_msc",       "Variation Selectors",                          ],
  0xFE10..0xFE1F      => [ "(Miscellaneous)",                         "_msc",       "Vertical Forms",                               ],
  0xFE20..0xFE2F      => [ "(Miscellaneous)",                         "_msc",       "Combining Half Marks",                         ],
  0xFE30..0xFE4F      => [ "CJK",                                     "_cjk",       "CJK Compatibility Forms",                      ],
  0xFE50..0xFE6F      => [ "(Miscellaneous)",                         "_msc",       "Small Form Variants",                          ],
  0xFE70..0xFEFF      => [ "Arabic",                                  "Arab",       "Arabic Presentation Forms-B",                  ],
  0xFF00..0xFFEF      => [ "(Miscellaneous)",                         "_msc",       "Halfwidth and Fullwidth Forms",                ],
  0xFFF0..0xFFFF      => [ "(Miscellaneous)",                         "_msc",       "Specials",                                     ],

  0x10000..0x1007F    => [ "Linear B",                                "Linb",       "Linear B Syllabary",                           ],
  0x10080..0x100FF    => [ "Linear B",                                "Linb",       "Linear B Ideograms",                           ],
  0x10100..0x1013F    => [ "(Classical)",                             "_cls",       "Aegean Numbers",                               ],
  0x10140..0x1018F    => [ "(Classical)",                             "_cls",       "Ancient Greek Numbers",                        ],
  0x10190..0x101CF    => [ "(Classical)",                             "_cls",       "Ancient Symbols",                              ],
  0x101D0..0x101FF    => [ "(Classical)",                             "_cls",       "Phaistos Disc",                                ],
  0x10280..0x1029F    => [ "Lycian",                                  "Lyci",       "Lycian",                                       ],
  0x102A0..0x102DF    => [ "Carian",                                  "Cari",       "Carian",                                       ],
  0x10300..0x1032F    => [ "Old Italic",                              "Ital",       "Old Italic",                                   ],
  0x10330..0x1034F    => [ "Gothic",                                  "Goth",       "Gothic",                                       ],
  0x10380..0x1039F    => [ "Ugaritic",                                "Ugar",       "Ugaritic",                                     ],
  0x103A0..0x103DF    => [ "Old Persian",                             "Xpeo",       "Old Persian",                                  ],
  0x10400..0x1044F    => [ "Deseret",                                 "Dsrt",       "Deseret",                                      ],
  0x10450..0x1047F    => [ "Shavian",                                 "Shaw",       "Shavian",                                      ],
  0x10480..0x104AF    => [ "Osmanya",                                 "Osma",       "Osmanya",                                      ],
  0x10800..0x1083F    => [ "Cypriot",                                 "Cprt",       "Cypriot Syllabary",                            ],
  0x10900..0x1091F    => [ "Phoenician",                              "Phnx",       "Phoenician",                                   ],
  0x10920..0x1093F    => [ "Lydian",                                  "Lydi",       "Lydian",                                       ],
  0x10A00..0x10A5F    => [ "Kharoshthi",                              "Khar",       "Kharoshthi",                                   ],
  0x12000..0x123FF    => [ "Sumero-Akkadian Cuneiform",               "Xsux",       "Cuneiform",                                    ],
  0x12400..0x1247F    => [ "Sumero-Akkadian Cuneiform",               "Xsux",       "Cuneiform Numbers and Punctuation",            ],
  0x1D000..0x1D0FF    => [ "(Classical)",                             "_cls",       "Byzantine Musical Symbols",                    ],
  0x1D100..0x1D1FF    => [ "(Musical)",                               "_mus",       "Musical Symbols",                              ],
  0x1D200..0x1D24F    => [ "(Classical)",                             "_cls",       "Ancient Greek Musical Notation",               ],
  0x1D300..0x1D35F    => [ "(Classical)",                             "_cls",       "Tai Xuan Jing Symbols",                        ],
  0x1D360..0x1D37F    => [ "(Classical)",                             "_cls",       "Counting Rod Numerals",                        ],
  0x1D400..0x1D7FF    => [ "(Mathematical)",                          "_mth",       "Mathematical Alphanumeric Symbols",            ],
  0x1F000..0x1F02F    => [ "(Symbols)",                               "_sym",       "Mahjong Tiles",                                ],
  0x1F030..0x1F09F    => [ "(Symbols)",                               "_sym",       "Domino Tiles",                                 ],
  0x20000..0x2A6DF    => [ "CJK",                                     "_cjk",       "CJK Unified Ideographs Extension B",           ],
  0x2F800..0x2FA1F    => [ "CJK",                                     "_cjk",       "CJK Compatibility Ideographs Supplement",      ],
  0xE0000..0xE007F    => [ "(Miscellaneous)",                         "_msc",       "Tags",                                         ],
  0xE0100..0xE01EF    => [ "(Miscellaneous)",                         "_msc",       "Variation Selectors Supplement",               ],
  0xF0000..0xFFFFF    => [ "(Private)",                               "_prv",       "Supplementary Private Use Area-A",             ],
  0x100000..0x10FFFF  => [ "(Private)",                               "_prv",       "Supplementary Private Use Area-B",             ],

}

#
# Find the Unicode plane for the character
#
def find_entity_plane(entity_num, freq, entity_name)
  plane_info = CHARACTER_PLANE_MAPPING.find{|plane, info| plane.include?(entity_num) }
  if !plane_info
    warn_missing_info :plane, entity_num, freq, entity_name
    plane_info = ['', '', '', ]
  end
  plane_info
end
