{"action":"created","issue":{"url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/318","repository_url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended","labels_url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/318/labels{/name}","comments_url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/318/comments","events_url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/318/events","html_url":"https://github.com/craftworkgames/MonoGame.Extended/issues/318","id":191973259,"number":318,"title":"[BitmapFont] Reconsider what we are going to do about word wrapping","user":{"login":"craftworkgames","id":3201643,"avatar_url":"https://avatars.githubusercontent.com/u/3201643?v=3","gravatar_id":"","url":"https://api.github.com/users/craftworkgames","html_url":"https://github.com/craftworkgames","followers_url":"https://api.github.com/users/craftworkgames/followers","following_url":"https://api.github.com/users/craftworkgames/following{/other_user}","gists_url":"https://api.github.com/users/craftworkgames/gists{/gist_id}","starred_url":"https://api.github.com/users/craftworkgames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/craftworkgames/subscriptions","organizations_url":"https://api.github.com/users/craftworkgames/orgs","repos_url":"https://api.github.com/users/craftworkgames/repos","events_url":"https://api.github.com/users/craftworkgames/events{/privacy}","received_events_url":"https://api.github.com/users/craftworkgames/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":16,"created_at":"2016-11-28T10:46:21Z","updated_at":"2016-11-29T01:52:33Z","closed_at":null,"body":"I've removed the word wrapping overloads from the `DrawString` methods in the [`BitmapFontExtensions`](https://github.com/craftworkgames/MonoGame.Extended/blob/develop/Source/MonoGame.Extended/BitmapFonts/BitmapFontExtensions.cs) class.\r\n\r\nThere's a few key reasons I decided to remove the word wrapping overloads:\r\n\r\n 1. Without word wrapping the `BitmapFont` class has a *very clear* scope. Parity with `SpriteFont`.\r\n \r\n 2. The wrapping code is quite complex and as a result caused a number of bugs (e.g. `MeasureString` wasn't taking into account wrapped lines).\r\n\r\n 3. Splitting strings into words and lines over and over again during rendering causes a lot of garbage collection. There are other ways to deal with this problem, but they make it even more clear it shouldn't be the responsibility of the `BitmapFont` class.\r\n\r\nFor reference (and for anyone still wanting to use [the old code](https://github.com/craftworkgames/MonoGame.Extended/blob/5922d13b9501ed086df078692905f8d98d0669c6/Source/MonoGame.Extended/BitmapFonts/BitmapFontExtensions.cs#L211)), here's a copy of the method before I started messing with it. Calls to `DrawInternal` should be replaceable by calls to `DrawString` with care.\r\n\r\n        private static void DrawWrapped(this SpriteBatch spriteBatch, BitmapFont font, string text, Vector2 position,\r\n            Color color, int wrapWidth, float layerDepth)\r\n        {\r\n            HandleNullParameters(font, text);\r\n\r\n            if (wrapWidth == int.MaxValue)\r\n            {\r\n                DrawInternal(spriteBatch, font, text, position, color, layerDepth);\r\n                return;\r\n            }\r\n\r\n            // parse the text and wrap it at the specified width\r\n            var dx = position.X;\r\n            var dy = position.Y;\r\n            var sentences = text.Split(new[] {'\\n'}, StringSplitOptions.None);\r\n\r\n            foreach (var sentence in sentences)\r\n            {\r\n                var words = sentence.Split(new[] {' '}, StringSplitOptions.None);\r\n\r\n                for (var i = 0; i < words.Length; i++)\r\n                {\r\n                    var word = words[i];\r\n                    var size = font.GetStringRectangle(word, Vector2.Zero);\r\n\r\n                    if ((i != 0) && (dx + size.Width >= wrapWidth))\r\n                    {\r\n                        dy += font.LineHeight;\r\n                        dx = position.X;\r\n                    }\r\n\r\n                    DrawInternal(spriteBatch, font, word, new Vector2(dx, dy), color, layerDepth);\r\n                    dx += size.Width;\r\n\r\n                    var spaceCharRegion = font.GetCharacterRegion(' ');\r\n                    if (i != words.Length - 1)\r\n                        dx += spaceCharRegion.XAdvance + font.LetterSpacing;\r\n                    else\r\n                        dx += spaceCharRegion.XOffset + spaceCharRegion.Width;\r\n                }\r\n\r\n                dx = position.X;\r\n                dy += font.LineHeight;\r\n            }\r\n        }\r\n"},"comment":{"url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/comments/263453159","html_url":"https://github.com/craftworkgames/MonoGame.Extended/issues/318#issuecomment-263453159","issue_url":"https://api.github.com/repos/craftworkgames/MonoGame.Extended/issues/318","id":263453159,"user":{"login":"craftworkgames","id":3201643,"avatar_url":"https://avatars.githubusercontent.com/u/3201643?v=3","gravatar_id":"","url":"https://api.github.com/users/craftworkgames","html_url":"https://github.com/craftworkgames","followers_url":"https://api.github.com/users/craftworkgames/followers","following_url":"https://api.github.com/users/craftworkgames/following{/other_user}","gists_url":"https://api.github.com/users/craftworkgames/gists{/gist_id}","starred_url":"https://api.github.com/users/craftworkgames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/craftworkgames/subscriptions","organizations_url":"https://api.github.com/users/craftworkgames/orgs","repos_url":"https://api.github.com/users/craftworkgames/repos","events_url":"https://api.github.com/users/craftworkgames/events{/privacy}","received_events_url":"https://api.github.com/users/craftworkgames/received_events","type":"User","site_admin":false},"created_at":"2016-11-29T01:52:33Z","updated_at":"2016-11-29T01:52:33Z","body":"> You mentioned renderers, which ones do you have in mind? \r\n\r\n`Spritefont` and `BitmapFont` are not renderers. They are the the model.\r\n\r\nThe current renderer uses `SpriteBatch` via an [extension method](https://github.com/craftworkgames/MonoGame.Extended/blob/develop/Source/MonoGame.Extended/BitmapFonts/BitmapFontExtensions.cs#L30) so it's effectively decoupled from the model.\r\n\r\n@LithiumToast has also been working on a renderer called [DynamicBatch2D](https://github.com/craftworkgames/MonoGame.Extended/blob/develop/Source/MonoGame.Extended.Graphics/DynamicBatch2D.cs) which could be used.\r\n\r\nBut the point is that if we decouple the responsiblities the renderer can be anything. Someone might want to write a renderer that writes the text to a texture floating in 3D space for example. \r\n"}}
{"actor":{"display_login":"craftworkgames"}}