~ubuntu-branches/ubuntu/lucid/tuxguitar/lucid

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * Created on 04-ene-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.herac.tuxguitar.gui.editors.tab.layout;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Rectangle;
import org.herac.tuxguitar.gui.TuxGuitar;
import org.herac.tuxguitar.gui.editors.TGPainter;
import org.herac.tuxguitar.gui.editors.tab.TGChordImpl;
import org.herac.tuxguitar.gui.editors.tab.TGLyricImpl;
import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
import org.herac.tuxguitar.gui.editors.tab.TGTrackImpl;
import org.herac.tuxguitar.gui.editors.tab.Tablature;
import org.herac.tuxguitar.gui.printer.PrintDocument;
import org.herac.tuxguitar.gui.printer.PrintStyles;
import org.herac.tuxguitar.song.models.TGMeasure;
import org.herac.tuxguitar.song.models.TGTrack;

/**
 * @author julian
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PrinterViewLayout extends ViewLayout{
	
	private static final int DEFAULT_SCORE_LINE_SPACING = 7;
	private static final int DEFAULT_STRING_SPACING = 8;
	private static final int DEFAULT_HORIZONTAL_SPACING = 15;
	private static final int MIN_SCORE_TAB_SPACING =  15;
	private static final int DEFAULT_TRACK_SPACING = 5;
	private static final int DEFAULT_FIRST_TRACK_SPACING = DEFAULT_TRACK_SPACING;
	private static final int DEFAULT_MIN_BUFFER_SEPARATOR = 15;
	private static final int DEFAULT_MIN_TOP_SPACING = 20;
	private static final int CHORD_FRET_INDEX_SPACING = 8;
	private static final int CHORD_STRING_SPACING = 4;
	private static final int CHORD_FRET_SPACING = 5;
	
	private PrintStyles styles;
	private PrintDocument document;
	private int page;
	
	private Font songNameFont;
	private Font trackNameFont;
	private Font songAuthorFont;
	
	public PrinterViewLayout(Tablature tablature,PrintStyles styles, float scale){
		super(tablature,( styles.getStyle() | DISPLAY_COMPACT ), scale );
		this.styles = styles;
	}
	
	protected void init( float scale ){
		this.setBufferEnabled(false);
		this.setFirstMeasureSpacing( getScaledValue(scale , DEFAULT_HORIZONTAL_SPACING ) );
		this.setMinBufferSeparator( getScaledValue(scale ,  DEFAULT_MIN_BUFFER_SEPARATOR ) );
		this.setMinTopSpacing( getScaledValue(scale ,  DEFAULT_MIN_TOP_SPACING ) );
		this.setMinScoreTabSpacing( getScaledValue(scale ,  MIN_SCORE_TAB_SPACING ) );
		this.setScoreLineSpacing( getScaledValue(scale ,  DEFAULT_SCORE_LINE_SPACING , 4) );
		this.setScoreSpacing( (( getScoreLineSpacing() * 4) + getMinScoreTabSpacing() ) );
		this.setFirstTrackSpacing( getScaledValue(scale ,  DEFAULT_FIRST_TRACK_SPACING ) );
		this.setTrackSpacing( getScaledValue(scale ,  DEFAULT_TRACK_SPACING ) );
		this.setStringSpacing( getScaledValue(scale ,  DEFAULT_STRING_SPACING  , 4) );
		this.setChordFretIndexSpacing( getScaledValue(scale ,  CHORD_FRET_INDEX_SPACING, 2 ) );
		this.setChordStringSpacing( getScaledValue(scale ,  CHORD_STRING_SPACING, 2 ) );
		this.setChordFretSpacing( getScaledValue(scale ,  CHORD_FRET_SPACING , 2 ) );
		this.setChordNoteSize(getScaledValue(scale ,  3 , 2 ));
		this.setRepeatEndingSpacing( getScaledValue(scale ,  20 ) );
		this.setTextSpacing( getScaledValue( scale, 15 ) );
		this.setMarkerSpacing( getScaledValue( scale, 15 ) );
		this.setTupletoSpacing( getScaledValue( scale,  10 ) );
		this.setEffectSpacing( getScaledValue(scale ,  8 ) );
		this.setScale( checkScale() );
		this.setFontScale( scale );
		
		// Initialize Tablature Defaults
		this.getTablature().setViewLayout( this ) ;
		this.getTablature().initDefaults();
		
		FontData[] fd = this.getResources().getPrinterDefaultFont().getFontData();
		String fontName = (fd != null && fd.length > 0 ? fd[0].getName() : new String());
		this.songNameFont = new Font(getTablature().getDisplay(),fontName,Math.round(16.0f * getFontScale()), SWT.BOLD | SWT.CENTER);
		this.trackNameFont = new Font(getTablature().getDisplay(),fontName,Math.round(8.0f * getFontScale()), SWT.BOLD | SWT.CENTER);
		this.songAuthorFont = new Font(getTablature().getDisplay(),fontName,Math.round(8.0f * getFontScale()), SWT.BOLD | SWT.CENTER);
	}
	
	public int getMode(){
		return 0;
	}
	
	public void makeDocument(PrintDocument document){
		this.page = 0;
		this.document = document;
		this.makeDocument();
	}
	
	private void makeDocument(){
		this.document.start();
		
		this.openPage();
		paintHeader(this.document.getPainter());
		paintSong(this.document.getPainter(),null,this.document.getBounds().x, ( this.document.getBounds().y + Math.round(80.0f * getFontScale() ) ) );
		paintFooter(this.document.getPainter());
		this.closePage();
		
		this.document.finish();
	}
	
	public void paintSong(TGPainter painter,Rectangle clientArea,int fromX,int fromY) {
		this.setWidth(0);
		this.setHeight(0);
		
		int style = getStyle();
		int posY = fromY + getFirstTrackSpacing();
		int height = getFirstTrackSpacing();
		int lineHeight = 0;
		
		TGTrackImpl track = (TGTrackImpl)getSongManager().getTrack(this.styles.getTrackNumber());
		((TGLyricImpl)track.getLyrics()).start(getSkippedBeats(track));
		
		TrackSpacing ts = new TrackSpacing(this) ;
		TempLine line = getTempLines(track,( this.styles.getFromMeasure() - 1 ),ts);
		while(!line.measures.isEmpty()){
			
			ts.setSize(TrackSpacing.POSITION_SCORE_MIDDLE_LINES, ((style & DISPLAY_SCORE) != 0?( (getScoreLineSpacing() * 5) ):0));
			if((style & DISPLAY_SCORE) != 0){
				ts.setSize(TrackSpacing.POSITION_SCORE_UP_LINES, Math.abs(line.minY));
				if(line.maxY + getMinScoreTabSpacing() > getScoreSpacing()){
					ts.setSize(TrackSpacing.POSITION_SCORE_DOWN_LINES, (line.maxY - (getScoreLineSpacing() * 4)) );
				}
			}
			if((style & DISPLAY_TABLATURE) != 0){
				ts.setSize(TrackSpacing.POSITION_TABLATURE_TOP_SEPARATOR, ((style & DISPLAY_SCORE) != 0 ? getMinScoreTabSpacing() : Math.max(Math.abs(line.minY), getStringSpacing()) ));
				ts.setSize(TrackSpacing.POSITION_TABLATURE, ((style & DISPLAY_SCORE) != 0 ?  track.getTabHeight() + getStringSpacing() + 1 : Math.max( line.maxY, track.getTabHeight() + getStringSpacing() + 1) ));
				//ts.setSize(TrackSpacing.POSITION_TABLATURE_TOP_SEPARATOR,((style & DISPLAY_SCORE) != 0 ?getMinScoreTabSpacing(): ((getStringSpacing() / 2) * 5) +1 ));
				//ts.setSize(TrackSpacing.POSITION_TABLATURE, ((style & DISPLAY_SCORE) != 0 ?track.getTabHeight() + getStringSpacing() +1:track.getTabHeight() + ((getStringSpacing() / 2) * 5) +1 ) );
			}
			ts.setSize(TrackSpacing.POSITION_LYRIC,10);
			checkDefaultSpacing(ts);
			
			lineHeight = ts.getSize();
			//Verifico si entra en la pagina actual
			if((posY + lineHeight + getTrackSpacing()) > (this.document.getBounds().y + getMaxHeight())){
				this.paintFooter(painter);
				this.closePage();
				this.openPage();
				posY = this.document.getBounds().y + getFirstTrackSpacing();
			}
			
			//pinto la linea
			paintLine(track,line,painter,fromX,posY,ts);
			
			posY += lineHeight + getTrackSpacing();
			height += lineHeight + getTrackSpacing();
			
			ts = new TrackSpacing(this) ;
			line = getTempLines(track,( line.lastIndex + 1 ),ts);
		}
		this.setHeight(height);
	}
	
	public void paintHeader(TGPainter painter){
		if(this.document.isPaintable(this.page) ){
			int x = this.document.getBounds().x;
			int y = this.document.getBounds().y;
			String songName = getSongManager().getSong().getName();
			String songAuthor = getSongManager().getSong().getAuthor();
			String trackName = "(" + getSongManager().getTrack(this.styles.getTrackNumber()).getName() + ")";
			
			if(songName == null || songName.length() == 0){
				songName = TuxGuitar.getProperty("print-header.default-song-name");
			}
			if(songAuthor == null || songAuthor.length() == 0){
				songAuthor = TuxGuitar.getProperty("print-header.default-song-author");
			}
			painter.setFont(this.songNameFont);
			painter.drawString(songName,(x + getCenter(painter,songName)),y);
			painter.setFont(this.trackNameFont);
			painter.drawString(trackName,(x + getCenter(painter,trackName)),(y + Math.round(30.0f * getFontScale())));
			painter.setFont(this.songAuthorFont);
			painter.drawString(songAuthor,(x + getRight(painter,songAuthor)),(y + Math.round(50.0f * getFontScale())));
		}
	}
	
	private void paintFooter(TGPainter painter){
		if(this.document.isPaintable(this.page) ){
			int x = this.document.getBounds().x;
			int y = this.document.getBounds().y;
			String pageNumber = Integer.toString(this.page);
			
			painter.setBackground(getResources().getColorWhite());
			painter.setForeground(getResources().getColorBlack());
			painter.drawString(pageNumber,(x + getRight(painter,pageNumber)),(y + getBottom(painter,pageNumber)));
		}
	}
	
	public void paintLine(TGTrackImpl track,TempLine line,TGPainter painter,int fromX, int fromY,TrackSpacing ts) {
		if(this.document.isPaintable(this.page) ){
			int posX = fromX;
			int posY = fromY;
			int width = 0;
			
			int measureSpacing = 0;
			if(line.fullLine){
				int diff = ( getMaxWidth() - line.tempWith);
				if(diff != 0 && line.measures.size() > 0){
					measureSpacing = diff / line.measures.size();
				}
			}
			
			for(int i = 0;i < line.measures.size();i ++){
				int index = ((Integer)line.measures.get(i)).intValue();
				TGMeasureImpl currMeasure = (TGMeasureImpl)track.getMeasure(index);
				
				//asigno la posicion dentro del compas
				currMeasure.setPosX(posX);
				currMeasure.setPosY(posY);
				currMeasure.setTs(ts);
				
				((TGLyricImpl)track.getLyrics()).setCurrentMeasure(currMeasure);
				
				currMeasure.setFirstOfLine(i == 0);
				
				paintMeasure(currMeasure,painter,measureSpacing);
				((TGLyricImpl)track.getLyrics()).paintCurrentNoteBeats(painter,this,currMeasure,posX, posY);
				
				int measureWidth = ( currMeasure.getWidth(this) + currMeasure.getSpacing() );
				posX += measureWidth;
				width += measureWidth;
			}
			this.setWidth(Math.max(getWidth(),width));
		}
	}
	
	private void openPage(){
		this.page ++;
		if(this.document.isPaintable(this.page)){
			this.document.pageStart();
		}
	}
	
	private void closePage(){
		if(this.document.isPaintable(this.page)){
			this.document.pageFinish();
		}
	}
	
	private int getCenter(TGPainter painter,String text){
		int textWidth = painter.getStringExtent(text).x;
		return ((getMaxWidth() - textWidth) / 2);
	}
	
	private int getRight(TGPainter painter,String text){
		int textWidth = painter.getStringExtent(text).x;
		return ((getMaxWidth() - textWidth));
	}
	
	private int getBottom(TGPainter painter,String text){
		int textHeight = painter.getStringExtent(text).y;
		return ((getMaxHeight() - textHeight));
	}
	
	private TempLine getTempLines(TGTrack track,int fromIndex,TrackSpacing ts) {
		TempLine line = new TempLine();
		int measureCount = track.countMeasures();
		for (int measureIdx = fromIndex; measureIdx < measureCount; measureIdx++) {
			TGMeasureImpl measure= (TGMeasureImpl) track.getMeasure(measureIdx);
			if( measure.getNumber() >= this.styles.getFromMeasure() && measure.getNumber() <= this.styles.getToMeasure()){
				
				//verifico si tengo que bajar de linea
				if((line.tempWith + measure.getWidth(this)) >=  getMaxWidth() && !line.measures.isEmpty()){
					line.fullLine = true;
					return line;
				}
				line.tempWith +=  measure.getWidth(this);
				line.maxY = (measure.getMaxY() > line.maxY)?measure.getMaxY():line.maxY;
				line.minY = (measure.getMinY() < line.minY)?measure.getMinY():line.minY;
				
				line.addMeasure(measureIdx);
				measure.registerSpacing(this,ts);
			}
		}
		
		return line;
	}
	
	private int getSkippedBeats(TGTrack track) {
		int beats = 0;
		
		for (int i = 0; i < (this.styles.getFromMeasure() - 1); i++) {
			TGMeasureImpl measure = (TGMeasureImpl) track.getMeasure(i);
			beats += measure.getNotEmptyBeats();
		}
		return beats;
	}
	
	public boolean isCaretVisible(){
		return false;
	}
	
	public boolean isPlayModeEnabled(){
		return false;
	}
	
	public void setLineStyle(TGPainter painter){
		painter.setLineWidth(1);
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setMeasureNumberStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground(getResources().getColorWhite());
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setDivisionsStyle(TGPainter painter, boolean fill){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground( (fill ? getResources().getColorBlack() :getResources().getColorWhite() ));
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setTempoStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground(getResources().getColorWhite());
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setTripletFeelStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground(getResources().getColorWhite());
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setTabNoteStyle(TGPainter painter,boolean playMode){
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
		painter.setFont(getResources().getPrinterNoteFont());
	}
	
	public void setTabNoteFooterStyle(TGPainter painter){
		painter.setForeground( getResources().getColorBlack());
		painter.setBackground( getResources().getColorBlack());
	}
	
	public void setTabEffectStyle(TGPainter painter){
		painter.setForeground( getResources().getColorBlack());
		painter.setBackground( getResources().getColorBlack());
	}
	
	public void setScoreNoteStyle(TGPainter painter,boolean playing){
		painter.setBackground( getResources().getColorBlack() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setScoreNoteFooterStyle(TGPainter painter){
		painter.setForeground( getResources().getColorBlack());
		painter.setBackground( getResources().getColorBlack());
	}
	
	public void setScoreEffectStyle(TGPainter painter){
		painter.setForeground( getResources().getColorBlack());
		painter.setBackground( getResources().getColorBlack());
	}
	
	public void setTimeSignatureStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterTimeSignatureFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setGraceStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterGraceFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setLyricStyle(TGPainter painter,boolean playMode){
		painter.setFont(getResources().getPrinterLyricFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setTextStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterTextFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setOfflineEffectStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setTupletoStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setRepeatEndingStyle(TGPainter painter){
		painter.setFont(getResources().getPrinterDefaultFont());
		painter.setBackground( getResources().getColorWhite() );
		painter.setForeground(getResources().getColorBlack());
	}
	
	public void setChordStyle(TGChordImpl chord){
		chord.setStyle(getStyle());
		chord.setFont(getResources().getPrinterChordFont());
		chord.setForegroundColor(getResources().getColorBlack());
		chord.setBackgroundColor(getResources().getColorWhite());
		chord.setColor(getResources().getColorBlack());
		chord.setNoteColor(getResources().getColorBlack());
		chord.setTonicColor(getResources().getColorBlack());
		chord.setFretSpacing(getChordFretSpacing());
		chord.setStringSpacing(getChordStringSpacing());
		chord.setNoteSize(getChordNoteSize());
		chord.setFirstFretSpacing(getChordFretIndexSpacing());
		chord.setFirstFretFont(getResources().getChordFretFont());
	}
	
	public int getMaxWidth(){
		return (this.document.getBounds().width - this.document.getBounds().x - 10);
	}
	
	public int getMaxHeight(){
		return (this.document.getBounds().height - this.document.getBounds().y - 10);
	}
	
	public boolean isFirstMeasure(TGMeasure measure){
		return (measure.getNumber() == this.styles.getFromMeasure());
	}
	
	public boolean isLastMeasure(TGMeasure measure){
		return (measure.getNumber() == this.styles.getToMeasure());
	}
	
	private int getScaledValue(float scale, int value){
		return getScaledValue(scale, value,1);
	}
	
	private int getScaledValue(float scale, int value, int minimum){
		float scaledValue = ( value * scale );
		return Math.max( Math.round( scaledValue ) , minimum );
	}
	
	public void disposeLayout(){
		super.disposeLayout();
		this.songNameFont.dispose();
		this.trackNameFont.dispose();
		this.songAuthorFont.dispose();
	}
	
	private class TempLine{
		protected int tempWith;
		protected int lastIndex;
		protected boolean fullLine;
		protected int maxY = 0;
		protected int minY = 50;
		protected List measures;
		
		public TempLine(){
			this.measures = new ArrayList();
		}
		
		protected void addMeasure(int index){
			this.measures.add(new Integer(index));
			this.lastIndex = index;
		}
	}
}