public static void AddCharacterGlyph(this DrawingVisualHost visualHost, Point position, string character, bool isSmall = false, Brush color = null) { Brush characterColor = color ?? Brushes.Black; DrawingVisual visual = new DrawingVisual(); ushort glyphIndex = character.GetGlyphIndexOfCharacter(); PageProperties pageProperties = (PageProperties)ViewModel.ViewModelLocator.Instance.Main.CurrentPageLayout; GlyphTypeface gtf; Typeface typeFace = TypeFaces.BravuraMusicFont; typeFace.TryGetGlyphTypeface(out gtf); double smallFactor = isSmall ? 0.7 : 1; Point calculatedPosition = new Point(position.X, position.Y /* + pageProperties.IndexStaffLinePositions[3]*/); double characterSize = pageProperties.StaffHeight.MMToWPFUnit() * smallFactor; using (DrawingContext dc = visual.RenderOpen()) { GlyphRun gr = new GlyphRun(gtf, 0, // Bi-directional nesting level false, // isSideways characterSize, // pt size new ushort[] { glyphIndex }, // glyphIndices calculatedPosition, // baselineOrigin new double[] { 0.0 }, // advanceWidths null, // glyphOffsets null, // characters null, // deviceFontName null, // clusterMap null, // caretStops null); // xmlLanguage dc.DrawGlyphRun(characterColor, gr); } visualHost.AddVisual(visual); }
private void Draw() { LayoutStyle.NotesLayoutStyle notesStyle = ViewModel.ViewModelLocator.Instance.Main.CurrentLayout.LayoutStyle.NotesStyle; DrawingVisual dravingVisualStem = new DrawingVisual(); using (DrawingContext dc = dravingVisualStem.RenderOpen()) { dc.DrawLine(new Pen(note.Color, notesStyle.StemThickness * sizeFactor), startPoint, endPoint); } //! check if added previously to prevent unnecessary remove+add calls bool stemVisualAdded = false; if (stemVisual == null) { stemVisual = new DrawingVisualHost(); stemVisual.Tag = "stem"; } else { stemVisual.ClearVisuals(); stemVisualAdded = true; //! no need to add to noteVisual } //! stemVisual.AddVisual(dravingVisualStem); //! add visual to noteVisual (one time only) if (!stemVisualAdded) { note.AddStem(stemVisual); } }
public void Draw(Dictionary <int, double> positionsTable) { Dictionary <int, Point> stemPositions = new Dictionary <int, Point>(); CalculateSlopes(positionsTable); CorrectStems(); foreach (var item in beamedStems) { int direction = item.Value.Stem.IsDirectionDown() ? -1 : 1; double offset = 2.5.TenthsToWPFUnit() * direction; Dictionary <string, Point> beamPositions = new Dictionary <string, Point>(); Point position = new Point(positionsTable[item.Key] + beamedStems[item.Key].Stem.GetStemEndCalculated().X, beamedStems[item.Key].Stem.GetStemEndCalculated().Y); double tempYOffset = offset; for (int i = 1; i <= item.Value.Beams.Count; i++) { beamPositions.Add(i.ToString(), new Point(position.X, position.Y + tempYOffset)); tempYOffset += 7.5.TenthsToWPFUnit() * direction; } stemPositions.Add(item.Key, new Point(positionsTable[item.Key] + beamedStems[item.Key].Stem.GetStemEndCalculated().X, beamedStems[item.Key].Stem.GetStemEndCalculated().Y + offset)); } DrawingVisual beamsDrawingVisual = new DrawingVisual(); var fractionKeys = stemPositions.Keys.ToArray(); int dir = beamedStems.FirstOrDefault().Value.Stem.IsDirectionDown() ? -1 : 1; using (DrawingContext dc = beamsDrawingVisual.RenderOpen()) { for (int i = 1; i < stemPositions.Count; i++) { //! skip counting beam hooks int beamsCount = beamedStems[fractionKeys[i - 1]].Beams.Count( x => x.Value != Model.Helpers.SimpleTypes.BeamValueMusicXML.forwardhook || x.Value != Model.Helpers.SimpleTypes.BeamValueMusicXML.backwardhook); for (int j = 0; j < beamsCount; j++) { if (beamedStems[fractionKeys[i]].Beams.ContainsKey(j + 1))//? necessary to skip beam drawing if current beam is end { dc.DrawLine(new Pen(beamedStems.FirstOrDefault().Value.Stem.GetColor(), 5.0.TenthsToWPFUnit()), new Point(stemPositions[fractionKeys[i - 1]].X, stemPositions[fractionKeys[i - 1]].Y + j * (7.5.TenthsToWPFUnit() * dir)), new Point(stemPositions[fractionKeys[i]].X, stemPositions[fractionKeys[i]].Y + j * (7.5.TenthsToWPFUnit() * dir))); } if (beamedStems[fractionKeys[i]].Beams.ContainsValue(Model.Helpers.SimpleTypes.BeamValueMusicXML.backwardhook) && j + 1 == beamsCount) { DrawBeamHooks(dc, i, j, stemPositions, Model.Helpers.SimpleTypes.BeamValueMusicXML.backwardhook); } if (beamedStems[fractionKeys[i - 1]].Beams.ContainsValue(Model.Helpers.SimpleTypes.BeamValueMusicXML.forwardhook) && j + 1 == beamsCount) { //? j-1 temp, need tests (without decrementing hook is drawn with one beamSpacing lower DrawBeamHooks(dc, i, j - 1, stemPositions, Model.Helpers.SimpleTypes.BeamValueMusicXML.forwardhook); } } } } DrawingVisualHost drawingVisualHost = new DrawingVisualHost(); drawingVisualHost.AddVisual(beamsDrawingVisual); beamVisuals.Add(drawingVisualHost); }
public static void AddLedgerLine(this DrawingVisualHost canvas, Point position, double noteHeadWidth) { DrawingVisual ledgerLine = new DrawingVisual(); Pen pen = new Pen(Brushes.Black, (0.9).TenthsToWPFUnit()); double offset = noteHeadWidth / 4; Point p1 = new Point(position.X - offset, position.Y); Point p2 = new Point(position.X + noteHeadWidth + offset, position.Y); using (DrawingContext dc = ledgerLine.RenderOpen()) { dc.DrawLine(pen, p1, p2); } canvas.AddVisual(ledgerLine); }
/// <summary> /// Visual red barline drawn at the beginning of measure /// </summary> /// <param name="staffHeight">Staff Line height (WPFUnits)</param> private void DrawTempStartBarline(double staffHeight) { temporaryStartBarline = new Canvas(); Point p1 = new Point(); Point p2 = new Point(0, staffHeight); Pen pen = new Pen(Brushes.Red, 1.5.TenthsToWPFUnit()); DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { dc.DrawLine(pen, p1, p2); } DrawingVisualHost dvh = new DrawingVisualHost(); dvh.AddVisual(dv); temporaryStartBarline.Children.Add(dvh); Children.Add(temporaryStartBarline); }
private void Draw(bool updatePositions = false) { DrawingVisual staffLineVisual = new DrawingVisual(); Pen pen = new Pen(Color, LineThickness); if (updatePositions || _yOffsets.Count == 0) { GenerateStaffLinesPositions(); } using (DrawingContext dc = staffLineVisual.RenderOpen()) { for (int i = 1; i <= _linesCount; i++) { dc.DrawLine(pen, new Point(0.0, _yOffsets[i]), new Point(Width, _yOffsets[i])); } } _visual.ClearVisuals(); _visual.AddVisual(staffLineVisual); }
private void CreateStaffLine() { Point p = new Point(0, layout.PageProperties.StaffHeight.MMToWPFUnit()); staffLinesCoords = new double[stavesCount]; staffLinesYpositions = new Dictionary <int, double[]>(); if (visualObject.Count != 0) { visualObject.ClearVisuals(); } for (uint i = 0; i < stavesCount; i++) { p.Y += (stavesDistance.TenthsToWPFUnit() + layout.PageProperties.StaffHeight.MMToWPFUnit()) * i; DrawableStaffLine staff = new DrawableStaffLine(layout.PageProperties, measureWidth, offsetPoint: p); staffLinesYpositions.Add((int)i + 1, staff.LinesYpositions); staffLinesCoords[i] = p.Y; visualObject.AddVisual(staff.PartialObjectVisual); } }