Newer
Older
SSSAward / lib_a4.php
@motoki miura motoki miura on 28 Feb 3 KB 2024
<?php

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;

use PhpOffice\PhpPresentation\DocumentLayout;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Slide\Background\Image;
use PhpOffice\PhpPresentation\Style\Fill;

/**
 * add Rich Text Shape テキストボックスの追加
 * A4 Paper size by UNIT_PIXEL = 794 x 1123
 */
function addRTShape(
    PhpPresentation $pres,
    Slide $slide,
    int $intw, // 幅のピクセル数
    float $ypercent, // ボックスのY座標を、パーセントで。(50だとちょうど半分、中央の高さ)
    string $mes,
    int $size,
    string $h_alignment = Alignment::HORIZONTAL_CENTER,
    string $v_alignment = Alignment::VERTICAL_AUTO
) {
    $oLayout = $pres->getLayout();
    // $intw = 600;//(int)( ($oLayout->getCX($oLayout::UNIT_PIXEL) * 0.8));
    $inth = 300; // 固定値           (int)($oLayout->getCY($oLayout::UNIT_PIXEL));
    $intoffx = (int)(($oLayout->getCX($oLayout::UNIT_PIXEL) - $intw) / 2);
    $intoffy = (int)(($oLayout->getCY($oLayout::UNIT_PIXEL) * $ypercent / 100));
    // echo $oLayout->getCX($oLayout::UNIT_PIXEL) . " " . $intw . " " . $inth . " " . $intoffx . " " . $intoffy . "\n";

    $shape2 = $slide->createRichTextShape()
        ->setWidth($intw)  // 794px
        ->setHeight($inth) // 1123px
        ->setOffsetX($intoffx)
        ->setOffsetY($intoffy);
    $shape2->getActiveParagraph()
        ->getAlignment()
        ->setHorizontal($h_alignment)
        ->setVertical($v_alignment);

    $textRun2 = $shape2->createTextRun($mes);

    $textRun2->getFont()
        ->setName('ヒラギノ明朝 Pro W3') // TODO: Windowsの場合は変更が必要かも
        ->setBold(false)
        ->setSize($size)
        ->setColor(new Color('FF000000')); // 最初のFFは不透明度。そのあとRRGGBB
    $shape2->getActiveParagraph()->setLineSpacing(120); // 行間(Line Spacing)。100だとぴったり。150だと1.5倍

}

/**
 * $ary['title']
 * $ary['authors] = [ "著者1" , "著者2", ...]
 */
function addHSJ(PhpPresentation $pres, $ary)
{
    $pres->createSlide();
    $pres->setActiveSlideIndex($pres->getSlideCount() - 1);
    $slide = $pres->getActiveSlide();

    $w600 = 600;
    addRTShape($pres, $slide, $w600, 15, $ary["eventname"], 23);
    addRTShape($pres, $slide, $w600, 20, $ary["awardname"], 26);

    $authors = ""; //著者名
    $aindex = 0;
    if (count($ary['authors']) % 2 == 1) { //著者人数が奇数
        $authors .= $ary['authors'][0] . "殿\r\n"; //まず、筆頭著者を書く
        $aindex = 1;
    }
    for ($i = $aindex; $i < count($ary['authors']); $i += 2) {
        $authors .= $ary['authors'][$i] . "殿  "; // 残りを2人ずつ書く
        $authors .= $ary['authors'][$i + 1] . "殿";
        $authors .= "\r\n";
    }
    $additionalline = 0;
    if (count($ary["authors"]) > 2) { //著者の行が増えたことを考慮し、本文をすこし下げる
        $additionalline = floor((count($ary["authors"]) - 1) / 2);
        // echo "additional " . $additionalline . "\n";
    }
    addRTShape($pres, $slide, $w600, 18.5 + $additionalline * 1.2, $authors, 24, Alignment::HORIZONTAL_CENTER, Alignment::VERTICAL_CENTER);

    $ary["content"] = str_replace("[:TITLE:]", $ary["title"], $ary["content"]);

    addRTShape($pres, $slide, 576, 36.5 + $additionalline * 2, $ary["content"], 18, Alignment::HORIZONTAL_GENERAL);

    // タイトルの長さによって、すこし下げる
    $title_additionalline = (mb_strlen($ary['title']) / 20);

    addRTShape(
        $pres,
        $slide,
        520,
        67 + $additionalline + $title_additionalline * 1.7,
        $ary['presenter'],
        18,
        Alignment::HORIZONTAL_RIGHT
    );


    return $slide; //あとで使うことがなければ、必要ない
}