예제 #1
0
        /// <summary>
        ///    Constructs and initializes a new instance of <see
        ///    cref="Picture" /> by reading in the contents of a
        ///    specified file abstraction.
        /// </summary>
        /// <param name="abstraction">
        ///    A <see cref="File.IFileAbstraction"/> object containing
        ///    abstraction of the file to read.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///    <paramref name="abstraction" /> is <see langword="null"
        ///    />.
        /// </exception>
        public Picture(File.IFileAbstraction abstraction)
        {
            if (abstraction == null)
            {
                throw new ArgumentNullException(nameof(abstraction));
            }

            Data        = ByteVector.FromFile(abstraction);
            Filename    = abstraction.Name;
            Description = abstraction.Name;

            if (!string.IsNullOrEmpty(Filename) && Filename.Contains("."))
            {
                MimeType = GetMimeFromExtension(Filename);
                Type     = MimeType.StartsWith("image/") ? PictureType.FrontCover : PictureType.NotAPicture;
            }
            else
            {
                string ext = GetExtensionFromData(Data);
                MimeType = GetMimeFromExtension(ext);
                if (ext != null)
                {
                    Type     = PictureType.FrontCover;
                    Filename = Description = "cover" + ext;
                }
                else
                {
                    Type     = PictureType.NotAPicture;
                    Filename = "UnknownType";
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a backup file
        /// </summary>
        /// <param name="filename"></param>
        public void CreateBackupFileAsync(string filename = null)
        {
            if (!AutoSaveBackups)
            {
                return;
            }

            // fire and forget
            Task.Run(() =>
            {
                if (string.IsNullOrEmpty(filename))
                {
                    filename = BackupFilename;
                }

                if (Filename == "untitled" || Filename.Contains("saved.bak"))
                {
                    return;
                }

                try
                {
                    File.WriteAllText(filename, CurrentText, Encoding);
                }
                catch
                { }
            });
        }
        public string GetFileExtension()
        {
            if (string.IsNullOrEmpty(Filename) || !Filename.Contains("."))
            {
                return(string.Empty);
            }
            var dotIndex = Filename.LastIndexOf('.');

            return(Filename.Substring(dotIndex));
        }
예제 #4
0
 private string GetFileEnding()
 {
     if (!Filename.Contains("."))
     {
         return("");
     }
     if (Filename.EndsWith("."))
     {
         return("");
     }
     return(this.Filename.Substring(Filename.LastIndexOf('.') + 1).ToLower());
 }
예제 #5
0
        /// <summary>
        /// Creates a backup file
        /// </summary>
        /// <param name="filename"></param>
        public void AutoSaveAsync(string filename = null)
        {
            if (AutoSaveDocuments)
            {
                if (_IsSaving)
                {
                    return;
                }

                Task.Run(() =>
                {
                    filename = Filename;

                    if (filename == "untitled")
                    {
                        return;
                    }

                    Save(filename, true);
                });
            }
            else if (AutoSaveBackups)
            {
                // fire and forget
                Task.Run(() =>
                {
                    if (string.IsNullOrEmpty(filename))
                    {
                        filename = BackupFilename;
                    }

                    if (Filename.Contains("saved.bak"))
                    {
                        return;
                    }

                    if (Filename == "untitled")
                    {
                        filename = Path.Combine(Path.GetTempPath(), "untitled.saved.md");
                    }

                    try
                    {
                        File.WriteAllText(filename, CurrentText, Encoding);
                    }
                    catch
                    { /* ignore save error, write next cycle */ }
                });
            }
        }
예제 #6
0
        /// <summary>
        /// Returns the remote file name for the specified provider.
        /// </summary>
        /// <param name="provider">The cloud provider.</param>
        /// <returns>A </returns>
        public string GetRemoteFileName(StorageProviderTypes provider)
        {
            // Different cloud providers may have different naming rules for URIs.
            // Azure for example is all lowercase required.

            if (FileID == Guid.Empty)
            {
                throw new InvalidOperationException("Cannot generate file name. FileID has not been set.");
            }

            if (provider == StorageProviderTypes.Azure)
            {
                if (Filename.Contains("."))
                {
                    // file with an extension.
                    var split     = Filename.Split('.');
                    var extension = split[split.Length - 1];

                    // we only want alphanumeric characters in the uri
                    // if for some reason windows allows special characters in the extension, dont use the extension in the uri then.
                    var reg = new Regex("^[a-zA-Z0-9]*$");

                    if (extension.Length > 0 && reg.IsMatch(extension))
                    {
                        return(string.Format("{0}-file-{1}-v{2}.{3}", Constants.Logging.AppName, FileID.ToString(), FileRevisionNumber, extension).ToLower());
                    }
                }

                // extensionless (or problematic extension) file.
                return(string.Format("{0}-file-{1}-v{2}", Constants.Logging.AppName, FileID.ToString(), FileRevisionNumber).ToLower());
            }
            else
            {
                throw new NotImplementedException("unexpected provider type: " + provider.ToString());
            }
        }
예제 #7
0
        public Bitmap GeneratePreview(TextEntry entry)
        {
            if (!Font.Characters.Any())
            {
                return(null);
            }

            // Globals
            var scene = Scene.General;

            if (Filename.Contains("tutorial_jpn"))
            {
                scene = Scene.Tutorial;
            }
            if (Filename.Contains("wizpedia_jpn"))
            {
                scene = Scene.Wizpedia;
            }

            var pad = new Dictionary <string, char>
            {
                ["PAD_A"] = 'a', // Circle
                ["PAD_B"] = 'b', // Cross
                ["PAD_X"] = 'c', // Triangle
                ["PAD_Y"] = 'd', // Square
                ["PAD_L"] = 'e', // L1
                ["PAD_R"] = 'f', // R1
                //["PAD_G"] = 'g', // D-Pad
                //["PAD_H"] = 'h', // D-Pad Up
                ["PAD_ADOWN"] = 'i', // D-Pad Down
                //["PAD_J"] = 'j', // D-Pad Left
                //["PAD_K"] = 'k', // D-Pad Right
                ["PAD_CLR"] = 'l',    // D-Pad Left & Right
                //["PAD_CUD"] = 'm', // D-Pad Up & Down
                ["PAD_ANALOG"] = 'n', // Left Analog Stick
                ["PAD_AUP"]    = 'o', // Right Analog Stick
                ["PAD_ALR"]    = 't', // Left/Right Analog Stick
                //["PAD_U"] = 'u' // Hand Cursor
                //["PAD_V"] = 'v' // Plus Sign
                //["PAD_W"] = 'w' // Start
                //["PAD_X"] = 'x' // Select
                ["PAD_L2"] = 'y', // L2
                ["PAD_R2"] = 'z', // R2
            };

            var genSize = 46f;
            var tutSize = 36f;
            var wizSize = 32.25f;

            // Main Kanvas
            var kanvas = new Bitmap(background.Width, background.Height, PixelFormat.Format32bppArgb);

            kanvas.SetResolution(96, 96);

            var lines          = 1;
            var fontHeight     = Font.Characters.First().GlyphHeight;
            var textBoxOffsetX = 166f;
            var textBoxOffsetY = 42;
            var textOffsetX    = 66;
            var boxWidth       = 400;
            var lineSpacing    = 4f;
            var size           = genSize;

            var magic = 0.02173913043478260869565217391304f; // 0.04166666666666666666666666666667

            float x = textBoxOffsetX + textOffsetX, y = 0;
            float scaleX = 1.0f, scaleY = 1.0f;

            using (var gfx = Graphics.FromImage(kanvas))
            {
                gfx.SmoothingMode     = SmoothingMode.HighQuality;
                gfx.InterpolationMode = InterpolationMode.Bicubic;
                gfx.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                // Cleanup text
                var str = entry.EditedText.Replace("\r", "");
                Font.SetColor(Color.Black);

                switch (scene)
                {
                case Scene.General:
                    gfx.DrawImage(background, 0, 0);
                    gfx.DrawImage(textBox, textBoxOffsetX, textBoxOffsetY, textBox.Width * 1.1f, textBox.Height * 1.1f);

                    // Cursor
                    int cursorX = 627, cursorY = 161;
                    var cursorScale = 0.825f;
                    gfx.DrawImage(cursor,
                                  new[]
                    {
                        new PointF(cursorX, cursorY),
                        new PointF(cursorX + cursor.Width * cursorScale, cursorY),
                        new PointF(cursorX, cursorY + cursor.Height * cursorScale)
                    },
                                  new RectangleF(0, 0, cursor.Width, cursor.Height), GraphicsUnit.Pixel
                                  );

                    // Wrap text
                    var results = TextWrapper.WrapText(str, Font, PadFont, new RectangleF(textBoxOffsetX + textOffsetX, textBoxOffsetY, boxWidth, textBox.Height), scaleX, 0, "\n");
                    str   = results.Text;
                    lines = results.LineCount;
                    size  = genSize;

                    // Set
                    x = textBoxOffsetX + textOffsetX;
                    y = textBoxOffsetY + textBox.Height * 1.1f / 2 - lines * fontHeight / 2 - (lines - 1) * lineSpacing / 2;

                    break;

                case Scene.Tutorial:
                    gfx.DrawImage(tutorial, 0, 0);

                    // Configure
                    textBoxOffsetX = 368.5f;
                    textOffsetX    = 0;
                    textBoxOffsetY = 465;
                    lineSpacing    = 2;
                    size           = tutSize;

                    // Set
                    x = textBoxOffsetX + textOffsetX;
                    y = textBoxOffsetY;
                    break;

                case Scene.Wizpedia:
                    gfx.DrawImage(wizpedia, 0, 0);

                    // Template
                    //DrawTransparentImage(gfx, new Bitmap(Path.Combine(PluginDirectory, _ID, "wizpedia_template.png")), 0.25f);

                    // Configure
                    textBoxOffsetX = 695f;
                    textOffsetX    = 0;
                    textBoxOffsetY = 302;
                    lineSpacing    = -1.25f;
                    size           = wizSize;

                    // Set
                    x = textBoxOffsetX + textOffsetX;
                    y = textBoxOffsetY;
                    break;
                }

                // Draw text
                for (var i = 0; i < str.Length; i++)
                {
                    var c = str[i];

                    if (c == '<')
                    {
                        var tag      = Regex.Match(str.Substring(i), @"</?(\w+) ?(.*?)>").Value;
                        var code     = Regex.Match(tag, @"(?<=</?)\w+").Value;
                        var isCloser = Regex.Match(tag, @"</\w+").Value.StartsWith("</");

                        switch (code)
                        {
                        case "COL":
                            Font.SetColor(isCloser ? Color.Black : ColorTranslator.FromHtml("#" + Regex.Match(tag, @"[0-9A-F]{6}").Value));
                            break;

                        case "SIZE":
                            if (isCloser)
                            {
                                if (scene == Scene.General)
                                {
                                    size = genSize;
                                }
                                else if (scene == Scene.Tutorial)
                                {
                                    size = tutSize;
                                }
                                else if (scene == Scene.Wizpedia)
                                {
                                    size = wizSize;
                                }
                            }
                            else
                            {
                                size = Convert.ToInt32(Regex.Match(tag, @"(?<= )\d+").Value);
                            }
                            break;

                        case "ICON":
                            var icon  = Regex.Match(tag, @"(?<= )\w+").Value;
                            var p     = pad[icon];
                            var scale = size * magic;     // Magic scaling value

                            PadFont.Draw(p, gfx, x, y, scale, scale);
                            x += PadFont.GetCharWidthInfo(p).GlyphWidth *scale;
                            break;

                        case "WIZP":
                            Font.SetColor(isCloser ? Color.Black : Color.Red);
                            break;
                        }

                        i += tag.Length - 1;
                    }
                    else
                    {
                        if (c == '\n')
                        {
                            x  = textBoxOffsetX + textOffsetX;
                            y += fontHeight + lineSpacing;
                            continue;
                        }

                        Font.Draw(c, gfx, x, y, size * magic, size * magic);
                        x += Font.GetCharWidthInfo(c).GlyphWidth *(size * magic);
                    }
                }
            }

            return(kanvas);
        }
예제 #8
0
        /// <summary>
        /// Creates a backup file
        /// </summary>
        /// <param name="filename"></param>
        public void AutoSaveAsync(string filename = null)
        {
            if (AutoSaveDocuments)
            {
                if (_IsSaving)
                {
                    return;
                }

                Task.Run(() =>
                {
                    filename = Filename;

                    if (filename == "untitled")
                    {
                        return;
                    }

                    try
                    {
                        lock (_SaveLock)
                        {
                            File.WriteAllText(filename, CurrentText, Encoding);
                            OriginalText = CurrentText;
                            UpdateCrc(filename);

                            if (Dispatcher != null)
                            {
                                // need dispatcher in order to handle the
                                // hooked up OnPropertyChanged events that fire
                                // on the UI which otherwise fail.
                                Dispatcher.InvokeAsync(() => { IsDirty = false; });
                            }
                            else
                            {
                                IsDirty = false;
                            }
                        }
                    }
                    catch
                    {
                        /* ignore save error, write next cycle */
                    }
                });
            }
            else if (AutoSaveBackups)
            {
                // fire and forget
                Task.Run(() =>
                {
                    if (string.IsNullOrEmpty(filename))
                    {
                        filename = BackupFilename;
                    }

                    if (Filename == "untitled" || Filename.Contains("saved.bak"))
                    {
                        return;
                    }

                    try
                    {
                        File.WriteAllText(filename, CurrentText, Encoding);
                    }
                    catch
                    { /* ignore save error, write next cycle */ }
                });
            }
        }