File: Braillo440VolumeWriter.java

package info (click to toggle)
brailleutils 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,084 kB
  • sloc: java: 13,138; xml: 659; makefile: 29; sh: 1
file content (185 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Braille Utils (C) 2010-2011 Daisy Consortium 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package com_braillo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.daisy.braille.embosser.EmbosserTools;
import org.daisy.paper.Dimensions;


class Braillo440VolumeWriter extends BrailloVolumeWriter {
	public enum Mode {SW_TWO_PAGE, SW_FOUR_PAGE, SWSF};
	private final int width;
	@SuppressWarnings("unused")
	private final int height;
	private final Dimensions pageFormat;
	private final Mode mode;
	private final double columnWidthMM;
	
	Braillo440VolumeWriter(Dimensions pageFormat, Mode mode, int width, int height, double columnWidthMM) {
		/*
		int margin = 4; // approximate width of unprintable margins.
		if (Paper.newPaper(PaperSize.A4).equals(paper)) {
			// Underestimate margin width for A4 in order to get 32 chars width (pages will be 21,74 cm wide)
			margin = 3;
		}
		this.width = paper.getWidth(cellWidth)-margin;*/
		
		// Paper.INCH_IN_MM = the smallest possible margins (0.5 inch + 0.5 inch)
		this.width = width;
		this.height = height;
		this.pageFormat = pageFormat;
		this.mode = mode;
		this.columnWidthMM = columnWidthMM;
		int inchHeight = (int)Math.ceil(pageFormat.getHeight()/EmbosserTools.INCH_IN_MM);
		if (width > 44 || inchHeight > 13) { 
			throw new IllegalArgumentException("Paper too wide or high: " + width + " chars x " + inchHeight + " inches."); 
		}
		if (width < 10) {
			throw new IllegalArgumentException("Paper too narrow: " + width + " chars.");
		}
	}

	@SuppressWarnings("unchecked")
	private List<Byte>[] newList(int s) {
		return new List[s];
	}

	public List<? extends List<Byte>> reorder(List<? extends List<Byte>> pages) {
		switch (mode) {
			case SW_FOUR_PAGE: case SWSF: {
					int s = (int)(Math.ceil(pages.size()/4d)*4);
					List<Byte>[] r = newList(s);
					for (int i=1;i<=s;i++) {
						int pos;
						if (mode==Mode.SW_FOUR_PAGE) {
							pos = getPos(i, s);
						} else if (mode==Mode.SWSF) {
							pos = getPosReversed(i, s);
						} else {
							throw new RuntimeException("Unexpected error.");
						}
						if (i>pages.size()) {
							r[pos] = new ArrayList<Byte>();
							for (byte b:ffSeq) {
								r[pos].add(b);
							}
						} else {
							r[pos] = pages.get(i-1);
						}
					}
					ArrayList<List<Byte>> ret = new ArrayList<List<Byte>>();
					for (List<Byte> r2 : r) {
						ret.add(r2);
					}
					return ret;
				}
			case SW_TWO_PAGE: {
					int s = (int)(Math.ceil(pages.size()/2d)*2);
					List<Byte>[] r = newList(s);
					for (int i=0;i<s;i++) {
						if (i>=pages.size()) {
							r[i] = new ArrayList<Byte>();
							for (byte b:ffSeq) {
								r[i].add(b);
							}
						} else {
							r[i] = pages.get(i);
						}
					}
					ArrayList<List<Byte>> ret = new ArrayList<List<Byte>>();
					for (List<Byte> r2 : r) {
						ret.add(r2);
					}
					return ret;
				}
			default:
				throw new RuntimeException("Unexpected error.");
		}
	}
	
	private int getPos(int pos, int len) {
		if (len % 4 != 0) {
			throw new IllegalArgumentException("len % 4 must be 0");
		}
		if (pos>len) {
			throw new IllegalArgumentException("len must be larger than pos");
		}
		if (pos<=len/2) {
			int s = (int)Math.ceil(pos / 2d);
			return (s - 1) * 4 + (pos - 1) % 2; 
		} else {
			int s = (int)Math.ceil((len - pos + 1) / 2d);
			return (s - 1) * 4 + (pos - 1) % 2 + 2;
		}
	}
	
	private int getPosReversed(int pos, int len) {
		return len-(getPos(pos, len)+1);
	}

	//jvm1.6@Override
	public byte[] getFooter(int len) {
		return new byte[0];
	}

	//jvm1.6@Override
	public byte[] getHeader(int len) throws IOException {
		// Round to the closest possible higher value, so that all characters fit on the page
		byte[] w = EmbosserTools.toBytes(width, 2);
		byte[] p;
		int pi = 0;
		byte m;
		double m1 = Math.round(((pageFormat.getWidth() - columnWidthMM) / (2 * EmbosserTools.INCH_IN_MM)) * 10) / 10d;
		double m2 = Math.round(((pageFormat.getWidth() - m1 * EmbosserTools.INCH_IN_MM - columnWidthMM) / EmbosserTools.INCH_IN_MM) * 10) / 10d;
		byte[] mb1 = EmbosserTools.toBytes((int)(m1 * 10), 2);
		byte[] mb2 = EmbosserTools.toBytes((int)(m2 * 10), 2);
		switch (mode) {
			case SW_FOUR_PAGE: case SWSF: {
				pi = (int)Math.ceil(len/4d);
				p = EmbosserTools.toBytes(pi, 2);
				m = '1';
				break;
			}
			case SW_TWO_PAGE: {
				pi = (int)Math.ceil(len/2d);
				p = EmbosserTools.toBytes(pi, 2);
				m = '0';
				break;
			}
			default:
				throw new RuntimeException("Unexpected error.");
		}
		if (pi>99) {
			throw new IOException("Too many sheets in one volume: " + pi);
		}
		return new byte[] {
			0x1b, 'A', m, 			// page mode
			0x1b, 'B', w[0], w[1], 	// width, in characters
			0x1b, 'D', mb1[0], mb1[1], 	// margin 1 
			0x1b, 'E', mb2[0], mb2[1], 	// margin 2
			0x1b, 'H', '1', 		// interpoint
			0x1b, 'P', p[0], p[1]	// number of sheets
			};
	}


}