<commit_before># frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Section' do
  it 'should apply font size according to section level' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title

    == Level 1

    === Level 2

    section content

    == Back To Level 1
    EOS

    expected_text = [
      ['Document Title', 27],
      ['Level 1', 22],
      ['Level 2', 18],
      ['section content', 10.5],
      ['Back To Level 1', 22],
    ]
    (expect pdf.text.map {|it| it.values_at :string, :font_size }).to eql expected_text
  end

  it 'should render section titles in bold by default' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title

    == Level 1

    === Level 2

    section content

    == Back To Level 1
    EOS

    expected_text = [
      ['Document Title', 'NotoSerif-Bold'],
      ['Level 1', 'NotoSerif-Bold'],
      ['Level 2', 'NotoSerif-Bold'],
      ['section content', 'NotoSerif'],
      ['Back To Level 1', 'NotoSerif-Bold'],
    ]
    (expect pdf.text.map {|it| it.values_at :string, :font_name }).to eql expected_text
  end

  it 'should allow theme to set font family for headings' do
    pdf_theme = {
      heading_font_family: 'Helvetica',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    = Document Title

    == Helvetica-Bold

    Noto Serif
    EOS

    heading_text = pdf.find_unique_text 'Helvetica-Bold'
    (expect heading_text[:font_name]).to eql 'Helvetica-Bold'
    body_text = pdf.find_unique_text 'Noto Serif'
    (expect body_text[:font_name]).to eql 'NotoSerif'
  end

  it 'should allow theme to set font family for headings per heading level' do
    pdf_theme = {
      heading_font_family: 'Helvetica',
      heading_h3_font_family: 'Times-Roman',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    = Document Title

    == Helvetica-Bold

    === Times-Bold
    EOS

    h2_text = pdf.find_unique_text 'Helvetica-Bold'
    (expect h2_text[:font_name]).to eql 'Helvetica-Bold'
    h3_text = pdf.find_unique_text 'Times-Bold'
    (expect h3_text[:font_name]).to eql 'Times-Bold'
  end

  it 'should allow theme to set font kerning for headings' do
    input = <<~'EOS'
    == Wave__|__

    content
    EOS

    pdf_theme = { heading_text_transform: 'uppercase' }
    pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
    with_kerning_position = (pdf.find_unique_text '|')[:x]

    pdf_theme[:heading_font_kerning] = 'none'
    pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
    without_kerning_position = (pdf.find_unique_text '|')[:x]

    (expect with_kerning_position).to be < without_kerning_position
  end

  it 'should add text formatting styles to styles defined in theme' do
    pdf = to_pdf <<~'EOS', pdf_theme: { heading_font_style: 'bold' }, analyze: true
    == Get Started _Quickly_
    EOS

    text = pdf.text
    (expect text).to have_size 2
    (expect text[0][:font_name]).to eql 'NotoSerif-Bold'
    (expect text[1][:font_name]).to eql 'NotoSerif-BoldItalic'
  end

  it 'should allow theme to set font kerning per heading level' do
    pdf_theme = {
      heading_text_transform: 'uppercase',
      heading_h2_font_size: 22,
      heading_h3_font_size: 22,
      heading_h3_font_kerning: 'none',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    == Wave__|__

    === Wave__|__
    EOS

    kerning_positions = (pdf.find_text '|').map {|it| it[:x] }

    (expect kerning_positions[0]).to be < kerning_positions[1]<commit_msg>fix order of font kerning tests in section context<commit_after># frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor::PDF::Converter - Section' do
  it 'should apply font size according to section level' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title

    == Level 1

    === Level 2

    section content

    == Back To Level 1
    EOS

    expected_text = [
      ['Document Title', 27],
      ['Level 1', 22],
      ['Level 2', 18],
      ['section content', 10.5],
      ['Back To Level 1', 22],
    ]
    (expect pdf.text.map {|it| it.values_at :string, :font_size }).to eql expected_text
  end

  it 'should render section titles in bold by default' do
    pdf = to_pdf <<~'EOS', analyze: true
    = Document Title

    == Level 1

    === Level 2

    section content

    == Back To Level 1
    EOS

    expected_text = [
      ['Document Title', 'NotoSerif-Bold'],
      ['Level 1', 'NotoSerif-Bold'],
      ['Level 2', 'NotoSerif-Bold'],
      ['section content', 'NotoSerif'],
      ['Back To Level 1', 'NotoSerif-Bold'],
    ]
    (expect pdf.text.map {|it| it.values_at :string, :font_name }).to eql expected_text
  end

  it 'should allow theme to set font family for headings' do
    pdf_theme = {
      heading_font_family: 'Helvetica',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    = Document Title

    == Helvetica-Bold

    Noto Serif
    EOS

    heading_text = pdf.find_unique_text 'Helvetica-Bold'
    (expect heading_text[:font_name]).to eql 'Helvetica-Bold'
    body_text = pdf.find_unique_text 'Noto Serif'
    (expect body_text[:font_name]).to eql 'NotoSerif'
  end

  it 'should allow theme to set font family for headings per heading level' do
    pdf_theme = {
      heading_font_family: 'Helvetica',
      heading_h3_font_family: 'Times-Roman',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    = Document Title

    == Helvetica-Bold

    === Times-Bold
    EOS

    h2_text = pdf.find_unique_text 'Helvetica-Bold'
    (expect h2_text[:font_name]).to eql 'Helvetica-Bold'
    h3_text = pdf.find_unique_text 'Times-Bold'
    (expect h3_text[:font_name]).to eql 'Times-Bold'
  end

  it 'should allow theme to set font kerning for headings' do
    input = <<~'EOS'
    == Wave__|__

    content
    EOS

    pdf_theme = { heading_text_transform: 'uppercase' }
    pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
    with_kerning_position = (pdf.find_unique_text '|')[:x]

    pdf_theme[:heading_font_kerning] = 'none'
    pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
    without_kerning_position = (pdf.find_unique_text '|')[:x]

    (expect with_kerning_position).to be < without_kerning_position
  end

  it 'should allow theme to set font kerning per heading level' do
    pdf_theme = {
      heading_text_transform: 'uppercase',
      heading_h2_font_size: 22,
      heading_h3_font_size: 22,
      heading_h3_font_kerning: 'none',
    }
    pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
    == Wave__|__

    === Wave__|__
    EOS

    kerning_positions = (pdf.find_text '|').map {|it| it[:x] }

    (expect kerning_positions[0]).to be < kerning_positions[1]
  end

  it 'should add text formatting styles to styles defined in theme' do
    pdf = to_pdf <<~'EOS', pdf_theme: { heading_font_style: 'bold' }, analyze: true
    == Get Started _Quickly_
    EOS

    text = pdf.text
    (expect text).to have_size 2
    (expect text[0][:font_name]).to eql 'NotoSerif-Bold'
    (expect text[1][:font_name]).to eql 'NotoSerif-BoldItalic'
{'fraction_non_alphanumeric': 0.11901103506076267, 'fraction_numerical': 0.011174745076127951, 'mean_word_length': 3.098454493417287, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4471708', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2470, 'n_words': 677}