Debian Codegraph @

src/uk/me/parabola/imgfmt/app/srt/SRTFile.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 java.nio.charset.Charset;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import uk.me.parabola.imgfmt.app.BufferedImgFileWriter;
20 import uk.me.parabola.imgfmt.app.ImgFile;
21 import uk.me.parabola.imgfmt.app.ImgFileWriter;
22 import uk.me.parabola.imgfmt.app.SectionWriter;
23 import uk.me.parabola.imgfmt.fs.ImgChannel;
24
25 /**
26 * The SRT file. This contains a table showing the sort order of
27 * the characters that is being used.
28 *
29 * @author Steve Ratcliffe
30 */
31 public class SRTFile extends ImgFile {
32
33 private SRTHeader header;
34
35 private Sort sort;
36 private boolean isMulti;
37
38 private String description;
39
40 private final List<Integer> srt8Starts = new ArrayList<>();
41
42 public SRTFile(ImgChannel chan) {
43 BufferedImgFileWriter fileWriter = new BufferedImgFileWriter(chan, "SRT");
44 fileWriter.setMaxSize(Long.MAX_VALUE);
45 setWriter(fileWriter);
46 }
47
48 /**
49 * Write out the file.
50 * This file has an unusual layout. There are several header like structures within
51 * the main body of the file, with the real header being very small.
52 */
53 public void write() {
54 ImgFileWriter writer = getWriter();
55 writeDescription(writer);
56 // Position at the start of the writable area.
57 position(header.getHeaderLength());
58 SectionWriter subWriter = header.makeSectionWriter(writer);
59 int header3Len = sort.getHeader3Len();
60 if (header3Len == 0)
61 header3Len = sort.isMulti()? SRTHeader.HEADER3_MULTI_LEN: SRTHeader.HEADER3_LEN;
62 subWriter.position(header3Len);
63 writeSrt4Chars(subWriter);
64 writeSrt5Expansions(subWriter);
65 if (sort.isMulti()) {
66 for (int i = 0; i <= sort.getMaxPage(); i++)
67 srt8Starts.add(-1);
68 writeSrt8(subWriter);
69 writeSrt7(subWriter);
70 }
71 subWriter.close();
72
73 // Header 2 is just after the real header
74 writer.position(header.getHeaderLength());
75 header.writeHeader2(writer);
76
77 // Header 3 is after the description
78 writer.position(header.getHeaderLength() + description.length() + 1 + SRTHeader.HEADER2_LEN);
79 header.writeHeader3(writer);
80
81 header.writeHeader(writer);
82 }
83
84 private void writeDescription(ImgFileWriter writer) {
85 writer.position(header.getHeaderLength() + SRTHeader.HEADER2_LEN);
86 writer.put(description.getBytes(Charset.forName("ascii")));
87 writer.put1u( 0);
88 header.endDescription(writer.position());
89 }
90
91 /**
92 * Write SRT4 the character table.
93 *
94 * @param writer The img file writer.
95 */
96 private void writeSrt4Chars(ImgFileWriter writer) {
97 for (int i = 1; i < 256; i++) {
98 writer.put1u(sort.getFlags(i));
99 writeWeights(writer, i);
100 }
101 header.endCharTable(writer.position());
102 }
103
104 private void writeWeights(ImgFileWriter writer, int i) {
105 int primary = sort.getPrimary(i);
106 int secondary = sort.getSecondary(i);
107 int tertiary = sort.getTertiary(i);
108 if (isMulti) {
109 assert primary <= 0xffff : primary;
110 assert secondary <= 0xff : secondary;
111 assert tertiary <= 0xff : tertiary;
112 writer.put2u(primary);
113 writer.put1u(secondary);
114 writer.put1u(tertiary);
115 } else {
116 assert primary <= 0xff : primary;
117 assert secondary <= 0xf : secondary;
118 assert tertiary <= 0xf : tertiary;
119 writer.put1u(primary);
120 writer.put1u((tertiary << 4) | (secondary & 0xf));
121 }
122 }
123
124 /**
125 * Write SRT5, the expansion table.
126 *
127 * Write out the expansion table. This is referenced from the character table, when
128 * the top nibble of the type is set via the primary position value.
129 */
130 private void writeSrt5Expansions(ImgFileWriter writer) {
131
132 int size = sort.getExpansionSize();
133 for (int j = 1; j <= size; j++) {
134 CodePosition b = sort.getExpansion(j);
135 if (isMulti) {
136 writer.put2u(b.getPrimary());
137 writer.put1u(b.getSecondary());
138 writer.put1u(b.getTertiary());
139 } else {
140 writer.put1u(b.getPrimary());
141 writer.put1u((b.getTertiary() << 4) | (b.getSecondary() & 0xf));
142 }
143 }
144
145 header.endTab2(writer.position());
146 }
147
148 private void writeSrt7(SectionWriter writer) {
149 assert sort.isMulti();
150 for (int i = 1; i <= sort.getMaxPage(); i++) {
151 writer.put4(srt8Starts.get(i));
152 }
153 header.endSrt7(writer.position());
154 }
155
156 private void writeSrt8(SectionWriter writer) {
157 assert sort.isMulti();
158
159 int offset = 0;
160 for (int p = 1; p <= sort.getMaxPage(); p++) {
161 if (sort.hasPage(p)) {
162 srt8Starts.set(p, offset);
163 for (int j = 0; j < 256; j++) {
164 int ch = p * 256 + j;
165 writer.put1u(sort.getFlags(ch));
166 writeWeights(writer, ch);
167 offset += 5;
168 }
169 }
170 }
171 header.endSrt8(writer.position());
172 }
173
174 public void setSort(Sort sort) {
175 this.sort = sort;
176 header = new SRTHeader(sort.getHeaderLen());
177 header.setSort(sort);
178 description = sort.getDescription();
179 isMulti = sort.isMulti();
180 }
181 }