forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferenceController_Subtitle.m
executable file
·396 lines (351 loc) · 15.9 KB
/
PreferenceController_Subtitle.m
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
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "PreferenceController.h"
#import "UserDefaults.h"
#import "AppController.h"
#import "MMovieView.h"
@implementation PreferenceController (Subtitle)
- (void)initSubtitlePane
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_subtitleEnableButton setState:[_defaults boolForKey:MSubtitleEnableKey]];
[self initSubtitleEncodingPopUpButton];
[self updateSubtitleFontAndSizeUI];
NSColor* textColor = [_defaults colorForKey:MSubtitleTextColorKey];
[_subtitleTextColorWell setColor:textColor];
[_subtitleTextOpacitySlider setFloatValue:[textColor alphaComponent]];
[_subtitleTextOpacityTextField setFloatValue:[textColor alphaComponent]];
NSColor* strokeColor = [_defaults colorForKey:MSubtitleStrokeColorKey];
float strokeWidth = [_defaults floatForKey:MSubtitleStrokeWidthKey];
[_subtitleStrokeColorWell setColor:strokeColor];
[_subtitleStrokeOpacitySlider setFloatValue:[strokeColor alphaComponent]];
[_subtitleStrokeOpacityTextField setFloatValue:[strokeColor alphaComponent]];
[_subtitleStrokeWidthSlider setFloatValue:strokeWidth];
[_subtitleStrokeWidthTextField setFloatValue:strokeWidth];
NSColor* shadowColor = [_defaults colorForKey:MSubtitleShadowColorKey];
float shadowBlur = [_defaults floatForKey:MSubtitleShadowBlurKey];
float shadowOffset = [_defaults floatForKey:MSubtitleShadowOffsetKey];
int shadowDarkness = [_defaults integerForKey:MSubtitleShadowDarknessKey];
[_subtitleShadowColorWell setColor:shadowColor];
[_subtitleShadowOpacitySlider setFloatValue:[shadowColor alphaComponent]];
[_subtitleShadowOpacityTextField setFloatValue:[shadowColor alphaComponent]];
[_subtitleShadowBlurSlider setFloatValue:shadowBlur];
[_subtitleShadowBlurTextField setFloatValue:shadowBlur];
[_subtitleShadowOffsetSlider setFloatValue:shadowOffset];
[_subtitleShadowOffsetTextField setFloatValue:shadowOffset];
[_subtitleShadowDarknessSlider setIntValue:shadowDarkness];
[_subtitleShadowDarknessTextField setIntValue:shadowDarkness];
int position = [_defaults integerForKey:MSubtitlePositionKey];
[_subtitlePositionPopUpButton selectItemWithTag:position];
float hMargin = [_defaults floatForKey:MSubtitleHMarginKey];
[_subtitleHMarginSlider setFloatValue:hMargin];
[_subtitleHMarginTextField setFloatValue:hMargin];
float vMargin = [_defaults floatForKey:MSubtitleVMarginKey];
[_subtitleVMarginSlider setFloatValue:vMargin];
[_subtitleVMarginTextField setFloatValue:vMargin];
float lineSpacing = [_defaults floatForKey:MSubtitleLineSpacingKey];
[_subtitleLineSpacingSlider setFloatValue:lineSpacing];
[_subtitleLineSpacingTextField setFloatValue:lineSpacing];
}
- (IBAction)subtitleEnbleAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
BOOL subtitleEnable = [_subtitleEnableButton state];
[_defaults setBool:subtitleEnable forKey:MSubtitleEnableKey];
[_appController setSubtitleEnable:subtitleEnable];
}
- (IBAction)subtitleEncodingAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_defaults setInteger:[[sender selectedItem] tag] forKey:MSubtitleEncodingKey];
[_appController reopenSubtitle];
}
- (IBAction)subtitleFontAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[[self window] makeFirstResponder:nil];
NSFont* font = [NSFont fontWithName:[_defaults stringForKey:MSubtitleFontNameKey]
size:[_defaults floatForKey:MSubtitleFontSizeKey]];
NSFontManager* fontManager = [NSFontManager sharedFontManager];
[fontManager setDelegate:self];
[fontManager orderFrontFontPanel:self];
[fontManager setSelectedFont:font isMultiple:FALSE];
}
- (float)fontSizeForAutoFontSizeChars:(int)chars
{
/*
NSFont* font;
NSString* fontName = [_defaults stringForKey:MSubtitleFontNameKey];
float fontSize = 10;
float hMargin = [_defaults floatForKey:MSubtitleHMarginKey] / 100.0; // percentage
float width, maxWidth = 640.0 - (640.0 * hMargin) * 2;
while (TRUE) {
font = [NSFont fontWithName:fontName size:fontSize];
width = [font boundingRectForFont].size.width * chars;
if (maxWidth < width) {
fontSize--;
break;
}
fontSize++;
};
return fontSize;
*/
NSString* testChar = NSLocalizedString(@"SubtitleTestChar", nil);
NSMutableString* s = [NSMutableString stringWithCapacity:100];
int i;
for (i = 0; i < chars; i++) {
[s appendString:testChar];
}
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithString:s];
NSRange range = NSMakeRange(0, [s length]);
NSFont* font;
NSSize maxSize = NSMakeSize(10000, 10000);
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading |
NSStringDrawingUsesDeviceMetrics;
NSString* fontName = [_defaults stringForKey:MSubtitleFontNameKey];
float fontSize = 10;
float hMargin = [_defaults floatForKey:MSubtitleHMarginKey] / 100.0; // percentage
float width, maxWidth = 640.0 - (640.0 * hMargin) * 2;
while (TRUE) {
font = [NSFont fontWithName:fontName size:fontSize];
[mas addAttribute:NSFontAttributeName value:font range:range];
width = [mas boundingRectWithSize:maxSize options:options].size.width;
if (maxWidth < width) {
fontSize--;
break;
}
fontSize++;
};
[mas release];
return fontSize;
}
- (void)updateFontSizeForAutoFontSizeChars
{
int chars = [_defaults integerForKey:MSubtitleAutoFontSizeCharsKey];
float fontSize = [self fontSizeForAutoFontSizeChars:chars];
[_defaults setFloat:fontSize forKey:MSubtitleFontSizeKey];
[self updateSubtitleFontAndSizeUI];
[_movieView setSubtitleFontName:[_defaults stringForKey:MSubtitleFontNameKey]
size:[_defaults floatForKey:MSubtitleFontSizeKey]];
}
- (IBAction)subtitleAutoFontSizeAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
BOOL autoFontSize = [_subtitleAutoFontSizeButton state];
[_defaults setBool:autoFontSize forKey:MSubtitleAutoFontSizeKey];
if (autoFontSize) {
[self updateFontSizeForAutoFontSizeChars];
}
else {
[self updateSubtitleFontAndSizeUI];
}
}
- (IBAction)subtitleAutoFontSizeCharsAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_defaults setInteger:[sender intValue] forKey:MSubtitleAutoFontSizeCharsKey];
[self updateFontSizeForAutoFontSizeChars];
}
- (void)changeFont:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
NSFont* font = [NSFont fontWithName:[_defaults stringForKey:MSubtitleFontNameKey]
size:[_defaults floatForKey:MSubtitleFontSizeKey]];
font = [sender convertFont:font];
float fontSize = [font pointSize];
if ([_defaults boolForKey:MSubtitleAutoFontSizeKey]) {
fontSize = [_defaults floatForKey:MSubtitleFontSizeKey];
}
[_defaults setObject:[font fontName] forKey:MSubtitleFontNameKey];
[_defaults setFloat:fontSize forKey:MSubtitleFontSizeKey];
[self updateSubtitleFontAndSizeUI];
[_movieView setSubtitleFontName:[_defaults stringForKey:MSubtitleFontNameKey]
size:[_defaults floatForKey:MSubtitleFontSizeKey]];
}
#define normalizedFloat normalizedVolume
- (IBAction)subtitleAttributesAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
enum {
SUBTITLE_TEXT_COLOR,
SUBTITLE_TEXT_OPACITY,
SUBTITLE_STROKE_COLOR,
SUBTITLE_STROKE_OPACITY,
SUBTITLE_STROKE_WIDTH,
SUBTITLE_SHADOW_COLOR,
SUBTITLE_SHADOW_OPACITY,
SUBTITLE_SHADOW_BLUR,
SUBTITLE_SHADOW_OFFSET,
SUBTITLE_SHADOW_DARKNESS,
};
switch ([sender tag]) {
case SUBTITLE_TEXT_COLOR :
case SUBTITLE_TEXT_OPACITY : {
NSColor* c = [_subtitleTextColorWell color];
NSColor* textColor = [NSColor colorWithCalibratedRed:[c redComponent]
green:[c greenComponent] blue:[c blueComponent]
alpha:[_subtitleTextOpacitySlider floatValue]];
[_subtitleTextColorWell setColor:textColor];
[_subtitleTextOpacityTextField setFloatValue:[textColor alphaComponent]];
[_defaults setColor:textColor forKey:MSubtitleTextColorKey];
[_movieView setSubtitleTextColor:textColor];
break;
}
case SUBTITLE_STROKE_COLOR :
case SUBTITLE_STROKE_OPACITY : {
NSColor* c = [_subtitleStrokeColorWell color];
NSColor* strokeColor = [NSColor colorWithCalibratedRed:[c redComponent]
green:[c greenComponent] blue:[c blueComponent]
alpha:[_subtitleStrokeOpacitySlider floatValue]];
[_subtitleStrokeColorWell setColor:strokeColor];
[_subtitleStrokeOpacityTextField setFloatValue:[strokeColor alphaComponent]];
[_defaults setColor:strokeColor forKey:MSubtitleStrokeColorKey];
[_movieView setSubtitleStrokeColor:strokeColor];
break;
}
case SUBTITLE_STROKE_WIDTH : {
float strokeWidth = normalizedFloat([_subtitleStrokeWidthSlider floatValue]);
[_subtitleStrokeWidthTextField setFloatValue:strokeWidth];
[_defaults setFloat:strokeWidth forKey:MSubtitleStrokeWidthKey];
[_movieView setSubtitleStrokeWidth:strokeWidth];
break;
}
case SUBTITLE_SHADOW_COLOR :
case SUBTITLE_SHADOW_OPACITY : {
NSColor* c = [_subtitleShadowColorWell color];
NSColor* shadowColor = [NSColor colorWithCalibratedRed:[c redComponent]
green:[c greenComponent] blue:[c blueComponent]
alpha:[_subtitleShadowOpacitySlider floatValue]];
[_subtitleShadowColorWell setColor:shadowColor];
[_subtitleShadowOpacityTextField setFloatValue:[shadowColor alphaComponent]];
[_defaults setColor:shadowColor forKey:MSubtitleShadowColorKey];
[_movieView setSubtitleShadowColor:shadowColor];
break;
}
case SUBTITLE_SHADOW_BLUR : {
float shadowBlur = normalizedFloat([_subtitleShadowBlurSlider floatValue]);
[_subtitleShadowBlurTextField setFloatValue:shadowBlur];
[_defaults setFloat:shadowBlur forKey:MSubtitleShadowBlurKey];
[_movieView setSubtitleShadowBlur:shadowBlur];
break;
}
case SUBTITLE_SHADOW_OFFSET : {
float shadowOffset = normalizedFloat([_subtitleShadowOffsetSlider floatValue]);
[_subtitleShadowOffsetTextField setFloatValue:shadowOffset];
[_defaults setFloat:shadowOffset forKey:MSubtitleShadowOffsetKey];
[_movieView setSubtitleShadowOffset:shadowOffset];
break;
}
case SUBTITLE_SHADOW_DARKNESS : {
float shadowDarkness = (float)[_subtitleShadowDarknessSlider intValue];
[_subtitleShadowDarknessTextField setIntValue:shadowDarkness];
[_defaults setInteger:shadowDarkness forKey:MSubtitleShadowDarknessKey];
[_movieView setSubtitleShadowDarkness:shadowDarkness];
break;
}
}
}
- (IBAction)subtitlePositionAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
enum {
SUBTITLE_POSITION,
SUBTITLE_H_MARGIN,
SUBTITLE_V_MARGIN,
SUBTITLE_LINE_SPACING,
};
switch ([sender tag]) {
case SUBTITLE_POSITION : {
int position = [[_subtitlePositionPopUpButton selectedItem] tag];
[_defaults setInteger:position forKey:MSubtitlePositionKey];
[_appController setSubtitlePosition:position];
break;
}
case SUBTITLE_H_MARGIN : {
float hMargin = [_subtitleHMarginSlider floatValue];
[_subtitleHMarginTextField setFloatValue:hMargin];
[_defaults setFloat:hMargin forKey:MSubtitleHMarginKey];
[_appController setSubtitleHMargin:hMargin];
if ([_defaults boolForKey:MSubtitleAutoFontSizeKey]) {
[self updateFontSizeForAutoFontSizeChars];
}
break;
}
case SUBTITLE_V_MARGIN : {
float vMargin = [_subtitleVMarginSlider floatValue];
[_subtitleVMarginTextField setFloatValue:vMargin];
[_defaults setFloat:vMargin forKey:MSubtitleVMarginKey];
[_appController setSubtitleVMargin:vMargin];
break;
}
case SUBTITLE_LINE_SPACING : {
float spacing = [_subtitleLineSpacingSlider floatValue];
[_subtitleLineSpacingTextField setFloatValue:spacing];
[_defaults setFloat:spacing forKey:MSubtitleLineSpacingKey];
[_appController setSubtitleLineSpacing:spacing];
break;
}
}
}
- (void)initSubtitleEncodingPopUpButton
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_subtitleEncodingPopUpButton removeAllItems];
initSubtitleEncodingMenu([_subtitleEncodingPopUpButton menu], nil);
[_subtitleEncodingPopUpButton selectItemWithTag:[_defaults integerForKey:MSubtitleEncodingKey]];
}
- (void)updateSubtitleFontAndSizeUI
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
NSMutableDictionary* attrs = [NSMutableDictionary dictionaryWithCapacity:3];
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSCenterTextAlignment];
[attrs setObject:[paragraphStyle autorelease] forKey:NSParagraphStyleAttributeName];
float fontSize = [_defaults floatForKey:MSubtitleFontSizeKey];
NSFont* font = [NSFont fontWithName:[_defaults stringForKey:MSubtitleFontNameKey]
size:MIN(fontSize, 20.0)];
[attrs setObject:font forKey:NSFontAttributeName];
NSString* title = [NSString localizedStringWithFormat:
@"%@ %g", [font displayName], fontSize];
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc]
initWithString:title attributes:attrs];
BOOL autoFontSize = [_defaults boolForKey:MSubtitleAutoFontSizeKey];
if (autoFontSize) {
NSRange range;
range.location = [[font displayName] length] + 1;
range.length = [title length] - range.location;
[mas addAttribute:NSForegroundColorAttributeName
value:[NSColor disabledControlTextColor]
range:range];
}
[_subtitleFontButton setAttributedTitle:[mas autorelease]];
int chars = [_defaults integerForKey:MSubtitleAutoFontSizeCharsKey];
[_subtitleAutoFontSizeButton setState:autoFontSize];
[_subtitleAutoFontSizeLabelTextField setEnabled:autoFontSize];
[_subtitleAutoFontSizeTextField setEnabled:autoFontSize];
[_subtitleAutoFontSizeTextField setEditable:autoFontSize];
[_subtitleAutoFontSizeTextField setIntValue:chars];
[_subtitleAutoFontSizeStepper setEnabled:autoFontSize];
[_subtitleAutoFontSizeStepper setIntValue:chars];
}
@end