Beispiel #1
0
 public ManifestFileNavigation(TagFile node, string base_path) : base(node, base_path)
 {
 }
Beispiel #2
0
        public SVG(string file_text, string file_path)
        {
            int viewboxes  = 0;
            int transforms = 0;

            var list = new List <List <Drawable> >();

            var file = TagFile.ParseText(file_text);

            foreach (var b in file.Enumerate())
            {
                if (b is TagFile)
                {
                    var tf = b as TagFile;

                    string outp;

                    switch (tf._Name)
                    {
                    case "svg":
                        if (tf._Params.TryGetValue("viewBox", out outp))
                        {
                            var strs = outp.Split(
                                new char[] { ' ' },
                                StringSplitOptions.RemoveEmptyEntries);

                            if (strs.Length == 4)
                            {
                                viewboxes++;
                                this._ViewBox.X      = int.Parse(strs[0]);
                                this._ViewBox.Y      = int.Parse(strs[1]);
                                this._ViewBox.Width  = int.Parse(strs[2]);
                                this._ViewBox.Height = int.Parse(strs[3]);
                            }
                            else
                            {
                                Console.WriteLine("ViewBox Error: " + outp);
                            }
                        }
                        break;

                    case "g":
                        if (tf._Params.TryGetValue("transform", out outp))
                        {
                            transforms++;
                            this._Transform = this.ParseTransform(outp);
                        }
                        break;

                    case "path":
                        // Handled Later!
                        break;
                    }
                }
            }

            if (transforms != 1)
            {
                string s = "SVG has " + transforms + " transforms";
                Console.WriteLine(s);
                throw new Exception(s);
            }

            if (viewboxes != 1)
            {
                string s = "SVG has " + transforms + " transforms";
                Console.WriteLine(s);
                throw new Exception(s);
            }

            foreach (var b in file.Enumerate())
            {
                if (b is TagFile)
                {
                    var tf = b as TagFile;
                    switch (tf._Name)
                    {
                    case "path":
                        foreach (var kvp in tf._Params)
                        {
                            switch (kvp.Key)
                            {
                            case "id":
                                break;

                            case "d":
                                if (kvp.Value.Contains("NaN"))
                                {
                                    Console.WriteLine(file_path + " contains some NaN's");
                                }
                                else
                                {
                                    this.ParsePath(this.Enumerate(kvp.Value), ref list);
                                }
                                break;

                            default:
                                Console.WriteLine("Path Error: " + kvp.Key + ", " + kvp.Value);
                                break;
                            }
                        }

                        break;
                    }
                }
            }

            var           ls                = new List <Drawable>();
            DrawableGroup last_group        = null;
            float         last_group_length = 0;

            foreach (var continuity in list)
            {
                foreach (var element in continuity)
                {
                    element.BreakIntoSmallPortions(
                        ref ls,
                        ref last_group,
                        ref last_group_length,
                        SVG.INCREMENT_DISTANCE);
                }
            }

            this._LiveDraw = ls.ToArray();
        }
Beispiel #3
0
 /// <summary>
 /// Saves the tag, wrapping it in the supplied TagFile.
 /// </summary>
 public void SaveTag(Tag tag, TagFile tagFile)
 {
     VerifyTagIsSavableToSpecifiedLocation(tag);
     // Until we have the revision functionality more solidified, we will just save over the head revision.
     SaveTagToHeadRevision(tag, tagFile);
 }
Beispiel #4
0
        /// <summary>
        /// Save the specified tag's data over the head revision of the tag file at the specified tag path.
        /// </summary>
        private void SaveTagToHeadRevision(Tag tag)
        {
            TagFile tf = GetTagFile(tag.TagPath);

            SaveTagToHeadRevision(tag, tf);
        }
        private static void xamlParseTransform(
            TagFile f0,
            float xScale,
            float yScale,
            float zScale,
            CadObject co)
        {
            var u1 = new HashSet<string>();
            foreach (var e1 in f0._Children)
            {
                if (e1 is TagFile)
                {
                    var f1 = e1 as TagFile;
                    if (u1.Contains(f1._Name)) Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " multiple " + f1._Name);
                    else
                    {
                        u1.Add(f1._Name);
                        switch (f1._Name)
                        {
                            case "MatrixTransform3D":
                                {
                                    foreach (var kv in f1._Params.AsEnumerable())
                                    {
                                        switch (kv.Key)
                                        {
                                            case "Matrix":
                                                {
                                                    var vals = kv.Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                                    var points = new List<double>();
                                                    double a;
                                                    foreach (var line in vals)
                                                        if (double.TryParse(line, out a))
                                                            points.Add(a);

                                                    if (points.Count == 16)
                                                    {
                                                        int i = 0;
                                                        co.BoolUseTranslationAndRotation = true;
                                                        co._Matrix.M11 = (float)points[i++];
                                                        co._Matrix.M12 = (float)points[i++];
                                                        co._Matrix.M13 = (float)points[i++];
                                                        co._Matrix.M14 = (float)points[i++];

                                                        co._Matrix.M21 = (float)points[i++];
                                                        co._Matrix.M22 = (float)points[i++];
                                                        co._Matrix.M23 = (float)points[i++];
                                                        co._Matrix.M24 = (float)points[i++];

                                                        co._Matrix.M31 = (float)points[i++];
                                                        co._Matrix.M32 = (float)points[i++];
                                                        co._Matrix.M33 = (float)points[i++];
                                                        co._Matrix.M34 = (float)points[i++];

                                                        co._Matrix.M41 = (float)points[i++];
                                                        co._Matrix.M42 = (float)points[i++];
                                                        co._Matrix.M43 = (float)points[i++];
                                                        co._Matrix.M44 = (float)points[i++];

                                                        co._Matrix.M41 *= xScale;
                                                        co._Matrix.M42 *= yScale;
                                                        co._Matrix.M43 *= zScale;
                                                    }
#if DEBUG
                                                    else Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " matrix doesn't have 16 values!");
#endif
                                                    break;
                                                }
                                            default:
#if DEBUG
                                                Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + ", MatrixTransform3D ignoring " + kv);
#endif

                                                break;
                                        }
                                    }
                                    break;
                                }
                            case "RotateTransform3D":
                                {
                                    bool no_angle = false;

                                    if (f1._Children.Length == 1)
                                    {
                                        var f2 = f1._Children[0] as TagFile;
                                        if (f2 != null)
                                        {
                                            if ((f2._Children.Length == 1) && ("RotateTransform3D.Rotation".Equals(f2._Name)))
                                            {
                                                var f3 = f2._Children[0] as TagFile;
                                                if (f3 != null)
                                                {
                                                    if ("AxisAngleRotation3D".Equals(f3._Name))
                                                    {
                                                        string angle;
                                                        if (f3._Params.TryGetValue("Angle", out angle))
                                                        {
                                                            if ("0".Equals(angle))
                                                            {
                                                                no_angle = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (!no_angle) f1.Display();
                                    break;
                                }
                            default:
#if DEBUG
                                Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " ignoring " + f1._Name);
#endif
                                break;
                        }
                    }
                }
            }
        }
        private static void xamlParseColor(
            TagFile f0,
            bool useAmbient,
            bool useDiffuse,
            bool useSpecular,
            bool useEmission,
            CadObject co)
        {
            //            f0.display();

            String output;
            const String SCOLOR = "Color";
            const String SOPAC = "Opacity";

            String matchD = "DiffuseMaterial";
            String matchS = "SpecularMaterial";
            String matchA = "AmbientMaterial";
            String matchE = "EmissionMaterial";
            String matchX = "SolidColorBrush";

            var matchD_ = f0.getMatches(ref matchD, ref matchX);
            var matchS_ = f0.getMatches(ref matchS, ref matchX);
            var matchA_ = f0.getMatches(ref matchA, ref matchX);
            var matchE_ = f0.getMatches(ref matchE, ref matchX);

            Color diffuse = Color.Black;
            Color specular = Color.Black;
            Color ambient = Color.Black;
            Color emission = Color.Black;

            if (matchD_.Count == 1 && useDiffuse)
            {
                var dict = matchD_[0]._Params;
                if (dict.TryGetValue(SCOLOR, out output))
                {
                    Int32 iColorInt = Convert.ToInt32(output.Substring(1), 16);
                    diffuse = System.Drawing.Color.FromArgb(iColorInt);
                }
                if (dict.TryGetValue(SOPAC, out output))
                {
                    Double a;
                    if (Double.TryParse(output, out a))
                    {
                        Byte alpha = (Byte)Math.Min(255, Math.Max(0, (a * 255)));
                        diffuse = Color.FromArgb(alpha, diffuse);
                    }
                }
            }

            if (matchS_.Count == 1 && useSpecular)
            {
                var dict = matchS_[0]._Params;
                if (dict.TryGetValue(SCOLOR, out output))
                {
                    Int32 iColorInt = Convert.ToInt32(output.Substring(1), 16);
                    specular = System.Drawing.Color.FromArgb(iColorInt);
                }
                if (dict.TryGetValue(SOPAC, out output))
                {
                    Double a;
                    if (Double.TryParse(output, out a))
                    {
                        Byte alpha = (Byte)Math.Min(255, Math.Max(0, (a * 255)));
                        specular = Color.FromArgb(alpha, specular);
                    }
                }
            }

            if (matchA_.Count == 1 && useAmbient)
            {
                var dict = matchA_[0]._Params;
                if (dict.TryGetValue(SCOLOR, out output))
                {
                    Int32 iColorInt = Convert.ToInt32(output.Substring(1), 16);
                    ambient = System.Drawing.Color.FromArgb(iColorInt);
                }
                if (dict.TryGetValue(SOPAC, out output))
                {
                    Double a;
                    if (Double.TryParse(output, out a))
                    {
                        Byte alpha = (Byte)Math.Min(255, Math.Max(0, (a * 255)));
                        ambient = Color.FromArgb(alpha, ambient);
                    }
                }
            }
            else if (useAmbient) ambient = specular;

            if (matchE_.Count == 1 && useEmission)
            {
                var dict = matchE_[0]._Params;
                if (dict.TryGetValue(SCOLOR, out output))
                {
                    Int32 iColorInt = Convert.ToInt32(output.Substring(1), 16);
                    emission = System.Drawing.Color.FromArgb(iColorInt);
                }
                if (dict.TryGetValue(SOPAC, out output))
                {
                    Double a;
                    if (Double.TryParse(output, out a))
                    {
                        Byte alpha = (Byte)Math.Min(255, Math.Max(0, (a * 255)));
                        emission = Color.FromArgb(alpha, emission);
                    }
                }
            }

            co._Color = new ColorGL();
            co._Color._Diffuse[0] = diffuse.R / 255.0f;
            co._Color._Diffuse[1] = diffuse.G / 255.0f;
            co._Color._Diffuse[2] = diffuse.B / 255.0f;
            co._Color._Diffuse[3] = diffuse.A / 255.0f;

            co._Color._Ambient[0] = ambient.R / 255.0f;
            co._Color._Ambient[1] = ambient.G / 255.0f;
            co._Color._Ambient[2] = ambient.B / 255.0f;
            co._Color._Ambient[3] = ambient.A / 255.0f;

            co._Color._Specular[0] = specular.R / 255.0f;
            co._Color._Specular[1] = specular.G / 255.0f;
            co._Color._Specular[2] = specular.B / 255.0f;
            co._Color._Specular[3] = specular.A / 255.0f;

            co._Color._Emission[0] = emission.R / 255.0f;
            co._Color._Emission[1] = emission.G / 255.0f;
            co._Color._Emission[2] = emission.B / 255.0f;
            co._Color._Emission[3] = emission.A / 255.0f;
        }
        public static void xamlModelVisual3DGroupChildren(
            TagFile f0,
            float xScale,
            float yScale,
            float zScale,
            bool useAmbient,
            bool useDiffuse,
            bool useSpecular,
            bool useEmission,
            List<CadObject> ls)
        {
            // var u3 = new HashSet<string>();
            foreach (var e1 in f0._Children)
            {
                if (e1 is TagFile)
                {
                    var f1 = e1 as TagFile;
                    // if (u3.Contains(f3._Name)) Console.WriteLine("CadObjectGenerator: Model3DGroup.Children multiple " + f3._Name);
                    // else
                    // {
                    // u3.Add(f3._Name);
                    switch (f1._Name)
                    {
                        case "GeometryModel3D":
                            ls.Add(xamlModelGeometryModel3D(f1, xScale, yScale, zScale, useAmbient, useDiffuse, useSpecular, useEmission));
                            break;
                        case "AmbientLight":
                            // f1.display();
                            break;
                        case "DirectionalLight":
                            // f1.display();
                            break;
                        default:
#if DEBUG
                            Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " ignoring " + f1._Name);
#endif
                            break;
                    }
                    // }
                }
            }
        }
        public static CadObject xamlModelGeometryModel3D(
            TagFile f0,
            float xScale,
            float yScale,
            float zScale,
            bool useAmbient,
            bool useDiffuse,
            bool useSpecular,
            bool useEmission)
        {
            var co = new CadObject();

            var u1 = new HashSet<string>();

            foreach (var e1 in f0._Children)
            {
                if (e1 is TagFile)
                {
                    var f1 = e1 as TagFile;

                    if (u1.Contains(f1._Name)) Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " multiple " + f1._Name);
                    else
                    {
                        u1.Add(f1._Name);
                        switch (f1._Name)
                        {
                            case "GeometryModel3D.Material":
                                {
                                    xamlParseColor(f1, useAmbient, useDiffuse, useSpecular, useEmission, co);
                                    break;
                                }
                            case "GeometryModel3D.Geometry":
                                var u2 = new HashSet<string>();
                                foreach (var e2 in f1._Children)
                                {
                                    if (e2 is TagFile)
                                    {
                                        var f2 = e2 as TagFile;
                                        if (u2.Contains(f2._Name)) Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " multiple " + f2._Name);
                                        else
                                        {
                                            switch (f2._Name)
                                            {
                                                case "MeshGeometry3D":
                                                    {
                                                        var data = f2._Params;
                                                        var verts = new List<Vector3>();
                                                        var norms = new List<Vector3>();
                                                        xamlParseDict(ref data, ref verts, ref norms);
                                                        var verts2array = verts.ToArray();
                                                        var norms2array = norms.ToArray();
                                                        for (int j = 0; j < verts.Count; j++)
                                                        {
                                                            verts2array[j].X *= xScale;
                                                            verts2array[j].Y *= yScale;
                                                            verts2array[j].Z *= zScale;
                                                        }
                                                        co.initializeWithVectorsAndNormals(verts2array, norms2array);
                                                        break;
                                                    }
                                                default:
                                                    Console.WriteLine("CadObjectGenerator: GeometryModel3D.Geometry ignoring " + f2._Name);
                                                    break;
                                            }
                                        }
                                    }
                                }
                                break;
                            default:
#if DEBUG
                                Console.WriteLine("CadObjectGenerator: GeometryModel3D ignoring " + f1._Name);
#endif
                                break;
                        }
                    }
                }
            }

            return co;
        }
        private static CadObject xamlModelVisual3D(
            TagFile f0,
            float xScale,
            float yScale,
            float zScale,
            bool useAmbient,
            bool useDiffuse,
            bool useSpecular,
            bool useEmission
            )
        {
            CadObject co = new CadObject();
            var ret = new List<CadObject>();

            foreach (var e1 in f0._Children)
            {
                if (e1 is TagFile)
                {
                    var f1 = e1 as TagFile;
                    switch (f1._Name)
                    {
                        case "ModelVisual3D.Transform":
                            {
                                xamlParseTransform(f1, xScale, yScale, zScale, co);
                                break;
                            }
                        case "ModelVisual3D.Content":
                            {
                                xamlModelVisual3DContent(f1, xScale, yScale, zScale, useAmbient, useDiffuse, useSpecular, useEmission, ret);
                                break;
                            }
                        case "ModelVisual3D.Children":
                            {
                                foreach (var e2 in f1._Children)
                                {
                                    if (e2 is TagFile)
                                    {
                                        var f2 = e2 as TagFile;
                                        switch (f2._Name)
                                        {
                                            case "ModelVisual3D":
                                                ret.Add(xamlModelVisual3D(f2, xScale, yScale, zScale, useAmbient, useDiffuse, useSpecular, useEmission));
                                                break;
                                            
                                            default:
                                                Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " ignoring " + f2._Name);
                                                break;
                                        }
                                    }
                                }
                                break;
                            }
                        default:
                            {
#if DEBUG
                                Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " ignoring " + f1._Name);
#endif
                                break;
                            }
                    }
                }
            }

            for (int i = 0; i < ret.Count; i++)
            {
                if (ret[i].Children != null)
                    if (ret[i].Children.Length != 0)
                        continue;

                if (ret[i].Vertices != null)
                    if (ret[i].Vertices.Length != 0)
                        continue;

                ret.RemoveAt(i);
                i--;
            }

            co.Children = ret.ToArray();

            return co;
        }
        public static void xamlModel3DGroup(
            TagFile f0,
            float xScale,
            float yScale,
            float zScale,
            bool useAmbient,
            bool useDiffuse,
            bool useSpecular,
            bool useEmission,
            List<CadObject> ls)
        {
            var u1 = new HashSet<string>();
            foreach (var e1 in f0._Children)
            {
                if (e1 is TagFile)
                {
                    var f1 = e1 as TagFile;
                    if (u1.Contains(f1._Name)) Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " multiple " + f1._Name);
                    else
                    {
                        u1.Add(f1._Name);
                        switch (f1._Name)
                        {
                            case "Model3DGroup.Children":
                                xamlModelVisual3DGroupChildren(f1, xScale, yScale, zScale, useAmbient, useDiffuse, useSpecular, useEmission, ls);
                                break;
                            default:
#if DEBUG
                                Console.WriteLine("CadObjectGenerator." + System.Reflection.MethodBase.GetCurrentMethod().Name + " ignoring " + f1._Name);
#endif
                                break;
                        }
                    }
                }
            }
        }
Beispiel #11
0
 private void SaveTitle(TagFile tf, System.IO.StreamWriter sw, int indent)
 {
     IndentWrite(sw, indent, "<text>");
     IndentWrite(sw, indent + 1, this.textBoxTitle.Text);
     IndentWrite(sw, indent, "</text>");
 }
Beispiel #12
0
        private void SaveNavMap(TagFile tf, System.IO.StreamWriter sw, int indent)
        {
            int tab_index = 0;
            int max_index = 0;

            foreach (var mfn in this._ItemTextHandler.EnumerateContent())
            {
                if (mfn.Checked && (mfn._NavigationType != ManifestFileNavigation.NavigationType.None))
                {
                    max_index  = Math.Max(max_index, tab_index + 1);
                    tab_index += 1 - mfn._NavigationPointCloses;
                }
            }

            var detail_names = new string[max_index];
            var indices      = new int[max_index]; // Starts 0

            switch (max_index)
            {
            case 1:
                detail_names[0] = "Chapter ";
                break;

            case 2:
                detail_names[0] = "Part ";
                detail_names[1] = "Chapter ";
                break;

            default:
                throw new Exception("OH NO!");
            }

            tab_index = 0;
            int index = 0;

            foreach (var mfn in this._ItemTextHandler.EnumerateContent())
            {
                if (mfn.Checked && (mfn._NavigationType != ManifestFileNavigation.NavigationType.None))
                {
                    String name = null;

                    switch (mfn._NavigationType)
                    {
                    case ManifestFileNavigation.NavigationType.Custom:
                        name = mfn._NavigationName;
                        break;

                    case ManifestFileNavigation.NavigationType.Default:
                        indices[tab_index]++;
                        name = detail_names[tab_index] + indices[tab_index];
                        break;

                    default:
                        throw new Exception("OH NO!");
                    }

                    index++;
                    tab_index += 1;

                    IndentWrite(sw, indent + tab_index, "<navPoint class=\"other\" id=\"navpoint-" + index + "\" playOrder=\"" + index + "\">");
                    IndentWrite(sw, indent + tab_index + 1, "<navLabel>");
                    IndentWrite(sw, indent + tab_index + 2, "<text>" + name + "</text>");
                    IndentWrite(sw, indent + tab_index + 1, "</navLabel>");
                    IndentWrite(sw, indent + tab_index + 1, "<content src=\"" + mfn._StringPath + "\"/>");

                    for (int i = 0; i < mfn._NavigationPointCloses; i++)
                    {
                        IndentWrite(sw, indent + tab_index, "</navPoint>");
                        tab_index -= 1;
                    }
                }
            }

            for (; tab_index > 0; tab_index--)
            {
                IndentWrite(sw, indent + tab_index, "</navPoint>");
            }

            /*
             * int play_order = 1;
             * foreach (var ti in tf._Children)
             * {
             *  if (ti is TagFile)
             *  {
             *      var np = ti as TagFile;
             *      if ("navPoint".Equals(np._Name))
             *          this.saveNavPoint(np, ref play_order, sw, indent);
             *
             *      else Console.WriteLine("Error: navMap has something besides navPoint");
             *  }
             *  else Console.WriteLine("Error: navMap has TagText");
             * }
             */
        }
Beispiel #13
0
        private void bGo_Click(object sender, EventArgs e)
        {
            Console.WriteLine("");
            Console.WriteLine("******************************");
            Console.WriteLine("*********** Saving ***********");
            Console.WriteLine("******************************");
            Console.WriteLine("");

            this.textBoxAuthor.Text = this.textBoxAuthor.Text.Trim();
            this.textBoxGenre.Text  = this.textBoxGenre.Text.Trim();
            this.textBoxTitle.Text  = this.textBoxTitle.Text.Trim();

            String base_path = Directory.GetParent(Directory.GetParent(this.textBoxPath.Text).FullName).FullName;

            String epub_title = this.textBoxGenre.Text + " " + this.textBoxTitle.Text + ".epub";

            epub_title = epub_title.ToLower();
            epub_title = epub_title.Replace(" ", "_");

            String new_home = Path.Combine(base_path, "new_books", epub_title);

            if (Directory.Exists(new_home))
            {
                Directory.Delete(new_home, true);
            }

            System.Threading.Thread.Sleep(100);
            Directory.CreateDirectory(new_home);
            System.Threading.Thread.Sleep(50);

            foreach (var mf in this._ListItems)
            {
                if (mf.Checked)
                {
                    string new_path = Path.Combine(new_home, mf._StringPath);
                    if (File.Exists(new_path))
                    {
                        Console.WriteLine("Error: Trying to Copy A File Twice: " + mf._StringPath);
                    }
                    else
                    {
                        String dir = Directory.GetParent(new_path).FullName;
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                            System.Threading.Thread.Sleep(50);
                        }
                        File.Copy(mf._StringPathFull, new_path);
                    }
                }
            }

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Path.Combine(new_home, "content.opf")))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8'?>");
                this.SaveContentToXML(this._TagFileBase, sw, 0);
            }

            var tf          = TagFile.ParseText(File.ReadAllText(Path.Combine(this.textBoxPath.Text, "toc.ncx")));
            var ncx_matches = tf.getMatchesAtAnyDepth("ncx").ToList();

            if (ncx_matches.Count != 1)
            {
                MessageBox.Show("Invalid XML 1: toc.ncx");
                return;
            }
            if (!(ncx_matches[0] is TagFile))
            {
                MessageBox.Show("Invalid XML 2: toc.ncx");
                return;
            }

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Path.Combine(new_home, "toc.ncx")))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8'?>");
                this.SaveContentToXML(ncx_matches[0] as TagFile, sw, 0);
            }

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Path.Combine(new_home, "mimetype")))
            {
                sw.Write("application/epub+zip");
            }

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Path.Combine(new_home, "iTunesMetadata.plist")))
            {
                sw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>artistName</key>\n\t<string>"
                         + this.textBoxAuthor.Text + "</string>\n\t<key>itemName</key>\n\t<string>"
                         + this.textBoxTitle.Text + "</string>\n\t<key>genre</key>\n\t<string>"
                         + this.textBoxGenre.Text + "</string>\n</dict>\n</plist>\n");
            }

            String meta_inf = Path.Combine(new_home, "META-INF");

            if (!Directory.Exists(meta_inf))
            {
                Directory.CreateDirectory(meta_inf);
                System.Threading.Thread.Sleep(50);
            }

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Path.Combine(meta_inf, "container.xml")))
            {
                sw.WriteLine("<?xml version=\"1.0\"?>");
                sw.WriteLine("<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">");
                sw.WriteLine("\t<rootfiles>");
                sw.WriteLine("\t\t<rootfile full-path=\"content.opf\" media-type=\"application/oebps-package+xml\"/>");
                sw.WriteLine("\t</rootfiles>");
                sw.WriteLine("</container>");
            }


            Console.WriteLine("");
            Console.WriteLine("Finished Save");
        }
Beispiel #14
0
        private void bLook_Click(object sender, EventArgs e)
        {
            if (this._DoingStuff)
            {
                return;
            }
            try
            {
                this._DoingStuff         = true;
                this.bOpen.Enabled       = false;
                this.bLook.Enabled       = false;
                this.bClear.Enabled      = true;
                this.bGo.Enabled         = true;
                this.textBoxPath.Enabled = false;

                var book_path = this.textBoxPath.Text;

                var content_text  = new List <ManifestFileNavigation>();
                var content_other = new List <ManifestFile>();
                var content_image = new List <ManifestFile>();

                {
                    var tf = TagFile.ParseText(File.ReadAllText(Path.Combine(book_path, "content.opf")));
                    var package_matches = tf.getMatchesAtAnyDepth("package").ToList();
                    if (package_matches.Count != 1)
                    {
                        throw new Exception("Invalid XML 1");
                    }
                    if (!(package_matches[0] is TagFile))
                    {
                        throw new Exception("Invalid XML2");
                    }
                    this._TagFileBase = package_matches[0] as TagFile;

                    TagFile manifest = null;
                    TagFile spine    = null;
                    foreach (var node1 in this._TagFileBase._Children)
                    {
                        if (node1 is TagFile)
                        {
                            var node2 = node1 as TagFile;
                            switch (node2._Name)
                            {
                            case "manifest":
                                if (manifest == null)
                                {
                                    manifest = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Manifests");
                                }
                                break;

                            case "spine":
                                if (spine == null)
                                {
                                    spine = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Spines");
                                }
                                break;

                            case "metadata":
                                if (this._TagFileMetadata == null)
                                {
                                    this._TagFileMetadata = node2;
                                }
                                else
                                {
                                    Console.WriteLine("Error: Multiple Metadatas");
                                }
                                break;
                            }
                        }
                    }

                    foreach (var node3 in manifest._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4   = node3 as TagFile;
                            var params_ = node4._Params;


                            var mt = new ManifestFile(node4, book_path);

                            bool include = true;

                            include &= !("toc.ncx".Equals(mt._StringPath));

                            if (include)
                            {
                                switch (mt._MediaType)
                                {
                                case ManifestFile.MediaType.Image:
                                    content_image.Add(mt);
                                    break;

                                case ManifestFile.MediaType.Text:
                                    mt = new ManifestFileNavigation(node4, book_path);
                                    content_text.Add(mt as ManifestFileNavigation);
                                    break;

                                case ManifestFile.MediaType.Other:
                                    content_other.Add(mt);
                                    break;
                                }

                                this._DictItemsByXMLID[mt._StringID]  = mt;
                                this._DictItemsByPath[mt._StringPath] = mt;
                                this._DictItemsByNode[node4]          = mt;
                                this._ListItems.Add(mt);
                            }
                        }
                    }

                    int spine_count = 0;
                    foreach (var node3 in spine._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4   = node3 as TagFile;
                            var params_ = node4._Params;

                            ManifestFile relative = null;
                            String       idref    = params_["idref"];
                            if (this._DictItemsByXMLID.TryGetValue(idref, out relative))
                            {
                                this._DictItemsByNode[node4] = relative;
                                relative._IntSpineCount      = spine_count++;
                            }
                            else
                            {
                                Console.WriteLine("Error, no corresponding manifest item for spine item: " + idref);
                            }
                        }
                    }

                    bool found_author = false;
                    bool found_title  = false;
                    bool found_genre  = false;
                    foreach (var node3 in this._TagFileMetadata._Children)
                    {
                        if (node3 is TagFile)
                        {
                            var node4 = node3 as TagFile;
                            switch (node4._Name)
                            {
                            case "dc:creator":
                            {
                                if (found_author)
                                {
                                    Console.Write("Error: Second Author Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_author            = true;
                                        this.textBoxAuthor.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Author Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Author Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;

                            case "dc:title":
                            {
                                if (found_title)
                                {
                                    Console.Write("Error: Second Title Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_title            = true;
                                        this.textBoxTitle.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Title Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Title Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;

                            case "dc:genre":
                            {
                                if (found_genre)
                                {
                                    Console.Write("Error: Second Genre Found: ");
                                    node4.Display();
                                }
                                else if (node4._Children.Length == 1)
                                {
                                    if (node4._Children.First() is TagText)
                                    {
                                        found_genre            = true;
                                        this.textBoxGenre.Text = (node4._Children.First() as TagText)._Text;
                                    }
                                    else
                                    {
                                        Console.Write("Error: Genre Wrong Type of Children: ");
                                        node4.Display();
                                    }
                                }
                                else
                                {
                                    Console.Write("Error: Genre Wrong Number of Children: ");
                                    node4.Display();
                                }
                            }
                            break;
                            }
                        }
                    }

                    var missing_files = new HashSet <String>();

                    foreach (ManifestFile mf in content_text)
                    {
                        if (mf._BoolFileExists)
                        {
                            int    last_dex = mf._StringPath.LastIndexOf("/");
                            String dir      = last_dex > 0 ? mf._StringPath.Substring(0, last_dex + 1) : "";

                            TagFile tf2 = TagFile.ParseText(File.ReadAllText(mf._StringPathFull));

                            foreach (var ls in tf2.getMatchesAtAnyDepth("link"))
                            {
                                this.haveEmbeddedItem(ls._Params["href"], dir, mf, missing_files);
                            }
                            foreach (var ls in tf2.getMatchesAtAnyDepth("img"))
                            {
                                this.haveEmbeddedItem(ls._Params["src"], dir, mf, missing_files);
                            }
                            foreach (var ls in tf2.getMatchesAtAnyDepth("image"))
                            {
                                this.haveEmbeddedItem(ls._Params["xlink:href"], dir, mf, missing_files);
                            }

                            foreach (var ls in tf2.getMatchesAtAnyDepth("style"))
                            {
                                foreach (var ls2 in ls._Children)
                                {
                                    if (ls2 is TagText)
                                    {
                                        String text = (ls2 as TagText)._Text;

                                        int dex  = -1;
                                        int dlex = 1;

                                        int count = -1;
                                        while (dlex > 0)
                                        {
                                            count++;
                                            dex  = dlex;
                                            dlex = text.IndexOf("@font-face", dex + 1);

                                            if (dlex > 0)
                                            {
                                                int first = text.IndexOf("{", dlex);
                                                int last  = text.IndexOf("}", dlex);
                                                var path  = FontFaceSerializer.getFontPath(
                                                    text.Substring(first + 1, last - first - 1));
                                                if (path != null)
                                                {
                                                    this.haveEmbeddedItem(path, dir, mf, missing_files);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    foreach (var comb in missing_files)
                    {
                        Console.WriteLine("Error: Missing File Reference: " + comb);
                    }

                    // Text with references get moved to other
                    for (int i = 0; i < content_text.Count; i++)
                    {
                        ManifestFile mf = content_text[i];
                        if (mf._IntRefrencesMax != 0)
                        {
                            content_text.RemoveAt(i);
                            content_other.Add(mf);
                            i--;
                        }
                    }

                    content_text.Sort();
                }


                { // Get table of contents data
                    var tf       = TagFile.ParseText(File.ReadAllText(Path.Combine(book_path, "toc.ncx")));
                    var nav_maps = tf.getMatchesAtAnyDepth("ncx", "navMap").ToList();
                    if (nav_maps.Count != 1)
                    {
                        MessageBox.Show("Invalid XML 1: toc.ncx");
                        return;
                    }
                    if (!(nav_maps[0] is TagFile))
                    {
                        MessageBox.Show("Invalid XML 2: toc.ncx");
                        return;
                    }

                    var name_path_indent_s = new List <Tuple <string, string, int> >();
                    Action <TagItem, int> recursive_search = null;
                    recursive_search = (TagItem node, int level) =>
                    {
                        if (node is TagFile)
                        {
                            foreach (var child in (node as TagFile)._Children)
                            {
                                recursive_search(child, level + 1);
                            }
                        }
                        else if (node is TagText)
                        {
                            if (node.MatchesHeirarchicalNaming(null, "text", "navLabel", "navPoint"))
                            {
                                var content_array = node.GetParent(3)?.getMatchesAtZeroDepth("content")?.ToArray();

                                if (content_array == null)
                                {
                                    return;
                                }
                                if (content_array.Length != 1)
                                {
                                    return;
                                }
                                if (!(content_array[0] is TagFile))
                                {
                                    return;
                                }

                                var content = content_array[0] as TagFile;

                                String path;
                                if (content._Params.TryGetValue("src", out path))
                                {
                                    // - 3 for the 3 parents to get to root!
                                    int    tab_index = level - 3;
                                    String chap_name = (node as TagText)._Text;
                                    name_path_indent_s.Add(new Tuple <string, string, int>(chap_name, path, tab_index));
                                    for (int i = 0; i < tab_index; i++)
                                    {
                                        Console.Write(" ");
                                    }
                                    Console.WriteLine(chap_name + " " + path);
                                }
                            }
                        }
                    };

                    recursive_search(nav_maps[0] as TagFile, 0);

                    if (name_path_indent_s.Count > 0)
                    {
                        var key_path_value_name_closes = new Dictionary <string, Tuple <string, int> >();

                        int min_index = name_path_indent_s.Select(s => s.Item3).Min();

                        for (int i = 0; i < name_path_indent_s.Count; i++)
                        {
                            var    tup       = name_path_indent_s[i];
                            String name      = tup.Item1;
                            String path      = tup.Item2;
                            int    tab_index = tup.Item3 - min_index;

                            int next_tab_index = (i == name_path_indent_s.Count - 1)
                                ? 0 : (name_path_indent_s[i + 1].Item3 - min_index);

                            key_path_value_name_closes[path] = new Tuple <string, int>(
                                name, 1 + tab_index - next_tab_index);
                        }

                        foreach (var mfn in content_text)
                        {
                            Tuple <string, int> tup;
                            if (key_path_value_name_closes.TryGetValue(mfn._StringPath, out tup))
                            {
                                mfn._NavigationName        = tup.Item1;
                                mfn._NavigationType        = ManifestFileNavigation.NavigationType.Custom;
                                mfn._NavigationPointCloses = tup.Item2;

                                mfn.UpdateAutoInclude(tup.Item1); // "Chapter" "Part" "Epilogue"
                            }
                        }
                    }
                }



                this._ItemImageHandler._Content = content_image.ToArray();
                this._ItemTextHandler.SetContent(content_text);
                this._ItemOtherHandler._Content = content_other.ToArray();

                this.uiTableView1.ReloadData();
                this.uiTableView2.ReloadData();
                this.uiTableView3.ReloadData();
            }
            finally
            {
                this._DoingStuff = false;
            }
        }
Beispiel #15
0
        public void saveNavPoint(TagFile np, ref int play_order, StreamWriter sw, int indent)
        {
            if (np._Params.ContainsKey("playOrder"))
            {
                np._Params["playOrder"] = play_order.ToString();
            }

            bool save = false;

            foreach (var ti_c in np._Children)
            {
                if (ti_c is TagFile)
                {
                    var tf_c = ti_c as TagFile;
                    if ("content".Equals(tf_c._Name))
                    {
                        String src = null;
                        if (tf_c._Params.TryGetValue("src", out src))
                        {
                            ManifestFile mf = null;
                            if (this._DictItemsByPath.TryGetValue(src, out mf))
                            {
                                if (mf.Checked)
                                {
                                    save = true;
                                    play_order++;
                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Error: navPoint missing ManifestFile: " + src);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error: navPoint no source on content");
                        }
                    }
                }
            }


            if (save)
            {
                String f, b;
                np.getStringXML(out f, out b);
                for (int i = 0; i < indent; i++)
                {
                    sw.Write('\t');
                }
                sw.WriteLine(f);

                foreach (var ti_c in np._Children)
                {
                    if (ti_c is TagFile)
                    {
                        var tf_c = ti_c as TagFile;
                        if ("navPoint".Equals(tf_c._Name))
                        {
                            this.saveNavPoint(tf_c, ref play_order, sw, indent + 1);
                        }
                        else
                        {
                            this.SaveContentToXML(ti_c, sw, indent + 1);
                        }
                    }
                    else
                    {
                        this.SaveContentToXML(ti_c, sw, indent + 1);
                    }
                }
                for (int i = 0; i < indent; i++)
                {
                    sw.Write('\t');
                }
                sw.WriteLine(b);
            }
        }