| 1 | /** | 
|---|
| 2 | * Copyright (c) 2006-2023 LOVE Development Team | 
|---|
| 3 | * | 
|---|
| 4 | * This software is provided 'as-is', without any express or implied | 
|---|
| 5 | * warranty.  In no event will the authors be held liable for any damages | 
|---|
| 6 | * arising from the use of this software. | 
|---|
| 7 | * | 
|---|
| 8 | * Permission is granted to anyone to use this software for any purpose, | 
|---|
| 9 | * including commercial applications, and to alter it and redistribute it | 
|---|
| 10 | * freely, subject to the following restrictions: | 
|---|
| 11 | * | 
|---|
| 12 | * 1. The origin of this software must not be misrepresented; you must not | 
|---|
| 13 | *    claim that you wrote the original software. If you use this software | 
|---|
| 14 | *    in a product, an acknowledgment in the product documentation would be | 
|---|
| 15 | *    appreciated but is not required. | 
|---|
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be | 
|---|
| 17 | *    misrepresented as being the original software. | 
|---|
| 18 | * 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 19 | **/ | 
|---|
| 20 |  | 
|---|
| 21 | // LOVE | 
|---|
| 22 | #include "ImageRasterizer.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "common/Exception.h" | 
|---|
| 25 | #include <string.h> | 
|---|
| 26 |  | 
|---|
| 27 | namespace love | 
|---|
| 28 | { | 
|---|
| 29 | namespace font | 
|---|
| 30 | { | 
|---|
| 31 |  | 
|---|
| 32 | static_assert(sizeof(Color32) == 4, "sizeof(Color32) must equal 4 bytes!"); | 
|---|
| 33 |  | 
|---|
| 34 | ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int , float dpiscale) | 
|---|
| 35 | : imageData(data) | 
|---|
| 36 | , glyphs(glyphs) | 
|---|
| 37 | , numglyphs(numglyphs) | 
|---|
| 38 | , extraSpacing(extraspacing) | 
|---|
| 39 | { | 
|---|
| 40 | this->dpiScale = dpiscale; | 
|---|
| 41 |  | 
|---|
| 42 | if (data->getFormat() != PIXELFORMAT_RGBA8) | 
|---|
| 43 | throw love::Exception( "Only 32-bit RGBA images are supported in Image Fonts!"); | 
|---|
| 44 |  | 
|---|
| 45 | load(); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | ImageRasterizer::~ImageRasterizer() | 
|---|
| 49 | { | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | int ImageRasterizer::getLineHeight() const | 
|---|
| 53 | { | 
|---|
| 54 | return getHeight(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const | 
|---|
| 58 | { | 
|---|
| 59 | GlyphMetrics gm = {}; | 
|---|
| 60 |  | 
|---|
| 61 | // Set relevant glyph metrics if the glyph is in this ImageFont | 
|---|
| 62 | std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph); | 
|---|
| 63 | if (it != imageGlyphs.end()) | 
|---|
| 64 | { | 
|---|
| 65 | gm.width = it->second.width; | 
|---|
| 66 | gm.advance = it->second.width + extraSpacing; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | gm.height = metrics.height; | 
|---|
| 70 |  | 
|---|
| 71 | GlyphData *g = new GlyphData(glyph, gm, PIXELFORMAT_RGBA8); | 
|---|
| 72 |  | 
|---|
| 73 | if (gm.width == 0) | 
|---|
| 74 | return g; | 
|---|
| 75 |  | 
|---|
| 76 | // We don't want another thread modifying our ImageData mid-copy. | 
|---|
| 77 | love::thread::Lock lock(imageData->getMutex()); | 
|---|
| 78 |  | 
|---|
| 79 | Color32 *gdpixels = (Color32 *) g->getData(); | 
|---|
| 80 | const Color32 *imagepixels = (const Color32 *) imageData->getData(); | 
|---|
| 81 |  | 
|---|
| 82 | // copy glyph pixels from imagedata to glyphdata | 
|---|
| 83 | for (int i = 0; i < g->getWidth() * g->getHeight(); i++) | 
|---|
| 84 | { | 
|---|
| 85 | Color32 p = imagepixels[it->second.x + (i % gm.width) + (imageData->getWidth() * (i / gm.width))]; | 
|---|
| 86 |  | 
|---|
| 87 | // Use transparency instead of the spacer color | 
|---|
| 88 | if (p == spacer) | 
|---|
| 89 | gdpixels[i] = Color32(0, 0, 0, 0); | 
|---|
| 90 | else | 
|---|
| 91 | gdpixels[i] = p; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | return g; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void ImageRasterizer::load() | 
|---|
| 98 | { | 
|---|
| 99 | auto pixels = (const Color32 *) imageData->getData(); | 
|---|
| 100 |  | 
|---|
| 101 | int imgw = imageData->getWidth(); | 
|---|
| 102 | int imgh = imageData->getHeight(); | 
|---|
| 103 |  | 
|---|
| 104 | // We don't want another thread modifying our ImageData mid-parse. | 
|---|
| 105 | love::thread::Lock lock(imageData->getMutex()); | 
|---|
| 106 |  | 
|---|
| 107 | // Set the only metric that matters | 
|---|
| 108 | metrics.height = imgh; | 
|---|
| 109 |  | 
|---|
| 110 | // Reading texture data begins | 
|---|
| 111 | spacer = pixels[0]; | 
|---|
| 112 |  | 
|---|
| 113 | int start = 0; | 
|---|
| 114 | int end = 0; | 
|---|
| 115 |  | 
|---|
| 116 | for (int i = 0; i < numglyphs; ++i) | 
|---|
| 117 | { | 
|---|
| 118 | start = end; | 
|---|
| 119 |  | 
|---|
| 120 | // Finds out where the first character starts | 
|---|
| 121 | while (start < imgw && pixels[start] == spacer) | 
|---|
| 122 | ++start; | 
|---|
| 123 |  | 
|---|
| 124 | end = start; | 
|---|
| 125 |  | 
|---|
| 126 | // Find where glyph ends. | 
|---|
| 127 | while (end < imgw && pixels[end] != spacer) | 
|---|
| 128 | ++end; | 
|---|
| 129 |  | 
|---|
| 130 | if (start >= end) | 
|---|
| 131 | break; | 
|---|
| 132 |  | 
|---|
| 133 | ImageGlyphData imageGlyph; | 
|---|
| 134 | imageGlyph.x = start; | 
|---|
| 135 | imageGlyph.width = end - start; | 
|---|
| 136 |  | 
|---|
| 137 | imageGlyphs[glyphs[i]] = imageGlyph; | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | int ImageRasterizer::getGlyphCount() const | 
|---|
| 142 | { | 
|---|
| 143 | return numglyphs; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | bool ImageRasterizer::hasGlyph(uint32 glyph) const | 
|---|
| 147 | { | 
|---|
| 148 | return imageGlyphs.find(glyph) != imageGlyphs.end(); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | Rasterizer::DataType ImageRasterizer::getDataType() const | 
|---|
| 152 | { | 
|---|
| 153 | return DATA_IMAGE; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | } // font | 
|---|
| 157 | } // love | 
|---|
| 158 |  | 
|---|