コード例 #1
0
ファイル: main.cs プロジェクト: s070746821/Wordcloud
        //look at last run and placeWord for more detailed explanation
        //this function attemps to place words on right side
        void placeWordRight(Word word)
        {

            Font wordFont;
            //List<Position> positions = new List<Position>();
            float modifyY;
            float modifyX;
            float fontSize;

            int hit;
            int wit;
            SizeF siz;

            
            nextStart = findRightNextPos();
            fontSize = word.fontSize * 10;
            wordFont = new Font(fontString, fontSize, fontstyle);
            siz = new SizeF(flagGraphics.MeasureString(word.actualWord, wordFont).Width - (fontSize * 0.45f) + 10, flagGraphics.MeasureString(word.actualWord, wordFont).Height * 0.6f + 10);

            while (fontSize <= 20 && siz.Height > 30)
            {
                fontSize -= 1;
                wordFont = new Font(fontString, fontSize, fontstyle);
                siz = new SizeF(flagGraphics.MeasureString(word.actualWord, wordFont).Width - (fontSize * 0.45f) + 10, flagGraphics.MeasureString(word.actualWord, wordFont).Height * 0.6f + 10);
            }

            hit = (int)((siz.Height + 10) / 10);
            wit = (int)((siz.Width + 10) / 10);

            //Console.WriteLine(nextStart + " " + wit);

            float tempX = nextStart.X;

            nextStart = new PointF(tempX - (wit * 10 - 10), nextStart.Y);

            //Console.WriteLine(nextStart);

            if (!checkFit(nextStart, hit, wit) && fontSize > 20)
            {

                //Console.WriteLine("make it smaller");
                fontSize -= 10;
                wordFont = new Font(fontString, fontSize, fontstyle);

                siz = new SizeF(flagGraphics.MeasureString(word.actualWord, wordFont).Width - (fontSize * 0.45f) + 10, flagGraphics.MeasureString(word.actualWord, wordFont).Height * 0.6f + 10);
                hit = (int)((siz.Height + 10) / 10);
                wit = (int)((siz.Width + 10) / 10);
                nextStart = new PointF(tempX - (wit * 10 - 10), nextStart.Y);
            }

            if (checkFit(nextStart, hit, wit))
            {
                tempWordCount++;
                word.place = new Position(flagGraphics.MeasureString(word.actualWord, wordFont), nextStart.X, nextStart.Y);
                modifyY = word.place.size.Height * 0.14f;
                modifyX = fontSize * 0.2f;
                word.place.size = new SizeF(word.place.size.Width - (fontSize * 0.45f), word.place.size.Height * 0.6f);
                //modifyY = words[i].place.size.Height * 0.14f;
                //modifyX = fontSize * 0.2f;
                //words[i].place.size = new SizeF(words[i].place.size.Width - (fontSize * 0.45f), words[i].place.size.Height * 0.6f);
                fillArray(word.place.size, word.place.pos);
                //Console.WriteLine(words[i].place.size + " at " + words[i].place.pos);
                PointF tempPoint = new PointF(word.place.pos.X - modifyX, word.place.pos.Y - modifyY);
                Brush tempColor = randColor();
                flagGraphics.DrawString(word.actualWord, wordFont, tempColor, tempPoint);

                if (wordSize)
                    wordsToResize.Add(new customBackground(word.actualWord, wordFont, tempColor, tempPoint));
                //temp
                //flagGraphics.DrawRectangle(new Pen(Color.Blue), new Rectangle((int)words[i].place.pos.X, (int)words[i].place.pos.Y, (int)words[i].place.size.Width, (int)words[i].place.size.Height));
                word.used = true;
                modifyX = 0;
                modifyY = 0;
            }
            else
            {
                word.place = new Position(new SizeF(0, 0), 0, 0);
                if (fontSize <= 20)
                {
                    lastRun(word);
                }
            }
        }
コード例 #2
0
ファイル: main.cs プロジェクト: s070746821/Wordcloud
        //this functions places the smallest words to fill remaining gaps
        //works by trying to place words from left
        void lastRun(Word word)
        {
            Font wordFont;
            //List<Position> positions = new List<Position>();
            float modifyY;
            float modifyX;
            float fontSize;

            int hit;
            int wit;
            SizeF siz;

            //loops through entire list of words


            nextStart = findLastRun();
            //fontSize = words[i].fontSize * 10;
            //Console.WriteLine(nextStart);

            //sets font to 10
            fontSize = 10;
            wordFont = new Font(fontString, fontSize, fontstyle);

            //this is the size of the actual word plus a small border
            siz = new SizeF(flagGraphics.MeasureString(word.actualWord, wordFont).Width - (fontSize * 0.45f) + 10, flagGraphics.MeasureString(word.actualWord, wordFont).Height * 0.6f + 10);

            //the idea behind this is that the program has basically ignored
            //all gaps of size 20 up to this point
            //now in order to fill these gaps, it must find a fontsize that
            //the word heights are less than 20 so they can fit inside
            //size 20
            while (siz.Height > 20)
            {
                fontSize -= 1;
                wordFont = new Font(fontString, fontSize, fontstyle);
                siz = new SizeF(flagGraphics.MeasureString(word.actualWord, wordFont).Width - (fontSize * 0.45f) + 10, flagGraphics.MeasureString(word.actualWord, wordFont).Height * 0.6f + 10);
            }
                
            //basically divides the height and width of the word
            //and rounds up no matter what
            //the assumption is basically if a word is 
            //9.9 or 9.1 pixels tall, both will fit into
            //a 10 pixel gap
            hit = (int)((siz.Height + 10) / 10);
            wit = (int)((siz.Width + 10) / 10);

            //Console.WriteLine(siz.Width + " " + siz.Height);

            //checks bitmap if word can fit
            if (checkFit(nextStart, hit, wit))
            {
                tempWordCount++;
                //if word fits, it places it

                //by default draw string has a bit gap surrounding the word
                //this makes the gap around the word smaller
                word.place = new Position(flagGraphics.MeasureString(word.actualWord, wordFont), nextStart.X, nextStart.Y);
                modifyY = word.place.size.Height * 0.14f;
                modifyX = fontSize * 0.2f;
                word.place.size = new SizeF(word.place.size.Width - (fontSize * 0.45f), word.place.size.Height * 0.6f);

                //modifyY = words[i].place.size.Height * 0.14f;
                //modifyX = fontSize * 0.2f;
                //words[i].place.size = new SizeF(words[i].place.size.Width - (fontSize * 0.45f), words[i].place.size.Height * 0.6f);

                //fills picSize array 
                fillArray(word.place.size, word.place.pos);

                //Console.WriteLine(words[i].place.size + " at " + words[i].place.pos);

                PointF tempPoint = new PointF(word.place.pos.X - modifyX, word.place.pos.Y - modifyY);
                Brush tempColor = randColor();
                flagGraphics.DrawString(word.actualWord, wordFont, tempColor, tempPoint);

                if (wordSize)
                    wordsToResize.Add(new customBackground(word.actualWord, wordFont, tempColor, tempPoint));

                //temp
                //flagGraphics.DrawRectangle(new Pen(Color.Blue), new Rectangle((int)words[i].place.pos.X, (int)words[i].place.pos.Y, (int)words[i].place.size.Width, (int)words[i].place.size.Height));
                word.used = true;
                modifyX = 0;
                modifyY = 0;
                //Console.WriteLine("lastfit");
            }
            else
            {
                //if word doesnt fit
                word.place = new Position(new SizeF(0, 0), 0, 0);
            }
            
        }