| 1 | /* |
| 2 | * Copyright (C) 2007,2014 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 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 | * |
| 14 | * Author: Steve Ratcliffe |
| 15 | * Create date: 14-Jan-2007 |
| 16 | */ |
| 17 | package uk.me.parabola.imgfmt.app.labelenc; |
| 18 | |
| 19 | import java.text.Normalizer; |
| 20 | import java.util.Locale; |
| 21 | |
| 22 | /** |
| 23 | * Format according to the '6 bit' .img format. The text is first upper |
| 24 | * cased. Any letter with a diacritic or accent is replaced with its base |
| 25 | * letter. |
| 26 | * |
| 27 | * For example Körnerstraße would become KORNERSTRASSE, |
| 28 | * Řípovská would become RIPOVSKA etc. |
| 29 | * |
| 30 | * I believe that some Garmin units are only capable of showing uppercase |
| 31 | * ascii characters, so this will be the default. |
| 32 | * |
| 33 | * @author Steve Ratcliffe |
| 34 | * @see <a href="http://garmin-img.sf.net">Garmin IMG File Format</a> |
| 35 | */ |
| 36 | public class Format6Encoder extends BaseEncoder implements CharacterEncoder { |
| 37 | |
| 38 | // This is 0x1b is the source document, but the accompanying code uses |
| 39 | // the value 0x1c, which seems to work. |
| 40 | private static final int SYMBOL_SHIFT = 0x1c; |
| 41 | |
| 42 | public static final String LETTERS = |
| 43 | " ABCDEFGHIJKLMNO" + // 0x00-0x0F |
| 44 | "PQRSTUVWXYZxx " + // 0x10-0x1F |
| 45 | "0123456789\u0001\u0002\u0003\u0004\u0005\u0006"; // 0x20-0x2F |
| 46 | |
| 47 | public static final String SYMBOLS = |
| 48 | "@!\"#$%&'()*+,-./" + // 0x00-0x0F |
| 49 | "xxxxxxxxxx:;<=>?" + // 0x10-0x1F |
| 50 | "xxxxxxxxxxx[\\]^_"; // 0x20-0x2F |
| 51 | |
| 52 | private final Transliterator transliterator = new TableTransliterator("ascii"); |
| 53 | |
| 54 | /** |
| 55 | * Encode the text into the 6 bit format. See the class level notes. |
| 56 | * |
| 57 | * @param text The original text, which can contain non-ascii characters. |
| 58 | * @return Encoded form of the text. Only uppercase ascii characters and |
| 59 | * some escape sequences will be present. |
| 60 | */ |
| 61 | public EncodedText encodeText(String text) { |
| 62 | if (text == null || text.isEmpty()) |
| 63 | return NO_TEXT; |
| 64 | String normalisedText = Normalizer.normalize(text, Normalizer.Form.NFC); |
| 65 | String s = transliterator.transliterate(normalisedText).toUpperCase(Locale.ENGLISH); |
| 66 | |
| 67 | // Allocate more than enough space on average for the label. |
| 68 | // if you overdo it then it will waste a lot of space , but |
| 69 | // not enough and there will be an error |
| 70 | byte[] buf = new byte[2 * s.length() + 4]; |
| 71 | int off = 0; |
| 72 | |
| 73 | for (char c : s.toCharArray()) { |
| 74 | |
| 75 | if (c == ' ') { |
| 76 | put6(buf, off++, 0); |
| 77 | } else if (c >= 'A' && c <= 'Z') { |
| 78 | put6(buf, off++, c - 'A' + 1); |
| 79 | } else if (c >= '0' && c <= '9') { |
| 80 | put6(buf, off++, c - '0' + 0x20); |
| 81 | } else if (c == 0x1b || c == 0x1c) { |
| 82 | put6(buf, off++, 0x1b); |
| 83 | put6(buf, off++, c + 0x10); |
| 84 | } else if (c >= 0x1d && c <= 0x1f) { |
| 85 | put6(buf, off++, c); |
| 86 | } else if (c >= 1 && c <= 6) { |
| 87 | // Highway shields |
| 88 | put6(buf, off++, 0x29 + c); |
| 89 | } else { |
| 90 | off = shiftedSymbol(buf, off, c); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | buf = put6(buf, off++, 0xff); |
| 95 | |
| 96 | int len = ((off - 1) * 6) / 8 + 1; |
| 97 | |
| 98 | char[] chars = s.toCharArray(); |
| 99 | return new EncodedText(buf, len, chars); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Certain characters have to be represented by two 6byte quantities. This |
| 104 | * routine sorts these out. |
| 105 | * |
| 106 | * @param buf The buffer to write into. |
| 107 | * @param startOffset The offset to start writing to in the output buffer. |
| 108 | * @param c The character that we are decoding. |
| 109 | * @return The final offset. This will be unchanged if there was nothing |
| 110 | * written because the character does not have any representation. |
| 111 | */ |
| 112 | private int shiftedSymbol(byte[] buf, int startOffset, char c) { |
| 113 | int off = startOffset; |
| 114 | int ind = SYMBOLS.indexOf(c); |
| 115 | if (ind >= 0) { |
| 116 | put6(buf, off++, SYMBOL_SHIFT); |
| 117 | put6(buf, off++, ind); |
| 118 | } |
| 119 | return off; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Each character is packed into 6 bits. This keeps track of everything so |
| 124 | * that the character can be put into the right place in the byte array. |
| 125 | * |
| 126 | * @param buf The buffer to populate. |
| 127 | * @param off The character offset, that is the number of the six bit |
| 128 | * character. |
| 129 | * @param c The character to place. |
| 130 | */ |
| 131 | private byte[] put6(byte[] buf, int off, int c) { |
| 132 | int bitOff = off * 6; |
| 133 | |
| 134 | // The byte offset |
| 135 | int byteOff = bitOff/8; |
| 136 | |
| 137 | // The offset within the byte |
| 138 | int shift = bitOff - 8*byteOff; |
| 139 | |
| 140 | int mask = 0xfc >> shift; |
| 141 | buf[byteOff] |= ((c << 2) >> shift) & mask; |
| 142 | |
| 143 | // IF the shift is greater than two we have to put the rest in the |
| 144 | // next byte. |
| 145 | if (shift > 2) { |
| 146 | mask = 0xfc << (8 - shift); |
| 147 | buf[byteOff + 1] = (byte) (((c << 2) << (8 - shift)) & mask); |
| 148 | } |
| 149 | |
| 150 | return buf; |
| 151 | } |
| 152 | } |