Class: Prune::Fonts::BaseJa

Inherits:
Base
  • Object
show all
Defined in:
lib/prune/fonts/base_ja.rb

Overview

Base class for Japanese fonts.

Direct Known Subclasses

MsGothic, MsMincho, MsPGothic, MsPMincho, MsPrGothic, MsUiGothic

Constant Summary collapse

HALF_SIZED_CHARS =
(
# CID:1 to 95.
%W[
  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4
  5 6 7 8 9 : ; < = > ? @ A B C D E F G H
  I J K L M N O P Q R S T U V W X Y Z \[ \\
  \] ^ _ ` a b c d e f g h i j k l m n o p
  q r s t u v w x y z { | } ~
] + [' ']).collect{|char| char.toutf16}

Instance Attribute Summary

Attributes inherited from Base

#font_name

Instance Method Summary collapse

Methods inherited from Base

bold?, #encoding, font_name, #font_sym, italic?, #name, #reference

Methods included from PObjects

pa, pd, ph, pl, pn, ps

Constructor Details

#initialize(document) ⇒ BaseJa

Returns a new instance of BaseJa.



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
# File 'lib/prune/fonts/base_ja.rb', line 20

def initialize(document)
  super(document)
  # Font descriptor element.
  @descriptor_element = FontDescriptor.new(
    document,
    pd(
      pn(:Ascent) => 859,
      pn(:Descent) => -140,
      pn(:CapHeight) => 769,
      pn(:MissingWidth) => 500,
      pn(:FontBBox) => pa(0, -136, 1000, 859)))
  # Font widths.
  w = pa(
    # Widths for fonts from CID:1 to 95 should be half sized.
    1, pa(
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500),
    # Widths for fonts from CID:327 to 389 should be half sized.
    327, pa(
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
      500, 500, 500))
  # Font body element.
  @body_element = Font.new(
    document,
    pd(
      pn(:Subtype) => pn(:CIDFontType0),
      pn(:CIDSystemInfo) => pd(
        pn(:Registry) => pl("Adobe"),
        pn(:Ordering) => pl("Japan1"),
        pn(:Supplement) => 2),
      pn(:FontDescriptor) => @descriptor_element.reference,
      # Default width.
      pn(:DW) => 1000,
      # Default width for vertical writing.
      pn(:DW2) => pa(880, -1000),
      # Widths.
      pn(:W) => w))
  # Main element.
  @main_element = Font.new(
    document,
    pd(
      pn(:Subtype) => pn(:Type0),
      pn(:Encoding) => pn("UniJIS-UCS2-H"),
      pn(:DescendantFonts) => pa(@body_element.reference)))
end

Instance Method Details

#decode(string) ⇒ Object

Decode string.



80
81
82
# File 'lib/prune/fonts/base_ja.rb', line 80

def decode(string)
  ph(string.toutf16.unpack("C*").collect{|c| "%02X" % c}.join)
end

#width(string, font_size) ⇒ Object

Get width of the text.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/prune/fonts/base_ja.rb', line 85

def width(string, font_size)
  bytes = string.toutf16.bytes
  width = 0
  bytes.each_slice(2) do |char_bytes|
    char = char_bytes.pack("C2")
    if HALF_SIZED_CHARS.include?(char)
      width += 500
    else
      width += 1000
    end
  end
  width * font_size / 1000
end