查看: 1940|回复: 0

[原创发布] TextBoard2.0

[复制链接]
梦石
0
星屑
6483
在线时间
119 小时
注册时间
2020-1-8
回帖
205
发表于 2023-6-24 10:04:43 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 RPGzh500223 于 2023-6-24 10:13 编辑

[pre lang="ruby"]=begin

使用FreeType进行中文的文本描绘,脚本编辑器置顶即可
支持文字粗体、斜体、旋转、描边、阴影
使用内存字体文件,仅支持矢量字体,不支持字体的kern设置
CPU支持SSE

FontEx用13个元素的数组来表示字体
[0] 内存字体文件 String
[1] 字体大小     Fixnum  须大于0
[2] 字体颜色     Fixnum/BigNum  单色填充  0xAaRrGgBb
                  String  沿文字方向线性渐变填充
                            渐变由节点描述,节点结构 float pos, int argb
                            至少2个节点,首节点pos须0.0f,尾节点pos须1.0f
                            后节点比前节点pos大0.001
[3] 水平方向斜体 Fixnum/Float  与Y正半轴的顺时针夹角 取值范围 [-65, 65]
[4] 垂直方向斜体 Fixnum/Float  与X正半轴的顺时针夹角 取值范围 [-65, 65]
                                  当水平方向斜体为0时,才有效
[5] 文本旋转角度 Fixnum/Float  与文字方向的逆时针夹角
[6] 水平方向粗体 Fixnum  可以负值
[7] 垂直方向粗体 Fixnum  可以负值
[8] 描边大小     Fixnum  须大于等于0
[9] 描边颜色     Fixnum/BigNum  单色填充  0xAaRrGgBb
[10]阴影         0/nil   表示无阴影
                  String  阴影描述,由节点组成,可多个节点
                          阴影节点 char offset_hori 相对于主体的水平偏移
                                   char offset_vert 相对于主体的垂直偏移
                                   char blur_size   模糊大小 建议 -16..16
                                                    负值类似空心效果
                                   char blend_times 阴影加强,类似PS ctrl+j
                                    int argb        阴影颜色                                             
[11]字体间距     Fixnum  字体间距调整值
[12]标志参数     Fixnum  
                          0..1bit 描绘方式 0: 正常;1: 加法;2: 减法
                                           加减法只影响alpha通道,3修正为0
                             2bit 布局方式 0:水平;1: 垂直
                             3bit [11]字体间距计算方式
                                  0: 额外附加值;1: 绝对值作为总间距
                             4bit 中文水平标点符号修正为垂直符号(垂直布局时)
                                  0: 不修正;1: 修正

颜色直接使用数值表示,并不使用RMXP的Color对象
  单色描绘时,颜色设置为0时,不进行描绘
  
文字描绘由单个字符分3层(先后依次为 阴影、描边、主体)描绘,字符可能相互影响
  描边是字体轮廓,类似空心字体的效果
  阴影以主体为基础,不包括描边

不进行主体描绘,[2] = 0
不进行描边描绘,[8] = 0
不进行阴影描绘,[10] = 0 或 nil

文本的要求,仅支持符合Utf8编码的部分(字符的首字节与后续字节都必须符合)

int, char, float,short 为C语言的类型
=end

class FontEx < Array
  
  def initialize(name = @@default_name, *args)
    super(13, 0)
    t = args.size
    t = 12 if t > 12
    t.times {|i| self[i + 1] = args }
    self.name = name
    self[1] = @@default_size  if t == 0
    self[2] = @@default_color if t == 1
  end
  
  
  ["file_buffer", "size", "color", "oblique_x", "oblique_y",
   "rotate_angle", "bold_x", "bold_y", "stroke_size", "stroke_color",
   "shadow", "kerning", "flags"].each_with_index do |attr, i|
     
    define_method(attr) { self }
    define_method(attr + "=") {|value| self = value}     
  end
   
  def name
    @font_name
  end
  
  def name=(font_name)
    @font_name = @@font_cache.include?(font_name) ? font_name : @@default_name
    self[0] = @@font_cache[@font_name]
  end

  def blend_type
    self[12] & 3
  end
  
  def blend_type=(type)
    type = 0 if type < 0 || type > 2
    self[12] = (self[12] & -4) | type
  end
  
  def layout
    self[12][2]
  end
  
  def layout_hori
    self[12] &= -5
  end
  
  def layout_vert(bPunctAmend = false)
    self[12] |= 4
    self[12] |= 16  if bPunctAmend == true
    self[12] &= -17 if bPunctAmend == false
  end

  def kerning_fixed=(bool)
    self[12] |= 8  if bool == true
    self[12] &= -9 if bool == false
  end
  
  def clampByte(value)
    value = 0   if value < 0
    value = 255 if value > 255
    return value.to_i
  end
  
  def make_color(r, g, b, a)
    r = self.clampByte(r)
    g = self.clampByte(g)
    b = self.clampByte(b)
    a = self.clampByte(a)
    return a << 24 | r << 16 | g << 8 | b
  end
  
  def color_set(r, g, b, a = 255)
    self[2] = self.make_color(r, g, b, a)
  end
  
  def stroke_color_set(r, g, b, a = 255)
    self[9] = make_color(r, g, b, a)
  end
  
  dll = "FreeType_RMXP.dll"
  
  Text_Out = Win32API.new(dll, 'text_out', 'LLLLLLL', 'L')
  Text_Out_Bzr = Win32API.new(dll, 'text_out_BZR', 'LLLLLLLL', 'L')
  Text_Size = Win32API.new(dll, 'text_size', 'LLLL', 'L')
  Draw_Text = Win32API.new(dll, 'draw_text', 'LLLLLL', 'L')
  
  #————————————————————————————
  # 类似GDI的text_out
  #     bitmap: 位图对象
  #      dx/dy: 文本描绘的位置
  #       text: 文本描绘的字符串
  # cTerminate: 字符串的终止符(终止符时仅最低位字节有效)
  #  alignBsln: 文本描绘的基准线对齐方式( < 0 或 > 2 修正为0 )
  #               水平布局时,0=>字符框顶端;1=>水平基准线;2=>字符框底端
  #               垂直布局时,0=>字符框左端;1=>垂直基准线;2=>字符框右端
  # alignPoint: 点(dx, dy)在文本描绘基准线上的位置( < 0 或 > 2 修正为0 )
  #               0=>起始点;1=>中间点;2=>终止点
  #
  # 备注:对齐方式,不含阴影部分
  #————————————————————————————
  def text_out(bitmap, dx, dy, text, cTerminate = -1, alignBsln = 0,
    alignPoint = 0)
   
    alignBsln = 0 if alignBsln < 0 || alignBsln > 2
    alignPoint = 0 if alignPoint < 0 || alignPoint > 2
   
    Text_Out.call(bitmap.__id__, dx, dy, text.__id__, cTerminate,
      self.__id__, alignBsln | alignPoint << 4)
  end
  
  #————————————————————————————
  # 使用二阶贝塞尔曲线实现的文本路径描绘
  #     bitmap: 位图对象  
  #       text: 文本描绘的字符串
  # cTerminate: 字符串的终止符(仅最低位字节有效)
  #     points: 贝塞尔曲线控制点的字符串,点结构 short x, short y
  #               至少3个点,后续每2个点与前一个点构成新的二阶贝塞尔曲线
  #               p0, p1, p2, p3, p4
  #               p0, p1, p2
  #                       p2, p3, p4
  #       step: 贝塞尔曲线计算时的步数,该值越小,文本间距越大
  #  alignBsln: 文本描绘的对齐方式( < 0 或 > 3 修正为0 )
  #               水平布局时,0=>水平基准线;1=>字符框顶端
  #                           2=>字符框上下中端;3=>字符框底端
  #               垂直布局时,0=>垂直基准线;1=>字符框左端
  #                           2=>字符框左右中端;3=>字符框右端
  #    bSmooth: 多个二阶贝塞尔曲线之间是否平滑过渡
  #               p0, p1, p1.5
  #                       p1.5, p2, p2.5
  #                                 p2.5, p3, p4
  # bCurveLine: 贝塞尔曲线是否直接写入位图( 不推荐使用 )
  #curve_color: 贝塞尔曲线的颜色,0xAaRrGgBb
  #
  # 备注:不支持 kerning_fixed = true
  #————————————————————————————
  def text_out_path(bitmap, text, cTerminate, points, step,
    alignBsln = 0, bSmooth = false, bCurveLine = false, curve_color = -1)
   
    mode = alignBsln
    mode = 0 if alignBsln < 0 || alignBsln > 3
    mode |= 0x10 if bSmooth == true
    mode |= 0x100 if bCurveLine == true
   
    Text_Out_Bzr.call(bitmap.__id__, text.__id__, cTerminate, self.__id__,
      points.__id__, step, mode, curve_color)
  end
  
  # 搭配draw_text使用,计算的文本大小含阴影部分
  def text_size(text, cTerminate = -1)
    rect = Rect.new(0, 0, 0, 0)
    Text_Size.call(self.__id__, text.__id__, cTerminate, rect.__id__)
    return rect
  end
  
  #————————————————————————————
  # 类似GDI的draw_text
  #     bitmap: 位图对象
  #       rect: 文本描绘的位置区域,RMXP Rect对象
  #       text: 文本描绘的字符串
  # cTerminate: 字符串的终止符(仅最低位字节有效)
  #      align: 文本描绘的对齐方式( < 0 或 > 9 修正为4 )
  #               对齐方式参考小键盘的1-9
  #————————————————————————————
  def draw_text(bitmap, rect, text, cTerminate = -1, align = 4)
    align = 4 if align < 1 || align > 9
    Draw_Text.call(bitmap.__id__, rect.__id__, text.__id__, cTerminate,
      self.__id__, align)
  end
   
  class << self
    def cache_clear
      @@font_cache.clear
    end
   
    # font_name作为键值,可以自定义
    def add_font(file_path, font_name)
      return if @@font_cache.has_key?(font_name)
      open(file_path, "rb") {|f| @@font_cache[font_name] = f.read }
    end
   
    def remove_font(font_name)
      @@font_cache.delete(font_name)
    end
  end
  
  @@font_cache = {}
  @@default_name = "黑体"
  @@default_size = 22
  @@default_color = -1
  
  # 添加需要使用的字体
  FontEx.add_font("C:/Windows/Fonts/simhei.ttf", "黑体")
end[/pre]

FreeType_RMXP.zip

288.2 KB, 下载次数: 5

FreeType_RMXP.dll

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部