7

String.length只会告诉我字符串中有多少个字符。(其实在 Ruby 1.9 之前,它只会告诉我有多少字节,这更没用了。)

我真的很想知道一个字符串有多少'en'宽。例如:

'foo'.width
# => 3

'moo'.width
# => 3.5          # m's, w's, etc. are wide

'foi'.width
# => 2.5          # i's, j's, etc. are narrow

'foo bar'.width
# => 6.25         # spaces are very narrow

n如果我能得到一个字符串的第一个 en会更好:

'foo'[0, 2.en]
# => "fo"

'filial'[0, 3.en]
# => "fili"

'foo bar baz'[0, 4.5en]
# => "foo b"

如果我可以制定整个事情的战略,那就更好了。有些人认为空格应该是 0.25en,有些人认为应该是 0.33,等等。

4

6 回答 6

15

您应该使用 RMagick gem 使用您想要的字体渲染“Draw”对象(您可以加载 .ttf 文件等)

代码看起来像这样:

   the_text = "TheTextYouWantTheWidthOf"
   label = Draw.new
   label.font = "Vera" #you can also specify a file name... check the rmagick docs to be sure
   label.text_antialias(true)
   label.font_style=Magick::NormalStyle
   label.font_weight=Magick::BoldWeight
   label.gravity=Magick::CenterGravity
   label.text(0,0,the_text)
   metrics = label.get_type_metrics(the_text)
   width = metrics.width
   height = metrics.height

您可以在我的按钮制作器中看到它的实际效果:http ://risingcode.com/button/everybodywangchungtonite

于 2008-12-19T12:11:22.950 回答
5

使用ttfunk gem 从字体文件中读取指标。然后,您可以在 em 中获取一串文本的宽度。这是我将这个示例添加到 gem 中的拉取请求。

require 'rubygems'
require 'ttfunk'
require 'valuable'
# Everything you never wanted to know about glyphs:
# http://chanae.walon.org/pub/ttf/ttf_glyphs.htm

# this code is a substantial reworking of:
# https://github.com/prawnpdf/ttfunk/blob/master/examples/metrics.rb

class Font
  attr_reader :file

  def initialize(path_to_file)
    @file = TTFunk::File.open(path_to_file)
  end

  def width_of( string )
    string.split('').map{|char| character_width( char )}.inject{|sum, x| sum + x}
  end

  def character_width( character )
    width_in_units = ( horizontal_metrics.for( glyph_id( character )).advance_width )
    width_in_units.to_f / units_per_em
  end

  def units_per_em
    @u_per_em ||= file.header.units_per_em
  end

  def horizontal_metrics
    @hm = file.horizontal_metrics
  end

  def glyph_id(character)
    character_code = character.unpack("U*").first
    file.cmap.unicode.first[character_code]
  end
end

这是在行动:

>> din = Font.new("#{File.dirname(__FILE__)}/../../fonts/DIN/DINPro-Light.ttf")
>> din.width_of("Hypertension")
=> 5.832
# which is correct! Hypertension in that font takes up about 5.832 em! It's over by maybe ... 0.015.
于 2015-02-25T17:24:23.220 回答
4

您可以尝试创建一个标准化的“宽度比例表”来计算近似值,基本上您需要存储每个字符的宽度,然后遍历字符串将宽度相加。

我在这里找到了这张桌子:

Left, Width, Advance values for ArialBD16 'c' through 'm'
Letter  Left    Width   Advance
c        1       7       9
d        1       8       10
e        1       8       9
f        0       6       5
g        0       9       10
h        1       8       10
i        1       2       4
j       -1       4       4
k        1       8       9
l        1       2       4
m        1       12      14

如果你想认真一点,我会从 webkit、gecko 和 OO.org 开始,但我猜字距调整和大小计算的算法并不简单。

于 2008-12-18T22:47:02.173 回答
4

如果您安装了 ImageMagick,您可以从命令行访问此信息。

$ convert xc: -font ./.fonts/HelveticaRoundedLTStd-Bd.otf  -pointsize 24 -debug annotate -annotate 0 'MyTestString' null: 2>&1
2010-11-02T19:17:48+00:00 0:00.010 0.010u 6.6.5 Annotate convert[22496]: annotate.c/RenderFreetype/1155/Annotate
  Font ./.fonts/HelveticaRoundedLTStd-Bd.otf; font-encoding none; text-encoding none; pointsize 24
2010-11-02T19:17:48+00:00 0:00.010 0.010u 6.6.5 Annotate convert[22496]: annotate.c/GetTypeMetrics/736/Annotate
  Metrics: text: MyTestString; width: 157; height: 29; ascent: 18; descent: -7; max advance: 24; bounds: 0,-5  20,17; origin: 158,0; pixels per em: 24,24; underline position: -1.5625; underline thickness: 0.78125
2010-11-02T19:17:48+00:00 0:00.010 0.010u 6.6.5 Annotate convert[22496]: annotate.c/RenderFreetype/1155/Annotate
  Font ./.fonts/HelveticaRoundedLTStd-Bd.otf; font-encoding none; text-encoding none; pointsize 24

要从 Ruby 中执行此操作,请使用反引号:

result = `convert xc: -font #{path_to_font} -pointsize #{size} -debug annotate -annotate 0 '#{string}' null: 2>&1`
if result =~ /width: (\d+);/
  $1
end
于 2010-11-02T19:27:47.180 回答
1

这是个好问题!

我正在尝试使用 ruby​​ 中的 pango/cairo 来解决它以进行 SVG 输出。我可能会使用 pango 来计算宽度,然后使用一个简单的 svg 元素。

我使用以下代码:

require "cairo"
require "pango"

paper = Cairo::Paper::A4_LANDSCAPE
TEXT = "Don't you love me anymore?"
def pac(surface)
        cr = Cairo::Context.new(surface)
        cr.select_font_face("Calibri",
                              Cairo::FONT_SLANT_NORMAL,
                              Cairo::FONT_WEIGHT_NORMAL)
    cr.set_font_size(12)
    extents = cr.text_extents(TEXT)
    puts extents
end

Cairo::ImageSurface.new(*paper.size("pt")) do |surface|
  cr = pac(surface)
end
于 2011-06-05T11:23:11.963 回答
0

一旦我必须在两行中显示一个字符串数组(包含即将到来的世界日、当前命名日等),将换行符放在适当的字符串之后,我必须确定字符串的累积宽度,以 Arial 打印。我打开我的文字编辑器,输入字母表,然后根据给定字体的宽度将字符分为两类:

w="023456789AÁBCDEFGHJKLMNOÓÖŐPQRSTUÚÜŰWZYaábcdeghksoóöőpqwuúüűzymn".chars.yield_self{|z| z.zip(Array.new(z.size){1.5})}.to_h.merge("1rfiíjltIÍ ".chars.yield_self{|z| z.zip(Array.new(z.size){1})}.to_h)
w.default=1
nntd=["01-21:A vallások világnapja", "01-19:Kanut", "Kenéz", "Margaréta", "Márió", "Máriusz", "Megyer", "Sára", "Szultána", "Vázsony"]
nntd.sort_by!{|z| z.chars.map{|q| w[q]}.sum}.reverse

然后我能够确定换行符的位置:

ind=nntd.collect.with_index.find_index{|z,i| nntd[0..i].join.chars.map{|q| w[q]}.sum >=nntd.join.chars.map{|q| w[q]}.sum/2}

t=[nntd[0..ind],nntd[ind+1..-1]].map{|z| z.join(",")}.join("\n")

毕竟我得到了一个不错的平衡输出,分为两行:

01-21:A vallások világnapja,01-19:Margaréta,Szultána Vázsony,Máriusz,Megyer,Kenez,Kanut,Márió,Sára

在此处输入图像描述

这样,我可以眨眼检查即将到来的世界日和当前的命名日。

于 2021-01-19T07:14:40.860 回答