RGSSType alpha 0.1
2013 年 11 月 27 日
一个用于直接在RGSS3的位图上写文字的类,
不经过bitmap.draw_text,而是直接写像素,
就算RGSS3启动时没有安装字体,安装字体之后使用RGSSType.new仍然有用
这个版本只实现了一个简单的写字体方法,而且并没有作为Bitmap的方法被定义
用法:
RGSSType#font
返回RGSSType使用的Font对象,可以设置他的字型字号颜色等, 文字轮廓阴影暂时不支持。
RGSSType#writetext(bitmap, string, x, y, color[, height])
在bitmap上,从(x, y)开始书写文字string, height
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | =begin   RGSSType Alpha 0.1   Author: 晴兰 Seiran   Website: http://seiran.mist.so   License:     Copyright (c) 2013, Seiran     All rights reserved.     Redistribution and use in source and binary forms, with or without      modification, are permitted provided that the following conditions are met:     1. Redistributions of source code must retain the above copyright notice,      this list of conditions and the following disclaimer.     2. Redistributions in binary form must reproduce the above copyright notice,      this list of conditions and the following disclaimer in the documentation      and/or other materials provided with the distribution.     3. Neither the name of the seiran.mist.so nor the names of its contributors      may be used to endorse or promote products derived from this software      without specific prior written permission.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"      AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE      IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE      ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE      LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR      CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS      INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN      CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)      ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE      POSSIBILITY OF SUCH DAMAGE. =end #encoding:UTF-8 module SAFX   module_function   def to_param(a)     case a when Integer then "L" when String then "p" end   end   def to_ptr(a)     case a  when Integer then a when String then [a].pack('p').unpack("L").first end   end   def api(dll,func)     lambda{|*args|        Win32API.new(dll,func,args.map{|x|to_param(x)}, 'i').call(*args)     }   end   def to_wc(str, cp = 65001)     (buf = ""*str.length)[0, 2*api('Kernel32', 'MultiByteToWideChar').call(cp,      0, to_ptr(str), -1, buf, buf.length)]   end   def to_mb(str, cp = 65001)     (buf = ""* str.length)[0, api('Kernel32', 'WideCharToMultiByte').call(cp,      0, to_ptr(str), -1, buf, buf.length, 0, 0)]   end end class RGSSType   module API     FUNC = {}     HANDLES  = [DL::dlopen('gdi32'), DL::dlopen("KERNEL32"),      DL::dlopen("USER32")]     def self.method_missing(sym, *args)       FUNC[sym] ||= DL::CFunc.new(         HANDLES.map{|x| x[sym.to_s] rescue nil}.compact[0] ,         DL::TYPE_VOIDP, '', :stdcall       )             FUNC[sym].call(args.map{|x| DL::CPtr[x]})     end   end   attr_accessor :font   def initialize(font = ::Font.new ,w = 300, h = 300)     @font = font     @dc = API.CreateCompatibleDC(0)     @ptr = DL.malloc(w * h * 4)     @bitmap = API.CreateBitmap(w, h, 1, 32, @ptr)     @w, @h = w, h     DL.free(@ptr)     resetfont   end   def resetfont     API.DeleteObject(@hfont) if @hfont != nil     API.SelectOBject(@dc, @hold) if @hold != nil     @hold = nil     @hfont = nil   end   def bits     buf = [].pack("x#{@w * @h * 4}")     API.GetBitmapBits(@bitmap, buf.size, buf)     buf   end   def boolvalue a     a ? 1 : 0   end   def dispose     resetfont     API.DeleteObject(@bitmap)     API.ReleaseDC(@dc)   end   def createfont     resetfont     args = [@font.size, 0, 0, 0,              @font.bold ? 700 : 400,              boolvalue(@font.italic),              0,0,             1, #default_charset             0,0,4,0,             SAFX.to_wc(@font.name+"")]     #                        here^     @hfont = API.CreateFontW(*args)     @hold = API.SelectObject(@dc, @hfont)   end   def getchar8b(a)     API.GetGlyphIndicesW(@dc, SAFX.to_wc(a+""), 1, (index=[].pack("x4")), 0)     #                                    here^     index = index.unpack("L").first     glyph = [].pack("x24")     mat   = [0,1,0,0,0,0,0,1].pack("S*")     len   = API.GetGlyphOutline(@dc, index, 0x85, glyph, 0, 0, mat)     buf   = [].pack "x#{len}"     len = API.GetGlyphOutline(@dc, index, 0x85, glyph, len, buf, mat)     bx, by, ox, oy, cx, cy = glyph.unpack("llllss")     [buf, bx, by, ox, oy, cx, cy, len]   end   def char_height(ch)     u =  getchar8b(ch)     height = u[5]   end   def writechar8b(bitmap, ch, cx, cy, color, height = nil)     u =  getchar8b(ch)     r,g,b,a=color.red, color.green, color.blue, color.alpha     return [0, 0] if u[0].size == 0     colors = 256.times.map{|x|       Color.new(r * x  * 4 / 256, g * x  * 4 / 256, b * x  * 4 / 256, a)     }     i = 0     height ||= u[0].size / ((u[1] + 3) / 4 * 4)     v = u[0].unpack("C*")     u[2].times{|y|       u[1].times{|x|          bitmap.set_pixel(x + cx + u[3], cy + height - u[4] + y, colors[v[i]*4])          i += 1       }       i = (i + 3) >> 2 << 2     }     [u[5], u[6]]   end   def writetext(bitmap, text, cx, cy, color)     createfont     height = text.split(//).map{|x| char_height(x)}.max     text.split(//).each{|ch|       w,h = writechar8b(bitmap, ch, cx, cy, color, height)       cx += w     }   end end |