Class: Input::LineParser
- Inherits:
-
Object
- Object
- Input::LineParser
- Defined in:
- mygame/lib/sprite_kit/vendor/input.rb
Instance Method Summary collapse
- #find_word_breaks(text) ⇒ Object
-
#initialize(word_wrap_chars, crlf_chars, font_style:) ⇒ LineParser
constructor
A new instance of LineParser.
- #perform_word_wrap(text, width, first_line_number = 0, first_line_start = 0, font_style = @font_style) ⇒ Object
Constructor Details
#initialize(word_wrap_chars, crlf_chars, font_style:) ⇒ LineParser
Returns a new instance of LineParser.
359 360 361 362 363 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 359 def initialize(word_wrap_chars, crlf_chars, font_style:) @word_wrap_chars = word_wrap_chars @crlf_chars = crlf_chars @font_style = font_style end |
Instance Method Details
#find_word_breaks(text) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 365 def find_word_breaks(text) # @word_chars = params[:word_chars] || ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['_', '-'] # @punctuation_chars = params[:punctuation_chars] || %w[! % , . ; : ' " ` ) \] } * &] # @crlf_chars = ["\r", "\n"] # @word_wrap_chars = @word_chars + @punctuation_chars words = [] word = '' index = -1 length = text.length mode = :leading_white_space while (index += 1) < length # mode = find a word-like thing case mode when :leading_white_space if text[index].strip == '' # leading white-space if @crlf_chars.include?(text[index]) # TODO: prolly need to replace \r\n with \n up front words << word word = "\n" else word << text[index] # TODO: consider how to render TAB, maybe convert TAB into 4 spaces? end else word << text[index] mode = :word_wrap_chars end when :word_wrap_chars # TODO: consider smarter handling. "something!)something" would be considered a word right now, theres an extra step needed if @word_wrap_chars.include?(text[index]) word << text[index] elsif @crlf_chars.include?(text[index]) words << word word = "\n" mode = :leading_white_space else word << text[index] mode = :trailing_white_space end when :trailing_white_space if text[index].strip == '' # trailing white-space if @crlf_chars.include?(text[index]) words << word word = "\n" # converting all new line chars to \n mode = :leading_white_space else word << text[index] # TODO: consider how to render TAB, maybe convert TAB into 4 spaces? end else words << word word = text[index] mode = :word_wrap_chars end end end words << word end |
#perform_word_wrap(text, width, first_line_number = 0, first_line_start = 0, font_style = @font_style) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 421 def perform_word_wrap(text, width, first_line_number = 0, first_line_start = 0, font_style = @font_style) @font_style = font_style words = find_word_breaks(text) lines = LineCollection.new line = '' i = -1 le = words.length while (i += 1) < le word = words[i] if word == "\n" unless line == '' lines << Line.new(lines.length + first_line_number, first_line_start, line, false, @font_style) first_line_start = lines.last.end end line = word else w = @font_style.string_width((line + word).rstrip) if w > width unless line == '' lines << Line.new(lines.length + first_line_number, first_line_start, line, true, @font_style) first_line_start = lines.last.end end # break up long words w = @font_style.string_width(word.rstrip) while w > width r = word.length - 1 l = 0 m = r.idiv(2) w = @font_style.string_width(word[0, m].rstrip) loop do if w == width # Whoa, add this lines << Line.new(lines.length + first_line_number, first_line_start, word[0, m], true, @font_style) first_line_start = lines.last.end word = word[m, word.length] break elsif w < width if r - l <= 1 lines << Line.new(lines.length + first_line_number, first_line_start, word[0, r], true, @font_style) first_line_start = lines.last.end word = word[r, word.length] break end # go right l = m + 1 m = (l + r).idiv(2) elsif w > width if r - l <= 1 lines << Line.new(lines.length + first_line_number, first_line_start, word[0, l], true, @font_style) first_line_start = lines.last.end word = word[l, word.length] break end # go left r = m - 1 m = (l + r).idiv(2) end w = @font_style.string_width(word[0, m].rstrip) end w = @font_style.string_width(word.rstrip) end line = word elsif word.start_with?("\n") unless line == '' lines << Line.new(lines.length + first_line_number, first_line_start, line, false, @font_style) first_line_start = lines.last.end end line = word else line << word end end end lines << Line.new(lines.length + first_line_number, first_line_start, line, false, @font_style) end |