Class: Cairo::Context

Inherits:
Object show all
Defined in:
lib/tagen/cairo.rb

Overview

class ImageSurface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(surface) ⇒ Context

Returns a new instance of Context.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/tagen/cairo.rb', line 225

def initialize(surface) 
  initialize_(surface)
  move_to(0,0)

  self.dsize = 13
  self.dborder = 1

  @line_cap =line_cap_
  @line_join =line_join_
  @line_dash =line_dash_
  @font_matrix =font_matrix_
  @font_options =font_options_
  @font_face =font_face_
  @operator =operator_
  @antialias =antialias_
end

Instance Attribute Details

#antialiasObject Also known as: antialias_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def antialias
  @antialias
end

#borderObject

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def border
  @border
end

#dsizeObject

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def dsize
  @dsize
end

#font_face(family, slant = :normal, weight = :normal) ⇒ Object

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def font_face
  @font_face
end

#font_matrixObject Also known as: font_matrix_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def font_matrix
  @font_matrix
end

#font_optionsObject Also known as: font_options_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def font_options
  @font_options
end

#line_capObject Also known as: line_cap_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def line_cap
  @line_cap
end

#line_dashObject

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def line_dash
  @line_dash
end

#line_joinObject Also known as: line_join_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def line_join
  @line_join
end

#operatorObject Also known as: operator_

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def operator
  @operator
end

#sizeObject

def initialize



242
243
244
# File 'lib/tagen/cairo.rb', line 242

def size
  @size
end

Instance Method Details

#arc(x, y, r, a1, a2) ⇒ Object



444
445
446
447
448
449
# File 'lib/tagen/cairo.rb', line 444

def arc(x,y,r, a1,a2)
  x,y=__pos(x,y)
  a1,a2=__angle(a1,a2)
  new_path # need new_path to clear prior-path
  arc_(x,y,r,a1,a2)
end

#arc_Object



443
# File 'lib/tagen/cairo.rb', line 443

alias arc_ arc

#arg_neg(x, y, r, a1, a2) ⇒ Object



452
453
454
455
456
457
# File 'lib/tagen/cairo.rb', line 452

def arg_neg(x,y,r, a1,a2)
  x,y=__pos(x,y)
  a1,a2=__angle(a1,a2)
  new_path
  arc_negative(x,y,r, a1,a2)
end

#bak(name, *args) ⇒ Object

options: xy x y translate scale matrix state size



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/tagen/cairo.rb', line 617

def bak(name, *args) 
  case name.to_sym
  # :xy x y
  when :xy
    x_, y_ = user_to_device(*current_point)
    yield
    move_to( *device_to_user(x_, y_))
  when :x
    x_, y_ = user_to_device(*current_point)
    yield
    x = device_to_user(x_, y_)[0]
    y = current_point[1]
    move_to(x,y)
  when :y
    x_, y_ = user_to_device(*current_point)
    yield
    x = current_point[0]
    y = device_to_user(x_, y_)[1]
    move_to(x,y)

  # :translate scale matrix
  when :translate
    xy_d = ud(0,0)
    yield
    a = xy_d.gach(&:to_s)
    translate(*a)
  when :scale
    txy_d = tud(1,1)
    yield
    scale(*txy_d.gach(&:to_s))
  when :matrix  # combine translate and scale
    xy_d = ud(0,0)
    txy_d = tud(1,1)
    yield
    scale(*txy_d.gach(&:to_s))
    translate(*xy_d.gach(&:to_s))

  # state
  when :state
    save
    yield
    restore

  # size
  when :size
    size_ = @dsize
    yield
    self.dsize = size_
  when :border
    border_ = @dborder
    yield
    self.dborder = border_

  # all: save xy size
  when :all 
    x_, y_ = user_to_device(*current_point)
    size_ = @dsize
    save
    yield
    restore
    move_to( *device_to_user(x_, y_))
    self.dsize = size_
  else
    raise ArgumentError, "bak(:#{name})"
  end
end

#btranslate(tx = 0, ty = 0) ⇒ Object



560
# File 'lib/tagen/cairo.rb', line 560

def btranslate tx=0,ty=0; translate tx.to_s, ty.to_s end

#centertext(text) ⇒ Object



343
344
345
346
347
348
# File 'lib/tagen/cairo.rb', line 343

def centertext(text)
  advanced_x = nil
  advanced_x = text_extents(text.to_s).to_a[-2]
  mvxy( ((1-advanced_x)/2.0).to_s , '0')
  showtext(text, true)
end

#circle(x, y, r) ⇒ Object



442
# File 'lib/tagen/cairo.rb', line 442

def circle(x,y,r); arc(x,y,r,0,360) end

#copy_path(flat = false) ⇒ Object



470
# File 'lib/tagen/cairo.rb', line 470

def copy_path(flat=false) flat ? copy_path_flat : copy_path_ end

#copy_path_Object



469
# File 'lib/tagen/cairo.rb', line 469

alias copy_path_ copy_path

#curveto(x1, y1, x2, y2, x3, y3) ⇒ Object



461
462
463
464
# File 'lib/tagen/cairo.rb', line 461

def curveto(x1,y1, x2,y2, x3,y3)
  x1,y1,x2,y2,x3,y3=__pos(x1,y1,x2,y2,x3,y3)
  curve_to(x1,y1, x2,y2, x3,y3)
end

#dborder=(border) ⇒ Object



491
492
493
494
495
# File 'lib/tagen/cairo.rb', line 491

def dborder=(border)
  @dborder = border
  @border = tdu(border)
  self.line_width = @border
end

#dmvx(x = nil) ⇒ Object



425
# File 'lib/tagen/cairo.rb', line 425

def dmvx(x=nil); x ? dmvxy(x, "0") : ud(*current_point)[0] end

#dmvxy(*xy) ⇒ Object



418
# File 'lib/tagen/cairo.rb', line 418

def dmvxy(*xy)  xy.empty? ? ud(*current_point) : move_to(*__dpos(*xy)) end

#dmvy(y = nil) ⇒ Object



430
# File 'lib/tagen/cairo.rb', line 430

def dmvy(y=nil) y ? dmvxy("0", y) : ud(*current_point)[1] end

#dscale(tx = 1, ty = 1, keep = true) ⇒ Object



547
# File 'lib/tagen/cairo.rb', line 547

def dscale tx=1,ty=1, keep=true; scale tx.to_s, ty.to_s, keep end

#du(x, y = nil) ⇒ Object

device to user (x,nil) (nil,y) (x,y)



593
594
595
596
597
598
599
600
601
# File 'lib/tagen/cairo.rb', line 593

def du(x,y=nil)
  if y==nil
    device_to_user(x,0)[0]
  elsif x==nil
    device_to_user(0,y)[1]
  else
    device_to_user(x,y)
  end
end

#fill(keep = true) ⇒ Object



381
382
383
384
385
386
387
388
389
# File 'lib/tagen/cairo.rb', line 381

def fill(keep=true)
  if keep
    xy = current_point
    fill_()
    move_to(*xy)
  else
    fill_()
  end
end

#fill_Object



380
# File 'lib/tagen/cairo.rb', line 380

alias fill_ fill

#font_face_Object

def initialize



251
252
253
# File 'lib/tagen/cairo.rb', line 251

def font_face
  @font_face
end

#has_pos?Boolean

Returns:

  • (Boolean)


515
# File 'lib/tagen/cairo.rb', line 515

def has_pos?; has_current_point? ? true : false end

#infill?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


351
# File 'lib/tagen/cairo.rb', line 351

def infill?(x,y); in_fill?(x,y) end

#initialize_Object



224
# File 'lib/tagen/cairo.rb', line 224

alias initialize_ initialize

#instroke?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


350
# File 'lib/tagen/cairo.rb', line 350

def instroke?(x,y); in_stroke?(x,y) end

#line(x, y, x1, y1) ⇒ Object



439
# File 'lib/tagen/cairo.rb', line 439

def line(x,y, x1,y1)  bak(:xy){mvxy(x,y); lineto(x1,y1) } end

#lineno(x, y) ⇒ Object



437
# File 'lib/tagen/cairo.rb', line 437

def lineno(x,y)   bak(:xy){lineto(x,y)} end

#lineto(x, y) ⇒ Object



435
# File 'lib/tagen/cairo.rb', line 435

def lineto(x,y)   line_to(*__pos(x,y) ) end

#mask(*args) ⇒ Object



391
392
393
394
395
396
397
# File 'lib/tagen/cairo.rb', line 391

def mask(*args)
  if args[0].class == Pattern
    mask(args[0])
  elsif args[0].class == Surface
    mask_surface(*args)
  end
end

#mvx(x = nil) ⇒ Object

def dpos; ud(*pos) end



424
# File 'lib/tagen/cairo.rb', line 424

def mvx(x=nil);  x ? mvxy(x, "0") : current_point[0] end

#mvxy(*xy) ⇒ Object

alias xy_ move_to def xy_(*xy) xy.empty? ? current_point : move_to(*xy) end



417
# File 'lib/tagen/cairo.rb', line 417

def mvxy(*xy)   xy.empty? ? current_point : move_to(*__pos(*xy)) end

#mvy(y = nil) ⇒ Object



429
# File 'lib/tagen/cairo.rb', line 429

def mvy(y=nil)  y ? mvxy("0", y ) : current_point[1] end

#newlineObject



330
331
332
# File 'lib/tagen/cairo.rb', line 330

def newline
  mvxy(0, newline_height.to_s)
end

#newline_heightObject



317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/tagen/cairo.rb', line 317

def newline_height
  out = nil; prev=nil
  text_scale_min do
    ascent, descent = font_extents.to_a
    out = ascent+descent
    prev = tud(nil, 1)
  end

  cur = tud(nil, 1)
  out = prev/cur*out
  out
end

#paint_img(img, selection = nil, rotate = 0, scale = 1) ⇒ Object

selection=[x|y=nil, w|h=nil]



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/tagen/cairo.rb', line 684

def paint_img(img, selection=nil, rotate=0, scale=1)  
  # handle arg selection
  selection ||= []
  select_x,select_y,select_w,select_h = selection
  select_x ||= 0; select_y ||= 0
  select_w ||= img.w-select_x; select_h ||= img.h-select_y
  select_w,select_h=1,1 if select_w<=0 or select_h<=0

  select_w,select_h = select_w*scale.to_f, select_h*scale.to_f
  select_x,select_y = select_x*scale.to_f, select_y*scale.to_f

  bak(:state) do

    # rotate 
    translate(surface.w/2, surface.h/2)
    rotate(rotate)
    translate(-select_w/2, -select_h/2)

    # selection [x,y,w,h]
    rectangle(0,0,select_w,select_h)
    clip
    translate(-select_x,-select_y)

    # scale
    scale(scale, scale)
    
    # paint
    set_source(img)
    paint
  end
end

#popObject



522
523
524
# File 'lib/tagen/cairo.rb', line 522

def pop
  @xy_stack.pop
end

#pop_group(to_source = false) ⇒ Object



409
410
411
412
413
# File 'lib/tagen/cairo.rb', line 409

def pop_group(to_source=false)
  group = pop_group_
  set_source(group) if to_source
  group
end

#pop_group_Object



408
# File 'lib/tagen/cairo.rb', line 408

alias pop_group_ pop_group

#pushObject

push pos



518
519
520
521
# File 'lib/tagen/cairo.rb', line 518

def push
  @xy_stack ||=[]
  @xy_stack.push(current_point)
end

#push_group(context = false) ⇒ Object



400
401
402
403
404
405
406
# File 'lib/tagen/cairo.rb', line 400

def push_group(context=false)
  if context
    push_group_widht_context(context)
  else
    push_group_
  end
end

#push_group_Object



399
# File 'lib/tagen/cairo.rb', line 399

alias push_group_ push_group

#rdmvx(x) ⇒ Object



427
# File 'lib/tagen/cairo.rb', line 427

def rdmvx x;  rdmvxy(x, 0) end

#rdmvxy(x, y) ⇒ Object



420
# File 'lib/tagen/cairo.rb', line 420

def rdmvxy(x,y)   move_to(*__tpos(x,y))  end

#rectangle(x, y, w, h) ⇒ Object



806
# File 'lib/tagen/cairo.rb', line 806

def rectangle(x,y,w,h)  rectangle_(*__pos(x,y), *__du(w,h) ) end

#rectangle_Object

rectangle with rounded support



805
# File 'lib/tagen/cairo.rb', line 805

alias rectangle_ rectangle

#rline(x, y, x1, y1) ⇒ Object



440
# File 'lib/tagen/cairo.rb', line 440

def rline(x,y, x1,y1) bak(:xy){rmvxy(x,y); rlineto(x1,y1)} end

#rlineno(x, y) ⇒ Object



438
# File 'lib/tagen/cairo.rb', line 438

def rlineno(x,y)  bak(:xy){rlineto(x,y)} end

#rlineto(x, y) ⇒ Object



436
# File 'lib/tagen/cairo.rb', line 436

def rlineto(x,y)  line_to(*__rpos(x,y)) end

#rmvdy(y) ⇒ Object



432
# File 'lib/tagen/cairo.rb', line 432

def rmvdy y;  rdmvxy(0, y) end

#rmvx(x) ⇒ Object



426
# File 'lib/tagen/cairo.rb', line 426

def rmvx x;   rmvxy(x, 0) end

#rmvxy(x, y) ⇒ Object



419
# File 'lib/tagen/cairo.rb', line 419

def rmvxy(x,y)    move_to(*__rpos(x,y)) end

#rmvy(y) ⇒ Object



431
# File 'lib/tagen/cairo.rb', line 431

def rmvy y;   rmvxy(0, y) end

#rotate(angle) ⇒ Object



528
# File 'lib/tagen/cairo.rb', line 528

def rotate(angle); rotate_(__angle(angle)) end

#rotate_Object



527
# File 'lib/tagen/cairo.rb', line 527

alias rotate_ rotate

#rrectangle(x, y, w, h) ⇒ Object



807
# File 'lib/tagen/cairo.rb', line 807

def rrectangle(x,y,w,h) rectangle_(*__rpos(x,y), *__du(w,h) ) end

#scale(*args) ⇒ Object

args@ tx,ty,keep=true keep size border



533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/tagen/cairo.rb', line 533

def scale(*args)
  if args.empty?
    tdu 1,1
  else
    tx, ty, keep = args
    keep ||= true
    tx,ty = __tdu(tx,ty)
    scale_(tx,ty)
    if keep
      self.dsize=@dsize
      self.dborder=@dborder
    end
  end
end

#scale_Object



530
# File 'lib/tagen/cairo.rb', line 530

alias scale_ scale

#sep(width, border = "1") ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/tagen/cairo.rb', line 334

def sep(width, border="1")
  mvxy(0, "0")
  line("0","0", width, "0")
  bak(:border) do
    self.border = border
    stroke
  end
end

#set_source(source) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/tagen/cairo.rb', line 354

def set_source(source)
  # [r,g,b,a=1.0] Pattern Surface
  # "#rgb[a]", "name"  (set_source_color)
  # Pixbuf             (set_source_pixbuf)
  case 
  when String===source
    set_source_color(source)
  when Array===source, source.kind_of?(Pattern), source.kind_of?(Surface)
    set_source_(source)
  else
    set_source_pixbuf(*args)
  end
end

#set_source_Object



353
# File 'lib/tagen/cairo.rb', line 353

alias set_source_ set_source

#showtext(text, newline = false) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/tagen/cairo.rb', line 307

def showtext(text, newline=false)
  text_scale_min do
    ascent, descent = font_extents.to_a
    mvxy('0', ascent.to_s)
    show_text(text.to_s)
    mvxy('0', (-ascent).to_s)
    mvxy(0, (ascent+descent).to_s) if newline
  end
end

#stroke(preverse = false, &blk) ⇒ Object



473
474
475
476
477
# File 'lib/tagen/cairo.rb', line 473

def stroke(preverse=false, &blk)
  bak(:xy) {
    stroke_(preverse, &blk)
  }
end

#stroke_Object



472
# File 'lib/tagen/cairo.rb', line 472

alias stroke_ stroke

#table(datas, oph = {}) ⇒ Object

base on scale(*wh) datas: [ [..], ]



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/tagen/cairo.rb', line 718

def table(datas, oph={}) 

  # width height
  # row_nrs col_nrs
  cols = oph[:pos]
  spacing = oph[:spacing] || [4,1]
  spacing = tdu(*spacing)

  # ==>datas row_nrs and col_nrs
  datas = [datas] if not Array===datas[0]
  row_nrs = datas.size
  col_nrs = datas[0].size

  # ==> cell_height 0.05 height
  cell_height = newline_height + spacing[1]*2
  height =  cell_height * datas.size

  ## ==>cols [0, 0.5, 1.0]
  # cell_widths [text_extents..]
  if oph[:pos] == "<" || oph[:pos] == ">"
    cell_widths = [0]
    cell_widths += datas.transpose.gach do |cols|
      cols.gach{|v| text_extents(v.to_s).to_a[-2]}.max
    end
  end

  case oph[:pos]
  when "<"
    if oph[:border]
      pos = cell_widths[0]+spacing[0]*2
      cols = [0, pos, pos+cell_widths[1]+spacing[0] ]
    else
      pos = cell_widths[0]+spacing[0]
      cols = [0, pos, pos+cell_widths[1]+spacing[0] ]
    end
  when ">"
    if oph[:border]
      pos = 1-(cell_widths[0]+cell_widths[1]+spacing[0]*4)
      cols = [ pos, pos+cell_widths[0]+spacing[0]*2, 1 ]
    else
      pos = 1-(cell_widths[0]+cell_widths[1]+spacing[0]*3)
      cols = [ pos, pos+cell_widths[0]+spacing[0], 1 ]
    end
  else
    cols ||= (0..col_nrs).map{|v| v/col_nrs.to_f}
  end
  width = cols[-1]


  # draw border 
  if oph[:border]
    rectangle cols[0], "0", cols[-1], height

    # draw rows
    bak(:xy) {
      row_nrs.each do 
        ry cell_height; rlineno cols[-1], 0
      end
    }

    # draw cols
    bak(:xy) {
      cols.gach do |v|
        x v; rlineno 0, height
      end
    }

    stroke
  end

  # draw text
  datas.gach do |rows|
    bak(:xy) {
      rxy 0, spacing[1]
      rows.gach do |row, i|
        xy cols[i], "0"
        rx spacing[0]
        showtext row
      end
    }
    ry cell_height
  end

  [width, height]
end

#tdu(x, y = nil) ⇒ Object

device to user distance (x,nil) (nil,y) (x,y)



605
606
607
608
609
610
611
612
613
# File 'lib/tagen/cairo.rb', line 605

def tdu(x,y=nil)
  if y==nil
    device_to_user_distance(x, 0)[0]
  elsif x==nil
    device_to_user_distance(0, y)[1]
  else
    device_to_user_distance(x,y)
  end
end

#text_scale_minObject



299
300
301
302
303
304
305
# File 'lib/tagen/cairo.rb', line 299

def text_scale_min
  min = tud(1,1).min
  bak(:scale) do
    dscale min, min
    yield
  end
end

#translate(*txy) ⇒ Object



550
551
552
553
554
555
556
557
558
# File 'lib/tagen/cairo.rb', line 550

def translate(*txy)
  if txy.empty?
    ud 0,0
  else
    txy_ = xy
    translate_(*__du(*txy))
    mvxy(*txy_)
  end
end

#translate_Object



549
# File 'lib/tagen/cairo.rb', line 549

alias translate_ translate

#tud(x, y = nil) ⇒ Object

user_to_device_sitance (x,nil) (nil,y) (x,y)



579
580
581
582
583
584
585
586
587
# File 'lib/tagen/cairo.rb', line 579

def tud(x,y=nil)
  if y==nil
    user_to_device_distance(x, 0)[0]
  elsif x==nil
    user_to_device_distance(0, y)[1]
  else
    user_to_device_distance(x,y)
  end
end

#ud(x, y = nil) ⇒ Object

user_to_device (x,nil) (nil,y) (x,y)



567
568
569
570
571
572
573
574
575
# File 'lib/tagen/cairo.rb', line 567

def ud(x,y=nil)
  if y==nil
    user_to_device(x,0)[0]
  elsif x==nil
    user_to_device(0,y)[1]
  else
    user_to_device(x,y)
  end
end