Debian Codegraph @

src/uk/me/parabola/imgfmt/app/srt/SRTHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2010.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.srt;
14
15 import uk.me.parabola.imgfmt.ReadFailedException;
16 import uk.me.parabola.imgfmt.app.CommonHeader;
17 import uk.me.parabola.imgfmt.app.ImgFileReader;
18 import uk.me.parabola.imgfmt.app.ImgFileWriter;
19 import uk.me.parabola.imgfmt.app.Section;
20 import uk.me.parabola.imgfmt.app.SectionWriter;
21
22 /**
23 * The header of the SRT file.
24 *
25 * This file determines the sort order of the label characters.
26 *
27 * @author Steve Ratcliffe
28 */
29 public class SRTHeader extends CommonHeader {
30 // The header length we are using for the SRT file
31 public static final int HEADER_LEN = 29; // newer files use 37
32 protected static final int HEADER2_LEN = 16;
33 protected static final int HEADER3_LEN = 52;
34 protected static final int HEADER3_MULTI_LEN = 92;
35
36 // The section structure of this file is somewhat different to other
37 // files, but I am still going to model it using Section.
38 private final Section header2 = new Section();
39
40 private final Section desc = new Section(header2);
41 private final Section subheader = new Section(desc);
42 private final Section chartab = new Section(3);
43 private final Section expansions = new Section(chartab, 2);
44 private final Section srt8 = new Section(expansions, 5);
45 private final Section srt7 = new Section(srt8, 4);
46
47
48 private Sort sort;
49
50 public SRTHeader() {
51 this(HEADER_LEN);
52 }
53
54 public SRTHeader(int headerLen) {
55 super(headerLen, "GARMIN SRT");
56 header2.setPosition(headerLen);
57 header2.setSize(16);
58
59 chartab.setPosition(HEADER3_LEN);
60 }
61
62 protected void readFileHeader(ImgFileReader reader) throws ReadFailedException {
63 reader.get2u(); // expected: 1
64 header2.setPosition(reader.get4());
65 header2.setSize(reader.get2u());
66 }
67
68 /**
69 * Write out the application header. This is unusual as it just points
70 * to an area which is itself just a header.
71 */
72 protected void writeFileHeader(ImgFileWriter writer) {
73 if (getHeaderLength() <= 29) {
74 writer.put2u(1);
75
76 writer.put4(header2.getPosition());
77 writer.put2u(header2.getSize());
78 } else {
79 // this appears in unicode maps dated 2016 and 2017
80 writer.put2u(0);
81 writer.put4(header2.getPosition());
82 writer.put2u(header2.getSize());
83 writer.put2u(1);
84 writer.put4(header2.getPosition());
85 writer.put2u(header2.getSize());
86
87 // older maps contain this variant (note the different order)
88 // writer.putChar((char) 1);
89 // writer.putInt(header2.getPosition());
90 // writer.putChar((char) header2.getSize());
91 // writer.putChar((char) 0);
92 // writer.putInt(desc.getPosition());
93 // writer.putChar((char) 0x10);
94
95 }
96 }
97
98 /**
99 * Section writer to write the character and expansions sections. These two sections are embedded within another
100 * section and their offsets are relative to that section.
101 * @param writer The real underlying writer.
102 * @return A new writer where offsets are relative to the start of the sub-header section.
103 */
104 SectionWriter makeSectionWriter(ImgFileWriter writer) {
105 return new SectionWriter(writer, subheader);
106 }
107
108 /**
109 * This is a header with pointers to the description and the main section.
110 * The offsets contained in this section are relative to the beginning of the file.
111 * @param writer Header is written here.
112 */
113 protected void writeHeader2(ImgFileWriter writer) {
114 desc.writeSectionInfo(writer);
115 subheader.writeSectionInfo(writer);
116 }
117
118 /**
119 * Header contained within the main section. Offsets within this section are relative to the beginning
120 * of the section.
121 * @param writer Header is written here.
122 */
123 protected void writeHeader3(ImgFileWriter writer) {
124 if (sort.isMulti()) {
125 writer.put2u(HEADER3_MULTI_LEN);
126 } else {
127 writer.put2u(HEADER3_LEN);
128 }
129
130 writer.put2u(sort.getId1());
131 writer.put2u(sort.getId2());
132 writer.put2u(sort.getCodepage());
133 if (sort.isMulti())
134 writer.put4(0x6f02);
135 else
136 writer.put4(0x2002);
137
138 chartab.writeSectionInfo(writer, true, true);
139 writer.put2u(0);
140
141 expansions.writeSectionInfo(writer, true, true);
142 writer.put2u(0);
143
144 // SRT6 A repeat pointer to the single byte character table
145 writer.put4(chartab.getPosition());
146 writer.put4(0);
147
148 if (sort.isMulti()) {
149 writer.put4(1);
150 writer.put4(sort.getMaxPage()); // max block in srt7
151
152 srt7.writeSectionInfo(writer, true);
153 writer.put2u(0);
154 writer.put4(0);
155 srt8.writeSectionInfo(writer, true);
156
157 writer.put2u(0);
158 writer.put4(0);
159 }
160 }
161
162 public void setSort(Sort sort) {
163 this.sort = sort;
164 if (sort.isMulti()) {
165 chartab.setPosition(HEADER3_MULTI_LEN);
166 chartab.setItemSize(5);
167 expansions.setItemSize(4);
168 }
169 }
170
171 /** Called after the description has been written to record the position. */
172 public void endDescription(int position) {
173 desc.setSize(position - desc.getPosition());
174 subheader.setPosition(position);
175 }
176
177 /** Called after the character table has been written to record the position. */
178 public void endCharTable(int position) {
179 chartab.setSize(position - chartab.getPosition());
180 }
181
182 /** Called after the expansions has been written to record the position. */
183 public void endTab2(int postition) {
184 subheader.setSize(postition - subheader.getPosition());
185 expansions.setSize(postition - expansions.getPosition());
186 }
187
188 public void endSrt8(int position) {
189 srt8.setSize(position - srt8.getPosition());
190 }
191
192 public void endSrt7(int position) {
193 srt7.setSize(position - srt7.getPosition());
194 }
195 }