| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.lbl; |
| 18 |
|
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.Map; |
| 21 |
|
| 22 |
import uk.me.parabola.imgfmt.MapFailedException; |
| 23 |
import uk.me.parabola.imgfmt.Utils; |
| 24 |
import uk.me.parabola.imgfmt.app.BufferedImgFileWriter; |
| 25 |
import uk.me.parabola.imgfmt.app.Exit; |
| 26 |
import uk.me.parabola.imgfmt.app.ImgFile; |
| 27 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 28 |
import uk.me.parabola.imgfmt.app.Label; |
| 29 |
import uk.me.parabola.imgfmt.app.labelenc.BaseEncoder; |
| 30 |
import uk.me.parabola.imgfmt.app.labelenc.CharacterEncoder; |
| 31 |
import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions; |
| 32 |
import uk.me.parabola.imgfmt.app.labelenc.EncodedText; |
| 33 |
import uk.me.parabola.imgfmt.app.srt.Sort; |
| 34 |
import uk.me.parabola.imgfmt.app.trergn.Subdivision; |
| 35 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 36 |
import uk.me.parabola.log.Logger; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class LBLFile extends ImgFile { |
| 49 |
private static final Logger log = Logger.getLogger(LBLFile.class); |
| 50 |
|
| 51 |
private CharacterEncoder textEncoder = CodeFunctions.getDefaultEncoder(); |
| 52 |
|
| 53 |
private final Map<EncodedText, Label> labelCache = new HashMap<>(); |
| 54 |
|
| 55 |
private final LBLHeader lblHeader = new LBLHeader(); |
| 56 |
|
| 57 |
private final PlacesFile places = new PlacesFile(); |
| 58 |
private Sort sort; |
| 59 |
|
| 60 |
|
| 61 |
private static final int OFFSET_MULTIPLIER = 1; |
| 62 |
|
| 63 |
|
| 64 |
private static final int MAX_LABEL_LEN = 170; |
| 65 |
|
| 66 |
public LBLFile(ImgChannel chan, Sort sort) { |
| 67 |
this.sort = sort; |
| 68 |
lblHeader.setSort(sort); |
| 69 |
lblHeader.setOffsetMultiplier(OFFSET_MULTIPLIER); |
| 70 |
setHeader(lblHeader); |
| 71 |
|
| 72 |
setWriter(new BufferedImgFileWriter(chan, "LBL")); |
| 73 |
|
| 74 |
position((long) LBLHeader.HEADER_LEN + lblHeader.getSortDescriptionLength()); |
| 75 |
|
| 76 |
|
| 77 |
getWriter().put1u(0); |
| 78 |
alignForNext(); |
| 79 |
|
| 80 |
places.init(this, lblHeader.getPlaceHeader()); |
| 81 |
places.setSort(sort); |
| 82 |
labelCache.put(BaseEncoder.NO_TEXT, Label.NULL_OUT_LABEL); |
| 83 |
} |
| 84 |
|
| 85 |
public void write() { |
| 86 |
writeBody(); |
| 87 |
} |
| 88 |
|
| 89 |
public void writePost() { |
| 90 |
|
| 91 |
|
| 92 |
ImgFileWriter writer = getWriter(); |
| 93 |
getHeader().writeHeader(writer); |
| 94 |
|
| 95 |
|
| 96 |
writer.put(Utils.toBytes(sort.getDescription())); |
| 97 |
writer.put1u(0); |
| 98 |
assert writer.position() == LBLHeader.HEADER_LEN + lblHeader.getSortDescriptionLength(); |
| 99 |
} |
| 100 |
|
| 101 |
private void writeBody() { |
| 102 |
|
| 103 |
|
| 104 |
lblHeader.setLabelSize(getWriter().position() - (LBLHeader.HEADER_LEN + lblHeader.getSortDescriptionLength())); |
| 105 |
places.write(getWriter()); |
| 106 |
} |
| 107 |
|
| 108 |
public void setCharacterType(String cs, boolean forceUpper) { |
| 109 |
log.info("encoding type " + cs); |
| 110 |
CodeFunctions cfuncs = CodeFunctions.createEncoderForLBL(cs); |
| 111 |
|
| 112 |
lblHeader.setEncodingType(cfuncs.getEncodingType()); |
| 113 |
textEncoder = cfuncs.getEncoder(); |
| 114 |
if (forceUpper && textEncoder instanceof BaseEncoder) { |
| 115 |
BaseEncoder baseEncoder = (BaseEncoder) textEncoder; |
| 116 |
baseEncoder.setUpperCase(true); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public void setEncoder(int encodingType, int codepage ) { |
| 121 |
CodeFunctions cfuncs = CodeFunctions.createEncoderForLBL(encodingType, codepage); |
| 122 |
|
| 123 |
lblHeader.setEncodingType(cfuncs.getEncodingType()); |
| 124 |
textEncoder = cfuncs.getEncoder(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public Label newLabel(String text) { |
| 135 |
return newLabel(text, true); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public Label newLabel(String text, boolean cutToMaxLen) { |
| 147 |
if (text != null && cutToMaxLen && text.length() > MAX_LABEL_LEN) { |
| 148 |
int trimmedLen; |
| 149 |
try { |
| 150 |
trimmedLen = text.offsetByCodePoints(0, MAX_LABEL_LEN); |
| 151 |
} catch (IndexOutOfBoundsException e) { |
| 152 |
trimmedLen = 0; |
| 153 |
} |
| 154 |
if (trimmedLen > 0) { |
| 155 |
do { |
| 156 |
--trimmedLen; |
| 157 |
if (text.charAt(trimmedLen) != ' ') |
| 158 |
break; |
| 159 |
} while (trimmedLen > 0); |
| 160 |
text = text.substring(0, trimmedLen + 1); |
| 161 |
} |
| 162 |
} |
| 163 |
EncodedText encodedText = textEncoder.encodeText(text); |
| 164 |
|
| 165 |
Label l = labelCache.get(encodedText); |
| 166 |
if (l == null) { |
| 167 |
l = new Label(encodedText.getChars()); |
| 168 |
labelCache.put(encodedText, l); |
| 169 |
|
| 170 |
l.setOffset(getNextLabelOffset()); |
| 171 |
if (encodedText.getLength() > 0) |
| 172 |
getWriter().put(encodedText.getCtext(), 0, encodedText.getLength()); |
| 173 |
|
| 174 |
alignForNext(); |
| 175 |
|
| 176 |
if (l.getOffset() > 0x3fffff) |
| 177 |
throw new MapFailedException("Overflow of LBL section"); |
| 178 |
} |
| 179 |
|
| 180 |
return l; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private void alignForNext() { |
| 189 |
|
| 190 |
while ((getCurrentLabelOffset() & ((1 << OFFSET_MULTIPLIER) - 1)) != 0) |
| 191 |
getWriter().put1u(0); |
| 192 |
} |
| 193 |
|
| 194 |
private int getNextLabelOffset() { |
| 195 |
return getCurrentLabelOffset() >> OFFSET_MULTIPLIER; |
| 196 |
} |
| 197 |
|
| 198 |
private int getCurrentLabelOffset() { |
| 199 |
return position() - (LBLHeader.HEADER_LEN + lblHeader.getSortDescriptionLength()); |
| 200 |
} |
| 201 |
|
| 202 |
public POIRecord createPOI(String name) { |
| 203 |
return places.createPOI(name); |
| 204 |
} |
| 205 |
|
| 206 |
public POIRecord createExitPOI(String name, Exit exit) { |
| 207 |
return places.createExitPOI(name, exit); |
| 208 |
} |
| 209 |
|
| 210 |
public void createPOIIndex(String name, int poiIndex, Subdivision group, int type) { |
| 211 |
places.createPOIIndex(name, poiIndex, group, type); |
| 212 |
} |
| 213 |
|
| 214 |
public Country createCountry(String name, String abbr) { |
| 215 |
return places.createCountry(name, abbr); |
| 216 |
} |
| 217 |
|
| 218 |
public Region createRegion(Country country, String region, String abbr) { |
| 219 |
return places.createRegion(country, region, abbr); |
| 220 |
} |
| 221 |
|
| 222 |
public City createCity(Region region, String city, boolean unique) { |
| 223 |
return places.createCity(region, city, unique); |
| 224 |
} |
| 225 |
|
| 226 |
public City createCity(Country country, String city, boolean unique) { |
| 227 |
return places.createCity(country, city, unique); |
| 228 |
} |
| 229 |
|
| 230 |
public Zip createZip(String code) { |
| 231 |
return places.createZip(code); |
| 232 |
} |
| 233 |
|
| 234 |
public Highway createHighway(Region region, String name) { |
| 235 |
return places.createHighway(region, name); |
| 236 |
} |
| 237 |
|
| 238 |
public ExitFacility createExitFacility(int type, char direction, int facilities, String description, boolean last) { |
| 239 |
return places.createExitFacility(type, direction, facilities, description, last); |
| 240 |
} |
| 241 |
|
| 242 |
public void allPOIsDone() { |
| 243 |
places.allPOIsDone(); |
| 244 |
} |
| 245 |
|
| 246 |
public void setSort(Sort sort) { |
| 247 |
this.sort = sort; |
| 248 |
lblHeader.setSort(sort); |
| 249 |
places.setSort(sort); |
| 250 |
} |
| 251 |
|
| 252 |
public int numCities() { |
| 253 |
return places.numCities(); |
| 254 |
} |
| 255 |
|
| 256 |
public int numZips() { |
| 257 |
return places.numZips(); |
| 258 |
} |
| 259 |
|
| 260 |
public int numHighways() { |
| 261 |
return places.numHighways(); |
| 262 |
} |
| 263 |
|
| 264 |
public int numExitFacilities() { |
| 265 |
return places.numExitFacilities(); |
| 266 |
} |
| 267 |
|
| 268 |
public int getCodePage() { |
| 269 |
return lblHeader.getCodePage(); |
| 270 |
} |
| 271 |
} |