Bläddra i källkod

adding Signed Distance Font support
fontSize2Scale renamed to fontSize

dmuratshin 9 år sedan
förälder
incheckning
8ffa90aaa1

+ 1 - 1
examples/Demo/src/TestText.h

@@ -134,7 +134,7 @@ public:
         style.hAlign = test.hAlign;
         style.font = resourcesUI.getResFont("big")->getFont();
         style.color = Color(0xffffffff);
-        style.fontSize2Scale = test.scale2size;
+        style.fontSize = test.scale2size;
         text->setStyle(style);
 
         text->setSize(test.size);

+ 32 - 6
oxygine/src/TextField.cpp

@@ -93,7 +93,12 @@ namespace oxygine
 
     void TextField::setFontSize2Scale(int scale2size)
     {
-        _style.fontSize2Scale = scale2size;
+        setFontSize(scale2size);
+    }
+
+    void TextField::setFontSize(int size)
+    {
+        _style.fontSize = size;
         needRebuild();
     }
 
@@ -108,6 +113,15 @@ namespace oxygine
         needRebuild();
     }
 
+    void TextField::setOutlineColor(const Color& c)
+    {
+        _style.outlineColor = c;
+    }
+    void TextField::setOutline(float v)
+    {
+        _style.outline = v;
+    }
+
     void TextField::setHAlign(TextStyle::HorizontalAlign align)
     {
         _style.hAlign = align;
@@ -173,7 +187,7 @@ namespace oxygine
 
     int TextField::getFontSize2Scale() const
     {
-        return _style.fontSize2Scale;
+        return _style.fontSize;
     }
 
     int TextField::getLinesOffset() const
@@ -206,6 +220,16 @@ namespace oxygine
         return _style.kerning;
     }
 
+    const Color& TextField::getOutlineColor() const
+    {
+        return _style.outlineColor;
+    }
+
+    float TextField::getOutline() const
+    {
+        return _style.outline;
+    }
+
     text::Symbol* TextField::getSymbolAt(int pos) const
     {
         return const_cast<TextField*>(this)->getRootNode()->getSymbol(pos);
@@ -309,8 +333,10 @@ namespace oxygine
             stream << " kerning=" << s.kerning;
         if (!onlydiff || def.linesOffset != s.linesOffset)
             stream << " linesOffset=" << s.linesOffset;
-        if (!onlydiff || def.fontSize2Scale != s.fontSize2Scale)
-            stream << " scale2size=" << s.fontSize2Scale;
+        if (!onlydiff || def.fontSize != s.fontSize)
+            stream << " fontSize=" << s.fontSize;
+        if (!onlydiff || def.outline != s.outline)
+            stream << " outline=" << s.outline;
         if (s.font)
         {
             stream << " font='" << s.font->getName() << "'";
@@ -363,7 +389,7 @@ namespace oxygine
 
         if (!_text.empty())
             node.append_attribute("text").set_value(_text.c_str());
-        setAttr(node, "fontsize2scale", _style.fontSize2Scale, def.fontSize2Scale);
+        setAttr(node, "fontsize2scale", _style.fontSize, def.fontSize);
         setAttr(node, "linesOffset", _style.linesOffset, def.linesOffset);
         setAttr(node, "kerning", _style.kerning, def.kerning);
         setAttr(node, "valign", _style.vAlign, def.vAlign);
@@ -386,7 +412,7 @@ namespace oxygine
         _style.hAlign = (TextStyle::HorizontalAlign)node.attribute("halign").as_int(def.hAlign);
         _style.multiline = node.attribute("multiline").as_bool(def.multiline);
         _style.breakLongWords = node.attribute("breakLongWords").as_bool(def.breakLongWords);
-        _style.fontSize2Scale = node.attribute("fontsize2scale").as_int(def.fontSize2Scale);
+        _style.fontSize = node.attribute("fontsize2scale").as_int(def.fontSize);
         _style.linesOffset = node.attribute("linesOffset").as_int(def.linesOffset);
         _style.kerning = node.attribute("kerning").as_int(def.kerning);
         const char* fnt = node.attribute("font").as_string(0);

+ 17 - 1
oxygine/src/TextField.h

@@ -39,6 +39,8 @@ namespace oxygine
         bool                        getMultiline() const;
         bool                        getBreakLongWords() const;
         text::Symbol*               getSymbolAt(int pos) const;
+        const Color&                getOutlineColor() const;
+        float                       getOutline() const;
 
 
         bool getBounds(RectF&) const OVERRIDE;
@@ -56,11 +58,20 @@ namespace oxygine
         void setLinesOffset(int offset);
         /**Overwrites TextStyle kerning*/
         void setKerning(int kerning);
-        /**Overwrites TextStyle scale2Size.*/
+        /**Overwrites TextStyle scale2Size. deprecated, use setFontSize*/
+        OXYGINE_DEPRECATED
         void setFontSize2Scale(int scale2size);
+        /**Overwrites TextStyle fontSize*/
+        void setFontSize(int size);
+
         /**Overwrites TextStyle font.*/
         void setFont(const Font* rs);
 
+        /**Overwrites TextStyle outlineColor. works only with SD font*/
+        void setOutlineColor(const Color&);
+        /**Overwrites TextStyle outline. works only with SD font*/
+        void setOutline(float v);
+
         void setStyle(const TextStyle& st);
         /**Changes text utf-8 string*/
         void setText(const std::string& str);
@@ -77,6 +88,11 @@ namespace oxygine
         bool isOn(const Vector2& localPosition);
 
 
+        typedef Property<Color, const Color&, TextField, &TextField::getOutlineColor, &TextField::setOutlineColor>  TweenOutlineColor;
+
+
+    public:
+
         std::string dump(const dumpOptions& options) const;
         void doRender(RenderState const& parentRenderState);
 

+ 14 - 5
oxygine/src/TextStyle.h

@@ -34,20 +34,29 @@ namespace oxygine
             linesOffset(0),
             kerning(0),
             multiline(false),
-            fontSize2Scale(0),
-            breakLongWords(false) {}
+            fontSize(0),
+            breakLongWords(false),
+            outline(0) {}
 
         const Font* font;
 
         HorizontalAlign hAlign;
         VerticalAlign vAlign;
 
-        int linesOffset;//distance offset between lines
-        int kerning;
+        int linesOffset;//vertical distance offset between lines
+        int kerning;//horizontal distance
         bool multiline;
         bool breakLongWords;//works with multiline flag. breakLongWords = false doesn't allow to break too long words
         Color color;
-        int fontSize2Scale;
+
+        Color outlineColor;//works only with SD fonts
+        float outline;//works only with SD fonts
+
+        union
+        {
+            int fontSize;
+            int fontSize2Scale;//DEPRECATED, use fontSize
+        };
     };
 
     std::string dumpStyle(const TextStyle& s, bool onlydiff);

+ 1 - 1
oxygine/src/initActor.h

@@ -68,7 +68,7 @@ namespace oxygine
         typedef argT<std::string, const std::string&, TextField, &TextField::setHtmlText> htmlText;
         typedef argT<TextStyle::HorizontalAlign, TextStyle::HorizontalAlign, TextField, &TextField::setHAlign> hAlign;
         typedef argT<TextStyle::VerticalAlign, TextStyle::VerticalAlign, TextField, &TextField::setVAlign> vAlign;
-        typedef argT<int, int, TextField, &TextField::setFontSize2Scale> fontSize2Scale;
+        typedef argT<int, int, TextField, &TextField::setFontSize> fontSize;
         typedef argT<const Font*, const Font*, TextField, &TextField::setFont> font;
         typedef argT<bool, bool, TextField, &TextField::setMultiline> multiline;
 

+ 43 - 25
oxygine/src/res/ResFontBM.cpp

@@ -17,7 +17,7 @@ namespace oxygine
         ResFontBM* font = 0;
 
         font = new ResFontBM();
-        font->_createFont(&context, false, false);
+        font->_createFont(&context, false, false, 1);
         setNode(font, context.walker.getNode());
         context.resources->add(font);
 
@@ -31,7 +31,7 @@ namespace oxygine
         ResFontBM* font = 0;
 
         font = new ResFontBM();
-        font->_createFont(&context, false, true);
+        font->_createFont(&context, false, true, 1);
         setNode(font, context.walker.getNode());
         context.resources->add(font);
 
@@ -45,7 +45,7 @@ namespace oxygine
         ResFontBM* font = 0;
 
         font = new ResFontBM();
-        font->_createFont(&context, true, false);
+        font->_createFont(&context, true, false, 1);
         setNode(font, context.walker.getNode());
 
         //context.meta = context.meta.next_sibling();
@@ -68,7 +68,14 @@ namespace oxygine
     {
         _premultipliedAlpha = premultipliedAlpha;
         _file = path;
-        _createFont(0, false, false);
+        _createFont(0, false, false, 1);
+    }
+
+    void ResFontBM::initSD(const char* fntPath, int downsample)
+    {
+        _premultipliedAlpha = true;
+        _file = fntPath;
+        _createFont(0, true, false, downsample);
     }
 
     void ResFontBM::cleanup()
@@ -220,7 +227,7 @@ namespace oxygine
         return data;
     }
 
-    void ResFontBM::_createFontFromTxt(CreateResourceContext* context, char* fontData, const std::string& fontPath)
+    void ResFontBM::_createFontFromTxt(CreateResourceContext* context, char* fontData, const std::string& fontPath, int downsample)
     {
         char* key = 0;
         char* value = 0;
@@ -271,6 +278,12 @@ namespace oxygine
             }
         }
 
+        tw /= downsample;
+        th /= downsample;
+        lineHeight /= downsample;
+        base /= downsample;
+        fontSize /= downsample;
+
         char tail[255];
         char head[255];
         path::split(fontPath.c_str(), head, tail);
@@ -370,16 +383,16 @@ namespace oxygine
             }
 
             spTexture t = _pages[page_].texture;
-            float iw = 1.0f / t->getWidth();
-            float ih = 1.0f / t->getHeight();
+            float iw = 1.0f / t->getWidth() / downsample;
+            float ih = 1.0f / t->getHeight() / downsample;
 
             glyph gl;
             gl.src = RectF(xpos * iw, ypos * ih, width * iw, height * ih);
-            gl.sw = width;
-            gl.sh = height;
-            gl.offset_x = xoffset;
-            gl.offset_y = yoffset - base;
-            gl.advance_x = xadvance;
+            gl.sw = width / downsample;
+            gl.sh = height / downsample;
+            gl.offset_x = xoffset / downsample;
+            gl.offset_y = yoffset / downsample - base;
+            gl.advance_x = xadvance / downsample;
             gl.advance_y = 0;
 
             int code = 0;
@@ -412,7 +425,7 @@ namespace oxygine
         _pages.push_back(p);
     }
 
-    void ResFontBM::_createFont(CreateResourceContext* context, bool sd, bool bmc)
+    void ResFontBM::_createFont(CreateResourceContext* context, bool sd, bool bmc, int downsample)
     {
         if (sd)
             _format = TF_L8;
@@ -440,7 +453,7 @@ namespace oxygine
 
         if (!(fb[0] == '<' && fb[1] == '?' && fb[2] == 'x'))
         {
-            _createFontFromTxt(context, reinterpret_cast<char*>(fb.getData()), path);
+            _createFontFromTxt(context, reinterpret_cast<char*>(fb.getData()), path, downsample);
             return;
         }
         /////////////////////////////////////////////////
@@ -453,12 +466,14 @@ namespace oxygine
         pugi::xml_node info = root.child("info");
 
         //<info face="Century Gothic" size="-24" bold="0" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="0"/>
-        int fontSize = info.attribute("size").as_int();
+        int fontSize = info.attribute("size").as_int() / downsample;
+
+
 
 
         pugi::xml_node common = info.next_sibling("common");
-        int lineHeight = common.attribute("lineHeight").as_int();
-        int base = common.attribute("base").as_int();
+        int lineHeight = common.attribute("lineHeight").as_int() / downsample;
+        int base = common.attribute("base").as_int() / downsample;
 
         pugi::xml_node pages = common.next_sibling("pages");
 
@@ -466,6 +481,10 @@ namespace oxygine
         int th = common.attribute("scaleH").as_int();
 
 
+        tw /= downsample;
+        th /= downsample;
+
+
         char folder[255];
         char tail[255];
         path::split(path.c_str(), folder, tail);
@@ -481,7 +500,6 @@ namespace oxygine
             load(0);
 
 
-
         fontSize = abs(fontSize);
         Font* font = new Font();
         font->init(getName().c_str(), fontSize, fontSize, lineHeight + fontSize - base);
@@ -537,16 +555,16 @@ namespace oxygine
             }
 
             spTexture t = _pages[page].texture;
-            float iw = 1.0f / t->getWidth();
-            float ih = 1.0f / t->getHeight();
+            float iw = 1.0f / t->getWidth() / downsample;
+            float ih = 1.0f / t->getHeight() / downsample;
 
             glyph gl;
             gl.src = RectF(xpos * iw, ypos * ih, width * iw, height * ih);
-            gl.sw = width;
-            gl.sh = height;
-            gl.offset_x = xoffset;
-            gl.offset_y = yoffset - base;
-            gl.advance_x = xadvance;
+            gl.sw = width / downsample;
+            gl.sh = height / downsample;
+            gl.offset_x = xoffset / downsample;
+            gl.offset_y = yoffset / downsample - base;
+            gl.advance_x = xadvance / downsample;
             gl.advance_y = 0;
 
             int code = 0;

+ 3 - 2
oxygine/src/res/ResFontBM.h

@@ -25,6 +25,7 @@ namespace oxygine
 
         /**loads "fnt" font from file, supported XML and text format*/
         void init(const char* fntPath, bool premultipliedAlpha = false);
+        void initSD(const char* fntPath, int downsample);
 
         void cleanup();
 
@@ -43,8 +44,8 @@ namespace oxygine
         void _load(LoadResourcesContext*);
         void _unload();
         void _restore(Restorable*, void*);
-        void _createFont(CreateResourceContext* context, bool sd, bool bmc);
-        void _createFontFromTxt(CreateResourceContext* context, char* fontData, const std::string& fontPath);
+        void _createFont(CreateResourceContext* context, bool sd, bool bmc, int downsample);
+        void _createFontFromTxt(CreateResourceContext* context, char* fontData, const std::string& fontPath, int downsample);
 
         typedef std::vector<page> pages;
         pages _pages;

+ 2 - 2
oxygine/src/text_utils/Aligner.cpp

@@ -66,8 +66,8 @@ namespace oxygine
 
             const TextStyle& st = getStyle();
             _scale = st.font->getScale();
-            if (st.fontSize2Scale)
-                _scale = st.font->getSize() / float(st.fontSize2Scale);
+            if (st.fontSize)
+                _scale = st.font->getSize() / float(st.fontSize);
 
             width = int(width * _scale);
             height = int(height * _scale);