/// <summary> /// Background task work method. /// </summary> private void DoPrepareSvg(object sender, DoWorkEventArgs e) { // Lock to allow only one of these operations at a time. lock (_updateLock) { KanjiDao dao = new KanjiDao(); // Get the kanji strokes. KanjiStrokes strokes = dao.GetKanjiStrokes(_kanjiEntity.DbKanji.ID); if (strokes != null && strokes.FramesSvg.Length > 0) { // If the strokes was successfuly retrieved, we have to read the compressed SVG contained inside. string svgString = StringCompressionHelper.Unzip(strokes.FramesSvg); StrokesCount = Regex.Matches(svgString, "<circle").Count; DispatcherHelper.Invoke(() => { StrokesImage = new Avalonia.Svg.Skia.SvgImage { Source = new SvgSource { Picture = new SKSvg().FromSvg(svgString) } }; SetCurrentStroke(1); }); } } }
/// <summary> /// Background task work method. /// </summary> private void DoPrepareSvg(object sender, DoWorkEventArgs e) { // Lock to allow only one of these operations at a time. lock (_updateLock) { KanjiDao dao = new KanjiDao(); // Get the kanji strokes. KanjiStrokes strokes = dao.GetKanjiStrokes(_kanjiEntity.DbKanji.ID); if (strokes != null && strokes.FramesSvg.Length > 0) { // If the strokes was successfuly retrieved, we have to read the compressed SVG contained inside. SharpVectors.Renderers.Wpf.WpfDrawingSettings settings = new SharpVectors.Renderers.Wpf.WpfDrawingSettings(); using (FileSvgReader r = new FileSvgReader(settings)) { // Unzip the stream and remove instances of "px" that are problematic for SharpVectors. string svgString = StringCompressionHelper.Unzip(strokes.FramesSvg); svgString = svgString.Replace("px", string.Empty); StrokesCount = Regex.Matches(svgString, "<circle").Count; using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(svgString))) { // Then read the stream to a DrawingGroup. // We are forced to do this operation on the UI thread because DrawingGroups must // be always manipulated by the same thread. DispatcherHelper.Invoke(() => { StrokesDrawingGroup = r.Read(stream); SetCurrentStroke(1); }); } } } } }
/// <summary> /// Attempts to retrieve the kanji strokes SVG matching the given kanji inside the zip file. /// </summary> /// <param name="k">Target kanji.</param> /// <returns>A kanji strokes entity, never null, that contains either the retrieved data or an /// empty byte array when the entry was not found.</returns> private KanjiStrokes RetrieveSvg(KanjiEntity k) { KanjiStrokes strokes = new KanjiStrokes(); strokes.FramesSvg = new byte[0]; if (!k.UnicodeValue.HasValue) { return(strokes); } ZipArchive svgZip = GetSvgZipArchive(); string entryName = string.Format("{0}_frames.svg", k.UnicodeValue.Value); ZipArchiveEntry entry = svgZip.GetEntry(entryName); if (entry != null) { using (Stream stream = entry.Open()) { strokes.FramesSvg = StringCompressionHelper.Zip(StreamHelper.ReadToEnd(stream)); StringCompressionHelper.Unzip(strokes.FramesSvg); } } return(strokes); }
//public KanjiStrokes GetKanjiStrokes(long id) //{ // KanjiStrokes result = null; // DaoConnection connection = null; // try // { // // Create and open synchronously the primary Kanji connection. // connection = DaoConnection.Open(DaoConnectionEnum.KanjiDatabase); // // FILTERS COMPUTED. // // Execute the final request. // IEnumerable<NameValueCollection> results = connection.Query( // "SELECT * " // + "FROM " + SqlHelper.Table_KanjiStrokes + " ks " // + "WHERE ks." + SqlHelper.Field_KanjiStrokes_Id + "=@ks;", // new DaoParameter("@ks", id)); // if (results.Any()) // { // KanjiStrokesBuilder builder = new KanjiStrokesBuilder(); // result = builder.BuildEntity(results.First(), null); // } // } // finally // { // if (connection != null) // { // connection.Dispose(); // } // } // return result; //} public KanjiStrokes GetKanjiStrokes(long id) { KanjiStrokes result = new KanjiStrokes(); result.ID = id; result.FramesSvg = new byte[0]; DaoConnection connection = null; SQLiteDataReader reader = null; try { // Create and open synchronously the primary Kanji connection. connection = DaoConnection.Open(DaoConnectionEnum.KanjiDatabase); reader = connection.QueryDataReader( string.Format("SELECT {0} FROM {1} WHERE {2}=@id;", SqlHelper.Field_KanjiStrokes_FramesSvg, SqlHelper.Table_KanjiStrokes, SqlHelper.Field_KanjiStrokes_Id), new DaoParameter("@id", id)); while (reader.Read()) { result.FramesSvg = GetBytes(reader); } } finally { if (reader != null) { reader.Close(); reader.Dispose(); } if (connection != null) { connection.Dispose(); } } return(result); }