Exemplo n.º 1
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.TableRowTest = ((System.Windows.Documents.TableRow)(target));
                return;

            case 2:
                this.MercuryZelle = ((System.Windows.Documents.TableCell)(target));
                return;
            }
            this._contentLoaded = true;
        }
Exemplo n.º 2
0
        public Action AddToFlowDocument(IResumeDataObject rdo, IResumeFormatObject rfo, Win.FlowDocument flowDoc)
        {
            return(() =>
            {
                var table = new Win.Table();
                table.RowGroups.Add(new Win.TableRowGroup());
                var rg = table.RowGroups[0];
                rg.Rows.Add(new Win.TableRow());
                rg.Rows.Add(new Win.TableRow());

                Win.TableCell add1Cell = new Win.TableCell(new Win.Paragraph(new Win.Run(rdo.AddressLine1)));
                Win.TableCell add2Cell = new Win.TableCell(new Win.Paragraph(new Win.Run(rdo.AddressLine2)));
                Win.TableCell phoneCell = new Win.TableCell(new Win.Paragraph(new Win.Run(rdo.Email)));
                Win.TableCell emailCell = new Win.TableCell(new Win.Paragraph(new Win.Run(rdo.PhoneNumber)));

                rg.Rows[0].Cells.Add(add1Cell);
                rg.Rows[1].Cells.Add(add2Cell);
                rg.Rows[0].Cells.Add(emailCell);
                rg.Rows[1].Cells.Add(phoneCell);

                add1Cell.TextAlignment = TextAlignment.Left;
                add2Cell.TextAlignment = TextAlignment.Left;
                phoneCell.TextAlignment = TextAlignment.Right;
                emailCell.TextAlignment = TextAlignment.Right;

                foreach (var row in rg.Rows)
                {
                    foreach (var cell in row.Cells)
                    {
                        cell.FontFamily = new FontFamily(rfo.BodyFontName);
                        cell.FontSize = rfo.BodyFontSizeWindows;
                    }
                }

                flowDoc.Blocks.Add(table);
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Renders DOM text elements recursively, i.e. including their childs.
        /// </summary>
        /// <param name="e">The root DOM text element.</param>
        /// <returns>The corresponding Wpf element.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// </exception>
        /// <exception cref="System.NotImplementedException"></exception>
        public object RenderRecursively(TextElement e)
        {
            object wpf = null;

            switch (e)
            {
            case BlockUIContainer buc:
            {
                wpf = new swd.BlockUIContainer();
            }
            break;

            case FlowDocument flowDocument:
            {
                // make sure the standard colors were set
                flowDocument.Foreground = ExCSS.Color.Black;
                flowDocument.Background = ExCSS.Color.White;

                var flowDocumente = new swd.FlowDocument()
                {
                    Name = NameOfFlowDocument
                };
                if (TemplateBindingViewportWidth is null)
                {
                    TemplateBindingViewportWidth = new Binding("ColumnWidth")
                    {
                        Source = flowDocumente
                    }
                }
                ;
                if (TemplateBindingViewportHeight is null)
                {
                    TemplateBindingViewportHeight = new Binding("ColumnWidth")
                    {
                        Source = flowDocumente
                    }
                }
                ;                                                                                    // Binding to ColumnWidth is not optimal, but better than nothing!

                if (flowDocument.Background.HasValue)
                {
                    flowDocumente.Background = GetBrushFromColor(flowDocument.Background.Value);
                }
                if (flowDocument.Foreground.HasValue)
                {
                    flowDocumente.Foreground = GetBrushFromColor(flowDocument.Foreground.Value);
                }

                wpf = flowDocumente;
            }
            break;

            case Hyperlink hl:
            {
                var hle = new swd.Hyperlink();
                if (!string.IsNullOrEmpty(hl.NavigateUri))
                {
                    if (System.Uri.TryCreate(hl.NavigateUri, UriKind.RelativeOrAbsolute, out var uri))
                    {
                        hle.NavigateUri = uri;
                    }
                }
                if (!string.IsNullOrEmpty(hl.TargetName))
                {
                    hle.TargetName = hl.TargetName;
                }
                wpf = hle;
            }
            break;

            case Image image:
            {
                var imagee = new System.Windows.Controls.Image();
                if (!string.IsNullOrEmpty(image.Source))
                {
                    imagee.SetBinding(System.Windows.Controls.Image.SourceProperty, $"ImageProvider[{image.Source}]");
                }

                if (image.Width == null && image.Height == null)
                {
                    imagee.Stretch = System.Windows.Media.Stretch.Uniform;

                    var binding = new Binding()
                    {
                        RelativeSource = RelativeSource.Self, Path = new System.Windows.PropertyPath("Source")
                    };
                    binding.Converter = ImageToImageWidthConverter.Instance;
                    imagee.SetBinding(System.Windows.Controls.Image.WidthProperty, binding);
                }
                else
                {
                    imagee.Stretch = System.Windows.Media.Stretch.Uniform;
                }

                if (image.Width != null)
                {
                    if (image.Width.IsPurelyAbsolute(out var widthPx))
                    {
                        imagee.Width = widthPx;
                    }
                    else
                    {
                        var multibinding = new MultiBinding();
                        multibinding.Bindings.Add(new Binding()
                            {
                                Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path
                            });
                        multibinding.Bindings.Add(new Binding()
                            {
                                Source = TemplateBindingViewportHeight.Source, Path = TemplateBindingViewportHeight.Path
                            });
                        multibinding.Converter          = CompoundLengthConverter.Instance;
                        multibinding.ConverterParameter = GetCompoundLengthConverterParameters(image.Width);
                        imagee.SetBinding(System.Windows.Controls.Image.WidthProperty, multibinding);
                    }
                }

                if (image.Height != null)
                {
                    if (image.Height.IsPurelyAbsolute(out var heightPx))
                    {
                        imagee.Height = heightPx;
                    }
                    else
                    {
                        var multibinding = new MultiBinding();
                        multibinding.Bindings.Add(new Binding()
                            {
                                Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path
                            });
                        multibinding.Bindings.Add(new Binding()
                            {
                                Source = TemplateBindingViewportHeight.Source, Path = TemplateBindingViewportHeight.Path
                            });
                        multibinding.Converter          = CompoundLengthConverter.Instance;
                        multibinding.ConverterParameter = GetCompoundLengthConverterParameters(image.Height);
                        imagee.SetBinding(System.Windows.Controls.Image.HeightProperty, multibinding);
                    }
                }

                // set max-width and max-height
                if (image.MaxWidth != null && image.MaxWidth.Value.IsAbsolute)
                {
                    imagee.MaxWidth = image.MaxWidth.Value.ToPixel();
                }
                else if (image.MaxWidth == null || image.MaxWidth.Value.Type == ExCSS.Length.Unit.Vw)
                {
                    double vwValue = image.MaxWidth.HasValue ? image.MaxWidth.Value.Value : 100;

                    var binding = new Binding()
                    {
                        Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path
                    };
                    binding.Converter          = RelativeSizeConverter.Instance;
                    binding.ConverterParameter = vwValue;
                    imagee.SetBinding(System.Windows.Controls.Image.MaxWidthProperty, binding);
                }
                else
                {
                    throw new InvalidProgramException();
                }


                if (image.MaxHeight != null && image.MaxHeight.Value.IsAbsolute)
                {
                    imagee.MaxHeight = image.MaxHeight.Value.ToPixel();
                }
                else if (image.MaxHeight == null || image.MaxHeight.Value.Type == ExCSS.Length.Unit.Vh)
                {
                    double vhValue = image.MaxHeight.HasValue ? image.MaxHeight.Value.Value : 100;
                    var    binding = new Binding()
                    {
                        Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path
                    };
                    binding.Converter          = RelativeSizeConverter.Instance;
                    binding.ConverterParameter = vhValue;
                    imagee.SetBinding(System.Windows.Controls.Image.MaxHeightProperty, binding);
                }
                else
                {
                    throw new InvalidProgramException();
                }

                wpf = imagee;
            }
            break;

            case InlineUIContainer iuc:
            {
                var inlineuiContainere = new swd.InlineUIContainer();
                wpf = inlineuiContainere;
            }
            break;

            case LineBreak lb:
            {
                wpf = new swd.LineBreak();
            }
            break;

            case List list:
            {
                var liste = new swd.List();
                if (list.MarkerStyle.HasValue)
                {
                    liste.MarkerStyle = ToMarkerStyle(list.MarkerStyle.Value);
                }
                wpf = liste;
            }
            break;

            case ListItem li:
            {
                wpf = new swd.ListItem();
            }
            break;

            case Paragraph p:
            {
                var pe = new swd.Paragraph();
                if (p.TextDecorations.HasValue)
                {
                    pe.TextDecorations = ToTextDecorations(p.TextDecorations.Value);
                }
                if (p.TextIndent.HasValue)
                {
                    pe.TextIndent = p.TextIndent.Value.IsAbsolute ? p.TextIndent.Value.ToPixel() : 0;
                }
                wpf = pe;
            }
            break;

            case Run run:
            {
                if (SplitIntoWords)
                {
                    wpf = CreateTextElement_SeparateWords(run.Text);
                }
                else if (SplitIntoSentences)
                {
                    wpf = CreateTextElement_SeparateSentences(run.Text);
                }
                else
                {
                    wpf = new swd.Run(run.Text);
                }
            }
            break;

            case Section s:
            {
                wpf = new swd.Section();
            }
            break;

            case Span span:
            {
                wpf = new swd.Span();
            }
            break;

            case Table tb:
            {
                var tbe = new swd.Table();
                foreach (var c in tb.Columns)
                {
                    if (c.Width.HasValue)
                    {
                        tbe.Columns.Add(new swd.TableColumn()
                            {
                                Width = new System.Windows.GridLength(c.Width.Value)
                            });
                    }
                    else
                    {
                        tbe.Columns.Add(new swd.TableColumn());
                    }
                }
                wpf = tbe;
            }
            break;

            case TableCell tc:
            {
                var tce = new swd.TableCell();
                if (1 != tc.ColumnSpan)
                {
                    tce.ColumnSpan = tc.ColumnSpan;
                }

                if (1 != tc.RowSpan)
                {
                    tce.RowSpan = tc.RowSpan;
                }
                if (tc.BorderBrush.HasValue)
                {
                    tce.BorderBrush = new System.Windows.Media.SolidColorBrush(ToColor(tc.BorderBrush.Value));
                }
                if (tc.BorderThickness.HasValue)
                {
                    tce.BorderThickness = ToThickness(tc.BorderThickness.Value);
                }
                wpf = tce;
            }
            break;

            case TableRow trow:
            {
                wpf = new swd.TableRow();
            }
            break;

            case TableRowGroup trg:
            {
                wpf = new swd.TableRowGroup();
            }
            break;

            default:
            {
                wpf = null;
            }
            break;
            }

            // Render TextElement properties

            if (wpf is swd.TextElement te)
            {
                if (!string.IsNullOrEmpty(e.FontFamily))
                {
                    te.FontFamily = GetFontFamily(e.FontFamily);
                }

                if (e.FontSize.HasValue)
                {
                    var fs = e.FontSize.Value;
                    fs          = Math.Max(0.004, fs);
                    te.FontSize = fs;
                }

                if (e.FontStyle.HasValue)
                {
                    te.FontStyle = ToFontStyle(e.FontStyle.Value);
                }

                if (e.FontWeight.HasValue)
                {
                    te.FontWeight = ToFontWeight(e.FontWeight.Value);
                }

                if (e.Foreground.HasValue && e.Foreground != e.ForegroundInheritedOnly)
                {
                    te.Foreground = GetBrushFromColor(e.Foreground.Value);
                }

                if (e.Background.HasValue && e.Background != e.BackgroundInheritedOnly)
                {
                    te.Background = GetBrushFromColor(e.Background.Value);
                }
            }

            // now special properties

            if (e is Block b && wpf is swd.Block be)
            {
                if (b.Margin.HasValue)
                {
                    be.Margin = ToThickness(b.Margin.Value);
                }

                if (b.Padding.HasValue)
                {
                    be.Padding = ToThickness(b.Padding.Value);
                }

                if (b.BorderBrush.HasValue)
                {
                    be.BorderBrush = new System.Windows.Media.SolidColorBrush(ToColor(b.BorderBrush.Value));
                }

                if (b.BorderThickness.HasValue)
                {
                    be.BorderThickness = ToThickness(b.BorderThickness.Value);
                }

                if (b.TextAlignment.HasValue)
                {
                    be.TextAlignment = ToTextAlignment(b.TextAlignment.Value);
                }

                if (b.LineHeight.HasValue)
                {
                    be.LineHeight = b.LineHeight.Value;
                }
            }
            if (e is Inline i && wpf is swd.Inline ie)
            {
                if (i.VerticalAlignment.HasValue)
                {
                    ie.BaselineAlignment = ToBaselineAlignment(i.VerticalAlignment.Value);
                }
            }
            //  finished rendering the attributes


            // now, render all children
            foreach (var child in e.Childs)
            {
                var childe = RenderRecursively(child);

                switch (wpf)
                {
                case swd.Figure figure:
                    figure.Blocks.Add((swd.Block)childe);
                    break;

                case swd.Floater floater:
                    floater.Blocks.Add((swd.Block)childe);
                    break;

                case swd.FlowDocument flowDocument:
                    flowDocument.Blocks.Add((swd.Block)childe);
                    break;

                case swd.List list:
                    list.ListItems.Add((swd.ListItem)childe);
                    break;

                case swd.ListItem listItem:
                    listItem.Blocks.Add((swd.Block)childe);
                    break;

                case swd.Section section:
                    section.Blocks.Add((swd.Block)childe);
                    break;

                case swd.Table table:
                    table.RowGroups.Add((swd.TableRowGroup)childe);
                    break;

                case swd.TableCell tableCell:
                    tableCell.Blocks.Add((swd.Block)childe);
                    break;

                case swd.TableRow tableRow:
                    tableRow.Cells.Add((swd.TableCell)childe);
                    break;

                case swd.TableRowGroup tableRowGroup:
                    tableRowGroup.Rows.Add((swd.TableRow)childe);
                    break;

                // now elements that can contain inlines
                case swd.Paragraph paragraph:
                    paragraph.Inlines.Add((swd.Inline)childe);
                    break;

                case swd.Span span:
                    span.Inlines.Add((swd.Inline)childe);
                    break;

                // now some specialties
                case swd.InlineUIContainer inlineUIContainer:
                    if (inlineUIContainer.Child != null)
                    {
                        throw new InvalidOperationException($"{nameof(swd.InlineUIContainer)} can not contain more than one child");
                    }
                    inlineUIContainer.Child = (System.Windows.UIElement)childe;
                    break;

                case swd.BlockUIContainer blockUIContainer:
                    if (blockUIContainer.Child != null)
                    {
                        throw new InvalidOperationException($"{nameof(swd.BlockUIContainer)} can not contain more than one child");
                    }
                    blockUIContainer.Child = (System.Windows.UIElement)childe;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            if (AttachDomAsTags)
            {
                if (wpf is System.Windows.FrameworkContentElement conEle)
                {
                    conEle.Tag = e;
                }
                else if (wpf is System.Windows.FrameworkElement uiEle)
                {
                    uiEle.Tag = e;
                }
            }

            return(wpf);
        }