Refactor to VLTextLayout, with original algorithm

This commit is contained in:
Matthias Neeracher 2007-12-25 16:49:45 +00:00
parent efbc96a7f8
commit 19cea3837c
3 changed files with 109 additions and 19 deletions

View File

@ -91,3 +91,45 @@ float VLLayout::NotePosition(int measure, VLFraction at) const
} }
return 0.0f; return 0.0f;
} }
VLFontHandler::~VLFontHandler()
{
}
VLTextLayout::VLTextLayout(VLFontHandler * regular, VLFontHandler * narrow)
: fRegularFont(regular), fNarrowFont(narrow)
{
}
void VLTextLayout::AddSyllable(const VLSyllable & syll, float x)
{
VLLayoutSyll ls;
static_cast<VLSyllable &>(ls) = syll;
ls.fX = x;
fSyllables.push_back(ls);
}
typedef std::vector<VLLayoutSyll>::iterator VLSyllIter;
#define NARROW_SPACE "\xE2\x80\x89"
void VLTextLayout::DrawLine(float y)
{
VLSyllIter syll = fSyllables.begin();
VLSyllIter end = fSyllables.end();
while (syll != end) {
std::string text = syll->fText;
float x = syll->fX-0.5*fRegularFont->Width(text.c_str());
if (syll == fSyllables.begin() && syll->fKind & VLSyllable::kHasPrev)
text = "-" NARROW_SPACE + text;
if (syll->fKind & VLSyllable::kHasNext)
text += NARROW_SPACE "-";
fRegularFont->Draw(x, y, text.c_str());
++syll;
}
}

View File

@ -43,6 +43,29 @@ public:
float NotePosition(int measure, VLFraction at) const; float NotePosition(int measure, VLFraction at) const;
}; };
class VLFontHandler {
public:
virtual void Draw(float x, float y, const char * utf8Text) = 0;
virtual float Width(const char * utf8Text) = 0;
virtual ~VLFontHandler();
};
struct VLLayoutSyll : public VLSyllable {
float fX;
};
class VLTextLayout {
public:
VLTextLayout(VLFontHandler * regular, VLFontHandler * narrow);
void AddSyllable(const VLSyllable & syll, float x);
void DrawLine(float y);
private:
VLFontHandler * fRegularFont;
VLFontHandler * fNarrowFont;
std::vector<VLLayoutSyll> fSyllables;
};
// Local Variables: // Local Variables:
// mode:C++ // mode:C++
// End: // End:

View File

@ -100,17 +100,50 @@
@end @end
class VLCocoaFontHandler : public VLFontHandler {
public:
VLCocoaFontHandler(NSString * name, float size);
virtual void Draw(float x, float y, const char * utf8Text);
virtual float Width(const char * utf8Text);
private:
NSDictionary * fTextAttr;
};
VLCocoaFontHandler::VLCocoaFontHandler(NSString * name, float size)
{
NSFont * font = [NSFont fontWithName:name size:size];
fTextAttr =
[[NSDictionary alloc] initWithObjectsAndKeys:
font, NSFontAttributeName, nil];
}
void VLCocoaFontHandler::Draw(float x, float y, const char * utf8Text)
{
NSString * t = [NSString stringWithUTF8String:utf8Text];
[t drawAtPoint:NSMakePoint(x,y) withAttributes:fTextAttr];
}
float VLCocoaFontHandler::Width(const char * utf8Text)
{
NSString * t = [NSString stringWithUTF8String:utf8Text];
NSSize sz= [t sizeWithAttributes:fTextAttr];
return sz.width;
}
@implementation VLSheetView (Lyrics) @implementation VLSheetView (Lyrics)
- (void) drawLyricsForSystem:(int)system stanza:(size_t)stanza - (void) drawLyricsForSystem:(int)system stanza:(size_t)stanza
{ {
static NSDictionary * sLyricsFont = nil; static VLFontHandler * sRegularFont = nil;
if (!sLyricsFont) static VLFontHandler * sNarrowFont = nil;
sLyricsFont = if (!sRegularFont) {
[[NSDictionary alloc] initWithObjectsAndKeys: sRegularFont = new VLCocoaFontHandler(@"Arial", 12.0f);
[NSFont fontWithName: @"Helvetica" size: 12], sNarrowFont = new VLCocoaFontHandler(@"ArialNarrow", 12.0f);
NSFontAttributeName, }
nil]; VLTextLayout text(sRegularFont, sNarrowFont);
const VLSong * song = [self song]; const VLSong * song = [self song];
const float kSystemY = [self systemY:system]; const float kSystemY = [self systemY:system];
@ -136,22 +169,14 @@
) { ) {
; ;
} else { } else {
NSString * syll = text.AddSyllable(note->fLyrics[stanza-1],
[NSString stringWithUTF8String: [self noteXInMeasure:measIdx at:at]);
note->fLyrics[stanza-1].fText.c_str()];
NSSize sz =
[syll sizeWithAttributes:sLyricsFont];
NSPoint syllLoc =
NSMakePoint([self noteXInMeasure:measIdx at:at]
- 0.5f*sz.width,
kSystemY+kLyricsY-stanza*kLyricsH);
if (note->fLyrics[stanza-1].fKind & VLSyllable::kHasNext)
syll = [syll stringByAppendingString:@" -"];
[syll drawAtPoint:syllLoc withAttributes:sLyricsFont];
} }
at += note->fDuration; at += note->fDuration;
} }
} }
text.DrawLine(kSystemY+kLyricsY-stanza*kLyricsH);
} }
- (void) editLyrics - (void) editLyrics