add msdf-atlas-gen libs linux64 & emscripten
This commit is contained in:
parent
0bb0e87a33
commit
6fb3f95348
143 changed files with 13729 additions and 0 deletions
21
libs/msdf-atlas-gen/include/LICENSE.txt
Normal file
21
libs/msdf-atlas-gen/include/LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 - 2023 Viktor Chlumsky
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
21
libs/msdf-atlas-gen/include/artery-font-format/LICENSE.txt
Normal file
21
libs/msdf-atlas-gen/include/artery-font-format/LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Viktor Chlumsky
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
6
libs/msdf-atlas-gen/include/artery-font-format/README.md
Normal file
6
libs/msdf-atlas-gen/include/artery-font-format/README.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
# Artery Atlas Font format library
|
||||||
|
|
||||||
|
This is a header-only C++ library that facilitates encoding and decoding of the Artery Atlas Font format – a specialized binary file format for storing fonts as bitmap atlases used by the [Artery Engine](https://www.arteryengine.com/), intended for use in video games and other hardware accelerated applications.
|
||||||
|
|
||||||
|
An Artery Atlas font file (*.arfont) wraps together the atlas bitmap(s), which can be compressed e.g. in PNG format, the layout of the atlas, as well as the font's and the individual glyphs' metrics and positioning data, including kerning pairs.
|
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "enums.h"
|
||||||
|
#include "structures.h"
|
||||||
|
#include "serialization.h"
|
||||||
|
|
||||||
|
// ARTERY ENGINE ATLAS FONT FORMAT LIBRARY v1.0.1
|
||||||
|
// Author: Viktor Chlumsky (c) 2020 - 2022
|
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
uint32 crc32Init();
|
||||||
|
uint32 crc32Update(uint32 crc, byte x);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "crc32.hpp"
|
|
@ -0,0 +1,48 @@
|
||||||
|
|
||||||
|
#include "crc32.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
inline uint32 crc32Init() {
|
||||||
|
return ~0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint32 crc32Update(uint32 crc, byte x) {
|
||||||
|
static const uint32 crc32Table[256] = {
|
||||||
|
0x00000000u, 0x77073096u, 0xee0e612cu, 0x990951bau, 0x076dc419u, 0x706af48fu, 0xe963a535u, 0x9e6495a3u,
|
||||||
|
0x0edb8832u, 0x79dcb8a4u, 0xe0d5e91eu, 0x97d2d988u, 0x09b64c2bu, 0x7eb17cbdu, 0xe7b82d07u, 0x90bf1d91u,
|
||||||
|
0x1db71064u, 0x6ab020f2u, 0xf3b97148u, 0x84be41deu, 0x1adad47du, 0x6ddde4ebu, 0xf4d4b551u, 0x83d385c7u,
|
||||||
|
0x136c9856u, 0x646ba8c0u, 0xfd62f97au, 0x8a65c9ecu, 0x14015c4fu, 0x63066cd9u, 0xfa0f3d63u, 0x8d080df5u,
|
||||||
|
0x3b6e20c8u, 0x4c69105eu, 0xd56041e4u, 0xa2677172u, 0x3c03e4d1u, 0x4b04d447u, 0xd20d85fdu, 0xa50ab56bu,
|
||||||
|
0x35b5a8fau, 0x42b2986cu, 0xdbbbc9d6u, 0xacbcf940u, 0x32d86ce3u, 0x45df5c75u, 0xdcd60dcfu, 0xabd13d59u,
|
||||||
|
0x26d930acu, 0x51de003au, 0xc8d75180u, 0xbfd06116u, 0x21b4f4b5u, 0x56b3c423u, 0xcfba9599u, 0xb8bda50fu,
|
||||||
|
0x2802b89eu, 0x5f058808u, 0xc60cd9b2u, 0xb10be924u, 0x2f6f7c87u, 0x58684c11u, 0xc1611dabu, 0xb6662d3du,
|
||||||
|
0x76dc4190u, 0x01db7106u, 0x98d220bcu, 0xefd5102au, 0x71b18589u, 0x06b6b51fu, 0x9fbfe4a5u, 0xe8b8d433u,
|
||||||
|
0x7807c9a2u, 0x0f00f934u, 0x9609a88eu, 0xe10e9818u, 0x7f6a0dbbu, 0x086d3d2du, 0x91646c97u, 0xe6635c01u,
|
||||||
|
0x6b6b51f4u, 0x1c6c6162u, 0x856530d8u, 0xf262004eu, 0x6c0695edu, 0x1b01a57bu, 0x8208f4c1u, 0xf50fc457u,
|
||||||
|
0x65b0d9c6u, 0x12b7e950u, 0x8bbeb8eau, 0xfcb9887cu, 0x62dd1ddfu, 0x15da2d49u, 0x8cd37cf3u, 0xfbd44c65u,
|
||||||
|
0x4db26158u, 0x3ab551ceu, 0xa3bc0074u, 0xd4bb30e2u, 0x4adfa541u, 0x3dd895d7u, 0xa4d1c46du, 0xd3d6f4fbu,
|
||||||
|
0x4369e96au, 0x346ed9fcu, 0xad678846u, 0xda60b8d0u, 0x44042d73u, 0x33031de5u, 0xaa0a4c5fu, 0xdd0d7cc9u,
|
||||||
|
0x5005713cu, 0x270241aau, 0xbe0b1010u, 0xc90c2086u, 0x5768b525u, 0x206f85b3u, 0xb966d409u, 0xce61e49fu,
|
||||||
|
0x5edef90eu, 0x29d9c998u, 0xb0d09822u, 0xc7d7a8b4u, 0x59b33d17u, 0x2eb40d81u, 0xb7bd5c3bu, 0xc0ba6cadu,
|
||||||
|
0xedb88320u, 0x9abfb3b6u, 0x03b6e20cu, 0x74b1d29au, 0xead54739u, 0x9dd277afu, 0x04db2615u, 0x73dc1683u,
|
||||||
|
0xe3630b12u, 0x94643b84u, 0x0d6d6a3eu, 0x7a6a5aa8u, 0xe40ecf0bu, 0x9309ff9du, 0x0a00ae27u, 0x7d079eb1u,
|
||||||
|
0xf00f9344u, 0x8708a3d2u, 0x1e01f268u, 0x6906c2feu, 0xf762575du, 0x806567cbu, 0x196c3671u, 0x6e6b06e7u,
|
||||||
|
0xfed41b76u, 0x89d32be0u, 0x10da7a5au, 0x67dd4accu, 0xf9b9df6fu, 0x8ebeeff9u, 0x17b7be43u, 0x60b08ed5u,
|
||||||
|
0xd6d6a3e8u, 0xa1d1937eu, 0x38d8c2c4u, 0x4fdff252u, 0xd1bb67f1u, 0xa6bc5767u, 0x3fb506ddu, 0x48b2364bu,
|
||||||
|
0xd80d2bdau, 0xaf0a1b4cu, 0x36034af6u, 0x41047a60u, 0xdf60efc3u, 0xa867df55u, 0x316e8eefu, 0x4669be79u,
|
||||||
|
0xcb61b38cu, 0xbc66831au, 0x256fd2a0u, 0x5268e236u, 0xcc0c7795u, 0xbb0b4703u, 0x220216b9u, 0x5505262fu,
|
||||||
|
0xc5ba3bbeu, 0xb2bd0b28u, 0x2bb45a92u, 0x5cb36a04u, 0xc2d7ffa7u, 0xb5d0cf31u, 0x2cd99e8bu, 0x5bdeae1du,
|
||||||
|
0x9b64c2b0u, 0xec63f226u, 0x756aa39cu, 0x026d930au, 0x9c0906a9u, 0xeb0e363fu, 0x72076785u, 0x05005713u,
|
||||||
|
0x95bf4a82u, 0xe2b87a14u, 0x7bb12baeu, 0x0cb61b38u, 0x92d28e9bu, 0xe5d5be0du, 0x7cdcefb7u, 0x0bdbdf21u,
|
||||||
|
0x86d3d2d4u, 0xf1d4e242u, 0x68ddb3f8u, 0x1fda836eu, 0x81be16cdu, 0xf6b9265bu, 0x6fb077e1u, 0x18b74777u,
|
||||||
|
0x88085ae6u, 0xff0f6a70u, 0x66063bcau, 0x11010b5cu, 0x8f659effu, 0xf862ae69u, 0x616bffd3u, 0x166ccf45u,
|
||||||
|
0xa00ae278u, 0xd70dd2eeu, 0x4e048354u, 0x3903b3c2u, 0xa7672661u, 0xd06016f7u, 0x4969474du, 0x3e6e77dbu,
|
||||||
|
0xaed16a4au, 0xd9d65adcu, 0x40df0b66u, 0x37d83bf0u, 0xa9bcae53u, 0xdebb9ec5u, 0x47b2cf7fu, 0x30b5ffe9u,
|
||||||
|
0xbdbdf21cu, 0xcabac28au, 0x53b39330u, 0x24b4a3a6u, 0xbad03605u, 0xcdd70693u, 0x54de5729u, 0x23d967bfu,
|
||||||
|
0xb3667a2eu, 0xc4614ab8u, 0x5d681b02u, 0x2a6f2b94u, 0xb40bbe37u, 0xc30c8ea1u, 0x5a05df1bu, 0x2d02ef8du,
|
||||||
|
};
|
||||||
|
return crc32Table[byte(x^crc)]^crc>>8;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
enum FontFlags {
|
||||||
|
FONT_BOLD = 0x01,
|
||||||
|
FONT_LIGHT = 0x02,
|
||||||
|
FONT_EXTRA_BOLD = 0x04,
|
||||||
|
FONT_CONDENSED = 0x08,
|
||||||
|
FONT_ITALIC = 0x10,
|
||||||
|
FONT_SMALL_CAPS = 0x20,
|
||||||
|
FONT_ICONOGRAPHIC = 0x0100,
|
||||||
|
FONT_SANS_SERIF = 0x0200,
|
||||||
|
FONT_SERIF = 0x0400,
|
||||||
|
FONT_MONOSPACE = 0x1000,
|
||||||
|
FONT_CURSIVE = 0x2000
|
||||||
|
};
|
||||||
|
|
||||||
|
enum CodepointType {
|
||||||
|
CP_UNSPECIFIED = 0,
|
||||||
|
CP_UNICODE = 1,
|
||||||
|
CP_INDEXED = 2,
|
||||||
|
CP_ICONOGRAPHIC = 14
|
||||||
|
};
|
||||||
|
|
||||||
|
enum MetadataFormat {
|
||||||
|
METADATA_NONE = 0,
|
||||||
|
METADATA_PLAINTEXT = 1,
|
||||||
|
METADATA_JSON = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImageType {
|
||||||
|
IMAGE_NONE = 0,
|
||||||
|
IMAGE_SRGB_IMAGE = 1,
|
||||||
|
IMAGE_LINEAR_MASK = 2,
|
||||||
|
IMAGE_MASKED_SRGB_IMAGE = 3,
|
||||||
|
IMAGE_SDF = 4,
|
||||||
|
IMAGE_PSDF = 5,
|
||||||
|
IMAGE_MSDF = 6,
|
||||||
|
IMAGE_MTSDF = 7,
|
||||||
|
IMAGE_MIXED_CONTENT = 255
|
||||||
|
};
|
||||||
|
|
||||||
|
enum PixelFormat {
|
||||||
|
PIXEL_UNKNOWN = 0,
|
||||||
|
PIXEL_BOOLEAN1 = 1,
|
||||||
|
PIXEL_UNSIGNED8 = 8,
|
||||||
|
PIXEL_FLOAT32 = 32
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImageEncoding {
|
||||||
|
IMAGE_UNKNOWN_ENCODING = 0,
|
||||||
|
IMAGE_RAW_BINARY = 1,
|
||||||
|
IMAGE_BMP = 4,
|
||||||
|
IMAGE_TIFF = 5,
|
||||||
|
IMAGE_PNG = 8,
|
||||||
|
IMAGE_TGA = 9
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImageOrientation {
|
||||||
|
ORIENTATION_TOP_DOWN = 1,
|
||||||
|
ORIENTATION_BOTTOM_UP = -1
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "enums.h"
|
||||||
|
#include "structures.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
typedef int ReadFunction(void *dst, int limit, void *userData);
|
||||||
|
typedef int WriteFunction(const void *src, int length, void *userData);
|
||||||
|
|
||||||
|
template <ReadFunction READ, typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool decode(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, void *userData);
|
||||||
|
|
||||||
|
template <WriteFunction WRITE, typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool encode(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, void *userData);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "serialization.hpp"
|
|
@ -0,0 +1,394 @@
|
||||||
|
|
||||||
|
#include "serialization.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include "crc32.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
#define ARTERY_FONT_HEADER_TAG "ARTERY/FONT\0\0\0\0\0"
|
||||||
|
#define ARTERY_FONT_HEADER_VERSION 1u
|
||||||
|
#define ARTERY_FONT_HEADER_MAGIC_NO 0x4d276a5cu
|
||||||
|
#define ARTERY_FONT_FOOTER_MAGIC_NO 0x55ccb363u
|
||||||
|
|
||||||
|
struct ArteryFontHeader {
|
||||||
|
char tag[16];
|
||||||
|
uint32 magicNo;
|
||||||
|
uint32 version;
|
||||||
|
uint32 flags;
|
||||||
|
uint32 realType;
|
||||||
|
uint32 reserved[4];
|
||||||
|
|
||||||
|
uint32 metadataFormat;
|
||||||
|
uint32 metadataLength;
|
||||||
|
uint32 variantCount;
|
||||||
|
uint32 variantsLength;
|
||||||
|
uint32 imageCount;
|
||||||
|
uint32 imagesLength;
|
||||||
|
uint32 appendixCount;
|
||||||
|
uint32 appendicesLength;
|
||||||
|
uint32 reserved2[8];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ArteryFontFooter {
|
||||||
|
uint32 salt;
|
||||||
|
uint32 magicNo;
|
||||||
|
uint32 reserved[4];
|
||||||
|
uint32 totalLength;
|
||||||
|
uint32 checksum;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename REAL>
|
||||||
|
struct FontVariantHeader {
|
||||||
|
uint32 flags;
|
||||||
|
uint32 weight;
|
||||||
|
uint32 codepointType;
|
||||||
|
uint32 imageType;
|
||||||
|
uint32 fallbackVariant;
|
||||||
|
uint32 fallbackGlyph;
|
||||||
|
uint32 reserved[6];
|
||||||
|
REAL metrics[32];
|
||||||
|
uint32 nameLength;
|
||||||
|
uint32 metadataLength;
|
||||||
|
uint32 glyphCount;
|
||||||
|
uint32 kernPairCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ImageHeader {
|
||||||
|
uint32 flags;
|
||||||
|
uint32 encoding;
|
||||||
|
uint32 width, height;
|
||||||
|
uint32 channels;
|
||||||
|
uint32 pixelFormat;
|
||||||
|
uint32 imageType;
|
||||||
|
uint32 rowLength;
|
||||||
|
sint32 orientation;
|
||||||
|
uint32 childImages;
|
||||||
|
uint32 textureFlags;
|
||||||
|
uint32 reserved[3];
|
||||||
|
uint32 metadataLength;
|
||||||
|
uint32 dataLength;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AppendixHeader {
|
||||||
|
uint32 metadataLength;
|
||||||
|
uint32 dataLength;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename REAL>
|
||||||
|
uint32 realTypeCode();
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline uint32 realTypeCode<float>() {
|
||||||
|
return 0x14u;
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline uint32 realTypeCode<double>() {
|
||||||
|
return 0x18u;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint32 paddedLength(uint32 len) {
|
||||||
|
if (len&0x03u)
|
||||||
|
len += 0x04u-(len&0x03u);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class STRING>
|
||||||
|
uint32 paddedStringLength(const STRING &str) {
|
||||||
|
uint32 len = str.length();
|
||||||
|
return paddedLength(len+(len > 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef __BIG_ENDIAN__
|
||||||
|
|
||||||
|
template <ReadFunction READ, typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool decode(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, void *userData) {
|
||||||
|
uint32 totalLength = 0;
|
||||||
|
uint32 prevLength = 0;
|
||||||
|
uint32 checksum = crc32Init();
|
||||||
|
byte dump[4];
|
||||||
|
#define ARTERY_FONT_DECODE_READ(target, len) { \
|
||||||
|
if (READ((target), (len), userData) != int(len)) \
|
||||||
|
return false; \
|
||||||
|
totalLength += (len); \
|
||||||
|
for (int i = 0; i < int(len); ++i) \
|
||||||
|
checksum = crc32Update(checksum, reinterpret_cast<const byte *>(target)[i]); \
|
||||||
|
}
|
||||||
|
#define ARTERY_FONT_DECODE_REALIGN() { \
|
||||||
|
if (totalLength&0x03u) { \
|
||||||
|
uint32 len = 0x04u-(totalLength&0x03u); \
|
||||||
|
ARTERY_FONT_DECODE_READ(dump, len); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#define ARTERY_FONT_DECODE_READ_STRING(str, len) { \
|
||||||
|
if ((len) > 0) { \
|
||||||
|
LIST<char> characters((len)+1); \
|
||||||
|
ARTERY_FONT_DECODE_READ((char *) characters, (len)+1); \
|
||||||
|
((char *) characters)[len] = '\0'; \
|
||||||
|
(str) = STRING((const char *) characters, int(len)); \
|
||||||
|
ARTERY_FONT_DECODE_REALIGN(); \
|
||||||
|
} else \
|
||||||
|
(str) = STRING(); \
|
||||||
|
}
|
||||||
|
int variantCount = 0;
|
||||||
|
int imageCount = 0;
|
||||||
|
int appendixCount = 0;
|
||||||
|
uint32 variantsLength = 0;
|
||||||
|
uint32 imagesLength = 0;
|
||||||
|
uint32 appendicesLength = 0;
|
||||||
|
// Read header
|
||||||
|
{
|
||||||
|
internal::ArteryFontHeader header;
|
||||||
|
ARTERY_FONT_DECODE_READ(&header, sizeof(header));
|
||||||
|
if (memcmp(header.tag, ARTERY_FONT_HEADER_TAG, sizeof(header.tag)))
|
||||||
|
return false;
|
||||||
|
if (header.magicNo != ARTERY_FONT_HEADER_MAGIC_NO)
|
||||||
|
return false;
|
||||||
|
if (header.realType != internal::realTypeCode<REAL>())
|
||||||
|
return false;
|
||||||
|
font.metadataFormat = (MetadataFormat) header.metadataFormat;
|
||||||
|
ARTERY_FONT_DECODE_READ_STRING(font.metadata, header.metadataLength);
|
||||||
|
variantCount = header.variantCount;
|
||||||
|
imageCount = header.imageCount;
|
||||||
|
appendixCount = header.appendixCount;
|
||||||
|
font.variants = LIST<FontVariant<REAL, LIST, STRING> >(header.variantCount);
|
||||||
|
font.images = LIST<FontImage<BYTE_ARRAY, STRING> >(header.imageCount);
|
||||||
|
font.appendices = LIST<FontAppendix<BYTE_ARRAY, STRING> >(header.appendixCount);
|
||||||
|
variantsLength = header.variantsLength;
|
||||||
|
imagesLength = header.imagesLength;
|
||||||
|
appendicesLength = header.appendicesLength;
|
||||||
|
}
|
||||||
|
prevLength = totalLength;
|
||||||
|
// Read variants
|
||||||
|
for (int i = 0; i < variantCount; ++i) {
|
||||||
|
FontVariant<REAL, LIST, STRING> &variant = font.variants[i];
|
||||||
|
internal::FontVariantHeader<REAL> header;
|
||||||
|
ARTERY_FONT_DECODE_READ(&header, sizeof(header));
|
||||||
|
variant.flags = header.flags;
|
||||||
|
variant.weight = header.weight;
|
||||||
|
variant.codepointType = (CodepointType) header.codepointType;
|
||||||
|
variant.imageType = (ImageType) header.imageType;
|
||||||
|
variant.fallbackVariant = header.fallbackVariant;
|
||||||
|
variant.fallbackGlyph = header.fallbackGlyph;
|
||||||
|
memcpy(&variant.metrics, header.metrics, sizeof(header.metrics));
|
||||||
|
ARTERY_FONT_DECODE_READ_STRING(variant.name, header.nameLength);
|
||||||
|
ARTERY_FONT_DECODE_READ_STRING(variant.metadata, header.metadataLength);
|
||||||
|
variant.glyphs = LIST<Glyph<REAL> >(header.glyphCount);
|
||||||
|
variant.kernPairs = LIST<KernPair<REAL> >(header.kernPairCount);
|
||||||
|
ARTERY_FONT_DECODE_READ((Glyph<REAL> *) variant.glyphs, header.glyphCount*sizeof(Glyph<REAL>));
|
||||||
|
ARTERY_FONT_DECODE_READ((KernPair<REAL> *) variant.kernPairs, header.kernPairCount*sizeof(KernPair<REAL>));
|
||||||
|
}
|
||||||
|
if (totalLength-prevLength != variantsLength)
|
||||||
|
return false;
|
||||||
|
prevLength = totalLength;
|
||||||
|
// Read images
|
||||||
|
for (int i = 0; i < imageCount; ++i) {
|
||||||
|
FontImage<BYTE_ARRAY, STRING> &image = font.images[i];
|
||||||
|
internal::ImageHeader header;
|
||||||
|
ARTERY_FONT_DECODE_READ(&header, sizeof(header));
|
||||||
|
image.flags = header.flags;
|
||||||
|
image.encoding = (ImageEncoding) header.encoding;
|
||||||
|
image.width = header.width;
|
||||||
|
image.height = header.height;
|
||||||
|
image.channels = header.channels;
|
||||||
|
image.pixelFormat = (PixelFormat) header.pixelFormat;
|
||||||
|
image.imageType = (ImageType) header.imageType;
|
||||||
|
image.rawBinaryFormat.rowLength = header.rowLength;
|
||||||
|
image.rawBinaryFormat.orientation = (ImageOrientation) header.orientation;
|
||||||
|
image.childImages = header.childImages;
|
||||||
|
image.textureFlags = header.textureFlags;
|
||||||
|
ARTERY_FONT_DECODE_READ_STRING(image.metadata, header.metadataLength);
|
||||||
|
image.data = BYTE_ARRAY(header.dataLength);
|
||||||
|
ARTERY_FONT_DECODE_READ((unsigned char *) image.data, header.dataLength);
|
||||||
|
ARTERY_FONT_DECODE_REALIGN();
|
||||||
|
}
|
||||||
|
if (totalLength-prevLength != imagesLength)
|
||||||
|
return false;
|
||||||
|
prevLength = totalLength;
|
||||||
|
// Read appendices
|
||||||
|
for (int i = 0; i < appendixCount; ++i) {
|
||||||
|
FontAppendix<BYTE_ARRAY, STRING> &appendix = font.appendices[i];
|
||||||
|
internal::AppendixHeader header;
|
||||||
|
ARTERY_FONT_DECODE_READ(&header, sizeof(header));
|
||||||
|
ARTERY_FONT_DECODE_READ_STRING(appendix.metadata, header.metadataLength);
|
||||||
|
appendix.data = BYTE_ARRAY(header.dataLength);
|
||||||
|
ARTERY_FONT_DECODE_READ((unsigned char *) appendix.data, header.dataLength);
|
||||||
|
ARTERY_FONT_DECODE_REALIGN();
|
||||||
|
}
|
||||||
|
if (totalLength-prevLength != appendicesLength)
|
||||||
|
return false;
|
||||||
|
prevLength = totalLength;
|
||||||
|
// Read footer
|
||||||
|
{
|
||||||
|
internal::ArteryFontFooter footer;
|
||||||
|
ARTERY_FONT_DECODE_READ(&footer, sizeof(footer)-sizeof(footer.checksum));
|
||||||
|
if (footer.magicNo != ARTERY_FONT_FOOTER_MAGIC_NO)
|
||||||
|
return false;
|
||||||
|
uint32 finalChecksum = checksum;
|
||||||
|
ARTERY_FONT_DECODE_READ(&footer.checksum, sizeof(footer.checksum));
|
||||||
|
if (footer.checksum != finalChecksum)
|
||||||
|
return false;
|
||||||
|
if (totalLength != footer.totalLength)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#undef ARTERY_FONT_DECODE_READ
|
||||||
|
#undef ARTERY_FONT_DECODE_REALIGN
|
||||||
|
#undef ARTERY_FONT_DECODE_READ_STRING
|
||||||
|
}
|
||||||
|
|
||||||
|
template <WriteFunction WRITE, typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool encode(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, void *userData) {
|
||||||
|
uint32 totalLength = 0;
|
||||||
|
uint32 checksum = crc32Init();
|
||||||
|
const byte padding[4] = { };
|
||||||
|
#define ARTERY_FONT_ENCODE_WRITE(data, len) { \
|
||||||
|
if (WRITE((data), (len), userData) != int(len)) \
|
||||||
|
return false; \
|
||||||
|
totalLength += (len); \
|
||||||
|
for (int i = 0; i < int(len); ++i) \
|
||||||
|
checksum = crc32Update(checksum, reinterpret_cast<const byte *>(data)[i]); \
|
||||||
|
}
|
||||||
|
#define ARTERY_FONT_ENCODE_REALIGN() { \
|
||||||
|
if (totalLength&0x03u) { \
|
||||||
|
uint32 len = 0x04u-(totalLength&0x03u); \
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(padding, len); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#define ARTERY_FONT_ENCODE_WRITE_STRING(str) { \
|
||||||
|
uint32 len = (str).length(); \
|
||||||
|
if ((len) > 0) { \
|
||||||
|
ARTERY_FONT_ENCODE_WRITE((const char *) (str), (len)); \
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(padding, 1) \
|
||||||
|
ARTERY_FONT_ENCODE_REALIGN(); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
int variantCount = 0;
|
||||||
|
int imageCount = 0;
|
||||||
|
int appendixCount = 0;
|
||||||
|
// Write header
|
||||||
|
{
|
||||||
|
internal::ArteryFontHeader header;
|
||||||
|
memcpy(header.tag, ARTERY_FONT_HEADER_TAG, sizeof(header.tag));
|
||||||
|
header.magicNo = ARTERY_FONT_HEADER_MAGIC_NO;
|
||||||
|
header.version = ARTERY_FONT_HEADER_VERSION;
|
||||||
|
header.flags = 0;
|
||||||
|
header.realType = internal::realTypeCode<REAL>();
|
||||||
|
memset(header.reserved, 0, sizeof(header.reserved));
|
||||||
|
header.metadataFormat = (uint32) font.metadataFormat;
|
||||||
|
header.metadataLength = font.metadata.length();
|
||||||
|
header.variantCount = variantCount = font.variants.length();
|
||||||
|
header.variantsLength = 0;
|
||||||
|
header.imageCount = imageCount = font.images.length();
|
||||||
|
header.imagesLength = 0;
|
||||||
|
header.appendixCount = appendixCount = font.appendices.length();
|
||||||
|
header.appendicesLength = 0;
|
||||||
|
memset(header.reserved2, 0, sizeof(header.reserved2));
|
||||||
|
for (int i = 0; i < variantCount; ++i) {
|
||||||
|
const FontVariant<REAL, LIST, STRING> &variant = font.variants[i];
|
||||||
|
header.variantsLength += sizeof(internal::FontVariantHeader<REAL>);
|
||||||
|
header.variantsLength += internal::paddedStringLength(variant.name);
|
||||||
|
header.variantsLength += internal::paddedStringLength(variant.metadata);
|
||||||
|
header.variantsLength += variant.glyphs.length()*sizeof(Glyph<REAL>);
|
||||||
|
header.variantsLength += variant.kernPairs.length()*sizeof(KernPair<REAL>);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < imageCount; ++i) {
|
||||||
|
const FontImage<BYTE_ARRAY, STRING> &image = font.images[i];
|
||||||
|
header.imagesLength += sizeof(internal::ImageHeader);
|
||||||
|
header.imagesLength += internal::paddedStringLength(image.metadata);
|
||||||
|
header.imagesLength += internal::paddedLength(image.data.length());
|
||||||
|
}
|
||||||
|
for (int i = 0; i < appendixCount; ++i) {
|
||||||
|
const FontAppendix<BYTE_ARRAY, STRING> &appendix = font.appendices[i];
|
||||||
|
header.appendicesLength += sizeof(internal::AppendixHeader);
|
||||||
|
header.appendicesLength += internal::paddedStringLength(appendix.metadata);
|
||||||
|
header.appendicesLength += internal::paddedLength(appendix.data.length());
|
||||||
|
}
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&header, sizeof(header));
|
||||||
|
ARTERY_FONT_ENCODE_WRITE_STRING(font.metadata);
|
||||||
|
}
|
||||||
|
// Write variants
|
||||||
|
for (int i = 0; i < variantCount; ++i) {
|
||||||
|
const FontVariant<REAL, LIST, STRING> &variant = font.variants[i];
|
||||||
|
internal::FontVariantHeader<REAL> header;
|
||||||
|
header.flags = variant.flags;
|
||||||
|
header.weight = variant.weight;
|
||||||
|
header.codepointType = (uint32) variant.codepointType;
|
||||||
|
header.imageType = (uint32) variant.imageType;
|
||||||
|
header.fallbackVariant = variant.fallbackVariant;
|
||||||
|
header.fallbackGlyph = variant.fallbackGlyph;
|
||||||
|
memset(header.reserved, 0, sizeof(header.reserved));
|
||||||
|
memcpy(header.metrics, &variant.metrics, sizeof(header.metrics));
|
||||||
|
header.nameLength = variant.name.length();
|
||||||
|
header.metadataLength = variant.metadata.length();
|
||||||
|
header.glyphCount = variant.glyphs.length();
|
||||||
|
header.kernPairCount = variant.kernPairs.length();
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&header, sizeof(header));
|
||||||
|
ARTERY_FONT_ENCODE_WRITE_STRING(variant.name);
|
||||||
|
ARTERY_FONT_ENCODE_WRITE_STRING(variant.metadata);
|
||||||
|
ARTERY_FONT_ENCODE_WRITE((const Glyph<REAL> *) variant.glyphs, header.glyphCount*sizeof(Glyph<REAL>));
|
||||||
|
ARTERY_FONT_ENCODE_WRITE((const KernPair<REAL> *) variant.kernPairs, header.kernPairCount*sizeof(KernPair<REAL>));
|
||||||
|
}
|
||||||
|
// Write images
|
||||||
|
for (int i = 0; i < imageCount; ++i) {
|
||||||
|
const FontImage<BYTE_ARRAY, STRING> &image = font.images[i];
|
||||||
|
internal::ImageHeader header;
|
||||||
|
header.flags = image.flags;
|
||||||
|
header.encoding = (uint32) image.encoding;
|
||||||
|
header.width = image.width;
|
||||||
|
header.height = image.height;
|
||||||
|
header.channels = image.channels;
|
||||||
|
header.pixelFormat = (uint32) image.pixelFormat;
|
||||||
|
header.imageType = (uint32) image.imageType;
|
||||||
|
header.rowLength = image.rawBinaryFormat.rowLength;
|
||||||
|
header.orientation = (sint32) image.rawBinaryFormat.orientation;
|
||||||
|
header.childImages = image.childImages;
|
||||||
|
header.textureFlags = image.textureFlags;
|
||||||
|
memset(header.reserved, 0, sizeof(header.reserved));
|
||||||
|
header.metadataLength = image.metadata.length();
|
||||||
|
header.dataLength = image.data.length();
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&header, sizeof(header));
|
||||||
|
ARTERY_FONT_ENCODE_WRITE_STRING(image.metadata);
|
||||||
|
ARTERY_FONT_ENCODE_WRITE((const unsigned char *) image.data, header.dataLength);
|
||||||
|
ARTERY_FONT_ENCODE_REALIGN();
|
||||||
|
}
|
||||||
|
// Write appendices
|
||||||
|
for (int i = 0; i < appendixCount; ++i) {
|
||||||
|
const FontAppendix<BYTE_ARRAY, STRING> &appendix = font.appendices[i];
|
||||||
|
internal::AppendixHeader header;
|
||||||
|
header.metadataLength = appendix.metadata.length();
|
||||||
|
header.dataLength = appendix.data.length();
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&header, sizeof(header));
|
||||||
|
ARTERY_FONT_ENCODE_WRITE_STRING(appendix.metadata);
|
||||||
|
ARTERY_FONT_ENCODE_WRITE((const unsigned char *) appendix.data, header.dataLength);
|
||||||
|
ARTERY_FONT_ENCODE_REALIGN();
|
||||||
|
}
|
||||||
|
// Write footer
|
||||||
|
{
|
||||||
|
internal::ArteryFontFooter footer;
|
||||||
|
footer.salt = 0;
|
||||||
|
footer.magicNo = ARTERY_FONT_FOOTER_MAGIC_NO;
|
||||||
|
memset(footer.reserved, 0, sizeof(footer.reserved));
|
||||||
|
footer.totalLength = totalLength+sizeof(footer);
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&footer, sizeof(footer)-sizeof(footer.checksum));
|
||||||
|
footer.checksum = checksum;
|
||||||
|
ARTERY_FONT_ENCODE_WRITE(&footer.checksum, sizeof(footer.checksum));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#undef ARTERY_FONT_ENCODE_WRITE
|
||||||
|
#undef ARTERY_FONT_ENCODE_REALIGN
|
||||||
|
#undef ARTERY_FONT_ENCODE_WRITE_STRING
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef ARTERY_FONT_HEADER_TAG
|
||||||
|
#undef ARTERY_FONT_HEADER_VERSION
|
||||||
|
#undef ARTERY_FONT_HEADER_MAGIC_NO
|
||||||
|
#undef ARTERY_FONT_FOOTER_MAGIC_NO
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "artery-font.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class StdList : private std::vector<T> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline StdList() { }
|
||||||
|
inline explicit StdList(int length) : std::vector<T>((size_t) length) { }
|
||||||
|
inline int length() const { return (int) std::vector<T>::size(); }
|
||||||
|
inline explicit operator T *() { return std::vector<T>::data(); }
|
||||||
|
inline explicit operator const T *() const { return std::vector<T>::data(); }
|
||||||
|
inline T &operator[](int index) { return std::vector<T>::operator[](index); }
|
||||||
|
inline const T &operator[](int index) const { return std::vector<T>::operator[](index); }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class StdString : private std::string {
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline StdString() { }
|
||||||
|
inline StdString(const char *characters, int length) : std::string(characters, (size_t) length) { }
|
||||||
|
inline int length() const { return (int) std::string::size(); }
|
||||||
|
inline explicit operator const char *() const { return std::string::c_str(); }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef StdList<unsigned char> StdByteArray;
|
||||||
|
|
||||||
|
template <typename REAL>
|
||||||
|
using StdArteryFont = ArteryFont<REAL, StdList, StdByteArray, StdString>;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include "serialization.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool read(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, FILE *file);
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool write(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, FILE *file);
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool readFile(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, const char *filename);
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool writeFile(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, const char *filename);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "stdio-serialization.hpp"
|
|
@ -0,0 +1,48 @@
|
||||||
|
|
||||||
|
#include "stdio-serialization.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
inline int fileRead(void *buffer, int length, void *file) {
|
||||||
|
return fread(buffer, 1, length, reinterpret_cast<FILE *>(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int fileWrite(const void *buffer, int length, void *file) {
|
||||||
|
return fwrite(buffer, 1, length, reinterpret_cast<FILE *>(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool read(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, FILE *file) {
|
||||||
|
return decode<internal::fileRead>(font, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool write(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, FILE *file) {
|
||||||
|
return encode<internal::fileWrite>(font, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool readFile(ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, const char *filename) {
|
||||||
|
FILE *file = fopen(filename, "rb");
|
||||||
|
if (!file)
|
||||||
|
return false;
|
||||||
|
bool result = read(font, file);
|
||||||
|
fclose(file);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
bool writeFile(const ArteryFont<REAL, LIST, BYTE_ARRAY, STRING> &font, const char *filename) {
|
||||||
|
FILE *file = fopen(filename, "wb");
|
||||||
|
if (!file)
|
||||||
|
return false;
|
||||||
|
bool result = write(font, file);
|
||||||
|
fclose(file);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "enums.h"
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
template <typename REAL>
|
||||||
|
struct Glyph {
|
||||||
|
uint32 codepoint;
|
||||||
|
uint32 image;
|
||||||
|
struct {
|
||||||
|
REAL l, b, r, t;
|
||||||
|
} planeBounds, imageBounds;
|
||||||
|
struct {
|
||||||
|
REAL h, v;
|
||||||
|
} advance;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename REAL>
|
||||||
|
struct KernPair {
|
||||||
|
uint32 codepoint1, codepoint2;
|
||||||
|
struct {
|
||||||
|
REAL h, v;
|
||||||
|
} advance;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class STRING>
|
||||||
|
struct FontVariant {
|
||||||
|
uint32 flags;
|
||||||
|
uint32 weight;
|
||||||
|
CodepointType codepointType;
|
||||||
|
ImageType imageType;
|
||||||
|
uint32 fallbackVariant;
|
||||||
|
uint32 fallbackGlyph;
|
||||||
|
struct Metrics {
|
||||||
|
// In pixels:
|
||||||
|
REAL fontSize;
|
||||||
|
REAL distanceRange;
|
||||||
|
// Proportional to font size:
|
||||||
|
REAL emSize;
|
||||||
|
REAL ascender, descender;
|
||||||
|
REAL lineHeight;
|
||||||
|
REAL underlineY, underlineThickness;
|
||||||
|
REAL reserved[24];
|
||||||
|
} metrics;
|
||||||
|
STRING name;
|
||||||
|
STRING metadata;
|
||||||
|
LIST<Glyph<REAL> > glyphs;
|
||||||
|
LIST<KernPair<REAL> > kernPairs;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class BYTE_ARRAY, class STRING>
|
||||||
|
struct FontImage {
|
||||||
|
uint32 flags;
|
||||||
|
ImageEncoding encoding;
|
||||||
|
uint32 width, height;
|
||||||
|
uint32 channels;
|
||||||
|
PixelFormat pixelFormat;
|
||||||
|
ImageType imageType;
|
||||||
|
struct {
|
||||||
|
uint32 rowLength;
|
||||||
|
ImageOrientation orientation;
|
||||||
|
} rawBinaryFormat;
|
||||||
|
uint32 childImages;
|
||||||
|
uint32 textureFlags;
|
||||||
|
STRING metadata;
|
||||||
|
BYTE_ARRAY data;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class BYTE_ARRAY, class STRING>
|
||||||
|
struct FontAppendix {
|
||||||
|
STRING metadata;
|
||||||
|
BYTE_ARRAY data;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename REAL, template <typename> class LIST, class BYTE_ARRAY, class STRING>
|
||||||
|
struct ArteryFont {
|
||||||
|
typedef FontVariant<REAL, LIST, STRING> Variant;
|
||||||
|
typedef FontImage<BYTE_ARRAY, STRING> Image;
|
||||||
|
typedef FontAppendix<BYTE_ARRAY, STRING> Appendix;
|
||||||
|
MetadataFormat metadataFormat;
|
||||||
|
STRING metadata;
|
||||||
|
LIST<Variant> variants;
|
||||||
|
LIST<Image> images;
|
||||||
|
LIST<Appendix> appendices;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace artery_font {
|
||||||
|
|
||||||
|
typedef unsigned char byte;
|
||||||
|
typedef int32_t sint32;
|
||||||
|
typedef uint32_t uint32;
|
||||||
|
|
||||||
|
}
|
BIN
libs/msdf-atlas-gen/include/artery-font-format/example.arfont
Normal file
BIN
libs/msdf-atlas-gen/include/artery-font-format/example.arfont
Normal file
Binary file not shown.
42
libs/msdf-atlas-gen/include/msdf-atlas-gen/AtlasGenerator.h
Normal file
42
libs/msdf-atlas-gen/include/msdf-atlas-gen/AtlasGenerator.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "Remap.h"
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
/** Prototype of an atlas generator class.
|
||||||
|
* An atlas generator maintains the atlas bitmap (AtlasStorage) and its layout and facilitates
|
||||||
|
* generation of bitmap representation of glyphs. The layout of the atlas is given by the caller.
|
||||||
|
*/
|
||||||
|
class AtlasGenerator {
|
||||||
|
|
||||||
|
public:
|
||||||
|
AtlasGenerator();
|
||||||
|
AtlasGenerator(int width, int height);
|
||||||
|
/// Generates bitmap representation for the supplied array of glyphs
|
||||||
|
void generate(const GlyphGeometry *glyphs, int count);
|
||||||
|
/// Resizes the atlas and rearranges the generated pixels according to the remapping array
|
||||||
|
void rearrange(int width, int height, const Remap *remapping, int count);
|
||||||
|
/// Resizes the atlas and keeps the generated pixels in place
|
||||||
|
void resize(int width, int height);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configuration of signed distance field generator
|
||||||
|
struct GeneratorAttributes {
|
||||||
|
msdfgen::MSDFGeneratorConfig config;
|
||||||
|
bool scanlinePass = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A function that generates the bitmap for a single glyph
|
||||||
|
template <typename T, int N>
|
||||||
|
using GeneratorFunction = void (*)(const msdfgen::BitmapRef<T, N> &, const GlyphGeometry &, const GeneratorAttributes &);
|
||||||
|
|
||||||
|
}
|
37
libs/msdf-atlas-gen/include/msdf-atlas-gen/AtlasStorage.h
Normal file
37
libs/msdf-atlas-gen/include/msdf-atlas-gen/AtlasStorage.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "Remap.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
/** Prototype of an atlas storage class.
|
||||||
|
* An atlas storage physically holds the pixels of the atlas
|
||||||
|
* and allows to read and write subsections represented as bitmaps.
|
||||||
|
* Can be implemented using a simple bitmap (BitmapAtlasStorage),
|
||||||
|
* as texture memory, or any other way.
|
||||||
|
*/
|
||||||
|
class AtlasStorage {
|
||||||
|
|
||||||
|
public:
|
||||||
|
AtlasStorage();
|
||||||
|
AtlasStorage(int width, int height);
|
||||||
|
/// Creates a copy with different dimensions
|
||||||
|
AtlasStorage(const AtlasStorage &orig, int width, int height);
|
||||||
|
/// Creates a copy with different dimensions and rearranges the pixels according to the remapping array
|
||||||
|
AtlasStorage(const AtlasStorage &orig, int width, int height, const Remap *remapping, int count);
|
||||||
|
/// Stores a subsection at x, y into the atlas storage. May be implemented for only some T, N
|
||||||
|
template <typename T, int N>
|
||||||
|
void put(int x, int y, const msdfgen::BitmapConstRef<T, N> &subBitmap);
|
||||||
|
/// Retrieves a subsection at x, y from the atlas storage. May be implemented for only some T, N
|
||||||
|
template <typename T, int N>
|
||||||
|
void get(int x, int y, const msdfgen::BitmapRef<T, N> &subBitmap) const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AtlasStorage.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// An implementation of AtlasStorage represented by a bitmap in memory (msdfgen::Bitmap)
|
||||||
|
template <typename T, int N>
|
||||||
|
class BitmapAtlasStorage {
|
||||||
|
|
||||||
|
public:
|
||||||
|
BitmapAtlasStorage();
|
||||||
|
BitmapAtlasStorage(int width, int height);
|
||||||
|
explicit BitmapAtlasStorage(const msdfgen::BitmapConstRef<T, N> &bitmap);
|
||||||
|
explicit BitmapAtlasStorage(msdfgen::Bitmap<T, N> &&bitmap);
|
||||||
|
BitmapAtlasStorage(const BitmapAtlasStorage<T, N> &orig, int width, int height);
|
||||||
|
BitmapAtlasStorage(const BitmapAtlasStorage<T, N> &orig, int width, int height, const Remap *remapping, int count);
|
||||||
|
operator msdfgen::BitmapConstRef<T, N>() const;
|
||||||
|
operator msdfgen::BitmapRef<T, N>();
|
||||||
|
operator msdfgen::Bitmap<T, N>() &&;
|
||||||
|
template <typename S>
|
||||||
|
void put(int x, int y, const msdfgen::BitmapConstRef<S, N> &subBitmap);
|
||||||
|
void get(int x, int y, const msdfgen::BitmapRef<T, N> &subBitmap) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
msdfgen::Bitmap<T, N> bitmap;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "BitmapAtlasStorage.hpp"
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
#include "BitmapAtlasStorage.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "bitmap-blit.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage() { }
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage(int width, int height) : bitmap(width, height) {
|
||||||
|
memset((T *) bitmap, 0, sizeof(T)*N*width*height);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage(const msdfgen::BitmapConstRef<T, N> &bitmap) : bitmap(bitmap) { }
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage(msdfgen::Bitmap<T, N> &&bitmap) : bitmap((msdfgen::Bitmap<T, N> &&) bitmap) { }
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage(const BitmapAtlasStorage<T, N> &orig, int width, int height) : bitmap(width, height) {
|
||||||
|
memset((T *) bitmap, 0, sizeof(T)*N*width*height);
|
||||||
|
blit(bitmap, orig.bitmap, 0, 0, 0, 0, std::min(width, orig.bitmap.width()), std::min(height, orig.bitmap.height()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::BitmapAtlasStorage(const BitmapAtlasStorage<T, N> &orig, int width, int height, const Remap *remapping, int count) : bitmap(width, height) {
|
||||||
|
memset((T *) bitmap, 0, sizeof(T)*N*width*height);
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
const Remap &remap = remapping[i];
|
||||||
|
blit(bitmap, orig.bitmap, remap.target.x, remap.target.y, remap.source.x, remap.source.y, remap.width, remap.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::operator msdfgen::BitmapConstRef<T, N>() const {
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::operator msdfgen::BitmapRef<T, N>() {
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
BitmapAtlasStorage<T, N>::operator msdfgen::Bitmap<T, N>() && {
|
||||||
|
return (msdfgen::Bitmap<T, N> &&) bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
template <typename S>
|
||||||
|
void BitmapAtlasStorage<T, N>::put(int x, int y, const msdfgen::BitmapConstRef<S, N> &subBitmap) {
|
||||||
|
blit(bitmap, subBitmap, x, y, 0, 0, subBitmap.width, subBitmap.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
void BitmapAtlasStorage<T, N>::get(int x, int y, const msdfgen::BitmapRef<T, N> &subBitmap) const {
|
||||||
|
blit(subBitmap, bitmap, 0, 0, x, y, subBitmap.width, subBitmap.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
libs/msdf-atlas-gen/include/msdf-atlas-gen/Charset.cpp
Normal file
39
libs/msdf-atlas-gen/include/msdf-atlas-gen/Charset.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
#include "Charset.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static Charset createAsciiCharset() {
|
||||||
|
Charset ascii;
|
||||||
|
for (unicode_t cp = 0x20; cp < 0x7f; ++cp)
|
||||||
|
ascii.add(cp);
|
||||||
|
return ascii;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Charset Charset::ASCII = createAsciiCharset();
|
||||||
|
|
||||||
|
void Charset::add(unicode_t cp) {
|
||||||
|
codepoints.insert(cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Charset::remove(unicode_t cp) {
|
||||||
|
codepoints.erase(cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Charset::size() const {
|
||||||
|
return codepoints.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Charset::empty() const {
|
||||||
|
return codepoints.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::set<unicode_t>::const_iterator Charset::begin() const {
|
||||||
|
return codepoints.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::set<unicode_t>::const_iterator Charset::end() const {
|
||||||
|
return codepoints.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
libs/msdf-atlas-gen/include/msdf-atlas-gen/Charset.h
Normal file
35
libs/msdf-atlas-gen/include/msdf-atlas-gen/Charset.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <set>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Represents a set of Unicode codepoints (characters)
|
||||||
|
class Charset {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// The set of the 95 printable ASCII characters
|
||||||
|
static const Charset ASCII;
|
||||||
|
|
||||||
|
/// Adds a codepoint
|
||||||
|
void add(unicode_t cp);
|
||||||
|
/// Removes a codepoint
|
||||||
|
void remove(unicode_t cp);
|
||||||
|
|
||||||
|
size_t size() const;
|
||||||
|
bool empty() const;
|
||||||
|
std::set<unicode_t>::const_iterator begin() const;
|
||||||
|
std::set<unicode_t>::const_iterator end() const;
|
||||||
|
|
||||||
|
/// Load character set from a text file with the correct syntax
|
||||||
|
bool load(const char *filename, bool disableCharLiterals = false);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::set<unicode_t> codepoints;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
49
libs/msdf-atlas-gen/include/msdf-atlas-gen/DynamicAtlas.h
Normal file
49
libs/msdf-atlas-gen/include/msdf-atlas-gen/DynamicAtlas.h
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "RectanglePacker.h"
|
||||||
|
#include "AtlasGenerator.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class can be used to produce a dynamic atlas to which more glyphs are added over time.
|
||||||
|
* It takes care of laying out and enlarging the atlas as necessary and delegates the actual work
|
||||||
|
* to the specified AtlasGenerator, which may e.g. do the work asynchronously.
|
||||||
|
*/
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
class DynamicAtlas {
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum ChangeFlag {
|
||||||
|
NO_CHANGE = 0x00,
|
||||||
|
RESIZED = 0x01,
|
||||||
|
REARRANGED = 0x02
|
||||||
|
};
|
||||||
|
typedef int ChangeFlags;
|
||||||
|
|
||||||
|
DynamicAtlas();
|
||||||
|
/// Creates with a configured generator. The generator must not contain any prior glyphs!
|
||||||
|
explicit DynamicAtlas(AtlasGenerator &&generator);
|
||||||
|
/// Adds a batch of glyphs. Adding more than one glyph at a time may improve packing efficiency
|
||||||
|
ChangeFlags add(GlyphGeometry *glyphs, int count, bool allowRearrange = false);
|
||||||
|
/// Allows access to generator. Do not add glyphs to the generator directly!
|
||||||
|
AtlasGenerator & atlasGenerator();
|
||||||
|
const AtlasGenerator & atlasGenerator() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
AtlasGenerator generator;
|
||||||
|
RectanglePacker packer;
|
||||||
|
int glyphCount;
|
||||||
|
int side;
|
||||||
|
std::vector<Rectangle> rectangles;
|
||||||
|
std::vector<Remap> remapBuffer;
|
||||||
|
int totalArea;
|
||||||
|
int padding;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "DynamicAtlas.hpp"
|
78
libs/msdf-atlas-gen/include/msdf-atlas-gen/DynamicAtlas.hpp
Normal file
78
libs/msdf-atlas-gen/include/msdf-atlas-gen/DynamicAtlas.hpp
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
|
||||||
|
#include "DynamicAtlas.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
DynamicAtlas<AtlasGenerator>::DynamicAtlas() : glyphCount(0), side(0), totalArea(0), padding(0) { }
|
||||||
|
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
DynamicAtlas<AtlasGenerator>::DynamicAtlas(AtlasGenerator &&generator) : generator((AtlasGenerator &&) generator), glyphCount(0), side(0), totalArea(0), padding(0) { }
|
||||||
|
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
typename DynamicAtlas<AtlasGenerator>::ChangeFlags DynamicAtlas<AtlasGenerator>::add(GlyphGeometry *glyphs, int count, bool allowRearrange) {
|
||||||
|
ChangeFlags changeFlags = 0;
|
||||||
|
int start = rectangles.size();
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
if (!glyphs[i].isWhitespace()) {
|
||||||
|
int w, h;
|
||||||
|
glyphs[i].getBoxSize(w, h);
|
||||||
|
Rectangle rect = { 0, 0, w+padding, h+padding };
|
||||||
|
rectangles.push_back(rect);
|
||||||
|
Remap remapEntry = { };
|
||||||
|
remapEntry.index = glyphCount+i;
|
||||||
|
remapEntry.width = w;
|
||||||
|
remapEntry.height = h;
|
||||||
|
remapBuffer.push_back(remapEntry);
|
||||||
|
totalArea += (w+padding)*(h+padding);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((int) rectangles.size() > start) {
|
||||||
|
int packerStart = start;
|
||||||
|
int remaining;
|
||||||
|
while ((remaining = packer.pack(rectangles.data()+packerStart, rectangles.size()-packerStart)) > 0) {
|
||||||
|
side = (side+!side)<<1;
|
||||||
|
while (side*side < totalArea)
|
||||||
|
side <<= 1;
|
||||||
|
if (allowRearrange) {
|
||||||
|
packer = RectanglePacker(side+padding, side+padding);
|
||||||
|
packerStart = 0;
|
||||||
|
} else {
|
||||||
|
packer.expand(side+padding, side+padding);
|
||||||
|
packerStart = rectangles.size()-remaining;
|
||||||
|
}
|
||||||
|
changeFlags |= RESIZED;
|
||||||
|
}
|
||||||
|
if (packerStart < start) {
|
||||||
|
for (int i = packerStart; i < start; ++i) {
|
||||||
|
Remap &remap = remapBuffer[i];
|
||||||
|
remap.source = remap.target;
|
||||||
|
remap.target.x = rectangles[i].x;
|
||||||
|
remap.target.y = rectangles[i].y;
|
||||||
|
}
|
||||||
|
generator.rearrange(side, side, remapBuffer.data(), start);
|
||||||
|
changeFlags |= REARRANGED;
|
||||||
|
} else if (changeFlags&RESIZED)
|
||||||
|
generator.resize(side, side);
|
||||||
|
for (int i = start; i < (int) rectangles.size(); ++i) {
|
||||||
|
remapBuffer[i].target.x = rectangles[i].x;
|
||||||
|
remapBuffer[i].target.y = rectangles[i].y;
|
||||||
|
glyphs[remapBuffer[i].index-glyphCount].placeBox(rectangles[i].x, rectangles[i].y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
generator.generate(glyphs, count);
|
||||||
|
glyphCount += count;
|
||||||
|
return changeFlags;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
AtlasGenerator & DynamicAtlas<AtlasGenerator>::atlasGenerator() {
|
||||||
|
return generator;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class AtlasGenerator>
|
||||||
|
const AtlasGenerator & DynamicAtlas<AtlasGenerator>::atlasGenerator() const {
|
||||||
|
return generator;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
185
libs/msdf-atlas-gen/include/msdf-atlas-gen/FontGeometry.cpp
Normal file
185
libs/msdf-atlas-gen/include/msdf-atlas-gen/FontGeometry.cpp
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
FontGeometry::GlyphRange::GlyphRange() : glyphs(), rangeStart(), rangeEnd() { }
|
||||||
|
|
||||||
|
FontGeometry::GlyphRange::GlyphRange(const std::vector<GlyphGeometry> *glyphs, size_t rangeStart, size_t rangeEnd) : glyphs(glyphs), rangeStart(rangeStart), rangeEnd(rangeEnd) { }
|
||||||
|
|
||||||
|
size_t FontGeometry::GlyphRange::size() const {
|
||||||
|
return glyphs->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::GlyphRange::empty() const {
|
||||||
|
return glyphs->empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
const GlyphGeometry * FontGeometry::GlyphRange::begin() const {
|
||||||
|
return glyphs->data()+rangeStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GlyphGeometry * FontGeometry::GlyphRange::end() const {
|
||||||
|
return glyphs->data()+rangeEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
FontGeometry::FontGeometry() : geometryScale(1), metrics(), preferredIdentifierType(GlyphIdentifierType::UNICODE_CODEPOINT), glyphs(&ownGlyphs), rangeStart(glyphs->size()), rangeEnd(glyphs->size()) { }
|
||||||
|
|
||||||
|
FontGeometry::FontGeometry(std::vector<GlyphGeometry> *glyphStorage) : geometryScale(1), metrics(), preferredIdentifierType(GlyphIdentifierType::UNICODE_CODEPOINT), glyphs(glyphStorage), rangeStart(glyphs->size()), rangeEnd(glyphs->size()) { }
|
||||||
|
|
||||||
|
int FontGeometry::loadGlyphset(msdfgen::FontHandle *font, double fontScale, const Charset &glyphset, bool preprocessGeometry, bool enableKerning) {
|
||||||
|
if (!(glyphs->size() == rangeEnd && loadMetrics(font, fontScale)))
|
||||||
|
return -1;
|
||||||
|
glyphs->reserve(glyphs->size()+glyphset.size());
|
||||||
|
int loaded = 0;
|
||||||
|
for (unicode_t index : glyphset) {
|
||||||
|
GlyphGeometry glyph;
|
||||||
|
if (glyph.load(font, geometryScale, msdfgen::GlyphIndex(index), preprocessGeometry)) {
|
||||||
|
addGlyph((GlyphGeometry &&) glyph);
|
||||||
|
++loaded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (enableKerning)
|
||||||
|
loadKerning(font);
|
||||||
|
preferredIdentifierType = GlyphIdentifierType::GLYPH_INDEX;
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
int FontGeometry::loadCharset(msdfgen::FontHandle *font, double fontScale, const Charset &charset, bool preprocessGeometry, bool enableKerning) {
|
||||||
|
if (!(glyphs->size() == rangeEnd && loadMetrics(font, fontScale)))
|
||||||
|
return -1;
|
||||||
|
glyphs->reserve(glyphs->size()+charset.size());
|
||||||
|
int loaded = 0;
|
||||||
|
for (unicode_t cp : charset) {
|
||||||
|
GlyphGeometry glyph;
|
||||||
|
if (glyph.load(font, geometryScale, cp, preprocessGeometry)) {
|
||||||
|
addGlyph((GlyphGeometry &&) glyph);
|
||||||
|
++loaded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (enableKerning)
|
||||||
|
loadKerning(font);
|
||||||
|
preferredIdentifierType = GlyphIdentifierType::UNICODE_CODEPOINT;
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::loadMetrics(msdfgen::FontHandle *font, double fontScale) {
|
||||||
|
if (!msdfgen::getFontMetrics(metrics, font))
|
||||||
|
return false;
|
||||||
|
if (metrics.emSize <= 0)
|
||||||
|
metrics.emSize = MSDF_ATLAS_DEFAULT_EM_SIZE;
|
||||||
|
geometryScale = fontScale/metrics.emSize;
|
||||||
|
metrics.emSize *= geometryScale;
|
||||||
|
metrics.ascenderY *= geometryScale;
|
||||||
|
metrics.descenderY *= geometryScale;
|
||||||
|
metrics.lineHeight *= geometryScale;
|
||||||
|
metrics.underlineY *= geometryScale;
|
||||||
|
metrics.underlineThickness *= geometryScale;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::addGlyph(const GlyphGeometry &glyph) {
|
||||||
|
if (glyphs->size() != rangeEnd)
|
||||||
|
return false;
|
||||||
|
glyphsByIndex.insert(std::make_pair(glyph.getIndex(), rangeEnd));
|
||||||
|
if (glyph.getCodepoint())
|
||||||
|
glyphsByCodepoint.insert(std::make_pair(glyph.getCodepoint(), rangeEnd));
|
||||||
|
glyphs->push_back(glyph);
|
||||||
|
++rangeEnd;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::addGlyph(GlyphGeometry &&glyph) {
|
||||||
|
if (glyphs->size() != rangeEnd)
|
||||||
|
return false;
|
||||||
|
glyphsByIndex.insert(std::make_pair(glyph.getIndex(), rangeEnd));
|
||||||
|
if (glyph.getCodepoint())
|
||||||
|
glyphsByCodepoint.insert(std::make_pair(glyph.getCodepoint(), rangeEnd));
|
||||||
|
glyphs->push_back((GlyphGeometry &&) glyph);
|
||||||
|
++rangeEnd;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int FontGeometry::loadKerning(msdfgen::FontHandle *font) {
|
||||||
|
int loaded = 0;
|
||||||
|
for (size_t i = rangeStart; i < rangeEnd; ++i)
|
||||||
|
for (size_t j = rangeStart; j < rangeEnd; ++j) {
|
||||||
|
double advance;
|
||||||
|
if (msdfgen::getKerning(advance, font, (*glyphs)[i].getGlyphIndex(), (*glyphs)[j].getGlyphIndex()) && advance) {
|
||||||
|
kerning[std::make_pair<int, int>((*glyphs)[i].getIndex(), (*glyphs)[j].getIndex())] = geometryScale*advance;
|
||||||
|
++loaded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontGeometry::setName(const char *name) {
|
||||||
|
if (name)
|
||||||
|
this->name = name;
|
||||||
|
else
|
||||||
|
this->name.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
double FontGeometry::getGeometryScale() const {
|
||||||
|
return geometryScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
const msdfgen::FontMetrics & FontGeometry::getMetrics() const {
|
||||||
|
return metrics;
|
||||||
|
}
|
||||||
|
|
||||||
|
GlyphIdentifierType FontGeometry::getPreferredIdentifierType() const {
|
||||||
|
return preferredIdentifierType;
|
||||||
|
}
|
||||||
|
|
||||||
|
FontGeometry::GlyphRange FontGeometry::getGlyphs() const {
|
||||||
|
return GlyphRange(glyphs, rangeStart, rangeEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
const GlyphGeometry * FontGeometry::getGlyph(msdfgen::GlyphIndex index) const {
|
||||||
|
std::map<int, size_t>::const_iterator it = glyphsByIndex.find(index.getIndex());
|
||||||
|
if (it != glyphsByIndex.end())
|
||||||
|
return &(*glyphs)[it->second];
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GlyphGeometry * FontGeometry::getGlyph(unicode_t codepoint) const {
|
||||||
|
std::map<unicode_t, size_t>::const_iterator it = glyphsByCodepoint.find(codepoint);
|
||||||
|
if (it != glyphsByCodepoint.end())
|
||||||
|
return &(*glyphs)[it->second];
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::getAdvance(double &advance, msdfgen::GlyphIndex index1, msdfgen::GlyphIndex index2) const {
|
||||||
|
const GlyphGeometry *glyph1 = getGlyph(index1);
|
||||||
|
if (!glyph1)
|
||||||
|
return false;
|
||||||
|
advance = glyph1->getAdvance();
|
||||||
|
std::map<std::pair<int, int>, double>::const_iterator it = kerning.find(std::make_pair<int, int>(index1.getIndex(), index2.getIndex()));
|
||||||
|
if (it != kerning.end())
|
||||||
|
advance += it->second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontGeometry::getAdvance(double &advance, unicode_t codepoint1, unicode_t codepoint2) const {
|
||||||
|
const GlyphGeometry *glyph1, *glyph2;
|
||||||
|
if (!((glyph1 = getGlyph(codepoint1)) && (glyph2 = getGlyph(codepoint2))))
|
||||||
|
return false;
|
||||||
|
advance = glyph1->getAdvance();
|
||||||
|
std::map<std::pair<int, int>, double>::const_iterator it = kerning.find(std::make_pair<int, int>(glyph1->getIndex(), glyph2->getIndex()));
|
||||||
|
if (it != kerning.end())
|
||||||
|
advance += it->second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::map<std::pair<int, int>, double> & FontGeometry::getKerning() const {
|
||||||
|
return kerning;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char * FontGeometry::getName() const {
|
||||||
|
if (name.empty())
|
||||||
|
return nullptr;
|
||||||
|
return name.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
86
libs/msdf-atlas-gen/include/msdf-atlas-gen/FontGeometry.h
Normal file
86
libs/msdf-atlas-gen/include/msdf-atlas-gen/FontGeometry.h
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
#include "Charset.h"
|
||||||
|
|
||||||
|
#define MSDF_ATLAS_DEFAULT_EM_SIZE 32.0
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Represents the geometry of all glyphs of a given font or font variant
|
||||||
|
class FontGeometry {
|
||||||
|
|
||||||
|
public:
|
||||||
|
class GlyphRange {
|
||||||
|
public:
|
||||||
|
GlyphRange();
|
||||||
|
GlyphRange(const std::vector<GlyphGeometry> *glyphs, size_t rangeStart, size_t rangeEnd);
|
||||||
|
size_t size() const;
|
||||||
|
bool empty() const;
|
||||||
|
const GlyphGeometry * begin() const;
|
||||||
|
const GlyphGeometry * end() const;
|
||||||
|
private:
|
||||||
|
const std::vector<GlyphGeometry> *glyphs;
|
||||||
|
size_t rangeStart, rangeEnd;
|
||||||
|
};
|
||||||
|
|
||||||
|
FontGeometry();
|
||||||
|
explicit FontGeometry(std::vector<GlyphGeometry> *glyphStorage);
|
||||||
|
|
||||||
|
/// Loads all glyphs in a glyphset (Charset elements are glyph indices), returns the number of successfully loaded glyphs
|
||||||
|
int loadGlyphset(msdfgen::FontHandle *font, double fontScale, const Charset &glyphset, bool preprocessGeometry = true, bool enableKerning = true);
|
||||||
|
/// Loads all glyphs in a charset (Charset elements are Unicode codepoints), returns the number of successfully loaded glyphs
|
||||||
|
int loadCharset(msdfgen::FontHandle *font, double fontScale, const Charset &charset, bool preprocessGeometry = true, bool enableKerning = true);
|
||||||
|
|
||||||
|
/// Only loads font metrics and geometry scale from font
|
||||||
|
bool loadMetrics(msdfgen::FontHandle *font, double fontScale);
|
||||||
|
/// Adds a loaded glyph
|
||||||
|
bool addGlyph(const GlyphGeometry &glyph);
|
||||||
|
bool addGlyph(GlyphGeometry &&glyph);
|
||||||
|
/// Loads kerning pairs for all glyphs that are currently present, returns the number of loaded kerning pairs
|
||||||
|
int loadKerning(msdfgen::FontHandle *font);
|
||||||
|
/// Sets a name to be associated with the font
|
||||||
|
void setName(const char *name);
|
||||||
|
|
||||||
|
/// Returns the geometry scale to be used when loading glyphs
|
||||||
|
double getGeometryScale() const;
|
||||||
|
/// Returns the processed font metrics
|
||||||
|
const msdfgen::FontMetrics & getMetrics() const;
|
||||||
|
/// Returns the type of identifier that was used to load glyphs
|
||||||
|
GlyphIdentifierType getPreferredIdentifierType() const;
|
||||||
|
/// Returns the list of all glyphs
|
||||||
|
GlyphRange getGlyphs() const;
|
||||||
|
/// Finds a glyph by glyph index or Unicode codepoint, returns null if not found
|
||||||
|
const GlyphGeometry * getGlyph(msdfgen::GlyphIndex index) const;
|
||||||
|
const GlyphGeometry * getGlyph(unicode_t codepoint) const;
|
||||||
|
/// Outputs the advance between two glyphs with kerning taken into consideration, returns false on failure
|
||||||
|
bool getAdvance(double &advance, msdfgen::GlyphIndex index1, msdfgen::GlyphIndex index2) const;
|
||||||
|
bool getAdvance(double &advance, unicode_t codepoint1, unicode_t codepoint2) const;
|
||||||
|
/// Returns the complete mapping of kerning pairs (by glyph indices) and their respective advance values
|
||||||
|
const std::map<std::pair<int, int>, double> & getKerning() const;
|
||||||
|
/// Returns the name associated with the font or null if not set
|
||||||
|
const char * getName() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
double geometryScale;
|
||||||
|
msdfgen::FontMetrics metrics;
|
||||||
|
GlyphIdentifierType preferredIdentifierType;
|
||||||
|
std::vector<GlyphGeometry> *glyphs;
|
||||||
|
size_t rangeStart, rangeEnd;
|
||||||
|
std::map<int, size_t> glyphsByIndex;
|
||||||
|
std::map<unicode_t, size_t> glyphsByCodepoint;
|
||||||
|
std::map<std::pair<int, int>, double> kerning;
|
||||||
|
std::vector<GlyphGeometry> ownGlyphs;
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
19
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphBox.h
Normal file
19
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphBox.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Rectangle.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// The glyph box - its bounds in plane and atlas
|
||||||
|
struct GlyphBox {
|
||||||
|
int index;
|
||||||
|
double advance;
|
||||||
|
struct {
|
||||||
|
double l, b, r, t;
|
||||||
|
} bounds;
|
||||||
|
Rectangle rect;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
178
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphGeometry.cpp
Normal file
178
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphGeometry.cpp
Normal file
|
@ -0,0 +1,178 @@
|
||||||
|
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <core/ShapeDistanceFinder.h>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
GlyphGeometry::GlyphGeometry() : index(), codepoint(), geometryScale(), bounds(), advance(), box() { }
|
||||||
|
|
||||||
|
bool GlyphGeometry::load(msdfgen::FontHandle *font, double geometryScale, msdfgen::GlyphIndex index, bool preprocessGeometry) {
|
||||||
|
if (font && msdfgen::loadGlyph(shape, font, index, &advance) && shape.validate()) {
|
||||||
|
this->index = index.getIndex();
|
||||||
|
this->geometryScale = geometryScale;
|
||||||
|
codepoint = 0;
|
||||||
|
advance *= geometryScale;
|
||||||
|
#ifdef MSDFGEN_USE_SKIA
|
||||||
|
if (preprocessGeometry)
|
||||||
|
msdfgen::resolveShapeGeometry(shape);
|
||||||
|
#endif
|
||||||
|
shape.normalize();
|
||||||
|
bounds = shape.getBounds();
|
||||||
|
#ifdef MSDFGEN_USE_SKIA
|
||||||
|
if (!preprocessGeometry)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
// Determine if shape is winded incorrectly and reverse it in that case
|
||||||
|
msdfgen::Point2 outerPoint(bounds.l-(bounds.r-bounds.l)-1, bounds.b-(bounds.t-bounds.b)-1);
|
||||||
|
if (msdfgen::SimpleTrueShapeDistanceFinder::oneShotDistance(shape, outerPoint) > 0) {
|
||||||
|
for (msdfgen::Contour &contour : shape.contours)
|
||||||
|
contour.reverse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GlyphGeometry::load(msdfgen::FontHandle *font, double geometryScale, unicode_t codepoint, bool preprocessGeometry) {
|
||||||
|
msdfgen::GlyphIndex index;
|
||||||
|
if (msdfgen::getGlyphIndex(index, font, codepoint)) {
|
||||||
|
if (load(font, geometryScale, index, preprocessGeometry)) {
|
||||||
|
this->codepoint = codepoint;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::edgeColoring(void (*fn)(msdfgen::Shape &, double, unsigned long long), double angleThreshold, unsigned long long seed) {
|
||||||
|
fn(shape, angleThreshold, seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::wrapBox(double scale, double range, double miterLimit) {
|
||||||
|
scale *= geometryScale;
|
||||||
|
range /= geometryScale;
|
||||||
|
box.range = range;
|
||||||
|
box.scale = scale;
|
||||||
|
if (bounds.l < bounds.r && bounds.b < bounds.t) {
|
||||||
|
double l = bounds.l, b = bounds.b, r = bounds.r, t = bounds.t;
|
||||||
|
l -= .5*range, b -= .5*range;
|
||||||
|
r += .5*range, t += .5*range;
|
||||||
|
if (miterLimit > 0)
|
||||||
|
shape.boundMiters(l, b, r, t, .5*range, miterLimit, 1);
|
||||||
|
double w = scale*(r-l);
|
||||||
|
double h = scale*(t-b);
|
||||||
|
box.rect.w = (int) ceil(w)+1;
|
||||||
|
box.rect.h = (int) ceil(h)+1;
|
||||||
|
box.translate.x = -l+.5*(box.rect.w-w)/scale;
|
||||||
|
box.translate.y = -b+.5*(box.rect.h-h)/scale;
|
||||||
|
} else {
|
||||||
|
box.rect.w = 0, box.rect.h = 0;
|
||||||
|
box.translate = msdfgen::Vector2();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::placeBox(int x, int y) {
|
||||||
|
box.rect.x = x, box.rect.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::setBoxRect(const Rectangle &rect) {
|
||||||
|
box.rect = rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GlyphGeometry::getIndex() const {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
msdfgen::GlyphIndex GlyphGeometry::getGlyphIndex() const {
|
||||||
|
return msdfgen::GlyphIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
unicode_t GlyphGeometry::getCodepoint() const {
|
||||||
|
return codepoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GlyphGeometry::getIdentifier(GlyphIdentifierType type) const {
|
||||||
|
switch (type) {
|
||||||
|
case GlyphIdentifierType::GLYPH_INDEX:
|
||||||
|
return index;
|
||||||
|
case GlyphIdentifierType::UNICODE_CODEPOINT:
|
||||||
|
return (int) codepoint;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const msdfgen::Shape & GlyphGeometry::getShape() const {
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
double GlyphGeometry::getAdvance() const {
|
||||||
|
return advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle GlyphGeometry::getBoxRect() const {
|
||||||
|
return box.rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::getBoxRect(int &x, int &y, int &w, int &h) const {
|
||||||
|
x = box.rect.x, y = box.rect.y;
|
||||||
|
w = box.rect.w, h = box.rect.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::getBoxSize(int &w, int &h) const {
|
||||||
|
w = box.rect.w, h = box.rect.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
double GlyphGeometry::getBoxRange() const {
|
||||||
|
return box.range;
|
||||||
|
}
|
||||||
|
|
||||||
|
msdfgen::Projection GlyphGeometry::getBoxProjection() const {
|
||||||
|
return msdfgen::Projection(msdfgen::Vector2(box.scale), box.translate);
|
||||||
|
}
|
||||||
|
|
||||||
|
double GlyphGeometry::getBoxScale() const {
|
||||||
|
return box.scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
msdfgen::Vector2 GlyphGeometry::getBoxTranslate() const {
|
||||||
|
return box.translate;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::getQuadPlaneBounds(double &l, double &b, double &r, double &t) const {
|
||||||
|
if (box.rect.w > 0 && box.rect.h > 0) {
|
||||||
|
double invBoxScale = 1/box.scale;
|
||||||
|
l = geometryScale*(-box.translate.x+.5*invBoxScale);
|
||||||
|
b = geometryScale*(-box.translate.y+.5*invBoxScale);
|
||||||
|
r = geometryScale*(-box.translate.x+(box.rect.w-.5)*invBoxScale);
|
||||||
|
t = geometryScale*(-box.translate.y+(box.rect.h-.5)*invBoxScale);
|
||||||
|
} else
|
||||||
|
l = 0, b = 0, r = 0, t = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlyphGeometry::getQuadAtlasBounds(double &l, double &b, double &r, double &t) const {
|
||||||
|
if (box.rect.w > 0 && box.rect.h > 0) {
|
||||||
|
l = box.rect.x+.5;
|
||||||
|
b = box.rect.y+.5;
|
||||||
|
r = box.rect.x+box.rect.w-.5;
|
||||||
|
t = box.rect.y+box.rect.h-.5;
|
||||||
|
} else
|
||||||
|
l = 0, b = 0, r = 0, t = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GlyphGeometry::isWhitespace() const {
|
||||||
|
return shape.contours.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
GlyphGeometry::operator GlyphBox() const {
|
||||||
|
GlyphBox box;
|
||||||
|
box.index = index;
|
||||||
|
box.advance = advance;
|
||||||
|
getQuadPlaneBounds(box.bounds.l, box.bounds.b, box.bounds.r, box.bounds.t);
|
||||||
|
box.rect.x = this->box.rect.x, box.rect.y = this->box.rect.y, box.rect.w = this->box.rect.w, box.rect.h = this->box.rect.h;
|
||||||
|
return box;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
79
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphGeometry.h
Normal file
79
libs/msdf-atlas-gen/include/msdf-atlas-gen/GlyphGeometry.h
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "Rectangle.h"
|
||||||
|
#include "GlyphBox.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Represents the shape geometry of a single glyph as well as its configuration
|
||||||
|
class GlyphGeometry {
|
||||||
|
|
||||||
|
public:
|
||||||
|
GlyphGeometry();
|
||||||
|
/// Loads glyph geometry from font
|
||||||
|
bool load(msdfgen::FontHandle *font, double geometryScale, msdfgen::GlyphIndex index, bool preprocessGeometry = true);
|
||||||
|
bool load(msdfgen::FontHandle *font, double geometryScale, unicode_t codepoint, bool preprocessGeometry = true);
|
||||||
|
/// Applies edge coloring to glyph shape
|
||||||
|
void edgeColoring(void (*fn)(msdfgen::Shape &, double, unsigned long long), double angleThreshold, unsigned long long seed);
|
||||||
|
/// Computes the dimensions of the glyph's box as well as the transformation for the generator function
|
||||||
|
void wrapBox(double scale, double range, double miterLimit);
|
||||||
|
/// Sets the glyph's box's position in the atlas
|
||||||
|
void placeBox(int x, int y);
|
||||||
|
/// Sets the glyph's box's rectangle in the atlas
|
||||||
|
void setBoxRect(const Rectangle &rect);
|
||||||
|
/// Returns the glyph's index within the font
|
||||||
|
int getIndex() const;
|
||||||
|
/// Returns the glyph's index as a msdfgen::GlyphIndex
|
||||||
|
msdfgen::GlyphIndex getGlyphIndex() const;
|
||||||
|
/// Returns the Unicode codepoint represented by the glyph or 0 if unknown
|
||||||
|
unicode_t getCodepoint() const;
|
||||||
|
/// Returns the glyph's identifier specified by the supplied identifier type
|
||||||
|
int getIdentifier(GlyphIdentifierType type) const;
|
||||||
|
/// Returns the glyph's shape
|
||||||
|
const msdfgen::Shape & getShape() const;
|
||||||
|
/// Returns the glyph's advance
|
||||||
|
double getAdvance() const;
|
||||||
|
/// Returns the glyph's box in the atlas
|
||||||
|
Rectangle getBoxRect() const;
|
||||||
|
/// Outputs the position and dimensions of the glyph's box in the atlas
|
||||||
|
void getBoxRect(int &x, int &y, int &w, int &h) const;
|
||||||
|
/// Outputs the dimensions of the glyph's box in the atlas
|
||||||
|
void getBoxSize(int &w, int &h) const;
|
||||||
|
/// Returns the range needed to generate the glyph's SDF
|
||||||
|
double getBoxRange() const;
|
||||||
|
/// Returns the projection needed to generate the glyph's bitmap
|
||||||
|
msdfgen::Projection getBoxProjection() const;
|
||||||
|
/// Returns the scale needed to generate the glyph's bitmap
|
||||||
|
double getBoxScale() const;
|
||||||
|
/// Returns the translation vector needed to generate the glyph's bitmap
|
||||||
|
msdfgen::Vector2 getBoxTranslate() const;
|
||||||
|
/// Outputs the bounding box of the glyph as it should be placed on the baseline
|
||||||
|
void getQuadPlaneBounds(double &l, double &b, double &r, double &t) const;
|
||||||
|
/// Outputs the bounding box of the glyph in the atlas
|
||||||
|
void getQuadAtlasBounds(double &l, double &b, double &r, double &t) const;
|
||||||
|
/// Returns true if the glyph is a whitespace and has no geometry
|
||||||
|
bool isWhitespace() const;
|
||||||
|
/// Simplifies to GlyphBox
|
||||||
|
operator GlyphBox() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int index;
|
||||||
|
unicode_t codepoint;
|
||||||
|
double geometryScale;
|
||||||
|
msdfgen::Shape shape;
|
||||||
|
msdfgen::Shape::Bounds bounds;
|
||||||
|
double advance;
|
||||||
|
struct {
|
||||||
|
Rectangle rect;
|
||||||
|
double range;
|
||||||
|
double scale;
|
||||||
|
msdfgen::Vector2 translate;
|
||||||
|
} box;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "GlyphBox.h"
|
||||||
|
#include "Workload.h"
|
||||||
|
#include "AtlasGenerator.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An implementation of AtlasGenerator that uses the specified generator function
|
||||||
|
* and AtlasStorage class and generates glyph bitmaps immediately
|
||||||
|
* (does not return until all submitted work is finished),
|
||||||
|
* but may use multiple threads (setThreadCount).
|
||||||
|
*/
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
class ImmediateAtlasGenerator {
|
||||||
|
|
||||||
|
public:
|
||||||
|
ImmediateAtlasGenerator();
|
||||||
|
ImmediateAtlasGenerator(int width, int height);
|
||||||
|
void generate(const GlyphGeometry *glyphs, int count);
|
||||||
|
void rearrange(int width, int height, const Remap *remapping, int count);
|
||||||
|
void resize(int width, int height);
|
||||||
|
/// Sets attributes for the generator function
|
||||||
|
void setAttributes(const GeneratorAttributes &attributes);
|
||||||
|
/// Sets the number of threads to be run by generate
|
||||||
|
void setThreadCount(int threadCount);
|
||||||
|
/// Allows access to the underlying AtlasStorage
|
||||||
|
const AtlasStorage & atlasStorage() const;
|
||||||
|
/// Returns the layout of the contained glyphs as a list of GlyphBoxes
|
||||||
|
const std::vector<GlyphBox> & getLayout() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
AtlasStorage storage;
|
||||||
|
std::vector<GlyphBox> layout;
|
||||||
|
std::vector<T> glyphBuffer;
|
||||||
|
std::vector<byte> errorCorrectionBuffer;
|
||||||
|
GeneratorAttributes attributes;
|
||||||
|
int threadCount;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "ImmediateAtlasGenerator.hpp"
|
|
@ -0,0 +1,82 @@
|
||||||
|
|
||||||
|
#include "ImmediateAtlasGenerator.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::ImmediateAtlasGenerator() : threadCount(1) { }
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::ImmediateAtlasGenerator(int width, int height) : storage(width, height), threadCount(1) { }
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
void ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::generate(const GlyphGeometry *glyphs, int count) {
|
||||||
|
int maxBoxArea = 0;
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
GlyphBox box = glyphs[i];
|
||||||
|
maxBoxArea = std::max(maxBoxArea, box.rect.w*box.rect.h);
|
||||||
|
layout.push_back((GlyphBox &&) box);
|
||||||
|
}
|
||||||
|
int threadBufferSize = N*maxBoxArea;
|
||||||
|
if (threadCount*threadBufferSize > (int) glyphBuffer.size())
|
||||||
|
glyphBuffer.resize(threadCount*threadBufferSize);
|
||||||
|
if (threadCount*maxBoxArea > (int) errorCorrectionBuffer.size())
|
||||||
|
errorCorrectionBuffer.resize(threadCount*maxBoxArea);
|
||||||
|
std::vector<GeneratorAttributes> threadAttributes(threadCount);
|
||||||
|
for (int i = 0; i < threadCount; ++i) {
|
||||||
|
threadAttributes[i] = attributes;
|
||||||
|
threadAttributes[i].config.errorCorrection.buffer = errorCorrectionBuffer.data()+i*maxBoxArea;
|
||||||
|
}
|
||||||
|
|
||||||
|
Workload([this, glyphs, &threadAttributes, threadBufferSize](int i, int threadNo) -> bool {
|
||||||
|
const GlyphGeometry &glyph = glyphs[i];
|
||||||
|
if (!glyph.isWhitespace()) {
|
||||||
|
int l, b, w, h;
|
||||||
|
glyph.getBoxRect(l, b, w, h);
|
||||||
|
msdfgen::BitmapRef<T, N> glyphBitmap(glyphBuffer.data()+threadNo*threadBufferSize, w, h);
|
||||||
|
GEN_FN(glyphBitmap, glyph, threadAttributes[threadNo]);
|
||||||
|
storage.put(l, b, msdfgen::BitmapConstRef<T, N>(glyphBitmap));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}, count).finish(threadCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
void ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::rearrange(int width, int height, const Remap *remapping, int count) {
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
layout[remapping[i].index].rect.x = remapping[i].target.x;
|
||||||
|
layout[remapping[i].index].rect.y = remapping[i].target.y;
|
||||||
|
}
|
||||||
|
AtlasStorage newStorage((AtlasStorage &&) storage, width, height, remapping, count);
|
||||||
|
storage = (AtlasStorage &&) newStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
void ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::resize(int width, int height) {
|
||||||
|
AtlasStorage newStorage((AtlasStorage &&) storage, width, height);
|
||||||
|
storage = (AtlasStorage &&) newStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
void ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::setAttributes(const GeneratorAttributes &attributes) {
|
||||||
|
this->attributes = attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
void ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::setThreadCount(int threadCount) {
|
||||||
|
this->threadCount = threadCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
const AtlasStorage & ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::atlasStorage() const {
|
||||||
|
return storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N, GeneratorFunction<T, N> GEN_FN, class AtlasStorage>
|
||||||
|
const std::vector<GlyphBox> & ImmediateAtlasGenerator<T, N, GEN_FN, AtlasStorage>::getLayout() const {
|
||||||
|
return layout;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/Rectangle.h
Normal file
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/Rectangle.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
struct Rectangle {
|
||||||
|
int x, y, w, h;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OrientedRectangle : Rectangle {
|
||||||
|
bool rotated;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
157
libs/msdf-atlas-gen/include/msdf-atlas-gen/RectanglePacker.cpp
Normal file
157
libs/msdf-atlas-gen/include/msdf-atlas-gen/RectanglePacker.cpp
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
|
||||||
|
#include "RectanglePacker.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
#define WORST_FIT 0x7fffffff
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static void removeFromUnorderedVector(std::vector<T> &vector, size_t index) {
|
||||||
|
if (index != vector.size()-1)
|
||||||
|
std::swap(vector[index], vector.back());
|
||||||
|
vector.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
int RectanglePacker::rateFit(int w, int h, int sw, int sh) {
|
||||||
|
return std::min(sw-w, sh-h);
|
||||||
|
}
|
||||||
|
|
||||||
|
RectanglePacker::RectanglePacker() : RectanglePacker(0, 0) { }
|
||||||
|
|
||||||
|
RectanglePacker::RectanglePacker(int width, int height) {
|
||||||
|
if (width > 0 && height > 0)
|
||||||
|
spaces.push_back(Rectangle { 0, 0, width, height });
|
||||||
|
}
|
||||||
|
|
||||||
|
void RectanglePacker::expand(int width, int height) {
|
||||||
|
if (width > 0 && height > 0) {
|
||||||
|
int oldWidth = 0, oldHeight = 0;
|
||||||
|
for (const Rectangle &space : spaces) {
|
||||||
|
if (space.x+space.w > oldWidth)
|
||||||
|
oldWidth = space.x+space.w;
|
||||||
|
if (space.y+space.h > oldHeight)
|
||||||
|
oldHeight = space.y+space.h;
|
||||||
|
}
|
||||||
|
spaces.push_back(Rectangle { 0, 0, width, height });
|
||||||
|
splitSpace(int(spaces.size()-1), oldWidth, oldHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RectanglePacker::splitSpace(int index, int w, int h) {
|
||||||
|
Rectangle space = spaces[index];
|
||||||
|
removeFromUnorderedVector(spaces, index);
|
||||||
|
Rectangle a = { space.x, space.y+h, w, space.h-h };
|
||||||
|
Rectangle b = { space.x+w, space.y, space.w-w, h };
|
||||||
|
if (w*(space.h-h) < h*(space.w-w))
|
||||||
|
a.w = space.w;
|
||||||
|
else
|
||||||
|
b.h = space.h;
|
||||||
|
if (a.w > 0 && a.h > 0)
|
||||||
|
spaces.push_back(a);
|
||||||
|
if (b.w > 0 && b.h > 0)
|
||||||
|
spaces.push_back(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int RectanglePacker::pack(Rectangle *rectangles, int count) {
|
||||||
|
std::vector<int> remainingRects(count);
|
||||||
|
for (int i = 0; i < count; ++i)
|
||||||
|
remainingRects[i] = i;
|
||||||
|
while (!remainingRects.empty()) {
|
||||||
|
int bestFit = WORST_FIT;
|
||||||
|
int bestSpace = -1;
|
||||||
|
int bestRect = -1;
|
||||||
|
for (size_t i = 0; i < spaces.size(); ++i) {
|
||||||
|
const Rectangle &space = spaces[i];
|
||||||
|
for (size_t j = 0; j < remainingRects.size(); ++j) {
|
||||||
|
const Rectangle &rect = rectangles[remainingRects[j]];
|
||||||
|
if (rect.w == space.w && rect.h == space.h) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
goto BEST_FIT_FOUND;
|
||||||
|
}
|
||||||
|
if (rect.w <= space.w && rect.h <= space.h) {
|
||||||
|
int fit = rateFit(rect.w, rect.h, space.w, space.h);
|
||||||
|
if (fit < bestFit) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
bestFit = fit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestSpace < 0 || bestRect < 0)
|
||||||
|
break;
|
||||||
|
BEST_FIT_FOUND:
|
||||||
|
Rectangle &rect = rectangles[remainingRects[bestRect]];
|
||||||
|
rect.x = spaces[bestSpace].x;
|
||||||
|
rect.y = spaces[bestSpace].y;
|
||||||
|
splitSpace(bestSpace, rect.w, rect.h);
|
||||||
|
removeFromUnorderedVector(remainingRects, bestRect);
|
||||||
|
}
|
||||||
|
return (int) remainingRects.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
int RectanglePacker::pack(OrientedRectangle *rectangles, int count) {
|
||||||
|
std::vector<int> remainingRects(count);
|
||||||
|
for (int i = 0; i < count; ++i)
|
||||||
|
remainingRects[i] = i;
|
||||||
|
while (!remainingRects.empty()) {
|
||||||
|
int bestFit = WORST_FIT;
|
||||||
|
int bestSpace = -1;
|
||||||
|
int bestRect = -1;
|
||||||
|
bool bestRotated = false;
|
||||||
|
for (size_t i = 0; i < spaces.size(); ++i) {
|
||||||
|
const Rectangle &space = spaces[i];
|
||||||
|
for (size_t j = 0; j < remainingRects.size(); ++j) {
|
||||||
|
const OrientedRectangle &rect = rectangles[remainingRects[j]];
|
||||||
|
if (rect.w == space.w && rect.h == space.h) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
bestRotated = false;
|
||||||
|
goto BEST_FIT_FOUND;
|
||||||
|
}
|
||||||
|
if (rect.h == space.w && rect.w == space.h) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
bestRotated = true;
|
||||||
|
goto BEST_FIT_FOUND;
|
||||||
|
}
|
||||||
|
if (rect.w <= space.w && rect.h <= space.h) {
|
||||||
|
int fit = rateFit(rect.w, rect.h, space.w, space.h);
|
||||||
|
if (fit < bestFit) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
bestRotated = false;
|
||||||
|
bestFit = fit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (rect.h <= space.w && rect.w <= space.h) {
|
||||||
|
int fit = rateFit(rect.h, rect.w, space.w, space.h);
|
||||||
|
if (fit < bestFit) {
|
||||||
|
bestSpace = i;
|
||||||
|
bestRect = j;
|
||||||
|
bestRotated = true;
|
||||||
|
bestFit = fit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestSpace < 0 || bestRect < 0)
|
||||||
|
break;
|
||||||
|
BEST_FIT_FOUND:
|
||||||
|
OrientedRectangle &rect = rectangles[remainingRects[bestRect]];
|
||||||
|
rect.x = spaces[bestSpace].x;
|
||||||
|
rect.y = spaces[bestSpace].y;
|
||||||
|
rect.rotated = bestRotated;
|
||||||
|
if (bestRotated)
|
||||||
|
splitSpace(bestSpace, rect.h, rect.w);
|
||||||
|
else
|
||||||
|
splitSpace(bestSpace, rect.w, rect.h);
|
||||||
|
removeFromUnorderedVector(remainingRects, bestRect);
|
||||||
|
}
|
||||||
|
return (int) remainingRects.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
30
libs/msdf-atlas-gen/include/msdf-atlas-gen/RectanglePacker.h
Normal file
30
libs/msdf-atlas-gen/include/msdf-atlas-gen/RectanglePacker.h
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "Rectangle.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Guillotine 2D single bin packer
|
||||||
|
class RectanglePacker {
|
||||||
|
|
||||||
|
public:
|
||||||
|
RectanglePacker();
|
||||||
|
RectanglePacker(int width, int height);
|
||||||
|
/// Expands the packing area - both width and height must be greater or equal to the previous value
|
||||||
|
void expand(int width, int height);
|
||||||
|
/// Packs the rectangle array, returns how many didn't fit (0 on success)
|
||||||
|
int pack(Rectangle *rectangles, int count);
|
||||||
|
int pack(OrientedRectangle *rectangles, int count);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Rectangle> spaces;
|
||||||
|
|
||||||
|
static int rateFit(int w, int h, int sw, int sh);
|
||||||
|
|
||||||
|
void splitSpace(int index, int w, int h);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
15
libs/msdf-atlas-gen/include/msdf-atlas-gen/Remap.h
Normal file
15
libs/msdf-atlas-gen/include/msdf-atlas-gen/Remap.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Represents the repositioning of a subsection of the atlas
|
||||||
|
struct Remap {
|
||||||
|
int index;
|
||||||
|
struct {
|
||||||
|
int x, y;
|
||||||
|
} source, target;
|
||||||
|
int width, height;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
168
libs/msdf-atlas-gen/include/msdf-atlas-gen/TightAtlasPacker.cpp
Normal file
168
libs/msdf-atlas-gen/include/msdf-atlas-gen/TightAtlasPacker.cpp
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
|
||||||
|
#include "TightAtlasPacker.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "Rectangle.h"
|
||||||
|
#include "rectangle-packing.h"
|
||||||
|
#include "size-selectors.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
int TightAtlasPacker::tryPack(GlyphGeometry *glyphs, int count, DimensionsConstraint dimensionsConstraint, int &width, int &height, int padding, double scale, double range, double miterLimit) {
|
||||||
|
// Wrap glyphs into boxes
|
||||||
|
std::vector<Rectangle> rectangles;
|
||||||
|
std::vector<GlyphGeometry *> rectangleGlyphs;
|
||||||
|
rectangles.reserve(count);
|
||||||
|
rectangleGlyphs.reserve(count);
|
||||||
|
for (GlyphGeometry *glyph = glyphs, *end = glyphs+count; glyph < end; ++glyph) {
|
||||||
|
if (!glyph->isWhitespace()) {
|
||||||
|
Rectangle rect = { };
|
||||||
|
glyph->wrapBox(scale, range, miterLimit);
|
||||||
|
glyph->getBoxSize(rect.w, rect.h);
|
||||||
|
if (rect.w > 0 && rect.h > 0) {
|
||||||
|
rectangles.push_back(rect);
|
||||||
|
rectangleGlyphs.push_back(glyph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// No non-zero size boxes?
|
||||||
|
if (rectangles.empty()) {
|
||||||
|
if (width < 0 || height < 0)
|
||||||
|
width = 0, height = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Box rectangle packing
|
||||||
|
if (width < 0 || height < 0) {
|
||||||
|
std::pair<int, int> dimensions = std::make_pair(width, height);
|
||||||
|
switch (dimensionsConstraint) {
|
||||||
|
case DimensionsConstraint::POWER_OF_TWO_SQUARE:
|
||||||
|
dimensions = packRectangles<SquarePowerOfTwoSizeSelector>(rectangles.data(), rectangles.size(), padding);
|
||||||
|
break;
|
||||||
|
case DimensionsConstraint::POWER_OF_TWO_RECTANGLE:
|
||||||
|
dimensions = packRectangles<PowerOfTwoSizeSelector>(rectangles.data(), rectangles.size(), padding);
|
||||||
|
break;
|
||||||
|
case DimensionsConstraint::MULTIPLE_OF_FOUR_SQUARE:
|
||||||
|
dimensions = packRectangles<SquareSizeSelector<4> >(rectangles.data(), rectangles.size(), padding);
|
||||||
|
break;
|
||||||
|
case DimensionsConstraint::EVEN_SQUARE:
|
||||||
|
dimensions = packRectangles<SquareSizeSelector<2> >(rectangles.data(), rectangles.size(), padding);
|
||||||
|
break;
|
||||||
|
case DimensionsConstraint::SQUARE:
|
||||||
|
dimensions = packRectangles<SquareSizeSelector<> >(rectangles.data(), rectangles.size(), padding);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!(dimensions.first > 0 && dimensions.second > 0))
|
||||||
|
return -1;
|
||||||
|
width = dimensions.first, height = dimensions.second;
|
||||||
|
} else {
|
||||||
|
if (int result = packRectangles(rectangles.data(), rectangles.size(), width, height, padding))
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
// Set glyph box placement
|
||||||
|
for (size_t i = 0; i < rectangles.size(); ++i)
|
||||||
|
rectangleGlyphs[i]->placeBox(rectangles[i].x, height-(rectangles[i].y+rectangles[i].h));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double TightAtlasPacker::packAndScale(GlyphGeometry *glyphs, int count, int width, int height, int padding, double unitRange, double pxRange, double miterLimit, double tolerance) {
|
||||||
|
bool lastResult = false;
|
||||||
|
#define TRY_PACK(scale) (lastResult = !tryPack(glyphs, count, DimensionsConstraint(), width, height, padding, (scale), unitRange+pxRange/(scale), miterLimit))
|
||||||
|
double minScale = 1, maxScale = 1;
|
||||||
|
if (TRY_PACK(1)) {
|
||||||
|
while (maxScale < 1e+32 && ((maxScale = 2*minScale), TRY_PACK(maxScale)))
|
||||||
|
minScale = maxScale;
|
||||||
|
} else {
|
||||||
|
while (minScale > 1e-32 && ((minScale = .5*maxScale), !TRY_PACK(minScale)))
|
||||||
|
maxScale = minScale;
|
||||||
|
}
|
||||||
|
if (minScale == maxScale)
|
||||||
|
return 0;
|
||||||
|
while (minScale/maxScale < 1-tolerance) {
|
||||||
|
double midScale = .5*(minScale+maxScale);
|
||||||
|
if (TRY_PACK(midScale))
|
||||||
|
minScale = midScale;
|
||||||
|
else
|
||||||
|
maxScale = midScale;
|
||||||
|
}
|
||||||
|
if (!lastResult)
|
||||||
|
TRY_PACK(minScale);
|
||||||
|
return minScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
TightAtlasPacker::TightAtlasPacker() :
|
||||||
|
width(-1), height(-1),
|
||||||
|
padding(0),
|
||||||
|
dimensionsConstraint(DimensionsConstraint::POWER_OF_TWO_SQUARE),
|
||||||
|
scale(-1),
|
||||||
|
minScale(1),
|
||||||
|
unitRange(0),
|
||||||
|
pxRange(0),
|
||||||
|
miterLimit(0),
|
||||||
|
scaleMaximizationTolerance(.001)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
int TightAtlasPacker::pack(GlyphGeometry *glyphs, int count) {
|
||||||
|
double initialScale = scale > 0 ? scale : minScale;
|
||||||
|
if (initialScale > 0) {
|
||||||
|
if (int remaining = tryPack(glyphs, count, dimensionsConstraint, width, height, padding, initialScale, unitRange+pxRange/initialScale, miterLimit))
|
||||||
|
return remaining;
|
||||||
|
} else if (width < 0 || height < 0)
|
||||||
|
return -1;
|
||||||
|
if (scale <= 0)
|
||||||
|
scale = packAndScale(glyphs, count, width, height, padding, unitRange, pxRange, miterLimit, scaleMaximizationTolerance);
|
||||||
|
if (scale <= 0)
|
||||||
|
return -1;
|
||||||
|
pxRange += scale*unitRange;
|
||||||
|
unitRange = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setDimensions(int width, int height) {
|
||||||
|
this->width = width, this->height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::unsetDimensions() {
|
||||||
|
width = -1, height = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setDimensionsConstraint(DimensionsConstraint dimensionsConstraint) {
|
||||||
|
this->dimensionsConstraint = dimensionsConstraint;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setPadding(int padding) {
|
||||||
|
this->padding = padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setScale(double scale) {
|
||||||
|
this->scale = scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setMinimumScale(double minScale) {
|
||||||
|
this->minScale = minScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setUnitRange(double unitRange) {
|
||||||
|
this->unitRange = unitRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setPixelRange(double pxRange) {
|
||||||
|
this->pxRange = pxRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::setMiterLimit(double miterLimit) {
|
||||||
|
this->miterLimit = miterLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TightAtlasPacker::getDimensions(int &width, int &height) const {
|
||||||
|
width = this->width, height = this->height;
|
||||||
|
}
|
||||||
|
|
||||||
|
double TightAtlasPacker::getScale() const {
|
||||||
|
return scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
double TightAtlasPacker::getPixelRange() const {
|
||||||
|
return pxRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class computes the layout of a static atlas and may optionally
|
||||||
|
* also find the minimum required dimensions and/or the maximum glyph scale
|
||||||
|
*/
|
||||||
|
class TightAtlasPacker {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// Constraints for the atlas's dimensions - see size selectors for more info
|
||||||
|
enum class DimensionsConstraint {
|
||||||
|
POWER_OF_TWO_SQUARE,
|
||||||
|
POWER_OF_TWO_RECTANGLE,
|
||||||
|
MULTIPLE_OF_FOUR_SQUARE,
|
||||||
|
EVEN_SQUARE,
|
||||||
|
SQUARE
|
||||||
|
};
|
||||||
|
|
||||||
|
TightAtlasPacker();
|
||||||
|
|
||||||
|
/// Computes the layout for the array of glyphs. Returns 0 on success
|
||||||
|
int pack(GlyphGeometry *glyphs, int count);
|
||||||
|
|
||||||
|
/// Sets the atlas's dimensions to be fixed
|
||||||
|
void setDimensions(int width, int height);
|
||||||
|
/// Sets the atlas's dimensions to be determined during pack
|
||||||
|
void unsetDimensions();
|
||||||
|
/// Sets the constraint to be used when determining dimensions
|
||||||
|
void setDimensionsConstraint(DimensionsConstraint dimensionsConstraint);
|
||||||
|
/// Sets the padding between glyph boxes
|
||||||
|
void setPadding(int padding);
|
||||||
|
/// Sets fixed glyph scale
|
||||||
|
void setScale(double scale);
|
||||||
|
/// Sets the minimum glyph scale
|
||||||
|
void setMinimumScale(double minScale);
|
||||||
|
/// Sets the unit component of the total distance range
|
||||||
|
void setUnitRange(double unitRange);
|
||||||
|
/// Sets the pixel component of the total distance range
|
||||||
|
void setPixelRange(double pxRange);
|
||||||
|
/// Sets the miter limit for bounds computation
|
||||||
|
void setMiterLimit(double miterLimit);
|
||||||
|
|
||||||
|
/// Outputs the atlas's final dimensions
|
||||||
|
void getDimensions(int &width, int &height) const;
|
||||||
|
/// Returns the final glyph scale
|
||||||
|
double getScale() const;
|
||||||
|
/// Returns the final combined pixel range (including converted unit range)
|
||||||
|
double getPixelRange() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int width, height;
|
||||||
|
int padding;
|
||||||
|
DimensionsConstraint dimensionsConstraint;
|
||||||
|
double scale;
|
||||||
|
double minScale;
|
||||||
|
double unitRange;
|
||||||
|
double pxRange;
|
||||||
|
double miterLimit;
|
||||||
|
double scaleMaximizationTolerance;
|
||||||
|
|
||||||
|
static int tryPack(GlyphGeometry *glyphs, int count, DimensionsConstraint dimensionsConstraint, int &width, int &height, int padding, double scale, double range, double miterLimit);
|
||||||
|
static double packAndScale(GlyphGeometry *glyphs, int count, int width, int height, int padding, double unitRange, double pxRange, double miterLimit, double tolerance);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
50
libs/msdf-atlas-gen/include/msdf-atlas-gen/Workload.cpp
Normal file
50
libs/msdf-atlas-gen/include/msdf-atlas-gen/Workload.cpp
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
|
||||||
|
#include "Workload.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <thread>
|
||||||
|
#include <atomic>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
Workload::Workload() : chunks(0) { }
|
||||||
|
|
||||||
|
Workload::Workload(const std::function<bool(int, int)> &workerFunction, int chunks) : workerFunction(workerFunction), chunks(chunks) { }
|
||||||
|
|
||||||
|
bool Workload::finishSequential() {
|
||||||
|
for (int i = 0; i < chunks; ++i)
|
||||||
|
if (!workerFunction(i, 0))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Workload::finishParallel(int threadCount) {
|
||||||
|
bool result = true;
|
||||||
|
std::atomic<int> next(0);
|
||||||
|
std::function<void(int)> threadWorker = [this, &result, &next](int threadNo) {
|
||||||
|
for (int i = next++; result && i < chunks; i = next++) {
|
||||||
|
if (!workerFunction(i, threadNo))
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
threads.reserve(threadCount);
|
||||||
|
for (int i = 0; i < threadCount; ++i)
|
||||||
|
threads.emplace_back(threadWorker, i);
|
||||||
|
for (std::thread &thread : threads)
|
||||||
|
thread.join();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Workload::finish(int threadCount) {
|
||||||
|
if (!chunks)
|
||||||
|
return true;
|
||||||
|
if (threadCount == 1 || chunks == 1)
|
||||||
|
return finishSequential();
|
||||||
|
if (threadCount > 1)
|
||||||
|
return finishParallel(std::min(threadCount, chunks));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
32
libs/msdf-atlas-gen/include/msdf-atlas-gen/Workload.h
Normal file
32
libs/msdf-atlas-gen/include/msdf-atlas-gen/Workload.h
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function allows to split a workload into multiple threads.
|
||||||
|
* The worker function:
|
||||||
|
* bool FN(int chunk, int threadNo);
|
||||||
|
* should process the given chunk (out of chunks) and return true.
|
||||||
|
* If false is returned, the process is interrupted.
|
||||||
|
*/
|
||||||
|
class Workload {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Workload();
|
||||||
|
Workload(const std::function<bool(int, int)> &workerFunction, int chunks);
|
||||||
|
/// Runs the process and returns true if all chunks have been processed
|
||||||
|
bool finish(int threadCount);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::function<bool(int, int)> workerFunction;
|
||||||
|
int chunks;
|
||||||
|
|
||||||
|
bool finishSequential();
|
||||||
|
bool finishParallel(int threadCount);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,194 @@
|
||||||
|
|
||||||
|
#include "artery-font-export.h"
|
||||||
|
|
||||||
|
#ifndef MSDF_ATLAS_NO_ARTERY_FONT
|
||||||
|
|
||||||
|
#include <artery-font/std-artery-font.h>
|
||||||
|
#include <artery-font/stdio-serialization.h>
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
#include "image-encode.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static artery_font::ImageType convertImageType(ImageType imageType) {
|
||||||
|
switch (imageType) {
|
||||||
|
case ImageType::HARD_MASK:
|
||||||
|
case ImageType::SOFT_MASK:
|
||||||
|
return artery_font::IMAGE_LINEAR_MASK;
|
||||||
|
case ImageType::SDF:
|
||||||
|
return artery_font::IMAGE_SDF;
|
||||||
|
case ImageType::PSDF:
|
||||||
|
return artery_font::IMAGE_PSDF;
|
||||||
|
case ImageType::MSDF:
|
||||||
|
return artery_font::IMAGE_MSDF;
|
||||||
|
case ImageType::MTSDF:
|
||||||
|
return artery_font::IMAGE_MTSDF;
|
||||||
|
}
|
||||||
|
return artery_font::IMAGE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static artery_font::CodepointType convertCodepointType(GlyphIdentifierType glyphIdentifierType) {
|
||||||
|
switch (glyphIdentifierType) {
|
||||||
|
case GlyphIdentifierType::GLYPH_INDEX:
|
||||||
|
return artery_font::CP_INDEXED;
|
||||||
|
case GlyphIdentifierType::UNICODE_CODEPOINT:
|
||||||
|
return artery_font::CP_UNICODE;
|
||||||
|
}
|
||||||
|
return artery_font::CP_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
static bool encodeTiff(std::vector<byte> &output, const msdfgen::BitmapConstRef<T, N> &atlas) {
|
||||||
|
// TODO
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static artery_font::PixelFormat getPixelFormat();
|
||||||
|
|
||||||
|
template <>
|
||||||
|
artery_font::PixelFormat getPixelFormat<byte>() {
|
||||||
|
return artery_font::PIXEL_UNSIGNED8;
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
artery_font::PixelFormat getPixelFormat<float>() {
|
||||||
|
return artery_font::PIXEL_FLOAT32;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename REAL, typename T, int N>
|
||||||
|
bool exportArteryFont(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<T, N> &atlas, const char *filename, const ArteryFontExportProperties &properties) {
|
||||||
|
artery_font::StdArteryFont<REAL> arfont = { };
|
||||||
|
arfont.metadataFormat = artery_font::METADATA_NONE;
|
||||||
|
|
||||||
|
arfont.variants = artery_font::StdList<typename artery_font::StdArteryFont<REAL>::Variant>(fontCount);
|
||||||
|
for (int i = 0; i < fontCount; ++i) {
|
||||||
|
const FontGeometry &font = fonts[i];
|
||||||
|
GlyphIdentifierType identifierType = font.getPreferredIdentifierType();
|
||||||
|
const msdfgen::FontMetrics &fontMetrics = font.getMetrics();
|
||||||
|
typename artery_font::StdArteryFont<REAL>::Variant &fontVariant = arfont.variants[i] = typename artery_font::StdArteryFont<REAL>::Variant();
|
||||||
|
fontVariant.codepointType = convertCodepointType(identifierType);
|
||||||
|
fontVariant.imageType = convertImageType(properties.imageType);
|
||||||
|
fontVariant.metrics.fontSize = REAL(properties.fontSize*fontMetrics.emSize);
|
||||||
|
if (properties.imageType != ImageType::HARD_MASK)
|
||||||
|
fontVariant.metrics.distanceRange = REAL(properties.pxRange);
|
||||||
|
fontVariant.metrics.emSize = REAL(fontMetrics.emSize);
|
||||||
|
fontVariant.metrics.ascender = REAL(fontMetrics.ascenderY);
|
||||||
|
fontVariant.metrics.descender = REAL(fontMetrics.descenderY);
|
||||||
|
fontVariant.metrics.lineHeight = REAL(fontMetrics.lineHeight);
|
||||||
|
fontVariant.metrics.underlineY = REAL(fontMetrics.underlineY);
|
||||||
|
fontVariant.metrics.underlineThickness = REAL(fontMetrics.underlineThickness);
|
||||||
|
const char *name = font.getName();
|
||||||
|
if (name)
|
||||||
|
(std::string &) fontVariant.name = name;
|
||||||
|
fontVariant.glyphs = artery_font::StdList<artery_font::Glyph<REAL> >(font.getGlyphs().size());
|
||||||
|
int j = 0;
|
||||||
|
for (const GlyphGeometry &glyphGeom : font.getGlyphs()) {
|
||||||
|
artery_font::Glyph<REAL> &glyph = fontVariant.glyphs[j++];
|
||||||
|
glyph.codepoint = glyphGeom.getIdentifier(identifierType);
|
||||||
|
glyph.image = 0;
|
||||||
|
double l, b, r, t;
|
||||||
|
glyphGeom.getQuadPlaneBounds(l, b, r, t);
|
||||||
|
glyph.planeBounds.l = REAL(l);
|
||||||
|
glyph.planeBounds.b = REAL(b);
|
||||||
|
glyph.planeBounds.r = REAL(r);
|
||||||
|
glyph.planeBounds.t = REAL(t);
|
||||||
|
glyphGeom.getQuadAtlasBounds(l, b, r, t);
|
||||||
|
glyph.imageBounds.l = REAL(l);
|
||||||
|
glyph.imageBounds.b = REAL(b);
|
||||||
|
glyph.imageBounds.r = REAL(r);
|
||||||
|
glyph.imageBounds.t = REAL(t);
|
||||||
|
glyph.advance.h = REAL(glyphGeom.getAdvance());
|
||||||
|
glyph.advance.v = REAL(0);
|
||||||
|
}
|
||||||
|
switch (identifierType) {
|
||||||
|
case GlyphIdentifierType::GLYPH_INDEX:
|
||||||
|
for (const std::pair<std::pair<int, int>, double> &elem : font.getKerning()) {
|
||||||
|
artery_font::KernPair<REAL> kernPair = { };
|
||||||
|
kernPair.codepoint1 = elem.first.first;
|
||||||
|
kernPair.codepoint2 = elem.first.second;
|
||||||
|
kernPair.advance.h = REAL(elem.second);
|
||||||
|
((std::vector<artery_font::KernPair<REAL> > &) fontVariant.kernPairs).push_back((artery_font::KernPair<REAL> &&) kernPair);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case GlyphIdentifierType::UNICODE_CODEPOINT:
|
||||||
|
for (const std::pair<std::pair<int, int>, double> &elem : font.getKerning()) {
|
||||||
|
const GlyphGeometry *glyph1 = font.getGlyph(msdfgen::GlyphIndex(elem.first.first));
|
||||||
|
const GlyphGeometry *glyph2 = font.getGlyph(msdfgen::GlyphIndex(elem.first.second));
|
||||||
|
if (glyph1 && glyph2 && glyph1->getCodepoint() && glyph2->getCodepoint()) {
|
||||||
|
artery_font::KernPair<REAL> kernPair = { };
|
||||||
|
kernPair.codepoint1 = glyph1->getCodepoint();
|
||||||
|
kernPair.codepoint2 = glyph2->getCodepoint();
|
||||||
|
kernPair.advance.h = REAL(elem.second);
|
||||||
|
((std::vector<artery_font::KernPair<REAL> > &) fontVariant.kernPairs).push_back((artery_font::KernPair<REAL> &&) kernPair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
arfont.images = artery_font::StdList<typename artery_font::StdArteryFont<REAL>::Image>(1);
|
||||||
|
{
|
||||||
|
typename artery_font::StdArteryFont<REAL>::Image &image = arfont.images[0] = typename artery_font::StdArteryFont<REAL>::Image();
|
||||||
|
image.width = atlas.width;
|
||||||
|
image.height = atlas.height;
|
||||||
|
image.channels = N;
|
||||||
|
image.imageType = convertImageType(properties.imageType);
|
||||||
|
switch (properties.imageFormat) {
|
||||||
|
case ImageFormat::PNG:
|
||||||
|
image.encoding = artery_font::IMAGE_PNG;
|
||||||
|
image.pixelFormat = artery_font::PIXEL_UNSIGNED8;
|
||||||
|
if (!encodePng((std::vector<byte> &) image.data, atlas))
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case ImageFormat::TIFF:
|
||||||
|
image.encoding = artery_font::IMAGE_TIFF;
|
||||||
|
image.pixelFormat = artery_font::PIXEL_FLOAT32;
|
||||||
|
if (!encodeTiff((std::vector<byte> &) image.data, atlas))
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case ImageFormat::BINARY:
|
||||||
|
image.pixelFormat = artery_font::PIXEL_UNSIGNED8;
|
||||||
|
goto BINARY_EITHER;
|
||||||
|
case ImageFormat::BINARY_FLOAT:
|
||||||
|
image.pixelFormat = artery_font::PIXEL_FLOAT32;
|
||||||
|
goto BINARY_EITHER;
|
||||||
|
BINARY_EITHER:
|
||||||
|
if (image.pixelFormat != getPixelFormat<T>())
|
||||||
|
return false;
|
||||||
|
image.encoding = artery_font::IMAGE_RAW_BINARY;
|
||||||
|
image.rawBinaryFormat.rowLength = N*sizeof(T)*atlas.width;
|
||||||
|
image.data = artery_font::StdByteArray(N*sizeof(T)*atlas.width*atlas.height);
|
||||||
|
switch (properties.yDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
image.rawBinaryFormat.orientation = artery_font::ORIENTATION_BOTTOM_UP;
|
||||||
|
memcpy((byte *) image.data, atlas.pixels, N*sizeof(T)*atlas.width*atlas.height);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN: {
|
||||||
|
image.rawBinaryFormat.orientation = artery_font::ORIENTATION_TOP_DOWN;
|
||||||
|
byte *imageData = (byte *) image.data;
|
||||||
|
for (int y = atlas.height-1; y >= 0; --y) {
|
||||||
|
memcpy(imageData, atlas.pixels+N*atlas.width*y, N*sizeof(T)*atlas.width);
|
||||||
|
imageData += N*sizeof(T)*atlas.width;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return artery_font::writeFile(arfont, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<byte, 1> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<byte, 3> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<byte, 4> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<float, 1> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<float, 3> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
template bool exportArteryFont<float>(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<float, 4> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef MSDF_ATLAS_NO_ARTERY_FONT
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
struct ArteryFontExportProperties {
|
||||||
|
double fontSize;
|
||||||
|
double pxRange;
|
||||||
|
ImageType imageType;
|
||||||
|
ImageFormat imageFormat;
|
||||||
|
YDirection yDirection;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Encodes the atlas bitmap and its layout into an Artery Atlas Font file
|
||||||
|
template <typename REAL, typename T, int N>
|
||||||
|
bool exportArteryFont(const FontGeometry *fonts, int fontCount, const msdfgen::BitmapConstRef<T, N> &atlas, const char *filename, const ArteryFontExportProperties &properties);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
72
libs/msdf-atlas-gen/include/msdf-atlas-gen/bitmap-blit.cpp
Normal file
72
libs/msdf-atlas-gen/include/msdf-atlas-gen/bitmap-blit.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
|
||||||
|
#include "bitmap-blit.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
#define BOUND_AREA() { \
|
||||||
|
if (dx < 0) w += dx, sx -= dx, dx = 0; \
|
||||||
|
if (dy < 0) h += dy, sy -= dy, dy = 0; \
|
||||||
|
if (sx < 0) w += sx, dx -= sx, sx = 0; \
|
||||||
|
if (sy < 0) h += sy, dy -= sy, sy = 0; \
|
||||||
|
w = std::max(0, std::min(w, std::min(dst.width-dx, src.width-sx))); \
|
||||||
|
h = std::max(0, std::min(h, std::min(dst.height-dy, src.height-sy))); \
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
void blitSameType(const msdfgen::BitmapRef<T, N> &dst, const msdfgen::BitmapConstRef<T, N> &src, int dx, int dy, int sx, int sy, int w, int h) {
|
||||||
|
BOUND_AREA();
|
||||||
|
for (int y = 0; y < h; ++y)
|
||||||
|
memcpy(dst(dx, dy+y), src(sx, sy+y), sizeof(T)*N*w);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BLIT_SAME_TYPE_IMPL(T, N) void blit(const msdfgen::BitmapRef<T, N> &dst, const msdfgen::BitmapConstRef<T, N> &src, int dx, int dy, int sx, int sy, int w, int h) { blitSameType(dst, src, dx, dy, sx, sy, w, h); }
|
||||||
|
|
||||||
|
BLIT_SAME_TYPE_IMPL(byte, 1)
|
||||||
|
BLIT_SAME_TYPE_IMPL(byte, 3)
|
||||||
|
BLIT_SAME_TYPE_IMPL(byte, 4)
|
||||||
|
BLIT_SAME_TYPE_IMPL(float, 1)
|
||||||
|
BLIT_SAME_TYPE_IMPL(float, 3)
|
||||||
|
BLIT_SAME_TYPE_IMPL(float, 4)
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 1> &dst, const msdfgen::BitmapConstRef<float, 1> &src, int dx, int dy, int sx, int sy, int w, int h) {
|
||||||
|
BOUND_AREA();
|
||||||
|
for (int y = 0; y < h; ++y) {
|
||||||
|
byte *dstPixel = dst(dx, dy+y);
|
||||||
|
for (int x = 0; x < w; ++x) {
|
||||||
|
const float *srcPixel = src(sx+x, sy+y);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(*srcPixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 3> &dst, const msdfgen::BitmapConstRef<float, 3> &src, int dx, int dy, int sx, int sy, int w, int h) {
|
||||||
|
BOUND_AREA();
|
||||||
|
for (int y = 0; y < h; ++y) {
|
||||||
|
byte *dstPixel = dst(dx, dy+y);
|
||||||
|
for (int x = 0; x < w; ++x) {
|
||||||
|
const float *srcPixel = src(sx+x, sy+y);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[0]);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[1]);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 4> &dst, const msdfgen::BitmapConstRef<float, 4> &src, int dx, int dy, int sx, int sy, int w, int h) {
|
||||||
|
BOUND_AREA();
|
||||||
|
for (int y = 0; y < h; ++y) {
|
||||||
|
byte *dstPixel = dst(dx, dy+y);
|
||||||
|
for (int x = 0; x < w; ++x) {
|
||||||
|
const float *srcPixel = src(sx+x, sy+y);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[0]);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[1]);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[2]);
|
||||||
|
*dstPixel++ = msdfgen::pixelFloatToByte(srcPixel[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
26
libs/msdf-atlas-gen/include/msdf-atlas-gen/bitmap-blit.h
Normal file
26
libs/msdf-atlas-gen/include/msdf-atlas-gen/bitmap-blit.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copies a rectangular section from source bitmap to destination bitmap.
|
||||||
|
* Width and height are not checked and must not exceed bitmap bounds!
|
||||||
|
*/
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 1> &dst, const msdfgen::BitmapConstRef<byte, 1> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 3> &dst, const msdfgen::BitmapConstRef<byte, 3> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 4> &dst, const msdfgen::BitmapConstRef<byte, 4> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<float, 1> &dst, const msdfgen::BitmapConstRef<float, 1> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<float, 3> &dst, const msdfgen::BitmapConstRef<float, 3> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<float, 4> &dst, const msdfgen::BitmapConstRef<float, 4> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 1> &dst, const msdfgen::BitmapConstRef<float, 1> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 3> &dst, const msdfgen::BitmapConstRef<float, 3> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
void blit(const msdfgen::BitmapRef<byte, 4> &dst, const msdfgen::BitmapConstRef<float, 4> &src, int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
|
||||||
|
}
|
250
libs/msdf-atlas-gen/include/msdf-atlas-gen/charset-parser.cpp
Normal file
250
libs/msdf-atlas-gen/include/msdf-atlas-gen/charset-parser.cpp
Normal file
|
@ -0,0 +1,250 @@
|
||||||
|
|
||||||
|
#include "Charset.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
#include "utf8.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static char escapedChar(char c) {
|
||||||
|
switch (c) {
|
||||||
|
case '0':
|
||||||
|
return '\0';
|
||||||
|
case 'n': case 'N':
|
||||||
|
return '\n';
|
||||||
|
case 'r': case 'R':
|
||||||
|
return '\r';
|
||||||
|
case 's': case 'S':
|
||||||
|
return ' ';
|
||||||
|
case 't': case 'T':
|
||||||
|
return '\t';
|
||||||
|
case '\\': case '"': case '\'':
|
||||||
|
default:
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readWord(std::string &str, FILE *f) {
|
||||||
|
while (true) {
|
||||||
|
int c = fgetc(f);
|
||||||
|
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')
|
||||||
|
str.push_back((char) c);
|
||||||
|
else
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool readString(std::string &str, FILE *f, char terminator) {
|
||||||
|
bool escape = false;
|
||||||
|
while (true) {
|
||||||
|
int c = fgetc(f);
|
||||||
|
if (c < 0)
|
||||||
|
return false;
|
||||||
|
if (escape) {
|
||||||
|
str.push_back(escapedChar((char) c));
|
||||||
|
escape = false;
|
||||||
|
} else {
|
||||||
|
if (c == terminator)
|
||||||
|
return true;
|
||||||
|
else if (c == '\\')
|
||||||
|
escape = true;
|
||||||
|
else
|
||||||
|
str.push_back((char) c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parseInt(int &i, const char *str) {
|
||||||
|
i = 0;
|
||||||
|
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { // hex
|
||||||
|
str += 2;
|
||||||
|
for (; *str; ++str) {
|
||||||
|
if (*str >= '0' && *str <= '9') {
|
||||||
|
i <<= 4;
|
||||||
|
i += *str-'0';
|
||||||
|
} else if (*str >= 'A' && *str <= 'F') {
|
||||||
|
i <<= 4;
|
||||||
|
i += *str-'A'+10;
|
||||||
|
} else if (*str >= 'a' && *str <= 'f') {
|
||||||
|
i <<= 4;
|
||||||
|
i += *str-'a'+10;
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else { // dec
|
||||||
|
for (; *str; ++str) {
|
||||||
|
if (*str >= '0' && *str <= '9') {
|
||||||
|
i *= 10;
|
||||||
|
i += *str-'0';
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string combinePath(const char *basePath, const char *relPath) {
|
||||||
|
if (relPath[0] == '/' || (relPath[0] && relPath[1] == ':')) // absolute path?
|
||||||
|
return relPath;
|
||||||
|
int lastSlash = -1;
|
||||||
|
for (int i = 0; basePath[i]; ++i)
|
||||||
|
if (basePath[i] == '/' || basePath[i] == '\\')
|
||||||
|
lastSlash = i;
|
||||||
|
if (lastSlash < 0)
|
||||||
|
return relPath;
|
||||||
|
return std::string(basePath, lastSlash+1)+relPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Charset::load(const char *filename, bool disableCharLiterals) {
|
||||||
|
|
||||||
|
if (FILE *f = fopen(filename, "rb")) {
|
||||||
|
|
||||||
|
enum {
|
||||||
|
CLEAR,
|
||||||
|
TIGHT,
|
||||||
|
RANGE_BRACKET,
|
||||||
|
RANGE_START,
|
||||||
|
RANGE_SEPARATOR,
|
||||||
|
RANGE_END
|
||||||
|
} state = CLEAR;
|
||||||
|
|
||||||
|
std::string buffer;
|
||||||
|
std::vector<unicode_t> unicodeBuffer;
|
||||||
|
unicode_t rangeStart = 0;
|
||||||
|
for (int c = fgetc(f), start = true; c >= 0; start = false) {
|
||||||
|
switch (c) {
|
||||||
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // number
|
||||||
|
if (!(state == CLEAR || state == RANGE_BRACKET || state == RANGE_SEPARATOR))
|
||||||
|
goto FAIL;
|
||||||
|
buffer.push_back((char) c);
|
||||||
|
c = readWord(buffer, f);
|
||||||
|
{
|
||||||
|
int cp;
|
||||||
|
if (!parseInt(cp, buffer.c_str()))
|
||||||
|
goto FAIL;
|
||||||
|
switch (state) {
|
||||||
|
case CLEAR:
|
||||||
|
if (cp >= 0)
|
||||||
|
add((unicode_t) cp);
|
||||||
|
state = TIGHT;
|
||||||
|
break;
|
||||||
|
case RANGE_BRACKET:
|
||||||
|
rangeStart = (unicode_t) cp;
|
||||||
|
state = RANGE_START;
|
||||||
|
break;
|
||||||
|
case RANGE_SEPARATOR:
|
||||||
|
for (unicode_t u = rangeStart; (int) u <= cp; ++u)
|
||||||
|
add(u);
|
||||||
|
state = RANGE_END;
|
||||||
|
break;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer.clear();
|
||||||
|
continue; // next character already read
|
||||||
|
case '\'': // single UTF-8 character
|
||||||
|
if (!(state == CLEAR || state == RANGE_BRACKET || state == RANGE_SEPARATOR) || disableCharLiterals)
|
||||||
|
goto FAIL;
|
||||||
|
if (!readString(buffer, f, '\''))
|
||||||
|
goto FAIL;
|
||||||
|
utf8Decode(unicodeBuffer, buffer.c_str());
|
||||||
|
if (unicodeBuffer.size() == 1) {
|
||||||
|
switch (state) {
|
||||||
|
case CLEAR:
|
||||||
|
if (unicodeBuffer[0] > 0)
|
||||||
|
add(unicodeBuffer[0]);
|
||||||
|
state = TIGHT;
|
||||||
|
break;
|
||||||
|
case RANGE_BRACKET:
|
||||||
|
rangeStart = unicodeBuffer[0];
|
||||||
|
state = RANGE_START;
|
||||||
|
break;
|
||||||
|
case RANGE_SEPARATOR:
|
||||||
|
for (unicode_t u = rangeStart; u <= unicodeBuffer[0]; ++u)
|
||||||
|
add(u);
|
||||||
|
state = RANGE_END;
|
||||||
|
break;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
goto FAIL;
|
||||||
|
unicodeBuffer.clear();
|
||||||
|
buffer.clear();
|
||||||
|
break;
|
||||||
|
case '"': // string of UTF-8 characters
|
||||||
|
if (state != CLEAR || disableCharLiterals)
|
||||||
|
goto FAIL;
|
||||||
|
if (!readString(buffer, f, '"'))
|
||||||
|
goto FAIL;
|
||||||
|
utf8Decode(unicodeBuffer, buffer.c_str());
|
||||||
|
for (unicode_t cp : unicodeBuffer)
|
||||||
|
add(cp);
|
||||||
|
unicodeBuffer.clear();
|
||||||
|
buffer.clear();
|
||||||
|
state = TIGHT;
|
||||||
|
break;
|
||||||
|
case '[': // character range start
|
||||||
|
if (state != CLEAR)
|
||||||
|
goto FAIL;
|
||||||
|
state = RANGE_BRACKET;
|
||||||
|
break;
|
||||||
|
case ']': // character range end
|
||||||
|
if (state == RANGE_END)
|
||||||
|
state = TIGHT;
|
||||||
|
else
|
||||||
|
goto FAIL;
|
||||||
|
break;
|
||||||
|
case '@': // annotation
|
||||||
|
if (state != CLEAR)
|
||||||
|
goto FAIL;
|
||||||
|
c = readWord(buffer, f);
|
||||||
|
if (buffer == "include") {
|
||||||
|
while (c == ' ' || c == '\t' || c == '\n' || c == '\r')
|
||||||
|
c = fgetc(f);
|
||||||
|
if (c != '"')
|
||||||
|
goto FAIL;
|
||||||
|
buffer.clear();
|
||||||
|
if (!readString(buffer, f, '"'))
|
||||||
|
goto FAIL;
|
||||||
|
load(combinePath(filename, buffer.c_str()).c_str());
|
||||||
|
state = TIGHT;
|
||||||
|
} else
|
||||||
|
goto FAIL;
|
||||||
|
buffer.clear();
|
||||||
|
break;
|
||||||
|
case ',': case ';': // separator
|
||||||
|
if (!(state == CLEAR || state == TIGHT)) {
|
||||||
|
if (state == RANGE_START)
|
||||||
|
state = RANGE_SEPARATOR;
|
||||||
|
else
|
||||||
|
goto FAIL;
|
||||||
|
} // else treat as whitespace
|
||||||
|
case ' ': case '\n': case '\r': case '\t': // whitespace
|
||||||
|
if (state == TIGHT)
|
||||||
|
state = CLEAR;
|
||||||
|
break;
|
||||||
|
case 0xef: // UTF-8 byte order mark
|
||||||
|
if (start) {
|
||||||
|
if (!(fgetc(f) == 0xbb && fgetc(f) == 0xbf))
|
||||||
|
goto FAIL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: // unexpected character
|
||||||
|
goto FAIL;
|
||||||
|
}
|
||||||
|
c = fgetc(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return state == CLEAR || state == TIGHT;
|
||||||
|
|
||||||
|
FAIL:
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
libs/msdf-atlas-gen/include/msdf-atlas-gen/csv-export.cpp
Normal file
45
libs/msdf-atlas-gen/include/msdf-atlas-gen/csv-export.cpp
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
|
||||||
|
#include "csv-export.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
bool exportCSV(const FontGeometry *fonts, int fontCount, int atlasWidth, int atlasHeight, YDirection yDirection, const char *filename) {
|
||||||
|
FILE *f = fopen(filename, "w");
|
||||||
|
if (!f)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (int i = 0; i < fontCount; ++i) {
|
||||||
|
for (const GlyphGeometry &glyph : fonts[i].getGlyphs()) {
|
||||||
|
double l, b, r, t;
|
||||||
|
if (fontCount > 1)
|
||||||
|
fprintf(f, "%d,", i);
|
||||||
|
fprintf(f, "%d,%.17g,", glyph.getIdentifier(fonts[i].getPreferredIdentifierType()), glyph.getAdvance());
|
||||||
|
glyph.getQuadPlaneBounds(l, b, r, t);
|
||||||
|
switch (yDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
fprintf(f, "%.17g,%.17g,%.17g,%.17g,", l, b, r, t);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
fprintf(f, "%.17g,%.17g,%.17g,%.17g,", l, -t, r, -b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
glyph.getQuadAtlasBounds(l, b, r, t);
|
||||||
|
switch (yDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
fprintf(f, "%.17g,%.17g,%.17g,%.17g\n", l, b, r, t);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
fprintf(f, "%.17g,%.17g,%.17g,%.17g\n", l, atlasHeight-t, r, atlasHeight-b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/csv-export.h
Normal file
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/csv-export.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the positioning data and atlas layout of the glyphs into a CSV file
|
||||||
|
* The columns are: font variant index (if fontCount > 1), glyph identifier (index or Unicode), horizontal advance, plane bounds (l, b, r, t), atlas bounds (l, b, r, t)
|
||||||
|
*/
|
||||||
|
bool exportCSV(const FontGeometry *fonts, int fontCount, int atlasWidth, int atlasHeight, YDirection yDirection, const char *filename);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
|
||||||
|
#include "glyph-generators.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
void scanlineGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs) {
|
||||||
|
msdfgen::rasterize(output, glyph.getShape(), glyph.getBoxScale(), glyph.getBoxTranslate(), MSDF_ATLAS_GLYPH_FILL_RULE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sdfGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs) {
|
||||||
|
msdfgen::generateSDF(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), attribs.config);
|
||||||
|
if (attribs.scanlinePass)
|
||||||
|
msdfgen::distanceSignCorrection(output, glyph.getShape(), glyph.getBoxProjection(), MSDF_ATLAS_GLYPH_FILL_RULE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void psdfGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs) {
|
||||||
|
msdfgen::generatePseudoSDF(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), attribs.config);
|
||||||
|
if (attribs.scanlinePass)
|
||||||
|
msdfgen::distanceSignCorrection(output, glyph.getShape(), glyph.getBoxProjection(), MSDF_ATLAS_GLYPH_FILL_RULE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void msdfGenerator(const msdfgen::BitmapRef<float, 3> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs) {
|
||||||
|
msdfgen::MSDFGeneratorConfig config = attribs.config;
|
||||||
|
if (attribs.scanlinePass)
|
||||||
|
config.errorCorrection.mode = msdfgen::ErrorCorrectionConfig::DISABLED;
|
||||||
|
msdfgen::generateMSDF(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), config);
|
||||||
|
if (attribs.scanlinePass) {
|
||||||
|
msdfgen::distanceSignCorrection(output, glyph.getShape(), glyph.getBoxProjection(), MSDF_ATLAS_GLYPH_FILL_RULE);
|
||||||
|
if (attribs.config.errorCorrection.mode != msdfgen::ErrorCorrectionConfig::DISABLED) {
|
||||||
|
config.errorCorrection.mode = attribs.config.errorCorrection.mode;
|
||||||
|
config.errorCorrection.distanceCheckMode = msdfgen::ErrorCorrectionConfig::DO_NOT_CHECK_DISTANCE;
|
||||||
|
msdfgen::msdfErrorCorrection(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mtsdfGenerator(const msdfgen::BitmapRef<float, 4> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs) {
|
||||||
|
msdfgen::MSDFGeneratorConfig config = attribs.config;
|
||||||
|
if (attribs.scanlinePass)
|
||||||
|
config.errorCorrection.mode = msdfgen::ErrorCorrectionConfig::DISABLED;
|
||||||
|
msdfgen::generateMTSDF(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), config);
|
||||||
|
if (attribs.scanlinePass) {
|
||||||
|
msdfgen::distanceSignCorrection(output, glyph.getShape(), glyph.getBoxProjection(), MSDF_ATLAS_GLYPH_FILL_RULE);
|
||||||
|
if (attribs.config.errorCorrection.mode != msdfgen::ErrorCorrectionConfig::DISABLED) {
|
||||||
|
config.errorCorrection.mode = attribs.config.errorCorrection.mode;
|
||||||
|
config.errorCorrection.distanceCheckMode = msdfgen::ErrorCorrectionConfig::DO_NOT_CHECK_DISTANCE;
|
||||||
|
msdfgen::msdfErrorCorrection(output, glyph.getShape(), glyph.getBoxProjection(), glyph.getBoxRange(), config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
#include "AtlasGenerator.h"
|
||||||
|
|
||||||
|
#define MSDF_ATLAS_GLYPH_FILL_RULE msdfgen::FILL_NONZERO
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
// Glyph bitmap generator functions
|
||||||
|
|
||||||
|
/// Generates non-anti-aliased binary image of the glyph using scanline rasterization
|
||||||
|
void scanlineGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs);
|
||||||
|
/// Generates a true signed distance field of the glyph
|
||||||
|
void sdfGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs);
|
||||||
|
/// Generates a signed pseudo-distance field of the glyph
|
||||||
|
void psdfGenerator(const msdfgen::BitmapRef<float, 1> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs);
|
||||||
|
/// Generates a multi-channel signed distance field of the glyph
|
||||||
|
void msdfGenerator(const msdfgen::BitmapRef<float, 3> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs);
|
||||||
|
/// Generates a multi-channel and alpha-encoded true signed distance field of the glyph
|
||||||
|
void mtsdfGenerator(const msdfgen::BitmapRef<float, 4> &output, const GlyphGeometry &glyph, const GeneratorAttributes &attribs);
|
||||||
|
|
||||||
|
}
|
157
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-encode.cpp
Normal file
157
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-encode.cpp
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
|
||||||
|
#include "image-encode.h"
|
||||||
|
|
||||||
|
#include <core/pixel-conversion.hpp>
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_LIBPNG
|
||||||
|
|
||||||
|
#include <png.h>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
class PngGuard {
|
||||||
|
png_structp png;
|
||||||
|
png_infop info;
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline PngGuard(png_structp png, png_infop info) : png(png), info(info) { }
|
||||||
|
inline ~PngGuard() {
|
||||||
|
png_destroy_write_struct(&png, &info);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
static void pngIgnoreError(png_structp, png_const_charp) { }
|
||||||
|
|
||||||
|
static void pngWrite(png_structp png, png_bytep data, png_size_t length) {
|
||||||
|
std::vector<byte> &output = *reinterpret_cast<std::vector<byte> *>(png_get_io_ptr(png));
|
||||||
|
output.insert(output.end(), data, data+length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pngFlush(png_structp) { }
|
||||||
|
|
||||||
|
static bool pngEncode(std::vector<byte> &output, const byte *pixels, int width, int height, int channels, int colorType) {
|
||||||
|
if (!(pixels && width && height))
|
||||||
|
return false;
|
||||||
|
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, &pngIgnoreError, &pngIgnoreError);
|
||||||
|
if (!png)
|
||||||
|
return false;
|
||||||
|
png_infop info = png_create_info_struct(png);
|
||||||
|
PngGuard guard(png, info);
|
||||||
|
if (!info)
|
||||||
|
return false;
|
||||||
|
std::vector<const byte *> rows(height);
|
||||||
|
for (int y = 0; y < height; ++y)
|
||||||
|
rows[y] = pixels+channels*width*(height-y-1);
|
||||||
|
if (setjmp(png_jmpbuf(png)))
|
||||||
|
return false;
|
||||||
|
png_set_write_fn(png, &output, &pngWrite, &pngFlush);
|
||||||
|
png_set_IHDR(png, info, width, height, 8, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
||||||
|
png_set_compression_level(png, 9);
|
||||||
|
png_set_rows(png, info, const_cast<png_bytepp>(&rows[0]));
|
||||||
|
png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool pngEncode(std::vector<byte> &output, const float *pixels, int width, int height, int channels, int colorType) {
|
||||||
|
if (!(pixels && width && height))
|
||||||
|
return false;
|
||||||
|
int subpixels = channels*width*height;
|
||||||
|
std::vector<byte> bytePixels(subpixels);
|
||||||
|
for (int i = 0; i < subpixels; ++i)
|
||||||
|
bytePixels[i] = msdfgen::pixelFloatToByte(pixels[i]);
|
||||||
|
return pngEncode(output, bytePixels.data(), width, height, channels, colorType);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 1> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 1, PNG_COLOR_TYPE_GRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 3> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 3, PNG_COLOR_TYPE_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 4> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 4, PNG_COLOR_TYPE_RGB_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 1> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 1, PNG_COLOR_TYPE_GRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 3> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 3, PNG_COLOR_TYPE_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 4> &bitmap) {
|
||||||
|
return pngEncode(output, bitmap.pixels, bitmap.width, bitmap.height, 4, PNG_COLOR_TYPE_RGB_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_LODEPNG
|
||||||
|
|
||||||
|
#include <lodepng.h>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 1> &bitmap) {
|
||||||
|
std::vector<byte> pixels(bitmap.width*bitmap.height);
|
||||||
|
for (int y = 0; y < bitmap.height; ++y)
|
||||||
|
memcpy(&pixels[bitmap.width*y], bitmap(0, bitmap.height-y-1), bitmap.width);
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_GREY);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 3> &bitmap) {
|
||||||
|
std::vector<byte> pixels(3*bitmap.width*bitmap.height);
|
||||||
|
for (int y = 0; y < bitmap.height; ++y)
|
||||||
|
memcpy(&pixels[3*bitmap.width*y], bitmap(0, bitmap.height-y-1), 3*bitmap.width);
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 4> &bitmap) {
|
||||||
|
std::vector<byte> pixels(4*bitmap.width*bitmap.height);
|
||||||
|
for (int y = 0; y < bitmap.height; ++y)
|
||||||
|
memcpy(&pixels[4*bitmap.width*y], bitmap(0, bitmap.height-y-1), 4*bitmap.width);
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_RGBA);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 1> &bitmap) {
|
||||||
|
std::vector<byte> pixels(bitmap.width*bitmap.height);
|
||||||
|
std::vector<byte>::iterator it = pixels.begin();
|
||||||
|
for (int y = bitmap.height-1; y >= 0; --y)
|
||||||
|
for (int x = 0; x < bitmap.width; ++x)
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(*bitmap(x, y));
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_GREY);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 3> &bitmap) {
|
||||||
|
std::vector<byte> pixels(3*bitmap.width*bitmap.height);
|
||||||
|
std::vector<byte>::iterator it = pixels.begin();
|
||||||
|
for (int y = bitmap.height-1; y >= 0; --y)
|
||||||
|
for (int x = 0; x < bitmap.width; ++x) {
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[0]);
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[1]);
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[2]);
|
||||||
|
}
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 4> &bitmap) {
|
||||||
|
std::vector<byte> pixels(4*bitmap.width*bitmap.height);
|
||||||
|
std::vector<byte>::iterator it = pixels.begin();
|
||||||
|
for (int y = bitmap.height-1; y >= 0; --y)
|
||||||
|
for (int x = 0; x < bitmap.width; ++x) {
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[0]);
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[1]);
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[2]);
|
||||||
|
*it++ = msdfgen::pixelFloatToByte(bitmap(x, y)[3]);
|
||||||
|
}
|
||||||
|
return !lodepng::encode(output, pixels, bitmap.width, bitmap.height, LCT_RGBA);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
20
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-encode.h
Normal file
20
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-encode.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
// Functions to encode an image as a sequence of bytes in memory
|
||||||
|
// Only PNG format available currently
|
||||||
|
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 1> &bitmap);
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 3> &bitmap);
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<msdfgen::byte, 4> &bitmap);
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 1> &bitmap);
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 3> &bitmap);
|
||||||
|
bool encodePng(std::vector<byte> &output, const msdfgen::BitmapConstRef<float, 4> &bitmap);
|
||||||
|
|
||||||
|
}
|
15
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-save.h
Normal file
15
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-save.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Saves the bitmap as an image file with the specified format
|
||||||
|
template <typename T, int N>
|
||||||
|
bool saveImage(const msdfgen::BitmapConstRef<T, N> &bitmap, ImageFormat format, const char *filename, YDirection outputYDirection = YDirection::BOTTOM_UP);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "image-save.hpp"
|
172
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-save.hpp
Normal file
172
libs/msdf-atlas-gen/include/msdf-atlas-gen/image-save.hpp
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
|
||||||
|
#include "image-save.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImageBinary(const msdfgen::BitmapConstRef<byte, N> &bitmap, const char *filename, YDirection outputYDirection);
|
||||||
|
template <int N>
|
||||||
|
bool saveImageBinaryLE(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection);
|
||||||
|
template <int N>
|
||||||
|
bool saveImageBinaryBE(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection);
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImageText(const msdfgen::BitmapConstRef<byte, N> &bitmap, const char *filename, YDirection outputYDirection);
|
||||||
|
template <int N>
|
||||||
|
bool saveImageText(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection);
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImage(const msdfgen::BitmapConstRef<byte, N> &bitmap, ImageFormat format, const char *filename, YDirection outputYDirection) {
|
||||||
|
switch (format) {
|
||||||
|
case ImageFormat::PNG:
|
||||||
|
return msdfgen::savePng(bitmap, filename);
|
||||||
|
case ImageFormat::BMP:
|
||||||
|
return msdfgen::saveBmp(bitmap, filename);
|
||||||
|
case ImageFormat::TIFF:
|
||||||
|
return false;
|
||||||
|
case ImageFormat::TEXT:
|
||||||
|
return saveImageText(bitmap, filename, outputYDirection);
|
||||||
|
case ImageFormat::TEXT_FLOAT:
|
||||||
|
return false;
|
||||||
|
case ImageFormat::BINARY:
|
||||||
|
return saveImageBinary(bitmap, filename, outputYDirection);
|
||||||
|
case ImageFormat::BINARY_FLOAT:
|
||||||
|
case ImageFormat::BINARY_FLOAT_BE:
|
||||||
|
return false;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImage(const msdfgen::BitmapConstRef<float, N> &bitmap, ImageFormat format, const char *filename, YDirection outputYDirection) {
|
||||||
|
switch (format) {
|
||||||
|
case ImageFormat::PNG:
|
||||||
|
return msdfgen::savePng(bitmap, filename);
|
||||||
|
case ImageFormat::BMP:
|
||||||
|
return msdfgen::saveBmp(bitmap, filename);
|
||||||
|
case ImageFormat::TIFF:
|
||||||
|
return msdfgen::saveTiff(bitmap, filename);
|
||||||
|
case ImageFormat::TEXT:
|
||||||
|
return false;
|
||||||
|
case ImageFormat::TEXT_FLOAT:
|
||||||
|
return saveImageText(bitmap, filename, outputYDirection);
|
||||||
|
case ImageFormat::BINARY:
|
||||||
|
return false;
|
||||||
|
case ImageFormat::BINARY_FLOAT:
|
||||||
|
return saveImageBinaryLE(bitmap, filename, outputYDirection);
|
||||||
|
case ImageFormat::BINARY_FLOAT_BE:
|
||||||
|
return saveImageBinaryBE(bitmap, filename, outputYDirection);
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImageBinary(const msdfgen::BitmapConstRef<byte, N> &bitmap, const char *filename, YDirection outputYDirection) {
|
||||||
|
bool success = false;
|
||||||
|
if (FILE *f = fopen(filename, "wb")) {
|
||||||
|
int written = 0;
|
||||||
|
switch (outputYDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
written = fwrite(bitmap.pixels, 1, N*bitmap.width*bitmap.height, f);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
for (int y = bitmap.height-1; y >= 0; --y)
|
||||||
|
written += fwrite(bitmap.pixels+N*bitmap.width*y, 1, N*bitmap.width, f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
success = written == N*bitmap.width*bitmap.height;
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool
|
||||||
|
#ifdef __BIG_ENDIAN__
|
||||||
|
saveImageBinaryBE
|
||||||
|
#else
|
||||||
|
saveImageBinaryLE
|
||||||
|
#endif
|
||||||
|
(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection) {
|
||||||
|
bool success = false;
|
||||||
|
if (FILE *f = fopen(filename, "wb")) {
|
||||||
|
int written = 0;
|
||||||
|
switch (outputYDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
written = fwrite(bitmap.pixels, sizeof(float), N*bitmap.width*bitmap.height, f);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
for (int y = bitmap.height-1; y >= 0; --y)
|
||||||
|
written += fwrite(bitmap.pixels+N*bitmap.width*y, sizeof(float), N*bitmap.width, f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
success = written == N*bitmap.width*bitmap.height;
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool
|
||||||
|
#ifdef __BIG_ENDIAN__
|
||||||
|
saveImageBinaryLE
|
||||||
|
#else
|
||||||
|
saveImageBinaryBE
|
||||||
|
#endif
|
||||||
|
(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection) {
|
||||||
|
bool success = false;
|
||||||
|
if (FILE *f = fopen(filename, "wb")) {
|
||||||
|
int written = 0;
|
||||||
|
for (int y = 0; y < bitmap.height; ++y) {
|
||||||
|
const float *p = bitmap.pixels+N*bitmap.width*(outputYDirection == YDirection::TOP_DOWN ? bitmap.height-y-1 : y);
|
||||||
|
for (int x = 0; x < bitmap.width; ++x) {
|
||||||
|
const unsigned char *b = reinterpret_cast<const unsigned char *>(p++);
|
||||||
|
for (int i = sizeof(float)-1; i >= 0; --i)
|
||||||
|
written += fwrite(b+i, 1, 1, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
success = written == sizeof(float)*N*bitmap.width*bitmap.height;
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImageText(const msdfgen::BitmapConstRef<byte, N> &bitmap, const char *filename, YDirection outputYDirection) {
|
||||||
|
bool success = false;
|
||||||
|
if (FILE *f = fopen(filename, "wb")) {
|
||||||
|
success = true;
|
||||||
|
for (int y = 0; y < bitmap.height; ++y) {
|
||||||
|
const byte *p = bitmap.pixels+N*bitmap.width*(outputYDirection == YDirection::TOP_DOWN ? bitmap.height-y-1 : y);
|
||||||
|
for (int x = 0; x < N*bitmap.width; ++x)
|
||||||
|
success &= fprintf(f, x ? " %02X" : "%02X", (unsigned) *p++) > 0;
|
||||||
|
success &= fprintf(f, "\n") > 0;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
bool saveImageText(const msdfgen::BitmapConstRef<float, N> &bitmap, const char *filename, YDirection outputYDirection) {
|
||||||
|
bool success = false;
|
||||||
|
if (FILE *f = fopen(filename, "wb")) {
|
||||||
|
success = true;
|
||||||
|
for (int y = 0; y < bitmap.height; ++y) {
|
||||||
|
const float *p = bitmap.pixels+N*bitmap.width*(outputYDirection == YDirection::TOP_DOWN ? bitmap.height-y-1 : y);
|
||||||
|
for (int x = 0; x < N*bitmap.width; ++x)
|
||||||
|
success &= fprintf(f, x ? " %g" : "%g", *p++) > 0;
|
||||||
|
success &= fprintf(f, "\n") > 0;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
186
libs/msdf-atlas-gen/include/msdf-atlas-gen/json-export.cpp
Normal file
186
libs/msdf-atlas-gen/include/msdf-atlas-gen/json-export.cpp
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
|
||||||
|
#include "json-export.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static std::string escapeJsonString(const char *str) {
|
||||||
|
char uval[7] = "\\u0000";
|
||||||
|
std::string outStr;
|
||||||
|
while (*str) {
|
||||||
|
switch (*str) {
|
||||||
|
case '\\':
|
||||||
|
outStr += "\\\\";
|
||||||
|
break;
|
||||||
|
case '"':
|
||||||
|
outStr += "\\\"";
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
outStr += "\\n";
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
outStr += "\\r";
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
outStr += "\\t";
|
||||||
|
break;
|
||||||
|
case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08: /* \\t */ /* \\n */ case 0x0b: case 0x0c: /* \\r */ case 0x0e: case 0x0f:
|
||||||
|
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
|
||||||
|
uval[4] = '0'+(*str >= 0x10);
|
||||||
|
uval[5] = "0123456789abcdef"[*str&0x0f];
|
||||||
|
outStr += uval;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
outStr.push_back(*str);
|
||||||
|
}
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char * imageTypeString(ImageType type) {
|
||||||
|
switch (type) {
|
||||||
|
case ImageType::HARD_MASK:
|
||||||
|
return "hardmask";
|
||||||
|
case ImageType::SOFT_MASK:
|
||||||
|
return "softmask";
|
||||||
|
case ImageType::SDF:
|
||||||
|
return "sdf";
|
||||||
|
case ImageType::PSDF:
|
||||||
|
return "psdf";
|
||||||
|
case ImageType::MSDF:
|
||||||
|
return "msdf";
|
||||||
|
case ImageType::MTSDF:
|
||||||
|
return "mtsdf";
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool exportJSON(const FontGeometry *fonts, int fontCount, double fontSize, double pxRange, int atlasWidth, int atlasHeight, ImageType imageType, YDirection yDirection, const char *filename, bool kerning) {
|
||||||
|
FILE *f = fopen(filename, "w");
|
||||||
|
if (!f)
|
||||||
|
return false;
|
||||||
|
fputs("{", f);
|
||||||
|
|
||||||
|
// Atlas properties
|
||||||
|
fputs("\"atlas\":{", f); {
|
||||||
|
fprintf(f, "\"type\":\"%s\",", imageTypeString(imageType));
|
||||||
|
if (imageType == ImageType::SDF || imageType == ImageType::PSDF || imageType == ImageType::MSDF || imageType == ImageType::MTSDF)
|
||||||
|
fprintf(f, "\"distanceRange\":%.17g,", pxRange);
|
||||||
|
fprintf(f, "\"size\":%.17g,", fontSize);
|
||||||
|
fprintf(f, "\"width\":%d,", atlasWidth);
|
||||||
|
fprintf(f, "\"height\":%d,", atlasHeight);
|
||||||
|
fprintf(f, "\"yOrigin\":\"%s\"", yDirection == YDirection::TOP_DOWN ? "top" : "bottom");
|
||||||
|
} fputs("},", f);
|
||||||
|
|
||||||
|
if (fontCount > 1)
|
||||||
|
fputs("\"variants\":[", f);
|
||||||
|
for (int i = 0; i < fontCount; ++i) {
|
||||||
|
const FontGeometry &font = fonts[i];
|
||||||
|
if (fontCount > 1)
|
||||||
|
fputs(i == 0 ? "{" : ",{", f);
|
||||||
|
|
||||||
|
// Font name
|
||||||
|
const char *name = font.getName();
|
||||||
|
if (name)
|
||||||
|
fprintf(f, "\"name\":\"%s\",", escapeJsonString(name).c_str());
|
||||||
|
|
||||||
|
// Font metrics
|
||||||
|
fputs("\"metrics\":{", f); {
|
||||||
|
double yFactor = yDirection == YDirection::TOP_DOWN ? -1 : 1;
|
||||||
|
const msdfgen::FontMetrics &metrics = font.getMetrics();
|
||||||
|
fprintf(f, "\"emSize\":%.17g,", metrics.emSize);
|
||||||
|
fprintf(f, "\"lineHeight\":%.17g,", metrics.lineHeight);
|
||||||
|
fprintf(f, "\"ascender\":%.17g,", yFactor*metrics.ascenderY);
|
||||||
|
fprintf(f, "\"descender\":%.17g,", yFactor*metrics.descenderY);
|
||||||
|
fprintf(f, "\"underlineY\":%.17g,", yFactor*metrics.underlineY);
|
||||||
|
fprintf(f, "\"underlineThickness\":%.17g", metrics.underlineThickness);
|
||||||
|
} fputs("},", f);
|
||||||
|
|
||||||
|
// Glyph mapping
|
||||||
|
fputs("\"glyphs\":[", f);
|
||||||
|
bool firstGlyph = true;
|
||||||
|
for (const GlyphGeometry &glyph : font.getGlyphs()) {
|
||||||
|
fputs(firstGlyph ? "{" : ",{", f);
|
||||||
|
switch (font.getPreferredIdentifierType()) {
|
||||||
|
case GlyphIdentifierType::GLYPH_INDEX:
|
||||||
|
fprintf(f, "\"index\":%d,", glyph.getIndex());
|
||||||
|
break;
|
||||||
|
case GlyphIdentifierType::UNICODE_CODEPOINT:
|
||||||
|
fprintf(f, "\"unicode\":%u,", glyph.getCodepoint());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fprintf(f, "\"advance\":%.17g", glyph.getAdvance());
|
||||||
|
double l, b, r, t;
|
||||||
|
glyph.getQuadPlaneBounds(l, b, r, t);
|
||||||
|
if (l || b || r || t) {
|
||||||
|
switch (yDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
fprintf(f, ",\"planeBounds\":{\"left\":%.17g,\"bottom\":%.17g,\"right\":%.17g,\"top\":%.17g}", l, b, r, t);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
fprintf(f, ",\"planeBounds\":{\"left\":%.17g,\"top\":%.17g,\"right\":%.17g,\"bottom\":%.17g}", l, -t, r, -b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
glyph.getQuadAtlasBounds(l, b, r, t);
|
||||||
|
if (l || b || r || t) {
|
||||||
|
switch (yDirection) {
|
||||||
|
case YDirection::BOTTOM_UP:
|
||||||
|
fprintf(f, ",\"atlasBounds\":{\"left\":%.17g,\"bottom\":%.17g,\"right\":%.17g,\"top\":%.17g}", l, b, r, t);
|
||||||
|
break;
|
||||||
|
case YDirection::TOP_DOWN:
|
||||||
|
fprintf(f, ",\"atlasBounds\":{\"left\":%.17g,\"top\":%.17g,\"right\":%.17g,\"bottom\":%.17g}", l, atlasHeight-t, r, atlasHeight-b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fputs("}", f);
|
||||||
|
firstGlyph = false;
|
||||||
|
} fputs("]", f);
|
||||||
|
|
||||||
|
// Kerning pairs
|
||||||
|
if (kerning) {
|
||||||
|
fputs(",\"kerning\":[", f);
|
||||||
|
bool firstPair = true;
|
||||||
|
switch (font.getPreferredIdentifierType()) {
|
||||||
|
case GlyphIdentifierType::GLYPH_INDEX:
|
||||||
|
for (const std::pair<std::pair<int, int>, double> &kernPair : font.getKerning()) {
|
||||||
|
fputs(firstPair ? "{" : ",{", f);
|
||||||
|
fprintf(f, "\"index1\":%d,", kernPair.first.first);
|
||||||
|
fprintf(f, "\"index2\":%d,", kernPair.first.second);
|
||||||
|
fprintf(f, "\"advance\":%.17g", kernPair.second);
|
||||||
|
fputs("}", f);
|
||||||
|
firstPair = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case GlyphIdentifierType::UNICODE_CODEPOINT:
|
||||||
|
for (const std::pair<std::pair<int, int>, double> &kernPair : font.getKerning()) {
|
||||||
|
const GlyphGeometry *glyph1 = font.getGlyph(msdfgen::GlyphIndex(kernPair.first.first));
|
||||||
|
const GlyphGeometry *glyph2 = font.getGlyph(msdfgen::GlyphIndex(kernPair.first.second));
|
||||||
|
if (glyph1 && glyph2 && glyph1->getCodepoint() && glyph2->getCodepoint()) {
|
||||||
|
fputs(firstPair ? "{" : ",{", f);
|
||||||
|
fprintf(f, "\"unicode1\":%u,", glyph1->getCodepoint());
|
||||||
|
fprintf(f, "\"unicode2\":%u,", glyph2->getCodepoint());
|
||||||
|
fprintf(f, "\"advance\":%.17g", kernPair.second);
|
||||||
|
fputs("}", f);
|
||||||
|
firstPair = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} fputs("]", f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fontCount > 1)
|
||||||
|
fputs("}", f);
|
||||||
|
}
|
||||||
|
if (fontCount > 1)
|
||||||
|
fputs("]", f);
|
||||||
|
|
||||||
|
fputs("}\n", f);
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/json-export.h
Normal file
14
libs/msdf-atlas-gen/include/msdf-atlas-gen/json-export.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Writes the font and glyph metrics and atlas layout data into a comprehensive JSON file
|
||||||
|
bool exportJSON(const FontGeometry *fonts, int fontCount, double fontSize, double pxRange, int atlasWidth, int atlasHeight, ImageType imageType, YDirection yDirection, const char *filename, bool kerning);
|
||||||
|
|
||||||
|
}
|
1064
libs/msdf-atlas-gen/include/msdf-atlas-gen/main.cpp
Normal file
1064
libs/msdf-atlas-gen/include/msdf-atlas-gen/main.cpp
Normal file
File diff suppressed because it is too large
Load diff
38
libs/msdf-atlas-gen/include/msdf-atlas-gen/msdf-atlas-gen.h
Normal file
38
libs/msdf-atlas-gen/include/msdf-atlas-gen/msdf-atlas-gen.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MULTI-CHANNEL SIGNED DISTANCE FIELD ATLAS GENERATOR
|
||||||
|
* ---------------------------------------------------
|
||||||
|
* A utility by Viktor Chlumsky, (c) 2020 - 2023
|
||||||
|
* Generates compact bitmap font atlases using MSDFgen
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "utf8.h"
|
||||||
|
#include "Rectangle.h"
|
||||||
|
#include "Charset.h"
|
||||||
|
#include "GlyphBox.h"
|
||||||
|
#include "GlyphGeometry.h"
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
#include "RectanglePacker.h"
|
||||||
|
#include "rectangle-packing.h"
|
||||||
|
#include "Workload.h"
|
||||||
|
#include "size-selectors.h"
|
||||||
|
#include "bitmap-blit.h"
|
||||||
|
#include "AtlasStorage.h"
|
||||||
|
#include "BitmapAtlasStorage.h"
|
||||||
|
#include "TightAtlasPacker.h"
|
||||||
|
#include "AtlasGenerator.h"
|
||||||
|
#include "ImmediateAtlasGenerator.h"
|
||||||
|
#include "DynamicAtlas.h"
|
||||||
|
#include "glyph-generators.h"
|
||||||
|
#include "image-encode.h"
|
||||||
|
#include "image-save.h"
|
||||||
|
#include "artery-font-export.h"
|
||||||
|
#include "csv-export.h"
|
||||||
|
#include "json-export.h"
|
||||||
|
#include "shadron-preview-generator.h"
|
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include "Rectangle.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Packs the rectangle array into an atlas with fixed dimensions, returns how many didn't fit (0 on success)
|
||||||
|
template <typename RectangleType>
|
||||||
|
int packRectangles(RectangleType *rectangles, int count, int width, int height, int padding = 0);
|
||||||
|
|
||||||
|
/// Packs the rectangle array into an atlas of unknown size, returns the minimum required dimensions constrained by SizeSelector
|
||||||
|
template <class SizeSelector, typename RectangleType>
|
||||||
|
std::pair<int, int> packRectangles(RectangleType *rectangles, int count, int padding = 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "rectangle-packing.hpp"
|
|
@ -0,0 +1,61 @@
|
||||||
|
|
||||||
|
#include "rectangle-packing.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "RectanglePacker.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static void copyRectanglePlacement(Rectangle &dst, const Rectangle &src) {
|
||||||
|
dst.x = src.x;
|
||||||
|
dst.y = src.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copyRectanglePlacement(OrientedRectangle &dst, const OrientedRectangle &src) {
|
||||||
|
dst.x = src.x;
|
||||||
|
dst.y = src.y;
|
||||||
|
dst.rotated = src.rotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename RectangleType>
|
||||||
|
int packRectangles(RectangleType *rectangles, int count, int width, int height, int padding) {
|
||||||
|
if (padding)
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
rectangles[i].w += padding;
|
||||||
|
rectangles[i].h += padding;
|
||||||
|
}
|
||||||
|
int result = RectanglePacker(width+padding, height+padding).pack(rectangles, count);
|
||||||
|
if (padding)
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
rectangles[i].w -= padding;
|
||||||
|
rectangles[i].h -= padding;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class SizeSelector, typename RectangleType>
|
||||||
|
std::pair<int, int> packRectangles(RectangleType *rectangles, int count, int padding) {
|
||||||
|
std::vector<RectangleType> rectanglesCopy(count);
|
||||||
|
int totalArea = 0;
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
rectanglesCopy[i].w = rectangles[i].w+padding;
|
||||||
|
rectanglesCopy[i].h = rectangles[i].h+padding;
|
||||||
|
totalArea += rectangles[i].w*rectangles[i].h;
|
||||||
|
}
|
||||||
|
std::pair<int, int> dimensions;
|
||||||
|
SizeSelector sizeSelector(totalArea);
|
||||||
|
int width, height;
|
||||||
|
while (sizeSelector(width, height)) {
|
||||||
|
if (!RectanglePacker(width+padding, height+padding).pack(rectanglesCopy.data(), count)) {
|
||||||
|
dimensions.first = width;
|
||||||
|
dimensions.second = height;
|
||||||
|
for (int i = 0; i < count; ++i)
|
||||||
|
copyRectanglePlacement(rectangles[i], rectanglesCopy[i]);
|
||||||
|
--sizeSelector;
|
||||||
|
} else
|
||||||
|
++sizeSelector;
|
||||||
|
}
|
||||||
|
return dimensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,172 @@
|
||||||
|
|
||||||
|
#include "shadron-preview-generator.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
static const char * const shadronFillGlyphMask = R"(
|
||||||
|
template <ATLAS, RANGE, COLOR>
|
||||||
|
glsl vec4 fillGlyph(vec2 texCoord) {
|
||||||
|
float fill = texture((ATLAS), texCoord).r;
|
||||||
|
return vec4(vec3(COLOR), fill);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char * const shadronFillGlyphSdf = R"(
|
||||||
|
template <ATLAS, RANGE, COLOR>
|
||||||
|
glsl vec4 fillGlyph(vec2 texCoord) {
|
||||||
|
vec3 s = texture((ATLAS), texCoord).rgb;
|
||||||
|
float sd = dot(vec2(RANGE), 0.5/fwidth(texCoord))*(median(s.r, s.g, s.b)-0.5);
|
||||||
|
float fill = clamp(sd+0.5, 0.0, 1.0);
|
||||||
|
return vec4(vec3(COLOR), fill);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
static const char * const shadronPreviewPreamble = R"(
|
||||||
|
#include <median>
|
||||||
|
|
||||||
|
glsl struct GlyphVertex {
|
||||||
|
vec2 coord;
|
||||||
|
vec2 texCoord;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <TEXT_SIZE>
|
||||||
|
glsl vec4 projectVertex(out vec2 texCoord, in GlyphVertex vertex) {
|
||||||
|
vec2 coord = vertex.coord;
|
||||||
|
float scale = 2.0/max((TEXT_SIZE).x, shadron_Aspect*(TEXT_SIZE).y);
|
||||||
|
scale *= exp(0.0625*shadron_Mouse.z);
|
||||||
|
coord += vec2(-0.5, 0.5)*vec2(TEXT_SIZE);
|
||||||
|
coord *= scale*vec2(1.0, shadron_Aspect);
|
||||||
|
texCoord = vertex.texCoord;
|
||||||
|
return vec4(coord, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
%s
|
||||||
|
#define PREVIEW_IMAGE(NAME, ATLAS, RANGE, COLOR, VERTEX_LIST, TEXT_SIZE, DIMENSIONS) model image NAME : \
|
||||||
|
vertex_data(GlyphVertex), \
|
||||||
|
fragment_data(vec2), \
|
||||||
|
vertex(projectVertex<TEXT_SIZE>, triangles, VERTEX_LIST), \
|
||||||
|
fragment(fillGlyph<ATLAS, RANGE, COLOR>), \
|
||||||
|
depth(false), \
|
||||||
|
blend(transparency), \
|
||||||
|
background(vec4(vec3(COLOR), 0.0)), \
|
||||||
|
dimensions(DIMENSIONS), \
|
||||||
|
resizable(true)
|
||||||
|
|
||||||
|
)";
|
||||||
|
|
||||||
|
static std::string relativizePath(const char *base, const char *target) {
|
||||||
|
if (target[0] == '/' || (target[0] && target[1] == ':')) // absolute path?
|
||||||
|
return target;
|
||||||
|
int commonPrefix = 0;
|
||||||
|
for (int i = 0; base[i] && target[i] && base[i] == target[i]; ++i) {
|
||||||
|
if (base[i] == '/' || base[i] == '\\')
|
||||||
|
commonPrefix = i+1;
|
||||||
|
}
|
||||||
|
base += commonPrefix;
|
||||||
|
target += commonPrefix;
|
||||||
|
int baseNesting = 0;
|
||||||
|
for (int i = 0; base[i]; ++i)
|
||||||
|
if (base[i] == '/' || base[i] == '\\')
|
||||||
|
++baseNesting;
|
||||||
|
std::string output;
|
||||||
|
for (int i = 0; i < baseNesting; ++i)
|
||||||
|
output += "../";
|
||||||
|
output += target;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string escapeString(const std::string &str) {
|
||||||
|
std::string output;
|
||||||
|
for (int i = 0; i < (int) str.size(); ++i) {
|
||||||
|
switch (str[i]) {
|
||||||
|
case '"':
|
||||||
|
output += "\\\"";
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
output += "\\\\";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
output.push_back(str[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool generateShadronPreview(const FontGeometry *fonts, int fontCount, ImageType atlasType, int atlasWidth, int atlasHeight, double pxRange, const unicode_t *text, const char *imageFilename, bool fullRange, const char *outputFilename) {
|
||||||
|
if (fontCount <= 0)
|
||||||
|
return false;
|
||||||
|
double texelWidth = 1./atlasWidth;
|
||||||
|
double texelHeight = 1./atlasHeight;
|
||||||
|
bool anyGlyphs = false;
|
||||||
|
FILE *file = fopen(outputFilename, "w");
|
||||||
|
if (!file)
|
||||||
|
return false;
|
||||||
|
fprintf(file, shadronPreviewPreamble, atlasType == ImageType::HARD_MASK || atlasType == ImageType::SOFT_MASK ? shadronFillGlyphMask : shadronFillGlyphSdf);
|
||||||
|
if (imageFilename)
|
||||||
|
fprintf(file, "image Atlas = file(\"%s\")", escapeString(relativizePath(outputFilename, imageFilename)).c_str());
|
||||||
|
else
|
||||||
|
fprintf(file, "image Atlas = file()");
|
||||||
|
fprintf(file, " : %sfilter(%s), map(repeat);\n", fullRange ? "full_range(true), " : "", atlasType == ImageType::HARD_MASK ? "nearest" : "linear");
|
||||||
|
fprintf(file, "const vec2 txRange = vec2(%.9g, %.9g);\n\n", pxRange*texelWidth, pxRange*texelHeight);
|
||||||
|
{
|
||||||
|
msdfgen::FontMetrics fontMetrics = fonts->getMetrics();
|
||||||
|
for (int i = 1; i < fontCount; ++i) {
|
||||||
|
fontMetrics.lineHeight = std::max(fontMetrics.lineHeight, fonts[i].getMetrics().lineHeight);
|
||||||
|
fontMetrics.ascenderY = std::max(fontMetrics.ascenderY, fonts[i].getMetrics().ascenderY);
|
||||||
|
fontMetrics.descenderY = std::min(fontMetrics.descenderY, fonts[i].getMetrics().descenderY);
|
||||||
|
}
|
||||||
|
double fsScale = 1/(fontMetrics.ascenderY-fontMetrics.descenderY);
|
||||||
|
fputs("vertex_list GlyphVertex textQuadVertices = {\n", file);
|
||||||
|
double x = 0, y = -fsScale*fontMetrics.ascenderY;
|
||||||
|
double textWidth = 0;
|
||||||
|
for (const unicode_t *cp = text; *cp; ++cp) {
|
||||||
|
if (*cp == '\r')
|
||||||
|
continue;
|
||||||
|
if (*cp == '\n') {
|
||||||
|
textWidth = std::max(textWidth, x);
|
||||||
|
x = 0;
|
||||||
|
y -= fsScale*fontMetrics.lineHeight;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < fontCount; ++i) {
|
||||||
|
const GlyphGeometry *glyph = fonts[i].getGlyph(*cp);
|
||||||
|
if (glyph) {
|
||||||
|
if (!glyph->isWhitespace()) {
|
||||||
|
double pl, pb, pr, pt;
|
||||||
|
double il, ib, ir, it;
|
||||||
|
glyph->getQuadPlaneBounds(pl, pb, pr, pt);
|
||||||
|
glyph->getQuadAtlasBounds(il, ib, ir, it);
|
||||||
|
pl *= fsScale, pb *= fsScale, pr *= fsScale, pt *= fsScale;
|
||||||
|
pl += x, pb += y, pr += x, pt += y;
|
||||||
|
il *= texelWidth, ib *= texelHeight, ir *= texelWidth, it *= texelHeight;
|
||||||
|
fprintf(file, " %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g, %.9g,\n",
|
||||||
|
pl, pb, il, ib,
|
||||||
|
pr, pb, ir, ib,
|
||||||
|
pl, pt, il, it,
|
||||||
|
pr, pt, ir, it,
|
||||||
|
pl, pt, il, it,
|
||||||
|
pr, pb, ir, ib
|
||||||
|
);
|
||||||
|
}
|
||||||
|
double advance = glyph->getAdvance();
|
||||||
|
fonts[i].getAdvance(advance, cp[0], cp[1]);
|
||||||
|
x += fsScale*advance;
|
||||||
|
anyGlyphs = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
textWidth = std::max(textWidth, x);
|
||||||
|
y += fsScale*fontMetrics.descenderY;
|
||||||
|
fputs("};\n", file);
|
||||||
|
fprintf(file, "const vec2 textSize = vec2(%.9g, %.9g);\n\n", textWidth, -y);
|
||||||
|
}
|
||||||
|
fputs("PREVIEW_IMAGE(Preview, Atlas, txRange, vec3(1.0), textQuadVertices, textSize, ivec2(1200, 400));\n", file);
|
||||||
|
fputs("export png(Preview, \"preview.png\");\n", file);
|
||||||
|
fclose(file);
|
||||||
|
return anyGlyphs;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <msdfgen.h>
|
||||||
|
#include <msdfgen-ext.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "FontGeometry.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Generates a Shadron script that displays a string using the generated atlas
|
||||||
|
bool generateShadronPreview(const FontGeometry *fonts, int fontCount, ImageType atlasType, int atlasWidth, int atlasHeight, double pxRange, const unicode_t *text, const char *imageFilename, bool fullRange, const char *outputFilename);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
|
||||||
|
#include "size-selectors.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
template <int MULTIPLE>
|
||||||
|
SquareSizeSelector<MULTIPLE>::SquareSizeSelector(int minArea) : lowerBound(0), upperBound(-1) {
|
||||||
|
if (minArea > 0)
|
||||||
|
lowerBound = int(sqrt(minArea-1))/MULTIPLE+1;
|
||||||
|
updateCurrent();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int MULTIPLE>
|
||||||
|
void SquareSizeSelector<MULTIPLE>::updateCurrent() {
|
||||||
|
if (upperBound < 0)
|
||||||
|
current = 5*lowerBound/4+16/MULTIPLE;
|
||||||
|
else
|
||||||
|
current = lowerBound+(upperBound-lowerBound)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int MULTIPLE>
|
||||||
|
bool SquareSizeSelector<MULTIPLE>::operator()(int &width, int &height) const {
|
||||||
|
width = MULTIPLE*current, height = MULTIPLE*current;
|
||||||
|
return lowerBound < upperBound || upperBound < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int MULTIPLE>
|
||||||
|
SquareSizeSelector<MULTIPLE> & SquareSizeSelector<MULTIPLE>::operator++() {
|
||||||
|
lowerBound = current+1;
|
||||||
|
updateCurrent();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int MULTIPLE>
|
||||||
|
SquareSizeSelector<MULTIPLE> & SquareSizeSelector<MULTIPLE>::operator--() {
|
||||||
|
upperBound = current;
|
||||||
|
updateCurrent();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template class SquareSizeSelector<1>;
|
||||||
|
template class SquareSizeSelector<2>;
|
||||||
|
template class SquareSizeSelector<4>;
|
||||||
|
|
||||||
|
SquarePowerOfTwoSizeSelector::SquarePowerOfTwoSizeSelector(int minArea) : side(1) {
|
||||||
|
while (side*side < minArea)
|
||||||
|
side <<= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SquarePowerOfTwoSizeSelector::operator()(int &width, int &height) const {
|
||||||
|
width = side, height = side;
|
||||||
|
return side > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SquarePowerOfTwoSizeSelector & SquarePowerOfTwoSizeSelector::operator++() {
|
||||||
|
side <<= 1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
SquarePowerOfTwoSizeSelector & SquarePowerOfTwoSizeSelector::operator--() {
|
||||||
|
side = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PowerOfTwoSizeSelector::PowerOfTwoSizeSelector(int minArea) : w(1), h(1) {
|
||||||
|
while (w*h < minArea)
|
||||||
|
++*this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PowerOfTwoSizeSelector::operator()(int &width, int &height) const {
|
||||||
|
width = w, height = h;
|
||||||
|
return w > 0 && h > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PowerOfTwoSizeSelector & PowerOfTwoSizeSelector::operator++() {
|
||||||
|
if (w == h)
|
||||||
|
w <<= 1;
|
||||||
|
else
|
||||||
|
h = w;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PowerOfTwoSizeSelector & PowerOfTwoSizeSelector::operator--() {
|
||||||
|
w = 0, h = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
54
libs/msdf-atlas-gen/include/msdf-atlas-gen/size-selectors.h
Normal file
54
libs/msdf-atlas-gen/include/msdf-atlas-gen/size-selectors.h
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
// The size selector classes are used to select the minimum dimensions of the atlas fitting a given constraint.
|
||||||
|
|
||||||
|
/// Selects square dimensions which are also a multiple of MULTIPLE
|
||||||
|
template <int MULTIPLE = 1>
|
||||||
|
class SquareSizeSelector {
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit SquareSizeSelector(int minArea = 0);
|
||||||
|
bool operator()(int &width, int &height) const;
|
||||||
|
SquareSizeSelector<MULTIPLE> & operator++();
|
||||||
|
SquareSizeSelector<MULTIPLE> & operator--();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int lowerBound, upperBound;
|
||||||
|
int current;
|
||||||
|
|
||||||
|
void updateCurrent();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Selects square power-of-two dimensions
|
||||||
|
class SquarePowerOfTwoSizeSelector {
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit SquarePowerOfTwoSizeSelector(int minArea = 0);
|
||||||
|
bool operator()(int &width, int &height) const;
|
||||||
|
SquarePowerOfTwoSizeSelector & operator++();
|
||||||
|
SquarePowerOfTwoSizeSelector & operator--();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int side;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Selects square or rectangular (2:1) power-of-two dimensions
|
||||||
|
class PowerOfTwoSizeSelector {
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit PowerOfTwoSizeSelector(int minArea = 0);
|
||||||
|
bool operator()(int &width, int &height) const;
|
||||||
|
PowerOfTwoSizeSelector & operator++();
|
||||||
|
PowerOfTwoSizeSelector & operator--();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int w, h;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
52
libs/msdf-atlas-gen/include/msdf-atlas-gen/types.h
Normal file
52
libs/msdf-atlas-gen/include/msdf-atlas-gen/types.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
typedef unsigned char byte;
|
||||||
|
typedef uint32_t unicode_t;
|
||||||
|
|
||||||
|
/// Type of atlas image contents
|
||||||
|
enum class ImageType {
|
||||||
|
/// Rendered glyphs without anti-aliasing (two colors only)
|
||||||
|
HARD_MASK,
|
||||||
|
/// Rendered glyphs with anti-aliasing
|
||||||
|
SOFT_MASK,
|
||||||
|
/// Signed (true) distance field
|
||||||
|
SDF,
|
||||||
|
/// Signed pseudo-distance field
|
||||||
|
PSDF,
|
||||||
|
/// Multi-channel signed distance field
|
||||||
|
MSDF,
|
||||||
|
/// Multi-channel & true signed distance field
|
||||||
|
MTSDF
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Atlas image encoding
|
||||||
|
enum class ImageFormat {
|
||||||
|
UNSPECIFIED,
|
||||||
|
PNG,
|
||||||
|
BMP,
|
||||||
|
TIFF,
|
||||||
|
TEXT,
|
||||||
|
TEXT_FLOAT,
|
||||||
|
BINARY,
|
||||||
|
BINARY_FLOAT,
|
||||||
|
BINARY_FLOAT_BE
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Glyph identification
|
||||||
|
enum class GlyphIdentifierType {
|
||||||
|
GLYPH_INDEX,
|
||||||
|
UNICODE_CODEPOINT
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Direction of the Y-axis
|
||||||
|
enum class YDirection {
|
||||||
|
BOTTOM_UP,
|
||||||
|
TOP_DOWN
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
38
libs/msdf-atlas-gen/include/msdf-atlas-gen/utf8.cpp
Normal file
38
libs/msdf-atlas-gen/include/msdf-atlas-gen/utf8.cpp
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
#include "utf8.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
void utf8Decode(std::vector<unicode_t> &codepoints, const char *utf8String) {
|
||||||
|
bool start = true;
|
||||||
|
int rBytes = 0;
|
||||||
|
unicode_t cp = 0;
|
||||||
|
|
||||||
|
for (const char *c = utf8String; *c; ++c) {
|
||||||
|
if (rBytes > 0) {
|
||||||
|
--rBytes;
|
||||||
|
if ((*c&0xc0) == 0x80)
|
||||||
|
cp |= (*c&0x3f)<<(6*rBytes);
|
||||||
|
// else error
|
||||||
|
} else if (!(*c&0x80)) {
|
||||||
|
cp = *c;
|
||||||
|
rBytes = 0;
|
||||||
|
} else if (*c&0x40) {
|
||||||
|
int block;
|
||||||
|
for (block = 0; ((unsigned char) *c<<block)&0x40 && block < 4; ++block);
|
||||||
|
if (block < 4) {
|
||||||
|
cp = (*c&(0x3f>>block))<<(6*block);
|
||||||
|
rBytes = block;
|
||||||
|
} else
|
||||||
|
continue; // error
|
||||||
|
} else
|
||||||
|
continue; // error
|
||||||
|
if (!rBytes) {
|
||||||
|
if (!(start && cp == 0xfeff)) // BOM
|
||||||
|
codepoints.push_back(cp);
|
||||||
|
start = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
12
libs/msdf-atlas-gen/include/msdf-atlas-gen/utf8.h
Normal file
12
libs/msdf-atlas-gen/include/msdf-atlas-gen/utf8.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace msdf_atlas {
|
||||||
|
|
||||||
|
/// Decodes the UTF-8 string into an array of Unicode codepoints
|
||||||
|
void utf8Decode(std::vector<unicode_t> &codepoints, const char *utf8String);
|
||||||
|
|
||||||
|
}
|
1
libs/msdf-atlas-gen/include/msdfgen/.gitattributes
vendored
Normal file
1
libs/msdf-atlas-gen/include/msdfgen/.gitattributes
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
* text=auto
|
30
libs/msdf-atlas-gen/include/msdfgen/.gitignore
vendored
Normal file
30
libs/msdf-atlas-gen/include/msdfgen/.gitignore
vendored
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/build/
|
||||||
|
/Debug/
|
||||||
|
/Release/
|
||||||
|
/Release OpenMP/
|
||||||
|
/Debug Library/
|
||||||
|
/Release Library/
|
||||||
|
/Release Library OpenMP/
|
||||||
|
/x86/
|
||||||
|
/x64/
|
||||||
|
.vs/
|
||||||
|
.vscode/
|
||||||
|
.DS_Store
|
||||||
|
*.exe
|
||||||
|
*.zip
|
||||||
|
*.user
|
||||||
|
*.sdf
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.iobj
|
||||||
|
*.suo
|
||||||
|
*.VC.opendb
|
||||||
|
*.VC.db
|
||||||
|
/bin/*.lib
|
||||||
|
/bin/msdfgen
|
||||||
|
output.png
|
||||||
|
render.png
|
||||||
|
out/
|
||||||
|
/build_xcode/
|
||||||
|
/cmake-gen.bat
|
||||||
|
/line-ending-check.bat
|
99
libs/msdf-atlas-gen/include/msdfgen/CHANGELOG.md
Normal file
99
libs/msdf-atlas-gen/include/msdfgen/CHANGELOG.md
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
|
||||||
|
## Version 1.10 (2023-01-15)
|
||||||
|
|
||||||
|
- Switched to vcpkg as the primary dependency management system
|
||||||
|
- Switched to libpng as the primary PNG file encoder
|
||||||
|
- Parameters of variable fonts can be specified
|
||||||
|
- Fixed a bug that prevented glyph 0 to be specified in a glyphset
|
||||||
|
|
||||||
|
### Version 1.9.2 (2021-12-01)
|
||||||
|
|
||||||
|
- Improved detection of numerical errors in cubic equation solver
|
||||||
|
- Added -windingpreprocess option
|
||||||
|
- Fixed edge coloring not restored if lost during preprocessing
|
||||||
|
|
||||||
|
### Version 1.9.1 (2021-07-09)
|
||||||
|
|
||||||
|
- Fixed an edge case bug in the new MSDF error correction algorithm
|
||||||
|
|
||||||
|
## Version 1.9 (2021-05-28)
|
||||||
|
|
||||||
|
- Error correction of multi-channel distance fields has been completely reworked
|
||||||
|
- Added new edge coloring strategy that optimizes colors based on distances between edges
|
||||||
|
- Added some minor functions for the library API
|
||||||
|
- Minor code refactor and optimizations
|
||||||
|
|
||||||
|
## Version 1.8 (2020-10-17)
|
||||||
|
|
||||||
|
- Integrated the Skia library into the project, which is used to preprocess the shape geometry and eliminate any self-intersections and other irregularities previously unsupported by the software
|
||||||
|
- The scanline pass and overlapping contour mode is made obsolete by this step and has been disabled by default. The preprocess step can be disabled by the new `-nopreprocess` switch and the former enabled by `-scanline` and `-overlap` respectively.
|
||||||
|
- The project can be built without the Skia library, forgoing the geometry preprocessing feature. This is controlled by the macro definition `MSDFGEN_USE_SKIA`
|
||||||
|
- Significantly improved performance of the core algorithm by reusing results from previously computed pixels
|
||||||
|
- Introduced an additional error correction routine which eliminates MSDF artifacts by analytically predicting results of bilinear interpolation
|
||||||
|
- Added the possibility to load font glyphs by their index rather than a Unicode value (use the prefix `g` before the character code in `-font` argument)
|
||||||
|
- Added `-distanceshift` argument that can be used to adjust the center of the distance range in the output distance field
|
||||||
|
- Fixed several errors in the evaluation of curve distances
|
||||||
|
- Fixed an issue with paths containing convergent corners (those whose inner angle is zero)
|
||||||
|
- The algorithm for pseudo-distance computation slightly changed, fixing certain rare edge cases and improving consistency
|
||||||
|
- Added the ability to supply own `FT_Face` handle to the msdfgen library
|
||||||
|
- Minor refactor of the core algorithm
|
||||||
|
|
||||||
|
### Version 1.7.1 (2020-03-09)
|
||||||
|
|
||||||
|
- Fixed an edge case bug in scanline rasterization
|
||||||
|
|
||||||
|
## Version 1.7 (2020-03-07)
|
||||||
|
|
||||||
|
- Added `mtsdf` mode - a combination of `msdf` with `sdf` in the alpha channel
|
||||||
|
- Distance fields can now be stored as uncompressed TIFF image files with floating point precision
|
||||||
|
- Bitmap class refactor - template argument split into data type and number of channels, bitmap reference classes introduced
|
||||||
|
- Added a secondary "ink trap" edge coloring heuristic, can be selected using `-coloringstrategy inktrap`
|
||||||
|
- Added computation of estimated rendering error for a given SDF
|
||||||
|
- Added computation of bounding box that includes sharp mitered corners
|
||||||
|
- The API for bounds computation of the `Shape` class changed for clarity
|
||||||
|
- Fixed several edge case bugs
|
||||||
|
|
||||||
|
## Version 1.6 (2019-04-08)
|
||||||
|
|
||||||
|
- Core algorithm rewritten to split up advanced edge selection logic into modular template arguments.
|
||||||
|
- Pseudo-distance evaluation reworked to eliminate discontinuities at the midpoint between edges.
|
||||||
|
- MSDF error correction reworked to also fix distances away from edges and consider diagonal pairs. Code simplified.
|
||||||
|
- Added scanline rasterization support for `Shape`.
|
||||||
|
- Added a scanline pass in the standalone version, which corrects the signs in the distance field according to the selected fill rule (`-fillrule`). Can be disabled using `-noscanline`.
|
||||||
|
- Fixed autoframe scaling, which previously caused the output to have unnecessary empty border.
|
||||||
|
- `-guessorder` switch no longer enabled by default, as the functionality is now provided by the scanline pass.
|
||||||
|
- Updated FreeType and other libraries, changed to static linkage
|
||||||
|
- Added 64-bit and static library builds to the Visual Studio solution
|
||||||
|
|
||||||
|
## Version 1.5 (2017-07-23)
|
||||||
|
|
||||||
|
- Fixed rounding error in cubic curve splitting.
|
||||||
|
- SVG parser fixes and support for additional path commands.
|
||||||
|
- Added CMake build script.
|
||||||
|
|
||||||
|
## Version 1.4 (2017-02-09)
|
||||||
|
|
||||||
|
- Reworked contour combining logic to support overlapping contours. Original algorithm preserved in functions with `_legacy` suffix, which are invoked by the new `-legacy` switch.
|
||||||
|
- Fixed a severe bug in cubic curve distance computation, where a control point lies at the endpoint.
|
||||||
|
- Standalone version now automatically detects if the input has the wrong orientation and adjusts the distance field accordingly. Can be disabled by `-keeporder` or `-reverseorder` switch.
|
||||||
|
- SVG parser fixes and improvements.
|
||||||
|
|
||||||
|
## Version 1.3 (2016-12-07)
|
||||||
|
|
||||||
|
- Fixed `-reverseorder` switch.
|
||||||
|
- Fixed glyph loading to use the proper method of acquiring outlines from FreeType.
|
||||||
|
|
||||||
|
## Version 1.2 (2016-07-20)
|
||||||
|
|
||||||
|
- Added option to specify that shape vertices are listed in reverse order (`-reverseorder`).
|
||||||
|
- Added option to set a seed for the edge coloring heuristic (-seed \<n\>), which can be used to adjust the output.
|
||||||
|
- Fixed parsing of glyph contours that start with a curve control point.
|
||||||
|
|
||||||
|
## Version 1.1 (2016-05-08)
|
||||||
|
|
||||||
|
- Switched to MIT license due to popular demand.
|
||||||
|
- Fixed SDF rendering anti-aliasing when the output is smaller than the distance field.
|
||||||
|
|
||||||
|
## Version 1.0 (2016-04-28)
|
||||||
|
|
||||||
|
- Project published.
|
262
libs/msdf-atlas-gen/include/msdfgen/CMakeLists.txt
Normal file
262
libs/msdf-atlas-gen/include/msdfgen/CMakeLists.txt
Normal file
|
@ -0,0 +1,262 @@
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
include(cmake/version.cmake)
|
||||||
|
|
||||||
|
option(MSDFGEN_CORE_ONLY "Only build the core library with no dependencies" OFF)
|
||||||
|
option(MSDFGEN_BUILD_STANDALONE "Build the msdfgen standalone executable" ON)
|
||||||
|
option(MSDFGEN_USE_VCPKG "Use vcpkg package manager to link project dependencies" ON)
|
||||||
|
option(MSDFGEN_USE_OPENMP "Build with OpenMP support for multithreaded code" OFF)
|
||||||
|
option(MSDFGEN_USE_CPP11 "Build with C++11 enabled" ON)
|
||||||
|
option(MSDFGEN_USE_SKIA "Build with the Skia library" ON)
|
||||||
|
option(MSDFGEN_INSTALL "Generate installation target" OFF)
|
||||||
|
option(MSDFGEN_DYNAMIC_RUNTIME "Link dynamic runtime library instead of static" OFF)
|
||||||
|
option(BUILD_SHARED_LIBS "Generate dynamic library files instead of static" OFF)
|
||||||
|
|
||||||
|
if(MSDFGEN_CORE_ONLY AND MSDFGEN_BUILD_STANDALONE)
|
||||||
|
message(WARNING "Option MSDFGEN_CORE_ONLY ignored - extensions are required for standalone executable")
|
||||||
|
set(MSDFGEN_CORE_ONLY OFF)
|
||||||
|
endif()
|
||||||
|
if(MSDFGEN_CORE_ONLY AND MSDFGEN_USE_VCPKG)
|
||||||
|
message(STATUS "Option MSDFGEN_USE_VCPKG ignored due to MSDFGEN_CORE_ONLY - core has no dependencies")
|
||||||
|
set(MSDFGEN_USE_VCPKG OFF)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
get_property(MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
|
||||||
|
if(NOT MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE)
|
||||||
|
message(STATUS "CMAKE_BUILD_TYPE not set, defaulting to Release")
|
||||||
|
set(CMAKE_BUILD_TYPE Release)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(MSDFGEN_DYNAMIC_RUNTIME)
|
||||||
|
set(MSDFGEN_MSVC_RUNTIME "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
|
||||||
|
else()
|
||||||
|
set(MSDFGEN_MSVC_RUNTIME "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(BUILD_SHARED_LIBS)
|
||||||
|
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(MSDFGEN_USE_VCPKG)
|
||||||
|
# Make sure that vcpkg toolchain file is set
|
||||||
|
if(NOT CMAKE_TOOLCHAIN_FILE)
|
||||||
|
if(DEFINED ENV{VCPKG_ROOT})
|
||||||
|
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
|
||||||
|
else()
|
||||||
|
message(SEND_ERROR "Vcpkg toolchain not configured. Either set VCPKG_ROOT environment variable or pass -DCMAKE_TOOLCHAIN_FILE=VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake to cmake")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
# Default to statically linked vcpkg triplet on Windows
|
||||||
|
if(WIN32 AND NOT VCPKG_TARGET_TRIPLET AND NOT MSDFGEN_DYNAMIC_RUNTIME)
|
||||||
|
if(CMAKE_GENERATOR_PLATFORM MATCHES "64$" AND NOT CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
|
||||||
|
set(VCPKG_TARGET_TRIPLET "x64-windows-static")
|
||||||
|
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "32$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
|
||||||
|
set(VCPKG_TARGET_TRIPLET "x86-windows-static")
|
||||||
|
else()
|
||||||
|
if(CMAKE_GENERATOR_PLATFORM)
|
||||||
|
message(WARNING "Vcpkg triplet not explicitly specified and could not be deduced. Recommend using -DVCPKG_TARGET_TRIPLET=x64-windows-static or similar")
|
||||||
|
else()
|
||||||
|
message(WARNING "Vcpkg triplet not explicitly specified and could not be deduced. Recommend using -A to explicitly select platform (Win32 or x64)")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
# Select project features
|
||||||
|
if(NOT MSDFGEN_VCPKG_FEATURES_SET)
|
||||||
|
set(VCPKG_MANIFEST_NO_DEFAULT_FEATURES ON)
|
||||||
|
if(NOT MSDFGEN_CORE_ONLY)
|
||||||
|
list(APPEND VCPKG_MANIFEST_FEATURES "extensions")
|
||||||
|
endif()
|
||||||
|
if(MSDFGEN_BUILD_STANDALONE)
|
||||||
|
list(APPEND VCPKG_MANIFEST_FEATURES "standalone")
|
||||||
|
endif()
|
||||||
|
if(MSDFGEN_USE_SKIA)
|
||||||
|
list(APPEND VCPKG_MANIFEST_FEATURES "geometry-preprocessing")
|
||||||
|
endif()
|
||||||
|
if(MSDFGEN_USE_OPENMP)
|
||||||
|
list(APPEND VCPKG_MANIFEST_FEATURES "openmp")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Version is specified in vcpkg.json
|
||||||
|
project(msdfgen VERSION ${MSDFGEN_VERSION} LANGUAGES CXX)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE MSDFGEN_CORE_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.h" "core/*.hpp")
|
||||||
|
file(GLOB_RECURSE MSDFGEN_CORE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "core/*.cpp")
|
||||||
|
file(GLOB_RECURSE MSDFGEN_EXT_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.h" "ext/*.hpp")
|
||||||
|
file(GLOB_RECURSE MSDFGEN_EXT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "ext/*.cpp" "lib/*.cpp")
|
||||||
|
|
||||||
|
# Core library
|
||||||
|
add_library(msdfgen-core "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" ${MSDFGEN_CORE_HEADERS} ${MSDFGEN_CORE_SOURCES})
|
||||||
|
add_library(msdfgen::msdfgen-core ALIAS msdfgen-core)
|
||||||
|
set_target_properties(msdfgen-core PROPERTIES PUBLIC_HEADER "${MSDFGEN_CORE_HEADERS}")
|
||||||
|
set_property(TARGET msdfgen-core PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
|
||||||
|
target_compile_definitions(msdfgen-core PUBLIC
|
||||||
|
MSDFGEN_VERSION=${MSDFGEN_VERSION}
|
||||||
|
MSDFGEN_VERSION_MAJOR=${MSDFGEN_VERSION_MAJOR}
|
||||||
|
MSDFGEN_VERSION_MINOR=${MSDFGEN_VERSION_MINOR}
|
||||||
|
MSDFGEN_VERSION_REVISION=${MSDFGEN_VERSION_REVISION}
|
||||||
|
MSDFGEN_COPYRIGHT_YEAR=${MSDFGEN_COPYRIGHT_YEAR}
|
||||||
|
)
|
||||||
|
target_include_directories(msdfgen-core INTERFACE
|
||||||
|
$<INSTALL_INTERFACE:include/msdfgen>
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
|
||||||
|
)
|
||||||
|
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-core)
|
||||||
|
|
||||||
|
if(MSDFGEN_USE_CPP11)
|
||||||
|
target_compile_features(msdfgen-core PUBLIC cxx_std_11)
|
||||||
|
target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_CPP11)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(MSDFGEN_USE_OPENMP)
|
||||||
|
# Note: Clang doesn't support OpenMP by default...
|
||||||
|
find_package(OpenMP REQUIRED COMPONENTS CXX)
|
||||||
|
target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_USE_OPENMP)
|
||||||
|
target_link_libraries(msdfgen-core PUBLIC OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(BUILD_SHARED_LIBS AND WIN32)
|
||||||
|
target_compile_definitions(msdfgen-core PRIVATE "MSDFGEN_PUBLIC=__declspec(dllexport)")
|
||||||
|
target_compile_definitions(msdfgen-core INTERFACE "MSDFGEN_PUBLIC=__declspec(dllimport)")
|
||||||
|
else()
|
||||||
|
target_compile_definitions(msdfgen-core PUBLIC MSDFGEN_PUBLIC=)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Extensions library
|
||||||
|
if(NOT MSDFGEN_CORE_ONLY)
|
||||||
|
if(NOT TARGET Freetype::Freetype)
|
||||||
|
find_package(Freetype REQUIRED)
|
||||||
|
endif()
|
||||||
|
if(NOT TARGET tinyxml2::tinyxml2)
|
||||||
|
find_package(tinyxml2 REQUIRED)
|
||||||
|
endif()
|
||||||
|
if(NOT TARGET PNG::PNG)
|
||||||
|
find_package(PNG REQUIRED)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(msdfgen-ext "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" ${MSDFGEN_EXT_HEADERS} ${MSDFGEN_EXT_SOURCES})
|
||||||
|
add_library(msdfgen::msdfgen-ext ALIAS msdfgen-ext)
|
||||||
|
set_target_properties(msdfgen-ext PROPERTIES PUBLIC_HEADER "${MSDFGEN_EXT_HEADERS}")
|
||||||
|
set_property(TARGET msdfgen-ext PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
|
||||||
|
target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_LIBPNG)
|
||||||
|
target_link_libraries(msdfgen-ext PRIVATE msdfgen::msdfgen-core Freetype::Freetype tinyxml2::tinyxml2 PNG::PNG)
|
||||||
|
target_include_directories(msdfgen-ext
|
||||||
|
PUBLIC
|
||||||
|
$<INSTALL_INTERFACE:include/msdfgen>
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen-ext)
|
||||||
|
|
||||||
|
if(MSDFGEN_USE_SKIA)
|
||||||
|
set(MSDFGEN_SKIA_LIB skia)
|
||||||
|
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
|
||||||
|
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
if(NOT TARGET skia)
|
||||||
|
if(MSDFGEN_USE_VCPKG)
|
||||||
|
find_package(unofficial-skia REQUIRED)
|
||||||
|
set(MSDFGEN_SKIA_LIB unofficial::skia::skia)
|
||||||
|
else()
|
||||||
|
find_package(skia REQUIRED)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
target_compile_features(msdfgen-ext PUBLIC cxx_std_17)
|
||||||
|
target_compile_definitions(msdfgen-ext PUBLIC MSDFGEN_USE_SKIA)
|
||||||
|
target_link_libraries(msdfgen-ext PRIVATE Threads::Threads ${MSDFGEN_SKIA_LIB})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(msdfgen-full INTERFACE)
|
||||||
|
add_library(msdfgen::msdfgen ALIAS msdfgen-full)
|
||||||
|
target_link_libraries(msdfgen-full INTERFACE msdfgen::msdfgen-core msdfgen::msdfgen-ext)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Standalone executable
|
||||||
|
if(MSDFGEN_BUILD_STANDALONE)
|
||||||
|
set(MSDFGEN_STANDALONE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
|
||||||
|
if(MSVC)
|
||||||
|
set(MSDFGEN_STANDALONE_SOURCES ${MSDFGEN_STANDALONE_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.rc")
|
||||||
|
endif()
|
||||||
|
add_executable(msdfgen ${MSDFGEN_STANDALONE_SOURCES})
|
||||||
|
target_compile_definitions(msdfgen PUBLIC MSDFGEN_STANDALONE)
|
||||||
|
target_compile_definitions(msdfgen PRIVATE MSDFGEN_VERSION_UNDERLINE=${MSDFGEN_VERSION_UNDERLINE})
|
||||||
|
set_property(TARGET msdfgen PROPERTY MSVC_RUNTIME_LIBRARY "${MSDFGEN_MSVC_RUNTIME}")
|
||||||
|
target_link_libraries(msdfgen PRIVATE msdfgen::msdfgen)
|
||||||
|
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT msdfgen)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
if(MSDFGEN_INSTALL)
|
||||||
|
include(GNUInstallDirs)
|
||||||
|
include(CMakePackageConfigHelpers)
|
||||||
|
set(MSDFGEN_CONFIG_PATH "lib/cmake/msdfgen")
|
||||||
|
|
||||||
|
# install tree package config
|
||||||
|
write_basic_package_version_file(
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
|
||||||
|
VERSION ${PROJECT_VERSION}
|
||||||
|
COMPATIBILITY SameMajorVersion
|
||||||
|
)
|
||||||
|
|
||||||
|
configure_package_config_file(
|
||||||
|
cmake/msdfgenConfig.cmake.in
|
||||||
|
${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake
|
||||||
|
INSTALL_DESTINATION ${MSDFGEN_CONFIG_PATH}
|
||||||
|
NO_CHECK_REQUIRED_COMPONENTS_MACRO
|
||||||
|
)
|
||||||
|
|
||||||
|
# build tree package config
|
||||||
|
configure_file(
|
||||||
|
cmake/msdfgenConfig.cmake.in
|
||||||
|
msdfgenConfig.cmake
|
||||||
|
@ONLY
|
||||||
|
)
|
||||||
|
|
||||||
|
install(TARGETS msdfgen-core EXPORT msdfgenTargets
|
||||||
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||||
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/core
|
||||||
|
)
|
||||||
|
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen.h" DESTINATION include/msdfgen)
|
||||||
|
if(MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
install(FILES $<TARGET_PDB_FILE:msdfgen-core> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT MSDFGEN_CORE_ONLY)
|
||||||
|
install(TARGETS msdfgen-ext EXPORT msdfgenTargets
|
||||||
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||||
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/msdfgen/ext
|
||||||
|
)
|
||||||
|
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/msdfgen-ext.h" DESTINATION include/msdfgen)
|
||||||
|
if(MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
install(FILES $<TARGET_PDB_FILE:msdfgen-ext> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
|
||||||
|
endif()
|
||||||
|
install(TARGETS msdfgen-full EXPORT msdfgenTargets)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
export(EXPORT msdfgenTargets NAMESPACE msdfgen:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenTargets.cmake")
|
||||||
|
install(EXPORT msdfgenTargets FILE msdfgenTargets.cmake NAMESPACE msdfgen:: DESTINATION ${MSDFGEN_CONFIG_PATH})
|
||||||
|
|
||||||
|
if(MSDFGEN_BUILD_STANDALONE)
|
||||||
|
install(TARGETS msdfgen EXPORT msdfgenBinaryTargets DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
if(MSVC)
|
||||||
|
install(FILES $<TARGET_PDB_FILE:msdfgen> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
|
||||||
|
endif()
|
||||||
|
export(EXPORT msdfgenBinaryTargets NAMESPACE msdfgen-standalone:: FILE "${CMAKE_CURRENT_BINARY_DIR}/msdfgenBinaryTargets.cmake")
|
||||||
|
install(EXPORT msdfgenBinaryTargets FILE msdfgenBinaryTargets.cmake NAMESPACE msdfgen-standalone:: DESTINATION ${MSDFGEN_CONFIG_PATH})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
install(
|
||||||
|
FILES
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/${MSDFGEN_CONFIG_PATH}/msdfgenConfig.cmake"
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/msdfgenConfigVersion.cmake"
|
||||||
|
DESTINATION ${MSDFGEN_CONFIG_PATH}
|
||||||
|
)
|
||||||
|
endif()
|
21
libs/msdf-atlas-gen/include/msdfgen/LICENSE.txt
Normal file
21
libs/msdf-atlas-gen/include/msdfgen/LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2016 - 2023 Viktor Chlumsky
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
215
libs/msdf-atlas-gen/include/msdfgen/README.md
Normal file
215
libs/msdf-atlas-gen/include/msdfgen/README.md
Normal file
|
@ -0,0 +1,215 @@
|
||||||
|
# Multi-channel signed distance field generator
|
||||||
|
|
||||||
|
This is a utility for generating signed distance fields from vector shapes and font glyphs,
|
||||||
|
which serve as a texture representation that can be used in real-time graphics to efficiently reproduce said shapes.
|
||||||
|
Although it can also be used to generate conventional signed distance fields best known from
|
||||||
|
[this Valve paper](https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf)
|
||||||
|
and pseudo-distance fields, its primary purpose is to generate multi-channel distance fields,
|
||||||
|
using a method I have developed. Unlike monochrome distance fields, they have the ability
|
||||||
|
to reproduce sharp corners almost perfectly by utilizing all three color channels.
|
||||||
|
|
||||||
|
The following comparison demonstrates the improvement in image quality.
|
||||||
|
|
||||||
|
![demo-msdf16](https://user-images.githubusercontent.com/18639794/106391899-e37ebe80-63ef-11eb-988b-4764004bb196.png)
|
||||||
|
![demo-sdf16](https://user-images.githubusercontent.com/18639794/106391905-e679af00-63ef-11eb-96c3-993176330911.png)
|
||||||
|
![demo-sdf32](https://user-images.githubusercontent.com/18639794/106391906-e7aadc00-63ef-11eb-8f84-d402d0dd9174.png)
|
||||||
|
|
||||||
|
- To learn more about this method, you can read my [Master's thesis](https://github.com/Chlumsky/msdfgen/files/3050967/thesis.pdf).
|
||||||
|
- Check out my [MSDF-Atlas-Gen](https://github.com/Chlumsky/msdf-atlas-gen) if you want to generate entire glyph atlases for text rendering.
|
||||||
|
- See what's new in the [changelog](CHANGELOG.md).
|
||||||
|
|
||||||
|
## Getting started
|
||||||
|
|
||||||
|
The project can be used either as a library or as a console program. It is divided into two parts, **[core](core)**
|
||||||
|
and **[extensions](ext)**. The core module has no dependencies and only uses bare C++. It contains all
|
||||||
|
key data structures and algorithms, which can be accessed through the [msdfgen.h](msdfgen.h) header.
|
||||||
|
Extensions contain utilities for loading fonts and SVG files, as well as saving PNG images.
|
||||||
|
Those are exposed by the [msdfgen-ext.h](msdfgen-ext.h) header. This module uses
|
||||||
|
[FreeType](https://freetype.org/),
|
||||||
|
[TinyXML2](https://www.grinninglizard.com/tinyxml2/),
|
||||||
|
[libpng](http://www.libpng.org/pub/png/libpng.html),
|
||||||
|
and (optionally) [Skia](https://skia.org/).
|
||||||
|
|
||||||
|
Additionally, there is the [main.cpp](main.cpp), which wraps the functionality into
|
||||||
|
a comprehensive standalone console program. To start using the program immediately,
|
||||||
|
there is a Windows binary available for download in the ["Releases" section](https://github.com/Chlumsky/msdfgen/releases).
|
||||||
|
To use the project as a library, you may install it via the [vcpkg](https://vcpkg.io) package manager as
|
||||||
|
```
|
||||||
|
vcpkg install msdfgen
|
||||||
|
```
|
||||||
|
Or, to build the project from source, you may use the included [CMake script](CMakeLists.txt).
|
||||||
|
In its default configuration, it requires [vcpkg](https://vcpkg.io) as the provider for third-party library dependencies.
|
||||||
|
If you set the environment variable `VCPKG_ROOT` to the vcpkg directory,
|
||||||
|
the CMake configuration will take care of fetching all required packages from vcpkg.
|
||||||
|
|
||||||
|
## Console commands
|
||||||
|
|
||||||
|
The standalone program is executed as
|
||||||
|
```
|
||||||
|
msdfgen.exe <mode> <input> <options>
|
||||||
|
```
|
||||||
|
where only the input specification is required.
|
||||||
|
|
||||||
|
Mode can be one of:
|
||||||
|
- **sdf** – generates a conventional monochrome (true) signed distance field.
|
||||||
|
- **psdf** – generates a monochrome signed pseudo-distance field.
|
||||||
|
- **msdf** (default) – generates a multi-channel signed distance field using my new method.
|
||||||
|
- **mtsdf** – generates a combined multi-channel and true signed distance field in the alpha channel.
|
||||||
|
|
||||||
|
The input can be specified as one of:
|
||||||
|
- **-font \<filename.ttf\> \<character code\>** – to load a glyph from a font file.
|
||||||
|
Character code can be expressed as either a decimal (63) or hexadecimal (0x3F) Unicode value, or an ASCII character
|
||||||
|
in single quotes ('?').
|
||||||
|
- **-svg \<filename.svg\>** – to load an SVG file. Note that only the last vector path in the file will be used.
|
||||||
|
- **-shapedesc \<filename.txt\>**, -defineshape \<definition\>, -stdin – to load a text description of the shape
|
||||||
|
from either a file, the next argument, or the standard input, respectively. Its syntax is documented further down.
|
||||||
|
|
||||||
|
The complete list of available options can be printed with **-help**.
|
||||||
|
Some of the important ones are:
|
||||||
|
- **-o \<filename\>** – specifies the output file name. The desired format will be deduced from the extension
|
||||||
|
(png, bmp, tif, txt, bin). Otherwise, use -format.
|
||||||
|
- **-size \<width\> \<height\>** – specifies the dimensions of the output distance field (in pixels).
|
||||||
|
- **-range \<range\>**, **-pxrange \<range\>** – specifies the width of the range around the shape
|
||||||
|
between the minimum and maximum representable signed distance in shape units or distance field pixels, respectivelly.
|
||||||
|
- **-scale \<scale\>** – sets the scale used to convert shape units to distance field pixels.
|
||||||
|
- **-translate \<x\> \<y\>** – sets the translation of the shape in shape units. Otherwise the origin (0, 0)
|
||||||
|
lies in the bottom left corner.
|
||||||
|
- **-autoframe** – automatically frames the shape to fit the distance field. If the output must be precisely aligned,
|
||||||
|
you should manually position it using -translate and -scale instead.
|
||||||
|
- **-angle \<angle\>** – specifies the maximum angle to be considered a corner.
|
||||||
|
Can be expressed in radians (3.0) or degrees with D at the end (171.9D).
|
||||||
|
- **-testrender \<filename.png\> \<width\> \<height\>** - tests the generated distance field by using it to render an image
|
||||||
|
of the original shape into a PNG file with the specified dimensions. Alternatively, -testrendermulti renders
|
||||||
|
an image without combining the color channels, and may give you an insight in how the multi-channel distance field works.
|
||||||
|
- **-exportshape \<filename.txt\>** - saves the text description of the shape with edge coloring to the specified file.
|
||||||
|
This can be later edited and used as input through -shapedesc.
|
||||||
|
- **-printmetrics** – prints some useful information about the shape's layout.
|
||||||
|
|
||||||
|
For example,
|
||||||
|
```
|
||||||
|
msdfgen.exe msdf -font C:\Windows\Fonts\arialbd.ttf 'M' -o msdf.png -size 32 32 -pxrange 4 -autoframe -testrender render.png 1024 1024
|
||||||
|
```
|
||||||
|
|
||||||
|
will take the glyph capital M from the Arial Bold typeface, generate a 32×32 multi-channel distance field
|
||||||
|
with a 4 pixels wide distance range, store it into msdf.png, and create a test render of the glyph as render.png.
|
||||||
|
|
||||||
|
**Note:** Do not use `-autoframe` to generate character maps! It is intended as a quick preview only.
|
||||||
|
|
||||||
|
## Library API
|
||||||
|
|
||||||
|
If you choose to use this utility inside your own program, there are a few simple steps you need to perform
|
||||||
|
in order to generate a distance field. Please note that all classes and functions are in the `msdfgen` namespace.
|
||||||
|
|
||||||
|
- Acquire a `Shape` object. You can either load it via `loadGlyph` or `loadSvgShape`, or construct it manually.
|
||||||
|
It consists of closed contours, which in turn consist of edges. An edge is represented by a `LinearEdge`, `QuadraticEdge`,
|
||||||
|
or `CubicEdge`. You can construct them from two endpoints and 0 to 2 Bézier control points.
|
||||||
|
- Normalize the shape using its `normalize` method and assign colors to edges if you need a multi-channel SDF.
|
||||||
|
This can be performed automatically using the `edgeColoringSimple` heuristic, or manually by setting each edge's
|
||||||
|
`color` member. Keep in mind that at least two color channels must be turned on in each edge, and iff two edges meet
|
||||||
|
at a sharp corner, they must only have one channel in common.
|
||||||
|
- Call `generateSDF`, `generatePseudoSDF`, or `generateMSDF` to generate a distance field into a floating point
|
||||||
|
`Bitmap` object. This can then be worked with further or saved to a file using `saveBmp`, `savePng`, or `saveTiff`.
|
||||||
|
- You may also render an image from the distance field using `renderSDF`. Consider calling `simulate8bit`
|
||||||
|
on the distance field beforehand to simulate the standard 8 bits/channel image format.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```c++
|
||||||
|
#include "msdfgen.h"
|
||||||
|
#include "msdfgen-ext.h"
|
||||||
|
|
||||||
|
using namespace msdfgen;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
FreetypeHandle *ft = initializeFreetype();
|
||||||
|
if (ft) {
|
||||||
|
FontHandle *font = loadFont(ft, "C:\\Windows\\Fonts\\arialbd.ttf");
|
||||||
|
if (font) {
|
||||||
|
Shape shape;
|
||||||
|
if (loadGlyph(shape, font, 'A')) {
|
||||||
|
shape.normalize();
|
||||||
|
// max. angle
|
||||||
|
edgeColoringSimple(shape, 3.0);
|
||||||
|
// image width, height
|
||||||
|
Bitmap<float, 3> msdf(32, 32);
|
||||||
|
// range, scale, translation
|
||||||
|
generateMSDF(msdf, shape, 4.0, 1.0, Vector2(4.0, 4.0));
|
||||||
|
savePng(msdf, "output.png");
|
||||||
|
}
|
||||||
|
destroyFont(font);
|
||||||
|
}
|
||||||
|
deinitializeFreetype(ft);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using a multi-channel distance field
|
||||||
|
|
||||||
|
Using a multi-channel distance field generated by this program is similarly simple to how a monochrome distance field is used.
|
||||||
|
The only additional operation is computing the **median** of the three channels inside the fragment shader,
|
||||||
|
right after sampling the distance field. This signed distance value can then be used the same way as usual.
|
||||||
|
|
||||||
|
The following is an example GLSL fragment shader with anti-aliasing:
|
||||||
|
|
||||||
|
```glsl
|
||||||
|
in vec2 texCoord;
|
||||||
|
out vec4 color;
|
||||||
|
uniform sampler2D msdf;
|
||||||
|
uniform vec4 bgColor;
|
||||||
|
uniform vec4 fgColor;
|
||||||
|
|
||||||
|
float median(float r, float g, float b) {
|
||||||
|
return max(min(r, g), min(max(r, g), b));
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec3 msd = texture(msdf, texCoord).rgb;
|
||||||
|
float sd = median(msd.r, msd.g, msd.b);
|
||||||
|
float screenPxDistance = screenPxRange()*(sd - 0.5);
|
||||||
|
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
|
||||||
|
color = mix(bgColor, fgColor, opacity);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here, `screenPxRange()` represents the distance field range in output screen pixels. For example, if the pixel range was set to 2
|
||||||
|
when generating a 32x32 distance field, and it is used to draw a quad that is 72x72 pixels on the screen,
|
||||||
|
it should return 4.5 (because 72/32 * 2 = 4.5).
|
||||||
|
**For 2D rendering, this can generally be replaced by a precomputed uniform value.**
|
||||||
|
|
||||||
|
For rendering in a **3D perspective only**, where the texture scale varies across the screen,
|
||||||
|
you may want to implement this function with fragment derivatives in the following way.
|
||||||
|
I would suggest precomputing `unitRange` as a uniform variable instead of `pxRange` for better performance.
|
||||||
|
|
||||||
|
```glsl
|
||||||
|
uniform float pxRange; // set to distance field's pixel range
|
||||||
|
|
||||||
|
float screenPxRange() {
|
||||||
|
vec2 unitRange = vec2(pxRange)/vec2(textureSize(msdf, 0));
|
||||||
|
vec2 screenTexSize = vec2(1.0)/fwidth(texCoord);
|
||||||
|
return max(0.5*dot(unitRange, screenTexSize), 1.0);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`screenPxRange()` must never be lower than 1. If it is lower than 2, there is a high probability that the anti-aliasing will fail
|
||||||
|
and you may want to re-generate your distance field with a wider range.
|
||||||
|
|
||||||
|
## Shape description syntax
|
||||||
|
|
||||||
|
The text shape description has the following syntax.
|
||||||
|
- Each closed contour is enclosed by braces: `{ <contour 1> } { <contour 2> }`
|
||||||
|
- Each point (and control point) is written as two real numbers separated by a comma.
|
||||||
|
- Points in a contour are separated with semicolons.
|
||||||
|
- The last point of each contour must be equal to the first, or the symbol `#` can be used, which represents the first point.
|
||||||
|
- There can be an edge segment specification between any two points, also separated by semicolons.
|
||||||
|
This can include the edge's color (`c`, `m`, `y` or `w`) and/or one or two Bézier curve control points inside parentheses.
|
||||||
|
|
||||||
|
For example,
|
||||||
|
```
|
||||||
|
{ -1, -1; m; -1, +1; y; +1, +1; m; +1, -1; y; # }
|
||||||
|
```
|
||||||
|
would represent a square with magenta and yellow edges,
|
||||||
|
```
|
||||||
|
{ 0, 1; (+1.6, -0.8; -1.6, -0.8); # }
|
||||||
|
```
|
||||||
|
is a teardrop shape formed by a single cubic Bézier curve.
|
50
libs/msdf-atlas-gen/include/msdfgen/core/Bitmap.h
Normal file
50
libs/msdf-atlas-gen/include/msdfgen/core/Bitmap.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "BitmapRef.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// A 2D image bitmap with N channels of type T. Pixel memory is managed by the class.
|
||||||
|
template <typename T, int N = 1>
|
||||||
|
class Bitmap {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Bitmap();
|
||||||
|
Bitmap(int width, int height);
|
||||||
|
Bitmap(const BitmapConstRef<T, N> &orig);
|
||||||
|
Bitmap(const Bitmap<T, N> &orig);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
Bitmap(Bitmap<T, N> &&orig);
|
||||||
|
#endif
|
||||||
|
~Bitmap();
|
||||||
|
Bitmap<T, N> & operator=(const BitmapConstRef<T, N> &orig);
|
||||||
|
Bitmap<T, N> & operator=(const Bitmap<T, N> &orig);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
Bitmap<T, N> & operator=(Bitmap<T, N> &&orig);
|
||||||
|
#endif
|
||||||
|
/// Bitmap width in pixels.
|
||||||
|
int width() const;
|
||||||
|
/// Bitmap height in pixels.
|
||||||
|
int height() const;
|
||||||
|
T * operator()(int x, int y);
|
||||||
|
const T * operator()(int x, int y) const;
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
explicit operator T *();
|
||||||
|
explicit operator const T *() const;
|
||||||
|
#else
|
||||||
|
operator T *();
|
||||||
|
operator const T *() const;
|
||||||
|
#endif
|
||||||
|
operator BitmapRef<T, N>();
|
||||||
|
operator BitmapConstRef<T, N>() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
T *pixels;
|
||||||
|
int w, h;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "Bitmap.hpp"
|
117
libs/msdf-atlas-gen/include/msdfgen/core/Bitmap.hpp
Normal file
117
libs/msdf-atlas-gen/include/msdfgen/core/Bitmap.hpp
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
|
||||||
|
#include "Bitmap.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0) { }
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) {
|
||||||
|
pixels = new T[N*w*h];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height) {
|
||||||
|
pixels = new T[N*w*h];
|
||||||
|
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h) {
|
||||||
|
pixels = new T[N*w*h];
|
||||||
|
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h) {
|
||||||
|
orig.pixels = NULL;
|
||||||
|
orig.w = 0, orig.h = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::~Bitmap() {
|
||||||
|
delete [] pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N> & Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {
|
||||||
|
if (pixels != orig.pixels) {
|
||||||
|
delete [] pixels;
|
||||||
|
w = orig.width, h = orig.height;
|
||||||
|
pixels = new T[N*w*h];
|
||||||
|
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N> & Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {
|
||||||
|
if (this != &orig) {
|
||||||
|
delete [] pixels;
|
||||||
|
w = orig.w, h = orig.h;
|
||||||
|
pixels = new T[N*w*h];
|
||||||
|
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N> & Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {
|
||||||
|
if (this != &orig) {
|
||||||
|
delete [] pixels;
|
||||||
|
pixels = orig.pixels;
|
||||||
|
w = orig.w, h = orig.h;
|
||||||
|
orig.pixels = NULL;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
int Bitmap<T, N>::width() const {
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
int Bitmap<T, N>::height() const {
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
T * Bitmap<T, N>::operator()(int x, int y) {
|
||||||
|
return pixels+N*(w*y+x);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
const T * Bitmap<T, N>::operator()(int x, int y) const {
|
||||||
|
return pixels+N*(w*y+x);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::operator T *() {
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::operator const T *() const {
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::operator BitmapRef<T, N>() {
|
||||||
|
return BitmapRef<T, N>(pixels, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
|
||||||
|
return BitmapConstRef<T, N>(pixels, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
libs/msdf-atlas-gen/include/msdfgen/core/BitmapRef.hpp
Normal file
43
libs/msdf-atlas-gen/include/msdfgen/core/BitmapRef.hpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
typedef unsigned char byte;
|
||||||
|
|
||||||
|
/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
|
||||||
|
template <typename T, int N = 1>
|
||||||
|
struct BitmapRef {
|
||||||
|
|
||||||
|
T *pixels;
|
||||||
|
int width, height;
|
||||||
|
|
||||||
|
inline BitmapRef() : pixels(NULL), width(0), height(0) { }
|
||||||
|
inline BitmapRef(T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }
|
||||||
|
|
||||||
|
inline T * operator()(int x, int y) const {
|
||||||
|
return pixels+N*(width*y+x);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
|
||||||
|
template <typename T, int N = 1>
|
||||||
|
struct BitmapConstRef {
|
||||||
|
|
||||||
|
const T *pixels;
|
||||||
|
int width, height;
|
||||||
|
|
||||||
|
inline BitmapConstRef() : pixels(NULL), width(0), height(0) { }
|
||||||
|
inline BitmapConstRef(const T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }
|
||||||
|
inline BitmapConstRef(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height) { }
|
||||||
|
|
||||||
|
inline const T * operator()(int x, int y) const {
|
||||||
|
return pixels+N*(width*y+x);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
90
libs/msdf-atlas-gen/include/msdfgen/core/Contour.cpp
Normal file
90
libs/msdf-atlas-gen/include/msdfgen/core/Contour.cpp
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
|
||||||
|
#include "Contour.h"
|
||||||
|
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
static double shoelace(const Point2 &a, const Point2 &b) {
|
||||||
|
return (b.x-a.x)*(a.y+b.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Contour::addEdge(const EdgeHolder &edge) {
|
||||||
|
edges.push_back(edge);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void Contour::addEdge(EdgeHolder &&edge) {
|
||||||
|
edges.push_back((EdgeHolder &&) edge);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EdgeHolder & Contour::addEdge() {
|
||||||
|
edges.resize(edges.size()+1);
|
||||||
|
return edges.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void boundPoint(double &l, double &b, double &r, double &t, Point2 p) {
|
||||||
|
if (p.x < l) l = p.x;
|
||||||
|
if (p.y < b) b = p.y;
|
||||||
|
if (p.x > r) r = p.x;
|
||||||
|
if (p.y > t) t = p.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Contour::bound(double &l, double &b, double &r, double &t) const {
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge)
|
||||||
|
(*edge)->bound(l, b, r, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Contour::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
|
||||||
|
if (edges.empty())
|
||||||
|
return;
|
||||||
|
Vector2 prevDir = edges.back()->direction(1).normalize(true);
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) {
|
||||||
|
Vector2 dir = -(*edge)->direction(0).normalize(true);
|
||||||
|
if (polarity*crossProduct(prevDir, dir) >= 0) {
|
||||||
|
double miterLength = miterLimit;
|
||||||
|
double q = .5*(1-dotProduct(prevDir, dir));
|
||||||
|
if (q > 0)
|
||||||
|
miterLength = min(1/sqrt(q), miterLimit);
|
||||||
|
Point2 miter = (*edge)->point(0)+border*miterLength*(prevDir+dir).normalize(true);
|
||||||
|
boundPoint(l, b, r, t, miter);
|
||||||
|
}
|
||||||
|
prevDir = (*edge)->direction(1).normalize(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Contour::winding() const {
|
||||||
|
if (edges.empty())
|
||||||
|
return 0;
|
||||||
|
double total = 0;
|
||||||
|
if (edges.size() == 1) {
|
||||||
|
Point2 a = edges[0]->point(0), b = edges[0]->point(1/3.), c = edges[0]->point(2/3.);
|
||||||
|
total += shoelace(a, b);
|
||||||
|
total += shoelace(b, c);
|
||||||
|
total += shoelace(c, a);
|
||||||
|
} else if (edges.size() == 2) {
|
||||||
|
Point2 a = edges[0]->point(0), b = edges[0]->point(.5), c = edges[1]->point(0), d = edges[1]->point(.5);
|
||||||
|
total += shoelace(a, b);
|
||||||
|
total += shoelace(b, c);
|
||||||
|
total += shoelace(c, d);
|
||||||
|
total += shoelace(d, a);
|
||||||
|
} else {
|
||||||
|
Point2 prev = edges.back()->point(0);
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) {
|
||||||
|
Point2 cur = (*edge)->point(0);
|
||||||
|
total += shoelace(prev, cur);
|
||||||
|
prev = cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sign(total);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Contour::reverse() {
|
||||||
|
for (int i = (int) edges.size()/2; i > 0; --i)
|
||||||
|
EdgeHolder::swap(edges[i-1], edges[edges.size()-i]);
|
||||||
|
for (std::vector<EdgeHolder>::iterator edge = edges.begin(); edge != edges.end(); ++edge)
|
||||||
|
(*edge)->reverse();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
libs/msdf-atlas-gen/include/msdfgen/core/Contour.h
Normal file
34
libs/msdf-atlas-gen/include/msdfgen/core/Contour.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "EdgeHolder.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// A single closed contour of a shape.
|
||||||
|
class Contour {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// The sequence of edges that make up the contour.
|
||||||
|
std::vector<EdgeHolder> edges;
|
||||||
|
|
||||||
|
/// Adds an edge to the contour.
|
||||||
|
void addEdge(const EdgeHolder &edge);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void addEdge(EdgeHolder &&edge);
|
||||||
|
#endif
|
||||||
|
/// Creates a new edge in the contour and returns its reference.
|
||||||
|
EdgeHolder & addEdge();
|
||||||
|
/// Adjusts the bounding box to fit the contour.
|
||||||
|
void bound(double &l, double &b, double &r, double &t) const;
|
||||||
|
/// Adjusts the bounding box to fit the contour border's mitered corners.
|
||||||
|
void boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const;
|
||||||
|
/// Computes the winding of the contour. Returns 1 if positive, -1 if negative.
|
||||||
|
int winding() const;
|
||||||
|
/// Reverses the sequence of edges on the contour.
|
||||||
|
void reverse();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
18
libs/msdf-atlas-gen/include/msdfgen/core/EdgeColor.h
Normal file
18
libs/msdf-atlas-gen/include/msdfgen/core/EdgeColor.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Edge color specifies which color channels an edge belongs to.
|
||||||
|
enum EdgeColor {
|
||||||
|
BLACK = 0,
|
||||||
|
RED = 1,
|
||||||
|
GREEN = 2,
|
||||||
|
YELLOW = 3,
|
||||||
|
BLUE = 4,
|
||||||
|
MAGENTA = 5,
|
||||||
|
CYAN = 6,
|
||||||
|
WHITE = 7
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
77
libs/msdf-atlas-gen/include/msdfgen/core/EdgeHolder.cpp
Normal file
77
libs/msdf-atlas-gen/include/msdfgen/core/EdgeHolder.cpp
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
|
||||||
|
#include "EdgeHolder.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
void EdgeHolder::swap(EdgeHolder &a, EdgeHolder &b) {
|
||||||
|
EdgeSegment *tmp = a.edgeSegment;
|
||||||
|
a.edgeSegment = b.edgeSegment;
|
||||||
|
b.edgeSegment = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder() : edgeSegment(NULL) { }
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder(EdgeSegment *segment) : edgeSegment(segment) { }
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, EdgeColor edgeColor) : edgeSegment(new LinearSegment(p0, p1, edgeColor)) { }
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : edgeSegment(new QuadraticSegment(p0, p1, p2, edgeColor)) { }
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : edgeSegment(new CubicSegment(p0, p1, p2, p3, edgeColor)) { }
|
||||||
|
|
||||||
|
EdgeHolder::EdgeHolder(const EdgeHolder &orig) : edgeSegment(orig.edgeSegment ? orig.edgeSegment->clone() : NULL) { }
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
EdgeHolder::EdgeHolder(EdgeHolder &&orig) : edgeSegment(orig.edgeSegment) {
|
||||||
|
orig.edgeSegment = NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EdgeHolder::~EdgeHolder() {
|
||||||
|
delete edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeHolder & EdgeHolder::operator=(const EdgeHolder &orig) {
|
||||||
|
if (this != &orig) {
|
||||||
|
delete edgeSegment;
|
||||||
|
edgeSegment = orig.edgeSegment ? orig.edgeSegment->clone() : NULL;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
EdgeHolder & EdgeHolder::operator=(EdgeHolder &&orig) {
|
||||||
|
if (this != &orig) {
|
||||||
|
delete edgeSegment;
|
||||||
|
edgeSegment = orig.edgeSegment;
|
||||||
|
orig.edgeSegment = NULL;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EdgeSegment & EdgeHolder::operator*() {
|
||||||
|
return *edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EdgeSegment & EdgeHolder::operator*() const {
|
||||||
|
return *edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeSegment * EdgeHolder::operator->() {
|
||||||
|
return edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EdgeSegment * EdgeHolder::operator->() const {
|
||||||
|
return edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeHolder::operator EdgeSegment *() {
|
||||||
|
return edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeHolder::operator const EdgeSegment *() const {
|
||||||
|
return edgeSegment;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
41
libs/msdf-atlas-gen/include/msdfgen/core/EdgeHolder.h
Normal file
41
libs/msdf-atlas-gen/include/msdfgen/core/EdgeHolder.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "edge-segments.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Container for a single edge of dynamic type.
|
||||||
|
class EdgeHolder {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// Swaps the edges held by a and b.
|
||||||
|
static void swap(EdgeHolder &a, EdgeHolder &b);
|
||||||
|
|
||||||
|
EdgeHolder();
|
||||||
|
EdgeHolder(EdgeSegment *segment);
|
||||||
|
EdgeHolder(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
|
||||||
|
EdgeHolder(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
|
||||||
|
EdgeHolder(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
|
||||||
|
EdgeHolder(const EdgeHolder &orig);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
EdgeHolder(EdgeHolder &&orig);
|
||||||
|
#endif
|
||||||
|
~EdgeHolder();
|
||||||
|
EdgeHolder & operator=(const EdgeHolder &orig);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
EdgeHolder & operator=(EdgeHolder &&orig);
|
||||||
|
#endif
|
||||||
|
EdgeSegment & operator*();
|
||||||
|
const EdgeSegment & operator*() const;
|
||||||
|
EdgeSegment * operator->();
|
||||||
|
const EdgeSegment * operator->() const;
|
||||||
|
operator EdgeSegment *();
|
||||||
|
operator const EdgeSegment *() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EdgeSegment *edgeSegment;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
495
libs/msdf-atlas-gen/include/msdfgen/core/MSDFErrorCorrection.cpp
Normal file
495
libs/msdf-atlas-gen/include/msdfgen/core/MSDFErrorCorrection.cpp
Normal file
|
@ -0,0 +1,495 @@
|
||||||
|
|
||||||
|
#include "MSDFErrorCorrection.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
#include "equation-solver.h"
|
||||||
|
#include "EdgeColor.h"
|
||||||
|
#include "bitmap-interpolation.hpp"
|
||||||
|
#include "edge-selectors.h"
|
||||||
|
#include "contour-combiners.h"
|
||||||
|
#include "ShapeDistanceFinder.h"
|
||||||
|
#include "generator-config.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
#define ARTIFACT_T_EPSILON .01
|
||||||
|
#define PROTECTION_RADIUS_TOLERANCE 1.001
|
||||||
|
|
||||||
|
#define CLASSIFIER_FLAG_CANDIDATE 0x01
|
||||||
|
#define CLASSIFIER_FLAG_ARTIFACT 0x02
|
||||||
|
|
||||||
|
const double ErrorCorrectionConfig::defaultMinDeviationRatio = 1.11111111111111111;
|
||||||
|
const double ErrorCorrectionConfig::defaultMinImproveRatio = 1.11111111111111111;
|
||||||
|
|
||||||
|
/// The base artifact classifier recognizes artifacts based on the contents of the SDF alone.
|
||||||
|
class BaseArtifactClassifier {
|
||||||
|
public:
|
||||||
|
inline BaseArtifactClassifier(double span, bool protectedFlag) : span(span), protectedFlag(protectedFlag) { }
|
||||||
|
/// Evaluates if the median value xm interpolated at xt in the range between am at at and bm at bt indicates an artifact.
|
||||||
|
inline int rangeTest(double at, double bt, double xt, float am, float bm, float xm) const {
|
||||||
|
// For protected texels, only consider inversion artifacts (interpolated median has different sign than boundaries). For the rest, it is sufficient that the interpolated median is outside its boundaries.
|
||||||
|
if ((am > .5f && bm > .5f && xm <= .5f) || (am < .5f && bm < .5f && xm >= .5f) || (!protectedFlag && median(am, bm, xm) != xm)) {
|
||||||
|
double axSpan = (xt-at)*span, bxSpan = (bt-xt)*span;
|
||||||
|
// Check if the interpolated median's value is in the expected range based on its distance (span) from boundaries a, b.
|
||||||
|
if (!(xm >= am-axSpan && xm <= am+axSpan && xm >= bm-bxSpan && xm <= bm+bxSpan))
|
||||||
|
return CLASSIFIER_FLAG_CANDIDATE|CLASSIFIER_FLAG_ARTIFACT;
|
||||||
|
return CLASSIFIER_FLAG_CANDIDATE;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/// Returns true if the combined results of the tests performed on the median value m interpolated at t indicate an artifact.
|
||||||
|
inline bool evaluate(double t, float m, int flags) const {
|
||||||
|
return (flags&2) != 0;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
double span;
|
||||||
|
bool protectedFlag;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The shape distance checker evaluates the exact shape distance to find additional artifacts at a significant performance cost.
|
||||||
|
template <template <typename> class ContourCombiner, int N>
|
||||||
|
class ShapeDistanceChecker {
|
||||||
|
public:
|
||||||
|
class ArtifactClassifier : public BaseArtifactClassifier {
|
||||||
|
public:
|
||||||
|
inline ArtifactClassifier(ShapeDistanceChecker *parent, const Vector2 &direction, double span) : BaseArtifactClassifier(span, parent->protectedFlag), parent(parent), direction(direction) { }
|
||||||
|
/// Returns true if the combined results of the tests performed on the median value m interpolated at t indicate an artifact.
|
||||||
|
inline bool evaluate(double t, float m, int flags) const {
|
||||||
|
if (flags&CLASSIFIER_FLAG_CANDIDATE) {
|
||||||
|
// Skip expensive distance evaluation if the point has already been classified as an artifact by the base classifier.
|
||||||
|
if (flags&CLASSIFIER_FLAG_ARTIFACT)
|
||||||
|
return true;
|
||||||
|
Vector2 tVector = t*direction;
|
||||||
|
float oldMSD[N], newMSD[3];
|
||||||
|
// Compute the color that would be currently interpolated at the artifact candidate's position.
|
||||||
|
Point2 sdfCoord = parent->sdfCoord+tVector;
|
||||||
|
interpolate(oldMSD, parent->sdf, sdfCoord);
|
||||||
|
// Compute the color that would be interpolated at the artifact candidate's position if error correction was applied on the current texel.
|
||||||
|
double aWeight = (1-fabs(tVector.x))*(1-fabs(tVector.y));
|
||||||
|
float aPSD = median(parent->msd[0], parent->msd[1], parent->msd[2]);
|
||||||
|
newMSD[0] = float(oldMSD[0]+aWeight*(aPSD-parent->msd[0]));
|
||||||
|
newMSD[1] = float(oldMSD[1]+aWeight*(aPSD-parent->msd[1]));
|
||||||
|
newMSD[2] = float(oldMSD[2]+aWeight*(aPSD-parent->msd[2]));
|
||||||
|
// Compute the evaluated distance (interpolated median) before and after error correction, as well as the exact shape distance.
|
||||||
|
float oldPSD = median(oldMSD[0], oldMSD[1], oldMSD[2]);
|
||||||
|
float newPSD = median(newMSD[0], newMSD[1], newMSD[2]);
|
||||||
|
float refPSD = float(parent->invRange*parent->distanceFinder.distance(parent->shapeCoord+tVector*parent->texelSize)+.5);
|
||||||
|
// Compare the differences of the exact distance and the before and after distances.
|
||||||
|
return parent->minImproveRatio*fabsf(newPSD-refPSD) < double(fabsf(oldPSD-refPSD));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
ShapeDistanceChecker *parent;
|
||||||
|
Vector2 direction;
|
||||||
|
};
|
||||||
|
Point2 shapeCoord, sdfCoord;
|
||||||
|
const float *msd;
|
||||||
|
bool protectedFlag;
|
||||||
|
inline ShapeDistanceChecker(const BitmapConstRef<float, N> &sdf, const Shape &shape, const Projection &projection, double invRange, double minImproveRatio) : distanceFinder(shape), sdf(sdf), invRange(invRange), minImproveRatio(minImproveRatio) {
|
||||||
|
texelSize = projection.unprojectVector(Vector2(1));
|
||||||
|
}
|
||||||
|
inline ArtifactClassifier classifier(const Vector2 &direction, double span) {
|
||||||
|
return ArtifactClassifier(this, direction, span);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
ShapeDistanceFinder<ContourCombiner<PseudoDistanceSelector> > distanceFinder;
|
||||||
|
BitmapConstRef<float, N> sdf;
|
||||||
|
double invRange;
|
||||||
|
Vector2 texelSize;
|
||||||
|
double minImproveRatio;
|
||||||
|
};
|
||||||
|
|
||||||
|
MSDFErrorCorrection::MSDFErrorCorrection() { }
|
||||||
|
|
||||||
|
MSDFErrorCorrection::MSDFErrorCorrection(const BitmapRef<byte, 1> &stencil, const Projection &projection, double range) : stencil(stencil), projection(projection) {
|
||||||
|
invRange = 1/range;
|
||||||
|
minDeviationRatio = ErrorCorrectionConfig::defaultMinDeviationRatio;
|
||||||
|
minImproveRatio = ErrorCorrectionConfig::defaultMinImproveRatio;
|
||||||
|
memset(stencil.pixels, 0, sizeof(byte)*stencil.width*stencil.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MSDFErrorCorrection::setMinDeviationRatio(double minDeviationRatio) {
|
||||||
|
this->minDeviationRatio = minDeviationRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MSDFErrorCorrection::setMinImproveRatio(double minImproveRatio) {
|
||||||
|
this->minImproveRatio = minImproveRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MSDFErrorCorrection::protectCorners(const Shape &shape) {
|
||||||
|
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
const EdgeSegment *prevEdge = contour->edges.back();
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
int commonColor = prevEdge->color&(*edge)->color;
|
||||||
|
// If the color changes from prevEdge to edge, this is a corner.
|
||||||
|
if (!(commonColor&(commonColor-1))) {
|
||||||
|
// Find the four texels that envelop the corner and mark them as protected.
|
||||||
|
Point2 p = projection.project((*edge)->point(0));
|
||||||
|
if (shape.inverseYAxis)
|
||||||
|
p.y = stencil.height-p.y;
|
||||||
|
int l = (int) floor(p.x-.5);
|
||||||
|
int b = (int) floor(p.y-.5);
|
||||||
|
int r = l+1;
|
||||||
|
int t = b+1;
|
||||||
|
// Check that the positions are within bounds.
|
||||||
|
if (l < stencil.width && b < stencil.height && r >= 0 && t >= 0) {
|
||||||
|
if (l >= 0 && b >= 0)
|
||||||
|
*stencil(l, b) |= (byte) PROTECTED;
|
||||||
|
if (r < stencil.width && b >= 0)
|
||||||
|
*stencil(r, b) |= (byte) PROTECTED;
|
||||||
|
if (l >= 0 && t < stencil.height)
|
||||||
|
*stencil(l, t) |= (byte) PROTECTED;
|
||||||
|
if (r < stencil.width && t < stencil.height)
|
||||||
|
*stencil(r, t) |= (byte) PROTECTED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prevEdge = *edge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determines if the channel contributes to an edge between the two texels a, b.
|
||||||
|
static bool edgeBetweenTexelsChannel(const float *a, const float *b, int channel) {
|
||||||
|
// Find interpolation ratio t (0 < t < 1) where an edge is expected (mix(a[channel], b[channel], t) == 0.5).
|
||||||
|
double t = (a[channel]-.5)/(a[channel]-b[channel]);
|
||||||
|
if (t > 0 && t < 1) {
|
||||||
|
// Interpolate channel values at t.
|
||||||
|
float c[3] = {
|
||||||
|
mix(a[0], b[0], t),
|
||||||
|
mix(a[1], b[1], t),
|
||||||
|
mix(a[2], b[2], t)
|
||||||
|
};
|
||||||
|
// This is only an edge if the zero-distance channel is the median.
|
||||||
|
return median(c[0], c[1], c[2]) == c[channel];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a bit mask of which channels contribute to an edge between the two texels a, b.
|
||||||
|
static int edgeBetweenTexels(const float *a, const float *b) {
|
||||||
|
return (
|
||||||
|
RED*edgeBetweenTexelsChannel(a, b, 0)+
|
||||||
|
GREEN*edgeBetweenTexelsChannel(a, b, 1)+
|
||||||
|
BLUE*edgeBetweenTexelsChannel(a, b, 2)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Marks texel as protected if one of its non-median channels is present in the channel mask.
|
||||||
|
static void protectExtremeChannels(byte *stencil, const float *msd, float m, int mask) {
|
||||||
|
if (
|
||||||
|
(mask&RED && msd[0] != m) ||
|
||||||
|
(mask&GREEN && msd[1] != m) ||
|
||||||
|
(mask&BLUE && msd[2] != m)
|
||||||
|
)
|
||||||
|
*stencil |= (byte) MSDFErrorCorrection::PROTECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, N> &sdf) {
|
||||||
|
float radius;
|
||||||
|
// Horizontal texel pairs
|
||||||
|
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(invRange, 0)).length());
|
||||||
|
for (int y = 0; y < sdf.height; ++y) {
|
||||||
|
const float *left = sdf(0, y);
|
||||||
|
const float *right = sdf(1, y);
|
||||||
|
for (int x = 0; x < sdf.width-1; ++x) {
|
||||||
|
float lm = median(left[0], left[1], left[2]);
|
||||||
|
float rm = median(right[0], right[1], right[2]);
|
||||||
|
if (fabsf(lm-.5f)+fabsf(rm-.5f) < radius) {
|
||||||
|
int mask = edgeBetweenTexels(left, right);
|
||||||
|
protectExtremeChannels(stencil(x, y), left, lm, mask);
|
||||||
|
protectExtremeChannels(stencil(x+1, y), right, rm, mask);
|
||||||
|
}
|
||||||
|
left += N, right += N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Vertical texel pairs
|
||||||
|
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(0, invRange)).length());
|
||||||
|
for (int y = 0; y < sdf.height-1; ++y) {
|
||||||
|
const float *bottom = sdf(0, y);
|
||||||
|
const float *top = sdf(0, y+1);
|
||||||
|
for (int x = 0; x < sdf.width; ++x) {
|
||||||
|
float bm = median(bottom[0], bottom[1], bottom[2]);
|
||||||
|
float tm = median(top[0], top[1], top[2]);
|
||||||
|
if (fabsf(bm-.5f)+fabsf(tm-.5f) < radius) {
|
||||||
|
int mask = edgeBetweenTexels(bottom, top);
|
||||||
|
protectExtremeChannels(stencil(x, y), bottom, bm, mask);
|
||||||
|
protectExtremeChannels(stencil(x, y+1), top, tm, mask);
|
||||||
|
}
|
||||||
|
bottom += N, top += N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Diagonal texel pairs
|
||||||
|
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(invRange)).length());
|
||||||
|
for (int y = 0; y < sdf.height-1; ++y) {
|
||||||
|
const float *lb = sdf(0, y);
|
||||||
|
const float *rb = sdf(1, y);
|
||||||
|
const float *lt = sdf(0, y+1);
|
||||||
|
const float *rt = sdf(1, y+1);
|
||||||
|
for (int x = 0; x < sdf.width-1; ++x) {
|
||||||
|
float mlb = median(lb[0], lb[1], lb[2]);
|
||||||
|
float mrb = median(rb[0], rb[1], rb[2]);
|
||||||
|
float mlt = median(lt[0], lt[1], lt[2]);
|
||||||
|
float mrt = median(rt[0], rt[1], rt[2]);
|
||||||
|
if (fabsf(mlb-.5f)+fabsf(mrt-.5f) < radius) {
|
||||||
|
int mask = edgeBetweenTexels(lb, rt);
|
||||||
|
protectExtremeChannels(stencil(x, y), lb, mlb, mask);
|
||||||
|
protectExtremeChannels(stencil(x+1, y+1), rt, mrt, mask);
|
||||||
|
}
|
||||||
|
if (fabsf(mrb-.5f)+fabsf(mlt-.5f) < radius) {
|
||||||
|
int mask = edgeBetweenTexels(rb, lt);
|
||||||
|
protectExtremeChannels(stencil(x+1, y), rb, mrb, mask);
|
||||||
|
protectExtremeChannels(stencil(x, y+1), lt, mlt, mask);
|
||||||
|
}
|
||||||
|
lb += N, rb += N, lt += N, rt += N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MSDFErrorCorrection::protectAll() {
|
||||||
|
byte *end = stencil.pixels+stencil.width*stencil.height;
|
||||||
|
for (byte *mask = stencil.pixels; mask < end; ++mask)
|
||||||
|
*mask |= (byte) PROTECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the median of the linear interpolation of texels a, b at t.
|
||||||
|
static float interpolatedMedian(const float *a, const float *b, double t) {
|
||||||
|
return median(
|
||||||
|
mix(a[0], b[0], t),
|
||||||
|
mix(a[1], b[1], t),
|
||||||
|
mix(a[2], b[2], t)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/// Returns the median of the bilinear interpolation with the given constant, linear, and quadratic terms at t.
|
||||||
|
static float interpolatedMedian(const float *a, const float *l, const float *q, double t) {
|
||||||
|
return float(median(
|
||||||
|
t*(t*q[0]+l[0])+a[0],
|
||||||
|
t*(t*q[1]+l[1])+a[1],
|
||||||
|
t*(t*q[2]+l[2])+a[2]
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determines if the interpolated median xm is an artifact.
|
||||||
|
static bool isArtifact(bool isProtected, double axSpan, double bxSpan, float am, float bm, float xm) {
|
||||||
|
return (
|
||||||
|
// For protected texels, only report an artifact if it would cause fill inversion (change between positive and negative distance).
|
||||||
|
(!isProtected || (am > .5f && bm > .5f && xm <= .5f) || (am < .5f && bm < .5f && xm >= .5f)) &&
|
||||||
|
// This is an artifact if the interpolated median is outside the range of possible values based on its distance from a, b.
|
||||||
|
!(xm >= am-axSpan && xm <= am+axSpan && xm >= bm-bxSpan && xm <= bm+bxSpan)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a linear interpolation artifact will occur at a point where two specific color channels are equal - such points have extreme median values.
|
||||||
|
template <class ArtifactClassifier>
|
||||||
|
static bool hasLinearArtifactInner(const ArtifactClassifier &artifactClassifier, float am, float bm, const float *a, const float *b, float dA, float dB) {
|
||||||
|
// Find interpolation ratio t (0 < t < 1) where two color channels are equal (mix(dA, dB, t) == 0).
|
||||||
|
double t = (double) dA/(dA-dB);
|
||||||
|
if (t > ARTIFACT_T_EPSILON && t < 1-ARTIFACT_T_EPSILON) {
|
||||||
|
// Interpolate median at t and let the classifier decide if its value indicates an artifact.
|
||||||
|
float xm = interpolatedMedian(a, b, t);
|
||||||
|
return artifactClassifier.evaluate(t, xm, artifactClassifier.rangeTest(0, 1, t, am, bm, xm));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a bilinear interpolation artifact will occur at a point where two specific color channels are equal - such points have extreme median values.
|
||||||
|
template <class ArtifactClassifier>
|
||||||
|
static bool hasDiagonalArtifactInner(const ArtifactClassifier &artifactClassifier, float am, float dm, const float *a, const float *l, const float *q, float dA, float dBC, float dD, double tEx0, double tEx1) {
|
||||||
|
// Find interpolation ratios t (0 < t[i] < 1) where two color channels are equal.
|
||||||
|
double t[2];
|
||||||
|
int solutions = solveQuadratic(t, dD-dBC+dA, dBC-dA-dA, dA);
|
||||||
|
for (int i = 0; i < solutions; ++i) {
|
||||||
|
// Solutions t[i] == 0 and t[i] == 1 are singularities and occur very often because two channels are usually equal at texels.
|
||||||
|
if (t[i] > ARTIFACT_T_EPSILON && t[i] < 1-ARTIFACT_T_EPSILON) {
|
||||||
|
// Interpolate median xm at t.
|
||||||
|
float xm = interpolatedMedian(a, l, q, t[i]);
|
||||||
|
// Determine if xm deviates too much from medians of a, d.
|
||||||
|
int rangeFlags = artifactClassifier.rangeTest(0, 1, t[i], am, dm, xm);
|
||||||
|
// Additionally, check xm against the interpolated medians at the local extremes tEx0, tEx1.
|
||||||
|
double tEnd[2];
|
||||||
|
float em[2];
|
||||||
|
// tEx0
|
||||||
|
if (tEx0 > 0 && tEx0 < 1) {
|
||||||
|
tEnd[0] = 0, tEnd[1] = 1;
|
||||||
|
em[0] = am, em[1] = dm;
|
||||||
|
tEnd[tEx0 > t[i]] = tEx0;
|
||||||
|
em[tEx0 > t[i]] = interpolatedMedian(a, l, q, tEx0);
|
||||||
|
rangeFlags |= artifactClassifier.rangeTest(tEnd[0], tEnd[1], t[i], am, dm, xm);
|
||||||
|
}
|
||||||
|
// tEx1
|
||||||
|
if (tEx1 > 0 && tEx1 < 1) {
|
||||||
|
tEnd[0] = 0, tEnd[1] = 1;
|
||||||
|
em[0] = am, em[1] = dm;
|
||||||
|
tEnd[tEx1 > t[i]] = tEx1;
|
||||||
|
em[tEx1 > t[i]] = interpolatedMedian(a, l, q, tEx1);
|
||||||
|
rangeFlags |= artifactClassifier.rangeTest(tEnd[0], tEnd[1], t[i], am, dm, xm);
|
||||||
|
}
|
||||||
|
if (artifactClassifier.evaluate(t[i], xm, rangeFlags))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a linear interpolation artifact will occur inbetween two horizontally or vertically adjacent texels a, b.
|
||||||
|
template <class ArtifactClassifier>
|
||||||
|
static bool hasLinearArtifact(const ArtifactClassifier &artifactClassifier, float am, const float *a, const float *b) {
|
||||||
|
float bm = median(b[0], b[1], b[2]);
|
||||||
|
return (
|
||||||
|
// Out of the pair, only report artifacts for the texel further from the edge to minimize side effects.
|
||||||
|
fabsf(am-.5f) >= fabsf(bm-.5f) && (
|
||||||
|
// Check points where each pair of color channels meets.
|
||||||
|
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[1]-a[0], b[1]-b[0]) ||
|
||||||
|
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[2]-a[1], b[2]-b[1]) ||
|
||||||
|
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[0]-a[2], b[0]-b[2])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a bilinear interpolation artifact will occur inbetween two diagonally adjacent texels a, d (with b, c forming the other diagonal).
|
||||||
|
template <class ArtifactClassifier>
|
||||||
|
static bool hasDiagonalArtifact(const ArtifactClassifier &artifactClassifier, float am, const float *a, const float *b, const float *c, const float *d) {
|
||||||
|
float dm = median(d[0], d[1], d[2]);
|
||||||
|
// Out of the pair, only report artifacts for the texel further from the edge to minimize side effects.
|
||||||
|
if (fabsf(am-.5f) >= fabsf(dm-.5f)) {
|
||||||
|
float abc[3] = {
|
||||||
|
a[0]-b[0]-c[0],
|
||||||
|
a[1]-b[1]-c[1],
|
||||||
|
a[2]-b[2]-c[2]
|
||||||
|
};
|
||||||
|
// Compute the linear terms for bilinear interpolation.
|
||||||
|
float l[3] = {
|
||||||
|
-a[0]-abc[0],
|
||||||
|
-a[1]-abc[1],
|
||||||
|
-a[2]-abc[2]
|
||||||
|
};
|
||||||
|
// Compute the quadratic terms for bilinear interpolation.
|
||||||
|
float q[3] = {
|
||||||
|
d[0]+abc[0],
|
||||||
|
d[1]+abc[1],
|
||||||
|
d[2]+abc[2]
|
||||||
|
};
|
||||||
|
// Compute interpolation ratios tEx (0 < tEx[i] < 1) for the local extremes of each color channel (the derivative 2*q[i]*tEx[i]+l[i] == 0).
|
||||||
|
double tEx[3] = {
|
||||||
|
-.5*l[0]/q[0],
|
||||||
|
-.5*l[1]/q[1],
|
||||||
|
-.5*l[2]/q[2]
|
||||||
|
};
|
||||||
|
// Check points where each pair of color channels meets.
|
||||||
|
return (
|
||||||
|
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[1]-a[0], b[1]-b[0]+c[1]-c[0], d[1]-d[0], tEx[0], tEx[1]) ||
|
||||||
|
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[2]-a[1], b[2]-b[1]+c[2]-c[1], d[2]-d[1], tEx[1], tEx[2]) ||
|
||||||
|
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[0]-a[2], b[0]-b[2]+c[0]-c[2], d[0]-d[2], tEx[2], tEx[0])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, N> &sdf) {
|
||||||
|
// Compute the expected deltas between values of horizontally, vertically, and diagonally adjacent texels.
|
||||||
|
double hSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange, 0)).length();
|
||||||
|
double vSpan = minDeviationRatio*projection.unprojectVector(Vector2(0, invRange)).length();
|
||||||
|
double dSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange)).length();
|
||||||
|
// Inspect all texels.
|
||||||
|
for (int y = 0; y < sdf.height; ++y) {
|
||||||
|
for (int x = 0; x < sdf.width; ++x) {
|
||||||
|
const float *c = sdf(x, y);
|
||||||
|
float cm = median(c[0], c[1], c[2]);
|
||||||
|
bool protectedFlag = (*stencil(x, y)&PROTECTED) != 0;
|
||||||
|
const float *l = NULL, *b = NULL, *r = NULL, *t = NULL;
|
||||||
|
// Mark current texel c with the error flag if an artifact occurs when it's interpolated with any of its 8 neighbors.
|
||||||
|
*stencil(x, y) |= (byte) (ERROR*(
|
||||||
|
(x > 0 && ((l = sdf(x-1, y)), hasLinearArtifact(BaseArtifactClassifier(hSpan, protectedFlag), cm, c, l))) ||
|
||||||
|
(y > 0 && ((b = sdf(x, y-1)), hasLinearArtifact(BaseArtifactClassifier(vSpan, protectedFlag), cm, c, b))) ||
|
||||||
|
(x < sdf.width-1 && ((r = sdf(x+1, y)), hasLinearArtifact(BaseArtifactClassifier(hSpan, protectedFlag), cm, c, r))) ||
|
||||||
|
(y < sdf.height-1 && ((t = sdf(x, y+1)), hasLinearArtifact(BaseArtifactClassifier(vSpan, protectedFlag), cm, c, t))) ||
|
||||||
|
(x > 0 && y > 0 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, l, b, sdf(x-1, y-1))) ||
|
||||||
|
(x < sdf.width-1 && y > 0 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, r, b, sdf(x+1, y-1))) ||
|
||||||
|
(x > 0 && y < sdf.height-1 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, l, t, sdf(x-1, y+1))) ||
|
||||||
|
(x < sdf.width-1 && y < sdf.height-1 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, r, t, sdf(x+1, y+1)))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <template <typename> class ContourCombiner, int N>
|
||||||
|
void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, N> &sdf, const Shape &shape) {
|
||||||
|
// Compute the expected deltas between values of horizontally, vertically, and diagonally adjacent texels.
|
||||||
|
double hSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange, 0)).length();
|
||||||
|
double vSpan = minDeviationRatio*projection.unprojectVector(Vector2(0, invRange)).length();
|
||||||
|
double dSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange)).length();
|
||||||
|
#ifdef MSDFGEN_USE_OPENMP
|
||||||
|
#pragma omp parallel
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
ShapeDistanceChecker<ContourCombiner, N> shapeDistanceChecker(sdf, shape, projection, invRange, minImproveRatio);
|
||||||
|
bool rightToLeft = false;
|
||||||
|
// Inspect all texels.
|
||||||
|
#ifdef MSDFGEN_USE_OPENMP
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (int y = 0; y < sdf.height; ++y) {
|
||||||
|
int row = shape.inverseYAxis ? sdf.height-y-1 : y;
|
||||||
|
for (int col = 0; col < sdf.width; ++col) {
|
||||||
|
int x = rightToLeft ? sdf.width-col-1 : col;
|
||||||
|
if ((*stencil(x, row)&ERROR))
|
||||||
|
continue;
|
||||||
|
const float *c = sdf(x, row);
|
||||||
|
shapeDistanceChecker.shapeCoord = projection.unproject(Point2(x+.5, y+.5));
|
||||||
|
shapeDistanceChecker.sdfCoord = Point2(x+.5, row+.5);
|
||||||
|
shapeDistanceChecker.msd = c;
|
||||||
|
shapeDistanceChecker.protectedFlag = (*stencil(x, row)&PROTECTED) != 0;
|
||||||
|
float cm = median(c[0], c[1], c[2]);
|
||||||
|
const float *l = NULL, *b = NULL, *r = NULL, *t = NULL;
|
||||||
|
// Mark current texel c with the error flag if an artifact occurs when it's interpolated with any of its 8 neighbors.
|
||||||
|
*stencil(x, row) |= (byte) (ERROR*(
|
||||||
|
(x > 0 && ((l = sdf(x-1, row)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(-1, 0), hSpan), cm, c, l))) ||
|
||||||
|
(row > 0 && ((b = sdf(x, row-1)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(0, -1), vSpan), cm, c, b))) ||
|
||||||
|
(x < sdf.width-1 && ((r = sdf(x+1, row)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(+1, 0), hSpan), cm, c, r))) ||
|
||||||
|
(row < sdf.height-1 && ((t = sdf(x, row+1)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(0, +1), vSpan), cm, c, t))) ||
|
||||||
|
(x > 0 && row > 0 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(-1, -1), dSpan), cm, c, l, b, sdf(x-1, row-1))) ||
|
||||||
|
(x < sdf.width-1 && row > 0 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(+1, -1), dSpan), cm, c, r, b, sdf(x+1, row-1))) ||
|
||||||
|
(x > 0 && row < sdf.height-1 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(-1, +1), dSpan), cm, c, l, t, sdf(x-1, row+1))) ||
|
||||||
|
(x < sdf.width-1 && row < sdf.height-1 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(+1, +1), dSpan), cm, c, r, t, sdf(x+1, row+1)))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int N>
|
||||||
|
void MSDFErrorCorrection::apply(const BitmapRef<float, N> &sdf) const {
|
||||||
|
int texelCount = sdf.width*sdf.height;
|
||||||
|
const byte *mask = stencil.pixels;
|
||||||
|
float *texel = sdf.pixels;
|
||||||
|
for (int i = 0; i < texelCount; ++i) {
|
||||||
|
if (*mask&ERROR) {
|
||||||
|
// Set all color channels to the median.
|
||||||
|
float m = median(texel[0], texel[1], texel[2]);
|
||||||
|
texel[0] = m, texel[1] = m, texel[2] = m;
|
||||||
|
}
|
||||||
|
++mask;
|
||||||
|
texel += N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BitmapConstRef<byte, 1> MSDFErrorCorrection::getStencil() const {
|
||||||
|
return stencil;
|
||||||
|
}
|
||||||
|
|
||||||
|
template void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, 3> &sdf);
|
||||||
|
template void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, 4> &sdf);
|
||||||
|
template void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, 3> &sdf);
|
||||||
|
template void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, 4> &sdf);
|
||||||
|
template void MSDFErrorCorrection::findErrors<SimpleContourCombiner>(const BitmapConstRef<float, 3> &sdf, const Shape &shape);
|
||||||
|
template void MSDFErrorCorrection::findErrors<SimpleContourCombiner>(const BitmapConstRef<float, 4> &sdf, const Shape &shape);
|
||||||
|
template void MSDFErrorCorrection::findErrors<OverlappingContourCombiner>(const BitmapConstRef<float, 3> &sdf, const Shape &shape);
|
||||||
|
template void MSDFErrorCorrection::findErrors<OverlappingContourCombiner>(const BitmapConstRef<float, 4> &sdf, const Shape &shape);
|
||||||
|
template void MSDFErrorCorrection::apply(const BitmapRef<float, 3> &sdf) const;
|
||||||
|
template void MSDFErrorCorrection::apply(const BitmapRef<float, 4> &sdf) const;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Projection.h"
|
||||||
|
#include "Shape.h"
|
||||||
|
#include "BitmapRef.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Performs error correction on a computed MSDF to eliminate interpolation artifacts. This is a low-level class, you may want to use the API in msdf-error-correction.h instead.
|
||||||
|
class MSDFErrorCorrection {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// Stencil flags.
|
||||||
|
enum Flags {
|
||||||
|
/// Texel marked as potentially causing interpolation errors.
|
||||||
|
ERROR = 1,
|
||||||
|
/// Texel marked as protected. Protected texels are only given the error flag if they cause inversion artifacts.
|
||||||
|
PROTECTED = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
MSDFErrorCorrection();
|
||||||
|
explicit MSDFErrorCorrection(const BitmapRef<byte, 1> &stencil, const Projection &projection, double range);
|
||||||
|
/// Sets the minimum ratio between the actual and maximum expected distance delta to be considered an error.
|
||||||
|
void setMinDeviationRatio(double minDeviationRatio);
|
||||||
|
/// Sets the minimum ratio between the pre-correction distance error and the post-correction distance error.
|
||||||
|
void setMinImproveRatio(double minImproveRatio);
|
||||||
|
/// Flags all texels that are interpolated at corners as protected.
|
||||||
|
void protectCorners(const Shape &shape);
|
||||||
|
/// Flags all texels that contribute to edges as protected.
|
||||||
|
template <int N>
|
||||||
|
void protectEdges(const BitmapConstRef<float, N> &sdf);
|
||||||
|
/// Flags all texels as protected.
|
||||||
|
void protectAll();
|
||||||
|
/// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF only.
|
||||||
|
template <int N>
|
||||||
|
void findErrors(const BitmapConstRef<float, N> &sdf);
|
||||||
|
/// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF and comparison with the exact shape distance.
|
||||||
|
template <template <typename> class ContourCombiner, int N>
|
||||||
|
void findErrors(const BitmapConstRef<float, N> &sdf, const Shape &shape);
|
||||||
|
/// Modifies the MSDF so that all texels with the error flag are converted to single-channel.
|
||||||
|
template <int N>
|
||||||
|
void apply(const BitmapRef<float, N> &sdf) const;
|
||||||
|
/// Returns the stencil in its current state (see Flags).
|
||||||
|
BitmapConstRef<byte, 1> getStencil() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BitmapRef<byte, 1> stencil;
|
||||||
|
Projection projection;
|
||||||
|
double invRange;
|
||||||
|
double minDeviationRatio;
|
||||||
|
double minImproveRatio;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
42
libs/msdf-atlas-gen/include/msdfgen/core/Projection.cpp
Normal file
42
libs/msdf-atlas-gen/include/msdfgen/core/Projection.cpp
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
#include "Projection.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
Projection::Projection() : scale(1), translate(0) { }
|
||||||
|
|
||||||
|
Projection::Projection(const Vector2 &scale, const Vector2 &translate) : scale(scale), translate(translate) { }
|
||||||
|
|
||||||
|
Point2 Projection::project(const Point2 &coord) const {
|
||||||
|
return scale*(coord+translate);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point2 Projection::unproject(const Point2 &coord) const {
|
||||||
|
return coord/scale-translate;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Projection::projectVector(const Vector2 &vector) const {
|
||||||
|
return scale*vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Projection::unprojectVector(const Vector2 &vector) const {
|
||||||
|
return vector/scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Projection::projectX(double x) const {
|
||||||
|
return scale.x*(x+translate.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
double Projection::projectY(double y) const {
|
||||||
|
return scale.y*(y+translate.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
double Projection::unprojectX(double x) const {
|
||||||
|
return x/scale.x-translate.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Projection::unprojectY(double y) const {
|
||||||
|
return y/scale.y-translate.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
37
libs/msdf-atlas-gen/include/msdfgen/core/Projection.h
Normal file
37
libs/msdf-atlas-gen/include/msdfgen/core/Projection.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Vector2.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// A transformation from shape coordinates to pixel coordinates.
|
||||||
|
class Projection {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Projection();
|
||||||
|
Projection(const Vector2 &scale, const Vector2 &translate);
|
||||||
|
/// Converts the shape coordinate to pixel coordinate.
|
||||||
|
Point2 project(const Point2 &coord) const;
|
||||||
|
/// Converts the pixel coordinate to shape coordinate.
|
||||||
|
Point2 unproject(const Point2 &coord) const;
|
||||||
|
/// Converts the vector to pixel coordinate space.
|
||||||
|
Vector2 projectVector(const Vector2 &vector) const;
|
||||||
|
/// Converts the vector from pixel coordinate space.
|
||||||
|
Vector2 unprojectVector(const Vector2 &vector) const;
|
||||||
|
/// Converts the X-coordinate from shape to pixel coordinate space.
|
||||||
|
double projectX(double x) const;
|
||||||
|
/// Converts the Y-coordinate from shape to pixel coordinate space.
|
||||||
|
double projectY(double y) const;
|
||||||
|
/// Converts the X-coordinate from pixel to shape coordinate space.
|
||||||
|
double unprojectX(double x) const;
|
||||||
|
/// Converts the Y-coordinate from pixel to shape coordinate space.
|
||||||
|
double unprojectY(double y) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector2 scale;
|
||||||
|
Vector2 translate;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
125
libs/msdf-atlas-gen/include/msdfgen/core/Scanline.cpp
Normal file
125
libs/msdf-atlas-gen/include/msdfgen/core/Scanline.cpp
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
|
||||||
|
#include "Scanline.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
static int compareIntersections(const void *a, const void *b) {
|
||||||
|
return sign(reinterpret_cast<const Scanline::Intersection *>(a)->x-reinterpret_cast<const Scanline::Intersection *>(b)->x);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool interpretFillRule(int intersections, FillRule fillRule) {
|
||||||
|
switch (fillRule) {
|
||||||
|
case FILL_NONZERO:
|
||||||
|
return intersections != 0;
|
||||||
|
case FILL_ODD:
|
||||||
|
return intersections&1;
|
||||||
|
case FILL_POSITIVE:
|
||||||
|
return intersections > 0;
|
||||||
|
case FILL_NEGATIVE:
|
||||||
|
return intersections < 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Scanline::overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule) {
|
||||||
|
double total = 0;
|
||||||
|
bool aInside = false, bInside = false;
|
||||||
|
int ai = 0, bi = 0;
|
||||||
|
double ax = !a.intersections.empty() ? a.intersections[ai].x : xTo;
|
||||||
|
double bx = !b.intersections.empty() ? b.intersections[bi].x : xTo;
|
||||||
|
while (ax < xFrom || bx < xFrom) {
|
||||||
|
double xNext = min(ax, bx);
|
||||||
|
if (ax == xNext && ai < (int) a.intersections.size()) {
|
||||||
|
aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
|
||||||
|
ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
|
||||||
|
}
|
||||||
|
if (bx == xNext && bi < (int) b.intersections.size()) {
|
||||||
|
bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
|
||||||
|
bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double x = xFrom;
|
||||||
|
while (ax < xTo || bx < xTo) {
|
||||||
|
double xNext = min(ax, bx);
|
||||||
|
if (aInside == bInside)
|
||||||
|
total += xNext-x;
|
||||||
|
if (ax == xNext && ai < (int) a.intersections.size()) {
|
||||||
|
aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
|
||||||
|
ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
|
||||||
|
}
|
||||||
|
if (bx == xNext && bi < (int) b.intersections.size()) {
|
||||||
|
bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
|
||||||
|
bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
|
||||||
|
}
|
||||||
|
x = xNext;
|
||||||
|
}
|
||||||
|
if (aInside == bInside)
|
||||||
|
total += xTo-x;
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
Scanline::Scanline() : lastIndex(0) { }
|
||||||
|
|
||||||
|
void Scanline::preprocess() {
|
||||||
|
lastIndex = 0;
|
||||||
|
if (!intersections.empty()) {
|
||||||
|
qsort(&intersections[0], intersections.size(), sizeof(Intersection), compareIntersections);
|
||||||
|
int totalDirection = 0;
|
||||||
|
for (std::vector<Intersection>::iterator intersection = intersections.begin(); intersection != intersections.end(); ++intersection) {
|
||||||
|
totalDirection += intersection->direction;
|
||||||
|
intersection->direction = totalDirection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scanline::setIntersections(const std::vector<Intersection> &intersections) {
|
||||||
|
this->intersections = intersections;
|
||||||
|
preprocess();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void Scanline::setIntersections(std::vector<Intersection> &&intersections) {
|
||||||
|
this->intersections = (std::vector<Intersection> &&) intersections;
|
||||||
|
preprocess();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int Scanline::moveTo(double x) const {
|
||||||
|
if (intersections.empty())
|
||||||
|
return -1;
|
||||||
|
int index = lastIndex;
|
||||||
|
if (x < intersections[index].x) {
|
||||||
|
do {
|
||||||
|
if (index == 0) {
|
||||||
|
lastIndex = 0;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
--index;
|
||||||
|
} while (x < intersections[index].x);
|
||||||
|
} else {
|
||||||
|
while (index < (int) intersections.size()-1 && x >= intersections[index+1].x)
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
lastIndex = index;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Scanline::countIntersections(double x) const {
|
||||||
|
return moveTo(x)+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Scanline::sumIntersections(double x) const {
|
||||||
|
int index = moveTo(x);
|
||||||
|
if (index >= 0)
|
||||||
|
return intersections[index].direction;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Scanline::filled(double x, FillRule fillRule) const {
|
||||||
|
return interpretFillRule(sumIntersections(x), fillRule);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
55
libs/msdf-atlas-gen/include/msdfgen/core/Scanline.h
Normal file
55
libs/msdf-atlas-gen/include/msdfgen/core/Scanline.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Fill rule dictates how intersection total is interpreted during rasterization.
|
||||||
|
enum FillRule {
|
||||||
|
FILL_NONZERO,
|
||||||
|
FILL_ODD, // "even-odd"
|
||||||
|
FILL_POSITIVE,
|
||||||
|
FILL_NEGATIVE
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Resolves the number of intersection into a binary fill value based on fill rule.
|
||||||
|
bool interpretFillRule(int intersections, FillRule fillRule);
|
||||||
|
|
||||||
|
/// Represents a horizontal scanline intersecting a shape.
|
||||||
|
class Scanline {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// An intersection with the scanline.
|
||||||
|
struct Intersection {
|
||||||
|
/// X coordinate.
|
||||||
|
double x;
|
||||||
|
/// Normalized Y direction of the oriented edge at the point of intersection.
|
||||||
|
int direction;
|
||||||
|
};
|
||||||
|
|
||||||
|
static double overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule);
|
||||||
|
|
||||||
|
Scanline();
|
||||||
|
/// Populates the intersection list.
|
||||||
|
void setIntersections(const std::vector<Intersection> &intersections);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void setIntersections(std::vector<Intersection> &&intersections);
|
||||||
|
#endif
|
||||||
|
/// Returns the number of intersections left of x.
|
||||||
|
int countIntersections(double x) const;
|
||||||
|
/// Returns the total sign of intersections left of x.
|
||||||
|
int sumIntersections(double x) const;
|
||||||
|
/// Decides whether the scanline is filled at x based on fill rule.
|
||||||
|
bool filled(double x, FillRule fillRule) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Intersection> intersections;
|
||||||
|
mutable int lastIndex;
|
||||||
|
|
||||||
|
void preprocess();
|
||||||
|
int moveTo(double x) const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
183
libs/msdf-atlas-gen/include/msdfgen/core/Shape.cpp
Normal file
183
libs/msdf-atlas-gen/include/msdfgen/core/Shape.cpp
Normal file
|
@ -0,0 +1,183 @@
|
||||||
|
|
||||||
|
#include "Shape.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
Shape::Shape() : inverseYAxis(false) { }
|
||||||
|
|
||||||
|
void Shape::addContour(const Contour &contour) {
|
||||||
|
contours.push_back(contour);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void Shape::addContour(Contour &&contour) {
|
||||||
|
contours.push_back((Contour &&) contour);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Contour & Shape::addContour() {
|
||||||
|
contours.resize(contours.size()+1);
|
||||||
|
return contours.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Shape::validate() const {
|
||||||
|
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
Point2 corner = contour->edges.back()->point(1);
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
if (!*edge)
|
||||||
|
return false;
|
||||||
|
if ((*edge)->point(0) != corner)
|
||||||
|
return false;
|
||||||
|
corner = (*edge)->point(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deconvergeEdge(EdgeHolder &edgeHolder, int param) {
|
||||||
|
{
|
||||||
|
const QuadraticSegment *quadraticSegment = dynamic_cast<const QuadraticSegment *>(&*edgeHolder);
|
||||||
|
if (quadraticSegment)
|
||||||
|
edgeHolder = quadraticSegment->convertToCubic();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
CubicSegment *cubicSegment = dynamic_cast<CubicSegment *>(&*edgeHolder);
|
||||||
|
if (cubicSegment)
|
||||||
|
cubicSegment->deconverge(param, MSDFGEN_DECONVERGENCE_FACTOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::normalize() {
|
||||||
|
for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour) {
|
||||||
|
if (contour->edges.size() == 1) {
|
||||||
|
EdgeSegment *parts[3] = { };
|
||||||
|
contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
|
||||||
|
contour->edges.clear();
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[0]));
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[1]));
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[2]));
|
||||||
|
} else {
|
||||||
|
EdgeHolder *prevEdge = &contour->edges.back();
|
||||||
|
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
Vector2 prevDir = (*prevEdge)->direction(1).normalize();
|
||||||
|
Vector2 curDir = (*edge)->direction(0).normalize();
|
||||||
|
if (dotProduct(prevDir, curDir) < MSDFGEN_CORNER_DOT_EPSILON-1) {
|
||||||
|
deconvergeEdge(*prevEdge, 1);
|
||||||
|
deconvergeEdge(*edge, 0);
|
||||||
|
}
|
||||||
|
prevEdge = &*edge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::bound(double &l, double &b, double &r, double &t) const {
|
||||||
|
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
|
||||||
|
contour->bound(l, b, r, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
|
||||||
|
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
|
||||||
|
contour->boundMiters(l, b, r, t, border, miterLimit, polarity);
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape::Bounds Shape::getBounds(double border, double miterLimit, int polarity) const {
|
||||||
|
static const double LARGE_VALUE = 1e240;
|
||||||
|
Shape::Bounds bounds = { +LARGE_VALUE, +LARGE_VALUE, -LARGE_VALUE, -LARGE_VALUE };
|
||||||
|
bound(bounds.l, bounds.b, bounds.r, bounds.t);
|
||||||
|
if (border > 0) {
|
||||||
|
bounds.l -= border, bounds.b -= border;
|
||||||
|
bounds.r += border, bounds.t += border;
|
||||||
|
if (miterLimit > 0)
|
||||||
|
boundMiters(bounds.l, bounds.b, bounds.r, bounds.t, border, miterLimit, polarity);
|
||||||
|
}
|
||||||
|
return bounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::scanline(Scanline &line, double y) const {
|
||||||
|
std::vector<Scanline::Intersection> intersections;
|
||||||
|
double x[3];
|
||||||
|
int dy[3];
|
||||||
|
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
int n = (*edge)->scanlineIntersections(x, dy, y);
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
Scanline::Intersection intersection = { x[i], dy[i] };
|
||||||
|
intersections.push_back(intersection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
|
||||||
|
#else
|
||||||
|
line.setIntersections(intersections);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int Shape::edgeCount() const {
|
||||||
|
int total = 0;
|
||||||
|
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
|
||||||
|
total += (int) contour->edges.size();
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::orientContours() {
|
||||||
|
struct Intersection {
|
||||||
|
double x;
|
||||||
|
int direction;
|
||||||
|
int contourIndex;
|
||||||
|
|
||||||
|
static int compare(const void *a, const void *b) {
|
||||||
|
return sign(reinterpret_cast<const Intersection *>(a)->x-reinterpret_cast<const Intersection *>(b)->x);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const double ratio = .5*(sqrt(5)-1); // an irrational number to minimize chance of intersecting a corner or other point of interest
|
||||||
|
std::vector<int> orientations(contours.size());
|
||||||
|
std::vector<Intersection> intersections;
|
||||||
|
for (int i = 0; i < (int) contours.size(); ++i) {
|
||||||
|
if (!orientations[i] && !contours[i].edges.empty()) {
|
||||||
|
// Find an Y that crosses the contour
|
||||||
|
double y0 = contours[i].edges.front()->point(0).y;
|
||||||
|
double y1 = y0;
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
|
||||||
|
y1 = (*edge)->point(1).y;
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
|
||||||
|
y1 = (*edge)->point(ratio).y; // in case all endpoints are in a horizontal line
|
||||||
|
double y = mix(y0, y1, ratio);
|
||||||
|
// Scanline through whole shape at Y
|
||||||
|
double x[3];
|
||||||
|
int dy[3];
|
||||||
|
for (int j = 0; j < (int) contours.size(); ++j) {
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contours[j].edges.begin(); edge != contours[j].edges.end(); ++edge) {
|
||||||
|
int n = (*edge)->scanlineIntersections(x, dy, y);
|
||||||
|
for (int k = 0; k < n; ++k) {
|
||||||
|
Intersection intersection = { x[k], dy[k], j };
|
||||||
|
intersections.push_back(intersection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qsort(&intersections[0], intersections.size(), sizeof(Intersection), &Intersection::compare);
|
||||||
|
// Disqualify multiple intersections
|
||||||
|
for (int j = 1; j < (int) intersections.size(); ++j)
|
||||||
|
if (intersections[j].x == intersections[j-1].x)
|
||||||
|
intersections[j].direction = intersections[j-1].direction = 0;
|
||||||
|
// Inspect scanline and deduce orientations of intersected contours
|
||||||
|
for (int j = 0; j < (int) intersections.size(); ++j)
|
||||||
|
if (intersections[j].direction)
|
||||||
|
orientations[intersections[j].contourIndex] += 2*((j&1)^(intersections[j].direction > 0))-1;
|
||||||
|
intersections.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Reverse contours that have the opposite orientation
|
||||||
|
for (int i = 0; i < (int) contours.size(); ++i)
|
||||||
|
if (orientations[i] < 0)
|
||||||
|
contours[i].reverse();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
55
libs/msdf-atlas-gen/include/msdfgen/core/Shape.h
Normal file
55
libs/msdf-atlas-gen/include/msdfgen/core/Shape.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "Contour.h"
|
||||||
|
#include "Scanline.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
// Threshold of the dot product of adjacent edge directions to be considered convergent.
|
||||||
|
#define MSDFGEN_CORNER_DOT_EPSILON .000001
|
||||||
|
// The proportional amount by which a curve's control point will be adjusted to eliminate convergent corners.
|
||||||
|
#define MSDFGEN_DECONVERGENCE_FACTOR .000001
|
||||||
|
|
||||||
|
/// Vector shape representation.
|
||||||
|
class Shape {
|
||||||
|
|
||||||
|
public:
|
||||||
|
struct Bounds {
|
||||||
|
double l, b, r, t;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The list of contours the shape consists of.
|
||||||
|
std::vector<Contour> contours;
|
||||||
|
/// Specifies whether the shape uses bottom-to-top (false) or top-to-bottom (true) Y coordinates.
|
||||||
|
bool inverseYAxis;
|
||||||
|
|
||||||
|
Shape();
|
||||||
|
/// Adds a contour.
|
||||||
|
void addContour(const Contour &contour);
|
||||||
|
#ifdef MSDFGEN_USE_CPP11
|
||||||
|
void addContour(Contour &&contour);
|
||||||
|
#endif
|
||||||
|
/// Adds a blank contour and returns its reference.
|
||||||
|
Contour & addContour();
|
||||||
|
/// Normalizes the shape geometry for distance field generation.
|
||||||
|
void normalize();
|
||||||
|
/// Performs basic checks to determine if the object represents a valid shape.
|
||||||
|
bool validate() const;
|
||||||
|
/// Adjusts the bounding box to fit the shape.
|
||||||
|
void bound(double &l, double &b, double &r, double &t) const;
|
||||||
|
/// Adjusts the bounding box to fit the shape border's mitered corners.
|
||||||
|
void boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const;
|
||||||
|
/// Computes the minimum bounding box that fits the shape, optionally with a (mitered) border.
|
||||||
|
Bounds getBounds(double border = 0, double miterLimit = 0, int polarity = 0) const;
|
||||||
|
/// Outputs the scanline that intersects the shape at y.
|
||||||
|
void scanline(Scanline &line, double y) const;
|
||||||
|
/// Returns the total number of edge segments
|
||||||
|
int edgeCount() const;
|
||||||
|
/// Assumes its contours are unoriented (even-odd fill rule). Attempts to orient them to conform to the non-zero winding rule.
|
||||||
|
void orientContours();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "Vector2.h"
|
||||||
|
#include "edge-selectors.h"
|
||||||
|
#include "contour-combiners.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Finds the distance between a point and a Shape. ContourCombiner dictates the distance metric and its data type.
|
||||||
|
template <class ContourCombiner>
|
||||||
|
class ShapeDistanceFinder {
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef typename ContourCombiner::DistanceType DistanceType;
|
||||||
|
|
||||||
|
// Passed shape object must persist until the distance finder is destroyed!
|
||||||
|
explicit ShapeDistanceFinder(const Shape &shape);
|
||||||
|
/// Finds the distance from origin. Not thread-safe! Is fastest when subsequent queries are close together.
|
||||||
|
DistanceType distance(const Point2 &origin);
|
||||||
|
|
||||||
|
/// Finds the distance between shape and origin. Does not allocate result cache used to optimize performance of multiple queries.
|
||||||
|
static DistanceType oneShotDistance(const Shape &shape, const Point2 &origin);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Shape &shape;
|
||||||
|
ContourCombiner contourCombiner;
|
||||||
|
std::vector<typename ContourCombiner::EdgeSelectorType::EdgeCache> shapeEdgeCache;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ShapeDistanceFinder<SimpleContourCombiner<TrueDistanceSelector> > SimpleTrueShapeDistanceFinder;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "ShapeDistanceFinder.hpp"
|
|
@ -0,0 +1,56 @@
|
||||||
|
|
||||||
|
#include "ShapeDistanceFinder.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
template <class ContourCombiner>
|
||||||
|
ShapeDistanceFinder<ContourCombiner>::ShapeDistanceFinder(const Shape &shape) : shape(shape), contourCombiner(shape), shapeEdgeCache(shape.edgeCount()) { }
|
||||||
|
|
||||||
|
template <class ContourCombiner>
|
||||||
|
typename ShapeDistanceFinder<ContourCombiner>::DistanceType ShapeDistanceFinder<ContourCombiner>::distance(const Point2 &origin) {
|
||||||
|
contourCombiner.reset(origin);
|
||||||
|
typename ContourCombiner::EdgeSelectorType::EdgeCache *edgeCache = &shapeEdgeCache[0];
|
||||||
|
|
||||||
|
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
typename ContourCombiner::EdgeSelectorType &edgeSelector = contourCombiner.edgeSelector(int(contour-shape.contours.begin()));
|
||||||
|
|
||||||
|
const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
|
||||||
|
const EdgeSegment *curEdge = contour->edges.back();
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
const EdgeSegment *nextEdge = *edge;
|
||||||
|
edgeSelector.addEdge(*edgeCache++, prevEdge, curEdge, nextEdge);
|
||||||
|
prevEdge = curEdge;
|
||||||
|
curEdge = nextEdge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contourCombiner.distance();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class ContourCombiner>
|
||||||
|
typename ShapeDistanceFinder<ContourCombiner>::DistanceType ShapeDistanceFinder<ContourCombiner>::oneShotDistance(const Shape &shape, const Point2 &origin) {
|
||||||
|
ContourCombiner contourCombiner(shape);
|
||||||
|
contourCombiner.reset(origin);
|
||||||
|
|
||||||
|
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
typename ContourCombiner::EdgeSelectorType &edgeSelector = contourCombiner.edgeSelector(int(contour-shape.contours.begin()));
|
||||||
|
|
||||||
|
const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
|
||||||
|
const EdgeSegment *curEdge = contour->edges.back();
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
|
||||||
|
const EdgeSegment *nextEdge = *edge;
|
||||||
|
typename ContourCombiner::EdgeSelectorType::EdgeCache dummy;
|
||||||
|
edgeSelector.addEdge(dummy, prevEdge, curEdge, nextEdge);
|
||||||
|
prevEdge = curEdge;
|
||||||
|
curEdge = nextEdge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contourCombiner.distance();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
libs/msdf-atlas-gen/include/msdfgen/core/SignedDistance.cpp
Normal file
29
libs/msdf-atlas-gen/include/msdfgen/core/SignedDistance.cpp
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
|
||||||
|
#include "SignedDistance.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cfloat>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
SignedDistance::SignedDistance() : distance(-DBL_MAX), dot(1) { }
|
||||||
|
|
||||||
|
SignedDistance::SignedDistance(double dist, double d) : distance(dist), dot(d) { }
|
||||||
|
|
||||||
|
bool operator<(SignedDistance a, SignedDistance b) {
|
||||||
|
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot < b.dot);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>(SignedDistance a, SignedDistance b) {
|
||||||
|
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot > b.dot);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<=(SignedDistance a, SignedDistance b) {
|
||||||
|
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot <= b.dot);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>=(SignedDistance a, SignedDistance b) {
|
||||||
|
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot >= b.dot);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
libs/msdf-atlas-gen/include/msdfgen/core/SignedDistance.h
Normal file
23
libs/msdf-atlas-gen/include/msdfgen/core/SignedDistance.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Represents a signed distance and alignment, which together can be compared to uniquely determine the closest edge segment.
|
||||||
|
class SignedDistance {
|
||||||
|
|
||||||
|
public:
|
||||||
|
double distance;
|
||||||
|
double dot;
|
||||||
|
|
||||||
|
SignedDistance();
|
||||||
|
SignedDistance(double dist, double d);
|
||||||
|
|
||||||
|
friend bool operator<(SignedDistance a, SignedDistance b);
|
||||||
|
friend bool operator>(SignedDistance a, SignedDistance b);
|
||||||
|
friend bool operator<=(SignedDistance a, SignedDistance b);
|
||||||
|
friend bool operator>=(SignedDistance a, SignedDistance b);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
146
libs/msdf-atlas-gen/include/msdfgen/core/Vector2.cpp
Normal file
146
libs/msdf-atlas-gen/include/msdfgen/core/Vector2.cpp
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
|
||||||
|
#include "Vector2.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
Vector2::Vector2(double val) : x(val), y(val) { }
|
||||||
|
|
||||||
|
Vector2::Vector2(double x, double y) : x(x), y(y) { }
|
||||||
|
|
||||||
|
void Vector2::reset() {
|
||||||
|
x = 0, y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Vector2::set(double x, double y) {
|
||||||
|
Vector2::x = x, Vector2::y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Vector2::length() const {
|
||||||
|
return sqrt(x*x+y*y);
|
||||||
|
}
|
||||||
|
|
||||||
|
double Vector2::direction() const {
|
||||||
|
return atan2(y, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::normalize(bool allowZero) const {
|
||||||
|
double len = length();
|
||||||
|
if (len == 0)
|
||||||
|
return Vector2(0, !allowZero);
|
||||||
|
return Vector2(x/len, y/len);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::getOrthogonal(bool polarity) const {
|
||||||
|
return polarity ? Vector2(-y, x) : Vector2(y, -x);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
|
||||||
|
double len = length();
|
||||||
|
if (len == 0)
|
||||||
|
return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
|
||||||
|
return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
|
||||||
|
Vector2 n = normalize(true);
|
||||||
|
double t = dotProduct(vector, n);
|
||||||
|
if (positive && t <= 0)
|
||||||
|
return Vector2();
|
||||||
|
return t*n;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2::operator const void*() const {
|
||||||
|
return x || y ? this : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Vector2::operator!() const {
|
||||||
|
return !x && !y;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Vector2::operator==(const Vector2 &other) const {
|
||||||
|
return x == other.x && y == other.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Vector2::operator!=(const Vector2 &other) const {
|
||||||
|
return x != other.x || y != other.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator+() const {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator-() const {
|
||||||
|
return Vector2(-x, -y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator+(const Vector2 &other) const {
|
||||||
|
return Vector2(x+other.x, y+other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator-(const Vector2 &other) const {
|
||||||
|
return Vector2(x-other.x, y-other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator*(const Vector2 &other) const {
|
||||||
|
return Vector2(x*other.x, y*other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator/(const Vector2 &other) const {
|
||||||
|
return Vector2(x/other.x, y/other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator*(double value) const {
|
||||||
|
return Vector2(x*value, y*value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator/(double value) const {
|
||||||
|
return Vector2(x/value, y/value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator+=(const Vector2 &other) {
|
||||||
|
x += other.x, y += other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator-=(const Vector2 &other) {
|
||||||
|
x -= other.x, y -= other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator*=(const Vector2 &other) {
|
||||||
|
x *= other.x, y *= other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator/=(const Vector2 &other) {
|
||||||
|
x /= other.x, y /= other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator*=(double value) {
|
||||||
|
x *= value, y *= value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 & Vector2::operator/=(double value) {
|
||||||
|
x /= value, y /= value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
double dotProduct(const Vector2 &a, const Vector2 &b) {
|
||||||
|
return a.x*b.x+a.y*b.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
double crossProduct(const Vector2 &a, const Vector2 &b) {
|
||||||
|
return a.x*b.y-a.y*b.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 operator*(double value, const Vector2 &vector) {
|
||||||
|
return Vector2(value*vector.x, value*vector.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 operator/(double value, const Vector2 &vector) {
|
||||||
|
return Vector2(value/vector.x, value/vector.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
66
libs/msdf-atlas-gen/include/msdfgen/core/Vector2.h
Normal file
66
libs/msdf-atlas-gen/include/msdfgen/core/Vector2.h
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A 2-dimensional euclidean vector with double precision.
|
||||||
|
* Implementation based on the Vector2 template from Artery Engine.
|
||||||
|
* @author Viktor Chlumsky
|
||||||
|
*/
|
||||||
|
struct Vector2 {
|
||||||
|
|
||||||
|
double x, y;
|
||||||
|
|
||||||
|
Vector2(double val = 0);
|
||||||
|
Vector2(double x, double y);
|
||||||
|
/// Sets the vector to zero.
|
||||||
|
void reset();
|
||||||
|
/// Sets individual elements of the vector.
|
||||||
|
void set(double x, double y);
|
||||||
|
/// Returns the vector's length.
|
||||||
|
double length() const;
|
||||||
|
/// Returns the angle of the vector in radians (atan2).
|
||||||
|
double direction() const;
|
||||||
|
/// Returns the normalized vector - one that has the same direction but unit length.
|
||||||
|
Vector2 normalize(bool allowZero = false) const;
|
||||||
|
/// Returns a vector with the same length that is orthogonal to this one.
|
||||||
|
Vector2 getOrthogonal(bool polarity = true) const;
|
||||||
|
/// Returns a vector with unit length that is orthogonal to this one.
|
||||||
|
Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const;
|
||||||
|
/// Returns a vector projected along this one.
|
||||||
|
Vector2 project(const Vector2 &vector, bool positive = false) const;
|
||||||
|
operator const void *() const;
|
||||||
|
bool operator!() const;
|
||||||
|
bool operator==(const Vector2 &other) const;
|
||||||
|
bool operator!=(const Vector2 &other) const;
|
||||||
|
Vector2 operator+() const;
|
||||||
|
Vector2 operator-() const;
|
||||||
|
Vector2 operator+(const Vector2 &other) const;
|
||||||
|
Vector2 operator-(const Vector2 &other) const;
|
||||||
|
Vector2 operator*(const Vector2 &other) const;
|
||||||
|
Vector2 operator/(const Vector2 &other) const;
|
||||||
|
Vector2 operator*(double value) const;
|
||||||
|
Vector2 operator/(double value) const;
|
||||||
|
Vector2 & operator+=(const Vector2 &other);
|
||||||
|
Vector2 & operator-=(const Vector2 &other);
|
||||||
|
Vector2 & operator*=(const Vector2 &other);
|
||||||
|
Vector2 & operator/=(const Vector2 &other);
|
||||||
|
Vector2 & operator*=(double value);
|
||||||
|
Vector2 & operator/=(double value);
|
||||||
|
/// Dot product of two vectors.
|
||||||
|
friend double dotProduct(const Vector2 &a, const Vector2 &b);
|
||||||
|
/// A special version of the cross product for 2D vectors (returns scalar value).
|
||||||
|
friend double crossProduct(const Vector2 &a, const Vector2 &b);
|
||||||
|
friend Vector2 operator*(double value, const Vector2 &vector);
|
||||||
|
friend Vector2 operator/(double value, const Vector2 &vector);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
|
||||||
|
typedef Vector2 Point2;
|
||||||
|
|
||||||
|
}
|
63
libs/msdf-atlas-gen/include/msdfgen/core/arithmetics.hpp
Normal file
63
libs/msdf-atlas-gen/include/msdfgen/core/arithmetics.hpp
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Returns the smaller of the arguments.
|
||||||
|
template <typename T>
|
||||||
|
inline T min(T a, T b) {
|
||||||
|
return b < a ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the larger of the arguments.
|
||||||
|
template <typename T>
|
||||||
|
inline T max(T a, T b) {
|
||||||
|
return a < b ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the middle out of three values
|
||||||
|
template <typename T>
|
||||||
|
inline T median(T a, T b, T c) {
|
||||||
|
return max(min(a, b), min(max(a, b), c));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the weighted average of a and b.
|
||||||
|
template <typename T, typename S>
|
||||||
|
inline T mix(T a, T b, S weight) {
|
||||||
|
return T((S(1)-weight)*a+weight*b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clamps the number to the interval from 0 to 1.
|
||||||
|
template <typename T>
|
||||||
|
inline T clamp(T n) {
|
||||||
|
return n >= T(0) && n <= T(1) ? n : T(n > T(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clamps the number to the interval from 0 to b.
|
||||||
|
template <typename T>
|
||||||
|
inline T clamp(T n, T b) {
|
||||||
|
return n >= T(0) && n <= b ? n : T(n > T(0))*b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clamps the number to the interval from a to b.
|
||||||
|
template <typename T>
|
||||||
|
inline T clamp(T n, T a, T b) {
|
||||||
|
return n >= a && n <= b ? n : n < a ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns 1 for positive values, -1 for negative values, and 0 for zero.
|
||||||
|
template <typename T>
|
||||||
|
inline int sign(T n) {
|
||||||
|
return (T(0) < n)-(n < T(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns 1 for non-negative values and -1 for negative values.
|
||||||
|
template <typename T>
|
||||||
|
inline int nonZeroSign(T n) {
|
||||||
|
return 2*(n > T(0))-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
#include "Vector2.h"
|
||||||
|
#include "BitmapRef.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
template <typename T, int N>
|
||||||
|
static void interpolate(T *output, const BitmapConstRef<T, N> &bitmap, Point2 pos) {
|
||||||
|
pos -= .5;
|
||||||
|
int l = (int) floor(pos.x);
|
||||||
|
int b = (int) floor(pos.y);
|
||||||
|
int r = l+1;
|
||||||
|
int t = b+1;
|
||||||
|
double lr = pos.x-l;
|
||||||
|
double bt = pos.y-b;
|
||||||
|
l = clamp(l, bitmap.width-1), r = clamp(r, bitmap.width-1);
|
||||||
|
b = clamp(b, bitmap.height-1), t = clamp(t, bitmap.height-1);
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
output[i] = mix(mix(bitmap(l, b)[i], bitmap(r, b)[i], lr), mix(bitmap(l, t)[i], bitmap(r, t)[i], lr), bt);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
134
libs/msdf-atlas-gen/include/msdfgen/core/contour-combiners.cpp
Normal file
134
libs/msdf-atlas-gen/include/msdfgen/core/contour-combiners.cpp
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
|
||||||
|
#include "contour-combiners.h"
|
||||||
|
|
||||||
|
#include <cfloat>
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
static void initDistance(double &distance) {
|
||||||
|
distance = -DBL_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void initDistance(MultiDistance &distance) {
|
||||||
|
distance.r = -DBL_MAX;
|
||||||
|
distance.g = -DBL_MAX;
|
||||||
|
distance.b = -DBL_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double resolveDistance(double distance) {
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double resolveDistance(const MultiDistance &distance) {
|
||||||
|
return median(distance.r, distance.g, distance.b);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
SimpleContourCombiner<EdgeSelector>::SimpleContourCombiner(const Shape &shape) { }
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
void SimpleContourCombiner<EdgeSelector>::reset(const Point2 &p) {
|
||||||
|
shapeEdgeSelector.reset(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
EdgeSelector & SimpleContourCombiner<EdgeSelector>::edgeSelector(int) {
|
||||||
|
return shapeEdgeSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
typename SimpleContourCombiner<EdgeSelector>::DistanceType SimpleContourCombiner<EdgeSelector>::distance() const {
|
||||||
|
return shapeEdgeSelector.distance();
|
||||||
|
}
|
||||||
|
|
||||||
|
template class SimpleContourCombiner<TrueDistanceSelector>;
|
||||||
|
template class SimpleContourCombiner<PseudoDistanceSelector>;
|
||||||
|
template class SimpleContourCombiner<MultiDistanceSelector>;
|
||||||
|
template class SimpleContourCombiner<MultiAndTrueDistanceSelector>;
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
OverlappingContourCombiner<EdgeSelector>::OverlappingContourCombiner(const Shape &shape) {
|
||||||
|
windings.reserve(shape.contours.size());
|
||||||
|
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
|
||||||
|
windings.push_back(contour->winding());
|
||||||
|
edgeSelectors.resize(shape.contours.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
void OverlappingContourCombiner<EdgeSelector>::reset(const Point2 &p) {
|
||||||
|
this->p = p;
|
||||||
|
for (typename std::vector<EdgeSelector>::iterator contourEdgeSelector = edgeSelectors.begin(); contourEdgeSelector != edgeSelectors.end(); ++contourEdgeSelector)
|
||||||
|
contourEdgeSelector->reset(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
EdgeSelector & OverlappingContourCombiner<EdgeSelector>::edgeSelector(int i) {
|
||||||
|
return edgeSelectors[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class EdgeSelector>
|
||||||
|
typename OverlappingContourCombiner<EdgeSelector>::DistanceType OverlappingContourCombiner<EdgeSelector>::distance() const {
|
||||||
|
int contourCount = (int) edgeSelectors.size();
|
||||||
|
EdgeSelector shapeEdgeSelector;
|
||||||
|
EdgeSelector innerEdgeSelector;
|
||||||
|
EdgeSelector outerEdgeSelector;
|
||||||
|
shapeEdgeSelector.reset(p);
|
||||||
|
innerEdgeSelector.reset(p);
|
||||||
|
outerEdgeSelector.reset(p);
|
||||||
|
for (int i = 0; i < contourCount; ++i) {
|
||||||
|
DistanceType edgeDistance = edgeSelectors[i].distance();
|
||||||
|
shapeEdgeSelector.merge(edgeSelectors[i]);
|
||||||
|
if (windings[i] > 0 && resolveDistance(edgeDistance) >= 0)
|
||||||
|
innerEdgeSelector.merge(edgeSelectors[i]);
|
||||||
|
if (windings[i] < 0 && resolveDistance(edgeDistance) <= 0)
|
||||||
|
outerEdgeSelector.merge(edgeSelectors[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
DistanceType shapeDistance = shapeEdgeSelector.distance();
|
||||||
|
DistanceType innerDistance = innerEdgeSelector.distance();
|
||||||
|
DistanceType outerDistance = outerEdgeSelector.distance();
|
||||||
|
double innerScalarDistance = resolveDistance(innerDistance);
|
||||||
|
double outerScalarDistance = resolveDistance(outerDistance);
|
||||||
|
DistanceType distance;
|
||||||
|
initDistance(distance);
|
||||||
|
|
||||||
|
int winding = 0;
|
||||||
|
if (innerScalarDistance >= 0 && fabs(innerScalarDistance) <= fabs(outerScalarDistance)) {
|
||||||
|
distance = innerDistance;
|
||||||
|
winding = 1;
|
||||||
|
for (int i = 0; i < contourCount; ++i)
|
||||||
|
if (windings[i] > 0) {
|
||||||
|
DistanceType contourDistance = edgeSelectors[i].distance();
|
||||||
|
if (fabs(resolveDistance(contourDistance)) < fabs(outerScalarDistance) && resolveDistance(contourDistance) > resolveDistance(distance))
|
||||||
|
distance = contourDistance;
|
||||||
|
}
|
||||||
|
} else if (outerScalarDistance <= 0 && fabs(outerScalarDistance) < fabs(innerScalarDistance)) {
|
||||||
|
distance = outerDistance;
|
||||||
|
winding = -1;
|
||||||
|
for (int i = 0; i < contourCount; ++i)
|
||||||
|
if (windings[i] < 0) {
|
||||||
|
DistanceType contourDistance = edgeSelectors[i].distance();
|
||||||
|
if (fabs(resolveDistance(contourDistance)) < fabs(innerScalarDistance) && resolveDistance(contourDistance) < resolveDistance(distance))
|
||||||
|
distance = contourDistance;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
return shapeDistance;
|
||||||
|
|
||||||
|
for (int i = 0; i < contourCount; ++i)
|
||||||
|
if (windings[i] != winding) {
|
||||||
|
DistanceType contourDistance = edgeSelectors[i].distance();
|
||||||
|
if (resolveDistance(contourDistance)*resolveDistance(distance) >= 0 && fabs(resolveDistance(contourDistance)) < fabs(resolveDistance(distance)))
|
||||||
|
distance = contourDistance;
|
||||||
|
}
|
||||||
|
if (resolveDistance(distance) == resolveDistance(shapeDistance))
|
||||||
|
distance = shapeDistance;
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
template class OverlappingContourCombiner<TrueDistanceSelector>;
|
||||||
|
template class OverlappingContourCombiner<PseudoDistanceSelector>;
|
||||||
|
template class OverlappingContourCombiner<MultiDistanceSelector>;
|
||||||
|
template class OverlappingContourCombiner<MultiAndTrueDistanceSelector>;
|
||||||
|
|
||||||
|
}
|
47
libs/msdf-atlas-gen/include/msdfgen/core/contour-combiners.h
Normal file
47
libs/msdf-atlas-gen/include/msdfgen/core/contour-combiners.h
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Shape.h"
|
||||||
|
#include "edge-selectors.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/// Simply selects the nearest contour.
|
||||||
|
template <class EdgeSelector>
|
||||||
|
class SimpleContourCombiner {
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef EdgeSelector EdgeSelectorType;
|
||||||
|
typedef typename EdgeSelector::DistanceType DistanceType;
|
||||||
|
|
||||||
|
explicit SimpleContourCombiner(const Shape &shape);
|
||||||
|
void reset(const Point2 &p);
|
||||||
|
EdgeSelector & edgeSelector(int i);
|
||||||
|
DistanceType distance() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EdgeSelector shapeEdgeSelector;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Selects the nearest contour that actually forms a border between filled and unfilled area.
|
||||||
|
template <class EdgeSelector>
|
||||||
|
class OverlappingContourCombiner {
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef EdgeSelector EdgeSelectorType;
|
||||||
|
typedef typename EdgeSelector::DistanceType DistanceType;
|
||||||
|
|
||||||
|
explicit OverlappingContourCombiner(const Shape &shape);
|
||||||
|
void reset(const Point2 &p);
|
||||||
|
EdgeSelector & edgeSelector(int i);
|
||||||
|
DistanceType distance() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Point2 p;
|
||||||
|
std::vector<int> windings;
|
||||||
|
std::vector<EdgeSelector> edgeSelectors;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
500
libs/msdf-atlas-gen/include/msdfgen/core/edge-coloring.cpp
Normal file
500
libs/msdf-atlas-gen/include/msdfgen/core/edge-coloring.cpp
Normal file
|
@ -0,0 +1,500 @@
|
||||||
|
|
||||||
|
#include "edge-coloring.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cfloat>
|
||||||
|
#include <queue>
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
static bool isCorner(const Vector2 &aDir, const Vector2 &bDir, double crossThreshold) {
|
||||||
|
return dotProduct(aDir, bDir) <= 0 || fabs(crossProduct(aDir, bDir)) > crossThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double estimateEdgeLength(const EdgeSegment *edge) {
|
||||||
|
double len = 0;
|
||||||
|
Point2 prev = edge->point(0);
|
||||||
|
for (int i = 1; i <= MSDFGEN_EDGE_LENGTH_PRECISION; ++i) {
|
||||||
|
Point2 cur = edge->point(1./MSDFGEN_EDGE_LENGTH_PRECISION*i);
|
||||||
|
len += (cur-prev).length();
|
||||||
|
prev = cur;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void switchColor(EdgeColor &color, unsigned long long &seed, EdgeColor banned = BLACK) {
|
||||||
|
EdgeColor combined = EdgeColor(color&banned);
|
||||||
|
if (combined == RED || combined == GREEN || combined == BLUE) {
|
||||||
|
color = EdgeColor(combined^WHITE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (color == BLACK || color == WHITE) {
|
||||||
|
static const EdgeColor start[3] = { CYAN, MAGENTA, YELLOW };
|
||||||
|
color = start[seed%3];
|
||||||
|
seed /= 3;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int shifted = color<<(1+(seed&1));
|
||||||
|
color = EdgeColor((shifted|shifted>>3)&WHITE);
|
||||||
|
seed >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void edgeColoringSimple(Shape &shape, double angleThreshold, unsigned long long seed) {
|
||||||
|
double crossThreshold = sin(angleThreshold);
|
||||||
|
std::vector<int> corners;
|
||||||
|
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
|
||||||
|
// Identify corners
|
||||||
|
corners.clear();
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
Vector2 prevDirection = contour->edges.back()->direction(1);
|
||||||
|
int index = 0;
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
|
||||||
|
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold))
|
||||||
|
corners.push_back(index);
|
||||||
|
prevDirection = (*edge)->direction(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth contour
|
||||||
|
if (corners.empty())
|
||||||
|
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
|
||||||
|
(*edge)->color = WHITE;
|
||||||
|
// "Teardrop" case
|
||||||
|
else if (corners.size() == 1) {
|
||||||
|
EdgeColor colors[3] = { WHITE, WHITE };
|
||||||
|
switchColor(colors[0], seed);
|
||||||
|
switchColor(colors[2] = colors[0], seed);
|
||||||
|
int corner = corners[0];
|
||||||
|
if (contour->edges.size() >= 3) {
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
contour->edges[(corner+i)%m]->color = (colors+1)[int(3+2.875*i/(m-1)-1.4375+.5)-3];
|
||||||
|
} else if (contour->edges.size() >= 1) {
|
||||||
|
// Less than three edge segments for three colors => edges must be split
|
||||||
|
EdgeSegment *parts[7] = { };
|
||||||
|
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
|
||||||
|
if (contour->edges.size() >= 2) {
|
||||||
|
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
|
||||||
|
parts[0]->color = parts[1]->color = colors[0];
|
||||||
|
parts[2]->color = parts[3]->color = colors[1];
|
||||||
|
parts[4]->color = parts[5]->color = colors[2];
|
||||||
|
} else {
|
||||||
|
parts[0]->color = colors[0];
|
||||||
|
parts[1]->color = colors[1];
|
||||||
|
parts[2]->color = colors[2];
|
||||||
|
}
|
||||||
|
contour->edges.clear();
|
||||||
|
for (int i = 0; parts[i]; ++i)
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Multiple corners
|
||||||
|
else {
|
||||||
|
int cornerCount = (int) corners.size();
|
||||||
|
int spline = 0;
|
||||||
|
int start = corners[0];
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
EdgeColor color = WHITE;
|
||||||
|
switchColor(color, seed);
|
||||||
|
EdgeColor initialColor = color;
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
int index = (start+i)%m;
|
||||||
|
if (spline+1 < cornerCount && corners[spline+1] == index) {
|
||||||
|
++spline;
|
||||||
|
switchColor(color, seed, EdgeColor((spline == cornerCount-1)*initialColor));
|
||||||
|
}
|
||||||
|
contour->edges[index]->color = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EdgeColoringInkTrapCorner {
|
||||||
|
int index;
|
||||||
|
double prevEdgeLengthEstimate;
|
||||||
|
bool minor;
|
||||||
|
EdgeColor color;
|
||||||
|
};
|
||||||
|
|
||||||
|
void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long seed) {
|
||||||
|
typedef EdgeColoringInkTrapCorner Corner;
|
||||||
|
double crossThreshold = sin(angleThreshold);
|
||||||
|
std::vector<Corner> corners;
|
||||||
|
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
|
||||||
|
// Identify corners
|
||||||
|
double splineLength = 0;
|
||||||
|
corners.clear();
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
Vector2 prevDirection = contour->edges.back()->direction(1);
|
||||||
|
int index = 0;
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
|
||||||
|
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold)) {
|
||||||
|
Corner corner = { index, splineLength };
|
||||||
|
corners.push_back(corner);
|
||||||
|
splineLength = 0;
|
||||||
|
}
|
||||||
|
splineLength += estimateEdgeLength(*edge);
|
||||||
|
prevDirection = (*edge)->direction(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Smooth contour
|
||||||
|
if (corners.empty())
|
||||||
|
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
|
||||||
|
(*edge)->color = WHITE;
|
||||||
|
// "Teardrop" case
|
||||||
|
else if (corners.size() == 1) {
|
||||||
|
EdgeColor colors[3] = { WHITE, WHITE };
|
||||||
|
switchColor(colors[0], seed);
|
||||||
|
switchColor(colors[2] = colors[0], seed);
|
||||||
|
int corner = corners[0].index;
|
||||||
|
if (contour->edges.size() >= 3) {
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
contour->edges[(corner+i)%m]->color = (colors+1)[int(3+2.875*i/(m-1)-1.4375+.5)-3];
|
||||||
|
} else if (contour->edges.size() >= 1) {
|
||||||
|
// Less than three edge segments for three colors => edges must be split
|
||||||
|
EdgeSegment *parts[7] = { };
|
||||||
|
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
|
||||||
|
if (contour->edges.size() >= 2) {
|
||||||
|
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
|
||||||
|
parts[0]->color = parts[1]->color = colors[0];
|
||||||
|
parts[2]->color = parts[3]->color = colors[1];
|
||||||
|
parts[4]->color = parts[5]->color = colors[2];
|
||||||
|
} else {
|
||||||
|
parts[0]->color = colors[0];
|
||||||
|
parts[1]->color = colors[1];
|
||||||
|
parts[2]->color = colors[2];
|
||||||
|
}
|
||||||
|
contour->edges.clear();
|
||||||
|
for (int i = 0; parts[i]; ++i)
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Multiple corners
|
||||||
|
else {
|
||||||
|
int cornerCount = (int) corners.size();
|
||||||
|
int majorCornerCount = cornerCount;
|
||||||
|
if (cornerCount > 3) {
|
||||||
|
corners.begin()->prevEdgeLengthEstimate += splineLength;
|
||||||
|
for (int i = 0; i < cornerCount; ++i) {
|
||||||
|
if (
|
||||||
|
corners[i].prevEdgeLengthEstimate > corners[(i+1)%cornerCount].prevEdgeLengthEstimate &&
|
||||||
|
corners[(i+1)%cornerCount].prevEdgeLengthEstimate < corners[(i+2)%cornerCount].prevEdgeLengthEstimate
|
||||||
|
) {
|
||||||
|
corners[i].minor = true;
|
||||||
|
--majorCornerCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EdgeColor color = WHITE;
|
||||||
|
EdgeColor initialColor = BLACK;
|
||||||
|
for (int i = 0; i < cornerCount; ++i) {
|
||||||
|
if (!corners[i].minor) {
|
||||||
|
--majorCornerCount;
|
||||||
|
switchColor(color, seed, EdgeColor(!majorCornerCount*initialColor));
|
||||||
|
corners[i].color = color;
|
||||||
|
if (!initialColor)
|
||||||
|
initialColor = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < cornerCount; ++i) {
|
||||||
|
if (corners[i].minor) {
|
||||||
|
EdgeColor nextColor = corners[(i+1)%cornerCount].color;
|
||||||
|
corners[i].color = EdgeColor((color&nextColor)^WHITE);
|
||||||
|
} else
|
||||||
|
color = corners[i].color;
|
||||||
|
}
|
||||||
|
int spline = 0;
|
||||||
|
int start = corners[0].index;
|
||||||
|
color = corners[0].color;
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
int index = (start+i)%m;
|
||||||
|
if (spline+1 < cornerCount && corners[spline+1].index == index)
|
||||||
|
color = corners[++spline].color;
|
||||||
|
contour->edges[index]->color = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EDGE COLORING BY DISTANCE - EXPERIMENTAL IMPLEMENTATION - WORK IN PROGRESS
|
||||||
|
#define MAX_RECOLOR_STEPS 16
|
||||||
|
#define EDGE_DISTANCE_PRECISION 16
|
||||||
|
|
||||||
|
static double edgeToEdgeDistance(const EdgeSegment &a, const EdgeSegment &b, int precision) {
|
||||||
|
if (a.point(0) == b.point(0) || a.point(0) == b.point(1) || a.point(1) == b.point(0) || a.point(1) == b.point(1))
|
||||||
|
return 0;
|
||||||
|
double iFac = 1./precision;
|
||||||
|
double minDistance = (b.point(0)-a.point(0)).length();
|
||||||
|
for (int i = 0; i <= precision; ++i) {
|
||||||
|
double t = iFac*i;
|
||||||
|
double d = fabs(a.signedDistance(b.point(t), t).distance);
|
||||||
|
minDistance = min(minDistance, d);
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= precision; ++i) {
|
||||||
|
double t = iFac*i;
|
||||||
|
double d = fabs(b.signedDistance(a.point(t), t).distance);
|
||||||
|
minDistance = min(minDistance, d);
|
||||||
|
}
|
||||||
|
return minDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double splineToSplineDistance(EdgeSegment * const *edgeSegments, int aStart, int aEnd, int bStart, int bEnd, int precision) {
|
||||||
|
double minDistance = DBL_MAX;
|
||||||
|
for (int ai = aStart; ai < aEnd; ++ai)
|
||||||
|
for (int bi = bStart; bi < bEnd && minDistance; ++bi) {
|
||||||
|
double d = edgeToEdgeDistance(*edgeSegments[ai], *edgeSegments[bi], precision);
|
||||||
|
minDistance = min(minDistance, d);
|
||||||
|
}
|
||||||
|
return minDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void colorSecondDegreeGraph(int *coloring, const int * const *edgeMatrix, int vertexCount, unsigned long long seed) {
|
||||||
|
for (int i = 0; i < vertexCount; ++i) {
|
||||||
|
int possibleColors = 7;
|
||||||
|
for (int j = 0; j < i; ++j) {
|
||||||
|
if (edgeMatrix[i][j])
|
||||||
|
possibleColors &= ~(1<<coloring[j]);
|
||||||
|
}
|
||||||
|
int color = 0;
|
||||||
|
switch (possibleColors) {
|
||||||
|
case 1:
|
||||||
|
color = 0;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
color = 1;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
color = (int) seed&1;
|
||||||
|
seed >>= 1;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
color = 2;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
color = ((int) seed+1&1)<<1;
|
||||||
|
seed >>= 1;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
color = ((int) seed&1)+1;
|
||||||
|
seed >>= 1;
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
color = int((seed+i)%3);
|
||||||
|
seed /= 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
coloring[i] = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int vertexPossibleColors(const int *coloring, const int *edgeVector, int vertexCount) {
|
||||||
|
int usedColors = 0;
|
||||||
|
for (int i = 0; i < vertexCount; ++i)
|
||||||
|
if (edgeVector[i])
|
||||||
|
usedColors |= 1<<coloring[i];
|
||||||
|
return 7&~usedColors;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void uncolorSameNeighbors(std::queue<int> &uncolored, int *coloring, const int * const *edgeMatrix, int vertex, int vertexCount) {
|
||||||
|
for (int i = vertex+1; i < vertexCount; ++i) {
|
||||||
|
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
|
||||||
|
coloring[i] = -1;
|
||||||
|
uncolored.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < vertex; ++i) {
|
||||||
|
if (edgeMatrix[vertex][i] && coloring[i] == coloring[vertex]) {
|
||||||
|
coloring[i] = -1;
|
||||||
|
uncolored.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool tryAddEdge(int *coloring, int * const *edgeMatrix, int vertexCount, int vertexA, int vertexB, int *coloringBuffer) {
|
||||||
|
static const int FIRST_POSSIBLE_COLOR[8] = { -1, 0, 1, 0, 2, 2, 1, 0 };
|
||||||
|
edgeMatrix[vertexA][vertexB] = 1;
|
||||||
|
edgeMatrix[vertexB][vertexA] = 1;
|
||||||
|
if (coloring[vertexA] != coloring[vertexB])
|
||||||
|
return true;
|
||||||
|
int bPossibleColors = vertexPossibleColors(coloring, edgeMatrix[vertexB], vertexCount);
|
||||||
|
if (bPossibleColors) {
|
||||||
|
coloring[vertexB] = FIRST_POSSIBLE_COLOR[bPossibleColors];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
memcpy(coloringBuffer, coloring, sizeof(int)*vertexCount);
|
||||||
|
std::queue<int> uncolored;
|
||||||
|
{
|
||||||
|
int *coloring = coloringBuffer;
|
||||||
|
coloring[vertexB] = FIRST_POSSIBLE_COLOR[7&~(1<<coloring[vertexA])];
|
||||||
|
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, vertexB, vertexCount);
|
||||||
|
int step = 0;
|
||||||
|
while (!uncolored.empty() && step < MAX_RECOLOR_STEPS) {
|
||||||
|
int i = uncolored.front();
|
||||||
|
uncolored.pop();
|
||||||
|
int possibleColors = vertexPossibleColors(coloring, edgeMatrix[i], vertexCount);
|
||||||
|
if (possibleColors) {
|
||||||
|
coloring[i] = FIRST_POSSIBLE_COLOR[possibleColors];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
coloring[i] = step++%3;
|
||||||
|
} while (edgeMatrix[i][vertexA] && coloring[i] == coloring[vertexA]);
|
||||||
|
uncolorSameNeighbors(uncolored, coloring, edgeMatrix, i, vertexCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!uncolored.empty()) {
|
||||||
|
edgeMatrix[vertexA][vertexB] = 0;
|
||||||
|
edgeMatrix[vertexB][vertexA] = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(coloring, coloringBuffer, sizeof(int)*vertexCount);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmpDoublePtr(const void *a, const void *b) {
|
||||||
|
return sign(**reinterpret_cast<const double * const *>(a)-**reinterpret_cast<const double * const *>(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed) {
|
||||||
|
|
||||||
|
std::vector<EdgeSegment *> edgeSegments;
|
||||||
|
std::vector<int> splineStarts;
|
||||||
|
|
||||||
|
double crossThreshold = sin(angleThreshold);
|
||||||
|
std::vector<int> corners;
|
||||||
|
for (std::vector<Contour>::iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
|
||||||
|
if (!contour->edges.empty()) {
|
||||||
|
// Identify corners
|
||||||
|
corners.clear();
|
||||||
|
Vector2 prevDirection = contour->edges.back()->direction(1);
|
||||||
|
int index = 0;
|
||||||
|
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge, ++index) {
|
||||||
|
if (isCorner(prevDirection.normalize(), (*edge)->direction(0).normalize(), crossThreshold))
|
||||||
|
corners.push_back(index);
|
||||||
|
prevDirection = (*edge)->direction(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
// Smooth contour
|
||||||
|
if (corners.empty())
|
||||||
|
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
|
||||||
|
edgeSegments.push_back(&**edge);
|
||||||
|
// "Teardrop" case
|
||||||
|
else if (corners.size() == 1) {
|
||||||
|
int corner = corners[0];
|
||||||
|
if (contour->edges.size() >= 3) {
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
if (i == m/2)
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
if (int(3+2.875*i/(m-1)-1.4375+.5)-3)
|
||||||
|
edgeSegments.push_back(&*contour->edges[(corner+i)%m]);
|
||||||
|
else
|
||||||
|
contour->edges[(corner+i)%m]->color = WHITE;
|
||||||
|
}
|
||||||
|
} else if (contour->edges.size() >= 1) {
|
||||||
|
// Less than three edge segments for three colors => edges must be split
|
||||||
|
EdgeSegment *parts[7] = { };
|
||||||
|
contour->edges[0]->splitInThirds(parts[0+3*corner], parts[1+3*corner], parts[2+3*corner]);
|
||||||
|
if (contour->edges.size() >= 2) {
|
||||||
|
contour->edges[1]->splitInThirds(parts[3-3*corner], parts[4-3*corner], parts[5-3*corner]);
|
||||||
|
edgeSegments.push_back(parts[0]);
|
||||||
|
edgeSegments.push_back(parts[1]);
|
||||||
|
parts[2]->color = parts[3]->color = WHITE;
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
edgeSegments.push_back(parts[4]);
|
||||||
|
edgeSegments.push_back(parts[5]);
|
||||||
|
} else {
|
||||||
|
edgeSegments.push_back(parts[0]);
|
||||||
|
parts[1]->color = WHITE;
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
edgeSegments.push_back(parts[2]);
|
||||||
|
}
|
||||||
|
contour->edges.clear();
|
||||||
|
for (int i = 0; parts[i]; ++i)
|
||||||
|
contour->edges.push_back(EdgeHolder(parts[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Multiple corners
|
||||||
|
else {
|
||||||
|
int cornerCount = (int) corners.size();
|
||||||
|
int spline = 0;
|
||||||
|
int start = corners[0];
|
||||||
|
int m = (int) contour->edges.size();
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
int index = (start+i)%m;
|
||||||
|
if (spline+1 < cornerCount && corners[spline+1] == index) {
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
++spline;
|
||||||
|
}
|
||||||
|
edgeSegments.push_back(&*contour->edges[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
splineStarts.push_back((int) edgeSegments.size());
|
||||||
|
|
||||||
|
int segmentCount = (int) edgeSegments.size();
|
||||||
|
int splineCount = (int) splineStarts.size()-1;
|
||||||
|
if (!splineCount)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::vector<double> distanceMatrixStorage(splineCount*splineCount);
|
||||||
|
std::vector<double *> distanceMatrix(splineCount);
|
||||||
|
for (int i = 0; i < splineCount; ++i)
|
||||||
|
distanceMatrix[i] = &distanceMatrixStorage[i*splineCount];
|
||||||
|
const double *distanceMatrixBase = &distanceMatrixStorage[0];
|
||||||
|
|
||||||
|
for (int i = 0; i < splineCount; ++i) {
|
||||||
|
distanceMatrix[i][i] = -1;
|
||||||
|
for (int j = i+1; j < splineCount; ++j) {
|
||||||
|
double dist = splineToSplineDistance(&edgeSegments[0], splineStarts[i], splineStarts[i+1], splineStarts[j], splineStarts[j+1], EDGE_DISTANCE_PRECISION);
|
||||||
|
distanceMatrix[i][j] = dist;
|
||||||
|
distanceMatrix[j][i] = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const double *> graphEdgeDistances;
|
||||||
|
graphEdgeDistances.reserve(splineCount*(splineCount-1)/2);
|
||||||
|
for (int i = 0; i < splineCount; ++i)
|
||||||
|
for (int j = i+1; j < splineCount; ++j)
|
||||||
|
graphEdgeDistances.push_back(&distanceMatrix[i][j]);
|
||||||
|
int graphEdgeCount = (int) graphEdgeDistances.size();
|
||||||
|
if (!graphEdgeDistances.empty())
|
||||||
|
qsort(&graphEdgeDistances[0], graphEdgeDistances.size(), sizeof(const double *), &cmpDoublePtr);
|
||||||
|
|
||||||
|
std::vector<int> edgeMatrixStorage(splineCount*splineCount);
|
||||||
|
std::vector<int *> edgeMatrix(splineCount);
|
||||||
|
for (int i = 0; i < splineCount; ++i)
|
||||||
|
edgeMatrix[i] = &edgeMatrixStorage[i*splineCount];
|
||||||
|
int nextEdge = 0;
|
||||||
|
for (; nextEdge < graphEdgeCount && !*graphEdgeDistances[nextEdge]; ++nextEdge) {
|
||||||
|
int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase);
|
||||||
|
int row = elem/splineCount;
|
||||||
|
int col = elem%splineCount;
|
||||||
|
edgeMatrix[row][col] = 1;
|
||||||
|
edgeMatrix[col][row] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> coloring(2*splineCount);
|
||||||
|
colorSecondDegreeGraph(&coloring[0], &edgeMatrix[0], splineCount, seed);
|
||||||
|
for (; nextEdge < graphEdgeCount; ++nextEdge) {
|
||||||
|
int elem = (int) (graphEdgeDistances[nextEdge]-distanceMatrixBase);
|
||||||
|
tryAddEdge(&coloring[0], &edgeMatrix[0], splineCount, elem/splineCount, elem%splineCount, &coloring[splineCount]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const EdgeColor colors[3] = { YELLOW, CYAN, MAGENTA };
|
||||||
|
int spline = -1;
|
||||||
|
for (int i = 0; i < segmentCount; ++i) {
|
||||||
|
if (splineStarts[spline+1] == i)
|
||||||
|
++spline;
|
||||||
|
edgeSegments[i]->color = colors[coloring[spline]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
libs/msdf-atlas-gen/include/msdfgen/core/edge-coloring.h
Normal file
29
libs/msdf-atlas-gen/include/msdfgen/core/edge-coloring.h
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Shape.h"
|
||||||
|
|
||||||
|
#define MSDFGEN_EDGE_LENGTH_PRECISION 4
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
/** Assigns colors to edges of the shape in accordance to the multi-channel distance field technique.
|
||||||
|
* May split some edges if necessary.
|
||||||
|
* angleThreshold specifies the maximum angle (in radians) to be considered a corner, for example 3 (~172 degrees).
|
||||||
|
* Values below 1/2 PI will be treated as the external angle.
|
||||||
|
*/
|
||||||
|
void edgeColoringSimple(Shape &shape, double angleThreshold, unsigned long long seed = 0);
|
||||||
|
|
||||||
|
/** The alternative "ink trap" coloring strategy is designed for better results with typefaces
|
||||||
|
* that use ink traps as a design feature. It guarantees that even if all edges that are shorter than
|
||||||
|
* both their neighboring edges are removed, the coloring remains consistent with the established rules.
|
||||||
|
*/
|
||||||
|
void edgeColoringInkTrap(Shape &shape, double angleThreshold, unsigned long long seed = 0);
|
||||||
|
|
||||||
|
/** The alternative coloring by distance tries to use different colors for edges that are close together.
|
||||||
|
* This should theoretically be the best strategy on average. However, since it needs to compute the distance
|
||||||
|
* between all pairs of edges, and perform a graph optimization task, it is much slower than the rest.
|
||||||
|
*/
|
||||||
|
void edgeColoringByDistance(Shape &shape, double angleThreshold, unsigned long long seed = 0);
|
||||||
|
|
||||||
|
}
|
504
libs/msdf-atlas-gen/include/msdfgen/core/edge-segments.cpp
Normal file
504
libs/msdf-atlas-gen/include/msdfgen/core/edge-segments.cpp
Normal file
|
@ -0,0 +1,504 @@
|
||||||
|
|
||||||
|
#include "edge-segments.h"
|
||||||
|
|
||||||
|
#include "arithmetics.hpp"
|
||||||
|
#include "equation-solver.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
void EdgeSegment::distanceToPseudoDistance(SignedDistance &distance, Point2 origin, double param) const {
|
||||||
|
if (param < 0) {
|
||||||
|
Vector2 dir = direction(0).normalize();
|
||||||
|
Vector2 aq = origin-point(0);
|
||||||
|
double ts = dotProduct(aq, dir);
|
||||||
|
if (ts < 0) {
|
||||||
|
double pseudoDistance = crossProduct(aq, dir);
|
||||||
|
if (fabs(pseudoDistance) <= fabs(distance.distance)) {
|
||||||
|
distance.distance = pseudoDistance;
|
||||||
|
distance.dot = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (param > 1) {
|
||||||
|
Vector2 dir = direction(1).normalize();
|
||||||
|
Vector2 bq = origin-point(1);
|
||||||
|
double ts = dotProduct(bq, dir);
|
||||||
|
if (ts > 0) {
|
||||||
|
double pseudoDistance = crossProduct(bq, dir);
|
||||||
|
if (fabs(pseudoDistance) <= fabs(distance.distance)) {
|
||||||
|
distance.distance = pseudoDistance;
|
||||||
|
distance.dot = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LinearSegment::LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
|
||||||
|
p[0] = p0;
|
||||||
|
p[1] = p1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QuadraticSegment::QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
|
||||||
|
if (p1 == p0 || p1 == p2)
|
||||||
|
p1 = 0.5*(p0+p2);
|
||||||
|
p[0] = p0;
|
||||||
|
p[1] = p1;
|
||||||
|
p[2] = p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
|
||||||
|
if ((p1 == p0 || p1 == p3) && (p2 == p0 || p2 == p3)) {
|
||||||
|
p1 = mix(p0, p3, 1/3.);
|
||||||
|
p2 = mix(p0, p3, 2/3.);
|
||||||
|
}
|
||||||
|
p[0] = p0;
|
||||||
|
p[1] = p1;
|
||||||
|
p[2] = p2;
|
||||||
|
p[3] = p3;
|
||||||
|
}
|
||||||
|
|
||||||
|
LinearSegment * LinearSegment::clone() const {
|
||||||
|
return new LinearSegment(p[0], p[1], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
QuadraticSegment * QuadraticSegment::clone() const {
|
||||||
|
return new QuadraticSegment(p[0], p[1], p[2], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
CubicSegment * CubicSegment::clone() const {
|
||||||
|
return new CubicSegment(p[0], p[1], p[2], p[3], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point2 LinearSegment::point(double param) const {
|
||||||
|
return mix(p[0], p[1], param);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point2 QuadraticSegment::point(double param) const {
|
||||||
|
return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point2 CubicSegment::point(double param) const {
|
||||||
|
Vector2 p12 = mix(p[1], p[2], param);
|
||||||
|
return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 LinearSegment::direction(double param) const {
|
||||||
|
return p[1]-p[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 QuadraticSegment::direction(double param) const {
|
||||||
|
Vector2 tangent = mix(p[1]-p[0], p[2]-p[1], param);
|
||||||
|
if (!tangent)
|
||||||
|
return p[2]-p[0];
|
||||||
|
return tangent;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 CubicSegment::direction(double param) const {
|
||||||
|
Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
|
||||||
|
if (!tangent) {
|
||||||
|
if (param == 0) return p[2]-p[0];
|
||||||
|
if (param == 1) return p[3]-p[1];
|
||||||
|
}
|
||||||
|
return tangent;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 LinearSegment::directionChange(double param) const {
|
||||||
|
return Vector2();
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 QuadraticSegment::directionChange(double param) const {
|
||||||
|
return (p[2]-p[1])-(p[1]-p[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 CubicSegment::directionChange(double param) const {
|
||||||
|
return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param);
|
||||||
|
}
|
||||||
|
|
||||||
|
double LinearSegment::length() const {
|
||||||
|
return (p[1]-p[0]).length();
|
||||||
|
}
|
||||||
|
|
||||||
|
double QuadraticSegment::length() const {
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
Vector2 br = p[2]-p[1]-ab;
|
||||||
|
double abab = dotProduct(ab, ab);
|
||||||
|
double abbr = dotProduct(ab, br);
|
||||||
|
double brbr = dotProduct(br, br);
|
||||||
|
double abLen = sqrt(abab);
|
||||||
|
double brLen = sqrt(brbr);
|
||||||
|
double crs = crossProduct(ab, br);
|
||||||
|
double h = sqrt(abab+abbr+abbr+brbr);
|
||||||
|
return (
|
||||||
|
brLen*((abbr+brbr)*h-abbr*abLen)+
|
||||||
|
crs*crs*log((brLen*h+abbr+brbr)/(brLen*abLen+abbr))
|
||||||
|
)/(brbr*brLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
SignedDistance LinearSegment::signedDistance(Point2 origin, double ¶m) const {
|
||||||
|
Vector2 aq = origin-p[0];
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
param = dotProduct(aq, ab)/dotProduct(ab, ab);
|
||||||
|
Vector2 eq = p[param > .5]-origin;
|
||||||
|
double endpointDistance = eq.length();
|
||||||
|
if (param > 0 && param < 1) {
|
||||||
|
double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
|
||||||
|
if (fabs(orthoDistance) < endpointDistance)
|
||||||
|
return SignedDistance(orthoDistance, 0);
|
||||||
|
}
|
||||||
|
return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
|
||||||
|
}
|
||||||
|
|
||||||
|
SignedDistance QuadraticSegment::signedDistance(Point2 origin, double ¶m) const {
|
||||||
|
Vector2 qa = p[0]-origin;
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
Vector2 br = p[2]-p[1]-ab;
|
||||||
|
double a = dotProduct(br, br);
|
||||||
|
double b = 3*dotProduct(ab, br);
|
||||||
|
double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
|
||||||
|
double d = dotProduct(qa, ab);
|
||||||
|
double t[3];
|
||||||
|
int solutions = solveCubic(t, a, b, c, d);
|
||||||
|
|
||||||
|
Vector2 epDir = direction(0);
|
||||||
|
double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
|
||||||
|
param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
|
||||||
|
{
|
||||||
|
epDir = direction(1);
|
||||||
|
double distance = (p[2]-origin).length(); // distance from B
|
||||||
|
if (distance < fabs(minDistance)) {
|
||||||
|
minDistance = nonZeroSign(crossProduct(epDir, p[2]-origin))*distance;
|
||||||
|
param = dotProduct(origin-p[1], epDir)/dotProduct(epDir, epDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < solutions; ++i) {
|
||||||
|
if (t[i] > 0 && t[i] < 1) {
|
||||||
|
Point2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;
|
||||||
|
double distance = qe.length();
|
||||||
|
if (distance <= fabs(minDistance)) {
|
||||||
|
minDistance = nonZeroSign(crossProduct(ab+t[i]*br, qe))*distance;
|
||||||
|
param = t[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (param >= 0 && param <= 1)
|
||||||
|
return SignedDistance(minDistance, 0);
|
||||||
|
if (param < .5)
|
||||||
|
return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
|
||||||
|
else
|
||||||
|
return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[2]-origin).normalize())));
|
||||||
|
}
|
||||||
|
|
||||||
|
SignedDistance CubicSegment::signedDistance(Point2 origin, double ¶m) const {
|
||||||
|
Vector2 qa = p[0]-origin;
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
Vector2 br = p[2]-p[1]-ab;
|
||||||
|
Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
|
||||||
|
|
||||||
|
Vector2 epDir = direction(0);
|
||||||
|
double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
|
||||||
|
param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
|
||||||
|
{
|
||||||
|
epDir = direction(1);
|
||||||
|
double distance = (p[3]-origin).length(); // distance from B
|
||||||
|
if (distance < fabs(minDistance)) {
|
||||||
|
minDistance = nonZeroSign(crossProduct(epDir, p[3]-origin))*distance;
|
||||||
|
param = dotProduct(epDir-(p[3]-origin), epDir)/dotProduct(epDir, epDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Iterative minimum distance search
|
||||||
|
for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
|
||||||
|
double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
|
||||||
|
Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
|
||||||
|
for (int step = 0; step < MSDFGEN_CUBIC_SEARCH_STEPS; ++step) {
|
||||||
|
// Improve t
|
||||||
|
Vector2 d1 = 3*ab+6*t*br+3*t*t*as;
|
||||||
|
Vector2 d2 = 6*br+6*t*as;
|
||||||
|
t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
|
||||||
|
if (t <= 0 || t >= 1)
|
||||||
|
break;
|
||||||
|
qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
|
||||||
|
double distance = qe.length();
|
||||||
|
if (distance < fabs(minDistance)) {
|
||||||
|
minDistance = nonZeroSign(crossProduct(d1, qe))*distance;
|
||||||
|
param = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (param >= 0 && param <= 1)
|
||||||
|
return SignedDistance(minDistance, 0);
|
||||||
|
if (param < .5)
|
||||||
|
return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
|
||||||
|
else
|
||||||
|
return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[3]-origin).normalize())));
|
||||||
|
}
|
||||||
|
|
||||||
|
int LinearSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
|
||||||
|
if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {
|
||||||
|
double param = (y-p[0].y)/(p[1].y-p[0].y);
|
||||||
|
x[0] = mix(p[0].x, p[1].x, param);
|
||||||
|
dy[0] = sign(p[1].y-p[0].y);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
|
||||||
|
int total = 0;
|
||||||
|
int nextDY = y > p[0].y ? 1 : -1;
|
||||||
|
x[total] = p[0].x;
|
||||||
|
if (p[0].y == y) {
|
||||||
|
if (p[0].y < p[1].y || (p[0].y == p[1].y && p[0].y < p[2].y))
|
||||||
|
dy[total++] = 1;
|
||||||
|
else
|
||||||
|
nextDY = 1;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
Vector2 br = p[2]-p[1]-ab;
|
||||||
|
double t[2];
|
||||||
|
int solutions = solveQuadratic(t, br.y, 2*ab.y, p[0].y-y);
|
||||||
|
// Sort solutions
|
||||||
|
double tmp;
|
||||||
|
if (solutions >= 2 && t[0] > t[1])
|
||||||
|
tmp = t[0], t[0] = t[1], t[1] = tmp;
|
||||||
|
for (int i = 0; i < solutions && total < 2; ++i) {
|
||||||
|
if (t[i] >= 0 && t[i] <= 1) {
|
||||||
|
x[total] = p[0].x+2*t[i]*ab.x+t[i]*t[i]*br.x;
|
||||||
|
if (nextDY*(ab.y+t[i]*br.y) >= 0) {
|
||||||
|
dy[total++] = nextDY;
|
||||||
|
nextDY = -nextDY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p[2].y == y) {
|
||||||
|
if (nextDY > 0 && total > 0) {
|
||||||
|
--total;
|
||||||
|
nextDY = -1;
|
||||||
|
}
|
||||||
|
if ((p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y)) && total < 2) {
|
||||||
|
x[total] = p[2].x;
|
||||||
|
if (nextDY < 0) {
|
||||||
|
dy[total++] = -1;
|
||||||
|
nextDY = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nextDY != (y >= p[2].y ? 1 : -1)) {
|
||||||
|
if (total > 0)
|
||||||
|
--total;
|
||||||
|
else {
|
||||||
|
if (fabs(p[2].y-y) < fabs(p[0].y-y))
|
||||||
|
x[total] = p[2].x;
|
||||||
|
dy[total++] = nextDY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
|
||||||
|
int total = 0;
|
||||||
|
int nextDY = y > p[0].y ? 1 : -1;
|
||||||
|
x[total] = p[0].x;
|
||||||
|
if (p[0].y == y) {
|
||||||
|
if (p[0].y < p[1].y || (p[0].y == p[1].y && (p[0].y < p[2].y || (p[0].y == p[2].y && p[0].y < p[3].y))))
|
||||||
|
dy[total++] = 1;
|
||||||
|
else
|
||||||
|
nextDY = 1;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Vector2 ab = p[1]-p[0];
|
||||||
|
Vector2 br = p[2]-p[1]-ab;
|
||||||
|
Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
|
||||||
|
double t[3];
|
||||||
|
int solutions = solveCubic(t, as.y, 3*br.y, 3*ab.y, p[0].y-y);
|
||||||
|
// Sort solutions
|
||||||
|
double tmp;
|
||||||
|
if (solutions >= 2) {
|
||||||
|
if (t[0] > t[1])
|
||||||
|
tmp = t[0], t[0] = t[1], t[1] = tmp;
|
||||||
|
if (solutions >= 3 && t[1] > t[2]) {
|
||||||
|
tmp = t[1], t[1] = t[2], t[2] = tmp;
|
||||||
|
if (t[0] > t[1])
|
||||||
|
tmp = t[0], t[0] = t[1], t[1] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < solutions && total < 3; ++i) {
|
||||||
|
if (t[i] >= 0 && t[i] <= 1) {
|
||||||
|
x[total] = p[0].x+3*t[i]*ab.x+3*t[i]*t[i]*br.x+t[i]*t[i]*t[i]*as.x;
|
||||||
|
if (nextDY*(ab.y+2*t[i]*br.y+t[i]*t[i]*as.y) >= 0) {
|
||||||
|
dy[total++] = nextDY;
|
||||||
|
nextDY = -nextDY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p[3].y == y) {
|
||||||
|
if (nextDY > 0 && total > 0) {
|
||||||
|
--total;
|
||||||
|
nextDY = -1;
|
||||||
|
}
|
||||||
|
if ((p[3].y < p[2].y || (p[3].y == p[2].y && (p[3].y < p[1].y || (p[3].y == p[1].y && p[3].y < p[0].y)))) && total < 3) {
|
||||||
|
x[total] = p[3].x;
|
||||||
|
if (nextDY < 0) {
|
||||||
|
dy[total++] = -1;
|
||||||
|
nextDY = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nextDY != (y >= p[3].y ? 1 : -1)) {
|
||||||
|
if (total > 0)
|
||||||
|
--total;
|
||||||
|
else {
|
||||||
|
if (fabs(p[3].y-y) < fabs(p[0].y-y))
|
||||||
|
x[total] = p[3].x;
|
||||||
|
dy[total++] = nextDY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
|
||||||
|
if (p.x < l) l = p.x;
|
||||||
|
if (p.y < b) b = p.y;
|
||||||
|
if (p.x > r) r = p.x;
|
||||||
|
if (p.y > t) t = p.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinearSegment::bound(double &l, double &b, double &r, double &t) const {
|
||||||
|
pointBounds(p[0], l, b, r, t);
|
||||||
|
pointBounds(p[1], l, b, r, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuadraticSegment::bound(double &l, double &b, double &r, double &t) const {
|
||||||
|
pointBounds(p[0], l, b, r, t);
|
||||||
|
pointBounds(p[2], l, b, r, t);
|
||||||
|
Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
|
||||||
|
if (bot.x) {
|
||||||
|
double param = (p[1].x-p[0].x)/bot.x;
|
||||||
|
if (param > 0 && param < 1)
|
||||||
|
pointBounds(point(param), l, b, r, t);
|
||||||
|
}
|
||||||
|
if (bot.y) {
|
||||||
|
double param = (p[1].y-p[0].y)/bot.y;
|
||||||
|
if (param > 0 && param < 1)
|
||||||
|
pointBounds(point(param), l, b, r, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::bound(double &l, double &b, double &r, double &t) const {
|
||||||
|
pointBounds(p[0], l, b, r, t);
|
||||||
|
pointBounds(p[3], l, b, r, t);
|
||||||
|
Vector2 a0 = p[1]-p[0];
|
||||||
|
Vector2 a1 = 2*(p[2]-p[1]-a0);
|
||||||
|
Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
|
||||||
|
double params[2];
|
||||||
|
int solutions;
|
||||||
|
solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
|
||||||
|
for (int i = 0; i < solutions; ++i)
|
||||||
|
if (params[i] > 0 && params[i] < 1)
|
||||||
|
pointBounds(point(params[i]), l, b, r, t);
|
||||||
|
solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
|
||||||
|
for (int i = 0; i < solutions; ++i)
|
||||||
|
if (params[i] > 0 && params[i] < 1)
|
||||||
|
pointBounds(point(params[i]), l, b, r, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinearSegment::reverse() {
|
||||||
|
Point2 tmp = p[0];
|
||||||
|
p[0] = p[1];
|
||||||
|
p[1] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuadraticSegment::reverse() {
|
||||||
|
Point2 tmp = p[0];
|
||||||
|
p[0] = p[2];
|
||||||
|
p[2] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::reverse() {
|
||||||
|
Point2 tmp = p[0];
|
||||||
|
p[0] = p[3];
|
||||||
|
p[3] = tmp;
|
||||||
|
tmp = p[1];
|
||||||
|
p[1] = p[2];
|
||||||
|
p[2] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinearSegment::moveStartPoint(Point2 to) {
|
||||||
|
p[0] = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuadraticSegment::moveStartPoint(Point2 to) {
|
||||||
|
Vector2 origSDir = p[0]-p[1];
|
||||||
|
Point2 origP1 = p[1];
|
||||||
|
p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
|
||||||
|
p[0] = to;
|
||||||
|
if (dotProduct(origSDir, p[0]-p[1]) < 0)
|
||||||
|
p[1] = origP1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::moveStartPoint(Point2 to) {
|
||||||
|
p[1] += to-p[0];
|
||||||
|
p[0] = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinearSegment::moveEndPoint(Point2 to) {
|
||||||
|
p[1] = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuadraticSegment::moveEndPoint(Point2 to) {
|
||||||
|
Vector2 origEDir = p[2]-p[1];
|
||||||
|
Point2 origP1 = p[1];
|
||||||
|
p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
|
||||||
|
p[2] = to;
|
||||||
|
if (dotProduct(origEDir, p[2]-p[1]) < 0)
|
||||||
|
p[1] = origP1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::moveEndPoint(Point2 to) {
|
||||||
|
p[2] += to-p[3];
|
||||||
|
p[3] = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
|
||||||
|
part1 = new LinearSegment(p[0], point(1/3.), color);
|
||||||
|
part2 = new LinearSegment(point(1/3.), point(2/3.), color);
|
||||||
|
part3 = new LinearSegment(point(2/3.), p[1], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
|
||||||
|
part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
|
||||||
|
part2 = new QuadraticSegment(point(1/3.), mix(mix(p[0], p[1], 5/9.), mix(p[1], p[2], 4/9.), .5), point(2/3.), color);
|
||||||
|
part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
|
||||||
|
part1 = new CubicSegment(p[0], p[0] == p[1] ? p[0] : mix(p[0], p[1], 1/3.), mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), point(1/3.), color);
|
||||||
|
part2 = new CubicSegment(point(1/3.),
|
||||||
|
mix(mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), mix(mix(p[1], p[2], 1/3.), mix(p[2], p[3], 1/3.), 1/3.), 2/3.),
|
||||||
|
mix(mix(mix(p[0], p[1], 2/3.), mix(p[1], p[2], 2/3.), 2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), 1/3.),
|
||||||
|
point(2/3.), color);
|
||||||
|
part3 = new CubicSegment(point(2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), p[2] == p[3] ? p[3] : mix(p[2], p[3], 2/3.), p[3], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
EdgeSegment * QuadraticSegment::convertToCubic() const {
|
||||||
|
return new CubicSegment(p[0], mix(p[0], p[1], 2/3.), mix(p[1], p[2], 1/3.), p[2], color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CubicSegment::deconverge(int param, double amount) {
|
||||||
|
Vector2 dir = direction(param);
|
||||||
|
Vector2 normal = dir.getOrthonormal();
|
||||||
|
double h = dotProduct(directionChange(param)-dir, normal);
|
||||||
|
switch (param) {
|
||||||
|
case 0:
|
||||||
|
p[1] += amount*(dir+sign(h)*sqrt(fabs(h))*normal);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
p[2] -= amount*(dir-sign(h)*sqrt(fabs(h))*normal);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
122
libs/msdf-atlas-gen/include/msdfgen/core/edge-segments.h
Normal file
122
libs/msdf-atlas-gen/include/msdfgen/core/edge-segments.h
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Vector2.h"
|
||||||
|
#include "SignedDistance.h"
|
||||||
|
#include "EdgeColor.h"
|
||||||
|
|
||||||
|
namespace msdfgen {
|
||||||
|
|
||||||
|
// Parameters for iterative search of closest point on a cubic Bezier curve. Increase for higher precision.
|
||||||
|
#define MSDFGEN_CUBIC_SEARCH_STARTS 4
|
||||||
|
#define MSDFGEN_CUBIC_SEARCH_STEPS 4
|
||||||
|
|
||||||
|
/// An abstract edge segment.
|
||||||
|
class EdgeSegment {
|
||||||
|
|
||||||
|
public:
|
||||||
|
EdgeColor color;
|
||||||
|
|
||||||
|
EdgeSegment(EdgeColor edgeColor = WHITE) : color(edgeColor) { }
|
||||||
|
virtual ~EdgeSegment() { }
|
||||||
|
/// Creates a copy of the edge segment.
|
||||||
|
virtual EdgeSegment * clone() const = 0;
|
||||||
|
/// Returns the point on the edge specified by the parameter (between 0 and 1).
|
||||||
|
virtual Point2 point(double param) const = 0;
|
||||||
|
/// Returns the direction the edge has at the point specified by the parameter.
|
||||||
|
virtual Vector2 direction(double param) const = 0;
|
||||||
|
/// Returns the change of direction (second derivative) at the point specified by the parameter.
|
||||||
|
virtual Vector2 directionChange(double param) const = 0;
|
||||||
|
/// Returns the minimum signed distance between origin and the edge.
|
||||||
|
virtual SignedDistance signedDistance(Point2 origin, double ¶m) const = 0;
|
||||||
|
/// Converts a previously retrieved signed distance from origin to pseudo-distance.
|
||||||
|
virtual void distanceToPseudoDistance(SignedDistance &distance, Point2 origin, double param) const;
|
||||||
|
/// Outputs a list of (at most three) intersections (their X coordinates) with an infinite horizontal scanline at y and returns how many there are.
|
||||||
|
virtual int scanlineIntersections(double x[3], int dy[3], double y) const = 0;
|
||||||
|
/// Adjusts the bounding box to fit the edge segment.
|
||||||
|
virtual void bound(double &l, double &b, double &r, double &t) const = 0;
|
||||||
|
|
||||||
|
/// Reverses the edge (swaps its start point and end point).
|
||||||
|
virtual void reverse() = 0;
|
||||||
|
/// Moves the start point of the edge segment.
|
||||||
|
virtual void moveStartPoint(Point2 to) = 0;
|
||||||
|
/// Moves the end point of the edge segment.
|
||||||
|
virtual void moveEndPoint(Point2 to) = 0;
|
||||||
|
/// Splits the edge segments into thirds which together represent the original edge.
|
||||||
|
virtual void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A line segment.
|
||||||
|
class LinearSegment : public EdgeSegment {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Point2 p[2];
|
||||||
|
|
||||||
|
LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
|
||||||
|
LinearSegment * clone() const;
|
||||||
|
Point2 point(double param) const;
|
||||||
|
Vector2 direction(double param) const;
|
||||||
|
Vector2 directionChange(double param) const;
|
||||||
|
double length() const;
|
||||||
|
SignedDistance signedDistance(Point2 origin, double ¶m) const;
|
||||||
|
int scanlineIntersections(double x[3], int dy[3], double y) const;
|
||||||
|
void bound(double &l, double &b, double &r, double &t) const;
|
||||||
|
|
||||||
|
void reverse();
|
||||||
|
void moveStartPoint(Point2 to);
|
||||||
|
void moveEndPoint(Point2 to);
|
||||||
|
void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A quadratic Bezier curve.
|
||||||
|
class QuadraticSegment : public EdgeSegment {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Point2 p[3];
|
||||||
|
|
||||||
|
QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
|
||||||
|
QuadraticSegment * clone() const;
|
||||||
|
Point2 point(double param) const;
|
||||||
|
Vector2 direction(double param) const;
|
||||||
|
Vector2 directionChange(double param) const;
|
||||||
|
double length() const;
|
||||||
|
SignedDistance signedDistance(Point2 origin, double ¶m) const;
|
||||||
|
int scanlineIntersections(double x[3], int dy[3], double y) const;
|
||||||
|
void bound(double &l, double &b, double &r, double &t) const;
|
||||||
|
|
||||||
|
void reverse();
|
||||||
|
void moveStartPoint(Point2 to);
|
||||||
|
void moveEndPoint(Point2 to);
|
||||||
|
void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
|
||||||
|
|
||||||
|
EdgeSegment * convertToCubic() const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A cubic Bezier curve.
|
||||||
|
class CubicSegment : public EdgeSegment {
|
||||||
|
|
||||||
|
public:
|
||||||
|
Point2 p[4];
|
||||||
|
|
||||||
|
CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
|
||||||
|
CubicSegment * clone() const;
|
||||||
|
Point2 point(double param) const;
|
||||||
|
Vector2 direction(double param) const;
|
||||||
|
Vector2 directionChange(double param) const;
|
||||||
|
SignedDistance signedDistance(Point2 origin, double ¶m) const;
|
||||||
|
int scanlineIntersections(double x[3], int dy[3], double y) const;
|
||||||
|
void bound(double &l, double &b, double &r, double &t) const;
|
||||||
|
|
||||||
|
void reverse();
|
||||||
|
void moveStartPoint(Point2 to);
|
||||||
|
void moveEndPoint(Point2 to);
|
||||||
|
void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
|
||||||
|
|
||||||
|
void deconverge(int param, double amount);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue