예제 #1
0
        /// <summary>Renders the matrix.</summary>
        /// <param name="width">The width.</param>
        /// <param name="content">The content.</param>
        /// <param name="scrollbars">if set to <c>true</c> [scrollbars] will be rendered.</param>
        /// <returns>the rendered text in a 2D bool matrix</returns>
        public bool[,] RenderMatrix(int width, object content, bool scrollbars = false)
        {
            lock (_rendererLock)
            {
                clearRenderElements();

                if (content != null && content is String)
                {
                    String text = content.ToString();
                    if (BrailleInterpreter != null)
                    {
                        //reduce the with if scrollbars should been shown
                        width -= scrollbars | RenderingProperties.HasFlag(RenderingProperties.ADD_SPACE_FOR_SCROLLBARS) ? 3 : 0;

                        int maxUsedWidth = 0;

                        //check available width
                        if (width > 0)
                        {
                            List <List <List <int> > > lines = new List <List <List <int> > >();

                            // split text into paragraphs/lines
                            string[] paragraphs = GetLinesOfString(text);

                            // foreach (var p in paragraphs)
                            int l = paragraphs.Length - 1;
                            for (int i = 0; i < paragraphs.Length; i++)
                            {
                                var p      = paragraphs[i];
                                int offset = lines.Count * (BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT);
                                List <List <List <int> > > paragraphLines = renderParagraph(p, width, ref maxUsedWidth, offset, i == l);
                                lines.AddRange(paragraphLines);
                            }

                            //reduce matrix if requested to minimum width
                            if (this.RenderingProperties.HasFlag(RenderingProperties.RETURN_REAL_WIDTH))
                            {
                                width = maxUsedWidth + (scrollbars ? 3 : 0);
                            }

                            // build start matrix
                            bool[,] matrix = buildMatrixFromLines(lines, width);

                            lines = null;


                            return(matrix);
                        }
                    }
                }
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Renders a paragraph.
        /// </summary>
        /// <param name="text">The text to render.</param>
        /// <param name="width">The available width.</param>
        /// <param name="maxUsedWidth">Maximum width of the used.</param>
        /// <param name="yOffset">The y offset.</param>
        /// <param name="isLast">if set to <c>true</c> if this is the last paragraph to render.</param>
        /// <returns></returns>
        private List <List <List <int> > > renderParagraph(string text, int width, ref int maxUsedWidth, int yOffset = 0, bool isLast = false)
        {
            List <List <List <int> > > lines = new List <List <List <int> > >();

            if (width > 0)
            {
                string[] words = GetWordsOfString(text);

                List <List <int> > currentLine = new List <List <int> >();
                int availableWidth             = width;


                int peX, peY, peW, peH = 0;
                peX = peY = peW = peH;
                peY = yOffset + lines.Count * (BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT);
                peX = width - availableWidth;
                RenderElement pe = new RenderElement(peX, peY, peW, peH, text)
                {
                    Type = BrailleRendererPartType.PARAGRAPH
                };


                foreach (var word in words)
                {
                    #region Rendering Element

                    int eX, eY, eW, eH = 0;
                    eX = eY = eW = eH;
                    eY = yOffset + lines.Count * (BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT);
                    eX = width - availableWidth;
                    RenderElement e = new RenderElement(eX, eY, eW, eH, word)
                    {
                        Type = BrailleRendererPartType.WORD
                    };

                    #endregion

                    // get the dot patterns for that word
                    List <List <int> > dots = getBrailleFromString(word);

                    // check if the line has still some entries
                    // if so, add a space char or open a new line
                    if (currentLine.Count > 0)
                    {
                        // check if the line is full
                        if (getMaxWidthOfString(currentLine) + BRAILLE_CHAR_WIDTH >= width)
                        {
                            makeNewLine(ref lines, ref currentLine, ref availableWidth, width, ref maxUsedWidth);
                        }
                        else
                        {
                            // add a space
                            currentLine.Add(new List <int>());
                            availableWidth -= INTER_CHAR_WIDTH + BRAILLE_CHAR_WIDTH;
                        }
                    }

                    //check if it fits into the available space
                    if (!fitsWordInRestOfLine(dots, availableWidth)) //no
                    {
                        makeNewLine(ref lines, ref currentLine, ref availableWidth, width, ref maxUsedWidth);
                    }

                    e.X = width - availableWidth;
                    e.Y = yOffset + lines.Count * (BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT);

                    int minWidth = getMinWidthOfString(dots);
                    if (minWidth > width) // the word is larger than a whole line
                    {
                        //this will start a new line, so add a new line to the y position
                        e.Y += BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT;

                        //split the word into several lines
                        List <List <List <int> > > wordLines = splitWordOverLines(dots, width, ref e);

                        if (wordLines != null && wordLines.Count > 0)
                        {
                            //remove empty line if generated twice
                            if (lines.Count > 0 && lines[lines.Count - 1].Count == 0)
                            {
                                lines.Remove(lines[Math.Max(0, lines.Count - 1)]);
                            }

                            // add them to the lines
                            lines.AddRange(wordLines);
                            //set the current line
                            currentLine = lines[lines.Count - 1];

                            //remove from list so it will not added twice
                            lines.Remove(currentLine);

                            //update the available size
                            availableWidth = width - getMinWidthOfString(currentLine);
                        }
                    }
                    else
                    {
                        //fill the word into the line
                        currentLine.AddRange(dots);

                        //update the available width
                        availableWidth -= getMinWidthOfString(dots);
                        if (availableWidth > INTER_CHAR_WIDTH)
                        {
                            availableWidth -= +INTER_CHAR_WIDTH;
                        }

                        e.Width  = minWidth + (availableWidth > INTER_CHAR_WIDTH ? INTER_CHAR_WIDTH : 0);
                        e.Height = BRAILLE_CHAR_HEIGHT + INTER_LINE_HEIGHT;
                    }

                    #region Rendering Element

                    pe.AddSubPart(e);

                    #endregion
                }
                lines.Add(currentLine);

                // update the maxUsed Width
                maxUsedWidth = Math.Max(maxUsedWidth, (width - availableWidth));

                if (isLast && RenderingProperties.HasFlag(RenderingProperties.IGNORE_LAST_LINESPACE))
                {
                    pe.Height -= 1;
                    // TODO: find the last line of subparts and decrease them as well
                }

                elements.AddLast(pe);
            }

            return(lines);
        }