Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/LBLFile.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 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: 03-Dec-2006
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 * The file that holds all the labels for the map.
40 *
41 * Would be quite simple, but there are a number of sections that hold country,
42 * region, city, etc. records.
43 *
44 * To begin with I shall only support regular labels.
45 *
46 * @author Steve Ratcliffe
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 /** Shift value for the label offset, we always use 1 here. */
61 private static final int OFFSET_MULTIPLIER = 1;
62
63 /** Garmin software doesn't display longer labels */
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 // The zero offset is for no label.
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 // Now that the body is written all the required offsets will be set up
91 // inside the header, so we can go back and write it.
92 ImgFileWriter writer = getWriter();
93 getHeader().writeHeader(writer);
94
95 // Text can be put between the header and the body of the file.
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 // The label section has already been written, but we need to record
103 // its size before doing anything else.
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 * Add a new label with the given text. Labels are shared, so that identical
129 * text is always represented by the same label.
130 *
131 * @param text The text of the label
132 * @return A reference to the created label
133 */
134 public Label newLabel(String text) {
135 return newLabel(text, true);
136 }
137
138 /**
139 * Add a new label with the given text. Labels are shared, so that identical
140 * text is always represented by the same label.
141 *
142 * @param text The text of the label
143 * @param cutToMaxLen if true, cut labels to max length
144 * @return A reference to the created label
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; // short enough
153 }
154 if (trimmedLen > 0) {
155 do { // logic elsewhere removes leading, trailing and multiple spaces, but now might have trailing; remove multiple anyway
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 * Align for the next label.
185 *
186 * Only has any effect when offsetMultiplier is not zero.
187 */
188 private void alignForNext() {
189 // Align ready for next label
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 }