From 9ad17a5c9d622714b8c69a9ec3e51c6a38326025 Mon Sep 17 00:00:00 2001 From: ExMix Date: Fri, 27 Jun 2014 14:23:29 +0300 Subject: [PATCH] review fixes --- font_generator/df_map.cpp | 17 +++++++++-------- font_generator/df_map.hpp | 9 +++++---- font_generator/engine.cpp | 31 +++++++++++++++---------------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/font_generator/df_map.cpp b/font_generator/df_map.cpp index 12b1d5b82d..8d13ec2f7e 100644 --- a/font_generator/df_map.cpp +++ b/font_generator/df_map.cpp @@ -1,12 +1,12 @@ #include "df_map.hpp" +#include "../std/algorithm.hpp" #include "../std/cmath.hpp" -DFMap::DFMap(const vector & data, int width, int height, unsigned char inValue, unsigned char outValue) +DFMap::DFMap(const vector & data, int width, int height, unsigned char inValue, unsigned char outValue) : m_width(width) , m_height(height) { - //m_distances = new float[m_width * m_height]; m_distances.resize(m_width * m_height); for (int i = 0; i < m_width * m_height; ++i) m_distances[i] = 0.0f; @@ -34,14 +34,14 @@ void DFMap::Normalize() float minSignedDistance = 0; for (int j = 0; j < m_height; ++j) for (int i = 0; i < m_width; ++i) - minSignedDistance = fmin(minSignedDistance, GetDistance(i, j)); + minSignedDistance = min(minSignedDistance, GetDistance(i, j)); float maxValue = 0.0f; for (int j = 0; j < m_height; ++j) for (int i = 0; i < m_width; ++i) { float d = GetDistance(i, j) - minSignedDistance; - maxValue = fmax(d, maxValue); + maxValue = max(d, maxValue); SetDistance(d, i, j); } @@ -65,19 +65,20 @@ unsigned char * DFMap::GenerateImage(int & w, int & h) return image; } -void DFMap::Do(vector const & data, unsigned char inValue, unsigned char outValue) +void DFMap::Do(vector const & data, unsigned char inValue, unsigned char outValue) { for (int j = 0; j < m_height; ++j) { for (int i = 0; i < m_width; ++i) { if (get(data, i, j) == inValue) - SetDistance(sqrtf(findRadialDistance(data, i, j, 256, outValue)), i, j); + SetDistance(sqrt(findRadialDistance(data, i, j, 256, outValue)), i, j); } } } -float DFMap::findRadialDistance(vector const & data, int pointX, int pointY, int maxRadius, unsigned char outValue) +float DFMap::findRadialDistance(vector const & data, int pointX, int pointY, + int maxRadius, unsigned char outValue) const { float minDistance = maxRadius * maxRadius; for (int j = pointY - maxRadius; j < pointY + maxRadius; ++j) @@ -95,7 +96,7 @@ float DFMap::findRadialDistance(vector const & data, int pointX, int xDist = pointX - i; int yDist = pointY - j; float d = xDist * xDist + yDist * yDist; - minDistance = fmin(d, minDistance); + minDistance = min(d, minDistance); } } } diff --git a/font_generator/df_map.hpp b/font_generator/df_map.hpp index 2b23654928..731f3f3c6b 100644 --- a/font_generator/df_map.hpp +++ b/font_generator/df_map.hpp @@ -1,11 +1,12 @@ #pragma once #include "../std/vector.hpp" +#include "../std/stdint.hpp" class DFMap { public: - DFMap(vector const & data, + DFMap(vector const & data, int width, int height, unsigned char inValue, unsigned char outValue); @@ -17,10 +18,10 @@ public: unsigned char * GenerateImage(int & w, int & h); private: - void Do(vector const & data, unsigned char inValue, unsigned char outValue); - float findRadialDistance(vector const & data, + void Do(vector const & data, unsigned char inValue, unsigned char outValue); + float findRadialDistance(vector const & data, int pointX, int pointY, - int radius, unsigned char outValue); + int radius, unsigned char outValue) const; template T get(vector const & data, int i, int j) const diff --git a/font_generator/engine.cpp b/font_generator/engine.cpp index 3d8bd3735b..aba34c52d4 100644 --- a/font_generator/engine.cpp +++ b/font_generator/engine.cpp @@ -10,6 +10,8 @@ #include #include +#include "../base/macros.hpp" + #include "../std/cmath.hpp" #include "../std/vector.hpp" #include "../std/map.hpp" @@ -47,7 +49,7 @@ namespace return m_height; } - ZeroPoint GetZeroPoint(int pageNumber) + ZeroPoint GetZeroPoint(int pageNumber) const { ZeroPoint zPoint; zPoint.m_x = pageNumber % GetWidth(); @@ -92,7 +94,7 @@ namespace MetricTemplate(16, 4, 4) }; - for (int i = 0; i < 15; ++ i) + for (unsigned long i = 0; i < ARRAY_SIZE(templates); ++ i) { if (templates[i].m_pageCount == pageCount) { @@ -155,13 +157,14 @@ namespace if (f.open(QIODevice::ReadOnly) == false) throw 1; - unsigned char * fontBuffer = new unsigned char[f.size()]; - f.read((char *)fontBuffer, f.size()); - stbtt_fontinfo fontInfo; - stbtt_InitFont(&fontInfo, fontBuffer, 0); + { + vector fontBuffer(f.size(), 0); + f.read((char *)&fontBuffer[0], fontBuffer.size()); + stbtt_InitFont(&fontInfo, &fontBuffer[0], 0); + } + float scale = stbtt_ScaleForPixelHeight(&fontInfo, /*GlyphScaler * */m_fontSize); - delete[] fontBuffer; for (range_iter_t range = font.value().begin(); range != font.value().end(); ++range) { for (int unicodeCode = range->first; unicodeCode <= range->second; ++unicodeCode) @@ -230,9 +233,7 @@ namespace int width = EtalonTextureSize * compositor.GetWidth(); int height = EtalonTextureSize * compositor.GetHeight(); - unsigned char * resultImg = new unsigned char[width * height]; - memset(resultImg, 0, width * height); - + vector resultImg(width * height, 0); bool firstEmpty = true; for (size_t k = 0; k < outRects.size(); ++k) { @@ -277,13 +278,11 @@ namespace foreach (GlyphInfo info, infos) delete[] info.m_img; - m_image = QImage(resultImg, width, height, QImage::Format_Indexed8); + m_image = QImage(&resultImg[0], width, height, QImage::Format_Indexed8); m_image.setColorCount(256); for (int i = 0; i < 256; ++i) m_image.setColor(i, qRgb(i, i, i)); - delete[] resultImg; - m_infos.clear(); m_infos.append(infos); m_infos.append(emptyInfos); @@ -294,11 +293,11 @@ namespace return m_image; } - QList const & GetInfos() { return m_infos; } + QList const & GetInfos() const { return m_infos; } private: - unsigned char * processGlyph(unsigned char * glyphImage, int width, int height, - int & newW, int & newH) + static unsigned char * processGlyph(unsigned char * glyphImage, int width, int height, + int & newW, int & newH) { static int border = 2; int sWidth = width + 4 * border;