<?php
function utf8_wordwrap($str, $width, $break, $cut = false) {
    $var_empty='';
    if (!$cut) {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){' . $width . ',}\b#U';
    } else {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){' . $width . '}#';
    }
    if (function_exists('mb_strlen')) {
        $str_len = mb_strlen($str, 'UTF-8');
    } else {
        $str_len = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $var_empty);
    }
    $while_what = ceil($str_len / $width);
    $i = 1;
    $return = '';
    $matches=array();
    while ($i < $while_what) {
        preg_match($regexp, $str, $matches);
        if(count($matches)===0){return $str;}
        $string = $matches[0];
        $return .= $string . $break;
        $str = substr($str, strlen($string));
        $i++;
    }
    return $return . $str;
}

function getAnswerWidth($text) {
    $size = 14; //19px=14pt;
    $angle = 0;
    $fontfile = dirname(__FILE__).DS."Droid-Sans-Fallback.ttf";
//    echo 'fontfile:' . $fontfile;
    $box = imagettfbbox($size, $angle, $fontfile, $text);
    if (!$box)
        return false;
    $min_x = min(array($box[0], $box[2], $box[4], $box[6]));
    $max_x = max(array($box[0], $box[2], $box[4], $box[6]));
    $width = ( $max_x - $min_x );
    return $width;
}
?>
<style type="text/css">
    <!--
    td{padding:2px;}
    span.cls_004{font-family:"Microsoft JhengHei Bold";font-size:14.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_004{font-family:"stsongstdlight";font-size:10.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_007{font-family:"Microsoft JhengHei Bold",serif;font-size:10.1px;color:rgb(0,92,161);font-weight:bold;font-style:normal;text-decoration: none}
    div.cls_007{font-family:"Microsoft JhengHei Bold",serif;font-size:10.1px;color:rgb(0,92,161);font-weight:bold;font-style:normal;text-decoration: none}
    span.cls_008{font-family:"stsongstdlight";font-size:14.1px;color:rgb(34,23,20);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_008{font-family:"stsongstdlight";font-size:10.1px;color:rgb(34,23,20);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_006{font-family:"Microsoft JhengHei",serif;font-size:14.1px;color:rgb(34,23,20);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_006{font-family:"Microsoft JhengHei",serif;font-size:10.1px;color:rgb(34,23,20);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_005{font-family:"Microsoft JhengHei",serif;font-size:14.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_005{font-family:"Microsoft JhengHei",serif;font-size:10.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_002{font-family:"stsongstdlight";font-size:20.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_002{font-family:"stsongstdlight";font-size:20.1px;color:rgb(0,92,161);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_003{font-family:"Microsoft JhengHei Bold",serif;font-size:20.1px;color:rgb(0,92,161);font-weight:bold;font-style:normal;text-decoration: none}
    div.cls_003{font-family:"Microsoft JhengHei Bold",serif;font-size:20.1px;color:rgb(0,92,161);font-weight:bold;font-style:normal;text-decoration: none}
    span.cls_009{font-family:"stsongstdlight";font-size:19px;color:rgb(58,93,162);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_009{font-family:"stsongstdlight";font-size:12.1px;color:rgb(58,93,162);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_010{font-family:"Microsoft JhengHei Bold",serif;font-size:19px;color:rgb(58,93,162);font-weight:bold;font-style:normal;text-decoration: none}
    div.cls_010{font-family:"Microsoft JhengHei Bold",serif;font-size:12.1px;color:rgb(58,93,162);font-weight:bold;font-style:normal;text-decoration: none}
    span.cls_011{font-family:"Microsoft JhengHei",serif;font-size:19px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_011{font-family:"Microsoft JhengHei",serif;font-size:12.1px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_012{font-family:"stsongstdlight";font-size:19px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_012{font-family:"stsongstdlight";font-size:12.1px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_014{font-family:"Microsoft JhengHei",serif;font-size:19px;color:rgb(238,42,36);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_014{font-family:"Microsoft JhengHei",serif;font-size:12.1px;color:rgb(238,42,36);font-weight:normal;font-style:normal;text-decoration: none}
    span.cls_015{font-family:"stsongstdlight";font-size:19px;color:rgb(238,42,36);font-weight:normal;font-style:normal;text-decoration: none}
    div.cls_015{font-family:"stsongstdlight";font-size:12.1px;color:rgb(238,42,36);font-weight:normal;font-style:normal;text-decoration: none}
    -->
</style>
<page format="A4" backleft="70px" backright="70px" backtop="190px" backbottom="70px">
    <page_header>
        <div style="margin-top:120px;margin-left:67px;width:595px;">
            <img src="<?= $imglogo ?>" width=200 style="padding-top:2em;padding-left:1.5em;float:left;">
            <div id="authorinfo">
                <div style="margin-left:340.54px;padding-top:1.5em;line-height:1.4;" class="cls_004">
                    <span class="cls_004">太子HQ分校</span>
                </div>
                <div style="margin-left:326.54px;line-height:1.4;" class="cls_008"><span class="cls_008">熱線 : 3401 9669</span></div>
                <div style="margin-left:264.78px;line-height:1.4;" class="cls_008"><span class="cls_008">青衣美景花園平台75號舖</span></div>
                <div style="margin-left:292.78px;line-height:1.4;" class="cls_004"><span class="cls_004">列印日期: </span><span class="cls_006"><?= $date ?></span></div>
            </div>
        </div>
    </page_header>
    <page_footer>[[page_cu]]/[[page_nb]]</page_footer>
    <div style="padding-left:5px;padding-top:10px;" class="cls_009"><span class="cls_009">練習題目</span><span class="cls_010">（</span><span class="cls_009">共</span><span class="cls_010"><?= count($questions) ?></span><span class="cls_009">題</span><span class="cls_010">）</span></div>
    <hr style="width:90%;margin:0 2em auto;border:1px solid lightblue;">
    <?php foreach ($questions as $k => $question): ?>
        <?php
        $answer1 = trim($question['answer1']);
        $a1width = getAnswerWidth($answer1);
        $answer2 = trim($question['answer2']);
        $a2width = getAnswerWidth($answer2);
        $answer3 = trim($question['answer3']);
        $a3width = getAnswerWidth($answer3);
        $answer4 = trim($question['answer4']);
        $a4width = getAnswerWidth($answer4);
        $correct = str_replace(array('1','2','3','4'),array('A','B','C','D'), trim($question['correct']));
        ?>
        <div class="questionblock" style="padding-top:30px;">                    
            <div class="cls_011"><span class="cls_011"><?= ($k + 1) ?>. </span><span class="cls_012"><?= $question['question'] ?></span></div>

            <div style='margin-left:20.84px;padding-top:5px;' class="cls_011">

                <?php if ($a1width >= 300 && $a2width < 300): ?><?php $answer1 = utf8_wordwrap($answer1, 31, "<br>\n", false); ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">A. </span><span class="cls_012"><?= $answer1 ?></span></td></tr><tr><td><span class="cls_011">B. </span><span class="cls_012"><?= $answer2 ?></span></td></tr></table>
                <?php elseif ($a1width >= 300 && $a2width >= 300): ?><?php $answer1 = utf8_wordwrap($answer1, 31, "<br>\n", false);
            $answer2 = utf8_wordwrap($answer2, 31, "<br>\n", false);
                    ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">A. </span><span class="cls_012"><?= $answer1 ?></span></td></tr><tr><td><span class="cls_011">B. </span><span class="cls_012"><?= $answer2 ?></span></td></tr></table>
                <?php elseif ($a1width < 300 && $a2width >= 300): ?><?php $answer2 = utf8_wordwrap($answer2, 31, "<br>\n", false); ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">A. </span><span class="cls_012"><?= $answer1 ?></span></td></tr><tr><td><span class="cls_011">B. </span><span class="cls_012"><?= $answer2 ?></span></td></tr></table>
                <?php else: ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">A. </span><span class="cls_012"><?= $answer1 ?></span></td><td><span class="cls_011">B. </span><span class="cls_012"><?= $answer2 ?></span></td></tr></table>
    <?php endif; ?>


            </div>

            <div style='margin-left:20.84px;padding-top:5px;' class="cls_011">
                <?php if ($a3width >= 300 && $a4width < 300): ?><?php $answer3 = utf8_wordwrap($answer3, 31, "<br>\n", false); ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">C. </span><span class="cls_012"><?= $answer3 ?></span></td></tr><tr><td><span class="cls_011">D. </span><span class="cls_012"><?= $answer4 ?></span></td></tr></table>
                <?php elseif ($a3width >= 300 && $a4width >= 300): ?><?php $answer3 = utf8_wordwrap($answer3, 31, "<br>\n", false);
            $answer4 = utf8_wordwrap($answer4, 31, "<br>\n", false); ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">C. </span><span class="cls_012"><?= $answer3 ?></span></td></tr><tr><td><span class="cls_011">D. </span><span class="cls_012"><?= $answer4 ?></span></td></tr></table>
                <?php elseif ($a3width < 300 && $a4width >= 300): ?><?php $answer4 = utf8_wordwrap($answer4, 31, "<br>\n", false); ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">C. </span><span class="cls_012"><?= $answer3 ?></span></td></tr><tr><td><span class="cls_011">D. </span><span class="cls_012"><?= $answer4 ?></span></td></tr></table>
                <?php else: ?>
                    <table><tr><td style='width:300px;'><span class="cls_011">C. </span><span class="cls_012"><?= $answer3 ?></span></td><td><span class="cls_011">D. </span><span class="cls_012"><?= $answer4 ?></span></td></tr></table>
    <?php endif; ?>
            </div>
            <?php if ($showcorrect): ?>
                <div class="cls_014" style='margin-left:10.84px;padding-top:13px;'><span class="cls_014">（</span><span class="cls_015">答案</span><span class="cls_014">：<?=$correct ?>）</span></div> 
        <?php endif; ?>
        </div>
<?php endforeach; ?>
</page>