Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BitmapImporter.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 // -----------------------------------------------------------------------------
24 // The following code is a port of MakeSpriteFont from DirectXTk
25 // http://go.microsoft.com/fwlink/?LinkId=248929
26 // -----------------------------------------------------------------------------
27 // Microsoft Public License (Ms-PL)
28 //
29 // This license governs use of the accompanying software. If you use the
30 // software, you accept this license. If you do not accept the license, do not
31 // use the software.
32 //
33 // 1. Definitions
34 // The terms "reproduce," "reproduction," "derivative works," and
35 // "distribution" have the same meaning here as under U.S. copyright law.
36 // A "contribution" is the original software, or any additions or changes to
37 // the software.
38 // A "contributor" is any person that distributes its contribution under this
39 // license.
40 // "Licensed patents" are a contributor's patent claims that read directly on
41 // its contribution.
42 //
43 // 2. Grant of Rights
44 // (A) Copyright Grant- Subject to the terms of this license, including the
45 // license conditions and limitations in section 3, each contributor grants
46 // you a non-exclusive, worldwide, royalty-free copyright license to reproduce
47 // its contribution, prepare derivative works of its contribution, and
48 // distribute its contribution or any derivative works that you create.
49 // (B) Patent Grant- Subject to the terms of this license, including the license
50 // conditions and limitations in section 3, each contributor grants you a
51 // non-exclusive, worldwide, royalty-free license under its licensed patents to
52 // make, have made, use, sell, offer for sale, import, and/or otherwise dispose
53 // of its contribution in the software or derivative works of the contribution
54 // in the software.
55 //
56 // 3. Conditions and Limitations
57 // (A) No Trademark License- This license does not grant you rights to use any
58 // contributors' name, logo, or trademarks.
59 // (B) If you bring a patent claim against any contributor over patents that
60 // you claim are infringed by the software, your patent license from such
61 // contributor to the software ends automatically.
62 // (C) If you distribute any portion of the software, you must retain all
63 // copyright, patent, trademark, and attribution notices that are present in the
64 // software.
65 // (D) If you distribute any portion of the software in source code form, you
66 // may do so only under this license by including a complete copy of this
67 // license with your distribution. If you distribute any portion of the software
68 // in compiled or object code form, you may only do so under a license that
69 // complies with this license.
70 // (E) The software is licensed "as-is." You bear the risk of using it. The
71 // contributors give no express warranties, guarantees or conditions. You may
72 // have additional consumer rights under your local laws which this license
73 // cannot change. To the extent permitted under your local laws, the
74 // contributors exclude the implied warranties of merchantability, fitness for a
75 // particular purpose and non-infringement.
76 //--------------------------------------------------------------------
77 
78 using System;
79 using System.Collections.Generic;
80 using System.Drawing;
81 using System.Drawing.Imaging;
82 
83 namespace SiliconStudio.Paradox.Assets.SpriteFont.Compiler
84 {
85  // Extracts font glyphs from a specially marked 2D bitmap. Characters should be
86  // arranged in a grid ordered from top left to bottom right. Monochrome characters
87  // should use white for solid areas and black for transparent areas. To include
88  // multicolored characters, add an alpha channel to the bitmap and use that to
89  // control which parts of the character are solid. The spaces between characters
90  // and around the edges of the grid should be filled with bright pink (red=255,
91  // green=0, blue=255). It doesn't matter if your grid includes lots of wasted space,
92  // because the converter will rearrange characters, packing as tightly as possible.
93  internal class BitmapImporter : IFontImporter
94  {
95  // Properties hold the imported font data.
96  public IEnumerable<Glyph> Glyphs { get; private set; }
97 
98  public float LineSpacing { get; private set; }
99 
100  public float BaseLine { get { return 0; } }
101 
102  public void Import(SpriteFontAsset options, List<char> characters)
103  {
104  // Load the source bitmap.
105  Bitmap bitmap;
106 
107  try
108  {
109  bitmap = new Bitmap(options.Source);
110  }
111  catch
112  {
113  throw new FontNotFoundException(options.Source);
114  }
115 
116  // Convert to our desired pixel format.
117  bitmap = BitmapUtils.ChangePixelFormat(bitmap, PixelFormat.Format32bppArgb);
118 
119  // What characters are included in this font?
120  int characterIndex = 0;
121  char currentCharacter = '\0';
122 
123  // Split the source image into a list of individual glyphs.
124  var glyphList = new List<Glyph>();
125 
126  Glyphs = glyphList;
127  LineSpacing = 0;
128 
129  foreach (Rectangle rectangle in FindGlyphs(bitmap))
130  {
131  if (characterIndex < characters.Count)
132  currentCharacter = characters[characterIndex++];
133  else
134  currentCharacter++;
135 
136  glyphList.Add(new Glyph(currentCharacter, bitmap, rectangle) { XAdvance = rectangle.Width });
137 
138  LineSpacing = Math.Max(LineSpacing, rectangle.Height);
139  }
140 
141  // If the bitmap doesn't already have an alpha channel, create one now.
142  if (BitmapUtils.IsAlphaEntirely(255, bitmap))
143  {
144  BitmapUtils.ConvertGreyToAlpha(bitmap, new Rectangle(0,0,bitmap.Width, bitmap.Height));
145  }
146  }
147 
148 
149  // Searches a 2D bitmap for characters that are surrounded by a marker pink color.
150  static IEnumerable<Rectangle> FindGlyphs(Bitmap bitmap)
151  {
152  using (var bitmapData = new BitmapUtils.PixelAccessor(bitmap, ImageLockMode.ReadOnly))
153  {
154  for (int y = 1; y < bitmap.Height; y++)
155  {
156  for (int x = 1; x < bitmap.Width; x++)
157  {
158  // Look for the top left corner of a character (a pixel that is not pink, but was pink immediately to the left and above it)
159  if (!IsMarkerColor(bitmapData[x, y]) &&
160  IsMarkerColor(bitmapData[x - 1, y]) &&
161  IsMarkerColor(bitmapData[x, y - 1]))
162  {
163  // Measure the size of this character.
164  int w = 1, h = 1;
165 
166  while ((x + w < bitmap.Width) && !IsMarkerColor(bitmapData[x + w, y]))
167  {
168  w++;
169  }
170 
171  while ((y + h < bitmap.Height) && !IsMarkerColor(bitmapData[x, y + h]))
172  {
173  h++;
174  }
175 
176  yield return new Rectangle(x, y, w, h);
177  }
178  }
179  }
180  }
181  }
182 
183 
184  // Checks whether a color is the magic magenta marker value.
185  static bool IsMarkerColor(Color color)
186  {
187  return color.ToArgb() == Color.Magenta.ToArgb();
188  }
189  }
190 }
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
UFile Source
Gets or sets the source file containing the font data if required.
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16